Skip to content

Commit

Permalink
Make PaginatedResource’s bodyHandler async
Browse files Browse the repository at this point in the history
Preparation for #1293 (making ICipher.decrypt asynchronous).
  • Loading branch information
lawrence-forooghian committed Jun 1, 2023
1 parent 4e61b5e commit 096a948
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/common/lib/client/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class Channel extends EventEmitter {
Utils.mixin(headers, rest.options.headers);

const options = this.channelOptions;
new PaginatedResource(rest, this.basePath + '/messages', headers, envelope, function (
new PaginatedResource(rest, this.basePath + '/messages', headers, envelope, async function (
body: any,
headers: Record<string, string>,
unpacked?: boolean
Expand Down
43 changes: 25 additions & 18 deletions src/common/lib/client/paginatedresource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ErrorInfo, { IPartialErrorInfo } from '../types/errorinfo';
import { PaginatedResultCallback } from '../../types/utils';
import Rest from './rest';

export type BodyHandler = (body: unknown, headers: Record<string, string>, packed?: boolean) => any;
export type BodyHandler = (body: unknown, headers: Record<string, string>, packed?: boolean) => Promise<any>;

function getRelParams(linkUrl: string) {
const urlMatch = linkUrl.match(/^\.\/(\w+)\?(.*)$/);
Expand Down Expand Up @@ -149,25 +149,32 @@ class PaginatedResource {
callback?.(err);
return;
}
let items, linkHeader, relParams;
try {
items = this.bodyHandler(body, headers || {}, unpacked);
} catch (e) {
/* If we got an error, the failure to parse the body is almost certainly
* due to that, so callback with that in preference over the parse error */
callback?.(err || e);
return;
}

if (headers && (linkHeader = headers['Link'] || headers['link'])) {
relParams = parseRelLinks(linkHeader);
}
const handleBody = async () => {
let items, linkHeader, relParams;

if (this.useHttpPaginatedResponse) {
callback(null, new HttpPaginatedResponse(this, items, headers || {}, statusCode as number, relParams, err));
} else {
callback(null, new PaginatedResult(this, items, relParams));
}
try {
items = await this.bodyHandler(body, headers || {}, unpacked);
} catch (e) {
/* If we got an error, the failure to parse the body is almost certainly
* due to that, so throw that in preference over the parse error */
throw err || e;
}

if (headers && (linkHeader = headers['Link'] || headers['link'])) {
relParams = parseRelLinks(linkHeader);
}

if (this.useHttpPaginatedResponse) {
return new HttpPaginatedResponse(this, items, headers || {}, statusCode as number, relParams, err);
} else {
return new PaginatedResult(this, items, relParams);
}
};

handleBody()
.then((result) => callback(null, result))
.catch((err) => callback(err, null));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/common/lib/client/presence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Presence extends EventEmitter {
Utils.mixin(headers, rest.options.headers);

const options = this.channel.channelOptions;
new PaginatedResource(rest, this.basePath, headers, envelope, function (
new PaginatedResource(rest, this.basePath, headers, envelope, async function (
body: any,
headers: Record<string, string>,
unpacked?: boolean
Expand Down Expand Up @@ -84,7 +84,7 @@ class Presence extends EventEmitter {
Utils.mixin(headers, rest.options.headers);

const options = this.channel.channelOptions;
new PaginatedResource(rest, this.basePath + '/history', headers, envelope, function (
new PaginatedResource(rest, this.basePath + '/history', headers, envelope, async function (
body: any,
headers: Record<string, string>,
unpacked?: boolean
Expand Down
6 changes: 3 additions & 3 deletions src/common/lib/client/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class DeviceRegistrations {

Utils.mixin(headers, rest.options.headers);

new PaginatedResource(rest, '/push/deviceRegistrations', headers, envelope, function (
new PaginatedResource(rest, '/push/deviceRegistrations', headers, envelope, async function (
body: any,
headers: Record<string, string>,
unpacked?: boolean
Expand Down Expand Up @@ -286,7 +286,7 @@ class ChannelSubscriptions {

Utils.mixin(headers, rest.options.headers);

new PaginatedResource(rest, '/push/channelSubscriptions', headers, envelope, function (
new PaginatedResource(rest, '/push/channelSubscriptions', headers, envelope, async function (
body: any,
headers: Record<string, string>,
unpacked?: boolean
Expand Down Expand Up @@ -334,7 +334,7 @@ class ChannelSubscriptions {

if (rest.options.pushFullWait) Utils.mixin(params, { fullWait: 'true' });

new PaginatedResource(rest, '/push/channels', headers, envelope, function (
new PaginatedResource(rest, '/push/channels', headers, envelope, async function (
body: unknown,
headers: Record<string, string>,
unpacked?: boolean
Expand Down
2 changes: 1 addition & 1 deletion src/common/lib/client/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class Rest {
path,
headers,
envelope,
function (resbody: unknown, headers: Record<string, string>, unpacked?: boolean) {
async function (resbody: unknown, headers: Record<string, string>, unpacked?: boolean) {
return Utils.ensureArray(unpacked ? resbody : decoder(resbody as string & Buffer));
},
/* useHttpPaginatedResponse: */ true
Expand Down

0 comments on commit 096a948

Please sign in to comment.