@peasant-labs/schema 0.52.0 → 0.53.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 +8 -0
- package/dist/internal/generated/routes.gen.d.ts +652 -0
- package/dist/internal/generated/routes.gen.js +808 -0
- package/dist/local-api.d.ts +2 -0
- package/dist/local-api.js +1 -0
- package/dist/route-member.d.ts +33 -0
- package/dist/route-member.js +50 -0
- package/dist/village-api.d.ts +2 -0
- package/dist/village-api.js +1 -0
- package/package.json +1 -1
package/dist/local-api.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { components as GeneratedComponents, operations as GeneratedOperations, paths as GeneratedPaths } from "./internal/generated/local-api.js";
|
|
2
2
|
export { PeasantLocalAPIVersion, LocalKickstartDroppedLabelsMax } from "./index.js";
|
|
3
|
+
export { ALL_HTTP_METHODS, ALL_LOCAL_ROUTES, HttpMethod, LocalRoute, isHttpMethod } from "./internal/generated/routes.gen.js";
|
|
4
|
+
export type { PathArgs, PathParams, RouteMember } from "./route-member.js";
|
|
3
5
|
export type paths = GeneratedPaths;
|
|
4
6
|
export type operations = GeneratedOperations;
|
|
5
7
|
export type components = GeneratedComponents;
|
package/dist/local-api.js
CHANGED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { HttpMethod } from "./internal/generated/routes.gen.js";
|
|
2
|
+
type PathParamNames<P extends string> = P extends `${string}{${infer Name}}${infer Rest}` ? Name | PathParamNames<Rest> : never;
|
|
3
|
+
/** PathParams is the parameter object of a path template: one key per {name}. */
|
|
4
|
+
export type PathParams<P extends string> = {
|
|
5
|
+
readonly [Name in PathParamNames<P>]: string | number;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* PathArgs is the argument list of a route member's path builder: no argument
|
|
9
|
+
* when the template has no parameter, otherwise one object carrying exactly the
|
|
10
|
+
* template's parameters. A missing or extra key is a compile error.
|
|
11
|
+
*/
|
|
12
|
+
export type PathArgs<P extends string> = [PathParamNames<P>] extends [never] ? [] : [params: PathParams<P>];
|
|
13
|
+
/** RouteMember is one operation a Village API or Local API document declares. */
|
|
14
|
+
export interface RouteMember<P extends string, M extends HttpMethod> {
|
|
15
|
+
/** The HTTP method, as sent on the wire. */
|
|
16
|
+
readonly method: M;
|
|
17
|
+
/** The document's path template, for example "/api/v1/pulls/{owner}/{name}/{number}". */
|
|
18
|
+
readonly pattern: P;
|
|
19
|
+
/** The document's operationId, for example "getPullRequestAttachment". */
|
|
20
|
+
readonly operationId: string;
|
|
21
|
+
/**
|
|
22
|
+
* Builds the request path. Every parameter value is escaped with
|
|
23
|
+
* encodeURIComponent. A value that is empty, "." or ".." throws a RangeError:
|
|
24
|
+
* clients resolve such a segment, so the request would reach another endpoint.
|
|
25
|
+
*/
|
|
26
|
+
path(...args: PathArgs<P>): string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* routeMember builds one frozen member. The generated route modules are its
|
|
30
|
+
* only callers; the template is split once, when the module loads.
|
|
31
|
+
*/
|
|
32
|
+
export declare function routeMember<P extends string, M extends HttpMethod>(method: M, pattern: P, operationId: string): RouteMember<P, M>;
|
|
33
|
+
export {};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
const parameterPattern = /\{([^{}]+)\}/g;
|
|
2
|
+
/**
|
|
3
|
+
* routeMember builds one frozen member. The generated route modules are its
|
|
4
|
+
* only callers; the template is split once, when the module loads.
|
|
5
|
+
*/
|
|
6
|
+
export function routeMember(method, pattern, operationId) {
|
|
7
|
+
const parts = [];
|
|
8
|
+
let offset = 0;
|
|
9
|
+
for (const match of pattern.matchAll(parameterPattern)) {
|
|
10
|
+
parts.push({ literal: pattern.slice(offset, match.index) });
|
|
11
|
+
parts.push({ parameter: match[1] ?? "" });
|
|
12
|
+
offset = match.index + match[0].length;
|
|
13
|
+
}
|
|
14
|
+
parts.push({ literal: pattern.slice(offset) });
|
|
15
|
+
const frozenParts = Object.freeze(parts.map((part) => Object.freeze(part)));
|
|
16
|
+
function path(...args) {
|
|
17
|
+
// A JavaScript caller can pass anything; a value that is not an object
|
|
18
|
+
// carries no parameter, so each parameter is then refused as missing.
|
|
19
|
+
const given = args[0];
|
|
20
|
+
const params = typeof given === "object" && given !== null ? args[0] : undefined;
|
|
21
|
+
let out = "";
|
|
22
|
+
for (const part of frozenParts) {
|
|
23
|
+
if ("literal" in part) {
|
|
24
|
+
out += part.literal;
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
const value = params === undefined || !Object.hasOwn(params, part.parameter) ? undefined : params[part.parameter];
|
|
28
|
+
out += encodeURIComponent(pathValue(operationId, pattern, part.parameter, value));
|
|
29
|
+
}
|
|
30
|
+
return out;
|
|
31
|
+
}
|
|
32
|
+
return Object.freeze({ method, pattern, operationId, path });
|
|
33
|
+
}
|
|
34
|
+
const unaddressableSegments = new Set(["", ".", ".."]);
|
|
35
|
+
function pathValue(operationId, pattern, parameter, value) {
|
|
36
|
+
if (typeof value === "string") {
|
|
37
|
+
if (!unaddressableSegments.has(value))
|
|
38
|
+
return value;
|
|
39
|
+
// The Go path builders return the same text in route.PathSegmentError.
|
|
40
|
+
throw new RangeError(`route path for ${operationId} (${pattern}) could not be built: parameter ${JSON.stringify(parameter)} is ${value === "" ? "empty" : JSON.stringify(value)}; ` +
|
|
41
|
+
'a path segment that is empty, "." or ".." is resolved by clients and proxies, so the request would reach a different endpoint; ' +
|
|
42
|
+
"pass the identifier of the resource the parameter names.");
|
|
43
|
+
}
|
|
44
|
+
if (typeof value === "number" && Number.isFinite(value))
|
|
45
|
+
return String(value);
|
|
46
|
+
const received = value === undefined ? "no value" : typeof value === "number" ? String(value) : `a ${value === null ? "null" : typeof value}`;
|
|
47
|
+
throw new TypeError(`route path for ${operationId} (${pattern}) could not be built: parameter ${JSON.stringify(parameter)} received ${received}; ` +
|
|
48
|
+
"a path parameter must be a string or a finite number, so the request would address the wrong resource; " +
|
|
49
|
+
`pass ${JSON.stringify(parameter)} in the object given to path().`);
|
|
50
|
+
}
|
package/dist/village-api.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import type { operations as GeneratedOperations, paths as GeneratedPaths } from "./internal/generated/village-api.js";
|
|
2
2
|
export { VillageAPIVersion } from "./index.js";
|
|
3
|
+
export { ALL_HTTP_METHODS, ALL_VILLAGE_ROUTES, HttpMethod, VillageRoute, isHttpMethod } from "./internal/generated/routes.gen.js";
|
|
4
|
+
export type { PathArgs, PathParams, RouteMember } from "./route-member.js";
|
|
3
5
|
export type paths = GeneratedPaths;
|
|
4
6
|
export type operations = GeneratedOperations;
|
package/dist/village-api.js
CHANGED