@arbol-ai/maximus 7.3.1 → 8.0.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/README.md +36 -38
- package/bin/maximus.js +4 -0
- package/config/oxfmt.mjs +1 -0
- package/config/oxlint.mjs +10 -0
- package/dist/analytics/analytics.d.ts +21 -9
- package/dist/analytics/analytics.js +19 -5
- package/dist/body.d.ts +1 -1
- package/dist/body.js +2 -2
- package/dist/chunks/agent-skills-Ef65jTf_.js +49 -0
- package/dist/chunks/agent-skills-m7y1EUch.js +3 -0
- package/dist/chunks/{body-6LichrQm.js → body-DOIHwIIl.js} +14 -7
- package/dist/chunks/{correlation-BEkcFFGT.js → correlation-4b23a-a4.js} +8 -5
- package/dist/chunks/define-app-CfVZBt7D.js +336 -0
- package/dist/chunks/errors-WfyKhiyk.js +131 -0
- package/dist/chunks/execution-CfZEx_JP.js +84 -0
- package/dist/chunks/generate-C-9FKW0o.js +197 -0
- package/dist/chunks/handler-DWTGoduQ.js +140 -0
- package/dist/chunks/http-layout-_h7xwgPC.js +128 -0
- package/dist/chunks/http-paths-DMBxwpp6.js +218 -0
- package/dist/chunks/{json-Bu4BLT25.js → json-B14evE6-.js} +21 -5
- package/dist/chunks/names-ncD7A3DV.js +280 -0
- package/dist/chunks/openapi-CV0KmJpS.js +1588 -0
- package/dist/chunks/openapi-file-OjSs9mRv.js +83 -0
- package/dist/chunks/pages-DyC0m8Zb.js +14 -0
- package/dist/chunks/prepare-configured-BmRJLg6J.js +3 -0
- package/dist/chunks/prepare-configured-Cithjf80.js +52 -0
- package/dist/chunks/project-files-C0OX7MQQ.js +130 -0
- package/dist/chunks/query-kind-CWKAXEME.js +40 -0
- package/dist/chunks/record-DQUDTpEA.js +10 -0
- package/dist/chunks/result-CKgZHrnj.js +56 -0
- package/dist/chunks/{runtime-BPnaa-NK.js → runtime-CVVf5hmp.js} +50 -25
- package/dist/chunks/scaffold-DGrcjR3h.js +3 -0
- package/dist/chunks/scaffold-DZD6dfoZ.js +138 -0
- package/dist/chunks/{schemas-CzTvbXb6.js → schemas-p_aRcMC1.js} +30 -14
- package/dist/chunks/server-CCc7fil4.js +410 -0
- package/dist/chunks/src--HZrH1U9.js +568 -0
- package/dist/chunks/standard-failures-CLjxS2ea.js +74 -0
- package/dist/chunks/{telemetry-BTwB68m5.js → telemetry-C3LfZcOB.js} +32 -11
- package/dist/chunks/{types-BWxDd3tY.d.ts → types-BQYB4Ads.d.ts} +998 -337
- package/dist/chunks/{validation-r-sgmsqb.js → validation-fTohBT6R.js} +69 -28
- package/dist/chunks/workers-types-iONp32T4.d.ts +143 -0
- package/dist/chunks/yaml-Dk2L24yQ.js +74 -0
- package/dist/diagnostics.d.ts +6 -4
- package/dist/diagnostics.js +2 -2
- package/dist/errors.d.ts +2 -2
- package/dist/errors.js +2 -125
- package/dist/http-paths.d.ts +1 -1
- package/dist/http-paths.js +1 -197
- package/dist/index.d.ts +2 -2
- package/dist/index.js +12 -10
- package/dist/internals.d.ts +1 -1
- package/dist/internals.js +5 -3
- package/dist/mcp/index.d.ts +3 -2
- package/dist/mcp/index.js +5 -387
- package/dist/postgres/index.d.ts +25 -8
- package/dist/postgres/index.js +17 -8
- package/dist/project/config.d.ts +139 -0
- package/dist/project/config.js +196 -0
- package/dist/project/index.d.ts +17 -4
- package/dist/project/index.js +44 -9
- package/dist/projection.d.ts +14 -13
- package/dist/projection.js +8 -32
- package/dist/schemas.d.ts +1 -1
- package/dist/schemas.js +2 -2
- package/dist/standard-failures.d.ts +2 -89
- package/dist/standard-failures.js +1 -72
- package/dist/tooling/cli.js +151 -107
- package/dist/tooling/testing.js +2 -2
- package/dist/vite/cloudflare-workers.js +25 -0
- package/dist/vite/index.d.ts +157 -0
- package/dist/vite/index.js +1477 -0
- package/dist/webhooks.d.ts +12 -10
- package/dist/webhooks.js +26 -12
- package/dist/workers/runtime.d.ts +1 -1
- package/dist/workers/runtime.js +1 -1
- package/package.json +30 -22
- package/skills/maximus/SKILL.md +165 -69
- package/skills/maximus/references/deploying.md +73 -0
- package/skills/maximus/references/errors-and-results.md +114 -55
- package/skills/maximus/references/project.md +202 -0
- package/skills/maximus/references/routes-and-tools.md +256 -0
- package/skills/maximus/references/testing.md +137 -50
- package/skills/maximus/references/transports.md +113 -100
- package/skills/maximus/references/writing-code.md +84 -0
- package/templates/mcp/mcp/instructions.md.template +2 -0
- package/templates/mcp/routes/v1/greeting.get.ts.template +12 -0
- package/templates/mcp/tests/mcp.test.ts.template +36 -0
- package/templates/project/.agents/skills/project/SKILL.md.template +21 -12
- package/templates/project/.github/workflows/ci.yml.template +4 -1
- package/templates/project/CLAUDE.md.template +41 -22
- package/templates/project/README.md.template +54 -47
- package/templates/project/middleware/10-response.ts.template +16 -0
- package/templates/project/package.json.template +16 -8
- package/templates/project/pages/index.ts.template +2 -4
- package/templates/project/routes/v1/greeting.get.ts.template +10 -0
- package/templates/project/routes/v1/greeting.post.ts.template +12 -0
- package/templates/project/tests/greeting.test.ts.template +45 -0
- package/templates/project/tsconfig.json.template +8 -5
- package/templates/project/vite.config.ts.template +15 -7
- package/templates/web/app/main.tsx.template +16 -0
- package/templates/web/app/root.tsx.template +17 -0
- package/templates/web/app/routes/index.tsx.template +9 -0
- package/templates/web/index.html.template +12 -0
- package/templates/web/package.json.template +12 -0
- package/templates/web/tsconfig.json.template +7 -0
- package/templates/web/vite.config.ts.template +22 -0
- package/dist/chunks/execution-B-yy4C7N.js +0 -691
- package/dist/chunks/prepare-project-zi106pWF.js +0 -146
- package/dist/chunks/record-DY-lX9w1.js +0 -34
- package/dist/chunks/src-DPSpGNnT.js +0 -1164
- package/dist/client/client.d.ts +0 -129
- package/dist/client/client.js +0 -321
- package/dist/tooling/vite.d.ts +0 -3
- package/dist/tooling/vite.js +0 -44
- package/skills/maximus/references/adding-an-operation.md +0 -179
- package/skills/maximus/references/building-a-service.md +0 -167
- package/skills/maximus/references/migration-from-v2.md +0 -62
- package/skills/maximus/references/migration-to-v5.md +0 -37
- package/skills/maximus/references/publishing-and-verification.md +0 -39
- package/templates/project/cloudflare.config.ts.template +0 -24
- package/templates/project/routes/v1/greeting.ts.template +0 -22
- package/templates/project/src/app.ts.template +0 -2
- package/templates/project/src/application.ts.template +0 -26
- package/templates/project/src/index.ts.template +0 -3
- package/templates/project/src/worker.ts.template +0 -6
- package/templates/service/.agents/skills/project/SKILL.md.template +0 -10
- package/templates/service/.github/workflows/ci.yml.template +0 -21
- package/templates/service/.gitignore.template +0 -9
- package/templates/service/CLAUDE.md.template +0 -5
- package/templates/service/README.md.template +0 -45
- package/templates/service/cloudflare.config.ts.template +0 -30
- package/templates/service/package.json.template +0 -36
- package/templates/service/src/app.ts.template +0 -30
- package/templates/service/src/index.ts.template +0 -13
- package/templates/service/tests/contract.test.ts.template +0 -12
- package/templates/service/tsconfig.json.template +0 -16
- package/templates/service/vite.config.ts.template +0 -13
- package/templates/service/vitest.config.ts.template +0 -10
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
// src/http-paths.ts
|
|
2
|
+
/**
|
|
3
|
+
* `@arbol-ai/maximus/http-paths`: versioned route templates, path parameter decoding, and the router that
|
|
4
|
+
* matches a request to exactly one operation.
|
|
5
|
+
*/
|
|
6
|
+
/** A fixed path segment of a route template. */
|
|
7
|
+
const STATIC_SEGMENT = /^[a-zA-Z0-9_-]+$/;
|
|
8
|
+
/** A parameter segment of a route template, `{name}`, occupying the whole segment. */
|
|
9
|
+
const PARAMETER_SEGMENT = /^\{([a-zA-Z][a-zA-Z0-9_]*)\}$/;
|
|
10
|
+
/** Parameter names that would reach an object's prototype. */
|
|
11
|
+
const UNSAFE_PARAMETERS = /* @__PURE__ */ new Set([
|
|
12
|
+
"constructor",
|
|
13
|
+
"prototype",
|
|
14
|
+
"__proto__"
|
|
15
|
+
]);
|
|
16
|
+
/** Headers that carry authority or framing: never an operation's input metadata. */
|
|
17
|
+
const RESERVED_HEADERS = /* @__PURE__ */ new Set([
|
|
18
|
+
"authorization",
|
|
19
|
+
"cookie",
|
|
20
|
+
"host",
|
|
21
|
+
"content-length",
|
|
22
|
+
"content-encoding"
|
|
23
|
+
]);
|
|
24
|
+
/** Whether a byte operation may read one of its input fields from this header. */
|
|
25
|
+
function isInputHeaderName(header) {
|
|
26
|
+
return /^[A-Za-z0-9-]+$/u.test(header) && !RESERVED_HEADERS.has(header.toLowerCase());
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The parameter names of a declared route template, in order. A template is versioned (`/v1/…`) with at least
|
|
30
|
+
* one segment after the version, each fixed or a complete `{parameter}` with a unique, safe name; anything
|
|
31
|
+
* else throws a TypeError. It reads declared templates, never a request's path.
|
|
32
|
+
*/
|
|
33
|
+
function operationPathParameters(path) {
|
|
34
|
+
const segments = path.split("/");
|
|
35
|
+
if (segments[0] !== "" || !/^v[1-9][0-9]*$/.test(segments[1] ?? "") || segments.length < 3) throw new TypeError(`Operations require a versioned path, such as /v1/tasks: ${path}`);
|
|
36
|
+
const parameters = [];
|
|
37
|
+
for (const segment of segments.slice(2)) {
|
|
38
|
+
const parameter = PARAMETER_SEGMENT.exec(segment)?.[1];
|
|
39
|
+
if (parameter) {
|
|
40
|
+
if (UNSAFE_PARAMETERS.has(parameter) || parameters.includes(parameter)) throw new TypeError(`Path parameter names must be unique and safe: ${path}`);
|
|
41
|
+
parameters.push(parameter);
|
|
42
|
+
} else if (!STATIC_SEGMENT.test(segment)) throw new TypeError(`Use fixed routes or complete {parameter} segments; empty and malformed segments are forbidden: ${path}`);
|
|
43
|
+
}
|
|
44
|
+
return parameters;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* One path segment, percent-decoded, or null when it does not decode or decodes to a value
|
|
48
|
+
* `isPathParameterValue` refuses, so decoding cannot change which route a path reaches.
|
|
49
|
+
*/
|
|
50
|
+
function decodePathParameter(segment) {
|
|
51
|
+
try {
|
|
52
|
+
const value = decodeURIComponent(segment);
|
|
53
|
+
return isPathParameterValue(value) ? value : null;
|
|
54
|
+
} catch {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Whether a value can be a path parameter: a nonempty string other than `.` and `..`, without a slash, a
|
|
60
|
+
* backslash, a control character or a lone surrogate, so it stays one segment when encoded.
|
|
61
|
+
*/
|
|
62
|
+
function isPathParameterValue(value) {
|
|
63
|
+
if (typeof value !== "string" || value.length === 0 || value === "." || value === ".." || /[\\/]/.test(value)) return false;
|
|
64
|
+
for (const character of value) {
|
|
65
|
+
const code = character.codePointAt(0) ?? 0;
|
|
66
|
+
if (code < 32 || code === 127 || code >= 55296 && code <= 57343) return false;
|
|
67
|
+
}
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
/** The methods an operation route may use. */
|
|
71
|
+
const HTTP_METHODS = /* @__PURE__ */ new Set([
|
|
72
|
+
"GET",
|
|
73
|
+
"POST",
|
|
74
|
+
"PUT",
|
|
75
|
+
"PATCH",
|
|
76
|
+
"DELETE"
|
|
77
|
+
]);
|
|
78
|
+
/** A route as a refusal names it: its method and path template, `GET /v1/notes/{noteId}`. */
|
|
79
|
+
function routeName(route) {
|
|
80
|
+
return `${route.definition.http?.method} ${route.definition.http?.path}`;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* The router for a set of definitions: `match(method, path)` finds the one route a request reaches, with its
|
|
84
|
+
* decoded parameters, and `ownsPath(path)` says whether any route answers a path. A definition without `http`
|
|
85
|
+
* is skipped. Construction throws a TypeError for an unsupported method, a malformed template, a duplicate
|
|
86
|
+
* route, or two templates that could match the same path. A fixed route is one map lookup; templates compile
|
|
87
|
+
* into states with one transition per segment, so matching never scans the routes.
|
|
88
|
+
*/
|
|
89
|
+
function createHttpRouteResolver(definitions) {
|
|
90
|
+
const exact = /* @__PURE__ */ new Map();
|
|
91
|
+
const roots = /* @__PURE__ */ new Map();
|
|
92
|
+
let nextId = 0;
|
|
93
|
+
/** A new tree node with the next id. */
|
|
94
|
+
const branch = () => {
|
|
95
|
+
const node = {
|
|
96
|
+
id: nextId,
|
|
97
|
+
fixed: /* @__PURE__ */ new Map()
|
|
98
|
+
};
|
|
99
|
+
nextId += 1;
|
|
100
|
+
return node;
|
|
101
|
+
};
|
|
102
|
+
for (const definition of definitions) {
|
|
103
|
+
if (!definition.http) continue;
|
|
104
|
+
if (!HTTP_METHODS.has(definition.http.method)) throw new TypeError(`Operation routes require a supported HTTP method: ${definition.http.method} ${definition.http.path}`);
|
|
105
|
+
const names = operationPathParameters(definition.http.path);
|
|
106
|
+
const segments = definition.http.path.slice(1).split("/");
|
|
107
|
+
const route = {
|
|
108
|
+
definition,
|
|
109
|
+
parameters: segments.flatMap((segment, position) => {
|
|
110
|
+
const parameter = segment.startsWith("{") ? segment.slice(1, -1) : void 0;
|
|
111
|
+
return parameter ? [{
|
|
112
|
+
name: parameter,
|
|
113
|
+
position
|
|
114
|
+
}] : [];
|
|
115
|
+
})
|
|
116
|
+
};
|
|
117
|
+
if (names.length === 0) {
|
|
118
|
+
const key = `${definition.http.method} ${definition.http.path}`;
|
|
119
|
+
if (exact.has(key)) throw new TypeError(`Duplicate operation route: ${key}`);
|
|
120
|
+
exact.set(key, route);
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
let node = roots.get(definition.http.method);
|
|
124
|
+
if (!node) {
|
|
125
|
+
node = branch();
|
|
126
|
+
roots.set(definition.http.method, node);
|
|
127
|
+
}
|
|
128
|
+
for (const segment of segments) if (segment.startsWith("{")) {
|
|
129
|
+
node.parameter ??= branch();
|
|
130
|
+
node = node.parameter;
|
|
131
|
+
} else {
|
|
132
|
+
let child = node.fixed.get(segment);
|
|
133
|
+
if (!child) {
|
|
134
|
+
child = branch();
|
|
135
|
+
node.fixed.set(segment, child);
|
|
136
|
+
}
|
|
137
|
+
node = child;
|
|
138
|
+
}
|
|
139
|
+
if (node.route) throw new TypeError(`Ambiguous operation path templates: ${routeName(node.route)} and ${routeName(route)}`);
|
|
140
|
+
node.route = route;
|
|
141
|
+
}
|
|
142
|
+
const states = /* @__PURE__ */ new Map();
|
|
143
|
+
/**
|
|
144
|
+
* The state for a set of tree nodes: their routes (at most one), and for each next segment the state of
|
|
145
|
+
* every node it can lead to, compiled once per set.
|
|
146
|
+
*/
|
|
147
|
+
function compile(nodes) {
|
|
148
|
+
const distinct = [...new Map(nodes.map((node) => [node.id, node])).values()];
|
|
149
|
+
const key = distinct.map((node) => node.id).toSorted((left, right) => left - right).join(",");
|
|
150
|
+
const known = states.get(key);
|
|
151
|
+
if (known) return known;
|
|
152
|
+
const routes = distinct.flatMap((node) => node.route ? [node.route] : []);
|
|
153
|
+
if (routes.length > 1) throw new TypeError(`Ambiguous overlapping operation path templates: ${routes.map(routeName).join(" and ")}`);
|
|
154
|
+
const state = {
|
|
155
|
+
fixed: /* @__PURE__ */ new Map(),
|
|
156
|
+
route: routes[0]
|
|
157
|
+
};
|
|
158
|
+
states.set(key, state);
|
|
159
|
+
const parameters = distinct.flatMap((node) => node.parameter ? [node.parameter] : []);
|
|
160
|
+
if (parameters.length > 0) state.parameter = compile(parameters);
|
|
161
|
+
const fixed = new Set(distinct.flatMap((node) => [...node.fixed.keys()]));
|
|
162
|
+
for (const segment of fixed) {
|
|
163
|
+
const children = distinct.flatMap((node) => {
|
|
164
|
+
const child = node.fixed.get(segment);
|
|
165
|
+
return child ? [child] : [];
|
|
166
|
+
});
|
|
167
|
+
state.fixed.set(segment, compile([...children, ...parameters]));
|
|
168
|
+
}
|
|
169
|
+
return state;
|
|
170
|
+
}
|
|
171
|
+
const templates = new Map([...roots].map(([method, root]) => [method, compile([root])]));
|
|
172
|
+
/**
|
|
173
|
+
* The route for a method and the path's segments: an exact route first, then one transition per segment
|
|
174
|
+
* through the compiled states.
|
|
175
|
+
*/
|
|
176
|
+
function findRoute(method, segments) {
|
|
177
|
+
const fixed = exact.get(`${method} /${segments.join("/")}`);
|
|
178
|
+
if (fixed) return fixed;
|
|
179
|
+
let state = templates.get(method);
|
|
180
|
+
for (const segment of segments) {
|
|
181
|
+
if (!segment || !state) return null;
|
|
182
|
+
state = state.fixed.get(segment) ?? state.parameter;
|
|
183
|
+
}
|
|
184
|
+
return state?.route ?? null;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* The route a request reaches, with its path parameters: every segment is decoded before it is matched, so
|
|
188
|
+
* `/v1/notes/%61rchive` is `/v1/notes/archive` and reaches its route, never a sibling `{noteId}` as the
|
|
189
|
+
* value `archive`. Null when none matches, or a segment is empty or decodes to an unsafe value.
|
|
190
|
+
*/
|
|
191
|
+
function match(method, path) {
|
|
192
|
+
if (!path.startsWith("/")) return null;
|
|
193
|
+
const segments = [];
|
|
194
|
+
for (const segment of path.slice(1).split("/")) {
|
|
195
|
+
const decoded = decodePathParameter(segment);
|
|
196
|
+
if (decoded === null) return null;
|
|
197
|
+
segments.push(decoded);
|
|
198
|
+
}
|
|
199
|
+
const route = findRoute(method, segments);
|
|
200
|
+
if (!route) return null;
|
|
201
|
+
const parameters = {};
|
|
202
|
+
for (const parameter of route.parameters) parameters[parameter.name] = segments[parameter.position] ?? "";
|
|
203
|
+
return {
|
|
204
|
+
definition: route.definition,
|
|
205
|
+
parameters
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
return {
|
|
209
|
+
match,
|
|
210
|
+
/** Whether any route answers a declared path, so a transport cannot claim an operation's path. */
|
|
211
|
+
ownsPath(path) {
|
|
212
|
+
const segments = path.slice(1).split("/");
|
|
213
|
+
return [...HTTP_METHODS].some((method) => findRoute(method, segments) !== null);
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export { operationPathParameters as a, isPathParameterValue as i, decodePathParameter as n, isInputHeaderName as r, createHttpRouteResolver as t };
|
|
@@ -1,16 +1,24 @@
|
|
|
1
|
-
import { r as readBodyText, t as httpBodyFailure } from "./body-
|
|
1
|
+
import { r as readBodyText, t as httpBodyFailure } from "./body-DOIHwIIl.js";
|
|
2
2
|
|
|
3
3
|
// src/internal/json.ts
|
|
4
4
|
/** The media type of a Content-Type value, lowercased and without its parameters. */
|
|
5
5
|
function mediaType(contentType) {
|
|
6
6
|
return contentType?.split(";", 1)[0]?.trim().toLowerCase();
|
|
7
7
|
}
|
|
8
|
-
/**
|
|
8
|
+
/**
|
|
9
|
+
* Whether a Content-Type value is `application/json` or a structured `+json` type such as
|
|
10
|
+
* `application/problem+json`, whatever its parameters and case.
|
|
11
|
+
*/
|
|
9
12
|
function isJsonMediaType(value) {
|
|
10
13
|
const type = mediaType(value);
|
|
11
14
|
return type === "application/json" || /^application\/[a-z0-9!#$&^_.+-]+\+json$/.test(type ?? "");
|
|
12
15
|
}
|
|
13
|
-
/**
|
|
16
|
+
/**
|
|
17
|
+
* Reads a JSON body within `maxBytes` and parses it, as `unknown` for a schema to check. A Content-Type other
|
|
18
|
+
* than `application/json` (or any `+json` type, with `allowJsonSuffix`) fails as `unexpected_content_type`
|
|
19
|
+
* before anything is read, and text that does not parse as `invalid_json`; the other failures are
|
|
20
|
+
* `readBodyText`'s.
|
|
21
|
+
*/
|
|
14
22
|
async function readJsonBody(source, options) {
|
|
15
23
|
const contentType = source.headers.get("content-type");
|
|
16
24
|
if (!(options.allowJsonSuffix ? isJsonMediaType(contentType) : mediaType(contentType) === "application/json")) {
|
|
@@ -34,7 +42,11 @@ async function readJsonBody(source, options) {
|
|
|
34
42
|
};
|
|
35
43
|
}
|
|
36
44
|
}
|
|
37
|
-
/**
|
|
45
|
+
/**
|
|
46
|
+
* Reads a response's JSON body as `readJsonBody` does, accepting any `+json` type: a 204 or 205 reads as
|
|
47
|
+
* `null`, and a signal already aborted fails as `aborted` without reading. Pass the signal the `fetch` used,
|
|
48
|
+
* so one deadline covers the request and its body.
|
|
49
|
+
*/
|
|
38
50
|
async function readJsonResponse(response, options) {
|
|
39
51
|
if (options.signal.aborted) {
|
|
40
52
|
response.body?.cancel().catch(() => {});
|
|
@@ -52,7 +64,11 @@ async function readJsonResponse(response, options) {
|
|
|
52
64
|
allowJsonSuffix: true
|
|
53
65
|
});
|
|
54
66
|
}
|
|
55
|
-
/**
|
|
67
|
+
/**
|
|
68
|
+
* The seconds a `Retry-After` value asks a client to wait, as delta seconds or as an HTTP date relative to
|
|
69
|
+
* `nowMs`, rounded up and held between 0 and `maximum` (an hour by default); undefined when the value is
|
|
70
|
+
* absent or unreadable. It is a hint: it never makes repeating a write safe.
|
|
71
|
+
*/
|
|
56
72
|
function retryAfterSeconds(value, nowMs, maximum = 3600) {
|
|
57
73
|
if (!value || !Number.isFinite(nowMs) || !Number.isSafeInteger(maximum) || maximum < 0) return;
|
|
58
74
|
const seconds = /^\d+$/.test(value.trim()) ? Number(value.trim()) : (Date.parse(value) - nowMs) / 1e3;
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
// src/project/names.ts
|
|
2
|
+
/** What a method does to one item: `GET /v1/notes/{noteId}` gets a note. */
|
|
3
|
+
const ITEM_VERBS = {
|
|
4
|
+
GET: "get",
|
|
5
|
+
POST: "post",
|
|
6
|
+
PUT: "replace",
|
|
7
|
+
PATCH: "update",
|
|
8
|
+
DELETE: "delete"
|
|
9
|
+
};
|
|
10
|
+
/** What a method does to a collection: `GET /v1/notes` lists notes and `POST /v1/notes` creates one. */
|
|
11
|
+
const COLLECTION_VERBS = {
|
|
12
|
+
GET: "list",
|
|
13
|
+
POST: "create",
|
|
14
|
+
PUT: "replace",
|
|
15
|
+
PATCH: "update",
|
|
16
|
+
DELETE: "delete"
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Words the ending rules get wrong: plurals whose singular no rule finds (`diagnoses`), singulars that end
|
|
20
|
+
* in s (`alias`, `diabetes`), and words that are their own plural or name one thing, which a path treats as
|
|
21
|
+
* a singular: `GET /v1/analytics` gets analytics rather than listing them.
|
|
22
|
+
*/
|
|
23
|
+
const IRREGULAR_SINGULARS = {
|
|
24
|
+
people: "person",
|
|
25
|
+
children: "child",
|
|
26
|
+
men: "man",
|
|
27
|
+
women: "woman",
|
|
28
|
+
feet: "foot",
|
|
29
|
+
teeth: "tooth",
|
|
30
|
+
analyses: "analysis",
|
|
31
|
+
diagnoses: "diagnosis",
|
|
32
|
+
prognoses: "prognosis",
|
|
33
|
+
hypotheses: "hypothesis",
|
|
34
|
+
syntheses: "synthesis",
|
|
35
|
+
theses: "thesis",
|
|
36
|
+
crises: "crisis",
|
|
37
|
+
indices: "index",
|
|
38
|
+
matrices: "matrix",
|
|
39
|
+
vertices: "vertex",
|
|
40
|
+
appendices: "appendix",
|
|
41
|
+
criteria: "criterion",
|
|
42
|
+
phenomena: "phenomenon",
|
|
43
|
+
aliases: "alias",
|
|
44
|
+
biases: "bias",
|
|
45
|
+
canvases: "canvas",
|
|
46
|
+
statuses: "status",
|
|
47
|
+
campuses: "campus",
|
|
48
|
+
viruses: "virus",
|
|
49
|
+
bonuses: "bonus",
|
|
50
|
+
buses: "bus",
|
|
51
|
+
censuses: "census",
|
|
52
|
+
surpluses: "surplus",
|
|
53
|
+
caches: "cache",
|
|
54
|
+
niches: "niche",
|
|
55
|
+
headaches: "headache",
|
|
56
|
+
movies: "movie",
|
|
57
|
+
cookies: "cookie",
|
|
58
|
+
calories: "calorie",
|
|
59
|
+
selfies: "selfie",
|
|
60
|
+
quizzes: "quiz",
|
|
61
|
+
series: "series",
|
|
62
|
+
species: "species",
|
|
63
|
+
news: "news",
|
|
64
|
+
analytics: "analytics",
|
|
65
|
+
genomics: "genomics",
|
|
66
|
+
logistics: "logistics",
|
|
67
|
+
alias: "alias",
|
|
68
|
+
bias: "bias",
|
|
69
|
+
canvas: "canvas",
|
|
70
|
+
atlas: "atlas",
|
|
71
|
+
gas: "gas",
|
|
72
|
+
pancreas: "pancreas",
|
|
73
|
+
diabetes: "diabetes",
|
|
74
|
+
herpes: "herpes",
|
|
75
|
+
rabies: "rabies",
|
|
76
|
+
scabies: "scabies",
|
|
77
|
+
measles: "measles",
|
|
78
|
+
mumps: "mumps"
|
|
79
|
+
};
|
|
80
|
+
/** Ending rules, in order: the first ending a plural has gives its singular. */
|
|
81
|
+
const SINGULAR_ENDINGS = [
|
|
82
|
+
[/([^aeiou])ies$/, "$1y"],
|
|
83
|
+
[/(ss|sh|ch|x|zz)es$/, "$1"],
|
|
84
|
+
[/(ss|us|is)$/, "$1"],
|
|
85
|
+
[/s$/, ""]
|
|
86
|
+
];
|
|
87
|
+
/** Words that complete the verb before them: `check-in` checks in, `opt-out` opts out. */
|
|
88
|
+
const PARTICLES = /* @__PURE__ */ new Set([
|
|
89
|
+
"in",
|
|
90
|
+
"out",
|
|
91
|
+
"up",
|
|
92
|
+
"off",
|
|
93
|
+
"on",
|
|
94
|
+
"down",
|
|
95
|
+
"over",
|
|
96
|
+
"back",
|
|
97
|
+
"away"
|
|
98
|
+
]);
|
|
99
|
+
/** Words that qualify the verb after them: `bulk-delete` deletes in bulk. */
|
|
100
|
+
const VERB_PREFIXES = /* @__PURE__ */ new Set(["bulk", "batch"]);
|
|
101
|
+
/** The singular of an English word: `notes` is `note`, `categories` `category`, `diagnoses` `diagnosis`. */
|
|
102
|
+
function singular(word) {
|
|
103
|
+
const irregular = IRREGULAR_SINGULARS[word];
|
|
104
|
+
if (irregular !== void 0) return irregular;
|
|
105
|
+
for (const [plural, replacement] of SINGULAR_ENDINGS) if (plural.test(word)) return word.replace(plural, replacement);
|
|
106
|
+
return word;
|
|
107
|
+
}
|
|
108
|
+
/** A segment's words in lowercase: `task-lists`, `task_lists` and `taskLists` are `task` and `lists`. */
|
|
109
|
+
function segmentWords(segment) {
|
|
110
|
+
return segment.replaceAll(/([a-z0-9])([A-Z])/g, "$1 $2").split(/[\s_-]+/).filter(Boolean).map((word) => word.toLowerCase());
|
|
111
|
+
}
|
|
112
|
+
/** A segment's words with the last one in the singular: `task-lists` is `task` and `list`. */
|
|
113
|
+
function singularWords(segment) {
|
|
114
|
+
const words = segmentWords(segment);
|
|
115
|
+
const last = words.pop();
|
|
116
|
+
return last === void 0 ? words : [...words, singular(last)];
|
|
117
|
+
}
|
|
118
|
+
/** Whether a segment's last word is a plural its singular differs from. */
|
|
119
|
+
function isPlural(segment) {
|
|
120
|
+
const last = segmentWords(segment).at(-1);
|
|
121
|
+
return last !== void 0 && singular(last) !== last;
|
|
122
|
+
}
|
|
123
|
+
/** Whether a segment is a `{parameter}`. */
|
|
124
|
+
function isParameter(segment) {
|
|
125
|
+
return segment?.startsWith("{") === true;
|
|
126
|
+
}
|
|
127
|
+
/** A path's version and its segments after it: `/v2/notes` is version `v2` and the segment `notes`. */
|
|
128
|
+
function pathParts(path) {
|
|
129
|
+
const segments = path.split("/").filter(Boolean);
|
|
130
|
+
const [first] = segments;
|
|
131
|
+
if (first !== void 0 && /^v[1-9][0-9]*$/.test(first)) return {
|
|
132
|
+
version: first,
|
|
133
|
+
segments: segments.slice(1)
|
|
134
|
+
};
|
|
135
|
+
return {
|
|
136
|
+
version: void 0,
|
|
137
|
+
segments
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* The words the segments before an operation's target add to it: a segment followed by a parameter names
|
|
142
|
+
* one item, in the singular (`days/{date}` is `day`), `me` is the caller's (`my`), and any other fixed
|
|
143
|
+
* segment stays as it is (`admin`).
|
|
144
|
+
*/
|
|
145
|
+
function ownerWords(segments) {
|
|
146
|
+
return segments.flatMap((segment, index) => {
|
|
147
|
+
if (isParameter(segment)) return [];
|
|
148
|
+
if (segment === "me") return ["my"];
|
|
149
|
+
return isParameter(segments[index + 1]) ? singularWords(segment) : segmentWords(segment);
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* An action's words split around what it acts on: its verb first, with a particle that completes it
|
|
154
|
+
* (`check-in`) or after a prefix that qualifies it (`bulk-delete`), then the rest (`mark-read`).
|
|
155
|
+
*/
|
|
156
|
+
function actionWords(action) {
|
|
157
|
+
const words = segmentWords(action);
|
|
158
|
+
const [first = "", second] = words;
|
|
159
|
+
const verbLength = second !== void 0 && (PARTICLES.has(second) || VERB_PREFIXES.has(first)) ? 2 : 1;
|
|
160
|
+
return {
|
|
161
|
+
verb: words.slice(0, verbLength),
|
|
162
|
+
rest: words.slice(verbLength)
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* The phrase of an operation on the segments after its version. A path that ends in a parameter acts on one
|
|
167
|
+
* item; one that ends in a plural acts on a collection; one that ends in any other word is an action on what
|
|
168
|
+
* precedes it when the method is POST, such as `archive` in `POST /v1/notes/{noteId}/archive`, and a
|
|
169
|
+
* singleton otherwise, such as `summary` in `GET /v1/notes/{noteId}/summary`. A POST to a word that nothing
|
|
170
|
+
* precedes has nothing to act on, so it creates what the word names: `POST /v1/greeting` creates a greeting.
|
|
171
|
+
*/
|
|
172
|
+
function operationPhrase(method, segments) {
|
|
173
|
+
const target = segments.at(-1);
|
|
174
|
+
const bare = {
|
|
175
|
+
verb: [method.toLowerCase()],
|
|
176
|
+
object: [],
|
|
177
|
+
one: false,
|
|
178
|
+
rest: []
|
|
179
|
+
};
|
|
180
|
+
if (target === void 0) return bare;
|
|
181
|
+
if (isParameter(target)) {
|
|
182
|
+
const collection = segments.at(-2);
|
|
183
|
+
if (collection === void 0 || isParameter(collection)) return bare;
|
|
184
|
+
const object = [...ownerWords(segments.slice(0, -2)), ...singularWords(collection)];
|
|
185
|
+
return {
|
|
186
|
+
verb: [ITEM_VERBS[method]],
|
|
187
|
+
object,
|
|
188
|
+
one: true,
|
|
189
|
+
rest: []
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
const owners = ownerWords(segments.slice(0, -1));
|
|
193
|
+
if (isPlural(target)) {
|
|
194
|
+
const created = method === "POST";
|
|
195
|
+
const words = created ? singularWords(target) : segmentWords(target);
|
|
196
|
+
return {
|
|
197
|
+
verb: [COLLECTION_VERBS[method]],
|
|
198
|
+
object: [...owners, ...words],
|
|
199
|
+
one: created,
|
|
200
|
+
rest: []
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
if (method === "POST" && !owners.length) return {
|
|
204
|
+
verb: ["create"],
|
|
205
|
+
object: segmentWords(target),
|
|
206
|
+
one: true,
|
|
207
|
+
rest: []
|
|
208
|
+
};
|
|
209
|
+
if (method === "POST") {
|
|
210
|
+
const { verb, rest } = actionWords(target);
|
|
211
|
+
return {
|
|
212
|
+
verb,
|
|
213
|
+
object: owners.at(-1) === "my" && !rest.length ? [...owners.slice(0, -1), "me"] : owners,
|
|
214
|
+
one: isParameter(segments.at(-2)),
|
|
215
|
+
rest
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
return {
|
|
219
|
+
verb: [ITEM_VERBS[method]],
|
|
220
|
+
object: [...owners, ...segmentWords(target)],
|
|
221
|
+
one: false,
|
|
222
|
+
rest: []
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
/** Words in camelCase: `get`, `day` and `booking` are `getDayBooking`. */
|
|
226
|
+
function camelCase(words) {
|
|
227
|
+
return words.map((word, index) => index === 0 ? word : word.charAt(0).toUpperCase() + word.slice(1)).join("");
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* A route's operation name: `GET /v1/notes` is `listNotes`, `GET /v1/notes/{noteId}` is `getNote`,
|
|
231
|
+
* `POST /v1/notes` is `createNote`, `PATCH /v1/notes/{noteId}` is `updateNote`,
|
|
232
|
+
* `POST /v1/notes/{noteId}/archive` is `archiveNote` and `GET /v1/days/{date}/slots` is `listDaySlots`. A
|
|
233
|
+
* version other than 1 ends the name: `GET /v2/notes` is `listNotesV2`.
|
|
234
|
+
*/
|
|
235
|
+
function routeName(method, path) {
|
|
236
|
+
const { version, segments } = pathParts(path);
|
|
237
|
+
const { verb, object, rest } = operationPhrase(method, segments);
|
|
238
|
+
const suffix = version === void 0 || version === "v1" ? "" : version.toUpperCase();
|
|
239
|
+
return `${camelCase([
|
|
240
|
+
...verb,
|
|
241
|
+
...object,
|
|
242
|
+
...rest
|
|
243
|
+
])}${suffix}`;
|
|
244
|
+
}
|
|
245
|
+
/** Words that take `a` though they start with a vowel, and `an` though they start with a consonant. */
|
|
246
|
+
const ARTICLE_EXCEPTIONS = [
|
|
247
|
+
["uni", "a"],
|
|
248
|
+
["use", "a"],
|
|
249
|
+
["usa", "a"],
|
|
250
|
+
["eu", "a"],
|
|
251
|
+
["one", "a"],
|
|
252
|
+
["hour", "an"],
|
|
253
|
+
["honest", "an"],
|
|
254
|
+
["honor", "an"]
|
|
255
|
+
];
|
|
256
|
+
/** The indefinite article before a word: `a note`, `an invoice`, `a user`, `an hour`. */
|
|
257
|
+
function article(word) {
|
|
258
|
+
const exception = ARTICLE_EXCEPTIONS.find(([prefix]) => word.startsWith(prefix));
|
|
259
|
+
if (exception) return exception[1];
|
|
260
|
+
return /^[aeiou]/.test(word) ? "an" : "a";
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* A route's title, from its method and path as its name: `Get a note`, `List notes`, `Create a day booking`,
|
|
264
|
+
* `Mark a note read`. A version other than 1 follows it: `List notes (v2)`.
|
|
265
|
+
*/
|
|
266
|
+
function routeTitle(method, path) {
|
|
267
|
+
const { version, segments } = pathParts(path);
|
|
268
|
+
const { verb, object, one, rest } = operationPhrase(method, segments);
|
|
269
|
+
const [first] = object;
|
|
270
|
+
const named = one && first !== void 0 ? [article(first), ...object] : object;
|
|
271
|
+
const phrase = [
|
|
272
|
+
...verb,
|
|
273
|
+
...named,
|
|
274
|
+
...rest
|
|
275
|
+
].join(" ");
|
|
276
|
+
const title = phrase.charAt(0).toUpperCase() + phrase.slice(1);
|
|
277
|
+
return version === void 0 || version === "v1" ? title : `${title} (${version})`;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export { routeTitle as n, routeName as t };
|