Skip to content

Commit

Permalink
Split up GET request (#109)
Browse files Browse the repository at this point in the history
Co-authored-by: Automated Version Bump <[email protected]>
  • Loading branch information
motechFR and Automated Version Bump authored Aug 29, 2023
1 parent c9c24ae commit 373fdcc
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@charmverse/core",
"version": "0.3.13",
"version": "0.3.14-rc-compute-page-pe.1",
"description": "Core API for Charmverse",
"type": "commonjs",
"types": "./dist/cjs/index.d.ts",
Expand Down
2 changes: 2 additions & 0 deletions src/lib/permissions/clients/abstractApiClient.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export abstract class AbstractPermissionsApiClient {

readonly authKey: string;

readonly getRequestBatchSize = 1000;

get jsonHeaders(): HeadersInit {
const headers = new Headers({});
headers.append('Content-Type', 'application/json');
Expand Down
25 changes: 23 additions & 2 deletions src/lib/permissions/pages/client/pagePermissionsHttpClient.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { asyncSeries } from 'lib/utilities/array';
import chunk from 'lodash/chunk';

import { DELETE, GET, POST, PUT } from '../../../http';
import type {
PageMeta,
Expand Down Expand Up @@ -28,8 +31,26 @@ export class PagePermissionsHttpClient extends AbstractPermissionsApiClient impl
return GET(`${this.prefix}/compute-page-permissions`, request);
}

bulkComputePagePermissions(request: BulkPagePermissionCompute): Promise<BulkPagePermissionFlags> {
return GET(`${this.prefix}/bulk-compute-page-permissions`, request);
async bulkComputePagePermissions(request: BulkPagePermissionCompute): Promise<BulkPagePermissionFlags> {
const pageIds = request.pageIds ?? [];

const chunkedPageIds = chunk(pageIds, this.getRequestBatchSize).map(
(pageIdChunk) => ({ pageIds: pageIdChunk, userId: request.userId } as BulkPagePermissionCompute)
);

const computedResult = await asyncSeries(chunkedPageIds, (chunkedRequest: BulkPagePermissionCompute) =>
GET<BulkPagePermissionFlags>(`${this.prefix}/bulk-compute-page-permissions`, chunkedRequest)
).then((results) => {
const mergedResults: BulkPagePermissionFlags = {};

for (const computedPagePermissions of results) {
Object.assign(mergedResults, computedPagePermissions);
}

return mergedResults;
});

return computedResult;
}

getAccessiblePages(request: PagesRequest): Promise<PageMeta[]> {
Expand Down
13 changes: 13 additions & 0 deletions src/lib/utilities/__tests__/array.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { asyncSeries } from '../array';

describe('Async utils', () => {
it('asyncSeries() returns resolved promises', async () => {
return asyncSeries([1, 2, 3], (num) => Promise.resolve(num + 1)).then((result) => {
expect(result).toEqual([2, 3, 4]);
});
});

it('asyncSeries() throws an error', async () => {
await expect(asyncSeries([1, 2, 3], () => Promise.reject('foo_error'))).rejects.toEqual('foo_error');
});
});
13 changes: 13 additions & 0 deletions src/lib/utilities/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,16 @@ export function flatArrayMap<T>(obj: { [key: string]: T[] }): T[] {
return list;
}, [] as T[]);
}
// usage: await asyncSeries([1,2,3], num => Promise.resolve(num));
export function asyncSeries<T, U>(
values: U[],
asyncIterator: (val: U) => Promise<T>,
index = 0,
results: T[] = []
): Promise<T[]> {
if (index >= values.length) return Promise.resolve(results);
return asyncIterator(values[index]).then((r) => {
results.push(r);
return asyncSeries(values, asyncIterator, index + 1, results);
});
}

0 comments on commit 373fdcc

Please sign in to comment.