From 407fbe27e683ab8fbc417e0a8c19694bae8696c8 Mon Sep 17 00:00:00 2001 From: Jim Anderson Date: Tue, 7 Jan 2025 13:24:21 -0600 Subject: [PATCH] update from latest generator --- apiModel.ts | 7 ++++--- client.ts | 21 ++++++++++----------- example/example1/package.json | 2 +- example/opentelemetry/opentelemetry.mjs | 4 ++-- telemetry/attributes.ts | 2 +- telemetry/configuration.ts | 2 +- tests/client.test.ts | 11 +++++------ tests/telemetry/attributes.test.ts | 1 + 8 files changed, 25 insertions(+), 25 deletions(-) diff --git a/apiModel.ts b/apiModel.ts index ea71374..984ea49 100644 --- a/apiModel.ts +++ b/apiModel.ts @@ -253,13 +253,13 @@ export interface CheckError { * @type {ErrorCode} * @memberof CheckError */ - inputError?: ErrorCode; + input_error?: ErrorCode; /** * * @type {InternalErrorCode} * @memberof CheckError */ - internalError?: InternalErrorCode; + internal_error?: InternalErrorCode; /** * * @type {string} @@ -585,7 +585,8 @@ export enum ErrorCode { DuplicateContextualTuple = 'duplicate_contextual_tuple', InvalidAuthorizationModel = 'invalid_authorization_model', UnsupportedSchemaVersion = 'unsupported_schema_version', - Cancelled = 'cancelled' + Cancelled = 'cancelled', + InvalidStartTime = 'invalid_start_time' } /** diff --git a/client.ts b/client.ts index 2840d57..c28f9bb 100644 --- a/client.ts +++ b/client.ts @@ -182,7 +182,6 @@ export type ClientBatchCheckSingleResponse = { error?: CheckError; } -// for server batch check export interface ClientBatchCheckResponse { responses: ClientBatchCheckSingleResponse[]; } @@ -664,11 +663,11 @@ export class OpenFgaClient extends BaseAPI { )) { responses.push(singleCheckResponse); } - return { responses }; } + private singleBatchCheck(body: BatchCheckRequest, options: ClientRequestOptsWithConsistency & ClientBatchCheckRequestOpts = {}): Promise { return this.api.batchCheck(this.getStoreId(options)!, body, options); } @@ -699,23 +698,23 @@ export class OpenFgaClient extends BaseAPI { } = options; setHeaderIfNotSet(headers, CLIENT_BULK_REQUEST_ID_HEADER, generateRandomIdWithNonUniqueFallback()); - + const correlationIdToCheck = new Map(); const transformed: BatchCheckItem[] = []; - + // Validate and transform checks for (const check of body.checks) { // Generate a correlation ID if not provided if (!check.correlationId) { check.correlationId = generateRandomIdWithNonUniqueFallback(); } - + // Ensure that correlation IDs are unique if (correlationIdToCheck.has(check.correlationId)) { throw new FgaValidationError("correlationId", "When calling batchCheck, correlation IDs must be unique"); } correlationIdToCheck.set(check.correlationId, check); - + // Transform the check into the BatchCheckItem format transformed.push({ tuple_key: { @@ -728,10 +727,10 @@ export class OpenFgaClient extends BaseAPI { correlation_id: check.correlationId, }); } - + // Split the transformed checks into batches based on maxBatchSize const batchedChecks = chunkArray(transformed, maxBatchSize); - + // Execute batch checks in parallel with a limit of maxParallelRequests const results: ClientBatchCheckSingleResponse[] = []; const batchResponses = asyncPool(maxParallelRequests, batchedChecks, async (batch: BatchCheckItem[]) => { @@ -744,7 +743,7 @@ export class OpenFgaClient extends BaseAPI { const response = await this.singleBatchCheck(batchRequest, { ...options, headers }); return response.result; }); - + // Collect the responses and associate them with their correlation IDs for await (const response of batchResponses) { if (response) { @@ -761,9 +760,9 @@ export class OpenFgaClient extends BaseAPI { } } } - + return { responses: results }; - } + } /** * Expand - Expands the relationships in userset tree format (evaluates) diff --git a/example/example1/package.json b/example/example1/package.json index e1689a8..9835006 100644 --- a/example/example1/package.json +++ b/example/example1/package.json @@ -9,7 +9,7 @@ "start": "node example1.mjs" }, "dependencies": { - "@openfga/sdk": "file:../../" + "@openfga/sdk": "^0.7.0" }, "engines": { "node": ">=16.13.0" diff --git a/example/opentelemetry/opentelemetry.mjs b/example/opentelemetry/opentelemetry.mjs index 9f87527..2414f15 100644 --- a/example/opentelemetry/opentelemetry.mjs +++ b/example/opentelemetry/opentelemetry.mjs @@ -29,7 +29,6 @@ const telemetryConfig = { }, [TelemetryMetric.HistogramRequestDuration]: { attributes: new Set([ - TelemetryAttribute.FgaClientRequestBatchCheckSize, TelemetryAttribute.HttpResponseStatusCode, TelemetryAttribute.UserAgentOriginal, TelemetryAttribute.FgaClientRequestMethod, @@ -41,6 +40,7 @@ const telemetryConfig = { }, [TelemetryMetric.HistogramQueryDuration]: { attributes: new Set([ + TelemetryAttribute.FgaClientRequestBatchCheckSize, TelemetryAttribute.HttpResponseStatusCode, TelemetryAttribute.UserAgentOriginal, TelemetryAttribute.FgaClientRequestMethod, @@ -127,7 +127,7 @@ async function main () { }, { authorizationModelId: "01JC6KPJ0CKSZ69C5Z26CYWX2N" }); - + console.log("writing tuple"); await fgaClient.write({ writes: [ diff --git a/telemetry/attributes.ts b/telemetry/attributes.ts index f713853..7da58c7 100644 --- a/telemetry/attributes.ts +++ b/telemetry/attributes.ts @@ -14,7 +14,6 @@ import { URL } from "url"; export enum TelemetryAttribute { - FgaClientRequestBatchCheckSize = "fga-client.request.batch_check_size", FgaClientRequestClientId = "fga-client.request.client_id", FgaClientRequestMethod = "fga-client.request.method", FgaClientRequestModelId = "fga-client.request.model_id", @@ -22,6 +21,7 @@ export enum TelemetryAttribute { FgaClientResponseModelId = "fga-client.response.model_id", FgaClientUser = "fga-client.user", HttpClientRequestDuration = "http.client.request.duration", + FgaClientRequestBatchCheckSize = "fga-client.request.batch_check_size", HttpHost = "http.host", HttpRequestMethod = "http.request.method", HttpRequestResendCount = "http.request.resend_count", diff --git a/telemetry/configuration.ts b/telemetry/configuration.ts index ab93da1..5de796b 100644 --- a/telemetry/configuration.ts +++ b/telemetry/configuration.ts @@ -71,7 +71,7 @@ export class TelemetryConfiguration implements TelemetryConfig { // TelemetryAttribute.HttpServerRequestDuration, // This not included by default as it has a very high cardinality which could increase costs for users - // TelemetryAttribute.FgaClientUser + // TelemetryAttribute.FgaClientUser, // TelemetryAttribute.FgaClientRequestBatchCheckSize ]); diff --git a/tests/client.test.ts b/tests/client.test.ts index 7e002da..bd88ab7 100644 --- a/tests/client.test.ts +++ b/tests/client.test.ts @@ -540,7 +540,6 @@ describe("OpenFGA Client", () => { .reply(200, { authorization_models: [], }); - expect(scope0.isDone()).toBe(false); expect(scope1.isDone()).toBe(false); expect(scope2.isDone()).toBe(false); @@ -634,7 +633,7 @@ describe("OpenFGA Client", () => { authorizationModelId: "01GAHCE4YVKPQEKZQHT2R89MQV", consistency: ConsistencyPreference.HigherConsistency, }); - + expect(scope.isDone()).toBe(true); expect(response.responses).toHaveLength(2); expect(response.responses[0].allowed).toBe(true); @@ -658,7 +657,7 @@ describe("OpenFGA Client", () => { "cor-3": { allowed: false, error: { - inputError: ErrorCode.RelationNotFound, + input_error: ErrorCode.RelationNotFound, message: "relation not found", } } @@ -707,7 +706,7 @@ describe("OpenFGA Client", () => { consistency: ConsistencyPreference.HigherConsistency, maxBatchSize: 2, }); - + expect(scope0.isDone()).toBe(true); expect(scope1.isDone()).toBe(true); expect(response.responses).toHaveLength(3); @@ -720,7 +719,7 @@ describe("OpenFGA Client", () => { expect(resp0?.request.user).toBe("user:81684243-9356-4421-8fbf-a4f8d36aa31b"); expect(resp0?.request.relation).toBe("can_read"); expect(resp0?.request.object).toBe("document"); - + expect(resp1?.allowed).toBe(false); expect(resp1?.request.user).toBe("folder:product"); expect(resp1?.request.relation).toBe("parent"); @@ -731,7 +730,7 @@ describe("OpenFGA Client", () => { expect(resp2?.request.relation).toBe("can_view"); expect(resp2?.request.object).toBe("document:9992ab2a-d83f-756d-9397-c5ed9f3cj8a4"); - expect(resp2?.error?.inputError).toBe(ErrorCode.RelationNotFound); + expect(resp2?.error?.input_error).toBe(ErrorCode.RelationNotFound); expect(resp2?.error?.message).toBe("relation not found"); }); it("should throw an error if auth fails", async () => { diff --git a/tests/telemetry/attributes.test.ts b/tests/telemetry/attributes.test.ts index 9ff2355..8116207 100644 --- a/tests/telemetry/attributes.test.ts +++ b/tests/telemetry/attributes.test.ts @@ -107,6 +107,7 @@ describe("TelemetryAttributes", () => { expect(attributes[TelemetryAttribute.FgaClientUser]).toBeUndefined(); }); + test("should create attributes from a batchCheck request body correctly", () => { const body = { authorization_model_id: "model-id",