Skip to content

Commit c587a9a

Browse files
committed
small refactor
1 parent 8e92f9b commit c587a9a

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

client.ts

+5-12
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ export class OpenFgaClient extends BaseAPI {
679679
maxBatchSize = DEFAULT_MAX_BATCH_SIZE,
680680
maxParallelRequests = DEFAULT_MAX_METHOD_PARALLEL_REQS,
681681
} = options;
682-
682+
683683
// TODO this right? Do it here?
684684
setHeaderIfNotSet(headers, CLIENT_METHOD_HEADER, "BatchCheck");
685685
setHeaderIfNotSet(headers, CLIENT_BULK_REQUEST_ID_HEADER, generateRandomIdWithNonUniqueFallback());
@@ -718,22 +718,16 @@ export class OpenFgaClient extends BaseAPI {
718718

719719
// Execute batch checks in parallel with a limit of maxParallelRequests
720720
const results: ClientBatchCheckSingleResponse[] = [];
721-
722-
const executeBatch = async (batch: BatchCheckItem[]) => {
723-
// Prepare request payload
721+
const batchResponses = asyncPool(maxParallelRequests, batchedChecks, async (batch: BatchCheckItem[]) => {
724722
const batchRequest: BatchCheckRequest = {
725723
checks: batch,
726-
authorization_model_id: options.authorizationModelId, // TODO this right here?
724+
authorization_model_id: options.authorizationModelId,
727725
consistency: options.consistency,
728726
};
729-
730-
// Make API call to execute the batch check
727+
731728
const response = await this.singleBatchCheck(batchRequest, options);
732729
return response.result;
733-
};
734-
735-
// Use asyncPool to process batches concurrently, but limit to maxParallelRequests
736-
const batchResponses = asyncPool(maxParallelRequests, batchedChecks, executeBatch);
730+
});
737731

738732
// Collect the responses and associate them with their correlation IDs
739733
for await (const response of batchResponses) {
@@ -752,7 +746,6 @@ export class OpenFgaClient extends BaseAPI {
752746
}
753747
}
754748

755-
// Return the final response in the expected format
756749
return { responses: results };
757750
}
758751

tests/client.test.ts

+30-3
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ describe("OpenFGA Client", () => {
598598
},
599599
},
600600
};
601-
const scope = nocks.singleBatchCheck(baseConfig.storeId!, mockedResponse, undefined, ConsistencyPreference.HigherConsistency);
601+
const scope = nocks.singleBatchCheck(baseConfig.storeId!, mockedResponse, undefined, ConsistencyPreference.HigherConsistency, "01GAHCE4YVKPQEKZQHT2R89MQV");
602602

603603
expect(scope.isDone()).toBe(false);
604604
const response = await fgaClient.batchCheck({
@@ -661,8 +661,8 @@ describe("OpenFGA Client", () => {
661661
},
662662
};
663663

664-
const scope0 = nocks.singleBatchCheck(baseConfig.storeId!, mockedResponse0, undefined, ConsistencyPreference.HigherConsistency);
665-
const scope1 = nocks.singleBatchCheck(baseConfig.storeId!, mockedResponse1, undefined, ConsistencyPreference.HigherConsistency);
664+
const scope0 = nocks.singleBatchCheck(baseConfig.storeId!, mockedResponse0, undefined, ConsistencyPreference.HigherConsistency, "01GAHCE4YVKPQEKZQHT2R89MQV");
665+
const scope1 = nocks.singleBatchCheck(baseConfig.storeId!, mockedResponse1, undefined, ConsistencyPreference.HigherConsistency, "01GAHCE4YVKPQEKZQHT2R89MQV");
666666

667667
expect(scope0.isDone()).toBe(false);
668668
expect(scope1.isDone()).toBe(false);
@@ -730,6 +730,33 @@ describe("OpenFGA Client", () => {
730730
expect(resp2?.error?.inputError).toBe(ErrorCode.RelationNotFound);
731731
expect(resp2?.error?.message).toBe("relation not found");
732732
});
733+
// it("should throw an error if auth fails", async () => {
734+
// const tuples = [{
735+
// user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
736+
// relation: "admin",
737+
// object: "workspace:1",
738+
// }];
739+
740+
// const scope0 = nocks.check(baseConfig.storeId!, tuples[0], defaultConfiguration.getBasePath(), {} as any,401);
741+
// const scope1 = nock(defaultConfiguration.getBasePath())
742+
// .get(`/stores/${defaultConfiguration.storeId!}/authorization-models`)
743+
// .query({ page_size: 1 })
744+
// .reply(401, {
745+
// authorization_models: [],
746+
// });
747+
// try {
748+
// await fgaClient.listRelations({
749+
// user: "user:81684243-9356-4421-8fbf-a4f8d36aa31b",
750+
// object: "workspace:1",
751+
// relations: ["admin"],
752+
// });
753+
// } catch (err) {
754+
// expect(err).toBeInstanceOf(FgaApiAuthenticationError);
755+
// } finally {
756+
// expect(scope0.isDone()).toBe(true);
757+
// expect(scope1.isDone()).toBe(false);
758+
// }
759+
// });
733760
});
734761

735762
describe("Expand", () => {

tests/helpers/nocks.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,12 @@ export const getNocks = ((nock: typeof Nock) => ({
219219
responseBody: BatchCheckResponse,
220220
basePath = defaultConfiguration.getBasePath(),
221221
consistency: ConsistencyPreference|undefined | undefined,
222+
authorizationModelId = "auth-model-id",
222223
) => {
223224
return nock(basePath)
224225
.post(`/stores/${storeId}/batch-check`, (body: BatchCheckRequest) =>
225-
body.consistency === consistency
226+
body.consistency === consistency &&
227+
body.authorization_model_id === authorizationModelId
226228
)
227229
.reply(200, responseBody);
228230
},

0 commit comments

Comments
 (0)