codehooks-js 1.3.7 → 1.3.8
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/package.json +2 -4
- package/types/aggregation/index.d.mts +38 -1
- package/types/crudlify/index.d.mts +9 -5
- package/types/crudlify/lib/eventhooks.d.mts +1 -0
- package/types/crudlify/lib/query/q2m/index.d.mts +7 -0
- package/types/crudlify/lib/query/q2m/q2m.d.mts +1 -0
- package/types/crudlify/lib/schema/json-schema/index.d.mts +1 -0
- package/types/crudlify/lib/schema/yup/index.d.mts +2 -1
- package/types/crudlify/lib/schema/zod/index.d.mts +1 -0
- package/types/index.d.ts +1254 -126
- package/types/webserver.d.mts +2 -1
- package/types/workflow/engine.d.mts +5 -40
- package/types/workflow/index.d.mts +1 -5
- package/workflow/engine.mjs +14 -3
package/types/index.d.ts
CHANGED
|
@@ -1,138 +1,1266 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Built-in persistent datastore.
|
|
3
|
+
* - Datastore is capable of serving NoSQL object data and Key-Value data.
|
|
4
|
+
*/
|
|
5
|
+
export type Datastore = {
|
|
6
|
+
/**
|
|
7
|
+
* - Connect to Datastore
|
|
8
|
+
*/
|
|
9
|
+
open: () => Promise<DatastoreAPI>;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @type {Datastore}
|
|
14
|
+
*/
|
|
15
|
+
export const Datastore: Datastore;
|
|
16
|
+
/**
|
|
17
|
+
* @type {Datastore}
|
|
18
|
+
*/
|
|
19
|
+
export const datastore: Datastore;
|
|
3
20
|
export const aggregation: typeof agg;
|
|
4
21
|
export const crudlify: typeof crud;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Express style API
|
|
25
|
+
* @type {Codehooks}
|
|
26
|
+
*/
|
|
5
27
|
export const coho: Codehooks;
|
|
6
28
|
export const app: Codehooks;
|
|
7
|
-
export const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
function createChannel(path: any, ...hook: any[]): void;
|
|
13
|
-
function publishEvent(channel: any, data: any, query?: {}): Promise<any>;
|
|
14
|
-
function createListener(channel: any, data: any): Promise<any>;
|
|
15
|
-
function getListeners(channel: any): Promise<any>;
|
|
16
|
-
function getListener(channel: any, listenerID: any): Promise<any>;
|
|
17
|
-
function removeListener(channel: any, listenerID: any): Promise<any>;
|
|
18
|
-
}
|
|
29
|
+
export const coderunner: any;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Scheduled background worker functions
|
|
33
|
+
*/
|
|
19
34
|
export namespace schedule {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
35
|
+
function runAt(date: any, payload: any, worker: string): Promise<any>;
|
|
36
|
+
function run(payload: any, worker: string): Promise<any>;
|
|
37
|
+
function cron(
|
|
38
|
+
cronExpression: string,
|
|
39
|
+
...hook: ((
|
|
40
|
+
request: httpRequest,
|
|
41
|
+
response: httpResponse,
|
|
42
|
+
next: nextFunction
|
|
43
|
+
) => any)[]
|
|
44
|
+
): void;
|
|
23
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Real time publish subscribe events
|
|
48
|
+
*/
|
|
49
|
+
export namespace realtime {
|
|
50
|
+
type SSEListener = {
|
|
51
|
+
_id: string;
|
|
52
|
+
[key: string]: any;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Create a real time channel for clients for listen to and server to publish to
|
|
56
|
+
* @param path string - e.g. '/goals'
|
|
57
|
+
* @example
|
|
58
|
+
* realtime.createChannel('/goals')
|
|
59
|
+
*/
|
|
60
|
+
function createChannel(path: string, ...hook: any): void;
|
|
61
|
+
/**
|
|
62
|
+
*
|
|
63
|
+
* @param channel string - e.g. '/goals'
|
|
64
|
+
* @param data object - Event object to publish
|
|
65
|
+
* @param query object - optional matching criteria for listeners to get the event
|
|
66
|
+
* @example
|
|
67
|
+
* realtime.publishEvent('/goals', {player: "Rod", score: 2})
|
|
68
|
+
*/
|
|
69
|
+
function publishEvent(
|
|
70
|
+
channel: string,
|
|
71
|
+
data: object,
|
|
72
|
+
query?: object
|
|
73
|
+
): Promise<object>;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Create a listener to use to connect to an EventSource later
|
|
77
|
+
* @param path string - Channel
|
|
78
|
+
* @param data object - Listener topics of interest
|
|
79
|
+
* @returns Promise with SSEListener
|
|
80
|
+
*/
|
|
81
|
+
function createListener(path: string, data: object): Promise<SSEListener>;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Get a listener topics
|
|
85
|
+
* @param path string - Channel
|
|
86
|
+
* @param listenerID - A valid _id for a listener
|
|
87
|
+
*/
|
|
88
|
+
function getListener(path: string, listenerID: string): Promise<SSEListener>;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Get all listeners on channel
|
|
92
|
+
* @param path string - Channel
|
|
93
|
+
*/
|
|
94
|
+
function getListeners(path: string): Promise<SSEListener[]>;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Delete a listenerID
|
|
98
|
+
* @param path string - Channel
|
|
99
|
+
* @param listenerID - A valid listenerID
|
|
100
|
+
*/
|
|
101
|
+
function removeListener(
|
|
102
|
+
path: string,
|
|
103
|
+
listenerID: string
|
|
104
|
+
): Promise<SSEListener>;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Virtual filesystem
|
|
108
|
+
*/
|
|
24
109
|
export namespace filestore {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
110
|
+
/**
|
|
111
|
+
* Raad file content as stream
|
|
112
|
+
* @see {@link https://codehooks.io/docs/fileapi#filestoregetreadstreampath API docs}
|
|
113
|
+
* @param path Absolute path to file
|
|
114
|
+
* @param options Not in use
|
|
115
|
+
* @returns Promise EventEmitter with chunks on data
|
|
116
|
+
* @example
|
|
117
|
+
* app.get('/image', async (req: httpRequest, res: httpResponse) => {
|
|
118
|
+
* const bufStream = await filestore.getReadStream('/img/logo.png');
|
|
119
|
+
* res.set('content-type', 'image/png')
|
|
120
|
+
* bufStream.on('data', (buf) => {
|
|
121
|
+
* res.write(buf, 'buffer')
|
|
122
|
+
* })
|
|
123
|
+
* .on('end', () => {
|
|
124
|
+
* res.end()
|
|
125
|
+
* })
|
|
126
|
+
* })
|
|
127
|
+
*/
|
|
128
|
+
function getReadStream(path: string, options?: any): Promise<any>;
|
|
129
|
+
/**
|
|
130
|
+
* Read file content as string
|
|
131
|
+
* @see {@link https://codehooks.io/docs/fileapi#filestorereadfilepath API docs}
|
|
132
|
+
* @param path Absolute path to file
|
|
133
|
+
* @param options
|
|
134
|
+
* @returns Promise File content as string
|
|
135
|
+
*/
|
|
136
|
+
function readFile(path: string, options?: any): Promise<String>;
|
|
137
|
+
/**
|
|
138
|
+
* Save file binary buffer
|
|
139
|
+
* @see {@link https://codehooks.io/docs/fileapi#filestoresavefilepath-filestream API docs}
|
|
140
|
+
* @param path Abolute path to file, e.g. /static/home.html
|
|
141
|
+
* @param buffer Stream data
|
|
142
|
+
* @param options Not in use
|
|
143
|
+
* @returns Promise with save result
|
|
144
|
+
* @example
|
|
145
|
+
* import {PassThrough} from 'stream';
|
|
146
|
+
* app.post('/file', async (req, res) => {
|
|
147
|
+
* try {
|
|
148
|
+
* const stream = new PassThrough();
|
|
149
|
+
* req.pipe(stream);
|
|
150
|
+
* const result = await filestore.saveFile('/static.html', stream);
|
|
151
|
+
* res.end(result)
|
|
152
|
+
* } catch (error) {
|
|
153
|
+
* console.error(error)
|
|
154
|
+
* res.status(404).end('Bad upload')
|
|
155
|
+
* }
|
|
156
|
+
* })
|
|
157
|
+
*/
|
|
158
|
+
function saveFile(path: string, buffer: any, options?: any): Promise<any>;
|
|
159
|
+
/**
|
|
160
|
+
* Delete file
|
|
161
|
+
* @see {@link https://codehooks.io/docs/fileapi#filestoredeletefilepath API docs}
|
|
162
|
+
* @param path
|
|
163
|
+
* @param options
|
|
164
|
+
*/
|
|
165
|
+
function deleteFile(path: string, options?: any): Promise<any>;
|
|
166
|
+
/**
|
|
167
|
+
* List file directories and files
|
|
168
|
+
* @see {@link https://codehooks.io/docs/fileapi#filestorelistpath API docs}
|
|
169
|
+
* @param path
|
|
170
|
+
* @param options
|
|
171
|
+
*/
|
|
172
|
+
function list(path: string, options?: any): Promise<Array<Object>>;
|
|
173
|
+
/**
|
|
174
|
+
* Read binary buffer from file
|
|
175
|
+
* @param path Path to file
|
|
176
|
+
* @returns Promise with Buffer
|
|
177
|
+
* @example
|
|
178
|
+
* const myBuf = await filestore.getFileAsBuffer('/images/logo.png');
|
|
179
|
+
*/
|
|
180
|
+
function readFileAsBuffer(path: any): Promise<any>;
|
|
31
181
|
}
|
|
32
182
|
export default _coho;
|
|
183
|
+
/**
|
|
184
|
+
* keyvalOptions
|
|
185
|
+
*/
|
|
186
|
+
export type keyvalOptions = {
|
|
187
|
+
/**
|
|
188
|
+
* - Time to live in milliseconds
|
|
189
|
+
*/
|
|
190
|
+
ttl: number;
|
|
191
|
+
/**
|
|
192
|
+
* - Name of isolated keyspace
|
|
193
|
+
*/
|
|
194
|
+
keyspace: string;
|
|
195
|
+
};
|
|
196
|
+
/**
|
|
197
|
+
* NoSQL API
|
|
198
|
+
*/
|
|
199
|
+
export type NoSQLAPI = {
|
|
200
|
+
/**
|
|
201
|
+
* - Get object by ID, returns a Promise with the object
|
|
202
|
+
*/
|
|
203
|
+
getOne: (query: string | object) => Promise<object>;
|
|
204
|
+
/**
|
|
205
|
+
* - Get object by ID, returns a Promise with the object - alias for getOne
|
|
206
|
+
*/
|
|
207
|
+
findOne: (query: string | object) => Promise<object>;
|
|
208
|
+
/**
|
|
209
|
+
* - Get stream of objects by query
|
|
210
|
+
* https://codehooks.io/docs/nosql-database-api#getmanycollection-query-options
|
|
211
|
+
*/
|
|
212
|
+
getMany: (query: object, options?: object) => DataStream;
|
|
213
|
+
/**
|
|
214
|
+
* - Get stream of objects by query, alias for getMany
|
|
215
|
+
*/
|
|
216
|
+
find: (query: object, options?: object) => DataStream;
|
|
217
|
+
/**
|
|
218
|
+
* - Insert a new data object into a collection in the current Datastore
|
|
219
|
+
*/
|
|
220
|
+
insertOne: (document: object) => Promise<object>;
|
|
221
|
+
/**
|
|
222
|
+
* - Update one data object by ID in a datastore collection.
|
|
223
|
+
* - Input document is patched with the matched database object
|
|
224
|
+
* @param query string | object
|
|
225
|
+
* @param document object
|
|
226
|
+
* @param updateoperators object
|
|
227
|
+
* @param options object - {upsert: true}
|
|
228
|
+
* @returns Promise<object>
|
|
229
|
+
*/
|
|
230
|
+
updateOne: (
|
|
231
|
+
query: string | object,
|
|
232
|
+
document: object,
|
|
233
|
+
updateoperators?: object,
|
|
234
|
+
options?: object
|
|
235
|
+
) => Promise<object>;
|
|
236
|
+
/**
|
|
237
|
+
* - Update multiple data objects in a datastore collection.
|
|
238
|
+
* - Input document is patched with the matched database objects
|
|
239
|
+
*/
|
|
240
|
+
updateMany: (
|
|
241
|
+
query: object,
|
|
242
|
+
document: object,
|
|
243
|
+
options?: object
|
|
244
|
+
) => Promise<object>;
|
|
245
|
+
/**
|
|
246
|
+
* - Replace one data object by ID in a datastore collection.
|
|
247
|
+
* - Input document overwrites the matched database object, the _id value is not overwritten.
|
|
248
|
+
*/
|
|
249
|
+
replaceOne: (
|
|
250
|
+
query: string | object,
|
|
251
|
+
document: object,
|
|
252
|
+
options?: object
|
|
253
|
+
) => Promise<object>;
|
|
254
|
+
/**
|
|
255
|
+
* - Replace multiple data objects in a datastore collection.
|
|
256
|
+
* - Input document overwrites the matched database objects. The _id value is not overwritten
|
|
257
|
+
*/
|
|
258
|
+
replaceMany: (
|
|
259
|
+
query: object,
|
|
260
|
+
document: object,
|
|
261
|
+
options?: object
|
|
262
|
+
) => Promise<object>;
|
|
263
|
+
/**
|
|
264
|
+
* - Remove one data object by ID in a collection in the current Datastore
|
|
265
|
+
*/
|
|
266
|
+
removeOne: (query: string | object) => Promise<object>;
|
|
267
|
+
/**
|
|
268
|
+
* - Remove multiple data objects in a collection in the current Datastore
|
|
269
|
+
*/
|
|
270
|
+
removeMany: (query: object) => Promise<object>;
|
|
271
|
+
/**
|
|
272
|
+
* Validate database colletion data agains JSON-Schema
|
|
273
|
+
* @param schema JSON-schema
|
|
274
|
+
* @returns Promise with result
|
|
275
|
+
*/
|
|
276
|
+
createSchema: (schema: object) => Promise<object>;
|
|
277
|
+
/**
|
|
278
|
+
* Validate database colletion data agains JSON-Schema
|
|
279
|
+
* @param schema JSON-schema
|
|
280
|
+
* @returns Promise with result
|
|
281
|
+
*/
|
|
282
|
+
setSchema: (schema: object) => Promise<object>;
|
|
283
|
+
/**
|
|
284
|
+
* Remove JSON-schema for database collection
|
|
285
|
+
* @param collection string
|
|
286
|
+
* @returns Promise with result
|
|
287
|
+
*/
|
|
288
|
+
removeSchema: () => Promise<object>;
|
|
289
|
+
/**
|
|
290
|
+
* Get JSON-schema for database collection
|
|
291
|
+
* @returns Promise with result
|
|
292
|
+
*/
|
|
293
|
+
getSchema: () => Promise<object>;
|
|
294
|
+
/**
|
|
295
|
+
* Count database collection items
|
|
296
|
+
* @returns Promise result
|
|
297
|
+
*/
|
|
298
|
+
count: () => Promise<object>;
|
|
299
|
+
};
|
|
300
|
+
/**
|
|
301
|
+
* Database API
|
|
302
|
+
*/
|
|
303
|
+
export type DatastoreAPI = {
|
|
304
|
+
/**
|
|
305
|
+
* - Get database collection by string name, returns an NoSQL API object
|
|
306
|
+
*/
|
|
307
|
+
collection: (collection: string) => NoSQLAPI;
|
|
308
|
+
/**
|
|
309
|
+
* - Get object by ID, returns a Promise with the object
|
|
310
|
+
*/
|
|
311
|
+
getOne: (collection: string, query: string | object) => Promise<any>;
|
|
312
|
+
/**
|
|
313
|
+
* - Get stream of objects by query
|
|
314
|
+
* * https://codehooks.io/docs/nosql-database-api#getmanycollection-query-options
|
|
315
|
+
*/
|
|
316
|
+
getMany: (collection: string, query?: object, options?: object) => DataStream;
|
|
317
|
+
/**
|
|
318
|
+
* - Get object by ID, returns a Promise with the object (alias for getOne)
|
|
319
|
+
*/
|
|
320
|
+
findOne: (collection: string, query: string | object) => Promise<any>;
|
|
321
|
+
/**
|
|
322
|
+
* - Alias for getMany
|
|
323
|
+
* https://codehooks.io/docs/nosql-database-api#getmanycollection-query-options
|
|
324
|
+
*/
|
|
325
|
+
find: (collection: string, query?: object, options?: object) => DataStream;
|
|
326
|
+
/**
|
|
327
|
+
* - Insert a new data object into a collection in the current Datastore
|
|
328
|
+
*/
|
|
329
|
+
insertOne: (collection: string, document: object) => Promise<object>;
|
|
330
|
+
/**
|
|
331
|
+
* - Update one data object by ID in a datastore collection.
|
|
332
|
+
* - Input document is patched with the matched database object
|
|
333
|
+
* @param collection string
|
|
334
|
+
* @param query string | object
|
|
335
|
+
* @param updateoperators object
|
|
336
|
+
* @param options object - {upsert: true}
|
|
337
|
+
* @returns Promise<any>
|
|
338
|
+
*/
|
|
339
|
+
updateOne: (
|
|
340
|
+
collection: string,
|
|
341
|
+
query: string | object,
|
|
342
|
+
updateoperators?: object,
|
|
343
|
+
options?: object
|
|
344
|
+
) => Promise<any>;
|
|
345
|
+
/**
|
|
346
|
+
* - Update multiple data objects in a datastore collection.
|
|
347
|
+
* - Input document is patched with the matched database objects
|
|
348
|
+
*/
|
|
349
|
+
updateMany: (
|
|
350
|
+
collection: string,
|
|
351
|
+
query: object,
|
|
352
|
+
document: object,
|
|
353
|
+
updateoperators?: object
|
|
354
|
+
) => Promise<any>;
|
|
355
|
+
/**
|
|
356
|
+
* - Replace one data object by ID in a datastore collection.
|
|
357
|
+
* - Input document overwrites the matched database object, the _id value is not overwritten.
|
|
358
|
+
*/
|
|
359
|
+
replaceOne: (
|
|
360
|
+
collection: string,
|
|
361
|
+
query: string | object,
|
|
362
|
+
options?: object
|
|
363
|
+
) => Promise<any>;
|
|
364
|
+
/**
|
|
365
|
+
* - Replace multiple data objects in a datastore collection.
|
|
366
|
+
* - Input document overwrites the matched database objects. The _id value is not overwritten
|
|
367
|
+
*/
|
|
368
|
+
replaceMany: (
|
|
369
|
+
collection: string,
|
|
370
|
+
query: object,
|
|
371
|
+
options?: object
|
|
372
|
+
) => Promise<any>;
|
|
373
|
+
/**
|
|
374
|
+
* - Remove one data object by ID in a collection in the current Datastore
|
|
375
|
+
*/
|
|
376
|
+
removeOne: (collection: string, query: string | object) => Promise<any>;
|
|
377
|
+
/**
|
|
378
|
+
* - Remove multiple data objects in a collection in the current Datastore
|
|
379
|
+
*/
|
|
380
|
+
removeMany: (collection: string, query: object) => Promise<any>;
|
|
381
|
+
/**
|
|
382
|
+
* - Set a key-value pair in the current Datastore
|
|
383
|
+
*/
|
|
384
|
+
set: (key: string, value: string | object, options?: object) => Promise<any>;
|
|
385
|
+
/**
|
|
386
|
+
* - Get a key-value pair in the current Datastore
|
|
387
|
+
*/
|
|
388
|
+
get: (key: string, options?: object) => Promise<any>;
|
|
389
|
+
/**
|
|
390
|
+
* - Get all key-value pair that matches keypattern in the current Datastore
|
|
391
|
+
*/
|
|
392
|
+
getAll: (keypattern: string, options?: object) => DataStream;
|
|
393
|
+
/**
|
|
394
|
+
* - Increment a key-value pair in the current Datastore
|
|
395
|
+
*/
|
|
396
|
+
incr: (key: string, value: number, options?: object) => Promise<any>;
|
|
397
|
+
/**
|
|
398
|
+
* - Decrement a key-value pair in the current Datastore
|
|
399
|
+
*/
|
|
400
|
+
decr: (key: string, value: number, options?: object) => Promise<any>;
|
|
401
|
+
/**
|
|
402
|
+
* - Delete a key-value pair in the current Datastore
|
|
403
|
+
*/
|
|
404
|
+
del: (key: string, value: object) => Promise<any>;
|
|
405
|
+
/**
|
|
406
|
+
* - Delete a range of key-value pairs in the current Datastore
|
|
407
|
+
*/
|
|
408
|
+
delAll: (keypattern: string, options?: object) => Promise<any>;
|
|
409
|
+
/**
|
|
410
|
+
* - Add a queued job in a datastore.
|
|
411
|
+
* - Jobs in the queue are processed by your worker function.
|
|
412
|
+
*/
|
|
413
|
+
enqueue: (topic: string, document: object, options?: object) => Promise<any>;
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Queue each item from the result of a database query to a worker function
|
|
417
|
+
* @see {@link https://codehooks.io/docs/queueapi#enqueuefromquerycollection-query-topic-options Codehooks docs}
|
|
418
|
+
* @param collection Collection name
|
|
419
|
+
* @param query Database Query
|
|
420
|
+
* @param topic Worker function topic string
|
|
421
|
+
* @param options Query options
|
|
422
|
+
* @returns Promise
|
|
423
|
+
* @example
|
|
424
|
+
* async function someFunction() {
|
|
425
|
+
* const query = {"Market Category": "G"};
|
|
426
|
+
* const topic = 'stockWorker';
|
|
427
|
+
* const qres = await conn.enqueueFromQuery('stocks', query, topic);
|
|
428
|
+
* }
|
|
429
|
+
*
|
|
430
|
+
* app.worker('stockWorker', (req: httpRequest, res: httpResponse) => {
|
|
431
|
+
* const { body } = req;
|
|
432
|
+
* console.log('Working on', body.payload)
|
|
433
|
+
* // do stuff with body.payload
|
|
434
|
+
* res.end()
|
|
435
|
+
* })
|
|
436
|
+
*/
|
|
437
|
+
enqueueFromQuery: (
|
|
438
|
+
collection: string,
|
|
439
|
+
query: object,
|
|
440
|
+
topic: string,
|
|
441
|
+
options?: object
|
|
442
|
+
) => Promise<object>;
|
|
443
|
+
/**
|
|
444
|
+
* Validate database colletion data agains JSON-Schema
|
|
445
|
+
* @param collection string
|
|
446
|
+
* @param schema JSON-schema
|
|
447
|
+
* @returns Promise with result
|
|
448
|
+
*/
|
|
449
|
+
createSchema: (collection: string, schema: object) => Promise<object>;
|
|
450
|
+
/**
|
|
451
|
+
* Validate database colletion data agains JSON-Schema
|
|
452
|
+
* @param collection string
|
|
453
|
+
* @param schema JSON-schema
|
|
454
|
+
* @returns Promise with result
|
|
455
|
+
*/
|
|
456
|
+
setSchema: (collection: string, schema: object) => Promise<object>;
|
|
457
|
+
/**
|
|
458
|
+
* Remove JSON-schema for database collection
|
|
459
|
+
* @param collection string
|
|
460
|
+
* @returns Promise with result
|
|
461
|
+
*/
|
|
462
|
+
removeSchema: (collection: string) => Promise<object>;
|
|
463
|
+
/**
|
|
464
|
+
* Get JSON-schema for database collection
|
|
465
|
+
* @param collection string
|
|
466
|
+
* @returns Promise with result
|
|
467
|
+
*/
|
|
468
|
+
getSchema: (collection: string) => Promise<object>;
|
|
469
|
+
/**
|
|
470
|
+
* Count database collection items
|
|
471
|
+
* @returns Promise result
|
|
472
|
+
*/
|
|
473
|
+
count: (collection: string) => Promise<object>;
|
|
474
|
+
};
|
|
475
|
+
/**
|
|
476
|
+
* Persistent NoSql and Kev-Value datastore
|
|
477
|
+
*/
|
|
478
|
+
export type DataStream = {
|
|
479
|
+
/**
|
|
480
|
+
* - Emits data, stream.on('data', (data) => //console.debug(data))
|
|
481
|
+
*/
|
|
482
|
+
on: (event: string, callback: (data: any) => any) => void;
|
|
483
|
+
/**
|
|
484
|
+
* - Pipe datastream to JSON output
|
|
485
|
+
*/
|
|
486
|
+
json: (response: httpResponse) => void;
|
|
487
|
+
/**
|
|
488
|
+
* - Return an array of objects
|
|
489
|
+
*/
|
|
490
|
+
toArray: () => Promise<object[]>;
|
|
491
|
+
/**
|
|
492
|
+
* Return an iterator
|
|
493
|
+
* @param data
|
|
494
|
+
* @returns Promise with iterator
|
|
495
|
+
*/
|
|
496
|
+
forEach: (data: object) => Promise<object>;
|
|
497
|
+
};
|
|
498
|
+
export type httpRequest = {
|
|
499
|
+
/**
|
|
500
|
+
* - HTTP header key value pairs, e.g. req.headers['content-type']
|
|
501
|
+
*/
|
|
502
|
+
headers: any;
|
|
503
|
+
/**
|
|
504
|
+
* - Get the URL query parameters as key-value pairs
|
|
505
|
+
*/
|
|
506
|
+
query: any;
|
|
507
|
+
/**
|
|
508
|
+
* - Get the URL route variables as key-value pairs
|
|
509
|
+
*/
|
|
510
|
+
params: any;
|
|
511
|
+
/**
|
|
512
|
+
* - Get the request body
|
|
513
|
+
* - JSON payload
|
|
514
|
+
*/
|
|
515
|
+
body: any;
|
|
516
|
+
/**
|
|
517
|
+
* - Get the URL full path, e.g. /dev/myroute
|
|
518
|
+
*/
|
|
519
|
+
path: string;
|
|
520
|
+
/**
|
|
521
|
+
* - Get the URL api path, e.g. /myroute
|
|
522
|
+
*/
|
|
523
|
+
apiPath: string;
|
|
524
|
+
/**
|
|
525
|
+
* - Get the URL full path and query parameter string
|
|
526
|
+
*/
|
|
527
|
+
originalUrl: string;
|
|
528
|
+
/**
|
|
529
|
+
* - Get the HTTP request verb
|
|
530
|
+
*/
|
|
531
|
+
method: string;
|
|
532
|
+
/**
|
|
533
|
+
* - Get the project URL domain name
|
|
534
|
+
*/
|
|
535
|
+
hostname: string;
|
|
536
|
+
/**
|
|
537
|
+
* Pipe data to stream
|
|
538
|
+
* @param data
|
|
539
|
+
* @returns
|
|
540
|
+
*/
|
|
541
|
+
pipe: (data: any) => void;
|
|
542
|
+
};
|
|
543
|
+
export type httpResponse = {
|
|
544
|
+
/**
|
|
545
|
+
* - End request (optional: send text|JSON data to client)
|
|
546
|
+
*/
|
|
547
|
+
end: (data: string | object | void) => void;
|
|
548
|
+
/**
|
|
549
|
+
* - Send text|JSON data to client and end request
|
|
550
|
+
* @example
|
|
551
|
+
* res.end()
|
|
552
|
+
* res.status(404).end()
|
|
553
|
+
*/
|
|
554
|
+
send: (data: string | object) => void;
|
|
555
|
+
/**
|
|
556
|
+
* - End request and send JSON data to client
|
|
557
|
+
* @example
|
|
558
|
+
* res.json({ user: 'tobi' })
|
|
559
|
+
* res.status(500).json({ error: 'message' })
|
|
560
|
+
*/
|
|
561
|
+
json: (document: object) => any;
|
|
562
|
+
/**
|
|
563
|
+
* - Write stream data to the client response.
|
|
564
|
+
* - Content-type must be set before any write operations
|
|
565
|
+
* - set optional type to 'buffer' for binary write operations
|
|
566
|
+
* @example
|
|
567
|
+
* res.set('content-type', 'text/plain')
|
|
568
|
+
* res.write('line one')
|
|
569
|
+
* res.write('\n')
|
|
570
|
+
* res.write('line two')
|
|
571
|
+
* res.end()
|
|
572
|
+
*/
|
|
573
|
+
write: (data: any, type?: string) => any;
|
|
574
|
+
/**
|
|
575
|
+
* - Set a response header value,
|
|
576
|
+
* @example
|
|
577
|
+
* res.set('Content-Type', 'text/html; charset=UTF-8');
|
|
578
|
+
*/
|
|
579
|
+
set: (header: string, value: string) => any;
|
|
580
|
+
/**
|
|
581
|
+
* - Set multiple response header values,
|
|
582
|
+
* @example
|
|
583
|
+
* res.headers({'Content-Type': 'text/html; charset=UTF-8','X-myheader': '123456890'});
|
|
584
|
+
*/
|
|
585
|
+
headers: (headers: object) => any;
|
|
586
|
+
/**
|
|
587
|
+
* Set a response header value
|
|
588
|
+
* @param header string
|
|
589
|
+
* @param value string
|
|
590
|
+
* @returns
|
|
591
|
+
*/
|
|
592
|
+
setHeader: (header: string, value: string | string[]) => any;
|
|
593
|
+
/**
|
|
594
|
+
* Remove any default headers from a response
|
|
595
|
+
* @param header string
|
|
596
|
+
* @returns
|
|
597
|
+
*/
|
|
598
|
+
removeHeader: (header: string) => any;
|
|
599
|
+
/**
|
|
600
|
+
* - Return a HTTP response status code for a client request,
|
|
601
|
+
* @example
|
|
602
|
+
* res.status(401).end();
|
|
603
|
+
*/
|
|
604
|
+
status: (code: number) => any;
|
|
605
|
+
/**
|
|
606
|
+
* - Render template with object data
|
|
607
|
+
* @example
|
|
608
|
+
* res.render('services', {title: "Services page"})
|
|
609
|
+
*/
|
|
610
|
+
render: (template: string, context: object) => void;
|
|
611
|
+
/**
|
|
612
|
+
* - Sets the response header to redirect client request
|
|
613
|
+
* @param statusCode - optional, set to 302 by default
|
|
614
|
+
* @param url
|
|
615
|
+
* @returns
|
|
616
|
+
* @example
|
|
617
|
+
* res.redirect('/assets/login.html'); // default status 302, Temporary
|
|
618
|
+
* res.redirect(301, '/home/correct.html'); // status code 301, Permanently
|
|
619
|
+
*/
|
|
620
|
+
redirect: (statusCode: number | string, url?: string) => void;
|
|
621
|
+
};
|
|
622
|
+
|
|
623
|
+
export type nextFunction = (error?: string) => void;
|
|
624
|
+
|
|
625
|
+
export type Filesystem = {
|
|
626
|
+
/**
|
|
627
|
+
* - Get binary file input stream. Takes a path (string) to file (e.g. /static/logo.png) and an options object (Object) for future use. Returns a Promise resolving to a binary data stream emitter.
|
|
628
|
+
*/
|
|
629
|
+
getReadStream: (filePath: string, options?: any) => Promise<any>;
|
|
630
|
+
/**
|
|
631
|
+
* - Get file as text. Takes an absolute path (string) to file (e.g. /static/home.html) and an options object (Object) for future use. Returns a Promise resolving to the file content (string).
|
|
632
|
+
*/
|
|
633
|
+
readFile: (filePath: string, options?: any) => Promise<string>;
|
|
634
|
+
/**
|
|
635
|
+
* - Save file binary buffer. Takes an absolute path (string) to file (e.g. /static/home.html), a buffer (Buffer) to save, and file meta data (Object). Returns a Promise resolving to the save result.
|
|
636
|
+
*/
|
|
637
|
+
saveFile: (filePath: string, buf: any, options?: any) => Promise<any>;
|
|
638
|
+
/**
|
|
639
|
+
* - Delete a file. Takes an absolute path (string) to file and an options object (Object) for future use. Returns a Promise resolving to the result.
|
|
640
|
+
*/
|
|
641
|
+
deleteFile: (filePath: string, options?: any) => Promise<any>;
|
|
642
|
+
/**
|
|
643
|
+
* - List content of a file directory. Takes an absolute path (string) to file and an options object (Object) for future use. Returns a Promise resolving to the result.
|
|
644
|
+
*/
|
|
645
|
+
list: (filePath: string, options?: any) => Promise<any>;
|
|
646
|
+
/**
|
|
647
|
+
* - Read file into a Buffer. Takes an absolute path (*) to file. Returns a Promise resolving to a Buffer.
|
|
648
|
+
*/
|
|
649
|
+
readFileAsBuffer: (filePath: any) => Promise<any>;
|
|
650
|
+
};
|
|
33
651
|
import { agg } from './aggregation/index.mjs';
|
|
34
|
-
import
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
652
|
+
import * as crud from './crudlify/index.mjs';
|
|
653
|
+
|
|
654
|
+
export class Codehooks {
|
|
655
|
+
static getInstance(): Codehooks;
|
|
656
|
+
/**
|
|
657
|
+
* Express style api
|
|
658
|
+
*/
|
|
659
|
+
settings: {};
|
|
660
|
+
routes: {};
|
|
661
|
+
queues: {};
|
|
662
|
+
jobs: {};
|
|
663
|
+
auths: {};
|
|
664
|
+
appmiddleware: any[];
|
|
665
|
+
datastore: any;
|
|
666
|
+
listeners: any[];
|
|
667
|
+
workers: {};
|
|
668
|
+
/**
|
|
669
|
+
* POST requests - e.g. app.post('/foo', callbackFunction)
|
|
670
|
+
* @example
|
|
671
|
+
* app.post('/myroute', (req, res) => {
|
|
672
|
+
* //console.debug('Body', req.body)
|
|
673
|
+
* res.json({"message": "thanks!"})
|
|
674
|
+
* })
|
|
675
|
+
* @param {string} path - API route, e.g. '/foo'
|
|
676
|
+
* @param {...function(httpRequest, httpResponse, function(string):void)} hook - callback function(s) with parameters (req, res, [next])
|
|
677
|
+
*/
|
|
678
|
+
post: (
|
|
679
|
+
path: string,
|
|
680
|
+
...hook: ((
|
|
681
|
+
request: httpRequest,
|
|
682
|
+
response: httpResponse,
|
|
683
|
+
next: nextFunction
|
|
684
|
+
) => any)[]
|
|
685
|
+
) => void;
|
|
686
|
+
/**
|
|
687
|
+
* GET requests - e.g. app.get('/foo/:ID', callbackFunction)
|
|
688
|
+
* @example
|
|
689
|
+
* app.get('/myroute', (req, res) => {
|
|
690
|
+
* //console.debug('Params', req.params)
|
|
691
|
+
* res.json({"message": "thanks!"})
|
|
692
|
+
* })
|
|
693
|
+
* @param {string} path - API route, e.g. '/foo/:ID'
|
|
694
|
+
* @param {...function(httpRequest, httpResponse, function(string):void)} hook - callback function(s) with parameters (req, res, [next])
|
|
695
|
+
*/
|
|
696
|
+
get: (
|
|
697
|
+
path: string,
|
|
698
|
+
...hook: ((
|
|
699
|
+
request: httpRequest,
|
|
700
|
+
response: httpResponse,
|
|
701
|
+
next: nextFunction
|
|
702
|
+
) => any)[]
|
|
703
|
+
) => void;
|
|
704
|
+
/**
|
|
705
|
+
* PUT requests
|
|
706
|
+
* @example
|
|
707
|
+
* app.put('/myroute', (req, res) => {
|
|
708
|
+
* //console.debug('Body', req.body)
|
|
709
|
+
* res.json({"message": "thanks!"})
|
|
710
|
+
* })
|
|
711
|
+
* @param {string} path - API route, e.g. '/foo/:ID'
|
|
712
|
+
* @param {...function(httpRequest, httpResponse, function(string):void):void} hook - callback function(s) with parameters (req, res, [next])
|
|
713
|
+
*/
|
|
714
|
+
put: (
|
|
715
|
+
path: string,
|
|
716
|
+
...hook: ((
|
|
717
|
+
request: httpRequest,
|
|
718
|
+
response: httpResponse,
|
|
719
|
+
next: nextFunction
|
|
720
|
+
) => void)[]
|
|
721
|
+
) => void;
|
|
722
|
+
/**
|
|
723
|
+
* PATCH requests
|
|
724
|
+
* @example
|
|
725
|
+
* app.patch('/myroute', (req, res) => {
|
|
726
|
+
* //console.debug('Body', req.body)
|
|
727
|
+
* res.json({"message": "thanks!"})
|
|
728
|
+
* })
|
|
729
|
+
* @param {string} path - API route, e.g. '/foo/:ID'
|
|
730
|
+
* @param {...function(httpRequest, httpResponse, function(string):void)} hook - callback function(s) with parameters (req, res, [next])
|
|
731
|
+
*/
|
|
732
|
+
patch: (
|
|
733
|
+
path: string,
|
|
734
|
+
...hook: ((
|
|
735
|
+
request: httpRequest,
|
|
736
|
+
response: httpResponse,
|
|
737
|
+
next: nextFunction
|
|
738
|
+
) => any)[]
|
|
739
|
+
) => void;
|
|
740
|
+
/**
|
|
741
|
+
* DELETE requests - app.delete('/foo', callbackFunction)
|
|
742
|
+
* @example
|
|
743
|
+
* app.delete('/myroute', (req, res) => {
|
|
744
|
+
* res.json({"message": "thanks!"})
|
|
745
|
+
* })
|
|
746
|
+
* @param {string} path - API route, e.g. '/foo/:ID'
|
|
747
|
+
* @param {...function(httpRequest, httpResponse, function(string):void)} hook - callback function(s) with parameters (req, res, [next])
|
|
748
|
+
*/
|
|
749
|
+
delete: (
|
|
750
|
+
path: string,
|
|
751
|
+
...hook: ((
|
|
752
|
+
request: httpRequest,
|
|
753
|
+
response: httpResponse,
|
|
754
|
+
next: nextFunction
|
|
755
|
+
) => any)[]
|
|
756
|
+
) => void;
|
|
757
|
+
/**
|
|
758
|
+
* All HTTP methods - app.all('/pets/cat', (req, res) => res.end('Mjau'))
|
|
759
|
+
* @example
|
|
760
|
+
* app.all('/myroute', (req, res) => {
|
|
761
|
+
* //console.debug('Body', req.body || 'no body')
|
|
762
|
+
* res.json({"message": "thanks!"})
|
|
763
|
+
* })
|
|
764
|
+
* @param {string} path - API route, e.g. '- API endpoint route, e.g. '/pets'
|
|
765
|
+
* @param {...function(httpRequest, httpResponse, function(string)):void} hook - callback function(s) with parameters (req, res, [next])
|
|
766
|
+
*/
|
|
767
|
+
all: (
|
|
768
|
+
path: string,
|
|
769
|
+
...hook: ((
|
|
770
|
+
request: httpRequest,
|
|
771
|
+
response: httpResponse,
|
|
772
|
+
next: (error?: string) => any
|
|
773
|
+
) => void)[]
|
|
774
|
+
) => void;
|
|
775
|
+
/**
|
|
776
|
+
* Allow custom authentication
|
|
777
|
+
* @example
|
|
778
|
+
* app.auth('/*', (req, res, next) => {
|
|
779
|
+
* if (1 === 1) {
|
|
780
|
+
* next()
|
|
781
|
+
* } else {
|
|
782
|
+
* res.status(403).end('No soup!')
|
|
783
|
+
* }
|
|
784
|
+
* })
|
|
785
|
+
* @param {string|RegExp} path - API route, e.g. '/foo/*'
|
|
786
|
+
* @param {...function(httpRequest, httpResponse, function(string):void):void} hook - callback function(s) with parameters (req, res, [next])
|
|
787
|
+
*/
|
|
788
|
+
auth: (
|
|
789
|
+
path: string | RegExp,
|
|
790
|
+
...hook: ((
|
|
791
|
+
request: httpRequest,
|
|
792
|
+
response: httpResponse,
|
|
793
|
+
next: nextFunction
|
|
794
|
+
) => void)[]
|
|
795
|
+
) => void;
|
|
796
|
+
/**
|
|
797
|
+
* Global middleware
|
|
798
|
+
* @example
|
|
799
|
+
* // global middleware
|
|
800
|
+
* app.use((req, res, next) => {
|
|
801
|
+
* //console.debug("Global middleware was here!");
|
|
802
|
+
* next(); // proceed
|
|
803
|
+
* })
|
|
804
|
+
* @param {...function(httpRequest, httpResponse, function(string):void):void} hook - callback function(s) with parameters (req, res, next)
|
|
805
|
+
*/
|
|
806
|
+
use(
|
|
807
|
+
...hook: ((
|
|
808
|
+
request: httpRequest,
|
|
809
|
+
response: httpResponse,
|
|
810
|
+
next: nextFunction
|
|
811
|
+
) => void)[]
|
|
812
|
+
): void;
|
|
813
|
+
/**
|
|
814
|
+
* Global middleware
|
|
815
|
+
* @example
|
|
816
|
+
* // middleware on a route defined by a regular expression
|
|
817
|
+
* app.use(/^\/(foo|bar)/, (req, res, next) => {
|
|
818
|
+
* //console.debug("RegExp route middleware was here!");
|
|
819
|
+
* next(); // proceed
|
|
820
|
+
* })
|
|
821
|
+
* @param {RegExp} regex
|
|
822
|
+
* @param {...function(httpRequest, httpResponse, function(string):void):void} hook - callback function(s) with parameters (req, res, next)
|
|
823
|
+
*/
|
|
824
|
+
use(
|
|
825
|
+
regex: RegExp,
|
|
826
|
+
...hook: ((
|
|
827
|
+
request: httpRequest,
|
|
828
|
+
response: httpResponse,
|
|
829
|
+
next: nextFunction
|
|
830
|
+
) => void)[]
|
|
831
|
+
): void;
|
|
832
|
+
/**
|
|
833
|
+
* Global middleware
|
|
834
|
+
* @example
|
|
835
|
+
* // middleware on a specific route
|
|
836
|
+
* app.use('/myroute', (req, res, next) => {
|
|
837
|
+
* //console.debug("Route middleware was here!");
|
|
838
|
+
* next(); // proceed
|
|
839
|
+
* })
|
|
840
|
+
* @param {string} path
|
|
841
|
+
* @param {...function(httpRequest, httpResponse, function(string):void):void} hook - callback function(s) with parameters (req, res, next)
|
|
842
|
+
*/
|
|
843
|
+
use(
|
|
844
|
+
path: string,
|
|
845
|
+
...hook: ((
|
|
846
|
+
request: httpRequest,
|
|
847
|
+
response: httpResponse,
|
|
848
|
+
next: nextFunction
|
|
849
|
+
) => void)[]
|
|
850
|
+
): void;
|
|
851
|
+
/**
|
|
852
|
+
* Global route middleware
|
|
853
|
+
* @example
|
|
854
|
+
* // Global middleware on a specific route
|
|
855
|
+
* app.use('/myroute', (req, res, next) => {
|
|
856
|
+
* //console.debug("Route middleware was here!");
|
|
857
|
+
* next(); // proceed
|
|
858
|
+
* })
|
|
859
|
+
* @param {string|RegExp} path - Optional API route or RegExp, e.g. '/myroute/*'
|
|
860
|
+
* @param {...function(httpRequest, httpResponse, function(string):void):void} hook - callback function(s) with parameters (req, res, next)
|
|
861
|
+
*/
|
|
862
|
+
useRoute: (
|
|
863
|
+
path: string | RegExp,
|
|
864
|
+
...hook: ((
|
|
865
|
+
request: httpRequest,
|
|
866
|
+
response: httpResponse,
|
|
867
|
+
next: nextFunction
|
|
868
|
+
) => void)[]
|
|
869
|
+
) => void;
|
|
870
|
+
/**
|
|
871
|
+
* Process queue job items for topic
|
|
872
|
+
* @param {string} topic
|
|
873
|
+
* @param {...function(httpRequest, httpResponse, function(string):void)} hook - callback function(s) with parameters (req, res, [next])
|
|
874
|
+
*/
|
|
875
|
+
queue: (
|
|
876
|
+
topic: string,
|
|
877
|
+
...hook: ((
|
|
878
|
+
request: httpRequest,
|
|
879
|
+
response: httpResponse,
|
|
880
|
+
next: nextFunction
|
|
881
|
+
) => any)[]
|
|
882
|
+
) => void;
|
|
883
|
+
/**
|
|
884
|
+
* Add application worker function
|
|
885
|
+
* @param {object} name a unique worker name or JSON configuration
|
|
886
|
+
* @param {...function(httpRequest, httpResponse, function(string):void)} hook - callback function(s) with parameters (req, res, [next])
|
|
887
|
+
* @example
|
|
888
|
+
* app.worker('myworker', (data, job) => {
|
|
889
|
+
* const {body:{payload}} = data
|
|
890
|
+
* //console.debug('worker payload data', payload)
|
|
891
|
+
* // do stuff with payload data
|
|
892
|
+
* job.end()
|
|
893
|
+
*})
|
|
894
|
+
* @example
|
|
895
|
+
* app.worker({name: 'myworker', workers: 5}, (data, job) => {
|
|
896
|
+
* const {body:{payload}} = data
|
|
897
|
+
* //console.debug('worker payload data', payload)
|
|
898
|
+
* // do stuff with payload data
|
|
899
|
+
* job.end()
|
|
900
|
+
*})
|
|
901
|
+
*/
|
|
902
|
+
worker: (
|
|
903
|
+
name: string | object,
|
|
904
|
+
...hook: ((
|
|
905
|
+
request: httpRequest,
|
|
906
|
+
response: httpResponse,
|
|
907
|
+
next: nextFunction
|
|
908
|
+
) => any)[]
|
|
909
|
+
) => void;
|
|
910
|
+
/**
|
|
911
|
+
* Create cron background jobs
|
|
912
|
+
* @example
|
|
913
|
+
* // call function each minute
|
|
914
|
+
* app.job('* * * * *', (req, res) => {
|
|
915
|
+
* //console.debug('tick each minute')
|
|
916
|
+
* res.end()
|
|
917
|
+
* })
|
|
918
|
+
* @param {string} cronExpression - cron expression
|
|
919
|
+
* @param {...function(httpRequest, httpResponse, function(string):void)} hook - callback function(s) with parameters (req, res, [next])
|
|
920
|
+
*/
|
|
921
|
+
job: (
|
|
922
|
+
cronExpression: string,
|
|
923
|
+
...hook: ((
|
|
924
|
+
request: httpRequest,
|
|
925
|
+
response: httpResponse,
|
|
926
|
+
next: nextFunction
|
|
927
|
+
) => any)[]
|
|
928
|
+
) => void;
|
|
929
|
+
/**
|
|
930
|
+
* Set up a server-sent events channel endpoint (SSE).
|
|
931
|
+
* This method registers a set of middleware (hooks) for the channel and defines an SSE endpoint.
|
|
932
|
+
*
|
|
933
|
+
* @param {string} path - The channel on which to establish a real-time connection. This acts as the base path for the SSE endpoint.
|
|
934
|
+
* @param {...function(httpRequest, httpResponse, function(string):void)} hook - Optional middleware functions to be applied to the channel (access check for example). These functions are executed in the order they are provided.
|
|
935
|
+
*/
|
|
936
|
+
realtime(
|
|
937
|
+
path: string,
|
|
938
|
+
...hook: ((
|
|
939
|
+
request: httpRequest,
|
|
940
|
+
response: httpResponse,
|
|
941
|
+
next: nextFunction
|
|
942
|
+
) => any)[]
|
|
943
|
+
): void;
|
|
944
|
+
|
|
945
|
+
/**
|
|
946
|
+
* Serve static file content from a source code diretory
|
|
947
|
+
* @param {Object} options -
|
|
948
|
+
* - options.route - API route to serve assets
|
|
949
|
+
* - options.directory - path to directory with assets/files
|
|
950
|
+
* - options.default - default file name if root is /
|
|
951
|
+
@param {...function(httpRequest, httpResponse, function(string):void)} hook - Optional middleware functions to be applied before resource is served
|
|
952
|
+
* @example
|
|
953
|
+
* app.static({route:'/static', directory: '/assets'}, (req, res, next) => {
|
|
954
|
+
* console.log("Serving a static resource", req.path);
|
|
955
|
+
* next();
|
|
956
|
+
* })
|
|
957
|
+
* @returns void
|
|
958
|
+
*/
|
|
959
|
+
static: (options: any,
|
|
960
|
+
...hook: ((
|
|
961
|
+
request: httpRequest,
|
|
962
|
+
response: httpResponse,
|
|
963
|
+
next: nextFunction
|
|
964
|
+
) => any)[]) => void;
|
|
965
|
+
/**
|
|
966
|
+
* Serve file content from a blob storage diretory
|
|
967
|
+
* @param {Object} options - {route: "/documents", directory: "/docs"}
|
|
968
|
+
* @returns void
|
|
969
|
+
*/
|
|
970
|
+
storage: (options: any,
|
|
971
|
+
...hook: ((
|
|
972
|
+
request: httpRequest,
|
|
973
|
+
response: httpResponse,
|
|
974
|
+
next: nextFunction
|
|
975
|
+
) => any)[]) => void;
|
|
976
|
+
/**
|
|
977
|
+
* Configuration settings
|
|
978
|
+
* @param {string} key
|
|
979
|
+
* @param {*} val
|
|
980
|
+
* @example
|
|
981
|
+
* app.set('views', '/views')
|
|
982
|
+
* app.set('view engine', {"hbs": handlebars})
|
|
983
|
+
*/
|
|
984
|
+
set: (key: string, val: any) => void;
|
|
985
|
+
/**
|
|
986
|
+
*
|
|
987
|
+
* @param {string} view
|
|
988
|
+
* @param {object} data
|
|
989
|
+
* @param {function(string):void)} cb
|
|
990
|
+
*/
|
|
991
|
+
render: (view: string, data: object, cb: any) => Promise<void>;
|
|
992
|
+
/**
|
|
993
|
+
* Create CRUD REST API for database
|
|
994
|
+
* @see {@link https://codehooks.io/docs/database-rest-api}
|
|
995
|
+
* @see {@link https://codehooks.io/docs/database-rest-api#data-validation-with-yup Data validation with Yup, Zod, JSON.schema}
|
|
996
|
+
* @param {object} schema
|
|
997
|
+
* @param {object} options - Optional {prefix: "/crud"} serve all crud routes under sub route /crud/*
|
|
998
|
+
* @returns Promise - Eventhooks
|
|
999
|
+
*/
|
|
1000
|
+
crudlify: (
|
|
1001
|
+
schema?: object,
|
|
1002
|
+
options?: object
|
|
1003
|
+
) => Promise<import('./crudlify/lib/eventhooks.mjs').EventHooks>;
|
|
1004
|
+
/**
|
|
1005
|
+
* Returns the Codehooks serverless definitions used by a serverless runtime engine
|
|
1006
|
+
* @example
|
|
1007
|
+
* export default app.init();
|
|
1008
|
+
* @returns {Object} App instance
|
|
1009
|
+
*/
|
|
1010
|
+
init: (cb?: any) => any;
|
|
1011
|
+
/**
|
|
1012
|
+
* Alias for app.init()
|
|
1013
|
+
* @example
|
|
1014
|
+
* export default app.start();
|
|
1015
|
+
* @returns {Object} App instance
|
|
1016
|
+
*/
|
|
1017
|
+
start: (cb?: any) => any;
|
|
1018
|
+
/**
|
|
1019
|
+
* Use Node Express app to run standalone
|
|
1020
|
+
* All Codehooks routes will be applied as Express Routes
|
|
1021
|
+
* @param {Object} express app instance
|
|
1022
|
+
* @param {Object} options for Datastore and routing space
|
|
1023
|
+
*/
|
|
1024
|
+
useExpress: (express: any, options: any) => Promise<void>;
|
|
1025
|
+
/**
|
|
1026
|
+
* Set Datastore interface
|
|
1027
|
+
* @param {*} ds
|
|
1028
|
+
*/
|
|
1029
|
+
setDatastore: (ds: any) => void;
|
|
1030
|
+
getDatastore: () => {
|
|
1031
|
+
open: () => Promise<any>;
|
|
1032
|
+
};
|
|
1033
|
+
addListener: (observer: any) => void;
|
|
1034
|
+
/**
|
|
1035
|
+
* Create a new steps workflow
|
|
1036
|
+
* @param name - Unique identifier for the steps workflow
|
|
1037
|
+
* @param description - Human-readable description of the steps workflow
|
|
1038
|
+
* @param steps - Object containing step definitions
|
|
1039
|
+
* @param options - Optional configuration options
|
|
1040
|
+
* @returns WorkflowEngine instance for managing the workflow
|
|
1041
|
+
*/
|
|
1042
|
+
createWorkflow: (name: string, description: string, steps: StepsDefinition, options?: any) => WorkflowEngine;
|
|
134
1043
|
}
|
|
135
|
-
import { Steps as Workflow } from './workflow/index.mjs';
|
|
136
|
-
import { StepsConfig as WorkflowConfig } from './workflow/index.mjs';
|
|
137
1044
|
declare const _coho: Codehooks;
|
|
138
|
-
|
|
1045
|
+
|
|
1046
|
+
/**
|
|
1047
|
+
* Events emitted by the workflow engine
|
|
1048
|
+
*/
|
|
1049
|
+
export type StepsEvents = {
|
|
1050
|
+
/**
|
|
1051
|
+
* Emitted when a new workflow is registered
|
|
1052
|
+
* @event
|
|
1053
|
+
*/
|
|
1054
|
+
'workflowCreated': { name: string; description: string };
|
|
1055
|
+
|
|
1056
|
+
/**
|
|
1057
|
+
* Emitted when a new workflow instance is started
|
|
1058
|
+
* @event
|
|
1059
|
+
*/
|
|
1060
|
+
'workflowStarted': { name: string; initialState: any };
|
|
1061
|
+
|
|
1062
|
+
/**
|
|
1063
|
+
* Emitted when a step begins execution
|
|
1064
|
+
* @event
|
|
1065
|
+
*/
|
|
1066
|
+
'stepStarted': { workflowName: string; step: string; state: any; instanceId: string };
|
|
1067
|
+
|
|
1068
|
+
/**
|
|
1069
|
+
* Emitted when a step's state is updated
|
|
1070
|
+
* @event
|
|
1071
|
+
*/
|
|
1072
|
+
'stateUpdated': { workflowName: string; state: any; instanceId: string };
|
|
1073
|
+
|
|
1074
|
+
/**
|
|
1075
|
+
* Emitted when a step is waiting for input
|
|
1076
|
+
* @event
|
|
1077
|
+
*/
|
|
1078
|
+
'stepWaiting': { workflowName: string; step: string; instanceId: string };
|
|
1079
|
+
|
|
1080
|
+
/**
|
|
1081
|
+
* Emitted when a workflow instance is continued after waiting
|
|
1082
|
+
* @event
|
|
1083
|
+
*/
|
|
1084
|
+
'stepContinued': { workflowName: string; step: string; instanceId: string };
|
|
1085
|
+
|
|
1086
|
+
/**
|
|
1087
|
+
* Emitted when a workflow instance is completed
|
|
1088
|
+
* @event
|
|
1089
|
+
*/
|
|
1090
|
+
'completed': { message: string; state: any };
|
|
1091
|
+
|
|
1092
|
+
/**
|
|
1093
|
+
* Emitted when a workflow instance is cancelled
|
|
1094
|
+
* @event
|
|
1095
|
+
*/
|
|
1096
|
+
'cancelled': { id: string };
|
|
1097
|
+
};
|
|
1098
|
+
|
|
1099
|
+
/**
|
|
1100
|
+
* Definition of steps in a workflow
|
|
1101
|
+
*/
|
|
1102
|
+
export type StepsDefinition = Record<string, (state: any, goto: (step: string | string[] | null, state: any) => void) => void | Promise<void>>;
|
|
1103
|
+
|
|
1104
|
+
/**
|
|
1105
|
+
* Engine for managing step-based workflows
|
|
1106
|
+
*/
|
|
1107
|
+
export type StepsEngine = {
|
|
1108
|
+
/**
|
|
1109
|
+
* Configure the workflow engine
|
|
1110
|
+
* @param options - Configuration options
|
|
1111
|
+
*/
|
|
1112
|
+
configure: (options: any) => void;
|
|
1113
|
+
} & {
|
|
1114
|
+
/**
|
|
1115
|
+
* Get the singleton instance
|
|
1116
|
+
*/
|
|
1117
|
+
getInstance: () => Steps;
|
|
1118
|
+
};
|
|
1119
|
+
|
|
1120
|
+
/**
|
|
1121
|
+
* Engine for managing step-based workflows
|
|
1122
|
+
* @extends Steps
|
|
1123
|
+
*/
|
|
1124
|
+
export type WorkflowEngine = Steps & {
|
|
1125
|
+
/**
|
|
1126
|
+
* Register a new steps workflow
|
|
1127
|
+
* @param name - Unique identifier for the workflow
|
|
1128
|
+
* @param description - Human-readable description
|
|
1129
|
+
* @param steps - Step definitions
|
|
1130
|
+
*/
|
|
1131
|
+
register: (name: string, description: string, steps: StepsDefinition) => Promise<string>;
|
|
1132
|
+
};
|
|
1133
|
+
|
|
1134
|
+
/**
|
|
1135
|
+
* Steps workflow engine for managing step-based workflows
|
|
1136
|
+
* @extends EventEmitter
|
|
1137
|
+
*/
|
|
1138
|
+
export type Steps = {
|
|
1139
|
+
/**
|
|
1140
|
+
* Configure the workflow engine
|
|
1141
|
+
* @param options - Configuration options
|
|
1142
|
+
*/
|
|
1143
|
+
configure: (options: any) => void;
|
|
1144
|
+
|
|
1145
|
+
/**
|
|
1146
|
+
* Register a new steps workflow
|
|
1147
|
+
* @param app - Codehooks application instance
|
|
1148
|
+
* @param name - Unique identifier for the steps workflow
|
|
1149
|
+
* @param description - Human-readable description
|
|
1150
|
+
* @param definition - Object containing step definitions
|
|
1151
|
+
* @returns Promise with the registered steps name
|
|
1152
|
+
*/
|
|
1153
|
+
register: (app: Codehooks, name: string, description: string, definition: Record<string, Function>) => Promise<string>;
|
|
1154
|
+
|
|
1155
|
+
/**
|
|
1156
|
+
* Start a new steps instance
|
|
1157
|
+
* @param name - Name of the steps workflow to start
|
|
1158
|
+
* @param initialState - Initial state for the steps instance
|
|
1159
|
+
* @returns Promise with the steps instance ID
|
|
1160
|
+
*/
|
|
1161
|
+
start: (name: string, initialState: any) => Promise<{ id: string }>;
|
|
1162
|
+
|
|
1163
|
+
/**
|
|
1164
|
+
* Update the state of a steps instance
|
|
1165
|
+
* @param stepsName - Name of the steps workflow
|
|
1166
|
+
* @param instanceId - ID of the steps instance
|
|
1167
|
+
* @param state - New state to update with
|
|
1168
|
+
* @param options - Options for the update, { continue: false } to avoid continuing the the step
|
|
1169
|
+
* @returns Promise with the updated state
|
|
1170
|
+
*/
|
|
1171
|
+
updateState: (stepsName: string, instanceId: string, state: any, options?: any) => Promise<any>;
|
|
1172
|
+
|
|
1173
|
+
/**
|
|
1174
|
+
* Continue a paused steps instance
|
|
1175
|
+
* @param stepsName - Name of the steps workflow
|
|
1176
|
+
* @param instanceId - ID of the steps instance
|
|
1177
|
+
* @returns Promise with the queue ID for the continued step
|
|
1178
|
+
*/
|
|
1179
|
+
continue: (stepsName: string, instanceId: string) => Promise<{ qId: string }>;
|
|
1180
|
+
|
|
1181
|
+
/**
|
|
1182
|
+
* Get the status of a steps instance
|
|
1183
|
+
* @param id - ID of the steps instance
|
|
1184
|
+
* @returns Promise with the steps status
|
|
1185
|
+
*/
|
|
1186
|
+
getStepsStatus: (id: string) => Promise<any>;
|
|
1187
|
+
|
|
1188
|
+
/**
|
|
1189
|
+
* Get all steps instances matching a filter
|
|
1190
|
+
* @param filter - Filter criteria for steps workflows
|
|
1191
|
+
* @returns Promise with list of steps instances
|
|
1192
|
+
*/
|
|
1193
|
+
getInstances: (filter: any) => Promise<any[]>;
|
|
1194
|
+
|
|
1195
|
+
/**
|
|
1196
|
+
* Cancel a steps instance
|
|
1197
|
+
* @param id - ID of the steps instance to cancel
|
|
1198
|
+
* @returns Promise with the cancellation result
|
|
1199
|
+
*/
|
|
1200
|
+
cancelSteps: (id: string) => Promise<any>;
|
|
1201
|
+
|
|
1202
|
+
/**
|
|
1203
|
+
* Register an event listener
|
|
1204
|
+
* @param event - Name of the event to listen for
|
|
1205
|
+
* @param listener - Callback function to handle the event
|
|
1206
|
+
* @example
|
|
1207
|
+
* Steps.on('stepStarted', ({ stepsName, step, state, instanceId }) => {
|
|
1208
|
+
* console.log(`Step ${step} started in workflow ${stepsName}`);
|
|
1209
|
+
* });
|
|
1210
|
+
*/
|
|
1211
|
+
on: (event: string, listener: (data: any) => void) => Steps;
|
|
1212
|
+
|
|
1213
|
+
/**
|
|
1214
|
+
* Register a one-time event listener
|
|
1215
|
+
* @param event - Name of the event to listen for
|
|
1216
|
+
* @param listener - Callback function to handle the event
|
|
1217
|
+
*/
|
|
1218
|
+
once: (event: string, listener: (data: any) => void) => Steps;
|
|
1219
|
+
|
|
1220
|
+
/**
|
|
1221
|
+
* Remove an event listener
|
|
1222
|
+
* @param event - Name of the event
|
|
1223
|
+
* @param listener - Callback function to remove
|
|
1224
|
+
*/
|
|
1225
|
+
off: (event: string, listener: (data: any) => void) => Steps;
|
|
1226
|
+
|
|
1227
|
+
/**
|
|
1228
|
+
* Emit an event
|
|
1229
|
+
* @param event - Name of the event to emit
|
|
1230
|
+
* @param data - Event data
|
|
1231
|
+
*/
|
|
1232
|
+
emit: (event: string, data: any) => boolean;
|
|
1233
|
+
|
|
1234
|
+
/**
|
|
1235
|
+
* Continue all timed out steps instances
|
|
1236
|
+
* @returns {Promise<Array<{qId: string}>>} Array of results containing queue IDs for continued workflows
|
|
1237
|
+
*/
|
|
1238
|
+
continueAllTimedOut: () => Promise<Array<{qId: string}>>;
|
|
1239
|
+
};
|
|
1240
|
+
|
|
1241
|
+
/**
|
|
1242
|
+
* Configuration for the Steps workflow engine
|
|
1243
|
+
*/
|
|
1244
|
+
export type StepsConfig = {
|
|
1245
|
+
/**
|
|
1246
|
+
* Set the collection name for storing steps data
|
|
1247
|
+
* @param name - Collection name
|
|
1248
|
+
*/
|
|
1249
|
+
setCollectionName: (name: string) => void;
|
|
1250
|
+
|
|
1251
|
+
/**
|
|
1252
|
+
* Set the queue prefix for steps jobs
|
|
1253
|
+
* @param prefix - Queue prefix
|
|
1254
|
+
*/
|
|
1255
|
+
setQueuePrefix: (prefix: string) => void;
|
|
1256
|
+
};
|
|
1257
|
+
|
|
1258
|
+
export const stepsconfig: StepsConfig;
|
|
1259
|
+
|
|
1260
|
+
/**
|
|
1261
|
+
* Configure the workflow engine
|
|
1262
|
+
* @param options - Configuration options
|
|
1263
|
+
*/
|
|
1264
|
+
export function configure(options: any): void;
|
|
1265
|
+
|
|
1266
|
+
//# sourceMappingURL=index.d.ts.map
|