Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split up GET request #109

Merged
merged 6 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
}