Skip to content

Commit

Permalink
update from latest generator
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyjames committed Jan 8, 2025
1 parent 59c000b commit 407fbe2
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 25 deletions.
7 changes: 4 additions & 3 deletions apiModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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'
}

/**
Expand Down
21 changes: 10 additions & 11 deletions client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ export type ClientBatchCheckSingleResponse = {
error?: CheckError;
}

// for server batch check
export interface ClientBatchCheckResponse {
responses: ClientBatchCheckSingleResponse[];
}
Expand Down Expand Up @@ -664,11 +663,11 @@ export class OpenFgaClient extends BaseAPI {
)) {
responses.push(singleCheckResponse);
}

return { responses };
}



private singleBatchCheck(body: BatchCheckRequest, options: ClientRequestOptsWithConsistency & ClientBatchCheckRequestOpts = {}): Promise<BatchCheckResponse> {
return this.api.batchCheck(this.getStoreId(options)!, body, options);
}
Expand Down Expand Up @@ -699,23 +698,23 @@ export class OpenFgaClient extends BaseAPI {
} = options;

setHeaderIfNotSet(headers, CLIENT_BULK_REQUEST_ID_HEADER, generateRandomIdWithNonUniqueFallback());

const correlationIdToCheck = new Map<string, ClientBatchCheckItem>();
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: {
Expand All @@ -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[]) => {
Expand All @@ -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) {
Expand All @@ -761,9 +760,9 @@ export class OpenFgaClient extends BaseAPI {
}
}
}

return { responses: results };
}
}

/**
* Expand - Expands the relationships in userset tree format (evaluates)
Expand Down
2 changes: 1 addition & 1 deletion example/example1/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"start": "node example1.mjs"
},
"dependencies": {
"@openfga/sdk": "file:../../"
"@openfga/sdk": "^0.7.0"
},
"engines": {
"node": ">=16.13.0"
Expand Down
4 changes: 2 additions & 2 deletions example/opentelemetry/opentelemetry.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const telemetryConfig = {
},
[TelemetryMetric.HistogramRequestDuration]: {
attributes: new Set([
TelemetryAttribute.FgaClientRequestBatchCheckSize,
TelemetryAttribute.HttpResponseStatusCode,
TelemetryAttribute.UserAgentOriginal,
TelemetryAttribute.FgaClientRequestMethod,
Expand All @@ -41,6 +40,7 @@ const telemetryConfig = {
},
[TelemetryMetric.HistogramQueryDuration]: {
attributes: new Set([
TelemetryAttribute.FgaClientRequestBatchCheckSize,
TelemetryAttribute.HttpResponseStatusCode,
TelemetryAttribute.UserAgentOriginal,
TelemetryAttribute.FgaClientRequestMethod,
Expand Down Expand Up @@ -127,7 +127,7 @@ async function main () {
}, {
authorizationModelId: "01JC6KPJ0CKSZ69C5Z26CYWX2N"
});

console.log("writing tuple");
await fgaClient.write({
writes: [
Expand Down
2 changes: 1 addition & 1 deletion telemetry/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
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",
FgaClientRequestStoreId = "fga-client.request.store_id",
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",
Expand Down
2 changes: 1 addition & 1 deletion telemetry/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
]);

Expand Down
11 changes: 5 additions & 6 deletions tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -658,7 +657,7 @@ describe("OpenFGA Client", () => {
"cor-3": {
allowed: false,
error: {
inputError: ErrorCode.RelationNotFound,
input_error: ErrorCode.RelationNotFound,
message: "relation not found",
}
}
Expand Down Expand Up @@ -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);
Expand All @@ -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");
Expand All @@ -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 () => {
Expand Down
1 change: 1 addition & 0 deletions tests/telemetry/attributes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 407fbe2

Please sign in to comment.