@withmarfa/client 0.0.1

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.js ADDED
@@ -0,0 +1,104 @@
1
+ // src/index.ts
2
+ import createFetchClient from "openapi-fetch";
3
+
4
+ // src/generated/contract.ts
5
+ var CONTRACT_VERSION = 3;
6
+
7
+ // src/index.ts
8
+ var CONTRACT_HEADER = "X-Marfa-Contract";
9
+ var ContractMismatchError = class extends Error {
10
+ constructor(served, status, expected = CONTRACT_VERSION) {
11
+ super(
12
+ served === null ? `The server answered ${String(status)} with no contract version, so its body cannot be read by a client generated for contract ${String(expected)}.` : `The server answered with contract ${JSON.stringify(served)}; this client was generated for contract ${String(expected)}. Use a client generated for the server's contract.`
13
+ );
14
+ this.served = served;
15
+ this.status = status;
16
+ this.expected = expected;
17
+ this.name = "ContractMismatchError";
18
+ }
19
+ served;
20
+ status;
21
+ expected;
22
+ };
23
+ function createClient(options) {
24
+ const fetcher = options.fetch ?? globalThis.fetch;
25
+ if (/[?#]/.test(options.baseUrl)) {
26
+ throw new TypeError(
27
+ "baseUrl names where the instance is served, with no query or fragment"
28
+ );
29
+ }
30
+ const base = new URL(options.baseUrl);
31
+ if (base.protocol !== "http:" && base.protocol !== "https:") {
32
+ throw new TypeError(`baseUrl must be http or https, not ${base.protocol}`);
33
+ }
34
+ const baseUrl = base.href.replace(/\/+$/, "");
35
+ const root = new URL(`${baseUrl}/`).href;
36
+ const expected = String(CONTRACT_VERSION);
37
+ const hold = async (response) => {
38
+ const served = response.headers.get(CONTRACT_HEADER);
39
+ if (served === expected) return response;
40
+ if (served === null && !response.ok) return response;
41
+ await response.body?.cancel();
42
+ throw new ContractMismatchError(served, response.status);
43
+ };
44
+ const send = async (request) => {
45
+ if (!request.url.startsWith(root)) {
46
+ throw new Error(
47
+ `This client is for ${baseUrl}; it refuses to send ${request.url}.`
48
+ );
49
+ }
50
+ const headers = new Headers(request.headers);
51
+ headers.set("Authorization", `Bearer ${options.credential}`);
52
+ return hold(
53
+ await fetcher(new Request(request, { headers, redirect: "error" }))
54
+ );
55
+ };
56
+ const gate = {
57
+ onRequest({ params }) {
58
+ for (const value of Object.values(params.path ?? {})) {
59
+ if (value === "." || value === "..") {
60
+ throw new Error(
61
+ `A path parameter of ${JSON.stringify(value)} names no resource.`
62
+ );
63
+ }
64
+ }
65
+ return void 0;
66
+ },
67
+ // A caller can hand one request a `fetch` of its own, which does not
68
+ // pass through `send`; its answer is still held here.
69
+ onResponse({ response }) {
70
+ return hold(response);
71
+ }
72
+ };
73
+ const client = createFetchClient({ baseUrl, fetch: send });
74
+ client.use(gate);
75
+ return client;
76
+ }
77
+ async function* pages(fetchPage) {
78
+ let cursor;
79
+ const followed = /* @__PURE__ */ new Set();
80
+ for (; ; ) {
81
+ const page = await fetchPage(cursor);
82
+ if (page.next_cursor !== null && typeof page.next_cursor !== "string") {
83
+ throw new Error(
84
+ "A page answered no next_cursor, so the walk cannot continue."
85
+ );
86
+ }
87
+ yield* page.data;
88
+ if (page.next_cursor === null) return;
89
+ if (followed.has(page.next_cursor)) {
90
+ throw new Error(
91
+ "The server answered a cursor this walk already followed, so it would not end."
92
+ );
93
+ }
94
+ followed.add(page.next_cursor);
95
+ cursor = page.next_cursor;
96
+ }
97
+ }
98
+ export {
99
+ CONTRACT_HEADER,
100
+ CONTRACT_VERSION,
101
+ ContractMismatchError,
102
+ createClient,
103
+ pages
104
+ };
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@withmarfa/client",
3
+ "version": "0.0.1",
4
+ "license": "Apache-2.0",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/withmarfa/marfa.git",
8
+ "directory": "packages/client"
9
+ },
10
+ "type": "module",
11
+ "publishConfig": {
12
+ "registry": "https://registry.npmjs.org",
13
+ "access": "public"
14
+ },
15
+ "exports": {
16
+ ".": {
17
+ "types": "./dist/index.d.ts",
18
+ "import": "./dist/index.js"
19
+ }
20
+ },
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "dependencies": {
25
+ "openapi-fetch": "0.17.0"
26
+ },
27
+ "devDependencies": {
28
+ "@types/node": "^26.2.0",
29
+ "openapi-typescript": "7.13.0",
30
+ "tsup": "^8.5.1",
31
+ "tsx": "^4.23.12",
32
+ "typescript": "^6.0.3"
33
+ },
34
+ "scripts": {
35
+ "build": "tsup",
36
+ "generate": "openapi-typescript ../../openapi.json -o src/generated/schema.ts && tsx scripts/generate-contract.ts",
37
+ "typecheck": "tsc --noEmit",
38
+ "test": "vitest run"
39
+ }
40
+ }