Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support usage of the consistency option #129

Merged
merged 1 commit into from
Jul 31, 2024
Merged
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
1 change: 1 addition & 0 deletions api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
Condition,
ConditionMetadata,
ConditionParamTypeRef,
ConsistencyPreference,
ContextualTupleKeys,
CreateStoreRequest,
CreateStoreResponse,
Expand Down
52 changes: 52 additions & 0 deletions apiModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,15 @@ export interface CheckRequest {
* @memberof CheckRequest
*/
context?: object;
/**
*
* @type {ConsistencyPreference}
* @memberof CheckRequest
*/
consistency?: ConsistencyPreference;
}


/**
*
* @export
Expand Down Expand Up @@ -287,6 +295,18 @@ export interface ConditionParamTypeRef {
}


/**
* - 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.
* @export
* @enum {string}
*/

export enum ConsistencyPreference {
Unspecified = 'UNSPECIFIED',
MinimizeLatency = 'MINIMIZE_LATENCY',
HigherConsistency = 'HIGHER_CONSISTENCY'
}

/**
*
* @export
Expand Down Expand Up @@ -438,7 +458,15 @@ export interface ExpandRequest {
* @memberof ExpandRequest
*/
authorization_model_id?: string;
/**
*
* @type {ConsistencyPreference}
* @memberof ExpandRequest
*/
consistency?: ConsistencyPreference;
}


/**
*
* @export
Expand Down Expand Up @@ -635,7 +663,15 @@ export interface ListObjectsRequest {
* @memberof ListObjectsRequest
*/
context?: object;
/**
*
* @type {ConsistencyPreference}
* @memberof ListObjectsRequest
*/
consistency?: ConsistencyPreference;
}


/**
*
* @export
Expand Down Expand Up @@ -710,7 +746,15 @@ export interface ListUsersRequest {
* @memberof ListUsersRequest
*/
context?: object;
/**
*
* @type {ConsistencyPreference}
* @memberof ListUsersRequest
*/
consistency?: ConsistencyPreference;
}


/**
*
* @export
Expand Down Expand Up @@ -956,7 +1000,15 @@ export interface ReadRequest {
* @memberof ReadRequest
*/
continuation_token?: string;
/**
*
* @type {ConsistencyPreference}
* @memberof ReadRequest
*/
consistency?: ConsistencyPreference;
}


