@occultist/occultist 0.0.1
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/LICENSE +21 -0
- package/README.md +144 -0
- package/dist/accept.d.ts +41 -0
- package/dist/accept.js +110 -0
- package/dist/accept.test.d.ts +1 -0
- package/dist/accept.test.js +44 -0
- package/dist/action.test.d.ts +1 -0
- package/dist/action.test.js +1 -0
- package/dist/actions/actionSets.d.ts +23 -0
- package/dist/actions/actionSets.js +49 -0
- package/dist/actions/actions.d.ts +163 -0
- package/dist/actions/actions.js +436 -0
- package/dist/actions/context.d.ts +78 -0
- package/dist/actions/context.js +112 -0
- package/dist/actions/meta.d.ts +49 -0
- package/dist/actions/meta.js +177 -0
- package/dist/actions/path.d.ts +21 -0
- package/dist/actions/path.js +83 -0
- package/dist/actions/path.test.d.ts +1 -0
- package/dist/actions/path.test.js +9 -0
- package/dist/actions/spec.d.ts +214 -0
- package/dist/actions/spec.js +1 -0
- package/dist/actions/types.d.ts +112 -0
- package/dist/actions/types.js +2 -0
- package/dist/actions/writer.d.ts +27 -0
- package/dist/actions/writer.js +140 -0
- package/dist/actions/writer.test.d.ts +1 -0
- package/dist/actions/writer.test.js +42 -0
- package/dist/auth/types.d.ts +14 -0
- package/dist/auth/types.js +1 -0
- package/dist/cache/cache.d.ts +30 -0
- package/dist/cache/cache.js +220 -0
- package/dist/cache/etag.d.ts +17 -0
- package/dist/cache/etag.js +83 -0
- package/dist/cache/etag.test.d.ts +1 -0
- package/dist/cache/etag.test.js +91 -0
- package/dist/cache/memory.d.ts +12 -0
- package/dist/cache/memory.js +36 -0
- package/dist/cache/types.d.ts +175 -0
- package/dist/cache/types.js +4 -0
- package/dist/errors.d.ts +11 -0
- package/dist/errors.js +54 -0
- package/dist/jsonld.d.ts +43 -0
- package/dist/jsonld.js +1 -0
- package/dist/makeTypeDefs.d.ts +27 -0
- package/dist/makeTypeDefs.js +70 -0
- package/dist/merge.d.ts +61 -0
- package/dist/merge.js +1 -0
- package/dist/mod.d.ts +14 -0
- package/dist/mod.js +14 -0
- package/dist/processAction.d.ts +15 -0
- package/dist/processAction.js +512 -0
- package/dist/registry.d.ts +88 -0
- package/dist/registry.js +314 -0
- package/dist/registry.test.d.ts +1 -0
- package/dist/registry.test.js +133 -0
- package/dist/request.d.ts +29 -0
- package/dist/request.js +118 -0
- package/dist/scopes.d.ts +35 -0
- package/dist/scopes.js +121 -0
- package/dist/scopes.test.d.ts +1 -0
- package/dist/scopes.test.js +55 -0
- package/dist/transformers/fileTransformer.d.ts +1 -0
- package/dist/transformers/fileTransformer.js +8 -0
- package/dist/types.d.ts +12 -0
- package/dist/types.js +1 -0
- package/dist/utils/alwaysArray.d.ts +1 -0
- package/dist/utils/alwaysArray.js +9 -0
- package/dist/utils/contextBuilder.d.ts +9 -0
- package/dist/utils/contextBuilder.js +82 -0
- package/dist/utils/getActionContext.d.ts +7 -0
- package/dist/utils/getActionContext.js +48 -0
- package/dist/utils/getInternalName.d.ts +6 -0
- package/dist/utils/getInternalName.js +7 -0
- package/dist/utils/getParamLocation.d.ts +2 -0
- package/dist/utils/getParamLocation.js +6 -0
- package/dist/utils/getPropertyValueSpecifications.d.ts +2 -0
- package/dist/utils/getPropertyValueSpecifications.js +49 -0
- package/dist/utils/getRequestBodyValues.d.ts +11 -0
- package/dist/utils/getRequestBodyValues.js +122 -0
- package/dist/utils/getRequestIRIValues.d.ts +14 -0
- package/dist/utils/getRequestIRIValues.js +133 -0
- package/dist/utils/isBodyInit.d.ts +1 -0
- package/dist/utils/isBodyInit.js +21 -0
- package/dist/utils/isNil.d.ts +1 -0
- package/dist/utils/isNil.js +4 -0
- package/dist/utils/isObject.d.ts +6 -0
- package/dist/utils/isObject.js +6 -0
- package/dist/utils/isPopulatedObject.d.ts +5 -0
- package/dist/utils/isPopulatedObject.js +8 -0
- package/dist/utils/isPopulatedString.d.ts +1 -0
- package/dist/utils/isPopulatedString.js +4 -0
- package/dist/utils/joinPaths.d.ts +1 -0
- package/dist/utils/joinPaths.js +31 -0
- package/dist/utils/makeAppendProblemDetails.d.ts +14 -0
- package/dist/utils/makeAppendProblemDetails.js +26 -0
- package/dist/utils/makeURLPattern.d.ts +5 -0
- package/dist/utils/makeURLPattern.js +12 -0
- package/dist/utils/normalizeURL.d.ts +4 -0
- package/dist/utils/normalizeURL.js +11 -0
- package/dist/utils/parseSearchParams.d.ts +3 -0
- package/dist/utils/parseSearchParams.js +24 -0
- package/dist/utils/preferredMediaTypes.d.ts +42 -0
- package/dist/utils/preferredMediaTypes.js +149 -0
- package/dist/utils/urlToIRI.d.ts +1 -0
- package/dist/utils/urlToIRI.js +8 -0
- package/dist/utils/validateSpecValue.d.ts +1 -0
- package/dist/utils/validateSpecValue.js +1 -0
- package/dist/validators.d.ts +16 -0
- package/dist/validators.js +134 -0
- package/lib/accept.test.ts +55 -0
- package/lib/accept.ts +147 -0
- package/lib/action.test.ts +2 -0
- package/lib/actions/actionSets.ts +88 -0
- package/lib/actions/actions.ts +795 -0
- package/lib/actions/context.ts +170 -0
- package/lib/actions/meta.ts +251 -0
- package/lib/actions/path.test.ts +15 -0
- package/lib/actions/path.ts +99 -0
- package/lib/actions/spec.ts +545 -0
- package/lib/actions/types.ts +146 -0
- package/lib/actions/writer.test.ts +57 -0
- package/lib/actions/writer.ts +176 -0
- package/lib/auth/types.ts +22 -0
- package/lib/cache/cache.ts +291 -0
- package/lib/cache/etag.test.ts +122 -0
- package/lib/cache/etag.ts +106 -0
- package/lib/cache/memory.ts +52 -0
- package/lib/cache/types.ts +240 -0
- package/lib/errors.ts +66 -0
- package/lib/jsonld.ts +67 -0
- package/lib/makeTypeDefs.ts +138 -0
- package/lib/merge.ts +86 -0
- package/lib/mod.ts +14 -0
- package/lib/processAction.ts +690 -0
- package/lib/registry.test.ts +174 -0
- package/lib/registry.ts +455 -0
- package/lib/request.ts +153 -0
- package/lib/scopes.test.ts +70 -0
- package/lib/scopes.ts +178 -0
- package/lib/transformers/fileTransformer.ts +10 -0
- package/lib/types.ts +13 -0
- package/lib/utils/alwaysArray.ts +10 -0
- package/lib/utils/contextBuilder.ts +111 -0
- package/lib/utils/getActionContext.ts +76 -0
- package/lib/utils/getInternalName.ts +15 -0
- package/lib/utils/getParamLocation.ts +14 -0
- package/lib/utils/getPropertyValueSpecifications.ts +76 -0
- package/lib/utils/getRequestBodyValues.ts +155 -0
- package/lib/utils/getRequestIRIValues.ts +201 -0
- package/lib/utils/isBodyInit.ts +22 -0
- package/lib/utils/isNil.ts +4 -0
- package/lib/utils/isObject.ts +8 -0
- package/lib/utils/isPopulatedObject.ts +9 -0
- package/lib/utils/isPopulatedString.ts +4 -0
- package/lib/utils/joinPaths.ts +36 -0
- package/lib/utils/makeAppendProblemDetails.ts +57 -0
- package/lib/utils/makeURLPattern.ts +18 -0
- package/lib/utils/normalizeURL.ts +15 -0
- package/lib/utils/parseSearchParams.ts +36 -0
- package/lib/utils/preferredMediaTypes.ts +220 -0
- package/lib/utils/urlToIRI.ts +11 -0
- package/lib/utils/validateSpecValue.ts +0 -0
- package/lib/validators.ts +186 -0
- package/package.json +41 -0
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
import { getActionContext } from "../utils/getActionContext.js";
|
|
2
|
+
import { getPropertyValueSpecifications } from "../utils/getPropertyValueSpecifications.js";
|
|
3
|
+
import { isPopulatedObject } from '../utils/isPopulatedObject.js';
|
|
4
|
+
import { joinPaths } from "../utils/joinPaths.js";
|
|
5
|
+
import { AfterDefinition, BeforeDefinition } from "./meta.js";
|
|
6
|
+
function isHandlerObj(handler) {
|
|
7
|
+
return isPopulatedObject(handler);
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* A handler definition which can be pulled from a registry, scope or action
|
|
11
|
+
* after an action is defined.
|
|
12
|
+
*/
|
|
13
|
+
export class HandlerDefinition {
|
|
14
|
+
name;
|
|
15
|
+
contentType;
|
|
16
|
+
handler;
|
|
17
|
+
meta;
|
|
18
|
+
action;
|
|
19
|
+
cache;
|
|
20
|
+
constructor(name, contentType, handler, meta, action, actionMeta) {
|
|
21
|
+
this.name = name;
|
|
22
|
+
this.contentType = contentType;
|
|
23
|
+
this.handler = handler;
|
|
24
|
+
this.action = action;
|
|
25
|
+
this.meta = Object.freeze({ ...meta ?? {} });
|
|
26
|
+
const cache = [];
|
|
27
|
+
for (let i = 0; i < actionMeta.cache.length; i++) {
|
|
28
|
+
cache.push(Object.freeze({ ...actionMeta.cache[i] }));
|
|
29
|
+
}
|
|
30
|
+
this.cache = Object.freeze(cache);
|
|
31
|
+
Object.freeze(this);
|
|
32
|
+
}
|
|
33
|
+
get [Symbol.toStringTag]() {
|
|
34
|
+
return `name=${this.name} contentType=${this.contentType}`;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export class FinalizedAction {
|
|
38
|
+
#spec;
|
|
39
|
+
#meta;
|
|
40
|
+
#typeDef;
|
|
41
|
+
#handlers;
|
|
42
|
+
constructor(typeDef, spec, meta, handlerArgs) {
|
|
43
|
+
this.#typeDef = typeDef;
|
|
44
|
+
this.#spec = spec ?? {};
|
|
45
|
+
this.#meta = meta;
|
|
46
|
+
this.#meta.action = this;
|
|
47
|
+
const handlers = new Map();
|
|
48
|
+
if (typeof handlerArgs.contentType === 'string') {
|
|
49
|
+
handlers.set(handlerArgs.contentType, new HandlerDefinition(this.name, handlerArgs.contentType, handlerArgs.handler, handlerArgs.meta, this, this.#meta));
|
|
50
|
+
}
|
|
51
|
+
else if (isPopulatedObject(handlerArgs)) {
|
|
52
|
+
for (let i = 0; i < handlerArgs.contentType.length; i++) {
|
|
53
|
+
handlers.set(handlerArgs.contentType[i], new HandlerDefinition(this.name, handlerArgs.contentType[i], handlerArgs.handler, handlerArgs.meta, this, this.#meta));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
this.#handlers = handlers;
|
|
57
|
+
}
|
|
58
|
+
static fromHandlers(typeDef, spec, meta, arg3, arg4) {
|
|
59
|
+
if (Array.isArray(arg3) || typeof arg3 === 'string') {
|
|
60
|
+
return new FinalizedAction(typeDef, spec, meta, {
|
|
61
|
+
contentType: arg3,
|
|
62
|
+
handler: arg4,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return new FinalizedAction(typeDef, spec, meta, arg3);
|
|
66
|
+
}
|
|
67
|
+
static async toJSONLD(action, scope) {
|
|
68
|
+
if (scope == null || action.typeDef == null) {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
const apiSpec = await getPropertyValueSpecifications(action.spec);
|
|
72
|
+
return {
|
|
73
|
+
'@context': action.context,
|
|
74
|
+
'@id': joinPaths(action.registry.rootIRI, scope.path, action.name),
|
|
75
|
+
'@type': action.term,
|
|
76
|
+
target: {
|
|
77
|
+
'@type': 'https://schema.org/EntryPoint',
|
|
78
|
+
httpMethod: action.method,
|
|
79
|
+
urlTemplate: action.template,
|
|
80
|
+
contentType: 'application/ld+json',
|
|
81
|
+
},
|
|
82
|
+
...apiSpec,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
get public() {
|
|
86
|
+
return this.#meta.public;
|
|
87
|
+
}
|
|
88
|
+
get method() {
|
|
89
|
+
return this.#meta.method;
|
|
90
|
+
}
|
|
91
|
+
get term() {
|
|
92
|
+
return this.#typeDef?.term;
|
|
93
|
+
}
|
|
94
|
+
get type() {
|
|
95
|
+
return this.#typeDef?.type;
|
|
96
|
+
}
|
|
97
|
+
get typeDef() {
|
|
98
|
+
return this.#typeDef;
|
|
99
|
+
}
|
|
100
|
+
get name() {
|
|
101
|
+
return this.#meta.name;
|
|
102
|
+
}
|
|
103
|
+
get template() {
|
|
104
|
+
return this.#meta.uriTemplate;
|
|
105
|
+
}
|
|
106
|
+
get pattern() {
|
|
107
|
+
return this.#meta.path.pattern;
|
|
108
|
+
}
|
|
109
|
+
get spec() {
|
|
110
|
+
return this.#spec;
|
|
111
|
+
}
|
|
112
|
+
get scope() {
|
|
113
|
+
return this.#meta.scope;
|
|
114
|
+
}
|
|
115
|
+
get registry() {
|
|
116
|
+
return this.#meta.registry;
|
|
117
|
+
}
|
|
118
|
+
get handlers() {
|
|
119
|
+
return Array.from(this.#handlers.values());
|
|
120
|
+
}
|
|
121
|
+
get contentTypes() {
|
|
122
|
+
return Array.from(this.#handlers.keys());
|
|
123
|
+
}
|
|
124
|
+
get context() {
|
|
125
|
+
return getActionContext({
|
|
126
|
+
spec: this.#spec,
|
|
127
|
+
//vocab: this.#vocab,
|
|
128
|
+
//aliases: this.#aliases,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
url() {
|
|
132
|
+
return joinPaths(this.#meta.registry.rootIRI, this.#meta.path.normalized);
|
|
133
|
+
}
|
|
134
|
+
jsonld() {
|
|
135
|
+
const scope = this.#meta.scope;
|
|
136
|
+
return FinalizedAction.toJSONLD(this, scope);
|
|
137
|
+
}
|
|
138
|
+
jsonldPartial() {
|
|
139
|
+
const scope = this.#meta.scope;
|
|
140
|
+
const typeDef = this.#typeDef;
|
|
141
|
+
if (scope == null || typeDef == null) {
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
'@type': typeDef.type,
|
|
146
|
+
'@id': joinPaths(scope.url(), this.#meta.name),
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
handle(arg1, arg2) {
|
|
150
|
+
let contentType;
|
|
151
|
+
let handler;
|
|
152
|
+
let meta;
|
|
153
|
+
if (isHandlerObj(arg1)) {
|
|
154
|
+
contentType = arg1.contentType;
|
|
155
|
+
handler = arg1.handler;
|
|
156
|
+
meta = Object.assign(Object.create(null), arg1.meta);
|
|
157
|
+
if (arg1.meta != null) {
|
|
158
|
+
for (const sym of Object.getOwnPropertySymbols(arg1.meta)) {
|
|
159
|
+
meta[sym] = arg1.meta[sym];
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
contentType = arg1;
|
|
165
|
+
handler = arg2;
|
|
166
|
+
meta = Object.create(null);
|
|
167
|
+
}
|
|
168
|
+
if (!Array.isArray(contentType)) {
|
|
169
|
+
this.#handlers.set(contentType, new HandlerDefinition(this.#meta.name, contentType, handler, meta, this, this.#meta));
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
for (let i = 0; i < contentType.length; i++) {
|
|
173
|
+
this.#handlers.set(contentType[i], new HandlerDefinition(this.#meta.name, contentType[i], handler, meta, this, this.#meta));
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return this;
|
|
177
|
+
}
|
|
178
|
+
async handleRequest(args) {
|
|
179
|
+
const handler = this.#handlers.get(args.contentType);
|
|
180
|
+
return this.#meta.handleRequest({
|
|
181
|
+
...args,
|
|
182
|
+
spec: this.#spec,
|
|
183
|
+
handler,
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
export class DefinedAction {
|
|
188
|
+
#spec;
|
|
189
|
+
#meta;
|
|
190
|
+
#typeDef;
|
|
191
|
+
constructor(typeDef, spec, meta) {
|
|
192
|
+
this.#spec = spec ?? {};
|
|
193
|
+
this.#meta = meta;
|
|
194
|
+
this.#typeDef = typeDef;
|
|
195
|
+
this.#meta.action = this;
|
|
196
|
+
}
|
|
197
|
+
get public() {
|
|
198
|
+
return this.#meta.public;
|
|
199
|
+
}
|
|
200
|
+
get method() {
|
|
201
|
+
return this.#meta.method;
|
|
202
|
+
}
|
|
203
|
+
get term() {
|
|
204
|
+
return this.#typeDef?.term;
|
|
205
|
+
}
|
|
206
|
+
get type() {
|
|
207
|
+
return this.#typeDef?.type;
|
|
208
|
+
}
|
|
209
|
+
get typeDef() {
|
|
210
|
+
return this.#typeDef;
|
|
211
|
+
}
|
|
212
|
+
get name() {
|
|
213
|
+
return this.#meta.name;
|
|
214
|
+
}
|
|
215
|
+
get template() {
|
|
216
|
+
return this.#meta.uriTemplate;
|
|
217
|
+
}
|
|
218
|
+
get pattern() {
|
|
219
|
+
return this.#meta.path.pattern;
|
|
220
|
+
}
|
|
221
|
+
get path() {
|
|
222
|
+
return this.#meta.path.normalized;
|
|
223
|
+
}
|
|
224
|
+
get spec() {
|
|
225
|
+
return this.#spec;
|
|
226
|
+
}
|
|
227
|
+
get scope() {
|
|
228
|
+
return this.#meta.scope;
|
|
229
|
+
}
|
|
230
|
+
get registry() {
|
|
231
|
+
return this.#meta.registry;
|
|
232
|
+
}
|
|
233
|
+
get handlers() {
|
|
234
|
+
return [];
|
|
235
|
+
}
|
|
236
|
+
get contentTypes() {
|
|
237
|
+
return [];
|
|
238
|
+
}
|
|
239
|
+
url() {
|
|
240
|
+
return '';
|
|
241
|
+
}
|
|
242
|
+
get context() {
|
|
243
|
+
return getActionContext({
|
|
244
|
+
spec: this.#spec,
|
|
245
|
+
// vocab: this.#vocab,
|
|
246
|
+
// aliases: this.#aliases,
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
jsonld() {
|
|
250
|
+
const scope = this.#meta.scope;
|
|
251
|
+
return FinalizedAction.toJSONLD(this, scope);
|
|
252
|
+
}
|
|
253
|
+
jsonldPartial() {
|
|
254
|
+
const scope = this.#meta.scope;
|
|
255
|
+
const typeDef = this.#typeDef;
|
|
256
|
+
if (scope == null || typeDef == null) {
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
return {
|
|
260
|
+
'@type': typeDef.type,
|
|
261
|
+
'@id': joinPaths(scope.url(), this.#meta.name),
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Defines a cache handling rule for this action.
|
|
266
|
+
*
|
|
267
|
+
* Defining caching rules after the `action.define()` method is safer
|
|
268
|
+
* if validating and transforming the action payload might cause
|
|
269
|
+
* auth sensitive checks to be run which might reject the request.
|
|
270
|
+
*/
|
|
271
|
+
cache(args) {
|
|
272
|
+
if (this.#meta.cache.length !== 0 &&
|
|
273
|
+
this.#meta.cacheOccurance === BeforeDefinition) {
|
|
274
|
+
throw new Error('Action cache may be defined either before or after ' +
|
|
275
|
+
'the definition method is called, but not both.');
|
|
276
|
+
}
|
|
277
|
+
else if (this.#meta.cacheOccurance === BeforeDefinition) {
|
|
278
|
+
this.#meta.cacheOccurance = AfterDefinition;
|
|
279
|
+
}
|
|
280
|
+
this.#meta.cache.push(args);
|
|
281
|
+
return this;
|
|
282
|
+
}
|
|
283
|
+
meta() {
|
|
284
|
+
return this;
|
|
285
|
+
}
|
|
286
|
+
use() {
|
|
287
|
+
return this;
|
|
288
|
+
}
|
|
289
|
+
handle(arg1, arg2) {
|
|
290
|
+
return FinalizedAction.fromHandlers(this.#typeDef, this.#spec, this.#meta, arg1, arg2);
|
|
291
|
+
}
|
|
292
|
+
async handleRequest(args) {
|
|
293
|
+
return this.#meta.handleRequest({
|
|
294
|
+
...args,
|
|
295
|
+
spec: this.#spec,
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
export class Action {
|
|
300
|
+
#spec = {};
|
|
301
|
+
#meta;
|
|
302
|
+
constructor(meta) {
|
|
303
|
+
this.#meta = meta;
|
|
304
|
+
this.#meta.action = this;
|
|
305
|
+
}
|
|
306
|
+
get public() {
|
|
307
|
+
return this.#meta.public;
|
|
308
|
+
}
|
|
309
|
+
get method() {
|
|
310
|
+
return this.#meta.method;
|
|
311
|
+
}
|
|
312
|
+
get term() {
|
|
313
|
+
return undefined;
|
|
314
|
+
}
|
|
315
|
+
get type() {
|
|
316
|
+
return undefined;
|
|
317
|
+
}
|
|
318
|
+
get typeDef() {
|
|
319
|
+
return undefined;
|
|
320
|
+
}
|
|
321
|
+
get name() {
|
|
322
|
+
return this.#meta.name;
|
|
323
|
+
}
|
|
324
|
+
get template() {
|
|
325
|
+
return this.#meta.uriTemplate;
|
|
326
|
+
}
|
|
327
|
+
get pattern() {
|
|
328
|
+
return this.#meta.path.pattern;
|
|
329
|
+
}
|
|
330
|
+
get path() {
|
|
331
|
+
return this.#meta.path.normalized;
|
|
332
|
+
}
|
|
333
|
+
get spec() {
|
|
334
|
+
return this.#spec;
|
|
335
|
+
}
|
|
336
|
+
get scope() {
|
|
337
|
+
return this.#meta.scope;
|
|
338
|
+
}
|
|
339
|
+
get registry() {
|
|
340
|
+
return this.#meta.registry;
|
|
341
|
+
}
|
|
342
|
+
get handlers() {
|
|
343
|
+
return [];
|
|
344
|
+
}
|
|
345
|
+
get contentTypes() {
|
|
346
|
+
return [];
|
|
347
|
+
}
|
|
348
|
+
get context() {
|
|
349
|
+
return getActionContext({
|
|
350
|
+
spec: this.#spec,
|
|
351
|
+
//vocab: this.#vocab,
|
|
352
|
+
//aliases: this.#aliases,
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
url() {
|
|
356
|
+
return '';
|
|
357
|
+
}
|
|
358
|
+
jsonld() {
|
|
359
|
+
return Promise.resolve(null);
|
|
360
|
+
}
|
|
361
|
+
jsonldPartial() {
|
|
362
|
+
return null;
|
|
363
|
+
}
|
|
364
|
+
use() {
|
|
365
|
+
return this;
|
|
366
|
+
}
|
|
367
|
+
define(args) {
|
|
368
|
+
return new DefinedAction(args.typeDef, args.spec ?? {}, this.#meta);
|
|
369
|
+
}
|
|
370
|
+
handle(arg1, arg2) {
|
|
371
|
+
return FinalizedAction.fromHandlers(null, this.#spec, this.#meta, arg1, arg2);
|
|
372
|
+
}
|
|
373
|
+
async handleRequest(args) {
|
|
374
|
+
return this.#meta.handleRequest({
|
|
375
|
+
...args,
|
|
376
|
+
spec: this.#spec,
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
export class PreAction {
|
|
381
|
+
#meta;
|
|
382
|
+
constructor(meta) {
|
|
383
|
+
this.#meta = meta;
|
|
384
|
+
}
|
|
385
|
+
use() {
|
|
386
|
+
return new Action(this.#meta);
|
|
387
|
+
}
|
|
388
|
+
define(args) {
|
|
389
|
+
return new DefinedAction(args.typeDef, args.spec, this.#meta);
|
|
390
|
+
}
|
|
391
|
+
handle(arg1, arg2) {
|
|
392
|
+
return FinalizedAction.fromHandlers(null, {}, this.#meta, arg1, arg2);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
export class Endpoint {
|
|
396
|
+
#meta;
|
|
397
|
+
constructor(meta) {
|
|
398
|
+
this.#meta = meta;
|
|
399
|
+
}
|
|
400
|
+
hint(hints) {
|
|
401
|
+
this.#meta.hints.push(hints);
|
|
402
|
+
return this;
|
|
403
|
+
}
|
|
404
|
+
compress() {
|
|
405
|
+
return this;
|
|
406
|
+
}
|
|
407
|
+
cache(args) {
|
|
408
|
+
this.#meta.cache.push(args);
|
|
409
|
+
return this;
|
|
410
|
+
}
|
|
411
|
+
etag() {
|
|
412
|
+
return this;
|
|
413
|
+
}
|
|
414
|
+
use() {
|
|
415
|
+
return new Action(this.#meta);
|
|
416
|
+
}
|
|
417
|
+
define(args) {
|
|
418
|
+
return new DefinedAction(args.typeDef, args.spec, this.#meta);
|
|
419
|
+
}
|
|
420
|
+
handle(arg1, arg2) {
|
|
421
|
+
return FinalizedAction.fromHandlers(undefined, {}, this.#meta, arg1, arg2);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
export class ActionAuth {
|
|
425
|
+
#meta;
|
|
426
|
+
constructor(meta) {
|
|
427
|
+
this.#meta = meta;
|
|
428
|
+
}
|
|
429
|
+
public() {
|
|
430
|
+
this.#meta.public = true;
|
|
431
|
+
return new Endpoint(this.#meta);
|
|
432
|
+
}
|
|
433
|
+
private() {
|
|
434
|
+
return new Endpoint(this.#meta);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { HandlerDefinition } from "../mod.js";
|
|
2
|
+
import type { Registry } from "../registry.js";
|
|
3
|
+
import type { ActionPayload, ActionSpec, ContextState, ParsedIRIValues } from "./spec.js";
|
|
4
|
+
import type { ImplementedAction } from "./types.js";
|
|
5
|
+
import type { ResponseBody } from "./writer.js";
|
|
6
|
+
export type CacheContextArgs = {
|
|
7
|
+
req: Request;
|
|
8
|
+
url: string;
|
|
9
|
+
contentType: string;
|
|
10
|
+
public: boolean;
|
|
11
|
+
authKey?: string;
|
|
12
|
+
handler: HandlerDefinition;
|
|
13
|
+
params: ParsedIRIValues;
|
|
14
|
+
query: ParsedIRIValues;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Request context object.
|
|
18
|
+
*/
|
|
19
|
+
export declare class CacheContext {
|
|
20
|
+
#private;
|
|
21
|
+
req: Request;
|
|
22
|
+
method: string;
|
|
23
|
+
url: string;
|
|
24
|
+
contentType: string;
|
|
25
|
+
public: boolean;
|
|
26
|
+
authKey?: string;
|
|
27
|
+
action: ImplementedAction;
|
|
28
|
+
registry: Registry;
|
|
29
|
+
params: ParsedIRIValues;
|
|
30
|
+
query: ParsedIRIValues;
|
|
31
|
+
headers: Headers;
|
|
32
|
+
constructor(args: CacheContextArgs);
|
|
33
|
+
get hit(): boolean;
|
|
34
|
+
set hit(hit: boolean);
|
|
35
|
+
get status(): undefined | number;
|
|
36
|
+
set status(status: number);
|
|
37
|
+
get body(): undefined | ResponseBody;
|
|
38
|
+
set body(body: ResponseBody);
|
|
39
|
+
get etag(): undefined | string;
|
|
40
|
+
set etag(etag: string);
|
|
41
|
+
get [Symbol.toStringTag](): string;
|
|
42
|
+
}
|
|
43
|
+
export type ContextArgs<State extends ContextState = ContextState, Spec extends ActionSpec = ActionSpec> = {
|
|
44
|
+
req: Request;
|
|
45
|
+
url: string;
|
|
46
|
+
contentType: string;
|
|
47
|
+
public: boolean;
|
|
48
|
+
authKey?: string;
|
|
49
|
+
handler: HandlerDefinition<State, Spec>;
|
|
50
|
+
params: ParsedIRIValues;
|
|
51
|
+
query: ParsedIRIValues;
|
|
52
|
+
payload: ActionPayload<Spec>;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Request context object.
|
|
56
|
+
*/
|
|
57
|
+
export declare class Context<State extends ContextState = ContextState, Spec extends ActionSpec = ActionSpec> {
|
|
58
|
+
#private;
|
|
59
|
+
req: Request;
|
|
60
|
+
method: string;
|
|
61
|
+
url: string;
|
|
62
|
+
contentType: string;
|
|
63
|
+
public: boolean;
|
|
64
|
+
authKey?: string;
|
|
65
|
+
state: State;
|
|
66
|
+
action: ImplementedAction<State, Spec>;
|
|
67
|
+
registry: Registry;
|
|
68
|
+
params: ParsedIRIValues;
|
|
69
|
+
query: ParsedIRIValues;
|
|
70
|
+
payload: ActionPayload<Spec>;
|
|
71
|
+
headers: Headers;
|
|
72
|
+
constructor(args: ContextArgs<State, Spec>);
|
|
73
|
+
get status(): undefined | number;
|
|
74
|
+
set status(status: number);
|
|
75
|
+
get body(): undefined | ResponseBody;
|
|
76
|
+
set body(body: ResponseBody);
|
|
77
|
+
get [Symbol.toStringTag](): string;
|
|
78
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
class EditableContext {
|
|
2
|
+
hit = false;
|
|
3
|
+
etag;
|
|
4
|
+
status;
|
|
5
|
+
body;
|
|
6
|
+
}
|
|
7
|
+
;
|
|
8
|
+
/**
|
|
9
|
+
* Request context object.
|
|
10
|
+
*/
|
|
11
|
+
export class CacheContext {
|
|
12
|
+
#editable = new EditableContext();
|
|
13
|
+
req;
|
|
14
|
+
method;
|
|
15
|
+
url;
|
|
16
|
+
contentType;
|
|
17
|
+
public = false;
|
|
18
|
+
authKey;
|
|
19
|
+
action;
|
|
20
|
+
registry;
|
|
21
|
+
params;
|
|
22
|
+
query;
|
|
23
|
+
headers = new Headers();
|
|
24
|
+
constructor(args) {
|
|
25
|
+
this.req = args.req;
|
|
26
|
+
this.url = args.url;
|
|
27
|
+
this.contentType = args.contentType;
|
|
28
|
+
this.public = args.public;
|
|
29
|
+
this.authKey = args.authKey;
|
|
30
|
+
this.action = args.handler.action;
|
|
31
|
+
this.method = args.handler.action.method;
|
|
32
|
+
this.registry = args.handler.action.registry;
|
|
33
|
+
this.params = args.params;
|
|
34
|
+
this.query = args.query;
|
|
35
|
+
Object.freeze(this);
|
|
36
|
+
}
|
|
37
|
+
get hit() {
|
|
38
|
+
return this.#editable.hit;
|
|
39
|
+
}
|
|
40
|
+
set hit(hit) {
|
|
41
|
+
this.#editable.hit = hit;
|
|
42
|
+
}
|
|
43
|
+
get status() {
|
|
44
|
+
return this.#editable.status;
|
|
45
|
+
}
|
|
46
|
+
set status(status) {
|
|
47
|
+
this.#editable.status = status;
|
|
48
|
+
}
|
|
49
|
+
get body() {
|
|
50
|
+
return this.#editable.body;
|
|
51
|
+
}
|
|
52
|
+
set body(body) {
|
|
53
|
+
this.#editable.body = body;
|
|
54
|
+
}
|
|
55
|
+
get etag() {
|
|
56
|
+
return this.#editable.etag;
|
|
57
|
+
}
|
|
58
|
+
set etag(etag) {
|
|
59
|
+
this.#editable.etag = etag;
|
|
60
|
+
}
|
|
61
|
+
get [Symbol.toStringTag]() {
|
|
62
|
+
return `action=${this.action.name} method=${this.method} contentType=${this.contentType}`;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Request context object.
|
|
67
|
+
*/
|
|
68
|
+
export class Context {
|
|
69
|
+
#editable = new EditableContext();
|
|
70
|
+
req;
|
|
71
|
+
method;
|
|
72
|
+
url;
|
|
73
|
+
contentType;
|
|
74
|
+
public = false;
|
|
75
|
+
authKey;
|
|
76
|
+
state = {};
|
|
77
|
+
action;
|
|
78
|
+
registry;
|
|
79
|
+
params;
|
|
80
|
+
query;
|
|
81
|
+
payload;
|
|
82
|
+
headers = new Headers();
|
|
83
|
+
constructor(args) {
|
|
84
|
+
this.req = args.req;
|
|
85
|
+
this.url = args.url;
|
|
86
|
+
this.contentType = args.contentType;
|
|
87
|
+
this.public = args.public;
|
|
88
|
+
this.authKey = args.authKey;
|
|
89
|
+
this.action = args.handler.action;
|
|
90
|
+
this.method = args.handler.action.method;
|
|
91
|
+
this.registry = args.handler.action.registry;
|
|
92
|
+
this.params = args.params;
|
|
93
|
+
this.query = args.query;
|
|
94
|
+
this.payload = args.payload;
|
|
95
|
+
Object.freeze(this);
|
|
96
|
+
}
|
|
97
|
+
get status() {
|
|
98
|
+
return this.#editable.status;
|
|
99
|
+
}
|
|
100
|
+
set status(status) {
|
|
101
|
+
this.#editable.status = status;
|
|
102
|
+
}
|
|
103
|
+
get body() {
|
|
104
|
+
return this.#editable.body;
|
|
105
|
+
}
|
|
106
|
+
set body(body) {
|
|
107
|
+
this.#editable.body = body;
|
|
108
|
+
}
|
|
109
|
+
get [Symbol.toStringTag]() {
|
|
110
|
+
return `action=${this.action.name} method=${this.method} contentType=${this.contentType}`;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { CacheInstanceArgs } from '../cache/types.js';
|
|
2
|
+
import { JSONValue } from '../jsonld.js';
|
|
3
|
+
import type { Registry } from '../registry.js';
|
|
4
|
+
import type { Scope } from "../scopes.js";
|
|
5
|
+
import { HandlerDefinition } from './actions.js';
|
|
6
|
+
import { Path } from "./path.js";
|
|
7
|
+
import type { ActionSpec, ContextState, FileValue, TransformerFn } from './spec.js';
|
|
8
|
+
import type { HintArgs, ImplementedAction } from './types.js';
|
|
9
|
+
import type { HTTPWriter, ResponseTypes } from "./writer.js";
|
|
10
|
+
export declare const BeforeDefinition = 0;
|
|
11
|
+
export declare const AfterDefinition = 1;
|
|
12
|
+
export declare class ActionMeta<State extends ContextState = ContextState, Spec extends ActionSpec = ActionSpec> {
|
|
13
|
+
#private;
|
|
14
|
+
rootIRI: string;
|
|
15
|
+
method: string;
|
|
16
|
+
name: string;
|
|
17
|
+
uriTemplate: string;
|
|
18
|
+
public: boolean;
|
|
19
|
+
authKey?: string;
|
|
20
|
+
path: Path;
|
|
21
|
+
hints: HintArgs[];
|
|
22
|
+
transformers: Map<string, TransformerFn<JSONValue | FileValue, State, Spec>>;
|
|
23
|
+
scope?: Scope;
|
|
24
|
+
registry: Registry;
|
|
25
|
+
writer: HTTPWriter;
|
|
26
|
+
action?: ImplementedAction<State, Spec>;
|
|
27
|
+
acceptCache: Set<string>;
|
|
28
|
+
compressBeforeCache: boolean;
|
|
29
|
+
cacheOccurance: 0 | 1;
|
|
30
|
+
cache: CacheInstanceArgs[];
|
|
31
|
+
serverTiming: boolean;
|
|
32
|
+
constructor(rootIRI: string, method: string, name: string, uriTemplate: string, registry: Registry, writer: HTTPWriter, scope?: Scope);
|
|
33
|
+
/**
|
|
34
|
+
* Called when the API is defined to compute all uncomputed values.
|
|
35
|
+
*/
|
|
36
|
+
finalize(): void;
|
|
37
|
+
handleRequest({ startTime, contentType, language: _language, encoding: _encoding, url, req, writer, spec, handler, }: {
|
|
38
|
+
startTime: number;
|
|
39
|
+
contentType?: string;
|
|
40
|
+
language?: string;
|
|
41
|
+
encoding?: string;
|
|
42
|
+
url: string;
|
|
43
|
+
req: Request;
|
|
44
|
+
writer: HTTPWriter;
|
|
45
|
+
spec?: Spec;
|
|
46
|
+
handler?: HandlerDefinition<State, Spec>;
|
|
47
|
+
}): Promise<ResponseTypes>;
|
|
48
|
+
get [Symbol.toStringTag](): string;
|
|
49
|
+
}
|