@routar/axios 0.1.1 → 1.1.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.
- package/dist/index.cjs +7 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +6 -2
- package/dist/index.js.map +1 -1
- package/package.json +2 -3
package/dist/index.cjs
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
// ../core/dist/index.js
|
|
4
|
+
function createExecutor(execute, middlewares = []) {
|
|
5
|
+
const chain = middlewares.reduceRight((next, mw) => (opts) => mw(opts, next), execute);
|
|
6
|
+
return { execute: chain };
|
|
7
|
+
}
|
|
4
8
|
|
|
5
9
|
// src/create-axios-executor.ts
|
|
6
10
|
function resolveInstance(input) {
|
|
7
|
-
if ("interceptors" in input) {
|
|
11
|
+
if ("interceptors" in input && typeof input.request === "function") {
|
|
8
12
|
return input;
|
|
9
13
|
}
|
|
10
14
|
return input();
|
|
11
15
|
}
|
|
12
16
|
function createAxiosExecutor(instanceOrFactory, options) {
|
|
13
|
-
return
|
|
17
|
+
return createExecutor(
|
|
14
18
|
async ({ method, url, params, body, headers, signal }) => {
|
|
15
19
|
const instance = await resolveInstance(instanceOrFactory);
|
|
16
20
|
const base = (instance.defaults.baseURL ?? "").replace(/\/$/, "");
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/create-axios-executor.ts"],"names":[
|
|
1
|
+
{"version":3,"sources":["../../core/src/create-executor.ts","../src/create-axios-executor.ts"],"names":[],"mappings":";;;AAuBO,SAAS,cAAA,CACd,OAAA,EACA,WAAA,GAAoC,EAAA,EAC1B;AACV,EAAA,MAAM,KAAA,GAAQ,WAAA,CAAY,WAAA,CAExB,CAAC,IAAA,EAAM,EAAA,KAAO,CAAC,IAAA,KAAS,EAAA,CAAG,IAAA,EAAM,IAAI,CAAA,EAAG,OAAO,CAAA;AACjD,EAAA,OAAO,EAAE,SAAS,KAAA,EAAA;AACpB;;;ACPA,SAAS,gBACP,KAAA,EACwC;AAGxC,EAAA,IAAI,cAAA,IAAmB,KAAA,IAAoB,OAAQ,KAAA,CAAc,YAAY,UAAA,EAAY;AACvF,IAAA,OAAO,KAAA;AAAA,EACT;AACA,EAAA,OAAQ,KAAA,EAA0B;AACpC;AA6BO,SAAS,mBAAA,CACd,mBACA,OAAA,EAGU;AACV,EAAA,OAAO,cAAA;AAAA,IACL,OAAO,EAAE,MAAA,EAAQ,GAAA,EAAK,QAAQ,IAAA,EAAM,OAAA,EAAS,QAAO,KAAM;AACxD,MAAA,MAAM,QAAA,GAAW,MAAM,eAAA,CAAgB,iBAAiB,CAAA;AACxD,MAAA,MAAM,QAAQ,QAAA,CAAS,QAAA,CAAS,WAAW,EAAA,EAAI,OAAA,CAAQ,OAAO,EAAE,CAAA;AAChE,MAAA,MAAM,EAAE,IAAA,EAAK,GAAI,MAAM,SAAS,OAAA,CAAQ;AAAA,QACtC,MAAA;AAAA,QACA,KAAK,IAAA,GAAO,GAAA;AAAA,QACZ,OAAA,EAAS,EAAA;AAAA,QACT,MAAA;AAAA,QACA,IAAA,EAAM,IAAA;AAAA,QACN,OAAA;AAAA,QACA;AAAA,OACD,CAAA;AACD,MAAA,OAAO,IAAA;AAAA,IACT,CAAA;AAAA,IACA,OAAA,EAAS;AAAA,GACX;AACF","file":"index.cjs","sourcesContent":["import type { ExecuteOptions, Executor, ExecutorMiddleware } from \"./types.js\";\n\n/**\n * Creates an {@link Executor} by wrapping a transport function with an\n * optional middleware chain.\n *\n * Middlewares are applied in declaration order — the first middleware is the\n * outermost wrapper and runs first on each request.\n *\n * @param execute - The underlying transport function (fetch, axios, etc.).\n * @param middlewares - Ordered list of middlewares to apply.\n *\n * @example\n * ```ts\n * const executor = createExecutor(\n * async ({ method, url, body }) => {\n * const res = await fetch(url, { method, body: JSON.stringify(body) });\n * return res.json();\n * },\n * [withTimeout(5000), withRetry(3), withLogger()],\n * );\n * ```\n */\nexport function createExecutor(\n execute: (options: ExecuteOptions) => Promise<unknown>,\n middlewares: ExecutorMiddleware[] = [],\n): Executor {\n const chain = middlewares.reduceRight<\n (options: ExecuteOptions) => Promise<unknown>\n >((next, mw) => (opts) => mw(opts, next), execute);\n return { execute: chain };\n}\n\n/**\n * Creates an {@link Executor} that selects the underlying transport at\n * request time based on the result of `resolver`.\n *\n * Use this to unify SSR and CSR behind a single API client — the resolver\n * picks the right executor per request, so `createApi` is called once and\n * works in both environments without duplicate `*ServerApi` instances.\n *\n * The resolver receives the full {@link ExecuteOptions} so it can branch on\n * environment, URL prefix, auth context, or any runtime condition.\n *\n * @param resolver - Called on every request; returns the executor to delegate to.\n *\n * @example\n * ```ts\n * // SSR vs CSR — pick transport based on environment\n * const apiExecutor = dispatchExecutor(() =>\n * typeof window === 'undefined' ? serverExecutor : clientExecutor,\n * );\n *\n * // Route by URL prefix — internal routes use a different transport\n * const apiExecutor = dispatchExecutor((opts) =>\n * opts.url.startsWith('/internal') ? internalExecutor : publicExecutor,\n * );\n * ```\n */\nexport function dispatchExecutor(\n resolver: (opts: ExecuteOptions) => Executor,\n): Executor {\n return {\n execute: (opts) => resolver(opts).execute(opts),\n };\n}\n","import type { Executor, ExecutorMiddleware } from \"@routar/core\";\nimport { createExecutor } from \"@routar/core\";\nimport type { AxiosInstance } from \"axios\";\n\n/** Zero-argument factory that returns an Axios instance (optionally async). */\ntype InstanceFactory = () => AxiosInstance | Promise<AxiosInstance>;\n\n/**\n * Accepted input for {@link createAxiosExecutor}.\n *\n * Pass a pre-configured `AxiosInstance` for CSR (the same instance is reused\n * across requests, which preserves interceptors and connection pooling).\n * Pass a factory function for SSR so a fresh instance — with per-request\n * headers or tokens — can be created on each call.\n */\nexport type InstanceOrFactory = AxiosInstance | InstanceFactory;\n\n/**\n * Discriminates between an `AxiosInstance` and a plain factory function.\n *\n * `AxiosInstance` is itself callable, so `typeof input === 'function'` cannot\n * distinguish the two. Duck-typing via `interceptors` works because Axios\n * always attaches it to instances, while user-supplied factories do not.\n */\nfunction resolveInstance(\n input: InstanceOrFactory,\n): AxiosInstance | Promise<AxiosInstance> {\n // AxiosInstance is callable but always has `interceptors` and a `request` method;\n // plain factory functions have neither.\n if (\"interceptors\" in (input as object) && typeof (input as any).request === \"function\") {\n return input as AxiosInstance;\n }\n return (input as InstanceFactory)();\n}\n\n/**\n * Creates an {@link Executor} backed by Axios.\n *\n * Suited for CSR where a single shared `AxiosInstance` is preferred (fast,\n * interceptor-friendly). Also supports SSR via a factory that produces a\n * fresh instance per request, allowing dynamic per-request auth headers.\n *\n * Axios error objects (`AxiosError`) propagate unchanged through the executor\n * so you can inspect `err.response`, `err.code`, etc. in middleware or\n * callers — use `withRetry`'s `shouldRetry` option to skip retries on 4xx.\n *\n * @param instanceOrFactory - A pre-built `AxiosInstance` (CSR) or a factory\n * function that returns one (SSR / per-request config).\n * @param options.middlewares - Middleware chain applied before the Axios call.\n *\n * @example\n * ```ts\n * // CSR — shared instance\n * const executor = createAxiosExecutor(axios.create({ baseURL: 'https://api.example.com' }));\n *\n * // SSR — factory\n * const executor = createAxiosExecutor(async () => {\n * const token = await getServerToken();\n * return axios.create({ baseURL: 'https://api.example.com', headers: { Authorization: `Bearer ${token}` } });\n * });\n * ```\n */\nexport function createAxiosExecutor(\n instanceOrFactory: InstanceOrFactory,\n options?: {\n middlewares?: ExecutorMiddleware[];\n },\n): Executor {\n return createExecutor(\n async ({ method, url, params, body, headers, signal }) => {\n const instance = await resolveInstance(instanceOrFactory);\n const base = (instance.defaults.baseURL ?? \"\").replace(/\\/$/, \"\");\n const { data } = await instance.request({\n method,\n url: base + url,\n baseURL: \"\",\n params,\n data: body,\n headers,\n signal,\n });\n return data;\n },\n options?.middlewares,\n );\n}\n"]}
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
// ../core/dist/index.js
|
|
2
|
+
function createExecutor(execute, middlewares = []) {
|
|
3
|
+
const chain = middlewares.reduceRight((next, mw) => (opts) => mw(opts, next), execute);
|
|
4
|
+
return { execute: chain };
|
|
5
|
+
}
|
|
2
6
|
|
|
3
7
|
// src/create-axios-executor.ts
|
|
4
8
|
function resolveInstance(input) {
|
|
5
|
-
if ("interceptors" in input) {
|
|
9
|
+
if ("interceptors" in input && typeof input.request === "function") {
|
|
6
10
|
return input;
|
|
7
11
|
}
|
|
8
12
|
return input();
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/create-axios-executor.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"sources":["../../core/src/create-executor.ts","../src/create-axios-executor.ts"],"names":[],"mappings":";AAuBO,SAAS,cAAA,CACd,OAAA,EACA,WAAA,GAAoC,EAAA,EAC1B;AACV,EAAA,MAAM,KAAA,GAAQ,WAAA,CAAY,WAAA,CAExB,CAAC,IAAA,EAAM,EAAA,KAAO,CAAC,IAAA,KAAS,EAAA,CAAG,IAAA,EAAM,IAAI,CAAA,EAAG,OAAO,CAAA;AACjD,EAAA,OAAO,EAAE,SAAS,KAAA,EAAA;AACpB;;;ACPA,SAAS,gBACP,KAAA,EACwC;AAGxC,EAAA,IAAI,cAAA,IAAmB,KAAA,IAAoB,OAAQ,KAAA,CAAc,YAAY,UAAA,EAAY;AACvF,IAAA,OAAO,KAAA;AAAA,EACT;AACA,EAAA,OAAQ,KAAA,EAA0B;AACpC;AA6BO,SAAS,mBAAA,CACd,mBACA,OAAA,EAGU;AACV,EAAA,OAAO,cAAA;AAAA,IACL,OAAO,EAAE,MAAA,EAAQ,GAAA,EAAK,QAAQ,IAAA,EAAM,OAAA,EAAS,QAAO,KAAM;AACxD,MAAA,MAAM,QAAA,GAAW,MAAM,eAAA,CAAgB,iBAAiB,CAAA;AACxD,MAAA,MAAM,QAAQ,QAAA,CAAS,QAAA,CAAS,WAAW,EAAA,EAAI,OAAA,CAAQ,OAAO,EAAE,CAAA;AAChE,MAAA,MAAM,EAAE,IAAA,EAAK,GAAI,MAAM,SAAS,OAAA,CAAQ;AAAA,QACtC,MAAA;AAAA,QACA,KAAK,IAAA,GAAO,GAAA;AAAA,QACZ,OAAA,EAAS,EAAA;AAAA,QACT,MAAA;AAAA,QACA,IAAA,EAAM,IAAA;AAAA,QACN,OAAA;AAAA,QACA;AAAA,OACD,CAAA;AACD,MAAA,OAAO,IAAA;AAAA,IACT,CAAA;AAAA,IACA,OAAA,EAAS;AAAA,GACX;AACF","file":"index.js","sourcesContent":["import type { ExecuteOptions, Executor, ExecutorMiddleware } from \"./types.js\";\n\n/**\n * Creates an {@link Executor} by wrapping a transport function with an\n * optional middleware chain.\n *\n * Middlewares are applied in declaration order — the first middleware is the\n * outermost wrapper and runs first on each request.\n *\n * @param execute - The underlying transport function (fetch, axios, etc.).\n * @param middlewares - Ordered list of middlewares to apply.\n *\n * @example\n * ```ts\n * const executor = createExecutor(\n * async ({ method, url, body }) => {\n * const res = await fetch(url, { method, body: JSON.stringify(body) });\n * return res.json();\n * },\n * [withTimeout(5000), withRetry(3), withLogger()],\n * );\n * ```\n */\nexport function createExecutor(\n execute: (options: ExecuteOptions) => Promise<unknown>,\n middlewares: ExecutorMiddleware[] = [],\n): Executor {\n const chain = middlewares.reduceRight<\n (options: ExecuteOptions) => Promise<unknown>\n >((next, mw) => (opts) => mw(opts, next), execute);\n return { execute: chain };\n}\n\n/**\n * Creates an {@link Executor} that selects the underlying transport at\n * request time based on the result of `resolver`.\n *\n * Use this to unify SSR and CSR behind a single API client — the resolver\n * picks the right executor per request, so `createApi` is called once and\n * works in both environments without duplicate `*ServerApi` instances.\n *\n * The resolver receives the full {@link ExecuteOptions} so it can branch on\n * environment, URL prefix, auth context, or any runtime condition.\n *\n * @param resolver - Called on every request; returns the executor to delegate to.\n *\n * @example\n * ```ts\n * // SSR vs CSR — pick transport based on environment\n * const apiExecutor = dispatchExecutor(() =>\n * typeof window === 'undefined' ? serverExecutor : clientExecutor,\n * );\n *\n * // Route by URL prefix — internal routes use a different transport\n * const apiExecutor = dispatchExecutor((opts) =>\n * opts.url.startsWith('/internal') ? internalExecutor : publicExecutor,\n * );\n * ```\n */\nexport function dispatchExecutor(\n resolver: (opts: ExecuteOptions) => Executor,\n): Executor {\n return {\n execute: (opts) => resolver(opts).execute(opts),\n };\n}\n","import type { Executor, ExecutorMiddleware } from \"@routar/core\";\nimport { createExecutor } from \"@routar/core\";\nimport type { AxiosInstance } from \"axios\";\n\n/** Zero-argument factory that returns an Axios instance (optionally async). */\ntype InstanceFactory = () => AxiosInstance | Promise<AxiosInstance>;\n\n/**\n * Accepted input for {@link createAxiosExecutor}.\n *\n * Pass a pre-configured `AxiosInstance` for CSR (the same instance is reused\n * across requests, which preserves interceptors and connection pooling).\n * Pass a factory function for SSR so a fresh instance — with per-request\n * headers or tokens — can be created on each call.\n */\nexport type InstanceOrFactory = AxiosInstance | InstanceFactory;\n\n/**\n * Discriminates between an `AxiosInstance` and a plain factory function.\n *\n * `AxiosInstance` is itself callable, so `typeof input === 'function'` cannot\n * distinguish the two. Duck-typing via `interceptors` works because Axios\n * always attaches it to instances, while user-supplied factories do not.\n */\nfunction resolveInstance(\n input: InstanceOrFactory,\n): AxiosInstance | Promise<AxiosInstance> {\n // AxiosInstance is callable but always has `interceptors` and a `request` method;\n // plain factory functions have neither.\n if (\"interceptors\" in (input as object) && typeof (input as any).request === \"function\") {\n return input as AxiosInstance;\n }\n return (input as InstanceFactory)();\n}\n\n/**\n * Creates an {@link Executor} backed by Axios.\n *\n * Suited for CSR where a single shared `AxiosInstance` is preferred (fast,\n * interceptor-friendly). Also supports SSR via a factory that produces a\n * fresh instance per request, allowing dynamic per-request auth headers.\n *\n * Axios error objects (`AxiosError`) propagate unchanged through the executor\n * so you can inspect `err.response`, `err.code`, etc. in middleware or\n * callers — use `withRetry`'s `shouldRetry` option to skip retries on 4xx.\n *\n * @param instanceOrFactory - A pre-built `AxiosInstance` (CSR) or a factory\n * function that returns one (SSR / per-request config).\n * @param options.middlewares - Middleware chain applied before the Axios call.\n *\n * @example\n * ```ts\n * // CSR — shared instance\n * const executor = createAxiosExecutor(axios.create({ baseURL: 'https://api.example.com' }));\n *\n * // SSR — factory\n * const executor = createAxiosExecutor(async () => {\n * const token = await getServerToken();\n * return axios.create({ baseURL: 'https://api.example.com', headers: { Authorization: `Bearer ${token}` } });\n * });\n * ```\n */\nexport function createAxiosExecutor(\n instanceOrFactory: InstanceOrFactory,\n options?: {\n middlewares?: ExecutorMiddleware[];\n },\n): Executor {\n return createExecutor(\n async ({ method, url, params, body, headers, signal }) => {\n const instance = await resolveInstance(instanceOrFactory);\n const base = (instance.defaults.baseURL ?? \"\").replace(/\\/$/, \"\");\n const { data } = await instance.request({\n method,\n url: base + url,\n baseURL: \"\",\n params,\n data: body,\n headers,\n signal,\n });\n return data;\n },\n options?.middlewares,\n );\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@routar/axios",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Axios-based executor for routar — transport adapter using Axios",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"api",
|
|
@@ -39,8 +39,7 @@
|
|
|
39
39
|
"dev": "tsup --watch"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
|
-
"axios": ">=1.0.0"
|
|
43
|
-
"@routar/core": "^0.1.0"
|
|
42
|
+
"axios": ">=1.0.0"
|
|
44
43
|
},
|
|
45
44
|
"devDependencies": {
|
|
46
45
|
"@routar/core": "workspace:*",
|