Skip to content

Commit

Permalink
Update response for batchCheck and clientBatchCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyjames committed Jan 10, 2025
1 parent b68e18e commit 0d36543
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
16 changes: 8 additions & 8 deletions client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export type ClientBatchCheckSingleClientResponse = {
});

export interface ClientBatchCheckClientResponse {
responses: ClientBatchCheckSingleClientResponse[];
result: ClientBatchCheckSingleClientResponse[];
}

export interface ClientBatchCheckClientRequestOpts {
Expand Down Expand Up @@ -183,7 +183,7 @@ export type ClientBatchCheckSingleResponse = {
}

export interface ClientBatchCheckResponse {
responses: ClientBatchCheckSingleResponse[];
result: ClientBatchCheckSingleResponse[];
}

export interface ClientWriteRequestOpts {
Expand Down Expand Up @@ -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;
Expand All @@ -661,9 +661,9 @@ export class OpenFgaClient extends BaseAPI {
};
})
)) {
responses.push(singleCheckResponse);
result.push(singleCheckResponse);
}
return { responses };
return { result };
}


Expand Down Expand Up @@ -761,7 +761,7 @@ export class OpenFgaClient extends BaseAPI {
}
}

return { responses: results };
return { result: results };
}

/**
Expand Down Expand Up @@ -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) };
}

/**
Expand Down
4 changes: 2 additions & 2 deletions example/example1/example1.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}`);
Expand Down
2 changes: 1 addition & 1 deletion example/opentelemetry/opentelemetry.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ async function main () {
}

console.log("Calling BatcCheck")
const { responses } = await fgaClient.batchCheck({
await fgaClient.batchCheck({
checks: [
{
object: "doc:roadmap",
Expand Down
20 changes: 10 additions & 10 deletions tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 0d36543

Please sign in to comment.