shelving 1.152.1 → 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/util.js +2 -2
- package/package.json +1 -1
- package/util/template.d.ts +9 -11
- package/util/template.js +10 -4
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 });
|