/**
*
* @export
Expand Down
41 changes: 26 additions & 15 deletions client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
CheckRequest,
CheckRequestTupleKey,
CheckResponse,
ConsistencyPreference,
CreateStoreRequest,
CreateStoreResponse,
ExpandRequestTupleKey,
Expand Down Expand Up @@ -111,8 +112,13 @@ export interface AuthorizationModelIdOpts {
authorizationModelId?: string;
}

export interface ConsistencyOpts {
consistency?: ConsistencyPreference
}

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; };

Expand Down Expand Up @@ -179,13 +185,13 @@ export interface ClientReadChangesRequest {

export type ClientExpandRequest = ExpandRequestTupleKey;
export type ClientReadRequest = ReadRequestTupleKey;
export type ClientListObjectsRequest = Omit<ListObjectsRequest, "authorization_model_id" | "contextual_tuples"> & {
export type ClientListObjectsRequest = Omit<ListObjectsRequest, "authorization_model_id" | "contextual_tuples" | "consistency"> & {
contextualTuples?: Array<TupleKey>
};
export type ClientListUsersRequest = Omit<ListUsersRequest, "authorization_model_id" | "contextual_tuples"> & {
export type ClientListUsersRequest = Omit<ListUsersRequest, "authorization_model_id" | "contextual_tuples" | "consistency"> & {
contextualTuples?: Array<TupleKey>
};
export type ClientListRelationsRequest = Omit<ClientCheckRequest, "relation"> & {
export type ClientListRelationsRequest = Omit<ClientCheckRequest, "relation" | "consistency"> & {
relations?: string[],
};
export type ClientWriteAssertionsRequest = (CheckRequestTupleKey & Pick<Assertion, "expectation">)[];
Expand Down Expand Up @@ -398,15 +404,16 @@ export class OpenFgaClient extends BaseAPI {
/**
* Read - Read tuples previously written to the store (does not evaluate)
* @param {ClientReadRequest} body
* @param {ClientRequestOpts & PaginationOptions} [options]
* @param {ClientRequestOpts & PaginationOptions & ConsistencyOpts} [options]
* @param {number} [options.pageSize]
* @param {string} [options.continuationToken]
* @param {object} [options.headers] - Custom headers to send alongside the request
* @param {ConsistencyPreference} [options.consistency] - The consistency preference to use
* @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
*/
async read(body: ClientReadRequest = {}, options: ClientRequestOptsWithStoreId & PaginationOptions = {}): PromiseResult<ReadResponse> {
async read(body: ClientReadRequest = {}, options: ClientRequestOptsWithStoreId & PaginationOptions & ConsistencyOpts = {}): PromiseResult<ReadResponse> {
const readRequest: ReadRequest = {
page_size: options.pageSize,
continuation_token: options.continuationToken,
Expand Down Expand Up @@ -556,14 +563,15 @@ export class OpenFgaClient extends BaseAPI {
/**
* Check - Check if a user has a particular relation with an object (evaluates)
* @param {ClientCheckRequest} body
* @param {ClientRequestOptsWithAuthZModelId} [options]
* @param {ClientRequestOptsWithConsistency} [options]
* @param {string} [options.authorizationModelId] - Overrides the authorization model id in the configuration
* @param {object} [options.headers] - Custom headers to send alongside the request
* @param {ConsistencyPreference} [options.consistency] - The consistency preference to use
* @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
*/
async check(body: ClientCheckRequest, options: ClientRequestOptsWithAuthZModelId = {}): PromiseResult<CheckResponse> {
async check(body: ClientCheckRequest, options: ClientRequestOptsWithConsistency = {}): PromiseResult<CheckResponse> {
return this.api.check(this.getStoreId(options)!, {
tuple_key: {
user: body.user,
Expand All @@ -587,7 +595,7 @@ export class OpenFgaClient extends BaseAPI {
* @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
*/
async batchCheck(body: ClientBatchCheckRequest, options: ClientRequestOptsWithAuthZModelId & BatchCheckRequestOpts = {}): Promise<ClientBatchCheckResponse> {
async batchCheck(body: ClientBatchCheckRequest, options: ClientRequestOptsWithConsistency & BatchCheckRequestOpts = {}): Promise<ClientBatchCheckResponse> {
const { headers = {}, maxParallelRequests = DEFAULT_MAX_METHOD_PARALLEL_REQS } = options;
setHeaderIfNotSet(headers, CLIENT_METHOD_HEADER, "BatchCheck");
setHeaderIfNotSet(headers, CLIENT_BULK_REQUEST_ID_HEADER, generateRandomIdWithNonUniqueFallback());
Expand Down Expand Up @@ -621,14 +629,15 @@ export class OpenFgaClient extends BaseAPI {
* @param {ClientExpandRequest} body
* @param {string} body.relation The relation
* @param {string} body.object The object, must be of the form: `<type>:<id>`
* @param {ClientRequestOptsWithAuthZModelId} [options]
* @param {ClientRequestOptsWithConsistency} [options]
* @param {string} [options.authorizationModelId] - Overrides the authorization model id in the configuration
* @param {object} [options.headers] - Custom headers to send alongside the request
* @param {ConsistencyPreference} [options.consistency] - The consistency preference to use
* @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
*/
async expand(body: ClientExpandRequest, options: ClientRequestOptsWithAuthZModelId = {}): PromiseResult<ExpandResponse> {
async expand(body: ClientExpandRequest, options: ClientRequestOptsWithConsistency = {}): PromiseResult<ExpandResponse> {
return this.api.expand(this.getStoreId(options)!, {
authorization_model_id: this.getAuthorizationModelId(options),
tuple_key: body,
Expand All @@ -638,14 +647,15 @@ export class OpenFgaClient extends BaseAPI {
/**
* ListObjects - List the objects of a particular type that the user has a certain relation to (evaluates)
* @param {ClientListObjectsRequest} body
* @param {ClientRequestOptsWithAuthZModelId} [options]
* @param {ClientRequestOptsWithConsistency} [options]
* @param {string} [options.authorizationModelId] - Overrides the authorization model id in the configuration
* @param {object} [options.headers] - Custom headers to send alongside the request
* @param {ConsistencyPreference} [options.consistency] - The consistency preference to use
* @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
*/
async listObjects(body: ClientListObjectsRequest, options: ClientRequestOptsWithAuthZModelId = {}): PromiseResult<ListObjectsResponse> {
async listObjects(body: ClientListObjectsRequest, options: ClientRequestOptsWithConsistency = {}): PromiseResult<ListObjectsResponse> {
return this.api.listObjects(this.getStoreId(options)!, {
authorization_model_id: this.getAuthorizationModelId(options),
user: body.user,
Expand All @@ -666,7 +676,7 @@ export class OpenFgaClient extends BaseAPI {
* @param {object} listRelationsRequest.context The contextual tuples to send
* @param options
*/
async listRelations(listRelationsRequest: ClientListRelationsRequest, options: ClientRequestOptsWithAuthZModelId & BatchCheckRequestOpts = {}): Promise<ClientListRelationsResponse> {
async listRelations(listRelationsRequest: ClientListRelationsRequest, options: ClientRequestOptsWithConsistency & BatchCheckRequestOpts = {}): Promise<ClientListRelationsResponse> {
const { user, object, relations, contextualTuples, context } = listRelationsRequest;
const { headers = {}, maxParallelRequests = DEFAULT_MAX_METHOD_PARALLEL_REQS } = options;
setHeaderIfNotSet(headers, CLIENT_METHOD_HEADER, "ListRelations");
Expand Down Expand Up @@ -695,14 +705,15 @@ export class OpenFgaClient extends BaseAPI {
/**
* ListUsers - List the objects of a particular type that the user has a certain relation to (evaluates)
* @param {ClientListUsersRequest} body
* @param {ClientRequestOptsWithAuthZModelId} [options]
* @param {ClientRequestOptsWithConsistency} [options]
* @param {string} [options.authorizationModelId] - Overrides the authorization model id in the configuration
* @param {object} [options.headers] - Custom headers to send alongside the request
* @param {ConsistencyPreference} [options.consistency] - The consistency preference to use
* @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
*/
async listUsers(body: ClientListUsersRequest, options: ClientRequestOptsWithAuthZModelId = {}): PromiseResult<ListUsersResponse> {
async listUsers(body: ClientListUsersRequest, options: ClientRequestOptsWithConsistency = {}): PromiseResult<ListUsersResponse> {
return this.api.listUsers(this.getStoreId(options)!, {
authorization_model_id: this.getAuthorizationModelId(options),
relation: body.relation,
Expand Down