@routar/axios 0.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/LICENSE +21 -0
- package/README.md +11 -0
- package/dist/index.cjs +29 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +46 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/package.json +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Kyungbae Min
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# @routar/axios
|
|
2
|
+
|
|
3
|
+
Axios-based executor for [routar](https://github.com/kbmin1129/routar) — transport adapter using Axios.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @routar/core @routar/axios axios
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
See the [main README](https://github.com/kbmin1129/routar) for full documentation.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@routar/core');
|
|
4
|
+
|
|
5
|
+
// src/create-axios-executor.ts
|
|
6
|
+
function resolveInstance(input) {
|
|
7
|
+
if ("interceptors" in input) {
|
|
8
|
+
return input;
|
|
9
|
+
}
|
|
10
|
+
return input();
|
|
11
|
+
}
|
|
12
|
+
function createAxiosExecutor(instanceOrFactory, options) {
|
|
13
|
+
return core.createExecutor(async ({ method, url, params, body, headers, signal }) => {
|
|
14
|
+
const instance = await resolveInstance(instanceOrFactory);
|
|
15
|
+
const { data } = await instance.request({
|
|
16
|
+
method,
|
|
17
|
+
url,
|
|
18
|
+
params,
|
|
19
|
+
data: body,
|
|
20
|
+
headers,
|
|
21
|
+
signal
|
|
22
|
+
});
|
|
23
|
+
return data;
|
|
24
|
+
}, options?.middlewares);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
exports.createAxiosExecutor = createAxiosExecutor;
|
|
28
|
+
//# sourceMappingURL=index.cjs.map
|
|
29
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/create-axios-executor.ts"],"names":["createExecutor"],"mappings":";;;;;AAwBA,SAAS,gBAAgB,KAAA,EAAkE;AAEzF,EAAA,IAAI,kBAAmB,KAAA,EAAkB;AACvC,IAAA,OAAO,KAAA;AAAA,EACT;AACA,EAAA,OAAQ,KAAA,EAA0B;AACpC;AA6BO,SAAS,mBAAA,CACd,mBACA,OAAA,EAGU;AACV,EAAA,OAAOA,mBAAA,CAAe,OAAO,EAAE,MAAA,EAAQ,KAAK,MAAA,EAAQ,IAAA,EAAM,OAAA,EAAS,MAAA,EAAO,KAAM;AAC9E,IAAA,MAAM,QAAA,GAAW,MAAM,eAAA,CAAgB,iBAAiB,CAAA;AACxD,IAAA,MAAM,EAAE,IAAA,EAAK,GAAI,MAAM,SAAS,OAAA,CAAQ;AAAA,MACtC,MAAA;AAAA,MACA,GAAA;AAAA,MACA,MAAA;AAAA,MACA,IAAA,EAAM,IAAA;AAAA,MACN,OAAA;AAAA,MACA;AAAA,KACD,CAAA;AACD,IAAA,OAAO,IAAA;AAAA,EACT,CAAA,EAAG,SAAS,WAAW,CAAA;AACzB","file":"index.cjs","sourcesContent":["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(input: InstanceOrFactory): AxiosInstance | Promise<AxiosInstance> {\n // AxiosInstance is callable but always has `interceptors`; plain factory functions do not.\n if ('interceptors' in (input as object)) {\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(async ({ method, url, params, body, headers, signal }) => {\n const instance = await resolveInstance(instanceOrFactory);\n const { data } = await instance.request({\n method,\n url,\n params,\n data: body,\n headers,\n signal,\n });\n return data;\n }, options?.middlewares);\n}\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { ExecutorMiddleware, Executor } from '@routar/core';
|
|
2
|
+
import { AxiosInstance } from 'axios';
|
|
3
|
+
|
|
4
|
+
/** Zero-argument factory that returns an Axios instance (optionally async). */
|
|
5
|
+
type InstanceFactory = () => AxiosInstance | Promise<AxiosInstance>;
|
|
6
|
+
/**
|
|
7
|
+
* Accepted input for {@link createAxiosExecutor}.
|
|
8
|
+
*
|
|
9
|
+
* Pass a pre-configured `AxiosInstance` for CSR (the same instance is reused
|
|
10
|
+
* across requests, which preserves interceptors and connection pooling).
|
|
11
|
+
* Pass a factory function for SSR so a fresh instance — with per-request
|
|
12
|
+
* headers or tokens — can be created on each call.
|
|
13
|
+
*/
|
|
14
|
+
type InstanceOrFactory = AxiosInstance | InstanceFactory;
|
|
15
|
+
/**
|
|
16
|
+
* Creates an {@link Executor} backed by Axios.
|
|
17
|
+
*
|
|
18
|
+
* Suited for CSR where a single shared `AxiosInstance` is preferred (fast,
|
|
19
|
+
* interceptor-friendly). Also supports SSR via a factory that produces a
|
|
20
|
+
* fresh instance per request, allowing dynamic per-request auth headers.
|
|
21
|
+
*
|
|
22
|
+
* Axios error objects (`AxiosError`) propagate unchanged through the executor
|
|
23
|
+
* so you can inspect `err.response`, `err.code`, etc. in middleware or
|
|
24
|
+
* callers — use `withRetry`'s `shouldRetry` option to skip retries on 4xx.
|
|
25
|
+
*
|
|
26
|
+
* @param instanceOrFactory - A pre-built `AxiosInstance` (CSR) or a factory
|
|
27
|
+
* function that returns one (SSR / per-request config).
|
|
28
|
+
* @param options.middlewares - Middleware chain applied before the Axios call.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* // CSR — shared instance
|
|
33
|
+
* const executor = createAxiosExecutor(axios.create({ baseURL: 'https://api.example.com' }));
|
|
34
|
+
*
|
|
35
|
+
* // SSR — factory
|
|
36
|
+
* const executor = createAxiosExecutor(async () => {
|
|
37
|
+
* const token = await getServerToken();
|
|
38
|
+
* return axios.create({ baseURL: 'https://api.example.com', headers: { Authorization: `Bearer ${token}` } });
|
|
39
|
+
* });
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
declare function createAxiosExecutor(instanceOrFactory: InstanceOrFactory, options?: {
|
|
43
|
+
middlewares?: ExecutorMiddleware[];
|
|
44
|
+
}): Executor;
|
|
45
|
+
|
|
46
|
+
export { createAxiosExecutor };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { ExecutorMiddleware, Executor } from '@routar/core';
|
|
2
|
+
import { AxiosInstance } from 'axios';
|
|
3
|
+
|
|
4
|
+
/** Zero-argument factory that returns an Axios instance (optionally async). */
|
|
5
|
+
type InstanceFactory = () => AxiosInstance | Promise<AxiosInstance>;
|
|
6
|
+
/**
|
|
7
|
+
* Accepted input for {@link createAxiosExecutor}.
|
|
8
|
+
*
|
|
9
|
+
* Pass a pre-configured `AxiosInstance` for CSR (the same instance is reused
|
|
10
|
+
* across requests, which preserves interceptors and connection pooling).
|
|
11
|
+
* Pass a factory function for SSR so a fresh instance — with per-request
|
|
12
|
+
* headers or tokens — can be created on each call.
|
|
13
|
+
*/
|
|
14
|
+
type InstanceOrFactory = AxiosInstance | InstanceFactory;
|
|
15
|
+
/**
|
|
16
|
+
* Creates an {@link Executor} backed by Axios.
|
|
17
|
+
*
|
|
18
|
+
* Suited for CSR where a single shared `AxiosInstance` is preferred (fast,
|
|
19
|
+
* interceptor-friendly). Also supports SSR via a factory that produces a
|
|
20
|
+
* fresh instance per request, allowing dynamic per-request auth headers.
|
|
21
|
+
*
|
|
22
|
+
* Axios error objects (`AxiosError`) propagate unchanged through the executor
|
|
23
|
+
* so you can inspect `err.response`, `err.code`, etc. in middleware or
|
|
24
|
+
* callers — use `withRetry`'s `shouldRetry` option to skip retries on 4xx.
|
|
25
|
+
*
|
|
26
|
+
* @param instanceOrFactory - A pre-built `AxiosInstance` (CSR) or a factory
|
|
27
|
+
* function that returns one (SSR / per-request config).
|
|
28
|
+
* @param options.middlewares - Middleware chain applied before the Axios call.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* // CSR — shared instance
|
|
33
|
+
* const executor = createAxiosExecutor(axios.create({ baseURL: 'https://api.example.com' }));
|
|
34
|
+
*
|
|
35
|
+
* // SSR — factory
|
|
36
|
+
* const executor = createAxiosExecutor(async () => {
|
|
37
|
+
* const token = await getServerToken();
|
|
38
|
+
* return axios.create({ baseURL: 'https://api.example.com', headers: { Authorization: `Bearer ${token}` } });
|
|
39
|
+
* });
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
declare function createAxiosExecutor(instanceOrFactory: InstanceOrFactory, options?: {
|
|
43
|
+
middlewares?: ExecutorMiddleware[];
|
|
44
|
+
}): Executor;
|
|
45
|
+
|
|
46
|
+
export { createAxiosExecutor };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { createExecutor } from '@routar/core';
|
|
2
|
+
|
|
3
|
+
// src/create-axios-executor.ts
|
|
4
|
+
function resolveInstance(input) {
|
|
5
|
+
if ("interceptors" in input) {
|
|
6
|
+
return input;
|
|
7
|
+
}
|
|
8
|
+
return input();
|
|
9
|
+
}
|
|
10
|
+
function createAxiosExecutor(instanceOrFactory, options) {
|
|
11
|
+
return createExecutor(async ({ method, url, params, body, headers, signal }) => {
|
|
12
|
+
const instance = await resolveInstance(instanceOrFactory);
|
|
13
|
+
const { data } = await instance.request({
|
|
14
|
+
method,
|
|
15
|
+
url,
|
|
16
|
+
params,
|
|
17
|
+
data: body,
|
|
18
|
+
headers,
|
|
19
|
+
signal
|
|
20
|
+
});
|
|
21
|
+
return data;
|
|
22
|
+
}, options?.middlewares);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { createAxiosExecutor };
|
|
26
|
+
//# sourceMappingURL=index.js.map
|
|
27
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/create-axios-executor.ts"],"names":[],"mappings":";;;AAwBA,SAAS,gBAAgB,KAAA,EAAkE;AAEzF,EAAA,IAAI,kBAAmB,KAAA,EAAkB;AACvC,IAAA,OAAO,KAAA;AAAA,EACT;AACA,EAAA,OAAQ,KAAA,EAA0B;AACpC;AA6BO,SAAS,mBAAA,CACd,mBACA,OAAA,EAGU;AACV,EAAA,OAAO,cAAA,CAAe,OAAO,EAAE,MAAA,EAAQ,KAAK,MAAA,EAAQ,IAAA,EAAM,OAAA,EAAS,MAAA,EAAO,KAAM;AAC9E,IAAA,MAAM,QAAA,GAAW,MAAM,eAAA,CAAgB,iBAAiB,CAAA;AACxD,IAAA,MAAM,EAAE,IAAA,EAAK,GAAI,MAAM,SAAS,OAAA,CAAQ;AAAA,MACtC,MAAA;AAAA,MACA,GAAA;AAAA,MACA,MAAA;AAAA,MACA,IAAA,EAAM,IAAA;AAAA,MACN,OAAA;AAAA,MACA;AAAA,KACD,CAAA;AACD,IAAA,OAAO,IAAA;AAAA,EACT,CAAA,EAAG,SAAS,WAAW,CAAA;AACzB","file":"index.js","sourcesContent":["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(input: InstanceOrFactory): AxiosInstance | Promise<AxiosInstance> {\n // AxiosInstance is callable but always has `interceptors`; plain factory functions do not.\n if ('interceptors' in (input as object)) {\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(async ({ method, url, params, body, headers, signal }) => {\n const instance = await resolveInstance(instanceOrFactory);\n const { data } = await instance.request({\n method,\n url,\n params,\n data: body,\n headers,\n signal,\n });\n return data;\n }, options?.middlewares);\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@routar/axios",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Axios-based executor for routar — transport adapter using Axios",
|
|
5
|
+
"keywords": ["api", "http", "typescript", "axios", "routar", "executor"],
|
|
6
|
+
"author": "Kyungbae Min",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/minr2kb/routar.git",
|
|
11
|
+
"directory": "packages/axios"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/minr2kb/routar#readme",
|
|
14
|
+
"type": "module",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"import": "./dist/index.js",
|
|
19
|
+
"require": "./dist/index.cjs"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"main": "./dist/index.cjs",
|
|
23
|
+
"module": "./dist/index.js",
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"files": ["dist", "README.md", "LICENSE"],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsup",
|
|
28
|
+
"dev": "tsup --watch"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"axios": ">=1.0.0",
|
|
32
|
+
"@routar/core": "^0.1.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@routar/core": "workspace:*",
|
|
36
|
+
"axios": "^1.16.1",
|
|
37
|
+
"tsup": "^8.0.0",
|
|
38
|
+
"typescript": "^6.0.0"
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": { "access": "public" }
|
|
41
|
+
}
|