Skip to content

Commit ded34ef

Browse files
authored
feat: support usage of the consistency option (#129)
2 parents dc3490c + bacbd79 commit ded34ef

File tree

3 files changed

+79
-15
lines changed

3 files changed

+79
-15
lines changed

api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import {
4242
Condition,
4343
ConditionMetadata,
4444
ConditionParamTypeRef,
45+
ConsistencyPreference,
4546
ContextualTupleKeys,
4647
CreateStoreRequest,
4748
CreateStoreResponse,

apiModel.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,15 @@ export interface CheckRequest {
158158
* @memberof CheckRequest
159159
*/
160160
context?: object;
161+
/**
162+
*
163+
* @type {ConsistencyPreference}
164+
* @memberof CheckRequest
165+
*/
166+
consistency?: ConsistencyPreference;
161167
}
168+
169+
162170
/**
163171
*
164172
* @export
@@ -287,6 +295,18 @@ export interface ConditionParamTypeRef {
287295
}
288296

289297

298+
/**
299+
* - UNSPECIFIED: Default if not set. Behavior will be the same as MINIMIZE_LATENCY - MINIMIZE_LATENCY: Minimize latency at the potential expense of lower consistency. - HIGHER_CONSISTENCY: Prefer higher consistency, at the potential expense of increased latency.
300+
* @export
301+
* @enum {string}
302+
*/
303+
304+
export enum ConsistencyPreference {
305+
Unspecified = 'UNSPECIFIED',
306+
MinimizeLatency = 'MINIMIZE_LATENCY',
307+
HigherConsistency = 'HIGHER_CONSISTENCY'
308+
}
309+
290310
/**
291311
*
292312
* @export
@@ -438,7 +458,15 @@ export interface ExpandRequest {
438458
* @memberof ExpandRequest
439459
*/
440460
authorization_model_id?: string;
461+
/**
462+
*
463+
* @type {ConsistencyPreference}
464+
* @memberof ExpandRequest
465+
*/
466+
consistency?: ConsistencyPreference;
441467
}
468+
469+
442470
/**
443471
*
444472
* @export
@@ -635,7 +663,15 @@ export interface ListObjectsRequest {
635663
* @memberof ListObjectsRequest
636664
*/
637665
context?: object;
666+
/**
667+
*
668+
* @type {ConsistencyPreference}
669+
* @memberof ListObjectsRequest
670+
*/
671+
consistency?: ConsistencyPreference;
638672
}
673+
674+
639675
/**
640676
*
641677
* @export
@@ -710,7 +746,15 @@ export interface ListUsersRequest {
710746
* @memberof ListUsersRequest
711747
*/
712748
context?: object;
749+
/**
750+
*
751+
* @type {ConsistencyPreference}
752+
* @memberof ListUsersRequest
753+
*/
754+
consistency?: ConsistencyPreference;
713755
}
756+
757+
714758
/**
715759
*
716760
* @export
@@ -956,7 +1000,15 @@ export interface ReadRequest {
9561000
* @memberof ReadRequest
9571001
*/
9581002
continuation_token?: string;
1003+
/**
1004+
*
1005+
* @type {ConsistencyPreference}
1006+
* @memberof ReadRequest
1007+
*/
1008+
consistency?: ConsistencyPreference;
9591009
}
1010+
1011+
9601012
/**
9611013
*
9621014
* @export

client.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
CheckRequest,
2121
CheckRequestTupleKey,
2222
CheckResponse,
23+
ConsistencyPreference,
2324
CreateStoreRequest,
2425
CreateStoreResponse,
2526
ExpandRequestTupleKey,
@@ -111,8 +112,13 @@ export interface AuthorizationModelIdOpts {
111112
authorizationModelId?: string;
112113
}
113114

115+
export interface ConsistencyOpts {
116+
consistency?: ConsistencyPreference
117+
}
118+
114119
export type ClientRequestOptsWithStoreId = ClientRequestOpts & StoreIdOpts;
115120
export type ClientRequestOptsWithAuthZModelId = ClientRequestOpts & StoreIdOpts & AuthorizationModelIdOpts;
121+
export type ClientRequestOptsWithConsistency = ClientRequestOpts & StoreIdOpts & AuthorizationModelIdOpts & ConsistencyOpts;
116122

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

@@ -179,13 +185,13 @@ export interface ClientReadChangesRequest {
179185

180186
export type ClientExpandRequest = ExpandRequestTupleKey;
181187
export type ClientReadRequest = ReadRequestTupleKey;
182-
export type ClientListObjectsRequest = Omit<ListObjectsRequest, "authorization_model_id" | "contextual_tuples"> & {
188+
export type ClientListObjectsRequest = Omit<ListObjectsRequest, "authorization_model_id" | "contextual_tuples" | "consistency"> & {
183189
contextualTuples?: Array<TupleKey>
184190
};
185-
export type ClientListUsersRequest = Omit<ListUsersRequest, "authorization_model_id" | "contextual_tuples"> & {
191+
export type ClientListUsersRequest = Omit<ListUsersRequest, "authorization_model_id" | "contextual_tuples" | "consistency"> & {
186192
contextualTuples?: Array<TupleKey>
187193
};
188-
export type ClientListRelationsRequest = Omit<ClientCheckRequest, "relation"> & {
194+
export type ClientListRelationsRequest = Omit<ClientCheckRequest, "relation" | "consistency"> & {
189195
relations?: string[],
190196
};
191197
export type ClientWriteAssertionsRequest = (CheckRequestTupleKey & Pick<Assertion, "expectation">)[];
@@ -398,15 +404,16 @@ export class OpenFgaClient extends BaseAPI {
398404
/**
399405
* Read - Read tuples previously written to the store (does not evaluate)
400406
* @param {ClientReadRequest} body
401-
* @param {ClientRequestOpts & PaginationOptions} [options]
407+
* @param {ClientRequestOpts & PaginationOptions & ConsistencyOpts} [options]
402408
* @param {number} [options.pageSize]
403409
* @param {string} [options.continuationToken]
404410
* @param {object} [options.headers] - Custom headers to send alongside the request
411+
* @param {ConsistencyPreference} [options.consistency] - The consistency preference to use
405412
* @param {object} [options.retryParams] - Override the retry parameters for this request
406413
* @param {number} [options.retryParams.maxRetry] - Override the max number of retries on each API request
407414
* @param {number} [options.retryParams.minWaitInMs] - Override the minimum wait before a retry is initiated
408415
*/
409-
async read(body: ClientReadRequest = {}, options: ClientRequestOptsWithStoreId & PaginationOptions = {}): PromiseResult<ReadResponse> {
416+
async read(body: ClientReadRequest = {}, options: ClientRequestOptsWithStoreId & PaginationOptions & ConsistencyOpts = {}): PromiseResult<ReadResponse> {
410417
const readRequest: ReadRequest = {
411418
page_size: options.pageSize,
412419
continuation_token: options.continuationToken,
@@ -556,14 +563,15 @@ export class OpenFgaClient extends BaseAPI {
556563
/**
557564
* Check - Check if a user has a particular relation with an object (evaluates)
558565
* @param {ClientCheckRequest} body
559-
* @param {ClientRequestOptsWithAuthZModelId} [options]
566+
* @param {ClientRequestOptsWithConsistency} [options]
560567
* @param {string} [options.authorizationModelId] - Overrides the authorization model id in the configuration
561568
* @param {object} [options.headers] - Custom headers to send alongside the request
569+
* @param {ConsistencyPreference} [options.consistency] - The consistency preference to use
562570
* @param {object} [options.retryParams] - Override the retry parameters for this request
563571
* @param {number} [options.retryParams.maxRetry] - Override the max number of retries on each API request
564572
* @param {number} [options.retryParams.minWaitInMs] - Override the minimum wait before a retry is initiated
565573
*/
566-
async check(body: ClientCheckRequest, options: ClientRequestOptsWithAuthZModelId = {}): PromiseResult<CheckResponse> {
574+
async check(body: ClientCheckRequest, options: ClientRequestOptsWithConsistency = {}): PromiseResult<CheckResponse> {
567575
return this.api.check(this.getStoreId(options)!, {
568576
tuple_key: {
569577
user: body.user,
@@ -587,7 +595,7 @@ export class OpenFgaClient extends BaseAPI {
587595
* @param {number} [options.retryParams.maxRetry] - Override the max number of retries on each API request
588596
* @param {number} [options.retryParams.minWaitInMs] - Override the minimum wait before a retry is initiated
589597
*/
590-
async batchCheck(body: ClientBatchCheckRequest, options: ClientRequestOptsWithAuthZModelId & BatchCheckRequestOpts = {}): Promise<ClientBatchCheckResponse> {
598+
async batchCheck(body: ClientBatchCheckRequest, options: ClientRequestOptsWithConsistency & BatchCheckRequestOpts = {}): Promise<ClientBatchCheckResponse> {
591599
const { headers = {}, maxParallelRequests = DEFAULT_MAX_METHOD_PARALLEL_REQS } = options;
592600
setHeaderIfNotSet(headers, CLIENT_METHOD_HEADER, "BatchCheck");
593601
setHeaderIfNotSet(headers, CLIENT_BULK_REQUEST_ID_HEADER, generateRandomIdWithNonUniqueFallback());
@@ -621,14 +629,15 @@ export class OpenFgaClient extends BaseAPI {
621629
* @param {ClientExpandRequest} body
622630
* @param {string} body.relation The relation
623631
* @param {string} body.object The object, must be of the form: `<type>:<id>`
624-
* @param {ClientRequestOptsWithAuthZModelId} [options]
632+
* @param {ClientRequestOptsWithConsistency} [options]
625633
* @param {string} [options.authorizationModelId] - Overrides the authorization model id in the configuration
626634
* @param {object} [options.headers] - Custom headers to send alongside the request
635+
* @param {ConsistencyPreference} [options.consistency] - The consistency preference to use
627636
* @param {object} [options.retryParams] - Override the retry parameters for this request
628637
* @param {number} [options.retryParams.maxRetry] - Override the max number of retries on each API request
629638
* @param {number} [options.retryParams.minWaitInMs] - Override the minimum wait before a retry is initiated
630639
*/
631-
async expand(body: ClientExpandRequest, options: ClientRequestOptsWithAuthZModelId = {}): PromiseResult<ExpandResponse> {
640+
async expand(body: ClientExpandRequest, options: ClientRequestOptsWithConsistency = {}): PromiseResult<ExpandResponse> {
632641
return this.api.expand(this.getStoreId(options)!, {
633642
authorization_model_id: this.getAuthorizationModelId(options),
634643
tuple_key: body,
@@ -638,14 +647,15 @@ export class OpenFgaClient extends BaseAPI {
638647
/**
639648
* ListObjects - List the objects of a particular type that the user has a certain relation to (evaluates)
640649
* @param {ClientListObjectsRequest} body
641-
* @param {ClientRequestOptsWithAuthZModelId} [options]
650+
* @param {ClientRequestOptsWithConsistency} [options]
642651
* @param {string} [options.authorizationModelId] - Overrides the authorization model id in the configuration
643652
* @param {object} [options.headers] - Custom headers to send alongside the request
653+
* @param {ConsistencyPreference} [options.consistency] - The consistency preference to use
644654
* @param {object} [options.retryParams] - Override the retry parameters for this request
645655
* @param {number} [options.retryParams.maxRetry] - Override the max number of retries on each API request
646656
* @param {number} [options.retryParams.minWaitInMs] - Override the minimum wait before a retry is initiated
647657
*/
648-
async listObjects(body: ClientListObjectsRequest, options: ClientRequestOptsWithAuthZModelId = {}): PromiseResult<ListObjectsResponse> {
658+
async listObjects(body: ClientListObjectsRequest, options: ClientRequestOptsWithConsistency = {}): PromiseResult<ListObjectsResponse> {
649659
return this.api.listObjects(this.getStoreId(options)!, {
650660
authorization_model_id: this.getAuthorizationModelId(options),
651661
user: body.user,
@@ -666,7 +676,7 @@ export class OpenFgaClient extends BaseAPI {
666676
* @param {object} listRelationsRequest.context The contextual tuples to send
667677
* @param options
668678
*/
669-
async listRelations(listRelationsRequest: ClientListRelationsRequest, options: ClientRequestOptsWithAuthZModelId & BatchCheckRequestOpts = {}): Promise<ClientListRelationsResponse> {
679+
async listRelations(listRelationsRequest: ClientListRelationsRequest, options: ClientRequestOptsWithConsistency & BatchCheckRequestOpts = {}): Promise<ClientListRelationsResponse> {
670680
const { user, object, relations, contextualTuples, context } = listRelationsRequest;
671681
const { headers = {}, maxParallelRequests = DEFAULT_MAX_METHOD_PARALLEL_REQS } = options;
672682
setHeaderIfNotSet(headers, CLIENT_METHOD_HEADER, "ListRelations");
@@ -695,14 +705,15 @@ export class OpenFgaClient extends BaseAPI {
695705
/**
696706
* ListUsers - List the objects of a particular type that the user has a certain relation to (evaluates)
697707
* @param {ClientListUsersRequest} body
698-
* @param {ClientRequestOptsWithAuthZModelId} [options]
708+
* @param {ClientRequestOptsWithConsistency} [options]
699709
* @param {string} [options.authorizationModelId] - Overrides the authorization model id in the configuration
700710
* @param {object} [options.headers] - Custom headers to send alongside the request
711+
* @param {ConsistencyPreference} [options.consistency] - The consistency preference to use
701712
* @param {object} [options.retryParams] - Override the retry parameters for this request
702713
* @param {number} [options.retryParams.maxRetry] - Override the max number of retries on each API request
703714
* @param {number} [options.retryParams.minWaitInMs] - Override the minimum wait before a retry is initiated
704715
*/
705-
async listUsers(body: ClientListUsersRequest, options: ClientRequestOptsWithAuthZModelId = {}): PromiseResult<ListUsersResponse> {
716+
async listUsers(body: ClientListUsersRequest, options: ClientRequestOptsWithConsistency = {}): PromiseResult<ListUsersResponse> {
706717
return this.api.listUsers(this.getStoreId(options)!, {
707718
authorization_model_id: this.getAuthorizationModelId(options),
708719
relation: body.relation,

0 commit comments

Comments
 (0)