@cratestack/runtime-axios 0.5.2

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Stephane Segning
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,22 @@
1
+ # @cratestack/runtime-axios
2
+
3
+ A `typeof fetch`-compatible transport for CrateStack's generated TypeScript RPC client
4
+ (`transport rpc` schemas), backed by [axios](https://axios-http.com/) instead of the global
5
+ `fetch` — useful for environments where axios is already the house HTTP client (interceptors,
6
+ proxy/agent config, request/response transforms you don't want to reimplement).
7
+
8
+ ## Usage
9
+
10
+ ```ts
11
+ import { createAxiosRuntime } from "@cratestack/runtime-axios";
12
+ import { CratestackRpcRuntime } from "./generated/runtime"; // your project's generated client
13
+ import myAxiosInstance from "./my-axios-instance";
14
+
15
+ const runtime = new CratestackRpcRuntime("https://api.example.com", {
16
+ fetch: createAxiosRuntime({ instance: myAxiosInstance }), // defaults to the `axios` singleton
17
+ });
18
+ ```
19
+
20
+ Every request is issued with `validateStatus: () => true` — a non-2xx response comes back as a
21
+ normal `Response` (so `CratestackRpcRuntime`/`RpcLink`s can inspect `response.status` and decode
22
+ the server's `RpcErrorBody` themselves) rather than axios throwing an `AxiosError`.
@@ -0,0 +1,16 @@
1
+ import { type AxiosInstance } from "axios";
2
+ /** Options for {@link createAxiosRuntime}. */
3
+ export interface AxiosRuntimeOptions {
4
+ /** Axios instance to issue requests through — e.g. one preconfigured
5
+ * with interceptors, a base URL, or a custom `httpsAgent`. Defaults
6
+ * to the top-level `axios` singleton. */
7
+ instance?: AxiosInstance;
8
+ }
9
+ /** Adapts an axios instance to the `typeof fetch` signature so it can be
10
+ * passed as `CratestackRpcClientOptions.fetch` (the generated runtime's
11
+ * constructor option) or as an `RpcLink`'s `request.fetchFn` override —
12
+ * axios becomes the actual transport while every link and the
13
+ * generated runtime keep speaking the Fetch API's `Request`/`Response`
14
+ * shape, unaware anything changed underneath. */
15
+ export declare function createAxiosRuntime(options?: AxiosRuntimeOptions): typeof fetch;
16
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAc,EAAE,KAAK,aAAa,EAAwC,MAAM,OAAO,CAAC;AASxF,8CAA8C;AAC9C,MAAM,WAAW,mBAAmB;IAClC;;8CAE0C;IAC1C,QAAQ,CAAC,EAAE,aAAa,CAAC;CAC1B;AAED;;;;;kDAKkD;AAClD,wBAAgB,kBAAkB,CAAC,OAAO,GAAE,mBAAwB,GAAG,OAAO,KAAK,CAqClF"}
package/dist/index.js ADDED
@@ -0,0 +1,101 @@
1
+ import axios from "axios";
2
+ // The Fetch API's `Response` constructor throws if given a non-null body
3
+ // alongside one of these statuses (https://fetch.spec.whatwg.org/#null-body-status)
4
+ // — even an empty `ArrayBuffer` counts as "non-null". axios always hands
5
+ // back a `data` buffer regardless of status, so it has to be dropped
6
+ // explicitly here rather than passed through.
7
+ const NULL_BODY_STATUSES = new Set([204, 205, 304]);
8
+ /** Adapts an axios instance to the `typeof fetch` signature so it can be
9
+ * passed as `CratestackRpcClientOptions.fetch` (the generated runtime's
10
+ * constructor option) or as an `RpcLink`'s `request.fetchFn` override —
11
+ * axios becomes the actual transport while every link and the
12
+ * generated runtime keep speaking the Fetch API's `Request`/`Response`
13
+ * shape, unaware anything changed underneath. */
14
+ export function createAxiosRuntime(options = {}) {
15
+ const instance = options.instance ?? axios;
16
+ return (async (input, init) => {
17
+ const resolved = await resolveRequest(input, init);
18
+ const config = {
19
+ url: resolved.url,
20
+ method: resolved.method,
21
+ headers: headersToObject(resolved.headers),
22
+ data: resolved.body,
23
+ // Built via conditional spread, not `signal: resolved.signal ??
24
+ // undefined` — with `exactOptionalPropertyTypes: true`, explicitly
25
+ // assigning `undefined` to `signal` isn't the same as omitting the
26
+ // key, and axios's `GenericAbortSignal` type (unlike `data`, which
27
+ // is `any`) rejects it.
28
+ ...(resolved.signal ? { signal: resolved.signal } : {}),
29
+ responseType: "arraybuffer",
30
+ // The generated runtime and every `RpcLink` decide success/failure
31
+ // from `response.status`/`response.ok` themselves (see
32
+ // `CratestackRpcRuntime.readUnaryResponse`) — axios's default of
33
+ // throwing on a non-2xx status would short-circuit that and turn
34
+ // every server-side `RpcErrorBody` into a thrown `AxiosError`
35
+ // instead of a normal `Response` the caller can inspect.
36
+ validateStatus: () => true,
37
+ };
38
+ const response = await instance.request(config);
39
+ return new Response(NULL_BODY_STATUSES.has(response.status) ? null : response.data, {
40
+ status: response.status,
41
+ statusText: response.statusText,
42
+ headers: axiosHeadersToFetchHeaders(response.headers),
43
+ });
44
+ });
45
+ }
46
+ /** The generated runtime only ever calls `fetchFn(url, init)` with a
47
+ * string URL and a populated `init` — this function's `Request`
48
+ * handling exists purely for `typeof fetch` compatibility with callers
49
+ * that pass a pre-built `Request` (e.g. from another link/middleware).
50
+ * `init`'s fields, when present, override the `Request`'s own — same
51
+ * precedence the real Fetch API gives `fetch(request, init)`. */
52
+ async function resolveRequest(input, init) {
53
+ if (typeof input === "string" || input instanceof URL) {
54
+ return {
55
+ url: input.toString(),
56
+ method: init?.method ?? "GET",
57
+ headers: init?.headers,
58
+ body: init?.body ?? undefined,
59
+ signal: init?.signal ?? undefined,
60
+ };
61
+ }
62
+ return {
63
+ url: input.url,
64
+ method: init?.method ?? input.method,
65
+ headers: init?.headers ?? input.headers,
66
+ body: init?.body !== undefined
67
+ ? init.body
68
+ : input.body !== null
69
+ ? await input.clone().arrayBuffer()
70
+ : undefined,
71
+ signal: init?.signal ?? input.signal,
72
+ };
73
+ }
74
+ function headersToObject(headers) {
75
+ const result = {};
76
+ if (!headers) {
77
+ return result;
78
+ }
79
+ // `.forEach()` (rather than `.entries()`/`for...of`) is the one
80
+ // iteration style declared on `Headers` consistently across both the
81
+ // DOM lib type and the Node/undici one that `axios`'s own type
82
+ // dependencies can pull into scope — the other two aren't.
83
+ new Headers(headers).forEach((value, key) => {
84
+ result[key] = value;
85
+ });
86
+ return result;
87
+ }
88
+ function axiosHeadersToFetchHeaders(headers) {
89
+ const result = new Headers();
90
+ if (typeof headers !== "object" || headers === null) {
91
+ return result;
92
+ }
93
+ for (const [key, value] of Object.entries(headers)) {
94
+ if (value === undefined) {
95
+ continue;
96
+ }
97
+ result.set(key, Array.isArray(value) ? value.join(", ") : String(value));
98
+ }
99
+ return result;
100
+ }
101
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAmE,MAAM,OAAO,CAAC;AAExF,yEAAyE;AACzE,oFAAoF;AACpF,yEAAyE;AACzE,qEAAqE;AACrE,8CAA8C;AAC9C,MAAM,kBAAkB,GAAG,IAAI,GAAG,CAAC,CAA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C1C,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;IACtB,CAAC,CAAC,CAAC;IACH,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,SAAS,0BAA0B,CAAC,OAAgB;IAClD,MAAM,MAAM,GAAG,IAAI,OAAO,EAAE,CAAC;IAC7B,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,KAAK,IAAI,EAAE,CAAC;QACpD,OAAO,MAAM,CAAC;IAChB,CAAC;IACD,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAkC,CAAC,EAAE,CAAC;QAC9E,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YACxB,SAAS;QACX,CAAC;QACD,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IAC3E,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC"}
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@cratestack/runtime-axios",
3
+ "version": "0.5.2",
4
+ "description": "A typeof-fetch-compatible transport for CrateStack's generated TypeScript RPC client, backed by axios.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/cratestack/cratestack.git",
9
+ "directory": "packages/cratestack-runtime-axios"
10
+ },
11
+ "homepage": "https://cratestack.dev",
12
+ "bugs": "https://github.com/cratestack/cratestack/issues",
13
+ "keywords": ["cratestack", "cstack", "rpc", "axios", "runtime"],
14
+ "type": "module",
15
+ "sideEffects": false,
16
+ "main": "./dist/index.js",
17
+ "types": "./dist/index.d.ts",
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "import": "./dist/index.js"
22
+ }
23
+ },
24
+ "files": ["dist", "README.md", "LICENSE"],
25
+ "scripts": {
26
+ "build": "tsc -p tsconfig.json",
27
+ "test": "vitest run",
28
+ "lint": "biome check ."
29
+ },
30
+ "peerDependencies": {
31
+ "axios": "^1.7.0"
32
+ },
33
+ "peerDependenciesMeta": {
34
+ "axios": { "optional": true }
35
+ },
36
+ "devDependencies": {
37
+ "axios": "^1.7.0",
38
+ "typescript": "^5.7.0",
39
+ "vitest": "^3.0.0"
40
+ },
41
+ "engines": {
42
+ "node": ">=18"
43
+ }
44
+ }