@zauru-sdk/services 6.11.10 → 6.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { bindHttpClientPolicyRuntime, getHttpClientPolicy, httpTimeoutMsForClient, isRetryableHttpError, } from "@zauru-sdk/common";
|
|
2
|
+
/**
|
|
3
|
+
* Bind Node ALS without a static `node:async_hooks` import. Vite's client
|
|
4
|
+
* bundle stubs that builtin as `__vite-browser-external` and fails named
|
|
5
|
+
* exports. `process.getBuiltinModule` is runtime-only (Node 20.16+/22+).
|
|
6
|
+
* Older Node and the browser keep common's fallback store.
|
|
7
|
+
*/
|
|
8
|
+
function tryBindNodeAls() {
|
|
9
|
+
const processRef = globalThis.process;
|
|
10
|
+
if (!processRef?.versions?.node ||
|
|
11
|
+
typeof processRef.getBuiltinModule !== "function") {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
try {
|
|
15
|
+
const hooks = processRef.getBuiltinModule("async_hooks") ??
|
|
16
|
+
processRef.getBuiltinModule("node:async_hooks");
|
|
17
|
+
const AsyncLocalStorage = hooks?.AsyncLocalStorage;
|
|
18
|
+
if (!AsyncLocalStorage) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const als = new AsyncLocalStorage();
|
|
22
|
+
bindHttpClientPolicyRuntime({
|
|
23
|
+
get: () => als.getStore(),
|
|
24
|
+
run: (policy, fn) => als.run(policy, fn),
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
// Keep common's in-process fallback.
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
tryBindNodeAls();
|
|
32
|
+
export function applyHttpClientTimeout(request, client) {
|
|
33
|
+
if (request.timeout != null && request.timeout > 0) {
|
|
34
|
+
return request;
|
|
35
|
+
}
|
|
36
|
+
request.timeout = httpTimeoutMsForClient(client);
|
|
37
|
+
return request;
|
|
38
|
+
}
|
|
39
|
+
export async function maybeRetryHttpRequest(error, instance, client) {
|
|
40
|
+
const axiosError = error;
|
|
41
|
+
const config = axiosError.config;
|
|
42
|
+
if (!config) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
const policy = getHttpClientPolicy();
|
|
46
|
+
const retries = policy.retries ?? 0;
|
|
47
|
+
const attempt = config.__zauruRetryCount ?? 0;
|
|
48
|
+
if (attempt >= retries || !isRetryableHttpError(axiosError, client)) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
config.__zauruRetryCount = attempt + 1;
|
|
52
|
+
console.log(`[http] retry ${config.__zauruRetryCount}/${retries} ${client} ${(config.method ?? "get").toUpperCase()} ${config.baseURL ?? ""}${config.url ?? ""}`);
|
|
53
|
+
return instance.request(config);
|
|
54
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import axios from "axios";
|
|
2
2
|
import chalk from "chalk";
|
|
3
3
|
import { config } from "@zauru-sdk/config";
|
|
4
|
+
import { applyHttpClientTimeout, maybeRetryHttpRequest, } from "./httpClientRuntime.js";
|
|
4
5
|
const axiosInstance = axios.create({
|
|
5
6
|
baseURL: `${config.graphqlAPIBaseURL}`,
|
|
6
7
|
});
|
|
@@ -8,7 +9,7 @@ axiosInstance.interceptors.request.use(function (request) {
|
|
|
8
9
|
// Do something before request is sent
|
|
9
10
|
const baseName = `${request.baseURL}${request.url}`;
|
|
10
11
|
console.log(chalk.green(baseName));
|
|
11
|
-
request
|
|
12
|
+
applyHttpClientTimeout(request, "graphql");
|
|
12
13
|
request.metadata = { startTime: Date.now() };
|
|
13
14
|
if (config.debugHTTP) {
|
|
14
15
|
console.log(chalk.cyan(`[DEBUG] REQUEST METHOD: ${request.method?.toUpperCase()}`));
|
|
@@ -54,7 +55,11 @@ axiosInstance.interceptors.response.use(function (response) {
|
|
|
54
55
|
console.log(chalk.cyan("[DEBUG] RESPONSE DATA:"), JSON.stringify(response.data, null, 2));
|
|
55
56
|
}
|
|
56
57
|
return response;
|
|
57
|
-
}, function (error) {
|
|
58
|
+
}, async function (error) {
|
|
59
|
+
const retried = await maybeRetryHttpRequest(error, axiosInstance, "graphql");
|
|
60
|
+
if (retried) {
|
|
61
|
+
return retried;
|
|
62
|
+
}
|
|
58
63
|
console.log(chalk.red("---------------- ERROR CON REQUEST GRAPHQL ----------------"));
|
|
59
64
|
console.log(`${error}`);
|
|
60
65
|
// Do something with response error
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import axios from "axios";
|
|
2
2
|
import chalk from "chalk";
|
|
3
3
|
import { config } from "@zauru-sdk/config";
|
|
4
|
+
import { applyHttpClientTimeout, maybeRetryHttpRequest, } from "./httpClientRuntime.js";
|
|
4
5
|
const axiosInstance = axios.create({
|
|
5
6
|
baseURL: `${config.zauruBaseURL}`,
|
|
6
7
|
});
|
|
@@ -8,7 +9,7 @@ axiosInstance.interceptors.request.use(function (request) {
|
|
|
8
9
|
// Do something before request is sent
|
|
9
10
|
const baseName = `${request.baseURL}${request.url}`;
|
|
10
11
|
console.log(chalk.green(baseName));
|
|
11
|
-
request
|
|
12
|
+
applyHttpClientTimeout(request, "zauru");
|
|
12
13
|
request.metadata = { startTime: Date.now() };
|
|
13
14
|
if (config.debugHTTP) {
|
|
14
15
|
console.log(chalk.cyan(`[DEBUG] REQUEST METHOD: ${request.method?.toUpperCase()}`));
|
|
@@ -54,7 +55,11 @@ axiosInstance.interceptors.response.use(function (response) {
|
|
|
54
55
|
console.log(chalk.cyan("[DEBUG] RESPONSE DATA:"), JSON.stringify(response.data, null, 2));
|
|
55
56
|
}
|
|
56
57
|
return response;
|
|
57
|
-
}, function (error) {
|
|
58
|
+
}, async function (error) {
|
|
59
|
+
const retried = await maybeRetryHttpRequest(error, axiosInstance, "zauru");
|
|
60
|
+
if (retried) {
|
|
61
|
+
return retried;
|
|
62
|
+
}
|
|
58
63
|
console.log(chalk.red("---------------- ERROR CON REQUEST (RESPONSE INTERCEPTOR) ----------------"));
|
|
59
64
|
// Do something with response error
|
|
60
65
|
const { response } = error;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { AxiosInstance, InternalAxiosRequestConfig } from "axios";
|
|
2
|
+
import { type HttpClientKind } from "@zauru-sdk/common";
|
|
3
|
+
export declare function applyHttpClientTimeout(request: InternalAxiosRequestConfig, client: HttpClientKind): InternalAxiosRequestConfig;
|
|
4
|
+
export declare function maybeRetryHttpRequest(error: unknown, instance: AxiosInstance, client: HttpClientKind): Promise<unknown | null>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zauru-sdk/services",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.13.0",
|
|
4
4
|
"description": "Servicios de consulta a Zauru",
|
|
5
5
|
"main": "./dist/esm/index.js",
|
|
6
6
|
"module": "./dist/esm/index.js",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"scripts": {
|
|
14
14
|
"build": "npm run build:esm",
|
|
15
15
|
"build:esm": "tsc -p tsconfig.esm.json",
|
|
16
|
-
"test": "
|
|
16
|
+
"test": "tsx --test src/zauru/httpClientRuntime.test.ts"
|
|
17
17
|
},
|
|
18
18
|
"files": [
|
|
19
19
|
"dist"
|
|
@@ -21,18 +21,19 @@
|
|
|
21
21
|
"author": "Oscar Cuéllar",
|
|
22
22
|
"license": "MIT",
|
|
23
23
|
"devDependencies": {
|
|
24
|
+
"tsx": "^4.23.15",
|
|
24
25
|
"typescript": "^5.1.6"
|
|
25
26
|
},
|
|
26
27
|
"dependencies": {
|
|
27
28
|
"@remix-run/node": "^2.8.1",
|
|
28
29
|
"@upstash/redis": "^1.34.5",
|
|
29
|
-
"@zauru-sdk/common": "^6.
|
|
30
|
+
"@zauru-sdk/common": "^6.10.0",
|
|
30
31
|
"@zauru-sdk/config": "^6.0.0",
|
|
31
|
-
"@zauru-sdk/graphql": "^6.
|
|
32
|
+
"@zauru-sdk/graphql": "^6.9.0",
|
|
32
33
|
"@zauru-sdk/types": "^6.24.0",
|
|
33
34
|
"axios": "^1.6.7",
|
|
34
35
|
"chalk": "5.3.0",
|
|
35
36
|
"moment": "^2.30.1"
|
|
36
37
|
},
|
|
37
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "2daa677e6cd4948b23bfbf862929b4c74655e4e0"
|
|
38
39
|
}
|