@gramlot/gramlot 0.2.8 → 0.2.10
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/gramlot.js +207 -165
- package/package.json +2 -3
- package/src/binding/inline.js +1 -1
- package/src/binding/runtime.js +3 -3
- package/src/bootstrap.js +1 -1
- package/src/builder/source.js +1 -1
- package/src/gramlot.d.ts +20 -49
- package/src/gramlot.js +21 -119
- package/src/handlers/dom-handler.d.ts +19 -0
- package/src/handlers/dom-handler.js +16 -0
- package/src/handlers/handler.d.ts +14 -0
- package/src/handlers/handler.js +13 -0
- package/src/handlers/rpc-handler.d.ts +21 -0
- package/src/handlers/rpc-handler.js +18 -0
- package/src/handlers/source-handler.d.ts +47 -0
- package/src/handlers/source-handler.js +114 -0
- package/src/handlers/utilities-handler.d.ts +17 -0
- package/src/handlers/utilities-handler.js +16 -0
- package/src/index.d.ts +6 -1
- package/src/index.js +6 -1
- package/src/inout.d.ts +2 -2
- package/src/inout.js +7 -7
- package/src/routes/README.md +0 -110
- package/src/routes/errors.d.ts +0 -12
- package/src/routes/errors.js +0 -15
- package/src/routes/index.d.ts +0 -14
- package/src/routes/index.js +0 -11
- package/src/routes/router-node.d.ts +0 -44
- package/src/routes/router-node.js +0 -117
- package/src/routes/router.d.ts +0 -68
- package/src/routes/router.js +0 -98
- package/src/routes/routing.d.ts +0 -56
- package/src/routes/routing.js +0 -75
- package/src/routes/signature.d.ts +0 -49
- package/src/routes/signature.js +0 -99
package/src/routes/routing.js
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
/* @ts-self-types="./routing.d.ts" */
|
|
2
|
-
/**
|
|
3
|
-
* `RoutingClass` and `registerRoute`: the base of classes whose methods are reached through routes.
|
|
4
|
-
*
|
|
5
|
-
* @module
|
|
6
|
-
*/
|
|
7
|
-
import {Signature} from './signature.js';
|
|
8
|
-
import {Router} from './router.js';
|
|
9
|
-
|
|
10
|
-
const MARKER = Symbol('gramlot.routes.marker');
|
|
11
|
-
const ROUTER = Symbol('gramlot.routes.router');
|
|
12
|
-
|
|
13
|
-
/** Walk from the instance's prototype to `RoutingClass.prototype`: the first own property of a name decides. */
|
|
14
|
-
const discoverEntries = instance => {
|
|
15
|
-
const entries = new Map();
|
|
16
|
-
const seen = new Set();
|
|
17
|
-
for (let proto = Object.getPrototypeOf(instance); proto; proto = Object.getPrototypeOf(proto)) {
|
|
18
|
-
const own = new Map();
|
|
19
|
-
for (const name of Object.getOwnPropertyNames(proto)) {
|
|
20
|
-
if (seen.has(name)) continue;
|
|
21
|
-
seen.add(name);
|
|
22
|
-
const marker = Object.getOwnPropertyDescriptor(proto, name).value?.[MARKER];
|
|
23
|
-
if (!marker) continue;
|
|
24
|
-
if (own.has(marker.name)) throw new Error(`handler name collision: '${marker.name}'`);
|
|
25
|
-
own.set(marker.name, marker);
|
|
26
|
-
}
|
|
27
|
-
for (const [name, marker] of own) if (!entries.has(name)) entries.set(name, marker);
|
|
28
|
-
if (proto === RoutingClass.prototype) break;
|
|
29
|
-
}
|
|
30
|
-
return entries;
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
/** A handler result with metadata, e.g. `mediaType`, for the dispatcher that builds the response. */
|
|
34
|
-
export class ResultWrapper {
|
|
35
|
-
constructor(value, metadata) {
|
|
36
|
-
this.value = value;
|
|
37
|
-
this.metadata = metadata;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/** True when `obj` is a `ResultWrapper`. */
|
|
42
|
-
export const isResultWrapper = obj => obj instanceof ResultWrapper;
|
|
43
|
-
|
|
44
|
-
/** Base class: each instance owns one `Router`, created on first access of `route`. */
|
|
45
|
-
export class RoutingClass {
|
|
46
|
-
/** Mark the method `methodName` of this class as a route, called after the class body. */
|
|
47
|
-
static registerRoute(methodName, {signature = new Signature(), docline, result, name, meta = {}} = {}) {
|
|
48
|
-
const original = Object.hasOwn(this.prototype, methodName) ? this.prototype[methodName] : undefined;
|
|
49
|
-
if (typeof original !== 'function') {
|
|
50
|
-
throw new TypeError(`registerRoute: '${methodName}' is not a method of ${this.name}`);
|
|
51
|
-
}
|
|
52
|
-
if (original[MARKER]) throw new TypeError(`registerRoute applied twice to '${methodName}'`);
|
|
53
|
-
const wrapper = function (kw = {}) {
|
|
54
|
-
return original.call(this, signature.bind(kw));
|
|
55
|
-
};
|
|
56
|
-
wrapper[MARKER] = {original, signature, docline, result, name: name ?? methodName, meta};
|
|
57
|
-
Object.defineProperty(this.prototype, methodName, {value: wrapper, writable: true, configurable: true});
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/** The instance's router, created on first access. */
|
|
61
|
-
get route() {
|
|
62
|
-
this[ROUTER] ??= new Router(this, discoverEntries(this));
|
|
63
|
-
return this[ROUTER];
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/** Declare child branches on this instance's router. */
|
|
67
|
-
addBranches(specs) {
|
|
68
|
-
return this.route.addBranches(specs);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/** Wrap a handler result with metadata. */
|
|
72
|
-
resultWrapper(value, metadata = {}) {
|
|
73
|
-
return new ResultWrapper(value, metadata);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `Signature` and `ReturnValue`: the declared parameters and result of a routed function.
|
|
3
|
-
*
|
|
4
|
-
* @module
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
/** A TYTX code accepted by a `Signature`. */
|
|
8
|
-
export type TytxCode = 'T' | 'L' | 'R' | 'N' | 'B' | 'D' | 'DHZ' | 'H';
|
|
9
|
-
|
|
10
|
-
/** A required parameter (its code) or an optional one (`[code, default]`). */
|
|
11
|
-
export type ParameterSpec = TytxCode | [TytxCode, unknown];
|
|
12
|
-
|
|
13
|
-
/** One declared parameter. */
|
|
14
|
-
export interface Parameter {
|
|
15
|
-
name: string;
|
|
16
|
-
type: TytxCode;
|
|
17
|
-
required: boolean;
|
|
18
|
-
default: unknown;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/** The named parameters of a routed function: TYTX code, required or default, in declaration order. */
|
|
22
|
-
export class Signature {
|
|
23
|
-
/**
|
|
24
|
-
* Declare the parameters. A TypeError is raised by a name that is not an identifier or is `_extraPath`,
|
|
25
|
-
* a spec that is neither a code nor `[code, default]`, and a default other than `null` that is not a value of its code.
|
|
26
|
-
*/
|
|
27
|
-
constructor(specs?: Record<string, ParameterSpec>);
|
|
28
|
-
/** Copies of the declared specs in order; changing them does not change the signature. */
|
|
29
|
-
readonly parameters: Parameter[];
|
|
30
|
-
/**
|
|
31
|
-
* A new object of the named values `kw` checked against the declaration, defaults applied.
|
|
32
|
-
* Unknown, missing or wrongly typed names raise a TypeError. A non-empty `extraPath` is carried as `_extraPath`;
|
|
33
|
-
* without the option, a non-empty string `_extraPath` of `kw` passes through.
|
|
34
|
-
*/
|
|
35
|
-
bind(kw: Record<string, unknown>, options?: {extraPath?: string}): Record<string, unknown>;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** What a routed function returns, documented only: never checked against the actual return. */
|
|
39
|
-
export class ReturnValue {
|
|
40
|
-
/** A TYTX code or an object of field specs. */
|
|
41
|
-
type?: TytxCode | Record<string, ParameterSpec>;
|
|
42
|
-
mediaType?: string;
|
|
43
|
-
docline?: string;
|
|
44
|
-
constructor(fields?: {
|
|
45
|
-
type?: TytxCode | Record<string, ParameterSpec>;
|
|
46
|
-
mediaType?: string;
|
|
47
|
-
docline?: string;
|
|
48
|
-
});
|
|
49
|
-
}
|
package/src/routes/signature.js
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
/* @ts-self-types="./signature.d.ts" */
|
|
2
|
-
/**
|
|
3
|
-
* `Signature` and `ReturnValue`: the declared parameters and result of a routed function.
|
|
4
|
-
*
|
|
5
|
-
* @module
|
|
6
|
-
*/
|
|
7
|
-
import {isDecimal} from '@genrojs/tytx';
|
|
8
|
-
|
|
9
|
-
const PARAMETERS = Symbol('gramlot.routes.parameters');
|
|
10
|
-
|
|
11
|
-
const isDate = value => value instanceof Date && !Number.isNaN(value.getTime());
|
|
12
|
-
|
|
13
|
-
const CHECKS = {
|
|
14
|
-
T: value => typeof value === 'string',
|
|
15
|
-
L: Number.isInteger,
|
|
16
|
-
R: value => typeof value === 'number' && Number.isFinite(value),
|
|
17
|
-
N: isDecimal,
|
|
18
|
-
B: value => typeof value === 'boolean',
|
|
19
|
-
D: isDate,
|
|
20
|
-
DHZ: isDate,
|
|
21
|
-
H: isDate,
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
const checkCode = code => {
|
|
25
|
-
if (!Object.hasOwn(CHECKS, code)) throw new TypeError(`unknown TYTX code '${code}'`);
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
/** A parameter name is an identifier; integer-like keys would lose their declaration order in an object. */
|
|
29
|
-
const NAME = /^[A-Za-z_$][\w$]*$/;
|
|
30
|
-
const EXTRA_PATH = '_extraPath';
|
|
31
|
-
|
|
32
|
-
const checkName = name => {
|
|
33
|
-
if (!NAME.test(name) || name === EXTRA_PATH) throw new TypeError(`parameter '${name}': not an admissible name`);
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
/** The named parameters of a routed function: TYTX code, required or default, in declaration order. */
|
|
37
|
-
export class Signature {
|
|
38
|
-
constructor(specs = {}) {
|
|
39
|
-
const parameters = [];
|
|
40
|
-
for (const [name, spec] of Object.entries(specs)) {
|
|
41
|
-
checkName(name);
|
|
42
|
-
if (typeof spec === 'string') {
|
|
43
|
-
checkCode(spec);
|
|
44
|
-
parameters.push({name, type: spec, required: true, default: undefined});
|
|
45
|
-
} else if (Array.isArray(spec) && spec.length === 2 && typeof spec[0] === 'string') {
|
|
46
|
-
checkCode(spec[0]);
|
|
47
|
-
if (spec[1] !== null && !CHECKS[spec[0]](spec[1])) {
|
|
48
|
-
throw new TypeError(`parameter '${name}': the default is not a valid '${spec[0]}' value`);
|
|
49
|
-
}
|
|
50
|
-
parameters.push({name, type: spec[0], required: false, default: spec[1]});
|
|
51
|
-
} else {
|
|
52
|
-
throw new TypeError(`parameter '${name}': a spec is a TYTX code or [code, default]`);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
this[PARAMETERS] = parameters;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/** The declared specs in order as `{name, type, required, default}`. */
|
|
59
|
-
get parameters() {
|
|
60
|
-
return this[PARAMETERS].map(parameter => ({...parameter}));
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/** A new object of the named values `kw` checked against the declaration, defaults applied; `_extraPath` passes through. */
|
|
64
|
-
bind(kw, {extraPath = kw[EXTRA_PATH]} = {}) {
|
|
65
|
-
const parameters = this.parameters;
|
|
66
|
-
const unknown = Object.keys(kw).filter(key => key !== EXTRA_PATH && !parameters.some(parameter => parameter.name === key));
|
|
67
|
-
if (unknown.length) throw new TypeError(`unknown parameter(s): ${unknown.join(', ')}`);
|
|
68
|
-
const bound = {};
|
|
69
|
-
for (const {name, type, required, default: fallback} of parameters) {
|
|
70
|
-
if (!Object.hasOwn(kw, name)) {
|
|
71
|
-
if (required) throw new TypeError(`missing required parameter '${name}'`);
|
|
72
|
-
bound[name] = fallback;
|
|
73
|
-
continue;
|
|
74
|
-
}
|
|
75
|
-
const value = kw[name];
|
|
76
|
-
const accepted = value === null ? !required && fallback === null : CHECKS[type](value);
|
|
77
|
-
if (!accepted) throw new TypeError(`parameter '${name}' is not a valid '${type}' value`);
|
|
78
|
-
bound[name] = value;
|
|
79
|
-
}
|
|
80
|
-
if (typeof extraPath === 'string' && extraPath !== '') bound[EXTRA_PATH] = extraPath;
|
|
81
|
-
return bound;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/** What a routed function returns, documented only: a TYTX code or an object field spec, a media type, a docline. */
|
|
86
|
-
export class ReturnValue {
|
|
87
|
-
constructor({type, mediaType, docline} = {}) {
|
|
88
|
-
if (typeof type === 'string') checkCode(type);
|
|
89
|
-
else if (type !== undefined) {
|
|
90
|
-
if (type === null || typeof type !== 'object' || Array.isArray(type)) {
|
|
91
|
-
throw new TypeError('ReturnValue type is a TYTX code or an object of field specs');
|
|
92
|
-
}
|
|
93
|
-
new Signature(type);
|
|
94
|
-
}
|
|
95
|
-
this.type = type;
|
|
96
|
-
this.mediaType = mediaType;
|
|
97
|
-
this.docline = docline;
|
|
98
|
-
}
|
|
99
|
-
}
|