Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

## v0.9.0

### [v0.9.0](https://github.com/openfga/js-sdk/compare/v0.8.1...v0.9.0) (2025-06-03)
### [v0.9.0](https://github.com/openfga/js-sdk/compare/v0.8.1...v0.9.0) (2025-06-04)

- feat: support client assertion for client credentials authentication (#228)

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,8 @@ Similar to [check](#check), but instead of checking a single user-object relatio

[API Documentation](https://openfga.dev/api/service#/Relationship%20Queries/BatchCheck)

> **Note**: The order of `batchCheck` results is not guaranteed to match the order of the checks provided. Use `correlationId` to pair responses with requests.

```javascript
const options = {
// if you'd like to override the authorization model id for this request
Expand Down
46 changes: 27 additions & 19 deletions api.ts

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions apiModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1920,7 +1920,23 @@ export interface WriteRequestDeletes {
* @memberof WriteRequestDeletes
*/
tuple_keys: Array<TupleKeyWithoutCondition>;
/**
* On \'error\', the API returns an error when deleting a tuple that does not exist. On \'ignore\', deletes of non-existent tuples are treated as no-ops.
* @type {string}
* @memberof WriteRequestDeletes
*/
on_missing?: WriteRequestDeletesOnMissingEnum;
}

/**
* @export
* @enum {string}
*/
export enum WriteRequestDeletesOnMissingEnum {
Error = 'error',
Ignore = 'ignore'
}

/**
*
* @export
Expand All @@ -1933,5 +1949,21 @@ export interface WriteRequestWrites {
* @memberof WriteRequestWrites
*/
tuple_keys: Array<TupleKey>;
/**
* On \'error\' ( or unspecified ), the API returns an error if an identical tuple already exists. On \'ignore\', identical writes are treated as no-ops (matching on user, relation, object, and RelationshipCondition).
* @type {string}
* @memberof WriteRequestWrites
*/
on_duplicate?: WriteRequestWritesOnDuplicateEnum;
}

/**
* @export
* @enum {string}
*/
export enum WriteRequestWritesOnDuplicateEnum {
Error = 'error',
Ignore = 'ignore'
}


32 changes: 30 additions & 2 deletions client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import asyncPool = require("tiny-async-pool");

import { OpenFgaApi } from "./api";
import {
type WriteRequestDeletesOnMissingEnum,
type WriteRequestWritesOnDuplicateEnum,
Assertion,
BatchCheckItem,
BatchCheckRequest,
Expand Down Expand Up @@ -127,7 +129,7 @@ export type ClientRequestOptsWithStoreId = ClientRequestOpts & StoreIdOpts;
export type ClientRequestOptsWithAuthZModelId = ClientRequestOpts & StoreIdOpts & AuthorizationModelIdOpts;
export type ClientRequestOptsWithConsistency = ClientRequestOpts & StoreIdOpts & AuthorizationModelIdOpts & ConsistencyOpts;

export type PaginationOptions = { pageSize?: number, continuationToken?: string; };
export type PaginationOptions = { pageSize?: number, continuationToken?: string, name?: string; };

export type ClientCheckRequest = CheckRequestTupleKey &
Pick<CheckRequest, "context"> &
Expand Down Expand Up @@ -187,12 +189,31 @@ export interface ClientBatchCheckResponse {
result: ClientBatchCheckSingleResponse[];
}

export declare enum ClientWriteRequestWritesOnDuplicateEnum {
Error = "error",
Ignore = "ignore"
}
export declare enum ClientWriteRequestDeletesOnMissingEnum {
Error = "error",
Ignore = "ignore"
}

Comment on lines +192 to +200
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Do not use declare enum in .ts source — exports won’t exist at runtime.

Consumers will likely import these enums as values. Use regular exported enums so runtime values are emitted.

Apply:

-export declare enum ClientWriteRequestWritesOnDuplicateEnum {
+export enum ClientWriteRequestWritesOnDuplicateEnum {
     Error = "error",
     Ignore = "ignore"
 }
-export declare enum ClientWriteRequestDeletesOnMissingEnum {
+export enum ClientWriteRequestDeletesOnMissingEnum {
     Error = "error",
     Ignore = "ignore"
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export declare enum ClientWriteRequestWritesOnDuplicateEnum {
Error = "error",
Ignore = "ignore"
}
export declare enum ClientWriteRequestDeletesOnMissingEnum {
Error = "error",
Ignore = "ignore"
}
export enum ClientWriteRequestWritesOnDuplicateEnum {
Error = "error",
Ignore = "ignore"
}
export enum ClientWriteRequestDeletesOnMissingEnum {
Error = "error",
Ignore = "ignore"
}
🤖 Prompt for AI Agents
In client.ts around lines 192 to 200, the enums are declared with "declare enum"
which emits no runtime values; replace those with regular exported enums (e.g.,
"export enum ClientWriteRequestWritesOnDuplicateEnum { ... }" and "export enum
ClientWriteRequestDeletesOnMissingEnum { ... }") so the enum values are emitted
into the compiled JS, update any imports/usages if needed, and run the
build/type-check to verify consumers can import the enums as runtime values.

export interface ClientWriteRequestOpts {
transaction?: {
disable?: boolean;
maxPerChunk?: number;
maxParallelRequests?: number;
}
idempotence?: {
/**
* On \'error\' ( or unspecified ), the API returns an error if an identical tuple already exists. On \'ignore\', identical writes are treated as no-ops (matching on user, relation, object, and RelationshipCondition).
*/
on_duplicate?: ClientWriteRequestWritesOnDuplicateEnum;
/**
* On \'error\', the API returns an error when deleting a tuple that does not exist. On \'ignore\', deletes of non-existent tuples are treated as no-ops.
*/
on_missing?: ClientWriteRequestDeletesOnMissingEnum;
}
}

export interface ClientWriteRequest {
Expand Down Expand Up @@ -311,14 +332,15 @@ export class OpenFgaClient extends BaseAPI {
* @param {ClientRequestOpts & PaginationOptions} [options]
* @param {number} [options.pageSize]
* @param {string} [options.continuationToken]
* @param {string} [options.name] - Filter stores by name
* @param {object} [options.headers] - Custom headers to send alongside the request
* @param {object} [options.retryParams] - Override the retry parameters for this request
* @param {number} [options.retryParams.maxRetry] - Override the max number of retries on each API request
* @param {number} [options.retryParams.minWaitInMs] - Override the minimum wait before a retry is initiated
* @throws { FgaError }
*/
async listStores(options: ClientRequestOptsWithAuthZModelId & PaginationOptions = {}): PromiseResult<ListStoresResponse> {
return this.api.listStores(options.pageSize, options.continuationToken, options);
return this.api.listStores(options.pageSize, options.continuationToken, options.name, options);
}

/**
Expand Down Expand Up @@ -497,9 +519,15 @@ export class OpenFgaClient extends BaseAPI {
};
if (writes?.length) {
apiBody.writes = { tuple_keys: writes };
if (options.idempotence?.on_duplicate) {
apiBody.writes.on_duplicate = options.idempotence.on_duplicate as unknown as WriteRequestWritesOnDuplicateEnum;
}
}
if (deletes?.length) {
apiBody.deletes = { tuple_keys: deletes };
if (options.idempotence?.on_missing) {
apiBody.deletes.on_missing = options.idempotence.on_missing as unknown as WriteRequestDeletesOnMissingEnum;
}
}
await this.api.write(this.getStoreId(options)!, apiBody, options);
return {
Expand Down
20 changes: 20 additions & 0 deletions tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,26 @@ describe("OpenFGA Client", () => {
expect(response.stores).toHaveLength(1);
expect(response.stores?.[0]).toMatchObject(store);
});

it("should properly call the ListStores API with name filter", async () => {
const store = { id: "some-id", name: "test-store" };
const scope = nocks.listStores(defaultConfiguration.getBasePath(), {
continuation_token: "",
stores: [{
...store,
created_at: "2023-11-02T15:27:47.951Z",
updated_at: "2023-11-02T15:27:47.951Z",
deleted_at: "2023-11-02T15:27:47.951Z",
}],
}, 200, { name: "test-store" });

expect(scope.isDone()).toBe(false);
const response = await fgaClient.listStores({ name: "test-store" });

expect(scope.isDone()).toBe(true);
expect(response.stores).toHaveLength(1);
expect(response.stores?.[0]).toMatchObject(store);
});
});

describe("CreateStore", () => {
Expand Down
9 changes: 6 additions & 3 deletions tests/helpers/nocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,13 @@ export const getNocks = ((nock: typeof Nock) => ({
}]
},
responseCode = 200,
queryParams?: { page_size?: number; continuation_token?: string; name?: string },
) => {
return nock(basePath)
.get("/stores")
.reply(responseCode, response);
const mock = nock(basePath).get("/stores");
if (queryParams) {
mock.query(queryParams);
}
return mock.reply(responseCode, response);
},
createStore: (
basePath = defaultConfiguration.getBasePath(),
Expand Down