@fluixi/api 1.0.0-alpha.108 → 1.0.0-alpha.110
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/chunk-AZSII76K.mjs +106 -0
- package/dist/chunk-CGGDPGK5.mjs +4888 -0
- package/dist/chunk-FIHRBQUV.mjs +43 -0
- package/dist/chunk-GKZ2OUXD.mjs +97 -0
- package/dist/chunk-GTIH4MXK.mjs +37 -0
- package/dist/chunk-H7XKDP6F.mjs +255 -0
- package/dist/chunk-MI6ZXL3J.mjs +21 -0
- package/dist/chunk-QZA7GDGB.mjs +62 -0
- package/dist/client.mjs +2 -0
- package/dist/errors.mjs +11 -32
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +44 -5411
- package/dist/input.mjs +7 -63
- package/dist/middleware.mjs +5 -16
- package/dist/openapi.mjs +11 -252
- package/dist/rate-limit.mjs +9 -4929
- package/dist/route.mjs +10 -162
- package/dist/security.mjs +11 -4990
- package/package.json +3 -3
package/dist/route.mjs
CHANGED
|
@@ -1,165 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
var unauthorized = (message = "Sign in to continue.") => new ApiError(401, "unauthorized", message);
|
|
12
|
-
var forbidden = (message = "Not allowed.") => new ApiError(403, "forbidden", message);
|
|
13
|
-
function toApiError(error) {
|
|
14
|
-
if (error instanceof ApiError) return { error, internal: false };
|
|
15
|
-
const { status, code, message } = error ?? {};
|
|
16
|
-
if (status === 401 || status === 403) {
|
|
17
|
-
if (typeof code === "string" && typeof message === "string") return { error: new ApiError(status, code, message), internal: false };
|
|
18
|
-
return { error: status === 401 ? unauthorized() : forbidden(), internal: false };
|
|
19
|
-
}
|
|
20
|
-
return { error: new ApiError(500, "internal", "Something went wrong on our side."), internal: true };
|
|
21
|
-
}
|
|
22
|
-
function errorResponse(error) {
|
|
23
|
-
const body = { error: { code: error.code, message: error.message } };
|
|
24
|
-
if (error.details !== void 0) body.error.details = error.details;
|
|
25
|
-
return new Response(JSON.stringify(body), {
|
|
26
|
-
status: error.status,
|
|
27
|
-
headers: { "content-type": "application/json; charset=utf-8" }
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// src/input.ts
|
|
32
|
-
var DEFAULT_BODY_LIMIT = 1024 * 1024;
|
|
33
|
-
function readQuery(url) {
|
|
34
|
-
const out = {};
|
|
35
|
-
for (const [key, value] of url.searchParams) {
|
|
36
|
-
const seen = out[key];
|
|
37
|
-
if (seen === void 0) out[key] = value;
|
|
38
|
-
else out[key] = Array.isArray(seen) ? [...seen, value] : [seen, value];
|
|
39
|
-
}
|
|
40
|
-
return out;
|
|
41
|
-
}
|
|
42
|
-
async function readJson(request, limit = DEFAULT_BODY_LIMIT) {
|
|
43
|
-
const declared = Number(request.headers.get("content-length"));
|
|
44
|
-
if (Number.isFinite(declared) && declared > limit) throw tooLarge(limit);
|
|
45
|
-
if (!request.body) return void 0;
|
|
46
|
-
const type = request.headers.get("content-type") ?? "";
|
|
47
|
-
const reader = request.body.getReader();
|
|
48
|
-
const chunks = [];
|
|
49
|
-
let size = 0;
|
|
50
|
-
for (; ; ) {
|
|
51
|
-
const { done, value } = await reader.read();
|
|
52
|
-
if (done) break;
|
|
53
|
-
size += value.byteLength;
|
|
54
|
-
if (size > limit) {
|
|
55
|
-
await reader.cancel();
|
|
56
|
-
throw tooLarge(limit);
|
|
57
|
-
}
|
|
58
|
-
chunks.push(value);
|
|
59
|
-
}
|
|
60
|
-
if (size === 0) return void 0;
|
|
61
|
-
if (!isJson(type)) {
|
|
62
|
-
throw new ApiError(415, "unsupported_media_type", "Send the body as application/json.");
|
|
63
|
-
}
|
|
64
|
-
const bytes = new Uint8Array(size);
|
|
65
|
-
let offset = 0;
|
|
66
|
-
for (const chunk of chunks) {
|
|
67
|
-
bytes.set(chunk, offset);
|
|
68
|
-
offset += chunk.byteLength;
|
|
69
|
-
}
|
|
70
|
-
try {
|
|
71
|
-
return JSON.parse(new TextDecoder().decode(bytes));
|
|
72
|
-
} catch {
|
|
73
|
-
throw new ApiError(400, "invalid_json", "The body is not valid JSON.");
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
function isJson(type) {
|
|
77
|
-
const mime = type.split(";")[0].trim().toLowerCase();
|
|
78
|
-
return mime === "application/json" || mime.endsWith("+json");
|
|
79
|
-
}
|
|
80
|
-
function tooLarge(limit) {
|
|
81
|
-
return new ApiError(413, "payload_too_large", `The body is larger than ${limit} bytes.`);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// src/route.ts
|
|
85
|
-
var CONTRACT = /* @__PURE__ */ Symbol.for("fluixi.api.contract");
|
|
86
|
-
var report = (error, request) => {
|
|
87
|
-
console.error(`[fluixi/api] ${request.method} ${new URL(request.url).pathname}`, error);
|
|
88
|
-
};
|
|
89
|
-
function setErrorReporter(reporter) {
|
|
90
|
-
report = reporter;
|
|
91
|
-
}
|
|
92
|
-
function route(contract) {
|
|
93
|
-
const handle = async (request, context = {}) => {
|
|
94
|
-
try {
|
|
95
|
-
const auth = contract.guard ? await contract.guard.check(request) : void 0;
|
|
96
|
-
const issues = [];
|
|
97
|
-
const params = await check(contract.params, context.params ?? {}, "params", issues);
|
|
98
|
-
const query = await check(contract.query, readQuery(new URL(request.url)), "query", issues);
|
|
99
|
-
const body = contract.body ? await check(contract.body, await readJson(request, contract.bodyLimit), "body", issues) : void 0;
|
|
100
|
-
if (issues.length) throw new ApiError(400, "validation_failed", "The request is not valid.", issues);
|
|
101
|
-
const input = { params, query, body, auth, request };
|
|
102
|
-
const out = await contract.handler(input);
|
|
103
|
-
const stale = !contract.revalidates ? [] : typeof contract.revalidates === "function" ? contract.revalidates(out, input) : contract.revalidates;
|
|
104
|
-
if (out instanceof Response) return withRevalidate(out, stale);
|
|
105
|
-
if (out === void 0 && !contract.response) return withRevalidate(new Response(null, { status: contract.status ?? 204 }), stale);
|
|
106
|
-
let value = out;
|
|
107
|
-
if (contract.response && contract.validateResponse !== false) {
|
|
108
|
-
const result = await contract.response["~standard"].validate(out);
|
|
109
|
-
if (result.issues) {
|
|
110
|
-
throw Object.assign(new Error("Response does not match the route contract."), {
|
|
111
|
-
issues: result.issues.map((issue) => toIssue(issue, "response"))
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
value = result.value;
|
|
115
|
-
}
|
|
116
|
-
return withRevalidate(
|
|
117
|
-
new Response(JSON.stringify(value ?? null), {
|
|
118
|
-
status: contract.status ?? 200,
|
|
119
|
-
headers: { "content-type": "application/json; charset=utf-8" }
|
|
120
|
-
}),
|
|
121
|
-
stale
|
|
122
|
-
);
|
|
123
|
-
} catch (thrown) {
|
|
124
|
-
const { error, internal } = toApiError(thrown);
|
|
125
|
-
if (internal) report(thrown, request);
|
|
126
|
-
return errorResponse(error);
|
|
127
|
-
}
|
|
128
|
-
};
|
|
129
|
-
Object.defineProperty(handle, CONTRACT, { value: contract });
|
|
130
|
-
return handle;
|
|
131
|
-
}
|
|
132
|
-
var REVALIDATE_HEADER = "x-fx-revalidate";
|
|
133
|
-
function withRevalidate(response, names) {
|
|
134
|
-
if (!names.length || !response.ok) return response;
|
|
135
|
-
const value = names.join(", ");
|
|
136
|
-
try {
|
|
137
|
-
response.headers.append(REVALIDATE_HEADER, value);
|
|
138
|
-
return response;
|
|
139
|
-
} catch {
|
|
140
|
-
const headers = new Headers(response.headers);
|
|
141
|
-
headers.append(REVALIDATE_HEADER, value);
|
|
142
|
-
return new Response(response.body, { status: response.status, statusText: response.statusText, headers });
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
function contractOf(handler) {
|
|
146
|
-
return typeof handler === "function" ? handler[CONTRACT] : void 0;
|
|
147
|
-
}
|
|
148
|
-
async function check(schema, value, where, issues) {
|
|
149
|
-
if (!schema) return value;
|
|
150
|
-
const result = await schema["~standard"].validate(value);
|
|
151
|
-
if (result.issues) {
|
|
152
|
-
for (const issue of result.issues) issues.push(toIssue(issue, where));
|
|
153
|
-
return void 0;
|
|
154
|
-
}
|
|
155
|
-
return result.value;
|
|
156
|
-
}
|
|
157
|
-
function toIssue(issue, where) {
|
|
158
|
-
const path = (issue.path ?? []).map(
|
|
159
|
-
(segment) => String(typeof segment === "object" && segment !== null ? segment.key : segment)
|
|
160
|
-
);
|
|
161
|
-
return { path: [where, ...path].join("."), message: issue.message };
|
|
162
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
CONTRACT,
|
|
3
|
+
REVALIDATE_HEADER,
|
|
4
|
+
contractOf,
|
|
5
|
+
route,
|
|
6
|
+
setErrorReporter
|
|
7
|
+
} from "./chunk-GKZ2OUXD.mjs";
|
|
8
|
+
import "./chunk-QZA7GDGB.mjs";
|
|
9
|
+
import "./chunk-FIHRBQUV.mjs";
|
|
10
|
+
import "./chunk-GTIH4MXK.mjs";
|
|
163
11
|
export {
|
|
164
12
|
CONTRACT,
|
|
165
13
|
REVALIDATE_HEADER,
|