From 0d365435898a7e159213f80ab292baa4490d3850 Mon Sep 17 00:00:00 2001 From: Jim Anderson Date: Fri, 10 Jan 2025 12:13:17 -0600 Subject: [PATCH] Update response for batchCheck and clientBatchCheck --- client.ts | 16 ++++++++-------- example/example1/example1.mjs | 4 ++-- example/opentelemetry/opentelemetry.mjs | 2 +- tests/client.test.ts | 20 ++++++++++---------- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/client.ts b/client.ts index c28f9bb..6faf8e3 100644 --- a/client.ts +++ b/client.ts @@ -145,7 +145,7 @@ export type ClientBatchCheckSingleClientResponse = { }); export interface ClientBatchCheckClientResponse { - responses: ClientBatchCheckSingleClientResponse[]; + result: ClientBatchCheckSingleClientResponse[]; } export interface ClientBatchCheckClientRequestOpts { @@ -183,7 +183,7 @@ export type ClientBatchCheckSingleResponse = { } export interface ClientBatchCheckResponse { - responses: ClientBatchCheckSingleResponse[]; + result: ClientBatchCheckSingleResponse[]; } export interface ClientWriteRequestOpts { @@ -643,7 +643,7 @@ export class OpenFgaClient extends BaseAPI { setHeaderIfNotSet(headers, CLIENT_METHOD_HEADER, "ClientBatchCheck"); setHeaderIfNotSet(headers, CLIENT_BULK_REQUEST_ID_HEADER, generateRandomIdWithNonUniqueFallback()); - const responses: ClientBatchCheckSingleClientResponse[] = []; + const result: ClientBatchCheckSingleClientResponse[] = []; for await (const singleCheckResponse of asyncPool(maxParallelRequests, body, (tuple) => this.check(tuple, { ...options, headers }) .then(response => { (response as ClientBatchCheckSingleClientResponse)._request = tuple; @@ -661,9 +661,9 @@ export class OpenFgaClient extends BaseAPI { }; }) )) { - responses.push(singleCheckResponse); + result.push(singleCheckResponse); } - return { responses }; + return { result }; } @@ -761,7 +761,7 @@ export class OpenFgaClient extends BaseAPI { } } - return { responses: results }; + return { result: results }; } /** @@ -836,12 +836,12 @@ export class OpenFgaClient extends BaseAPI { context, })), { ...options, headers, maxParallelRequests }); - const firstErrorResponse = batchCheckResults.responses.find(response => (response as any).error); + const firstErrorResponse = batchCheckResults.result.find(response => (response as any).error); if (firstErrorResponse) { throw (firstErrorResponse as any).error; } - return { relations: batchCheckResults.responses.filter(result => result.allowed).map(result => result._request.relation) }; + return { relations: batchCheckResults.result.filter(result => result.allowed).map(result => result._request.relation) }; } /** diff --git a/example/example1/example1.mjs b/example/example1/example1.mjs index 521040e..dae177f 100644 --- a/example/example1/example1.mjs +++ b/example/example1/example1.mjs @@ -188,7 +188,7 @@ async function main () { // execute a batch check const anneCorrelationId = randomUUID(); - const { responses } = await fgaClient.batchCheck({ + const { result } = await fgaClient.batchCheck({ checks: [ { // should have access @@ -209,7 +209,7 @@ async function main () { ] }); - const anneAllowed = responses.filter(r => r.correlationId === anneCorrelationId); + const anneAllowed = result.filter(r => r.correlationId === anneCorrelationId); console.log(`Anne is allowed access to ${anneAllowed.length} documents`); anneAllowed.forEach(item => { console.log(`Anne is allowed access to ${item.request.object}`); diff --git a/example/opentelemetry/opentelemetry.mjs b/example/opentelemetry/opentelemetry.mjs index 2414f15..c29fbe4 100644 --- a/example/opentelemetry/opentelemetry.mjs +++ b/example/opentelemetry/opentelemetry.mjs @@ -101,7 +101,7 @@ async function main () { } console.log("Calling BatcCheck") - const { responses } = await fgaClient.batchCheck({ + await fgaClient.batchCheck({ checks: [ { object: "doc:roadmap", diff --git a/tests/client.test.ts b/tests/client.test.ts index bd88ab7..5e96114 100644 --- a/tests/client.test.ts +++ b/tests/client.test.ts @@ -549,8 +549,8 @@ describe("OpenFGA Client", () => { expect(scope1.isDone()).toBe(true); expect(scope2.isDone()).toBe(true); expect(scope3.isDone()).toBe(false); - expect(response.responses.length).toBe(3); - expect(response.responses.sort((a, b) => String(a._request.object).localeCompare(b._request.object))) + expect(response.result.length).toBe(3); + expect(response.result.sort((a, b) => String(a._request.object).localeCompare(b._request.object))) .toMatchObject(expect.arrayContaining([ { _request: tuples[0], allowed: true, }, { _request: tuples[1], allowed: false }, @@ -585,7 +585,7 @@ describe("OpenFGA Client", () => { const response = await fgaClient.batchCheck({ checks: [], }); - expect(response.responses.length).toBe(0); + expect(response.result.length).toBe(0); }); it("should handle single batch successfully", async () => { const mockedResponse = { @@ -635,9 +635,9 @@ describe("OpenFGA Client", () => { }); expect(scope.isDone()).toBe(true); - expect(response.responses).toHaveLength(2); - expect(response.responses[0].allowed).toBe(true); - expect(response.responses[1].allowed).toBe(false); + expect(response.result).toHaveLength(2); + expect(response.result[0].allowed).toBe(true); + expect(response.result[1].allowed).toBe(false); }); it("should split batches successfully", async () => { const mockedResponse0 = { @@ -709,11 +709,11 @@ describe("OpenFGA Client", () => { expect(scope0.isDone()).toBe(true); expect(scope1.isDone()).toBe(true); - expect(response.responses).toHaveLength(3); + expect(response.result).toHaveLength(3); - const resp0 = response.responses.find(r => r.correlationId === "cor-1"); - const resp1 = response.responses.find(r => r.correlationId === "cor-2"); - const resp2 = response.responses.find(r => r.correlationId === "cor-3"); + const resp0 = response.result.find(r => r.correlationId === "cor-1"); + const resp1 = response.result.find(r => r.correlationId === "cor-2"); + const resp2 = response.result.find(r => r.correlationId === "cor-3"); expect(resp0?.allowed).toBe(true); expect(resp0?.request.user).toBe("user:81684243-9356-4421-8fbf-a4f8d36aa31b");