diff --git a/package.json b/package.json index 05f6aa5..4bbb9e6 100644 --- a/package.json +++ b/package.json @@ -31,24 +31,18 @@ "require": "./dist/interface.js", "default": "./dist/interface.js" }, + "./transactions": { + "types": "./dist/transactions/index.d.ts", + "import": "./dist/esm/transactions/index.js", + "require": "./dist/transactions/index.js", + "default": "./dist/transactions/index.js" + }, "./transactions/ledger": { "types": "./dist/transactions/ledger.d.ts", "import": "./dist/esm/transactions/ledger.js", "require": "./dist/transactions/ledger.js", "default": "./dist/transactions/ledger.js" }, - "./dist/*.js": { - "types": "./dist/*.d.ts", - "import": "./dist/esm/*.js", - "require": "./dist/*.js", - "default": "./dist/*.js" - }, - "./dist/*": { - "types": "./dist/*.d.ts", - "import": "./dist/esm/*.js", - "require": "./dist/*.js", - "default": "./dist/*.js" - }, "./package.json": "./package.json" }, "repository": "https://github.com/MetaNames/sdk.git", @@ -61,14 +55,15 @@ "dist" ], "scripts": { - "build": "yarn build:cjs && yarn build:esm", + "build": "yarn clean && yarn build:cjs && yarn build:esm", "docs": "typedoc --out docs src", "format": "eslint --fix src", "test": "jest -i", "prepublishOnly": "yarn build", "audit-ci": "bash scripts/audit-ci.sh", "build:cjs": "tsc", - "build:esm": "tsc -p tsconfig.esm.json && node scripts/finalize-esm.js" + "build:esm": "tsc -p tsconfig.esm.json && node scripts/finalize-esm.js", + "clean": "rm -rf dist" }, "dependencies": { "@ledgerhq/hw-transport": "^6.34.0", diff --git a/src/repositories/helpers/client.ts b/src/repositories/helpers/client.ts index 8cd32d4..8b71184 100644 --- a/src/repositories/helpers/client.ts +++ b/src/repositories/helpers/client.ts @@ -23,11 +23,27 @@ function buildOptions(method: RequestType, headers: Record, sign } export function getRequest(url: string, timeoutMs = DEFAULT_TIMEOUT_MS): Promise { - return handleFetch(promiseRetry(() => fetchWithTimeout(url, "GET", jsonHeaders, undefined, timeoutMs))) + return handleFetch(promiseRetry(() => request(url, "GET", jsonHeaders, undefined, timeoutMs))) } export function postRequest(url: string, body: unknown, timeoutMs = DEFAULT_TIMEOUT_MS): Promise { - return handleFetch(promiseRetry(() => fetchWithTimeout(url, "POST", jsonBodyHeaders, body, timeoutMs))) + return handleFetch(promiseRetry(() => request(url, "POST", jsonBodyHeaders, body, timeoutMs))) +} + +/** + * A reader node under load answers 429 or 503. That answer is not the + * contract's state, but returning it as `undefined` made every caller report a + * missing contract: `getAll()` against a busy node surfaced as "Contract not + * found". Those statuses are retried instead. 404 and the other client errors + * still fall through to `undefined`, which is how a missing AVL value is + * reported. + */ +function request(url: string, method: RequestType, headers: Record, body: unknown, timeoutMs: number): Promise { + return fetchWithTimeout(url, method, headers, body, timeoutMs).then((response) => { + if (response.status === 429 || response.status >= 500) throw new Error(`${method} ${url} failed with HTTP ${response.status}`) + + return response + }) } async function fetchWithTimeout(url: string, method: RequestType, headers: Record, body: unknown, timeoutMs: number): Promise {