temba 0.15.0 → 0.16.0
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/README.md +12 -17
- package/config/index.d.ts +4 -0
- package/config/index.js +5 -1
- package/config/index.js.map +1 -1
- package/index.d.ts +4 -1
- package/index.js +13 -3
- package/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -72,11 +72,13 @@ Alternatively, add Temba to your app manually:
|
|
|
72
72
|
```js
|
|
73
73
|
import temba from 'temba'
|
|
74
74
|
const server = temba.create()
|
|
75
|
+
server.start()
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
3.In your console you'll see:
|
|
75
79
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
console.log(`Temba is running on port ${port}`)
|
|
79
|
-
})
|
|
80
|
+
```
|
|
81
|
+
✅ Server listening on port 3000
|
|
80
82
|
```
|
|
81
83
|
|
|
82
84
|
### Configuration
|
|
@@ -296,18 +298,7 @@ The `responseBodyInterceptor` will only be called when the response was successf
|
|
|
296
298
|
|
|
297
299
|
### Custom router
|
|
298
300
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
```js
|
|
302
|
-
const server = temba.create()
|
|
303
|
-
|
|
304
|
-
// 🛑 Although `server` is an Express instance, the following does not work:
|
|
305
|
-
server.get('/hello', (req, res) => {
|
|
306
|
-
res.send('hello world')
|
|
307
|
-
})
|
|
308
|
-
```
|
|
309
|
-
|
|
310
|
-
The reason is that Temba is in charge of all Express routes, to make sure only _resource routes_ can be overruled by a custom router. To add your own routes, create an Express router, and configure it as a `customRouter`:
|
|
301
|
+
Because Temba uses Express under the hood, you can create an Express router, and configure it as a `customRouter`:
|
|
311
302
|
|
|
312
303
|
```js
|
|
313
304
|
// Example code of how to create an Express router, from the official Express docs at https://expressjs.com/en/guide/routing.html:
|
|
@@ -380,6 +371,7 @@ const config = {
|
|
|
380
371
|
connectionString: 'mongodb://localhost:27017',
|
|
381
372
|
customRouter: router,
|
|
382
373
|
delay: 500,
|
|
374
|
+
port: 4321,
|
|
383
375
|
requestBodyInterceptor: {
|
|
384
376
|
post: ({ resourceName, requestBody }) => {
|
|
385
377
|
// Validate, or even change the requestBody
|
|
@@ -410,6 +402,7 @@ These are all the possible settings:
|
|
|
410
402
|
| `connectionString` | See [MongoDB](#mongodb) | `null` |
|
|
411
403
|
| `customRouter` | See [Custom router](#custom-router) | `null` |
|
|
412
404
|
| `delay` | After processing the request, the delay in milliseconds before the request should be sent. | `0` |
|
|
405
|
+
| `port` | The port your Temba server listens on | `3000` |
|
|
413
406
|
| `requestBodyInterceptor` | See [Request body validation or mutation](#request-body-validation-or-mutation) | `noop` |
|
|
414
407
|
| `resourceNames` | See [Allowing specific resources only](#allowing-specific-resources-only) | `[]` |
|
|
415
408
|
| `responseBodyInterceptor` | See [Response body interception](#request-body-validation-or-mutation) | `noop` |
|
|
@@ -424,7 +417,9 @@ Although I won't promise if and when, these are some things to consider for the
|
|
|
424
417
|
|
|
425
418
|
- Better **security**, for example CORS, CSRF, etc.
|
|
426
419
|
|
|
427
|
-
- Connecting to a **SQLite** database
|
|
420
|
+
- Connecting to a **SQLite** database, see https://github.com/bouwe77/temba/issues/24
|
|
421
|
+
|
|
422
|
+
- Opt-in logging with debug-js events
|
|
428
423
|
|
|
429
424
|
- Generic **filtering and sorting**, for example: `GET /api/movies?filter=releaseYear ge 1980 and releaseYear le 1989&sort=-releaseYear,title&page=2&limit=20&fields=title,releaseYear,genre`
|
|
430
425
|
|
package/config/index.d.ts
CHANGED
|
@@ -12,6 +12,8 @@ export type Config = {
|
|
|
12
12
|
delay: number;
|
|
13
13
|
customRouter: Router;
|
|
14
14
|
returnNullFields: boolean;
|
|
15
|
+
isTesting: boolean;
|
|
16
|
+
port: number;
|
|
15
17
|
};
|
|
16
18
|
export type RouterConfig = Pick<Config, 'validateResources' | 'resourceNames' | 'apiPrefix' | 'cacheControl' | 'requestBodyInterceptor' | 'responseBodyInterceptor' | 'returnNullFields'>;
|
|
17
19
|
export type UserConfig = {
|
|
@@ -25,5 +27,7 @@ export type UserConfig = {
|
|
|
25
27
|
responseBodyInterceptor?: ResponseBodyInterceptor;
|
|
26
28
|
customRouter?: Router;
|
|
27
29
|
returnNullFields?: boolean;
|
|
30
|
+
isTesting?: boolean;
|
|
31
|
+
port?: number;
|
|
28
32
|
};
|
|
29
33
|
export declare function initConfig(userConfig: UserConfig): Config;
|
package/config/index.js
CHANGED
|
@@ -25,9 +25,11 @@ const defaultConfig = {
|
|
|
25
25
|
},
|
|
26
26
|
customRouter: null,
|
|
27
27
|
returnNullFields: true,
|
|
28
|
+
isTesting: false,
|
|
29
|
+
port: 3000,
|
|
28
30
|
};
|
|
29
31
|
function initConfig(userConfig) {
|
|
30
|
-
var _a;
|
|
32
|
+
var _a, _b, _c;
|
|
31
33
|
if (!userConfig)
|
|
32
34
|
return defaultConfig;
|
|
33
35
|
const config = Object.assign({}, defaultConfig);
|
|
@@ -75,6 +77,8 @@ function initConfig(userConfig) {
|
|
|
75
77
|
config.customRouter = userConfig.customRouter;
|
|
76
78
|
}
|
|
77
79
|
config.returnNullFields = (_a = userConfig.returnNullFields) !== null && _a !== void 0 ? _a : true;
|
|
80
|
+
config.isTesting = (_b = userConfig.isTesting) !== null && _b !== void 0 ? _b : false;
|
|
81
|
+
config.port = (_c = userConfig.port) !== null && _c !== void 0 ? _c : 3000;
|
|
78
82
|
return config;
|
|
79
83
|
}
|
|
80
84
|
exports.initConfig = initConfig;
|
package/config/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/config/index.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/config/index.ts"],"names":[],"mappings":";;;AA6CA,MAAM,aAAa,GAAW;IAC5B,aAAa,EAAE,EAAE;IACjB,iBAAiB,EAAE,KAAK;IACxB,YAAY,EAAE,IAAI;IAClB,SAAS,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AE;QAC3B,MAAM,CAAC,YAAY,GAAG,UAAU,CAAC,YAAY,CAAA;KAC9C;IAED,MAAM,CAAC,gBAAgB,GAAG,MAAA,UAAU,CAAC,gBAAgB,mCAAI,IAAI,CAAA;IAE7D,MAAM,CAAC,SAAS,GAAG,MAAA,UAAU,CAAC,SAAS,mCAAI,KAAK,CAAA;IAEhD,MAAM,CAAC,IAAI,GAAG,MAAA,UAAU,CAAC,IAAI,mCAAI,IAAI,CAAA;IAErC,OAAO,MAAM,CAAA;AACf,CAAC;AAxED,gCAwEC"}
|
package/index.d.ts
CHANGED
|
@@ -1,2 +1,5 @@
|
|
|
1
1
|
import { Config } from './config';
|
|
2
|
-
export declare function create(userConfig?: Config):
|
|
2
|
+
export declare function create(userConfig?: Config): {
|
|
3
|
+
start: () => void;
|
|
4
|
+
Express: import("express-serve-static-core").Express;
|
|
5
|
+
};
|
package/index.js
CHANGED
|
@@ -47,8 +47,6 @@ function createServer(userConfig) {
|
|
|
47
47
|
const delayMiddleware = (0, delayMiddleware_1.createDelayMiddleware)(config.delay);
|
|
48
48
|
app.use(delayMiddleware);
|
|
49
49
|
}
|
|
50
|
-
//TODO customRoutes:
|
|
51
|
-
// - Al deze routing code naar een aparte functie
|
|
52
50
|
// Serve a static folder, if configured.
|
|
53
51
|
if (config.staticFolder) {
|
|
54
52
|
app.use(express_1.default.static(config.staticFolder));
|
|
@@ -75,7 +73,19 @@ function createServer(userConfig) {
|
|
|
75
73
|
app.all('*', routes_1.handleMethodNotAllowed);
|
|
76
74
|
if (config.apiPrefix)
|
|
77
75
|
app.all(`${config.apiPrefix}*`, routes_1.handleMethodNotAllowed);
|
|
78
|
-
return
|
|
76
|
+
return {
|
|
77
|
+
start: () => {
|
|
78
|
+
if (config.isTesting) {
|
|
79
|
+
console.log('⛔️ Server not started. Remove or disable isTesting from your config.');
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
app.listen(config.port, () => {
|
|
83
|
+
console.log(`✅ Server listening on port ${config.port}`);
|
|
84
|
+
});
|
|
85
|
+
},
|
|
86
|
+
// Expose Express for testing purposes only, e.g. usage with supertest.
|
|
87
|
+
Express: config.isTesting ? app : undefined,
|
|
88
|
+
};
|
|
79
89
|
}
|
|
80
90
|
function create(userConfig) {
|
|
81
91
|
return createServer(userConfig);
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,mDAAuC;AACvC,oDAA2B;AAC3B,4CAKwB;AACxB,+CAAiD;AACjD,qCAAyD;AACzD,gDAAuB;AACvB,6DAA+D;AAE/D,SAAS,YAAY,CAAC,UAAuB;IAC3C,MAAM,MAAM,GAAG,IAAA,mBAAU,EAAC,UAAU,CAAC,CAAA;IAErC,MAAM,OAAO,GAAG,IAAA,uBAAa,EAAC,MAAM,CAAC,gBAAgB,CAAC,CAAA;IAEtD,MAAM,GAAG,GAAG,IAAA,iBAAO,GAAE,CAAA;IACrB,GAAG,CAAC,GAAG,CAAC,IAAA,cAAI,GAAE,CAAC,CAAA;IAEf,4BAA4B;IAC5B,GAAG,CAAC,GAAG,CAAC,IAAA,gBAAM,EAAC,MAAM,CAAC,CAAC,CAAA;IAEvB,gCAAgC;IAChC,GAAG,CAAC,GAAG,CAAC,IAAA,cAAI,EAAC,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC,CAAC,CAAA;IAElD,IAAI,MAAM,CAAC,KAAK,GAAG,CAAC,EAAE;QACpB,MAAM,eAAe,GAAG,IAAA,uCAAqB,EAAC,MAAM,CAAC,KAAK,CAAC,CAAA;QAC3D,GAAG,CAAC,GAAG,CAAC,eAAe,CAAC,CAAA;KACzB;IAED,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,m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