codehooks-js 1.2.13 → 1.2.15
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/crudlify/index.mjs +1 -1
- package/crudlify/lib/schema/yup/index.mjs +16 -10
- package/index.js +10 -57
- package/package.json +1 -3
- package/types/index.d.ts +48 -6
- package/coderunner.mjs +0 -22
- package/cronjob.mjs +0 -36
package/crudlify/index.mjs
CHANGED
|
@@ -15,7 +15,6 @@ let _query = null;
|
|
|
15
15
|
let _eventHooks = null;
|
|
16
16
|
|
|
17
17
|
export default async function crudlify(app, schema = {}, options = { schema: "yup", query: "q2m", prefix: "" }) {
|
|
18
|
-
console.log('Init crudlify', schema, options)
|
|
19
18
|
_schema = schema;
|
|
20
19
|
_opt = options;
|
|
21
20
|
if (_opt.prefix === undefined || _opt.prefix === '/') {
|
|
@@ -111,6 +110,7 @@ async function createFunc(req, res) {
|
|
|
111
110
|
if (_schema[collection] != undefined) {
|
|
112
111
|
|
|
113
112
|
if (_schema[collection] != null) {
|
|
113
|
+
console.debug('validate', document)
|
|
114
114
|
_validate(_schema[collection], document, _opt)
|
|
115
115
|
.then(async function (value) {
|
|
116
116
|
document = _cast(_schema[collection], value)
|
|
@@ -1,20 +1,26 @@
|
|
|
1
1
|
const debug = console.debug;
|
|
2
2
|
|
|
3
3
|
export const validate = (schema, document) => {
|
|
4
|
-
|
|
4
|
+
|
|
5
5
|
return new Promise(async (resolve, reject) => {
|
|
6
|
+
debug('Validate Yup', document)
|
|
6
7
|
if (schema == null) {
|
|
7
8
|
debug('Null validator')
|
|
8
|
-
resolve(
|
|
9
|
+
resolve(document)
|
|
9
10
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
11
|
+
try {
|
|
12
|
+
const valid = await schema.validate(document);
|
|
13
|
+
console.debug('validated result', valid)
|
|
14
|
+
if (valid) {
|
|
15
|
+
debug('Validate ok', valid)
|
|
16
|
+
resolve(document)
|
|
17
|
+
} else {
|
|
18
|
+
debug('Validate error', valid)
|
|
19
|
+
reject(valid)
|
|
20
|
+
}
|
|
21
|
+
} catch (error) {
|
|
22
|
+
console.error('Yup error', error)
|
|
23
|
+
reject(error)
|
|
18
24
|
}
|
|
19
25
|
});
|
|
20
26
|
}
|
package/index.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import {agg} from './aggregation/index.mjs';
|
|
2
2
|
import {crudlify as crud} from './crudlify/index.mjs';
|
|
3
3
|
import {serveStatic as ws, render as renderView} from './webserver.mjs';
|
|
4
|
-
let cronjob = null;
|
|
5
|
-
let coderun = null;
|
|
6
|
-
|
|
7
4
|
|
|
8
5
|
function createRoute(str) {
|
|
9
6
|
if(str instanceof RegExp) {
|
|
@@ -22,7 +19,8 @@ class Codehooks {
|
|
|
22
19
|
datastore = null;
|
|
23
20
|
listeners = [];
|
|
24
21
|
workers = {};
|
|
25
|
-
realtime = {}
|
|
22
|
+
realtime = {};
|
|
23
|
+
startup = {};
|
|
26
24
|
|
|
27
25
|
post = (path, ...hook) => {
|
|
28
26
|
this.routes[`POST ${createRoute(path)}`] = hook;
|
|
@@ -118,7 +116,7 @@ class Codehooks {
|
|
|
118
116
|
})
|
|
119
117
|
}
|
|
120
118
|
|
|
121
|
-
init = () => {
|
|
119
|
+
init = (...hook) => {
|
|
122
120
|
const manifest = {
|
|
123
121
|
workerhooks: this.workers,
|
|
124
122
|
routehooks: this.routes,
|
|
@@ -126,63 +124,17 @@ class Codehooks {
|
|
|
126
124
|
jobhooks: this.jobs,
|
|
127
125
|
authhooks: this.auths,
|
|
128
126
|
globalhooks: this.appmiddleware,
|
|
129
|
-
useExpress: this.useExpress,
|
|
130
127
|
settings: this.settings,
|
|
131
128
|
realtime: this.realtime,
|
|
132
129
|
app: this
|
|
133
130
|
};
|
|
131
|
+
if (hook) {
|
|
132
|
+
manifest.startup = hook;
|
|
133
|
+
}
|
|
134
134
|
return manifest;
|
|
135
135
|
};
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
cronjob = import("./cronjob.mjs")
|
|
139
|
-
coderun = import('./coderunner.mjs')
|
|
140
|
-
const { datastore, space = '/dev' } = options;
|
|
141
|
-
await datastore.connect();
|
|
142
|
-
this.setDatastore(datastore);
|
|
143
|
-
|
|
144
|
-
for (let key in this.auths) {
|
|
145
|
-
const func = this.auth[key];
|
|
146
|
-
console.error('Auth middleware not implemented:', key)
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
for (let key in this.appmiddleware) {
|
|
150
|
-
const func = this.appmiddleware[key];
|
|
151
|
-
if (func.path) {
|
|
152
|
-
express.use(`${space}${func.path}`, func.func);
|
|
153
|
-
} else {
|
|
154
|
-
express.use(func);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
for (let key in this.routes) {
|
|
159
|
-
const func = this.routes[key];
|
|
160
|
-
const [method, route] = key.split(' ');
|
|
161
|
-
switch (method) {
|
|
162
|
-
case 'GET': express.get(`${space}${route}`, ...func); break;
|
|
163
|
-
case 'PUT': express.put(`${space}${route}`, ...func); break;
|
|
164
|
-
case 'POST': express.post(`${space}${route}`, ...func); break;
|
|
165
|
-
case 'PATCH': express.patch(`${space}${route}`, ...func); break;
|
|
166
|
-
case 'DELETE': express.delete(`${space}${route}`, ...func); break;
|
|
167
|
-
case '*': express.all(`${space}${route}`, ...func); break;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// apply jobs
|
|
172
|
-
for (let key in this.jobs) {
|
|
173
|
-
const func = this.jobs[key];
|
|
174
|
-
const cron = key;
|
|
175
|
-
cronjob(cron, func);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
// apply queue workers
|
|
179
|
-
for (let key in this.queues) {
|
|
180
|
-
const func = this.queues[key];
|
|
181
|
-
const topic = key;
|
|
182
|
-
//console.debug('Apply queue', topic, func);
|
|
183
|
-
this.datastore.setQueue(topic, func);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
136
|
+
// alias
|
|
137
|
+
start = this.init;
|
|
186
138
|
|
|
187
139
|
setDatastore = (ds) => {
|
|
188
140
|
this.datastore = ds;
|
|
@@ -199,9 +151,11 @@ class Codehooks {
|
|
|
199
151
|
}
|
|
200
152
|
}
|
|
201
153
|
}
|
|
154
|
+
|
|
202
155
|
addListener = (observer) => {
|
|
203
156
|
this.listeners.push(observer);
|
|
204
157
|
}
|
|
158
|
+
|
|
205
159
|
static getInstance() {
|
|
206
160
|
if (!this.instance) {
|
|
207
161
|
this.instance = new Codehooks();
|
|
@@ -233,7 +187,6 @@ export const aggregation = agg;
|
|
|
233
187
|
export const crudlify = crud;
|
|
234
188
|
export const coho = _coho;
|
|
235
189
|
export const app = _coho;
|
|
236
|
-
export const coderunner = coderun;
|
|
237
190
|
|
|
238
191
|
export const realtime = {
|
|
239
192
|
createChannel: (path, ...hook) => {
|
package/package.json
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "codehooks-js",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.15",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Codehooks.io official library - provides express.JS like syntax",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"types": "./types",
|
|
8
8
|
"files": [
|
|
9
9
|
"index.js",
|
|
10
|
-
"coderunner.mjs",
|
|
11
|
-
"cronjob.mjs",
|
|
12
10
|
"./aggregation/index.mjs",
|
|
13
11
|
"./webserver.mjs",
|
|
14
12
|
"./crudlify/index.mjs",
|
package/types/index.d.ts
CHANGED
|
@@ -476,26 +476,40 @@ export type httpResponse = {
|
|
|
476
476
|
end: (data: string | object | void) => void;
|
|
477
477
|
/**
|
|
478
478
|
* - Send text|JSON data to client and end request
|
|
479
|
+
* @example
|
|
480
|
+
* res.end()
|
|
481
|
+
* res.status(404).end()
|
|
479
482
|
*/
|
|
480
483
|
send: (data: string | object) => void;
|
|
481
484
|
/**
|
|
482
485
|
* - End request and send JSON data to client
|
|
486
|
+
* @example
|
|
487
|
+
* res.json({ user: 'tobi' })
|
|
488
|
+
* res.status(500).json({ error: 'message' })
|
|
483
489
|
*/
|
|
484
490
|
json: (document: object) => any;
|
|
485
491
|
/**
|
|
486
492
|
* - Write stream data to the client response.
|
|
487
493
|
* - Content-type must be set before any write operations
|
|
488
494
|
* - set optional type to 'buffer' for binary write operations
|
|
495
|
+
* @example
|
|
496
|
+
* res.set('content-type', 'text/plain')
|
|
497
|
+
* res.write('line one')
|
|
498
|
+
* res.write('\n')
|
|
499
|
+
* res.write('line two')
|
|
500
|
+
* res.end()
|
|
489
501
|
*/
|
|
490
502
|
write: (data: any, type?: string) => any;
|
|
491
503
|
/**
|
|
492
504
|
* - Set a response header value,
|
|
493
|
-
*
|
|
505
|
+
* @example
|
|
506
|
+
* res.set('Content-Type', 'text/html; charset=UTF-8');
|
|
494
507
|
*/
|
|
495
508
|
set: (header: string, value: string) => any;
|
|
496
509
|
/**
|
|
497
510
|
* - Set multiple response header values,
|
|
498
|
-
*
|
|
511
|
+
* @example
|
|
512
|
+
* res.headers({'Content-Type': 'text/html; charset=UTF-8','X-myheader': '123456890'});
|
|
499
513
|
*/
|
|
500
514
|
headers: (headers: object) => any;
|
|
501
515
|
/**
|
|
@@ -513,14 +527,26 @@ export type httpResponse = {
|
|
|
513
527
|
removeHeader: (header: string) => any;
|
|
514
528
|
/**
|
|
515
529
|
* - Return a HTTP response status code for a client request,
|
|
516
|
-
*
|
|
530
|
+
* @example
|
|
531
|
+
* res.status(401).end();
|
|
517
532
|
*/
|
|
518
533
|
status: (code: number) => any;
|
|
519
534
|
/**
|
|
520
535
|
* - Render template with object data
|
|
521
|
-
*
|
|
536
|
+
* @example
|
|
537
|
+
* res.render('services', {title: "Services page"})
|
|
522
538
|
*/
|
|
523
539
|
render: (template: string, context: object) => void;
|
|
540
|
+
/**
|
|
541
|
+
* - Sets the response header to redirect client request
|
|
542
|
+
* @param statusCode - optional, set to 302 by default
|
|
543
|
+
* @param url
|
|
544
|
+
* @returns
|
|
545
|
+
* @example
|
|
546
|
+
* res.redirect('/assets/login.html'); // default status 302, Temporary
|
|
547
|
+
* res.redirect(301, '/home/correct.html'); // status code 301, Permanently
|
|
548
|
+
*/
|
|
549
|
+
redirect: (statusCode: number | string, url?: string) => void;
|
|
524
550
|
};
|
|
525
551
|
|
|
526
552
|
export type nextFunction = (error?: string) => void;
|
|
@@ -840,7 +866,16 @@ declare class Codehooks {
|
|
|
840
866
|
|
|
841
867
|
/**
|
|
842
868
|
* Serve static file content from a source code diretory
|
|
843
|
-
* @param {Object} options -
|
|
869
|
+
* @param {Object} options -
|
|
870
|
+
* - options.route - API route to serve assets
|
|
871
|
+
* - options.directory - path to directory with assets/files
|
|
872
|
+
* - options.default - default file name if root is /
|
|
873
|
+
@param {...function(httpRequest, httpResponse, function(string):void)} hook - Optional middleware functions to be applied before resource is served
|
|
874
|
+
* @example
|
|
875
|
+
* app.static({route:'/static', directory: '/assets'}, (req, res, next) => {
|
|
876
|
+
* console.log("Serving a static resource", req.path);
|
|
877
|
+
* next();
|
|
878
|
+
* })
|
|
844
879
|
* @returns void
|
|
845
880
|
*/
|
|
846
881
|
static: (options: any,
|
|
@@ -894,7 +929,14 @@ declare class Codehooks {
|
|
|
894
929
|
* export default app.init();
|
|
895
930
|
* @returns {Object} App instance
|
|
896
931
|
*/
|
|
897
|
-
init: () => any;
|
|
932
|
+
init: (cb?: any) => any;
|
|
933
|
+
/**
|
|
934
|
+
* Alias for app.init()
|
|
935
|
+
* @example
|
|
936
|
+
* export default app.start();
|
|
937
|
+
* @returns {Object} App instance
|
|
938
|
+
*/
|
|
939
|
+
start: (cb?: any) => any;
|
|
898
940
|
/**
|
|
899
941
|
* Use Node Express app to run standalone
|
|
900
942
|
* All Codehooks routes will be applied as Express Routes
|
package/coderunner.mjs
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
export default function execAll(funcarr, context, callfuncs, nextfunc) {
|
|
2
|
-
const runOne = (pos) => {
|
|
3
|
-
(async function () {
|
|
4
|
-
try {
|
|
5
|
-
if (funcarr[pos] !== null && funcarr[pos] !== undefined) {
|
|
6
|
-
await funcarr[pos](context, callfuncs, (nextparam) => {
|
|
7
|
-
if (nextparam && nextfunc) {
|
|
8
|
-
return nextfunc(nextparam)
|
|
9
|
-
}
|
|
10
|
-
runOne(pos + 1);
|
|
11
|
-
});
|
|
12
|
-
} else {
|
|
13
|
-
if (nextfunc) nextfunc();
|
|
14
|
-
}
|
|
15
|
-
} catch (ex) {
|
|
16
|
-
console.error("Unhandled Codehook function exception:", ex || ex, funcarr, pos);
|
|
17
|
-
callfuncs.end("Unhandled Codehook exception:" + ex.message || ex);
|
|
18
|
-
}
|
|
19
|
-
})();
|
|
20
|
-
}
|
|
21
|
-
runOne(0);
|
|
22
|
-
}
|
package/cronjob.mjs
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import crontab from 'node-cron';
|
|
2
|
-
import cronparser from 'cron-parser';
|
|
3
|
-
import execAll from './coderunner.mjs';
|
|
4
|
-
const debug = console.debug;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export default function run(cronexpr, theFunc) {
|
|
8
|
-
//return console.log('Not implemented in standalone version')
|
|
9
|
-
try {
|
|
10
|
-
// verify cron expression
|
|
11
|
-
cronparser.parseExpression(cronexpr);
|
|
12
|
-
const newtimer = crontab.schedule(cronexpr, async () => {
|
|
13
|
-
const theContext = {
|
|
14
|
-
end: function (output) {
|
|
15
|
-
if (output) console.log(output);
|
|
16
|
-
return this;
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
debug('tick', cronexpr, theFunc);
|
|
20
|
-
try {
|
|
21
|
-
if (Array.isArray(theFunc)) {
|
|
22
|
-
await execAll(theFunc, {}, theContext);
|
|
23
|
-
} else {
|
|
24
|
-
await theFunc({}, theContext);
|
|
25
|
-
}
|
|
26
|
-
} catch (ex) {
|
|
27
|
-
console.log("Cron err", ex.message)
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
} catch (ex) {
|
|
31
|
-
console.error("Something is fishy with the cron expression", `'${cronexpr}'`)
|
|
32
|
-
if (argv.debug) console.error(ex.message)
|
|
33
|
-
debug(ex)
|
|
34
|
-
process.exit(1)
|
|
35
|
-
}
|
|
36
|
-
}
|