@settlemint/sdk-utils
Advanced tools
Comparing version 1.0.0-maine2083d56 to 1.0.0-mainf5733251
@@ -37,2 +37,7 @@ import { PathLike } from 'node:fs'; | ||
* @returns The root directory of the monorepo or null if not found | ||
* @example | ||
* import { findMonoRepoRoot } from "@settlemint/sdk-utils"; | ||
* | ||
* const root = await findMonoRepoRoot("/path/to/your/project"); | ||
* console.log(root); // Output: /path/to/your/project/packages/core | ||
*/ | ||
@@ -45,2 +50,7 @@ declare function findMonoRepoRoot(startDir: string): Promise<string | null>; | ||
* @returns An array of package directories | ||
* @example | ||
* import { findMonoRepoPackages } from "@settlemint/sdk-utils"; | ||
* | ||
* const packages = await findMonoRepoPackages("/path/to/your/project"); | ||
* console.log(packages); // Output: ["/path/to/your/project/packages/core", "/path/to/your/project/packages/ui"] | ||
*/ | ||
@@ -47,0 +57,0 @@ declare function findMonoRepoPackages(projectDir: string): Promise<string[]>; |
@@ -475,2 +475,7 @@ import { ZodString, z, ZodSchema } from 'zod'; | ||
* @returns The root directory of the monorepo or null if not found | ||
* @example | ||
* import { findMonoRepoRoot } from "@settlemint/sdk-utils"; | ||
* | ||
* const root = await findMonoRepoRoot("/path/to/your/project"); | ||
* console.log(root); // Output: /path/to/your/project/packages/core | ||
*/ | ||
@@ -483,2 +488,7 @@ declare function findMonoRepoRoot(startDir: string): Promise<string | null>; | ||
* @returns An array of package directories | ||
* @example | ||
* import { findMonoRepoPackages } from "@settlemint/sdk-utils"; | ||
* | ||
* const packages = await findMonoRepoPackages("/path/to/your/project"); | ||
* console.log(packages); // Output: ["/path/to/your/project/packages/core", "/path/to/your/project/packages/ui"] | ||
*/ | ||
@@ -488,2 +498,51 @@ declare function findMonoRepoPackages(projectDir: string): Promise<string[]>; | ||
/** | ||
* Retry an HTTP request with exponential backoff and jitter. | ||
* Only retries on server errors (5xx), rate limits (429), timeouts (408), and network errors. | ||
* | ||
* @param input - The URL or Request object to fetch | ||
* @param init - The fetch init options | ||
* @param maxRetries - Maximum number of retry attempts | ||
* @param initialSleepTime - Initial sleep time between retries in ms | ||
* @returns The fetch Response | ||
* @throws Error if all retries fail | ||
* @example | ||
* import { fetchWithRetry } from "@settlemint/sdk-utils"; | ||
* | ||
* const response = await fetchWithRetry("https://api.example.com/data"); | ||
*/ | ||
declare function fetchWithRetry(input: RequestInfo | URL, init?: RequestInit, maxRetries?: number, initialSleepTime?: number): Promise<Response>; | ||
/** | ||
* Executes a GraphQL request with automatic retries using exponential backoff and jitter. | ||
* Only retries on server errors (5xx), rate limits (429), timeouts (408), and network errors. | ||
* Will also retry if the GraphQL response contains errors. | ||
* | ||
* @param input - The URL or Request object for the GraphQL endpoint | ||
* @param init - Optional fetch configuration options | ||
* @param maxRetries - Maximum retry attempts before failing (default: 5) | ||
* @param initialSleepTime - Initial delay between retries in milliseconds (default: 3000) | ||
* @returns The parsed GraphQL response data | ||
* @throws Error if all retries fail or if GraphQL response contains errors | ||
* @example | ||
* import { graphqlFetchWithRetry } from "@settlemint/sdk-utils"; | ||
* | ||
* const data = await graphqlFetchWithRetry<{ user: { id: string } }>( | ||
* "https://api.example.com/graphql", | ||
* { | ||
* method: "POST", | ||
* headers: { "Content-Type": "application/json" }, | ||
* body: JSON.stringify({ | ||
* query: `query GetUser($id: ID!) { | ||
* user(id: $id) { | ||
* id | ||
* } | ||
* }`, | ||
* variables: { id: "123" } | ||
* }) | ||
* } | ||
* ); | ||
*/ | ||
declare function graphqlFetchWithRetry<Data>(input: RequestInfo | URL, init?: RequestInit, maxRetries?: number, initialSleepTime?: number): Promise<Data>; | ||
/** | ||
* Attempts to parse a JSON string into a typed value, returning a default value if parsing fails. | ||
@@ -633,16 +692,9 @@ * | ||
* @returns The result of the function or undefined if it fails. | ||
* @example | ||
* import { retryWhenFailed } from "@settlemint/sdk-utils"; | ||
* import { readFile } from "node:fs/promises"; | ||
* | ||
* const result = await retryWhenFailed(() => readFile("/path/to/file.txt"), 3, 1_000); | ||
*/ | ||
declare function retryWhenFailed<T>(fn: () => Promise<T>, maxRetries?: number, initialSleepTime?: number, stopOnError?: (error: Error) => boolean): Promise<T>; | ||
/** | ||
* Retry an HTTP request with exponential backoff and jitter. | ||
* Only retries on server errors (5xx), rate limits (429), timeouts (408), and network errors. | ||
* | ||
* @param input - The URL or Request object to fetch | ||
* @param init - The fetch init options | ||
* @param maxRetries - Maximum number of retry attempts | ||
* @param initialSleepTime - Initial sleep time between retries in ms | ||
* @returns The fetch Response | ||
* @throws Error if all retries fail | ||
*/ | ||
declare function fetchWithRetry(input: RequestInfo | URL, init?: RequestInit, maxRetries?: number, initialSleepTime?: number): Promise<Response>; | ||
@@ -841,2 +893,2 @@ /** | ||
export { type AccessToken, AccessTokenSchema, type ApplicationAccessToken, ApplicationAccessTokenSchema, type DotEnv, type DotEnvPartial, DotEnvSchema, DotEnvSchemaPartial, type ExecuteCommandOptions, type Id, IdSchema, type PersonalAccessToken, PersonalAccessTokenSchema, type SpinnerOptions, type Template, UniqueNameSchema, type Url, type UrlOrPath, UrlOrPathSchema, type UrlPath, UrlPathSchema, UrlSchema, ascii, cancel, capitalizeFirstLetter, emptyDir, ensureBrowser, ensureServer, executeCommand, exists, fetchWithRetry, findMonoRepoPackages, findMonoRepoRoot, formatTargetDir, getPackageManager, getPackageManagerExecutable, installDependencies, intro, isEmpty, isPackageInstalled, loadEnv, maskTokens, note, outro, projectRoot, retryWhenFailed, runsInBrowser, runsOnServer, setName, spinner, templates, tryParseJson, validate, writeEnv }; | ||
export { type AccessToken, AccessTokenSchema, type ApplicationAccessToken, ApplicationAccessTokenSchema, type DotEnv, type DotEnvPartial, DotEnvSchema, DotEnvSchemaPartial, type ExecuteCommandOptions, type Id, IdSchema, type PersonalAccessToken, PersonalAccessTokenSchema, type SpinnerOptions, type Template, UniqueNameSchema, type Url, type UrlOrPath, UrlOrPathSchema, type UrlPath, UrlPathSchema, UrlSchema, ascii, cancel, capitalizeFirstLetter, emptyDir, ensureBrowser, ensureServer, executeCommand, exists, fetchWithRetry, findMonoRepoPackages, findMonoRepoRoot, formatTargetDir, getPackageManager, getPackageManagerExecutable, graphqlFetchWithRetry, installDependencies, intro, isEmpty, isPackageInstalled, loadEnv, maskTokens, note, outro, projectRoot, retryWhenFailed, runsInBrowser, runsOnServer, setName, spinner, templates, tryParseJson, validate, writeEnv }; |
{ | ||
"name": "@settlemint/sdk-utils", | ||
"description": "Shared utilities and helper functions for SettleMint SDK modules", | ||
"version": "1.0.0-maine2083d56", | ||
"version": "1.0.0-mainf5733251", | ||
"type": "module", | ||
@@ -6,0 +6,0 @@ "private": false, |
103
README.md
@@ -46,2 +46,3 @@ <p align="center"> | ||
- [getPackageManagerExecutable()](#getpackagemanagerexecutable) | ||
- [graphqlFetchWithRetry()](#graphqlfetchwithretry) | ||
- [installDependencies()](#installdependencies) | ||
@@ -349,3 +350,3 @@ - [intro()](#intro) | ||
Defined in: [sdk/utils/src/retry.ts:52](https://github.com/settlemint/sdk/blob/v1.0.0/sdk/utils/src/retry.ts#L52) | ||
Defined in: [sdk/utils/src/http/fetch-with-retry.ts:18](https://github.com/settlemint/sdk/blob/v1.0.0/sdk/utils/src/http/fetch-with-retry.ts#L18) | ||
@@ -374,2 +375,10 @@ Retry an HTTP request with exponential backoff and jitter. | ||
##### Example | ||
```ts | ||
import { fetchWithRetry } from "@settlemint/sdk-utils"; | ||
const response = await fetchWithRetry("https://api.example.com/data"); | ||
``` | ||
*** | ||
@@ -381,3 +390,3 @@ | ||
Defined in: [sdk/utils/src/filesystem/mono-repo.ts:49](https://github.com/settlemint/sdk/blob/v1.0.0/sdk/utils/src/filesystem/mono-repo.ts#L49) | ||
Defined in: [sdk/utils/src/filesystem/mono-repo.ts:59](https://github.com/settlemint/sdk/blob/v1.0.0/sdk/utils/src/filesystem/mono-repo.ts#L59) | ||
@@ -398,2 +407,11 @@ Finds all packages in a monorepo | ||
##### Example | ||
```ts | ||
import { findMonoRepoPackages } from "@settlemint/sdk-utils"; | ||
const packages = await findMonoRepoPackages("/path/to/your/project"); | ||
console.log(packages); // Output: ["/path/to/your/project/packages/core", "/path/to/your/project/packages/ui"] | ||
``` | ||
*** | ||
@@ -405,3 +423,3 @@ | ||
Defined in: [sdk/utils/src/filesystem/mono-repo.ts:14](https://github.com/settlemint/sdk/blob/v1.0.0/sdk/utils/src/filesystem/mono-repo.ts#L14) | ||
Defined in: [sdk/utils/src/filesystem/mono-repo.ts:19](https://github.com/settlemint/sdk/blob/v1.0.0/sdk/utils/src/filesystem/mono-repo.ts#L19) | ||
@@ -422,2 +440,11 @@ Finds the root directory of a monorepo | ||
##### Example | ||
```ts | ||
import { findMonoRepoRoot } from "@settlemint/sdk-utils"; | ||
const root = await findMonoRepoRoot("/path/to/your/project"); | ||
console.log(root); // Output: /path/to/your/project/packages/core | ||
``` | ||
*** | ||
@@ -517,2 +544,61 @@ | ||
#### graphqlFetchWithRetry() | ||
> **graphqlFetchWithRetry**\<`Data`\>(`input`, `init`?, `maxRetries`?, `initialSleepTime`?): `Promise`\<`Data`\> | ||
Defined in: [sdk/utils/src/http/graphql-fetch-with-retry.ts:34](https://github.com/settlemint/sdk/blob/v1.0.0/sdk/utils/src/http/graphql-fetch-with-retry.ts#L34) | ||
Executes a GraphQL request with automatic retries using exponential backoff and jitter. | ||
Only retries on server errors (5xx), rate limits (429), timeouts (408), and network errors. | ||
Will also retry if the GraphQL response contains errors. | ||
##### Type Parameters | ||
| Type Parameter | | ||
| ------ | | ||
| `Data` | | ||
##### Parameters | ||
| Parameter | Type | Default value | Description | | ||
| ------ | ------ | ------ | ------ | | ||
| `input` | `URL` \| `RequestInfo` | `undefined` | The URL or Request object for the GraphQL endpoint | | ||
| `init`? | `RequestInit` | `undefined` | Optional fetch configuration options | | ||
| `maxRetries`? | `number` | `5` | Maximum retry attempts before failing (default: 5) | | ||
| `initialSleepTime`? | `number` | `3_000` | Initial delay between retries in milliseconds (default: 3000) | | ||
##### Returns | ||
`Promise`\<`Data`\> | ||
The parsed GraphQL response data | ||
##### Throws | ||
Error if all retries fail or if GraphQL response contains errors | ||
##### Example | ||
```ts | ||
import { graphqlFetchWithRetry } from "@settlemint/sdk-utils"; | ||
const data = await graphqlFetchWithRetry<{ user: { id: string } }>( | ||
"https://api.example.com/graphql", | ||
{ | ||
method: "POST", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ | ||
query: `query GetUser($id: ID!) { | ||
user(id: $id) { | ||
id | ||
} | ||
}`, | ||
variables: { id: "123" } | ||
}) | ||
} | ||
); | ||
``` | ||
*** | ||
#### installDependencies() | ||
@@ -831,3 +917,3 @@ | ||
Defined in: [sdk/utils/src/retry.ts:9](https://github.com/settlemint/sdk/blob/v1.0.0/sdk/utils/src/retry.ts#L9) | ||
Defined in: [sdk/utils/src/retry.ts:14](https://github.com/settlemint/sdk/blob/v1.0.0/sdk/utils/src/retry.ts#L14) | ||
@@ -857,2 +943,11 @@ Retry a function when it fails. | ||
##### Example | ||
```ts | ||
import { retryWhenFailed } from "@settlemint/sdk-utils"; | ||
import { readFile } from "node:fs/promises"; | ||
const result = await retryWhenFailed(() => readFile("/path/to/file.txt"), 3, 1_000); | ||
``` | ||
*** | ||
@@ -859,0 +954,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
665671
5047
1636