Skip to content
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
3 changes: 3 additions & 0 deletions .openapi-generator-ignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
.npmignore
configuration.ts
common.ts
base.ts
54 changes: 1 addition & 53 deletions .openapi-generator/FILES
Original file line number Diff line number Diff line change
@@ -1,68 +1,16 @@
.codecov.yml
.eslintignore
.eslintrc.js
.fossa.yml
.github/CODEOWNERS
.github/ISSUE_TEMPLATE/bug_report.yaml
.github/ISSUE_TEMPLATE/config.yaml
.github/ISSUE_TEMPLATE/feature_request.yaml
.gitignore
.gitignore
.madgerc
.npmrc
CHANGELOG.md
CONTRIBUTING.md
LICENSE
README.md
VERSION.txt
api.ts
apiModel.ts
base.ts
client.ts
common.ts
configuration.ts
credentials/credentials.ts
credentials/index.ts
credentials/types.ts
docs/opentelemetry.md
errors.ts
example/Makefile
example/README.md
example/example1/example1.mjs
example/example1/package.json
example/opentelemetry/.env.example
example/opentelemetry/.npmrc
example/opentelemetry/README.md
example/opentelemetry/instrumentation.mjs
example/opentelemetry/opentelemetry.mjs
example/opentelemetry/package.json
constants/index.ts
git_push.sh
index.ts
package.json
telemetry/attributes.ts
telemetry/configuration.ts
telemetry/counters.ts
telemetry/histograms.ts
telemetry/metrics.ts
tests/client.test.ts
tests/headers.test.ts
tests/helpers/default-config.ts
tests/helpers/index.ts
tests/helpers/nocks.ts
tests/index.test.ts
tests/jest.config.js
tests/telemetry/attributes.test.ts
tests/telemetry/configuration.test.ts
tests/telemetry/counters.test.ts
tests/telemetry/histograms.test.ts
tests/telemetry/metrics.test.ts
tests/tsconfig.spec.json
tests/validation.test.ts
tsconfig.json
utils/assert-never.ts
utils/chunk-array.ts
utils/generate-random-id.ts
utils/index.ts
utils/set-header-if-not-set.ts
utils/set-not-enumerable-property.ts
validation.ts
14 changes: 10 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Reading and following these guidelines will help us make the contribution proces
* [Getting Started](#getting-started)
* [Making Changes](#making-changes)
* [Opening Issues](#opening-issues)
* [Submitting Pull Requests](#submitting-pull-requests) [Note: We are not accepting Pull Requests at this time!]
* [Submitting Pull Requests](#submitting-pull-requests)
* [Getting in Touch](#getting-in-touch)
* [Have a question or problem?](#have-a-question-or-problem)
* [Vulnerability Reporting](#vulnerability-reporting)
Expand All @@ -23,17 +23,23 @@ By participating and contributing to this project, you are expected to uphold ou

### Making Changes

When contributing to a repository, the first step is to open an issue on [sdk-generator](https://github.com/openfga/sdk-generator) to discuss the change you wish to make before making them.
When contributing to a repository, the first step is to open [an issue](https://github.com/openfga/js-sdk/issues) to discuss the change you wish to make before making them.

### Opening Issues

Before you submit a new issue please make sure to search all open and closed issues. It is possible your feature request/issue has already been answered. Make sure to also check the [OpenFGA discussions](https://github.com/orgs/openfga/discussions).

That repo includes an issue template that will walk through all the places to check before submitting your issue here. Please follow the instructions there to make sure this is not a duplicate issue and that we have everything we need to research and reproduce this problem.
The repo includes an issue template that will walk through all the places to check before submitting your issue here. Please follow the instructions there to make sure this is not a duplicate issue and that we have everything we need to research and reproduce this problem.

If you have found a bug or if you have a feature request, please report them in the [repo issues](https://github.com/openfga/js-sdk/issues). Cross-SDK bugs and feature requests can be additionally reported in the [sdk-generator repo](https://github.com/openfga/sdk-generator/issues) issues section, where the individual SDK issue is then linked.

**Please do not report security vulnerabilities on the public GitHub issue tracker.**

### Submitting Pull Requests

While we accept Pull Requests on this repository, the SDKs are autogenerated so please consider additionally submitting your Pull Requests to the [sdk-generator](https://github.com/openfga/sdk-generator) and linking the two PRs together and to the corresponding issue. This will greatly assist the OpenFGA team in being able to give timely reviews as well as deploying fixes and updates to our other SDKs as well.
Feel free to submit a Pull Request against this repository. Please make sure to follow the existing code style and include tests where applicable.

Some files in this repository are autogenerated. These files have a comment at the top indicating that they are autogenerated and should not be modified directly - the files are usually identified by a header marking them as such, or by their inclusion in [`.openapi-generator/FILES`](./.openapi-generator/FILES). Changes to these files should be made in the [sdk-generator](https://github.com/openfga/sdk-generator) repository in tandem, so please consider additionally submitting your Pull Requests to the [sdk-generator](https://github.com/openfga/sdk-generator) and linking the two PRs together and to the corresponding issue. This will greatly assist the OpenFGA team in being able to give timely reviews as well as deploying fixes and updates to our other SDKs as well.

## Getting in touch

Expand Down
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -830,13 +830,7 @@ This SDK supports producing metrics that can be consumed as part of an [OpenTele

## Contributing

### Issues

If you have found a bug or if you have a feature request, please report them on the [sdk-generator repo](https://github.com/openfga/sdk-generator/issues) issues section. Please do not report security vulnerabilities on the public GitHub issue tracker.

### Pull Requests

While we accept Pull Requests on this repository, the SDKs are autogenerated so please consider additionally submitting your Pull Requests to the [sdk-generator](https://github.com/openfga/sdk-generator) and linking the two PRs together and to the corresponding issue. This will greatly assist the OpenFGA team in being able to give timely reviews as well as deploying fixes and updates to our other SDKs as well.
See [CONTRIBUTING](./CONTRIBUTING.md) for details.

## Author

Expand Down
14 changes: 0 additions & 14 deletions base.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
/**
* JavaScript and Node.js SDK for OpenFGA
*
* API version: 1.x
* Website: https://openfga.dev
* Documentation: https://openfga.dev/docs
* Support: https://openfga.dev/community
* License: [Apache-2.0](https://github.com/openfga/js-sdk/blob/main/LICENSE)
*
* NOTE: This file was auto generated by OpenAPI Generator (https://openapi-generator.tech). DO NOT EDIT.
*/


// Some imports not used depending on template conditions
import globalAxios, { AxiosInstance } from "axios";
import * as http from "http";
import * as https from "https";
Expand Down
22 changes: 5 additions & 17 deletions client.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
/**
* JavaScript and Node.js SDK for OpenFGA
*
* API version: 1.x
* Website: https://openfga.dev
* Documentation: https://openfga.dev/docs
* Support: https://openfga.dev/community
* License: [Apache-2.0](https://github.com/openfga/js-sdk/blob/main/LICENSE)
*
* NOTE: This file was auto generated by OpenAPI Generator (https://openapi-generator.tech). DO NOT EDIT.
*/


import { AxiosResponse, AxiosInstance } from "axios";
import asyncPool = require("tiny-async-pool");

Expand Down Expand Up @@ -60,6 +47,7 @@ import {
setHeaderIfNotSet,
} from "./utils";
import { isWellFormedUlidString } from "./validation";
import SdkConstants from "./constants";

export type UserClientConfigurationParams = UserConfigurationParams & {
storeId?: string;
Expand Down Expand Up @@ -101,10 +89,10 @@ export class ClientConfiguration extends Configuration {
}
}

const DEFAULT_MAX_METHOD_PARALLEL_REQS = 10;
const DEFAULT_MAX_BATCH_SIZE = 50;
const CLIENT_METHOD_HEADER = "X-OpenFGA-Client-Method";
const CLIENT_BULK_REQUEST_ID_HEADER = "X-OpenFGA-Client-Bulk-Request-Id";
const DEFAULT_MAX_METHOD_PARALLEL_REQS = SdkConstants.ClientMaxMethodParallelRequests;
const DEFAULT_MAX_BATCH_SIZE = SdkConstants.ClientMaxBatchSize;
const CLIENT_METHOD_HEADER = SdkConstants.ClientMethodHeader;
const CLIENT_BULK_REQUEST_ID_HEADER = SdkConstants.ClientBulkRequestIdHeader;

export interface ClientRequestOpts {
retryParams?: RetryParams;
Expand Down
42 changes: 15 additions & 27 deletions common.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
/**
* JavaScript and Node.js SDK for OpenFGA
*
* API version: 1.x
* Website: https://openfga.dev
* Documentation: https://openfga.dev/docs
* Support: https://openfga.dev/community
* License: [Apache-2.0](https://github.com/openfga/js-sdk/blob/main/LICENSE)
*
* NOTE: This file was auto generated by OpenAPI Generator (https://openapi-generator.tech). DO NOT EDIT.
*/


import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios";

import { Configuration } from "./configuration";
import SdkConstants from "./constants";
import type { Credentials } from "./credentials";
import {
FgaApiError,
Expand All @@ -26,19 +14,13 @@ import {
} from "./errors";
import { setNotEnumerableProperty } from "./utils";
import { TelemetryAttribute, TelemetryAttributes } from "./telemetry/attributes";
import { MetricRecorder } from "./telemetry/metrics";
import { TelemetryHistograms } from "./telemetry/histograms";

/**
*
* @export
*/
export const DUMMY_BASE_URL = "https://example.com";
// Retry-After header validation: minimum 1 second, maximum 30 minutes (1800 seconds)
const MIN_RETRY_DELAY_MS = 1_000; // 1 second
const MAX_RETRY_DELAY_MS = 1_800_000; // 30 minutes
// Exponential backoff cap: maximum 120 seconds (2 minutes)
const MAX_EXPONENTIAL_BACKOFF_MS = 120_000; // 120 seconds
export const DUMMY_BASE_URL = `https://${SdkConstants.SampleBaseDomain}`;

/**
*
Expand Down Expand Up @@ -145,16 +127,16 @@ function calculateExponentialBackoffWithJitter(retryAttempt: number, minWaitInMs
const minDelayMs = Math.ceil(2 ** retryAttempt * minWaitInMs);
const maxDelayMs = Math.ceil(2 ** (retryAttempt + 1) * minWaitInMs);
const randomDelayMs = Math.floor(Math.random() * (maxDelayMs - minDelayMs) + minDelayMs);
return Math.min(randomDelayMs, MAX_EXPONENTIAL_BACKOFF_MS);
return Math.min(randomDelayMs, SdkConstants.MaxBackoffTimeInSec * 1000);
}

/**
* Validates if a retry delay is within acceptable bounds
* @param delayMs - Delay in milliseconds
* @returns True if delay is between MIN_RETRY_DELAY_MS and MAX_RETRY_DELAY_MS
* @returns True if delay is between {@link SdkConstants.DefaultMinWaitInMs}ms and {@link SdkConstants.RetryHeaderMaxAllowableDurationInSec}s
*/
function isValidRetryDelay(delayMs: number): boolean {
return delayMs >= MIN_RETRY_DELAY_MS && delayMs <= MAX_RETRY_DELAY_MS;
return delayMs >= SdkConstants.DefaultMinWaitInMs && delayMs <= SdkConstants.RetryHeaderMaxAllowableDurationInSec * 1000;
}

/**
Expand All @@ -163,7 +145,13 @@ function isValidRetryDelay(delayMs: number): boolean {
* @returns Delay in milliseconds if valid, undefined otherwise
*/
function parseRetryAfterHeader(headers: Record<string, string | string[] | undefined>): number | undefined {
const retryAfterHeader = headers["retry-after"] || headers["Retry-After"];
// Find the retry-after header regardless of case
const retryAfterHeaderNameLower = SdkConstants.RetryAfterHeaderName.toLowerCase();
const retryAfterKey = Object.keys(headers).find(key =>
key.toLowerCase() === retryAfterHeaderNameLower
);

const retryAfterHeader = retryAfterKey ? headers[retryAfterKey] : undefined;

if (!retryAfterHeader) {
return undefined;
Expand Down Expand Up @@ -281,7 +269,7 @@ export async function attemptHttpRequest<B, R>(
retryDelayMs = calculateExponentialBackoffWithJitter(iterationCount, config.minWaitInMs);
}

await new Promise(r => setTimeout(r, Math.min(retryDelayMs, MAX_RETRY_DELAY_MS)));
await new Promise(r => setTimeout(r, Math.min(retryDelayMs, SdkConstants.RetryHeaderMaxAllowableDurationInSec * 1000)));
}
} while (iterationCount < config.maxRetry + 1);
}
Expand All @@ -293,8 +281,8 @@ export const createRequestFunction = function (axiosArgs: RequestArgs, axiosInst
configuration.isValid();

const retryParams = axiosArgs.options?.retryParams ? axiosArgs.options?.retryParams : configuration.retryParams;
const maxRetry:number = retryParams ? retryParams.maxRetry : 0;
const minWaitInMs:number = retryParams ? retryParams.minWaitInMs : 0;
const maxRetry: number = retryParams?.maxRetry ?? 0;
const minWaitInMs: number = retryParams?.minWaitInMs ?? 0;

const start = performance.now();

Expand Down
28 changes: 8 additions & 20 deletions configuration.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
/**
* JavaScript and Node.js SDK for OpenFGA
*
* API version: 1.x
* Website: https://openfga.dev
* Documentation: https://openfga.dev/docs
* Support: https://openfga.dev/community
* License: [Apache-2.0](https://github.com/openfga/js-sdk/blob/main/LICENSE)
*
* NOTE: This file was auto generated by OpenAPI Generator (https://openapi-generator.tech). DO NOT EDIT.
*/


import { ApiTokenConfig, AuthCredentialsConfig, ClientCredentialsConfig, CredentialsMethod } from "./credentials/types";
import { FgaValidationError, } from "./errors";
import { assertParamExists, isWellFormedUlidString, isWellFormedUriString } from "./validation";
import { assertParamExists, isWellFormedUriString } from "./validation";
import { TelemetryConfig, TelemetryConfiguration } from "./telemetry/configuration";
import SdkConstants from "./constants";

// default maximum number of retry
const DEFAULT_MAX_RETRY = 3;
const DEFAULT_MAX_RETRY = SdkConstants.DefaultMaxRetry;

// default minimum wait period in retry - but will backoff exponentially
const DEFAULT_MIN_WAIT_MS = 100;
const DEFAULT_MIN_WAIT_MS = SdkConstants.DefaultMinWaitInMs;

const DEFAULT_USER_AGENT = "openfga-sdk js/0.9.0";
const DEFAULT_USER_AGENT = SdkConstants.UserAgent;

export interface RetryParams {
maxRetry?: number;
Expand All @@ -45,7 +33,7 @@ export interface UserConfigurationParams {
telemetry?: TelemetryConfig;
}

export function GetDefaultRetryParams (maxRetry = DEFAULT_MAX_RETRY, minWaitInMs = DEFAULT_MIN_WAIT_MS) {
export function GetDefaultRetryParams (maxRetry: number = DEFAULT_MAX_RETRY, minWaitInMs: number = DEFAULT_MIN_WAIT_MS) {
return {
maxRetry: maxRetry,
minWaitInMs: minWaitInMs,
Expand Down Expand Up @@ -75,7 +63,7 @@ export class Configuration {
* @type {string}
* @memberof Configuration
*/
private static sdkVersion = "0.9.0";
private static sdkVersion = SdkConstants.SdkVersion;

/**
* provide the full api URL (e.g. `https://api.fga.example`)
Expand Down Expand Up @@ -192,7 +180,7 @@ export class Configuration {
);
}

if (this.retryParams?.maxRetry && this.retryParams.maxRetry > 15) {
if (this.retryParams?.maxRetry && this.retryParams.maxRetry > SdkConstants.RetryMaxAllowedNumber) {
throw new FgaValidationError("Configuration.retryParams.maxRetry exceeds maximum allowed limit of 15");
}

Expand Down
Loading