temba 0.16.0 → 0.17.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 +33 -37
- package/config/index.d.ts +3 -3
- package/config/index.js +5 -5
- package/config/index.js.map +1 -1
- package/package.json +1 -1
- package/queries/in-memory.d.ts +7 -7
- package/queries/in-memory.js +23 -23
- package/queries/in-memory.js.map +1 -1
- package/queries/mongo.d.ts +7 -7
- package/queries/mongo.js +14 -14
- package/queries/mongo.js.map +1 -1
- package/queries/queries.d.ts +14 -14
- package/routes/delete.js +4 -4
- package/routes/delete.js.map +1 -1
- package/routes/get.js +5 -5
- package/routes/get.js.map +1 -1
- package/routes/interceptors.js +9 -9
- package/routes/interceptors.js.map +1 -1
- package/routes/patch.js +7 -7
- package/routes/patch.js.map +1 -1
- package/routes/post.js +6 -6
- package/routes/post.js.map +1 -1
- package/routes/put.js +7 -7
- package/routes/put.js.map +1 -1
- package/routes/routes.js +2 -2
- package/routes/routes.js.map +1 -1
- package/routes/types.d.ts +4 -4
- package/urls/urlMiddleware.d.ts +1 -1
- package/urls/urlMiddleware.js +5 -5
- package/urls/urlMiddleware.js.map +1 -1
- package/urls/urlParser.d.ts +1 -1
- package/urls/urlParser.js +3 -3
- package/urls/urlParser.js.map +1 -1
package/README.md
CHANGED
|
@@ -75,7 +75,7 @@ const server = temba.create()
|
|
|
75
75
|
server.start()
|
|
76
76
|
```
|
|
77
77
|
|
|
78
|
-
3.In your console you'll see:
|
|
78
|
+
3. In your console you'll see:
|
|
79
79
|
|
|
80
80
|
```
|
|
81
81
|
✅ Server listening on port 3000
|
|
@@ -136,11 +136,11 @@ For every resource you use in your requests, a collection is created in the data
|
|
|
136
136
|
|
|
137
137
|
### Allowing specific resources only
|
|
138
138
|
|
|
139
|
-
If you only want to allow specific resource names, configure them by providing a `
|
|
139
|
+
If you only want to allow specific resource names, configure them by providing a `resources` key in the config object when creating the Temba server:
|
|
140
140
|
|
|
141
141
|
```js
|
|
142
142
|
const config = {
|
|
143
|
-
|
|
143
|
+
resources: ['movies', 'actors'],
|
|
144
144
|
}
|
|
145
145
|
const server = temba.create(config)
|
|
146
146
|
```
|
|
@@ -210,14 +210,14 @@ You can even omit a request body when doing a `POST`, `PATCH`, or `PUT`. If you
|
|
|
210
210
|
```js
|
|
211
211
|
const config = {
|
|
212
212
|
requestBodyInterceptor: {
|
|
213
|
-
post: ({
|
|
214
|
-
// Validate, or even change the
|
|
213
|
+
post: ({ resource, body }) => {
|
|
214
|
+
// Validate, or even change the request body
|
|
215
215
|
},
|
|
216
|
-
put: ({
|
|
217
|
-
// Validate, or even change the
|
|
216
|
+
put: ({ resource, body }) => {
|
|
217
|
+
// Validate, or even change the request body
|
|
218
218
|
},
|
|
219
|
-
patch: ({
|
|
220
|
-
// Validate, or even change the
|
|
219
|
+
patch: ({ resource, body }) => {
|
|
220
|
+
// Validate, or even change the request body
|
|
221
221
|
},
|
|
222
222
|
},
|
|
223
223
|
}
|
|
@@ -227,7 +227,7 @@ const server = temba.create(config)
|
|
|
227
227
|
|
|
228
228
|
The `requestBodyInterceptor` is an object with a `post`, and/or `patch`, and/or `put` field, which contains the callback function you want Temba to call before the JSON is saved to the database.
|
|
229
229
|
|
|
230
|
-
The callback function receives an object containing the `
|
|
230
|
+
The callback function receives an object containing the `resource`, which for example is `movies` if you request `POST /movies`, and the `body`, which is the JSON object of the request body.
|
|
231
231
|
|
|
232
232
|
Your callback function can return the following things:
|
|
233
233
|
|
|
@@ -240,13 +240,13 @@ Example:
|
|
|
240
240
|
```js
|
|
241
241
|
const config = {
|
|
242
242
|
requestBodyInterceptor: {
|
|
243
|
-
post: ({
|
|
244
|
-
// Do not allow Pokemons to be created: 400 Bad
|
|
245
|
-
if (
|
|
243
|
+
post: ({ resource, body }) => {
|
|
244
|
+
// Do not allow Pokemons to be created: 400 Bad Req best
|
|
245
|
+
if (resource === 'pokemons') return 'You are not allowed to create new Pokemons'
|
|
246
246
|
|
|
247
247
|
// Add a genre to Star Trek films:
|
|
248
|
-
if (
|
|
249
|
-
return { ...
|
|
248
|
+
if (resource === 'movies' && body.title.startsWith('Star Trek'))
|
|
249
|
+
return { ...body, genre: 'Science Fiction' }
|
|
250
250
|
|
|
251
251
|
// If you end up here, void will be returned, so the request will just be saved.
|
|
252
252
|
},
|
|
@@ -262,17 +262,17 @@ To change the response body of a `GET` request, before it's being sent to the cl
|
|
|
262
262
|
|
|
263
263
|
```js
|
|
264
264
|
const config = {
|
|
265
|
-
responseBodyInterceptor: ({
|
|
266
|
-
if (
|
|
265
|
+
responseBodyInterceptor: ({ resource, body, id }) => {
|
|
266
|
+
if (resource === 'movies') {
|
|
267
267
|
if (id) {
|
|
268
|
-
//
|
|
268
|
+
// response body is an object
|
|
269
269
|
return {
|
|
270
|
-
...
|
|
270
|
+
...body,
|
|
271
271
|
stuff: 'more stuff',
|
|
272
272
|
}
|
|
273
273
|
} else {
|
|
274
|
-
//
|
|
275
|
-
return
|
|
274
|
+
// response body is an array
|
|
275
|
+
return body.map((x) => ({
|
|
276
276
|
...x,
|
|
277
277
|
stuff: 'more stuff',
|
|
278
278
|
}))
|
|
@@ -286,9 +286,9 @@ const config = {
|
|
|
286
286
|
const server = temba.create(config)
|
|
287
287
|
```
|
|
288
288
|
|
|
289
|
-
`responseBodyInterceptor` is a callback function that provides an object containing the `
|
|
289
|
+
`responseBodyInterceptor` is a callback function that provides an object containing the `resource`, `body`, and the `id`. Depending on whether it's a collection or item request, the `body` is either an array or object, and the `id` can be `undefined`.
|
|
290
290
|
|
|
291
|
-
In the example above we check for the `id` being defined, but a runtime check to determine the type of `
|
|
291
|
+
In the example above we check for the `id` being defined, but a runtime check to determine the type of `body` would also suffice.
|
|
292
292
|
|
|
293
293
|
Whatever you return in this function will become the response body and will be serialized as JSON and returned to the client.
|
|
294
294
|
|
|
@@ -347,7 +347,7 @@ router.get('api/stuff', (req, res) => {
|
|
|
347
347
|
const config = {
|
|
348
348
|
apiPrefix: 'api',
|
|
349
349
|
customRouter: router,
|
|
350
|
-
|
|
350
|
+
resources: ['stuff'],
|
|
351
351
|
staticFolder: 'build',
|
|
352
352
|
}
|
|
353
353
|
const server = temba.create(config)
|
|
@@ -373,18 +373,18 @@ const config = {
|
|
|
373
373
|
delay: 500,
|
|
374
374
|
port: 4321,
|
|
375
375
|
requestBodyInterceptor: {
|
|
376
|
-
post: ({
|
|
377
|
-
// Validate, or even change the
|
|
376
|
+
post: ({ resource, body }) => {
|
|
377
|
+
// Validate, or even change the request body
|
|
378
378
|
},
|
|
379
|
-
patch: ({
|
|
380
|
-
// Validate, or even change the
|
|
379
|
+
patch: ({ resource, body }) => {
|
|
380
|
+
// Validate, or even change the request body
|
|
381
381
|
},
|
|
382
|
-
put: ({
|
|
383
|
-
// Validate, or even change the
|
|
382
|
+
put: ({ resource, body }) => {
|
|
383
|
+
// Validate, or even change the request body
|
|
384
384
|
},
|
|
385
385
|
},
|
|
386
|
-
|
|
387
|
-
responseBodyInterceptor: ({
|
|
386
|
+
resources: ['movies', 'actors'],
|
|
387
|
+
responseBodyInterceptor: ({ resource, body, id }) => {
|
|
388
388
|
// Change the response body before it is sent to the client
|
|
389
389
|
},
|
|
390
390
|
returnNullFields: false,
|
|
@@ -404,7 +404,7 @@ These are all the possible settings:
|
|
|
404
404
|
| `delay` | After processing the request, the delay in milliseconds before the request should be sent. | `0` |
|
|
405
405
|
| `port` | The port your Temba server listens on | `3000` |
|
|
406
406
|
| `requestBodyInterceptor` | See [Request body validation or mutation](#request-body-validation-or-mutation) | `noop` |
|
|
407
|
-
| `
|
|
407
|
+
| `resources` | See [Allowing specific resources only](#allowing-specific-resources-only) | `[]` |
|
|
408
408
|
| `responseBodyInterceptor` | See [Response body interception](#request-body-validation-or-mutation) | `noop` |
|
|
409
409
|
| `returnNullFields` | Whether fields with a `null` value should be returned in responses. | `true` |
|
|
410
410
|
| `staticFolder` | See [Static assets](#static-assets) | `null` |
|
|
@@ -417,16 +417,12 @@ Although I won't promise if and when, these are some things to consider for the
|
|
|
417
417
|
|
|
418
418
|
- Better **security**, for example CORS, CSRF, etc.
|
|
419
419
|
|
|
420
|
-
- Connecting to a **SQLite** database, see https://github.com/bouwe77/temba/issues/24
|
|
421
|
-
|
|
422
420
|
- Opt-in logging with debug-js events
|
|
423
421
|
|
|
424
422
|
- 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`
|
|
425
423
|
|
|
426
424
|
- Intial data seed when using in-memory.
|
|
427
425
|
|
|
428
|
-
- Get rid of Express?
|
|
429
|
-
|
|
430
426
|
## Under the hood
|
|
431
427
|
|
|
432
428
|
Temba is built with JavaScript, [Node](https://nodejs.org), [Express](https://expressjs.com/), [Jest](https://jestjs.io/), [Supertest](https://www.npmjs.com/package/supertest), and [@rakered/mongo](https://www.npmjs.com/package/@rakered/mongo).
|
package/config/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { Router } from 'express';
|
|
|
2
2
|
import { RequestBodyInterceptor, ResponseBodyInterceptor } from '../routes/types';
|
|
3
3
|
export type Config = {
|
|
4
4
|
validateResources: boolean;
|
|
5
|
-
|
|
5
|
+
resources: string[];
|
|
6
6
|
apiPrefix: string;
|
|
7
7
|
cacheControl: string;
|
|
8
8
|
requestBodyInterceptor: RequestBodyInterceptor;
|
|
@@ -15,9 +15,9 @@ export type Config = {
|
|
|
15
15
|
isTesting: boolean;
|
|
16
16
|
port: number;
|
|
17
17
|
};
|
|
18
|
-
export type RouterConfig = Pick<Config, 'validateResources' | '
|
|
18
|
+
export type RouterConfig = Pick<Config, 'validateResources' | 'resources' | 'apiPrefix' | 'cacheControl' | 'requestBodyInterceptor' | 'responseBodyInterceptor' | 'returnNullFields'>;
|
|
19
19
|
export type UserConfig = {
|
|
20
|
-
|
|
20
|
+
resources?: string[];
|
|
21
21
|
staticFolder?: string;
|
|
22
22
|
apiPrefix?: string;
|
|
23
23
|
connectionString?: string;
|
package/config/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.initConfig = void 0;
|
|
4
4
|
const defaultConfig = {
|
|
5
|
-
|
|
5
|
+
resources: [],
|
|
6
6
|
validateResources: false,
|
|
7
7
|
staticFolder: null,
|
|
8
8
|
apiPrefix: '',
|
|
@@ -20,8 +20,8 @@ const defaultConfig = {
|
|
|
20
20
|
// do nothing
|
|
21
21
|
},
|
|
22
22
|
},
|
|
23
|
-
responseBodyInterceptor: ({
|
|
24
|
-
return
|
|
23
|
+
responseBodyInterceptor: ({ body }) => {
|
|
24
|
+
return body;
|
|
25
25
|
},
|
|
26
26
|
customRouter: null,
|
|
27
27
|
returnNullFields: true,
|
|
@@ -33,8 +33,8 @@ function initConfig(userConfig) {
|
|
|
33
33
|
if (!userConfig)
|
|
34
34
|
return defaultConfig;
|
|
35
35
|
const config = Object.assign({}, defaultConfig);
|
|
36
|
-
if (userConfig.
|
|
37
|
-
config.
|
|
36
|
+
if (userConfig.resources && userConfig.resources.length > 0) {
|
|
37
|
+
config.resources = userConfig.resources;
|
|
38
38
|
config.validateResources = true;
|
|
39
39
|
}
|
|
40
40
|
if (userConfig.staticFolder) {
|
package/config/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/config/index.ts"],"names":[],"mappings":";;;AA6CA,MAAM,aAAa,GAAW;IAC5B,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/config/index.ts"],"names":[],"mappings":";;;AA6CA,MAAM,aAAa,GAAW;IAC5B,SAAS,EAAE,EAAE;IACb,iBAAiB,EAAE,KAAK;IACxB,YAAY,EAAE,IAAI;IAClB,SAAS,EAAE,EAAE;IACb,gBAAgB,EAAE,IAAI;IACtB,YAAY,EAAE,UAAU;IACxB,KAAK,EAAE,CAAC;IACR,sBAAsB,EAAE;QACtB,IAAI,EAAE,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CAAI,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/package.json
CHANGED
package/queries/in-memory.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
declare function connectToDatabase(): void;
|
|
2
|
-
declare function getAll(
|
|
3
|
-
declare function getById(
|
|
4
|
-
declare function create(
|
|
5
|
-
declare function update(
|
|
6
|
-
declare function replace(
|
|
7
|
-
declare function deleteById(
|
|
8
|
-
declare function deleteAll(
|
|
2
|
+
declare function getAll(resource: any): Promise<unknown>;
|
|
3
|
+
declare function getById(resource: any, id: any): Promise<unknown>;
|
|
4
|
+
declare function create(resource: any, item: any): Promise<unknown>;
|
|
5
|
+
declare function update(resource: any, item: any): Promise<unknown>;
|
|
6
|
+
declare function replace(resource: any, item: any): Promise<unknown>;
|
|
7
|
+
declare function deleteById(resource: any, id: any): Promise<void>;
|
|
8
|
+
declare function deleteAll(resource: any): Promise<unknown>;
|
|
9
9
|
declare const _default: {
|
|
10
10
|
connectToDatabase: typeof connectToDatabase;
|
|
11
11
|
getAll: typeof getAll;
|
package/queries/in-memory.js
CHANGED
|
@@ -4,54 +4,54 @@ const data = {};
|
|
|
4
4
|
function connectToDatabase() {
|
|
5
5
|
// do nothing
|
|
6
6
|
}
|
|
7
|
-
function getAll(
|
|
8
|
-
createResourceArrayIfNecessary(
|
|
7
|
+
function getAll(resource) {
|
|
8
|
+
createResourceArrayIfNecessary(resource);
|
|
9
9
|
return new Promise((resolve) => {
|
|
10
|
-
resolve(data[
|
|
10
|
+
resolve(data[resource]);
|
|
11
11
|
});
|
|
12
12
|
}
|
|
13
|
-
function getById(
|
|
14
|
-
createResourceArrayIfNecessary(
|
|
13
|
+
function getById(resource, id) {
|
|
14
|
+
createResourceArrayIfNecessary(resource);
|
|
15
15
|
return new Promise((resolve) => {
|
|
16
|
-
resolve(data[
|
|
16
|
+
resolve(data[resource].find((item) => item.id === id));
|
|
17
17
|
});
|
|
18
18
|
}
|
|
19
|
-
function create(
|
|
20
|
-
createResourceArrayIfNecessary(
|
|
19
|
+
function create(resource, item) {
|
|
20
|
+
createResourceArrayIfNecessary(resource);
|
|
21
21
|
const newItem = Object.assign(Object.assign({}, item), { id: String(new Date().getTime()) });
|
|
22
|
-
data[
|
|
22
|
+
data[resource] = [...data[resource], newItem];
|
|
23
23
|
return new Promise((resolve) => {
|
|
24
24
|
resolve(newItem);
|
|
25
25
|
});
|
|
26
26
|
}
|
|
27
|
-
function update(
|
|
28
|
-
createResourceArrayIfNecessary(
|
|
27
|
+
function update(resource, item) {
|
|
28
|
+
createResourceArrayIfNecessary(resource);
|
|
29
29
|
const updatedItem = Object.assign({}, item);
|
|
30
|
-
data[
|
|
30
|
+
data[resource] = [...data[resource].filter((r) => r.id !== item.id), updatedItem];
|
|
31
31
|
return new Promise((resolve) => {
|
|
32
32
|
resolve(updatedItem);
|
|
33
33
|
});
|
|
34
34
|
}
|
|
35
|
-
function replace(
|
|
36
|
-
return update(
|
|
35
|
+
function replace(resource, item) {
|
|
36
|
+
return update(resource, item);
|
|
37
37
|
}
|
|
38
|
-
function deleteById(
|
|
39
|
-
createResourceArrayIfNecessary(
|
|
40
|
-
data[
|
|
38
|
+
function deleteById(resource, id) {
|
|
39
|
+
createResourceArrayIfNecessary(resource);
|
|
40
|
+
data[resource] = data[resource].filter((item) => item.id !== id);
|
|
41
41
|
return new Promise((resolve) => {
|
|
42
42
|
resolve();
|
|
43
43
|
});
|
|
44
44
|
}
|
|
45
|
-
function deleteAll(
|
|
46
|
-
createResourceArrayIfNecessary(
|
|
47
|
-
data[
|
|
45
|
+
function deleteAll(resource) {
|
|
46
|
+
createResourceArrayIfNecessary(resource);
|
|
47
|
+
data[resource] = [];
|
|
48
48
|
return new Promise((resolve) => {
|
|
49
49
|
resolve([]);
|
|
50
50
|
});
|
|
51
51
|
}
|
|
52
|
-
function createResourceArrayIfNecessary(
|
|
53
|
-
if (!data.hasOwnProperty(
|
|
54
|
-
data[
|
|
52
|
+
function createResourceArrayIfNecessary(resource) {
|
|
53
|
+
if (!data.hasOwnProperty(resource))
|
|
54
|
+
data[resource] = [];
|
|
55
55
|
}
|
|
56
56
|
exports.default = {
|
|
57
57
|
connectToDatabase,
|
package/queries/in-memory.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"in-memory.js","sourceRoot":"","sources":["../../src/queries/in-memory.ts"],"names":[],"mappings":";;AAAA,MAAM,IAAI,GAAG,EAAE,CAAA;AAEf,SAAS,iBAAiB;IACxB,aAAa;AACf,CAAC;AAED,SAAS,MAAM,CAAC,
|
|
1
|
+
{"version":3,"file":"in-memory.js","sourceRoot":"","sources":["../../src/queries/in-memory.ts"],"names":[],"mappings":";;AAAA,MAAM,IAAI,GAAG,EAAE,CAAA;AAEf,SAAS,iBAAiB;IACxB,aAAa;AACf,CAAC;AAED,SAAS,MAAM,CAAC,QAAQ;IACtB,8BAA8B,CAAC,QAAQ,CAAC,CAAA;IAExC,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC7B,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAA;IACzB,CAAC,CAAC,CAAA;AACJ,CAAC;AAED,SAAS,OAAO,CAAC,QAAQ,EAAE,EAAE;IAC3B,8BAA8B,CAAC,QAAQ,CAAC,CAAA;IAExC,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC7B,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,CAAA;IACxD,CAAC,CAAC,CAAA;AACJ,CAAC;AAED,SAAS,MAAM,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O,CAAO,CAAC,OAAO,EAAE,EAAE;QACnC,OAAO,EAAE,CAAA;IACX,CAAC,CAAC,CAAA;AACJ,CAAC;AAED,SAAS,SAAS,CAAC,QAAQ;IACzB,8BAA8B,CAAC,QAAQ,CAAC,CAAA;IAExC,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAA;IACnB,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC7B,OAAO,CAAC,EAAE,CAAC,CAAA;IACb,CAAC,CAAC,CAAA;AACJ,CAAC;AAED,SAAS,8BAA8B,CAAC,QAAQ;IAC9C,IAAI,CAAC,IAAI,CAAC,cAAc,CAAC,QAAQ,CAAC;QAAE,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAA;AACzD,CAAC;AAED,kBAAe;IACb,iBAAiB;IACjB,MAAM;IACN,OAAO;IACP,MAAM;IACN,MAAM;IACN,OAAO;IACP,UAAU;IACV,SAAS;CACV,CAAA"}
|
package/queries/mongo.d.ts
CHANGED
|
@@ -9,11 +9,11 @@ export default function createMongoQueries(connectionString: any): {
|
|
|
9
9
|
deleteAll: typeof deleteAll;
|
|
10
10
|
};
|
|
11
11
|
declare function connectToDatabase(): Promise<void>;
|
|
12
|
-
declare function getAll(
|
|
13
|
-
declare function getById(
|
|
14
|
-
declare function create(
|
|
15
|
-
declare function update(
|
|
16
|
-
declare function replace(
|
|
17
|
-
declare function deleteById(
|
|
18
|
-
declare function deleteAll(
|
|
12
|
+
declare function getAll(resource: any): Promise<any>;
|
|
13
|
+
declare function getById(resource: any, id: any): Promise<any>;
|
|
14
|
+
declare function create(resource: any, item: any): Promise<any>;
|
|
15
|
+
declare function update(resource: any, item: any): Promise<any>;
|
|
16
|
+
declare function replace(resource: any, item: any): Promise<any>;
|
|
17
|
+
declare function deleteById(resource: any, id: any): Promise<void>;
|
|
18
|
+
declare function deleteAll(resource: any): Promise<void>;
|
|
19
19
|
export {};
|
package/queries/mongo.js
CHANGED
|
@@ -41,61 +41,61 @@ function connectToDatabase() {
|
|
|
41
41
|
}
|
|
42
42
|
});
|
|
43
43
|
}
|
|
44
|
-
function getAll(
|
|
44
|
+
function getAll(resource) {
|
|
45
45
|
return __awaiter(this, void 0, void 0, function* () {
|
|
46
46
|
yield connectToDatabase();
|
|
47
|
-
const items = yield db[
|
|
47
|
+
const items = yield db[resource].find({});
|
|
48
48
|
if (!items)
|
|
49
49
|
return [];
|
|
50
50
|
return items.map((item) => removeUnderscoreFromId(item));
|
|
51
51
|
});
|
|
52
52
|
}
|
|
53
|
-
function getById(
|
|
53
|
+
function getById(resource, id) {
|
|
54
54
|
return __awaiter(this, void 0, void 0, function* () {
|
|
55
55
|
yield connectToDatabase();
|
|
56
|
-
const item = yield db[
|
|
56
|
+
const item = yield db[resource].findOne({ _id: id });
|
|
57
57
|
if (!item)
|
|
58
58
|
return null;
|
|
59
59
|
return removeUnderscoreFromId(item);
|
|
60
60
|
});
|
|
61
61
|
}
|
|
62
|
-
function create(
|
|
62
|
+
function create(resource, item) {
|
|
63
63
|
return __awaiter(this, void 0, void 0, function* () {
|
|
64
64
|
yield connectToDatabase();
|
|
65
|
-
const createdItem = yield db[
|
|
65
|
+
const createdItem = yield db[resource].insertOne(item);
|
|
66
66
|
return removeUnderscoreFromId(createdItem.ops[0]);
|
|
67
67
|
});
|
|
68
68
|
}
|
|
69
|
-
function update(
|
|
69
|
+
function update(resource, item) {
|
|
70
70
|
return __awaiter(this, void 0, void 0, function* () {
|
|
71
71
|
yield connectToDatabase();
|
|
72
72
|
const id = item.id;
|
|
73
73
|
delete item.id;
|
|
74
|
-
const updatedItem = yield db[
|
|
74
|
+
const updatedItem = yield db[resource].findOneAndUpdate({ _id: id }, { $set: item }, { returnOriginal: false });
|
|
75
75
|
return removeUnderscoreFromId(updatedItem.value);
|
|
76
76
|
});
|
|
77
77
|
}
|
|
78
|
-
function replace(
|
|
78
|
+
function replace(resource, item) {
|
|
79
79
|
return __awaiter(this, void 0, void 0, function* () {
|
|
80
80
|
yield connectToDatabase();
|
|
81
81
|
const id = item.id;
|
|
82
82
|
delete item.id;
|
|
83
|
-
const replacedItem = yield db[
|
|
83
|
+
const replacedItem = yield db[resource].findOneAndReplace({ _id: id }, item, {
|
|
84
84
|
returnOriginal: false,
|
|
85
85
|
});
|
|
86
86
|
return removeUnderscoreFromId(replacedItem.value);
|
|
87
87
|
});
|
|
88
88
|
}
|
|
89
|
-
function deleteById(
|
|
89
|
+
function deleteById(resource, id) {
|
|
90
90
|
return __awaiter(this, void 0, void 0, function* () {
|
|
91
91
|
yield connectToDatabase();
|
|
92
|
-
yield db[
|
|
92
|
+
yield db[resource].deleteOne({ _id: id });
|
|
93
93
|
});
|
|
94
94
|
}
|
|
95
|
-
function deleteAll(
|
|
95
|
+
function deleteAll(resource) {
|
|
96
96
|
return __awaiter(this, void 0, void 0, function* () {
|
|
97
97
|
yield connectToDatabase();
|
|
98
|
-
yield db[
|
|
98
|
+
yield db[resource].deleteMany({});
|
|
99
99
|
});
|
|
100
100
|
}
|
|
101
101
|
function removeUnderscoreFromId(item) {
|
package/queries/mongo.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mongo.js","sourceRoot":"","sources":["../../src/queries/mongo.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,0CAAwC;AAExC,IAAI,GAAG,CAAA;AACP,IAAI,EAAE,CAAA;AAEN,SAAwB,kBAAkB,CAAC,gBAAgB;IACzD,GAAG,GAAG,gBAAgB,CAAA;IAEtB,OAAO;QACL,iBAAiB;QACjB,MAAM;QACN,OAAO;QACP,MAAM;QACN,MAAM;QACN,OAAO;QACP,UAAU;QACV,SAAS;KACV,CAAA;AACH,CAAC;AAbD,qCAaC;AAED,SAAe,iBAAiB;;QAC9B,IAAI,CAAC,EAAE,EAAE;YACP,OAAO,CAAC,GAAG,CAAC,0BAA0B,CAAC,CAAA;YACvC,IAAI;gBACF,EAAE,GAAG,MAAM,IAAA,eAAO,EAAC,GAAG,CAAC,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A;QAEd,MAAM,YAAY,GAAG,MAAM,EAAE,CAAC,QAAQ,CAAC,CAAC,iBAAiB,CAAC,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE;YAC3E,cAAc,EAAE,KAAK;SACtB,CAAC,CAAA;QAEF,OAAO,sBAAsB,CAAC,YAAY,CAAC,KAAK,CAAC,CAAA;IACnD,CAAC;CAAA;AAED,SAAe,UAAU,CAAC,QAAQ,EAAE,EAAE;;QACpC,MAAM,iBAAiB,EAAE,CAAA;QAEzB,MAAM,EAAE,CAAC,QAAQ,CAAC,CAAC,SAAS,CAAC,EAAE,GAAG,EAAE,EAAE,EAAE,CAAC,CAAA;IAC3C,CAAC;CAAA;AAED,SAAe,SAAS,CAAC,QAAQ;;QAC/B,MAAM,iBAAiB,EAAE,CAAA;QAEzB,MAAM,EAAE,CAAC,QAAQ,CAAC,CAAC,UAAU,CAAC,EAAE,CAAC,CAAA;IACnC,CAAC;CAAA;AAED,SAAS,sBAAsB,CAAC,IAAI;IAClC,MAAM,WAAW,mCAAQ,IAAI,KAAE,EAAE,EAAE,IAAI,CAAC,GAAG,GAAE,CAAA;IAC7C,OAAO,WAAW,CAAC,GAAG,CAAA;IACtB,OAAO,WAAW,CAAA;AACpB,CAAC"}
|
package/queries/queries.d.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
declare function createQueries(connectionString: any): {
|
|
2
2
|
connectToDatabase: () => Promise<void>;
|
|
3
|
-
getAll: (
|
|
4
|
-
getById: (
|
|
5
|
-
create: (
|
|
6
|
-
update: (
|
|
7
|
-
replace: (
|
|
8
|
-
deleteById: (
|
|
9
|
-
deleteAll: (
|
|
3
|
+
getAll: (resource: any) => Promise<any>;
|
|
4
|
+
getById: (resource: any, id: any) => Promise<any>;
|
|
5
|
+
create: (resource: any, item: any) => Promise<any>;
|
|
6
|
+
update: (resource: any, item: any) => Promise<any>;
|
|
7
|
+
replace: (resource: any, item: any) => Promise<any>;
|
|
8
|
+
deleteById: (resource: any, id: any) => Promise<void>;
|
|
9
|
+
deleteAll: (resource: any) => Promise<void>;
|
|
10
10
|
} | {
|
|
11
11
|
connectToDatabase: () => void;
|
|
12
|
-
getAll: (
|
|
13
|
-
getById: (
|
|
14
|
-
create: (
|
|
15
|
-
update: (
|
|
16
|
-
replace: (
|
|
17
|
-
deleteById: (
|
|
18
|
-
deleteAll: (
|
|
12
|
+
getAll: (resource: any) => Promise<unknown>;
|
|
13
|
+
getById: (resource: any, id: any) => Promise<unknown>;
|
|
14
|
+
create: (resource: any, item: any) => Promise<unknown>;
|
|
15
|
+
update: (resource: any, item: any) => Promise<unknown>;
|
|
16
|
+
replace: (resource: any, item: any) => Promise<unknown>;
|
|
17
|
+
deleteById: (resource: any, id: any) => Promise<void>;
|
|
18
|
+
deleteAll: (resource: any) => Promise<unknown>;
|
|
19
19
|
};
|
|
20
20
|
export { createQueries };
|
package/routes/delete.js
CHANGED
|
@@ -14,15 +14,15 @@ function createDeleteRoutes(queries) {
|
|
|
14
14
|
function handleDelete(req, res) {
|
|
15
15
|
return __awaiter(this, void 0, void 0, function* () {
|
|
16
16
|
try {
|
|
17
|
-
const {
|
|
17
|
+
const { resource, id } = req.requestInfo;
|
|
18
18
|
if (id) {
|
|
19
|
-
const item = yield queries.getById(
|
|
19
|
+
const item = yield queries.getById(resource, id);
|
|
20
20
|
if (item) {
|
|
21
|
-
yield queries.deleteById(
|
|
21
|
+
yield queries.deleteById(resource, id);
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
else {
|
|
25
|
-
yield queries.deleteAll(
|
|
25
|
+
yield queries.deleteAll(resource);
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
catch (error) {
|
package/routes/delete.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"delete.js","sourceRoot":"","sources":["../../src/routes/delete.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,SAAS,kBAAkB,CAAC,OAAO;IACjC,SAAe,YAAY,CAAC,GAAG,EAAE,GAAG;;YAClC,IAAI;gBACF,MAAM,EAAE,
|
|
1
|
+
{"version":3,"file":"delete.js","sourceRoot":"","sources":["../../src/routes/delete.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,SAAS,kBAAkB,CAAC,OAAO;IACjC,SAAe,YAAY,CAAC,GAAG,EAAE,GAAG;;YAClC,IAAI;gBACF,MAAM,EAAE,QAAQ,EAAE,EAAE,EAAE,GAAG,GAAG,CAAC,WAAW,CAAA;gBAExC,IAAI,EAAE,EAAE;oBACN,MAAM,IAAI,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAA;oBAChD,IAAI,IAAI,EAAE;wBACR,MAAM,OAAO,CAAC,UAAU,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAA;qBACvC;iBACF;qBAAM;oBACL,MAAM,OAAO,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAA;iBAClC;aACF;YAAC,OAAO,KAAc,EAAE;gBACvB,OAAO,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAG,KAAe,CAAC,OAAO,EAAE,CAAC,CAAA;aACnE;YAED,OAAO,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAA;QAC/B,CAAC;KAAA;IAED,OAAO;QACL,YAAY;KACb,CAAA;AACH,CAAC;AAEQ,gDAAkB"}
|
package/routes/get.js
CHANGED
|
@@ -15,10 +15,10 @@ function createGetRoutes(queries, cacheControl, responseBodyInterceptor, returnN
|
|
|
15
15
|
function handleGetResource(req, res) {
|
|
16
16
|
return __awaiter(this, void 0, void 0, function* () {
|
|
17
17
|
try {
|
|
18
|
-
const {
|
|
18
|
+
const { resource, id } = req.requestInfo;
|
|
19
19
|
res.set('Cache-control', cacheControl);
|
|
20
20
|
if (id) {
|
|
21
|
-
const item = yield queries.getById(
|
|
21
|
+
const item = yield queries.getById(resource, id);
|
|
22
22
|
if (!item) {
|
|
23
23
|
res.status(404);
|
|
24
24
|
return res.send();
|
|
@@ -26,7 +26,7 @@ function createGetRoutes(queries, cacheControl, responseBodyInterceptor, returnN
|
|
|
26
26
|
let theItem = item;
|
|
27
27
|
if (responseBodyInterceptor) {
|
|
28
28
|
try {
|
|
29
|
-
theItem = responseBodyInterceptor({
|
|
29
|
+
theItem = responseBodyInterceptor({ resource, body: item, id });
|
|
30
30
|
if (!theItem)
|
|
31
31
|
theItem = item;
|
|
32
32
|
}
|
|
@@ -40,11 +40,11 @@ function createGetRoutes(queries, cacheControl, responseBodyInterceptor, returnN
|
|
|
40
40
|
res.json(returnNullFields ? theItem : (0, utils_1.removeNullFields)(theItem));
|
|
41
41
|
return res.send();
|
|
42
42
|
}
|
|
43
|
-
const items = yield queries.getAll(
|
|
43
|
+
const items = yield queries.getAll(resource);
|
|
44
44
|
let theItems = items;
|
|
45
45
|
if (responseBodyInterceptor) {
|
|
46
46
|
try {
|
|
47
|
-
theItems = responseBodyInterceptor({
|
|
47
|
+
theItems = responseBodyInterceptor({ resource, body: items });
|
|
48
48
|
if (!theItems)
|
|
49
49
|
theItems = items;
|
|
50
50
|
}
|
package/routes/get.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"get.js","sourceRoot":"","sources":["../../src/routes/get.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,mCAA0C;AAE1C,SAAS,eAAe,CAAC,OAAO,EAAE,YAAY,EAAE,uBAAuB,EAAE,gBAAgB;IACvF,SAAe,iBAAiB,CAAC,GAAG,EAAE,GAAG;;YACvC,IAAI;gBACF,MAAM,EAAE,
|
|
1
|
+
{"version":3,"file":"get.js","sourceRoot":"","sources":["../../src/routes/get.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,mCAA0C;AAE1C,SAAS,eAAe,CAAC,OAAO,EAAE,YAAY,EAAE,uBAAuB,EAAE,gBAAgB;IACvF,SAAe,iBAAiB,CAAC,GAAG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AAC,EAAE,QAAQ,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAA;wBAC7D,IAAI,CAAC,QAAQ;4BAAE,QAAQ,GAAG,KAAK,CAAA;qBAChC;oBAAC,OAAO,KAAK,EAAE;wBACd,OAAO,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;4BAC1B,OAAO,EAAE,oCAAoC,GAAG,KAAK,CAAC,OAAO;yBAC9D,CAAC,CAAA;qBACH;iBACF;gBAED,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;gBACf,GAAG,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAA,wBAAgB,EAAC,IAAI,CAAC,CAAC,CAAC,CAAA;gBACtF,OAAO,GAAG,CAAC,IAAI,EAAE,CAAA;aAClB;YAAC,OAAO,KAAc,EAAE;gBACvB,OAAO,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAG,KAAe,CAAC,OAAO,EAAE,CAAC,CAAA;aACnE;QACH,CAAC;KAAA;IAED,OAAO;QACL,iBAAiB;KAClB,CAAA;AACH,CAAC;AAEQ,0CAAe"}
|
package/routes/interceptors.js
CHANGED
|
@@ -2,18 +2,18 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.interceptRequestBody = void 0;
|
|
4
4
|
function interceptRequestBody(intercept, req) {
|
|
5
|
-
const {
|
|
6
|
-
let
|
|
7
|
-
const validationResult = intercept({
|
|
8
|
-
if (!validationResult && typeof
|
|
9
|
-
return
|
|
5
|
+
const { resource } = req.requestInfo;
|
|
6
|
+
let body = req.body;
|
|
7
|
+
const validationResult = intercept({ resource, body });
|
|
8
|
+
if (!validationResult && typeof body === 'object')
|
|
9
|
+
return body;
|
|
10
10
|
if (typeof validationResult === 'string')
|
|
11
11
|
return validationResult;
|
|
12
|
-
// The
|
|
12
|
+
// The request body was replaced by something else.
|
|
13
13
|
if (validationResult)
|
|
14
|
-
|
|
15
|
-
if (typeof
|
|
16
|
-
return
|
|
14
|
+
body = validationResult;
|
|
15
|
+
if (typeof body === 'object') {
|
|
16
|
+
return body;
|
|
17
17
|
}
|
|
18
18
|
else
|
|
19
19
|
return req.body;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interceptors.js","sourceRoot":"","sources":["../../src/routes/interceptors.ts"],"names":[],"mappings":";;;AAEA,SAAS,oBAAoB,CAAC,SAAyC,EAAE,GAAG;IAC1E,MAAM,EAAE,
|
|
1
|
+
{"version":3,"file":"interceptors.js","sourceRoot":"","sources":["../../src/routes/interceptors.ts"],"names":[],"mappings":";;;AAEA,SAAS,oBAAoB,CAAC,SAAyC,EAAE,GAAG;IAC1E,MAAM,EAAE,QAAQ,EAAE,GAAG,GAAG,CAAC,WAAW,CAAA;IACpC,IAAI,IAAI,GAAG,GAAG,CAAC,IAAI,CAAA;IAEnB,MAAM,gBAAgB,GAAG,SAAS,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAA;IAEtD,IAAI,CAAC,gBAAgB,IAAI,OAAO,IAAI,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAA;IAE9D,IAAI,OAAO,gBAAgB,KAAK,QAAQ;QAAE,OAAO,gBAAgB,CAAA;IAEjE,mDAAmD;IACnD,IAAI,gBAAgB;QAAE,IAAI,GAAG,gBAAgB,CAAA;IAE7C,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE;QAC5B,OAAO,IAAI,CAAA;KACZ;;QAAM,OAAO,GAAG,CAAC,IAAI,CAAA;AACxB,CAAC;AAEQ,oDAAoB"}
|
package/routes/patch.js
CHANGED
|
@@ -16,19 +16,19 @@ function createPatchRoutes(queries, requestBodyInterceptor, returnNullFields) {
|
|
|
16
16
|
function handlePatch(req, res) {
|
|
17
17
|
return __awaiter(this, void 0, void 0, function* () {
|
|
18
18
|
try {
|
|
19
|
-
const {
|
|
20
|
-
const
|
|
21
|
-
if (typeof
|
|
22
|
-
return res.status(400).json({ message:
|
|
19
|
+
const { resource, id } = req.requestInfo;
|
|
20
|
+
const body = (0, interceptors_1.interceptRequestBody)(requestBodyInterceptor.patch, req);
|
|
21
|
+
if (typeof body === 'string')
|
|
22
|
+
return res.status(400).json({ message: body }).send();
|
|
23
23
|
let item = null;
|
|
24
24
|
if (id)
|
|
25
|
-
item = yield queries.getById(
|
|
25
|
+
item = yield queries.getById(resource, id);
|
|
26
26
|
if (!item)
|
|
27
27
|
return res.status(404).json({
|
|
28
28
|
message: `ID '${id}' not found`,
|
|
29
29
|
});
|
|
30
|
-
item = Object.assign(Object.assign(Object.assign({}, item),
|
|
31
|
-
const updatedItem = yield queries.update(
|
|
30
|
+
item = Object.assign(Object.assign(Object.assign({}, item), body), { id });
|
|
31
|
+
const updatedItem = yield queries.update(resource, item);
|
|
32
32
|
return res
|
|
33
33
|
.status(200)
|
|
34
34
|
.json(returnNullFields ? updatedItem : (0, utils_1.removeNullFields)(updatedItem))
|
package/routes/patch.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"patch.js","sourceRoot":"","sources":["../../src/routes/patch.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,iDAAqD;AACrD,mCAA0C;AAE1C,SAAS,iBAAiB,CAAC,OAAO,EAAE,sBAAsB,EAAE,gBAAgB;IAC1E,SAAe,WAAW,CAAC,GAAG,EAAE,GAAG;;YACjC,IAAI;gBACF,MAAM,EAAE,
|
|
1
|
+
{"version":3,"file":"patch.js","sourceRoot":"","sources":["../../src/routes/patch.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,iDAAqD;AACrD,mCAA0C;AAE1C,SAAS,iBAAiB,CAAC,OAAO,EAAE,sBAAsB,EAAE,gBAAgB;IAC1E,SAAe,WAAW,CAAC,GAAG,EAAE,GAAG;;YAC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,17 @@ function createPostRoutes(queries, requestBodyInterceptor, returnNullFields) {
|
|
|
17
17
|
function handlePost(req, res) {
|
|
18
18
|
return __awaiter(this, void 0, void 0, function* () {
|
|
19
19
|
try {
|
|
20
|
-
const {
|
|
21
|
-
const
|
|
22
|
-
if (typeof
|
|
23
|
-
return res.status(400).json({ message:
|
|
24
|
-
const newItem = yield queries.create(
|
|
20
|
+
const { resource } = req.requestInfo;
|
|
21
|
+
const body = (0, interceptors_1.interceptRequestBody)(requestBodyInterceptor.post, req);
|
|
22
|
+
if (typeof body === 'string')
|
|
23
|
+
return res.status(400).json({ message: body }).send();
|
|
24
|
+
const newItem = yield queries.create(resource, body);
|
|
25
25
|
return res
|
|
26
26
|
.set({
|
|
27
27
|
Location: (0, url_1.format)({
|
|
28
28
|
protocol: req.protocol,
|
|
29
29
|
host: req.get('host'),
|
|
30
|
-
pathname: `${
|
|
30
|
+
pathname: `${resource}/${newItem.id}`,
|
|
31
31
|
}),
|
|
32
32
|
})
|
|
33
33
|
.status(201)
|
package/routes/post.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"post.js","sourceRoot":"","sources":["../../src/routes/post.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,6BAA4B;AAC5B,iDAAqD;AACrD,mCAA0C;AAE1C,SAAS,gBAAgB,CAAC,OAAO,EAAE,sBAAsB,EAAE,gBAAgB;IACzE,SAAe,UAAU,CAAC,GAAG,EAAE,GAAG;;YAChC,IAAI;gBACF,MAAM,EAAE,
|
|
1
|
+
{"version":3,"file":"post.js","sourceRoot":"","sources":["../../src/routes/post.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,6BAA4B;AAC5B,iDAAqD;AACrD,mCAA0C;AAE1C,SAAS,gBAAgB,CAAC,OAAO,EAAE,sBAAsB,EAAE,gBAAgB;IACzE,SAAe,UAAU,CAAC,GAAG,EAAE,GAAG;;YAChC,IAAI;gBACF,MAAM,EAAE,QAAQ,EAAE,GAAG,GAAG,CAAC,WAAW,CAAA;gBAEpC,MAAM,IAAI,GAAG,IAAA,mCAAoB,EAAC,sBAAsB,CAAC,IAAI,EAAE,GAAG,CAAC,CAAA;gBAEnE,IAAI,OAAO,IAAI,KAAK,QAAQ;oBAAE,OAAO,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAA;gBAEnF,MAAM,OAAO,GAAG,MAAM,OAAO,CAAC,MAAM,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAA;gBAEpD,OAAO,GAAG;qBACP,GAAG,CAAC;oBACH,QAAQ,EAAE,IAAA,YAAM,EAAC;wBACf,QAAQ,EAAE,GAAG,CAAC,QAAQ;wBACtB,IAAI,EAAE,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC;wBACrB,QAAQ,EAAE,GAAG,QAAQ,IAAI,OAAO,CAAC,EAAE,EAAE;qBACtC,CAAC;iBACH,CAAC;qBACD,MAAM,CAAC,GAAG,CAAC;qBACX,IAAI,CAAC,gBAAgB,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAA,wBAAgB,EAAC,OAAO,CAAC,CAAC;qBAC5D,IAAI,EAAE,CAAA;aACV;YAAC,OAAO,KAAc,EAAE;gBACvB,OAAO,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAG,KAAe,CAAC,OAAO,EAAE,CAAC,CAAA;aACnE;QACH,CAAC;KAAA;IAED,OAAO;QACL,UAAU;KACX,CAAA;AACH,CAAC;AAEQ,4CAAgB"}
|
package/routes/put.js
CHANGED
|
@@ -16,19 +16,19 @@ function createPutRoutes(queries, requestBodyInterceptor, returnNullFields) {
|
|
|
16
16
|
function handlePut(req, res) {
|
|
17
17
|
return __awaiter(this, void 0, void 0, function* () {
|
|
18
18
|
try {
|
|
19
|
-
const {
|
|
20
|
-
const
|
|
21
|
-
if (typeof
|
|
22
|
-
return res.status(400).json({ message:
|
|
19
|
+
const { resource, id } = req.requestInfo;
|
|
20
|
+
const body = (0, interceptors_1.interceptRequestBody)(requestBodyInterceptor.put, req);
|
|
21
|
+
if (typeof body === 'string')
|
|
22
|
+
return res.status(400).json({ message: body }).send();
|
|
23
23
|
let item = null;
|
|
24
24
|
if (id)
|
|
25
|
-
item = yield queries.getById(
|
|
25
|
+
item = yield queries.getById(resource, id);
|
|
26
26
|
if (!item)
|
|
27
27
|
return res.status(404).json({
|
|
28
28
|
message: `ID '${id}' not found`,
|
|
29
29
|
});
|
|
30
|
-
item = Object.assign(Object.assign({},
|
|
31
|
-
const replacedItem = yield queries.replace(
|
|
30
|
+
item = Object.assign(Object.assign({}, body), { id });
|
|
31
|
+
const replacedItem = yield queries.replace(resource, item);
|
|
32
32
|
return res
|
|
33
33
|
.status(200)
|
|
34
34
|
.json(returnNullFields ? replacedItem : (0, utils_1.removeNullFields)(replacedItem))
|
package/routes/put.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"put.js","sourceRoot":"","sources":["../../src/routes/put.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,iDAAqD;AACrD,mCAA0C;AAE1C,SAAS,eAAe,CAAC,OAAO,EAAE,sBAAsB,EAAE,gBAAgB;IACxE,SAAe,SAAS,CAAC,GAAG,EAAE,GAAG;;YAC/B,IAAI;gBACF,MAAM,EAAE,
|
|
1
|
+
{"version":3,"file":"put.js","sourceRoot":"","sources":["../../src/routes/put.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,iDAAqD;AACrD,mCAA0C;AAE1C,SAAS,eAAe,CAAC,OAAO,EAAE,sBAAsB,EAAE,gBAAgB;IACxE,SAAe,SAAS,CAAC,GAAG,EAAE,GAAG;;YAC/B,IAAI;gBACF,MAAM,EAAE,QAAQ,EAAE,EAAE,EAAE,GAAG,GAAG,CAAC,WAAW,CAAA;gBAExC,MAAM,IAAI,GAAG,IAAA,mCAAoB,EAAC,sBAAsB,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA;gBAElE,IAAI,OAAO,IAAI,KAAK,QAAQ;oBAAE,OAAO,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAA;gBAEnF,IAAI,IAAI,GAAG,IAAI,CAAA;gBACf,IAAI,EAAE;oBAAE,IAAI,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAA;gBAElD,IAAI,CAAC,IAAI;oBACP,OAAO,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;wBAC1B,OAAO,EAAE,OAAO,EAAE,aAAa;qBAChC,CAAC,CAAA;gBAEJ,IAAI,mCAAQ,IAAI,KAAE,EAAE,GAAE,CAAA;gBAEtB,MAAM,YAAY,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAA;gBAE1D,OAAO,GAAG;qBACP,MAAM,CAAC,GAAG,CAAC;qBACX,IAAI,CAAC,gBAAgB,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,IAAA,wBAAgB,EAAC,YAAY,CAAC,CAAC;qBACtE,IAAI,EAAE,CAAA;aACV;YAAC,OAAO,KAAc,EAAE;gBACvB,OAAO,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAG,KAAe,CAAC,OAAO,EAAE,CAAC,CAAA;aACnE;QACH,CAAC;KAAA;IAED,OAAO;QACL,SAAS;KACV,CAAA;AACH,CAAC;AAEQ,0CAAe"}
|
package/routes/routes.js
CHANGED
|
@@ -21,13 +21,13 @@ const delete_1 = require("./delete");
|
|
|
21
21
|
const urlMiddleware_1 = require("../urls/urlMiddleware");
|
|
22
22
|
const express_1 = __importDefault(require("express"));
|
|
23
23
|
function createResourceRouter(queries, routerConfig) {
|
|
24
|
-
const { validateResources,
|
|
24
|
+
const { validateResources, resources, apiPrefix, cacheControl, requestBodyInterceptor, responseBodyInterceptor, returnNullFields, } = routerConfig;
|
|
25
25
|
const { handleGetResource } = (0, get_1.createGetRoutes)(queries, cacheControl, responseBodyInterceptor, returnNullFields);
|
|
26
26
|
const { handlePost } = (0, post_1.createPostRoutes)(queries, requestBodyInterceptor, returnNullFields);
|
|
27
27
|
const { handlePut } = (0, put_1.createPutRoutes)(queries, requestBodyInterceptor, returnNullFields);
|
|
28
28
|
const { handlePatch } = (0, patch_1.createPatchRoutes)(queries, requestBodyInterceptor, returnNullFields);
|
|
29
29
|
const { handleDelete } = (0, delete_1.createDeleteRoutes)(queries);
|
|
30
|
-
const validateResource = (0, urlMiddleware_1.createValidateResourceMiddleware)(validateResources,
|
|
30
|
+
const validateResource = (0, urlMiddleware_1.createValidateResourceMiddleware)(validateResources, resources);
|
|
31
31
|
const getResourceAndId = (0, urlMiddleware_1.createResourceAndIdParser)(apiPrefix);
|
|
32
32
|
const resourceRouter = express_1.default.Router();
|
|
33
33
|
resourceRouter
|
package/routes/routes.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"routes.js","sourceRoot":"","sources":["../../src/routes/routes.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,+BAAuC;AACvC,iCAAyC;AACzC,+BAAuC;AACvC,mCAA2C;AAC3C,qCAA6C;AAC7C,yDAAmG;AAEnG,sDAA6B;AAG7B,SAAS,oBAAoB,CAAC,OAAO,EAAE,YAA0B;IAC/D,MAAM,EACJ,iBAAiB,EACjB,
|
|
1
|
+
{"version":3,"file":"routes.js","sourceRoot":"","sources":["../../src/routes/routes.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,+BAAuC;AACvC,iCAAyC;AACzC,+BAAuC;AACvC,mCAA2C;AAC3C,qCAA6C;AAC7C,yDAAmG;AAEnG,sDAA6B;AAG7B,SAAS,oBAAoB,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AE,YAAY,CAAC,CAAA;IAEhE,OAAO,cAAc,CAAA;AACvB,CAAC;AAqBQ,oDAAoB;AAnB7B,iDAAiD;AACjD,MAAM,UAAU,GAAG,iBAAO,CAAC,MAAM,EAAE,CAAA;AAkBJ,gCAAU;AAjBzC,UAAU,CAAC,GAAG,CAAC,GAAG,EAAE,CAAO,CAAC,EAAE,GAAG,EAAE,EAAE;IACnC,OAAO,GAAG,CAAC,IAAI,CAAC,aAAa,CAAC,CAAA;AAChC,CAAC,CAAA,CAAC,CAAA;AAEF,sDAAsD;AACtD,UAAU,CAAC,GAAG,CAAC,GAAG,EAAE,sBAAsB,CAAC,CAAA;AAE3C,0CAA0C;AAC1C,SAAS,sBAAsB,CAAC,CAAC,EAAE,GAAG;IACpC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,oBAAoB,EAAE,CAAC,CAAA;AACzD,CAAC;AAO0C,wDAAsB;AALjE,gCAAgC;AAChC,SAAS,cAAc,CAAC,CAAC,EAAE,GAAG;IAC5B,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,WAAW,EAAE,CAAC,CAAA;AAChD,CAAC;AAEkE,wCAAc"}
|
package/routes/types.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
type RequestInfo = {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
resource: string;
|
|
3
|
+
body: unknown;
|
|
4
4
|
};
|
|
5
5
|
export type RequestBodyInterceptorCallback = (info: RequestInfo) => void | string | object;
|
|
6
6
|
export type RequestBodyInterceptor = {
|
|
@@ -9,8 +9,8 @@ export type RequestBodyInterceptor = {
|
|
|
9
9
|
put?: RequestBodyInterceptorCallback;
|
|
10
10
|
};
|
|
11
11
|
type ResponseInfo = {
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
resource: string;
|
|
13
|
+
body: unknown;
|
|
14
14
|
id?: string;
|
|
15
15
|
};
|
|
16
16
|
export type ResponseBodyInterceptor = (info: ResponseInfo) => unknown;
|
package/urls/urlMiddleware.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
declare function createResourceAndIdParser(apiPrefix: any): (req: any, _: any, next: any) => any;
|
|
2
|
-
declare function createValidateResourceMiddleware(validateResources: any,
|
|
2
|
+
declare function createValidateResourceMiddleware(validateResources: any, resources: any): (req: any, res: any, next: any) => any;
|
|
3
3
|
export { createResourceAndIdParser, createValidateResourceMiddleware };
|
package/urls/urlMiddleware.js
CHANGED
|
@@ -11,16 +11,16 @@ function createResourceAndIdParser(apiPrefix) {
|
|
|
11
11
|
};
|
|
12
12
|
}
|
|
13
13
|
exports.createResourceAndIdParser = createResourceAndIdParser;
|
|
14
|
-
function createValidateResourceMiddleware(validateResources,
|
|
14
|
+
function createValidateResourceMiddleware(validateResources, resources) {
|
|
15
15
|
return function validateResource(req, res, next) {
|
|
16
16
|
if (!validateResources)
|
|
17
17
|
return next();
|
|
18
|
-
const {
|
|
19
|
-
if (!
|
|
18
|
+
const { resource } = req.requestInfo;
|
|
19
|
+
if (!resource)
|
|
20
20
|
return next();
|
|
21
|
-
if (!
|
|
21
|
+
if (!resources.includes(resource.toLowerCase())) {
|
|
22
22
|
return res.status(404).json({
|
|
23
|
-
message: `'${
|
|
23
|
+
message: `'${resource}' is an unknown resource`,
|
|
24
24
|
});
|
|
25
25
|
}
|
|
26
26
|
return next();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"urlMiddleware.js","sourceRoot":"","sources":["../../src/urls/urlMiddleware.ts"],"names":[],"mappings":";;;AAAA,2CAAsC;AAEtC,SAAS,yBAAyB,CAAC,SAAS;IAC1C,OAAO,SAAS,gBAAgB,CAAC,GAAG,EAAE,CAAC,EAAE,IAAI;QAC3C,MAAM,GAAG,GAAG,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC,CAAA;QAC9C,MAAM,O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;aAChD,CAAC,CAAA;SACH;QAED,OAAO,IAAI,EAAE,CAAA;IACf,CAAC,CAAA;AACH,CAAC;AAEmC,4EAAgC"}
|
package/urls/urlParser.d.ts
CHANGED
package/urls/urlParser.js
CHANGED
|
@@ -3,11 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.parseUrl = void 0;
|
|
4
4
|
function parseUrl(url) {
|
|
5
5
|
if (!url || (url && !url.trim()))
|
|
6
|
-
return {
|
|
6
|
+
return { resource: null, id: null };
|
|
7
7
|
const urlSegments = url.split('/').filter((i) => i);
|
|
8
|
-
const
|
|
8
|
+
const resource = urlSegments.length > 0 ? urlSegments[0] : null;
|
|
9
9
|
const id = urlSegments.length > 1 ? urlSegments[1] : null;
|
|
10
|
-
return {
|
|
10
|
+
return { resource, id };
|
|
11
11
|
}
|
|
12
12
|
exports.parseUrl = parseUrl;
|
|
13
13
|
//# sourceMappingURL=urlParser.js.map
|
package/urls/urlParser.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"urlParser.js","sourceRoot":"","sources":["../../src/urls/urlParser.ts"],"names":[],"mappings":";;;AAAA,SAAS,QAAQ,CAAC,GAAG;IACnB,IAAI,CAAC,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC;QAAE,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"urlParser.js","sourceRoot":"","sources":["../../src/urls/urlParser.ts"],"names":[],"mappings":";;;AAAA,SAAS,QAAQ,CAAC,GAAG;IACnB,IAAI,CAAC,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC;QAAE,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE,EAAE,IAAI,EAAE,CAAA;IAErE,MAAM,WAAW,GAAG,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAA;IAEnD,MAAM,QAAQ,GAAG,WAAW,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAA;IAC/D,MAAM,EAAE,GAAG,WAAW,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAA;IAEzD,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAA;AACzB,CAAC;AAEQ,4BAAQ"}
|