@withmarfa/client 0.0.1 → 0.0.3
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/README.md +4 -2
- package/dist/index.d.ts +4288 -487
- package/dist/index.js +64 -4
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,8 +1,17 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import createFetchClient
|
|
2
|
+
import createFetchClient, {
|
|
3
|
+
defaultBodySerializer,
|
|
4
|
+
mergeHeaders
|
|
5
|
+
} from "openapi-fetch";
|
|
6
|
+
|
|
7
|
+
// src/generated/byte-bodies.ts
|
|
8
|
+
var BYTE_BODIES = {
|
|
9
|
+
"POST /blobs": "application/octet-stream",
|
|
10
|
+
"POST /admin/restore-archive": "application/gzip"
|
|
11
|
+
};
|
|
3
12
|
|
|
4
13
|
// src/generated/contract.ts
|
|
5
|
-
var CONTRACT_VERSION =
|
|
14
|
+
var CONTRACT_VERSION = 0;
|
|
6
15
|
|
|
7
16
|
// src/index.ts
|
|
8
17
|
var CONTRACT_HEADER = "X-Marfa-Contract";
|
|
@@ -20,6 +29,17 @@ var ContractMismatchError = class extends Error {
|
|
|
20
29
|
status;
|
|
21
30
|
expected;
|
|
22
31
|
};
|
|
32
|
+
var BEARER = /^[A-Za-z0-9\-._~+/]+=*$/;
|
|
33
|
+
function checkCredential(credential) {
|
|
34
|
+
if (BEARER.test(credential)) return;
|
|
35
|
+
if (credential === "") {
|
|
36
|
+
throw new TypeError("credential is empty");
|
|
37
|
+
}
|
|
38
|
+
const at = credential.search(/[^A-Za-z0-9\-._~+/=]/);
|
|
39
|
+
throw new TypeError(
|
|
40
|
+
at === -1 ? "credential is not a bearer token: '=' may only end one, after at least one other character" : `credential is not a bearer token: the character at index ${String(at)} is not one a bearer token may hold`
|
|
41
|
+
);
|
|
42
|
+
}
|
|
23
43
|
function createClient(options) {
|
|
24
44
|
const fetcher = options.fetch ?? globalThis.fetch;
|
|
25
45
|
if (/[?#]/.test(options.baseUrl)) {
|
|
@@ -32,6 +52,8 @@ function createClient(options) {
|
|
|
32
52
|
throw new TypeError(`baseUrl must be http or https, not ${base.protocol}`);
|
|
33
53
|
}
|
|
34
54
|
const baseUrl = base.href.replace(/\/+$/, "");
|
|
55
|
+
const credential = options.credential;
|
|
56
|
+
checkCredential(credential);
|
|
35
57
|
const root = new URL(`${baseUrl}/`).href;
|
|
36
58
|
const expected = String(CONTRACT_VERSION);
|
|
37
59
|
const hold = async (response) => {
|
|
@@ -48,7 +70,7 @@ function createClient(options) {
|
|
|
48
70
|
);
|
|
49
71
|
}
|
|
50
72
|
const headers = new Headers(request.headers);
|
|
51
|
-
headers.set("Authorization", `Bearer ${
|
|
73
|
+
headers.set("Authorization", `Bearer ${credential}`);
|
|
52
74
|
return hold(
|
|
53
75
|
await fetcher(new Request(request, { headers, redirect: "error" }))
|
|
54
76
|
);
|
|
@@ -70,10 +92,48 @@ function createClient(options) {
|
|
|
70
92
|
return hold(response);
|
|
71
93
|
}
|
|
72
94
|
};
|
|
73
|
-
const
|
|
95
|
+
const serialize = defaultBodySerializer;
|
|
96
|
+
const client = createFetchClient({
|
|
97
|
+
baseUrl,
|
|
98
|
+
fetch: send,
|
|
99
|
+
bodySerializer: (body, headers) => isBytes(body) ? body : serialize(body, headers)
|
|
100
|
+
});
|
|
74
101
|
client.use(gate);
|
|
102
|
+
labelBytes(client);
|
|
75
103
|
return client;
|
|
76
104
|
}
|
|
105
|
+
var isBytes = (body) => body instanceof Blob || body instanceof ArrayBuffer || ArrayBuffer.isView(body) || body instanceof ReadableStream;
|
|
106
|
+
function labelBytes(client) {
|
|
107
|
+
const label = (method, path, init) => {
|
|
108
|
+
const body = init?.body;
|
|
109
|
+
if (!init || !isBytes(body)) return init;
|
|
110
|
+
const own = body instanceof Blob ? body.type : "";
|
|
111
|
+
const type = own !== "" ? own : BYTE_BODIES[`${method} ${path}`] ?? "application/octet-stream";
|
|
112
|
+
return {
|
|
113
|
+
...init,
|
|
114
|
+
headers: mergeHeaders({ "Content-Type": type }, init.headers),
|
|
115
|
+
// fetch refuses a streamed body unless told it is sent in one
|
|
116
|
+
// direction, before any answer is read.
|
|
117
|
+
...body instanceof ReadableStream ? { duplex: "half" } : {}
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
const request = client.request;
|
|
121
|
+
const labeled = (method, path, init) => request(method, path, label(method.toUpperCase(), path, init));
|
|
122
|
+
const calls = client;
|
|
123
|
+
calls.request = labeled;
|
|
124
|
+
for (const method of [
|
|
125
|
+
"GET",
|
|
126
|
+
"PUT",
|
|
127
|
+
"POST",
|
|
128
|
+
"DELETE",
|
|
129
|
+
"OPTIONS",
|
|
130
|
+
"HEAD",
|
|
131
|
+
"PATCH",
|
|
132
|
+
"TRACE"
|
|
133
|
+
]) {
|
|
134
|
+
calls[method] = (path, init) => labeled(method, path, init);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
77
137
|
async function* pages(fetchPage) {
|
|
78
138
|
let cursor;
|
|
79
139
|
const followed = /* @__PURE__ */ new Set();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@withmarfa/client",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -28,12 +28,12 @@
|
|
|
28
28
|
"@types/node": "^26.2.0",
|
|
29
29
|
"openapi-typescript": "7.13.0",
|
|
30
30
|
"tsup": "^8.5.1",
|
|
31
|
-
"tsx": "^4.23.
|
|
31
|
+
"tsx": "^4.23.15",
|
|
32
32
|
"typescript": "^6.0.3"
|
|
33
33
|
},
|
|
34
34
|
"scripts": {
|
|
35
35
|
"build": "tsup",
|
|
36
|
-
"generate": "
|
|
36
|
+
"generate": "tsx scripts/generate-schema.ts && tsx scripts/generate-contract.ts",
|
|
37
37
|
"typecheck": "tsc --noEmit",
|
|
38
38
|
"test": "vitest run"
|
|
39
39
|
}
|