shelving 1.152.0 → 1.152.2
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/api/Endpoint.d.ts +18 -18
- package/api/util.js +2 -2
- package/package.json +1 -1
- package/util/template.d.ts +9 -11
- package/util/template.js +10 -4
package/api/Endpoint.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { AbsolutePath } from "../util/path.js";
|
|
2
2
|
import { type Validator } from "../util/validate.js";
|
|
3
3
|
import type { EndpointCallback, EndpointHandler } from "./util.js";
|
|
4
4
|
/** Types for an HTTP request or response that does something. */
|
|
@@ -15,12 +15,12 @@ export declare class Endpoint<P, R> {
|
|
|
15
15
|
/** Endpoint method. */
|
|
16
16
|
readonly method: EndpointMethod;
|
|
17
17
|
/** Endpoint path, e.g. `/patient/{id}` */
|
|
18
|
-
readonly path:
|
|
18
|
+
readonly path: AbsolutePath;
|
|
19
19
|
/** Payload validator. */
|
|
20
20
|
readonly payload: Validator<P>;
|
|
21
21
|
/** Result validator. */
|
|
22
22
|
readonly result: Validator<R>;
|
|
23
|
-
constructor(method: EndpointMethod, path:
|
|
23
|
+
constructor(method: EndpointMethod, path: AbsolutePath, payload: Validator<P>, result: Validator<R>);
|
|
24
24
|
/**
|
|
25
25
|
* Return an `EndpointHandler` for this endpoint.
|
|
26
26
|
*
|
|
@@ -46,34 +46,34 @@ export type EndpointType<X extends Endpoint<unknown, unknown>> = X extends Endpo
|
|
|
46
46
|
* Represent a GET request to a specified path, with validated payload and return types.
|
|
47
47
|
* "The GET method requests a representation of the specified resource. Requests using GET should only retrieve data and should not contain a request content."
|
|
48
48
|
*/
|
|
49
|
-
export declare function GET<P, R>(path:
|
|
50
|
-
export declare function GET<P>(path:
|
|
51
|
-
export declare function GET<R>(path:
|
|
49
|
+
export declare function GET<P, R>(path: AbsolutePath, payload?: Validator<P>, result?: Validator<R>): Endpoint<P, R>;
|
|
50
|
+
export declare function GET<P>(path: AbsolutePath, payload: Validator<P>): Endpoint<P, undefined>;
|
|
51
|
+
export declare function GET<R>(path: AbsolutePath, payload: undefined, result: Validator<R>): Endpoint<undefined, R>;
|
|
52
52
|
/**
|
|
53
53
|
* Represent a POST request to a specified path, with validated payload and return types.
|
|
54
54
|
* "The POST method submits an entity to the specified resource, often causing a change in state or side effects on the server.
|
|
55
55
|
*/
|
|
56
|
-
export declare function POST<P, R>(path:
|
|
57
|
-
export declare function POST<P>(path:
|
|
58
|
-
export declare function POST<R>(path:
|
|
56
|
+
export declare function POST<P, R>(path: AbsolutePath, payload?: Validator<P>, result?: Validator<R>): Endpoint<P, R>;
|
|
57
|
+
export declare function POST<P>(path: AbsolutePath, payload: Validator<P>): Endpoint<P, undefined>;
|
|
58
|
+
export declare function POST<R>(path: AbsolutePath, payload: undefined, result: Validator<R>): Endpoint<undefined, R>;
|
|
59
59
|
/**
|
|
60
60
|
* Represent a PUT request to a specified path, with validated payload and return types.
|
|
61
61
|
* "The PUT method replaces all current representations of the target resource with the request content."
|
|
62
62
|
*/
|
|
63
|
-
export declare function PUT<P, R>(path:
|
|
64
|
-
export declare function PUT<P>(path:
|
|
65
|
-
export declare function PUT<R>(path:
|
|
63
|
+
export declare function PUT<P, R>(path: AbsolutePath, payload?: Validator<P>, result?: Validator<R>): Endpoint<P, R>;
|
|
64
|
+
export declare function PUT<P>(path: AbsolutePath, payload: Validator<P>): Endpoint<P, undefined>;
|
|
65
|
+
export declare function PUT<R>(path: AbsolutePath, payload: undefined, result: Validator<R>): Endpoint<undefined, R>;
|
|
66
66
|
/**
|
|
67
67
|
* Represent a PATCH request to a specified path, with validated payload and return types.
|
|
68
68
|
* "The PATCH method applies partial modifications to a resource."
|
|
69
69
|
*/
|
|
70
|
-
export declare function PATCH<P, R>(path:
|
|
71
|
-
export declare function PATCH<P>(path:
|
|
72
|
-
export declare function PATCH<R>(path:
|
|
70
|
+
export declare function PATCH<P, R>(path: AbsolutePath, payload?: Validator<P>, result?: Validator<R>): Endpoint<P, R>;
|
|
71
|
+
export declare function PATCH<P>(path: AbsolutePath, payload: Validator<P>): Endpoint<P, undefined>;
|
|
72
|
+
export declare function PATCH<R>(path: AbsolutePath, payload: undefined, result: Validator<R>): Endpoint<undefined, R>;
|
|
73
73
|
/**
|
|
74
74
|
* Represent a DELETE request to a specified path, with validated payload and return types.
|
|
75
75
|
* "The DELETE method deletes the specified resource."
|
|
76
76
|
*/
|
|
77
|
-
export declare function DELETE<P, R>(path:
|
|
78
|
-
export declare function DELETE<P>(path:
|
|
79
|
-
export declare function DELETE<R>(path:
|
|
77
|
+
export declare function DELETE<P, R>(path: AbsolutePath, payload?: Validator<P>, result?: Validator<R>): Endpoint<P, R>;
|
|
78
|
+
export declare function DELETE<P>(path: AbsolutePath, payload: Validator<P>): Endpoint<P, undefined>;
|
|
79
|
+
export declare function DELETE<R>(path: AbsolutePath, payload: undefined, result: Validator<R>): Endpoint<undefined, R>;
|
package/api/util.js
CHANGED
|
@@ -32,9 +32,9 @@ export function handleEndpoints(request, endpoints) {
|
|
|
32
32
|
if (!pathParams)
|
|
33
33
|
continue;
|
|
34
34
|
// Make a simple dictionary object from the `{placeholder}` path params and the `?a=123` query params from the URL.
|
|
35
|
-
const
|
|
35
|
+
const combinedParams = searchParams.size ? { ...getDictionary(searchParams), ...pathParams } : pathParams;
|
|
36
36
|
// Get the response by calling the callback.
|
|
37
|
-
return handleEndpoint(endpoint, callback,
|
|
37
|
+
return handleEndpoint(endpoint, callback, combinedParams, request);
|
|
38
38
|
}
|
|
39
39
|
// No handler matched the request.
|
|
40
40
|
throw new NotFoundError("No matching endpoint", { received: requestUrl, caller: handleEndpoints });
|
package/package.json
CHANGED
package/util/template.d.ts
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
1
|
import type { ImmutableArray } from "./array.js";
|
|
2
2
|
import { type ImmutableDictionary } from "./dictionary.js";
|
|
3
3
|
import type { AnyCaller } from "./function.js";
|
|
4
|
-
import type
|
|
5
|
-
/** Dictionary of named template values in `{ myPlaceholder: "value" }` format. */
|
|
6
|
-
export type TemplateDictionary = ImmutableDictionary<string>;
|
|
7
|
-
/** Callback that returns the right replacement string for a given placeholder. */
|
|
8
|
-
export type TemplateCallback = (placeholder: string) => string;
|
|
4
|
+
import { type NotString, type PossibleString } from "./string.js";
|
|
9
5
|
/**
|
|
10
6
|
* Things that can be converted to the value for a named placeholder.
|
|
11
7
|
*
|
|
12
|
-
* `
|
|
8
|
+
* `PossibleString` — Single string used for every `{placeholder}`
|
|
13
9
|
* `ImmutableArray<string>` — Array of strings (or functions that return strings) used for `*` numbered placeholders e.g. `["John"]`
|
|
14
|
-
* `
|
|
15
|
-
* `
|
|
10
|
+
* `ImmutableDictionary<PossibleString>` — Object containing named strings used for named placeholders, e.g. `{ val1: "Ellie", val2: 123 }`
|
|
11
|
+
* `(placeholder: string) => string` — Function that returns the right string for a named `{placeholder}`.v
|
|
16
12
|
*/
|
|
17
|
-
export type TemplateValues =
|
|
13
|
+
export type TemplateValues = PossibleString | ImmutableArray<PossibleString> | ImmutableDictionary<PossibleString> | ((placeholder: string) => string);
|
|
14
|
+
/** The output of matching a template is a dictionary in `{ myPlaceholder: "value" }` format. */
|
|
15
|
+
export type TemplateMatches = ImmutableDictionary<string>;
|
|
18
16
|
/**
|
|
19
17
|
* Get list of placeholders named in a template string.
|
|
20
18
|
*
|
|
@@ -32,11 +30,11 @@ export declare function getPlaceholders(template: string): readonly string[];
|
|
|
32
30
|
*
|
|
33
31
|
* @return An object containing values, e.g. `{ name: "Dave", country: "UK", city: "Manchester" }`, or undefined if target didn't match the template.
|
|
34
32
|
*/
|
|
35
|
-
export declare function matchTemplate(template: string, target: string, caller?: AnyCaller):
|
|
33
|
+
export declare function matchTemplate(template: string, target: string, caller?: AnyCaller): TemplateMatches | undefined;
|
|
36
34
|
/**
|
|
37
35
|
* Match multiple templates against a target string and return the first match.
|
|
38
36
|
*/
|
|
39
|
-
export declare function matchTemplates(templates: Iterable<string> & NotString, target: string):
|
|
37
|
+
export declare function matchTemplates(templates: Iterable<string> & NotString, target: string): TemplateMatches | undefined;
|
|
40
38
|
/**
|
|
41
39
|
* Turn ":year-:month" and `{ year: "2016"... }` etc into "2016-06..." etc.
|
|
42
40
|
*
|
package/util/template.js
CHANGED
|
@@ -3,6 +3,7 @@ import { ValueError } from "../error/ValueError.js";
|
|
|
3
3
|
import { EMPTY_DICTIONARY } from "./dictionary.js";
|
|
4
4
|
import { setMapItem } from "./map.js";
|
|
5
5
|
import { isObject } from "./object.js";
|
|
6
|
+
import { getString } from "./string.js";
|
|
6
7
|
// RegExp to find named variables in several formats e.g. `:a`, `${b}`, `{{c}}` or `{d}`
|
|
7
8
|
const R_PLACEHOLDERS = /(\*|:[a-z][a-z0-9]*|\$\{[a-z][a-z0-9]*\}|\{\{[a-z][a-z0-9]*\}\}|\{[a-z][a-z0-9]*\})/i;
|
|
8
9
|
// Find actual name within template placeholder e.g. `${name}` → `name`
|
|
@@ -112,13 +113,18 @@ export function renderTemplate(template, values, caller = renderTemplate) {
|
|
|
112
113
|
return output;
|
|
113
114
|
}
|
|
114
115
|
function _replaceTemplateKey(key, values, caller) {
|
|
115
|
-
if (typeof values === "string")
|
|
116
|
-
return values;
|
|
117
116
|
if (typeof values === "function")
|
|
118
117
|
return values(key);
|
|
119
118
|
if (isObject(values)) {
|
|
120
|
-
|
|
121
|
-
|
|
119
|
+
// Dictionary or array of values.
|
|
120
|
+
const v = getString(values[key]);
|
|
121
|
+
if (v !== undefined)
|
|
122
|
+
return v;
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
// Single value for all placeholders.
|
|
126
|
+
const v = getString(values);
|
|
127
|
+
if (v !== undefined)
|
|
122
128
|
return v;
|
|
123
129
|
}
|
|
124
130
|
throw new RequiredError(`Template key "${key}" must be defined`, { received: values, key, caller });
|