temba 0.9.0 → 0.10.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 +20 -17
- package/dist/config/index.js +1 -8
- package/dist/config/index.js.map +1 -1
- package/dist/delay/delayMiddleware.js +16 -0
- package/dist/delay/delayMiddleware.js.map +1 -0
- package/dist/errors/errors.js +34 -0
- package/dist/errors/errors.js.map +1 -0
- package/dist/errors/types.js.map +1 -1
- package/dist/queries/queries.js +17 -0
- package/dist/queries/queries.js.map +1 -0
- package/dist/routes/delete.js.map +1 -1
- package/dist/routes/get.js.map +1 -1
- package/dist/routes/post.js +3 -1
- package/dist/routes/post.js.map +1 -1
- package/dist/routes/put.js +5 -2
- package/dist/routes/put.js.map +1 -1
- package/dist/routes/routes.js +56 -0
- package/dist/routes/routes.js.map +1 -0
- package/dist/routes/types.js +3 -0
- package/dist/routes/types.js.map +1 -0
- package/dist/routes/validator.js +11 -7
- package/dist/routes/validator.js.map +1 -1
- package/dist/server.js +6 -6
- package/dist/server.js.map +1 -1
- package/dist/urls/urlMiddleware.js +31 -0
- package/dist/urls/urlMiddleware.js.map +1 -0
- package/package.json +5 -6
package/README.md
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
# Temba
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/temba)
|
|
3
4
|
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
|
4
|
-
|
|
5
5
|
[](#contributors-)
|
|
6
|
-
|
|
7
6
|
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
|
8
7
|
|
|
9
8
|
**Get a simple MongoDB REST API with zero coding in less than 30 seconds (seriously).**
|
|
@@ -20,6 +19,8 @@ This project is inspired by the fantastic [json-server](https://github.com/typic
|
|
|
20
19
|
|
|
21
20
|
[Getting Started](#getting-started)
|
|
22
21
|
|
|
22
|
+
[What Tema does](#what-temba-does)
|
|
23
|
+
|
|
23
24
|
[Usage](#usage)
|
|
24
25
|
|
|
25
26
|
## Temba?
|
|
@@ -39,7 +40,7 @@ Prerequisites you need to have:
|
|
|
39
40
|
|
|
40
41
|
> Wthout a database, Temba also works. However, then data is kept in memory and flushed every time the server restarts.
|
|
41
42
|
|
|
42
|
-
### Use `npx`
|
|
43
|
+
### Use the starter with `npx`
|
|
43
44
|
|
|
44
45
|
Create your own Temba server with the following command and you are up and running!
|
|
45
46
|
|
|
@@ -47,7 +48,7 @@ Create your own Temba server with the following command and you are up and runni
|
|
|
47
48
|
npx create-temba-server my-rest-api
|
|
48
49
|
```
|
|
49
50
|
|
|
50
|
-
|
|
51
|
+
This command clones the [Temba-starter](https://github.com/bouwe77/temba-starter) repository and installs all dependencies.
|
|
51
52
|
|
|
52
53
|
### Manually adding to an existing app
|
|
53
54
|
|
|
@@ -71,9 +72,7 @@ server.listen(port, () => {
|
|
|
71
72
|
|
|
72
73
|
By passing a config object to the `create` function you can customize Temba's behavior. Refer to the [config settings](#config-settings-overview) below for the various possibilities.
|
|
73
74
|
|
|
74
|
-
##
|
|
75
|
-
|
|
76
|
-
### Introduction
|
|
75
|
+
## What Temba does
|
|
77
76
|
|
|
78
77
|
Out of the box, Temba gives you a CRUD REST API to any resource name you can think of.
|
|
79
78
|
|
|
@@ -94,9 +93,21 @@ The HTTP methods that are supported are `GET`, `POST`, `PUT` and `DELETE`. For a
|
|
|
94
93
|
|
|
95
94
|
On the root URI (e.g. http://localhost:8080/) only a `GET` request is supported, which shows you a message indicating the API is working. All other HTTP methods on the root URI return a `405 Method Not Allowed` response.
|
|
96
95
|
|
|
97
|
-
###
|
|
96
|
+
### JSON
|
|
97
|
+
|
|
98
|
+
Temba supports JSON only.
|
|
99
|
+
|
|
100
|
+
Request bodies sent with a `POST` and `PUT` requests are valid when the request body is either empty, or when it's valid formatted JSON. Adding a `Content-Type: application/json` header is required. If you send a request with invalid formatted JSON, a `400 Bad Request` response is returned.
|
|
101
|
+
|
|
102
|
+
Any valid formatted JSON is accepted and stored. If you want to validate or even change the JSON in the request bodies, check out the [`requestBodyValidator`](#request-body-validation-or-mutation) callbacks.
|
|
98
103
|
|
|
99
|
-
|
|
104
|
+
IDs are auto generated when creating resources. IDs in the JSON request body are ignored for any request.
|
|
105
|
+
|
|
106
|
+
## Usage
|
|
107
|
+
|
|
108
|
+
### In-memory or MongoDB
|
|
109
|
+
|
|
110
|
+
By default data is stored in memory. This means the data is flushed when the server restarts. To persist your data, provide the `connectionString` config setting for your MongoDB database:
|
|
100
111
|
|
|
101
112
|
```js
|
|
102
113
|
const config = {
|
|
@@ -118,14 +129,6 @@ const server = temba.create(config)
|
|
|
118
129
|
|
|
119
130
|
Requests on these resources only give a `404 Not Found` if the ID does not exist. Requests on any other resource will always return a `404 Not Found`.
|
|
120
131
|
|
|
121
|
-
### JSON
|
|
122
|
-
|
|
123
|
-
Temba only supports JSON. If you send a request with invalid formatted JSON, a `400 Bad Request` response is returned.
|
|
124
|
-
|
|
125
|
-
When sending JSON data (`POST` and `PUT` requests), adding a `Content-Type: application/json` header is required.
|
|
126
|
-
|
|
127
|
-
IDs are auto generated when creating resources. IDs in the JSON request body are ignored.
|
|
128
|
-
|
|
129
132
|
### Static assets
|
|
130
133
|
|
|
131
134
|
If you want to host static assets next to the REST API, configure the `staticFolder`:
|
package/dist/config/index.js
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.initConfig = void 0;
|
|
4
|
-
const logging_1 = require("../logging");
|
|
5
4
|
const defaultConfig = {
|
|
6
5
|
resourceNames: [],
|
|
7
6
|
validateResources: false,
|
|
8
|
-
logLevel: logging_1.logLevels.DEBUG,
|
|
9
7
|
staticFolder: null,
|
|
10
8
|
apiPrefix: '',
|
|
11
9
|
connectionString: null,
|
|
@@ -28,11 +26,6 @@ function initConfig(userConfig) {
|
|
|
28
26
|
config.resourceNames = userConfig.resourceNames;
|
|
29
27
|
config.validateResources = true;
|
|
30
28
|
}
|
|
31
|
-
if (userConfig.logLevel &&
|
|
32
|
-
userConfig.logLevel.length !== 0 &&
|
|
33
|
-
Object.keys(logging_1.logLevels).includes(userConfig.logLevel.toUpperCase())) {
|
|
34
|
-
config.logLevel = userConfig.logLevel.toUpperCase();
|
|
35
|
-
}
|
|
36
29
|
if (userConfig.staticFolder) {
|
|
37
30
|
config.staticFolder = userConfig.staticFolder.replace(/[^a-zA-Z0-9]/g, '');
|
|
38
31
|
}
|
|
@@ -47,7 +40,7 @@ function initConfig(userConfig) {
|
|
|
47
40
|
config.cacheControl = userConfig.cacheControl;
|
|
48
41
|
}
|
|
49
42
|
if (userConfig.delay &&
|
|
50
|
-
userConfig.delay
|
|
43
|
+
userConfig.delay !== 0 &&
|
|
51
44
|
typeof Number(userConfig.delay) === 'number' &&
|
|
52
45
|
Number(userConfig.delay) > 0 &&
|
|
53
46
|
Number(userConfig.delay) < 10000) {
|
package/dist/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":";;;AAaA,MAAM,aAAa,GAAW;IAC5B,aAAa,EAAE,EAAE;IACjB,iBAAiB,EAAE,KAAK;IACxB,YAAY,EAAE,IAAI;IAClB,SAAS,EAAE,EAAE;IACb,gBAAgB,EAAE,IAAI;IACtB,YAAY,EAAE,UAAU;IACxB,KAAK,EAAE,CAAC;IACR,oBAAoB,EAAE;QACpB,IAAI,EAAE,GAAG,EAAE;YACT,aAAa;QACf,CAAC;QACD,GAAG,EAAE,GAAG,EAAE;YACR,aAAa;QACf,CAAC;KACF;CACF,CAAA;AAED,SAAgB,UAAU,CAAC,UAAkB;IAC3C,IAAI,CAAC,UAAU;QAAE,OAAO,aAAa,CAAA;IAErC,MAAM,MAAM,qBAAQ,aAAa,CAAE,CAA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I,KAAK,UAAU,EAC1D;YACA,MAAM,CAAC,oBAAoB,CAAC,IAAI,GAAG,UAAU,CAAC,oBAAoB,CAAC,IAAI,CAAA;SACxE;QACD,IACE,UAAU,CAAC,oBAAoB,CAAC,GAAG;YACnC,OAAO,UAAU,CAAC,oBAAoB,CAAC,GAAG,KAAK,UAAU,EACzD;YACA,MAAM,CAAC,oBAAoB,CAAC,GAAG,GAAG,UAAU,CAAC,oBAAoB,CAAC,GAAG,CAAA;SACtE;KACF;IAED,OAAO,MAAM,CAAA;AACf,CAAC;AArDD,gCAqDC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createDelayMiddleware = void 0;
|
|
7
|
+
const connect_pause_1 = __importDefault(require("connect-pause"));
|
|
8
|
+
function createDelayMiddleware(delay) {
|
|
9
|
+
return function (req, res, next) {
|
|
10
|
+
console.log('Start delay...');
|
|
11
|
+
(0, connect_pause_1.default)(delay)(req, res, next);
|
|
12
|
+
console.log('Delay finished!');
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
exports.createDelayMiddleware = createDelayMiddleware;
|
|
16
|
+
//# sourceMappingURL=delayMiddleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"delayMiddleware.js","sourceRoot":"","sources":["../../src/delay/delayMiddleware.ts"],"names":[],"mappings":";;;;;;AAAA,kEAAiC;AAEjC,SAAS,qBAAqB,CAAC,KAAK;IAClC,OAAO,UAAU,GAAG,EAAE,GAAG,EAAE,IAAI;QAC7B,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAA;QAC7B,IAAA,uBAAK,EAAC,KAAK,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,CAAA;QAC5B,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAA;IAChC,CAAC,CAAA;AACH,CAAC;AAEQ,sDAAqB"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.new500InternalServerError = exports.new400BadRequestError = exports.new404NotFoundError = exports.errorHandler = exports.HttpError = void 0;
|
|
4
|
+
class HttpError extends Error {
|
|
5
|
+
constructor(status, message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.status = status;
|
|
8
|
+
this.message = message;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
exports.HttpError = HttpError;
|
|
12
|
+
// This is the error middleware that will be used by the server.
|
|
13
|
+
function errorHandler(e, _, res) {
|
|
14
|
+
const status = 500;
|
|
15
|
+
let message = 'Unknown error';
|
|
16
|
+
if (e instanceof Error) {
|
|
17
|
+
message = e.message;
|
|
18
|
+
}
|
|
19
|
+
return res.status(status).json({ message });
|
|
20
|
+
}
|
|
21
|
+
exports.errorHandler = errorHandler;
|
|
22
|
+
function new404NotFoundError(message = 'Not Found') {
|
|
23
|
+
return new HttpError(404, message);
|
|
24
|
+
}
|
|
25
|
+
exports.new404NotFoundError = new404NotFoundError;
|
|
26
|
+
function new400BadRequestError(message = 'Bad Request') {
|
|
27
|
+
return new HttpError(400, message);
|
|
28
|
+
}
|
|
29
|
+
exports.new400BadRequestError = new400BadRequestError;
|
|
30
|
+
function new500InternalServerError(message = 'Internal Server Error') {
|
|
31
|
+
return new HttpError(500, message);
|
|
32
|
+
}
|
|
33
|
+
exports.new500InternalServerError = new500InternalServerError;
|
|
34
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors/errors.ts"],"names":[],"mappings":";;;AAAA,MAAa,SAAU,SAAQ,KAAK;IAIlC,YAAY,MAAc,EAAE,OAAe;QACzC,KAAK,CAAC,OAAO,CAAC,CAAA;QAEd,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;IACxB,CAAC;CACF;AAVD,8BAUC;AAED,gEAAgE;AAChE,SAAS,YAAY,CAAC,CAAU,EAAE,CAAC,EAAE,GAAG;IACtC,MAAM,MAAM,GAAG,GAAG,CAAA;IAClB,IAAI,OAAO,GAAG,eAAe,CAAA;IAE7B,IAAI,CAAC,YAAY,KAAK,EAAE;QACtB,OAAO,GAAG,CAAC,CAAC,OAAO,CAAA;KACpB;IAED,OAAO,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,CAAC,CAAA;AAC7C,CAAC;AAeC,oCAAY;AAbd,SAAS,mBAAmB,CAAC,OAAO,GAAG,WAAW;IAChD,OAAO,IAAI,SAAS,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;AACpC,CAAC;AAYC,kDAAmB;AAVrB,SAAS,qBAAqB,CAAC,OAAO,GAAG,aAAa;IACpD,OAAO,IAAI,SAAS,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;AACpC,CAAC;AASC,sDAAqB;AAPvB,SAAS,yBAAyB,CAAC,OAAO,GAAG,uBAAuB;IAClE,OAAO,IAAI,SAAS,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;AACpC,CAAC;AAMC,8DAAyB"}
|
package/dist/errors/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/errors/types.ts"],"names":[],"mappings":";;;AAAA,MAAa,UAAW,SAAQ,KAAK;IACnC,YAAY,GAAW,EAAS,
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/errors/types.ts"],"names":[],"mappings":";;;AAAA,MAAa,UAAW,SAAQ,KAAK;IACnC,YAAY,GAAW,EAAS,MAA6B;QAC3D,KAAK,CAAC,GAAG,CAAC,CAAA;QADoB,WAAM,GAAN,MAAM,CAAuB;QAE3D,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,UAAU,CAAC,SAAS,CAAC,CAAA;QAEjD,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;IACtB,CAAC;CACF;AAPD,gCAOC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createQueries = void 0;
|
|
7
|
+
const in_memory_1 = __importDefault(require("./in-memory"));
|
|
8
|
+
const mongo_1 = __importDefault(require("./mongo"));
|
|
9
|
+
function createQueries(connectionString) {
|
|
10
|
+
if (!connectionString) {
|
|
11
|
+
return in_memory_1.default;
|
|
12
|
+
}
|
|
13
|
+
const mongoQueries = (0, mongo_1.default)(connectionString);
|
|
14
|
+
return mongoQueries;
|
|
15
|
+
}
|
|
16
|
+
exports.createQueries = createQueries;
|
|
17
|
+
//# sourceMappingURL=queries.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"queries.js","sourceRoot":"","sources":["../../src/queries/queries.ts"],"names":[],"mappings":";;;;;;AAAA,4DAAyC;AACzC,oDAAwC;AAExC,SAAS,aAAa,CAAC,gBAAgB;IACrC,IAAI,CAAC,gBAAgB,EAAE;QACrB,OAAO,mBAAe,CAAA;KACvB;IAED,MAAM,YAAY,GAAG,IAAA,eAAkB,EAAC,gBAAgB,CAAC,CAAA;IACzD,OAAO,YAAY,CAAA;AACrB,CAAC;AAEQ,sCAAa"}
|
|
@@ -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,EAAE,IAAI;;YACxC,IAAI;gBACF,MAAM,EAAE,YAAY,EAAE,EAAE,EAAE,GAAG,GAAG,CAAC,WAAW,CAAA;gBAE5C,IAAI,EAAE,EAAE;oBACN,MAAM,IAAI,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,YAAY,EAAE,EAAE,CAAC,CAAA;oBACpD,IAAI,IAAI,EAAE;wBACR,MAAM,OAAO,CAAC,UAAU,CAAC,YAAY,EAAE,EAAE,CAAC,CAAA;qBAC3C;iBACF;qBAAM;oBACL,MAAM,OAAO,CAAC,SAAS,CAAC,YAAY,CAAC,CAAA;iBACtC;aACF;YAAC,OAAO,
|
|
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,EAAE,IAAI;;YACxC,IAAI;gBACF,MAAM,EAAE,YAAY,EAAE,EAAE,EAAE,GAAG,GAAG,CAAC,WAAW,CAAA;gBAE5C,IAAI,EAAE,EAAE;oBACN,MAAM,IAAI,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,YAAY,EAAE,EAAE,CAAC,CAAA;oBACpD,IAAI,IAAI,EAAE;wBACR,MAAM,OAAO,CAAC,UAAU,CAAC,YAAY,EAAE,EAAE,CAAC,CAAA;qBAC3C;iBACF;qBAAM;oBACL,MAAM,OAAO,CAAC,SAAS,CAAC,YAAY,CAAC,CAAA;iBACtC;aACF;YAAC,OAAO,KAAc,EAAE;gBACvB,OAAO,IAAI,CAAC,KAAK,CAAC,CAAA;aACnB;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/dist/routes/get.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"get.js","sourceRoot":"","sources":["../../src/routes/get.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,SAAS,eAAe,CAAC,OAAO,EAAE,YAAY;IAC5C,SAAe,iBAAiB,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI;;YAC7C,IAAI;gBACF,MAAM,EAAE,YAAY,EAAE,EAAE,EAAE,GAAG,GAAG,CAAC,WAAW,CAAA;gBAE5C,GAAG,CAAC,GAAG,CAAC,eAAe,EAAE,YAAY,CAAC,CAAA;gBAEtC,IAAI,EAAE,EAAE;oBACN,MAAM,IAAI,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,YAAY,EAAE,EAAE,CAAC,CAAA;oBAEpD,IAAI,CAAC,IAAI,EAAE;wBACT,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;wBACf,OAAO,GAAG,CAAC,IAAI,EAAE,CAAA;qBAClB;oBAED,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;oBACf,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;oBACd,OAAO,GAAG,CAAC,IAAI,EAAE,CAAA;iBAClB;gBAED,MAAM,KAAK,GAAG,MAAM,OAAO,CAAC,MAAM,CAAC,YAAY,CAAC,CAAA;gBAChD,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;gBACf,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;gBACf,OAAO,GAAG,CAAC,IAAI,EAAE,CAAA;aAClB;YAAC,OAAO,
|
|
1
|
+
{"version":3,"file":"get.js","sourceRoot":"","sources":["../../src/routes/get.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,SAAS,eAAe,CAAC,OAAO,EAAE,YAAY;IAC5C,SAAe,iBAAiB,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI;;YAC7C,IAAI;gBACF,MAAM,EAAE,YAAY,EAAE,EAAE,EAAE,GAAG,GAAG,CAAC,WAAW,CAAA;gBAE5C,GAAG,CAAC,GAAG,CAAC,eAAe,EAAE,YAAY,CAAC,CAAA;gBAEtC,IAAI,EAAE,EAAE;oBACN,MAAM,IAAI,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,YAAY,EAAE,EAAE,CAAC,CAAA;oBAEpD,IAAI,CAAC,IAAI,EAAE;wBACT,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;wBACf,OAAO,GAAG,CAAC,IAAI,EAAE,CAAA;qBAClB;oBAED,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;oBACf,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;oBACd,OAAO,GAAG,CAAC,IAAI,EAAE,CAAA;iBAClB;gBAED,MAAM,KAAK,GAAG,MAAM,OAAO,CAAC,MAAM,CAAC,YAAY,CAAC,CAAA;gBAChD,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;gBACf,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;gBACf,OAAO,GAAG,CAAC,IAAI,EAAE,CAAA;aAClB;YAAC,OAAO,KAAc,EAAE;gBACvB,OAAO,IAAI,CAAC,KAAK,CAAC,CAAA;aACnB;QACH,CAAC;KAAA;IAED,OAAO;QACL,iBAAiB;KAClB,CAAA;AACH,CAAC;AAEQ,0CAAe"}
|
package/dist/routes/post.js
CHANGED
|
@@ -17,7 +17,9 @@ function createPostRoutes(queries, requestBodyValidator) {
|
|
|
17
17
|
return __awaiter(this, void 0, void 0, function* () {
|
|
18
18
|
try {
|
|
19
19
|
const { resourceName } = req.requestInfo;
|
|
20
|
-
const requestBody = (0, validator_1.validateRequestBody)(requestBodyValidator.post,
|
|
20
|
+
const requestBody = (0, validator_1.validateRequestBody)(requestBodyValidator.post, req);
|
|
21
|
+
if (typeof requestBody === 'string')
|
|
22
|
+
return res.status(400).json({ message: requestBody }).send();
|
|
21
23
|
const newItem = yield queries.create(resourceName, requestBody);
|
|
22
24
|
return res
|
|
23
25
|
.set({
|
package/dist/routes/post.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"post.js","sourceRoot":"","sources":["../../src/routes/post.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,6BAA4B;AAC5B,2CAAiD;AAEjD,SAAS,gBAAgB,CAAC,OAAO,EAAE,oBAAoB;IACrD,SAAe,UAAU,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI;;YACtC,IAAI;gBACF,MAAM,EAAE,YAAY,EAAE,GAAG,GAAG,CAAC,WAAW,CAAA;gBAExC,MAAM,WAAW,GAAG,IAAA,+BAAmB,
|
|
1
|
+
{"version":3,"file":"post.js","sourceRoot":"","sources":["../../src/routes/post.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,6BAA4B;AAC5B,2CAAiD;AAEjD,SAAS,gBAAgB,CAAC,OAAO,EAAE,oBAAoB;IACrD,SAAe,UAAU,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI;;YACtC,IAAI;gBACF,MAAM,EAAE,YAAY,EAAE,GAAG,GAAG,CAAC,WAAW,CAAA;gBAExC,MAAM,WAAW,GAAG,IAAA,+BAAmB,EAAC,oBAAoB,CAAC,IAAI,EAAE,GAAG,CAAC,CAAA;gBAEvE,IAAI,OAAO,WAAW,KAAK,QAAQ;oBACjC,OAAO,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,WAAW,EAAE,CAAC,CAAC,IAAI,EAAE,CAAA;gBAE9D,MAAM,OAAO,GAAG,MAAM,OAAO,CAAC,MAAM,CAAC,YAAY,EAAE,WAAW,CAAC,CAAA;gBAE/D,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,YAAY,IAAI,OAAO,CAAC,EAAE,EAAE;qBAC1C,CAAC;iBACH,CAAC;qBACD,MAAM,CAAC,GAAG,CAAC;qBACX,IAAI,CAAC,OAAO,CAAC;qBACb,IAAI,EAAE,CAAA;aACV;YAAC,OAAO,KAAc,EAAE;gBACvB,OAAO,IAAI,CAAC,KAAK,CAAC,CAAA;aACnB;QACH,CAAC;KAAA;IAED,OAAO;QACL,UAAU;KACX,CAAA;AACH,CAAC;AAEQ,4CAAgB"}
|
package/dist/routes/put.js
CHANGED
|
@@ -10,17 +10,20 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.createPutRoutes = void 0;
|
|
13
|
-
const errors_1 = require("../errors");
|
|
13
|
+
const errors_1 = require("../errors/errors");
|
|
14
14
|
const validator_1 = require("./validator");
|
|
15
15
|
function createPutRoutes(queries, requestBodyValidator) {
|
|
16
16
|
function handlePut(req, res, next) {
|
|
17
17
|
return __awaiter(this, void 0, void 0, function* () {
|
|
18
18
|
try {
|
|
19
19
|
const { resourceName, id } = req.requestInfo;
|
|
20
|
-
const requestBody = (0, validator_1.validateRequestBody)(requestBodyValidator.put,
|
|
20
|
+
const requestBody = (0, validator_1.validateRequestBody)(requestBodyValidator.put, req);
|
|
21
|
+
if (typeof requestBody === 'string')
|
|
22
|
+
return res.status(400).json({ message: requestBody }).send();
|
|
21
23
|
let item = null;
|
|
22
24
|
if (id)
|
|
23
25
|
item = yield queries.getById(resourceName, id);
|
|
26
|
+
// TODO return a response instead of calling next
|
|
24
27
|
if (!item)
|
|
25
28
|
return next((0, errors_1.new404NotFoundError)(`ID '${id}' not found`));
|
|
26
29
|
item = Object.assign(Object.assign({}, requestBody), { id });
|
package/dist/routes/put.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"put.js","sourceRoot":"","sources":["../../src/routes/put.ts"],"names":[],"mapp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,0CAAe"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.handleNotFound = exports.handleMethodNotAllowed = exports.rootRouter = exports.createResourceRouter = void 0;
|
|
16
|
+
const get_1 = require("./get");
|
|
17
|
+
const post_1 = require("./post");
|
|
18
|
+
const put_1 = require("./put");
|
|
19
|
+
const delete_1 = require("./delete");
|
|
20
|
+
const urlMiddleware_1 = require("../urls/urlMiddleware");
|
|
21
|
+
const express_1 = __importDefault(require("express"));
|
|
22
|
+
function createResourceRouter(queries, { validateResources, resourceNames, apiPrefix, cacheControl, requestBodyValidator, }) {
|
|
23
|
+
const { handleGetResource } = (0, get_1.createGetRoutes)(queries, cacheControl);
|
|
24
|
+
const { handlePost } = (0, post_1.createPostRoutes)(queries, requestBodyValidator);
|
|
25
|
+
const { handlePut } = (0, put_1.createPutRoutes)(queries, requestBodyValidator);
|
|
26
|
+
const { handleDelete } = (0, delete_1.createDeleteRoutes)(queries);
|
|
27
|
+
const validateResource = (0, urlMiddleware_1.createValidateResourceMiddleware)(validateResources, resourceNames);
|
|
28
|
+
const getResourceAndId = (0, urlMiddleware_1.createResourceAndIdParser)(apiPrefix);
|
|
29
|
+
const resourceRouter = express_1.default.Router();
|
|
30
|
+
resourceRouter
|
|
31
|
+
.get('*', getResourceAndId, validateResource, handleGetResource)
|
|
32
|
+
.post('*', getResourceAndId, validateResource, handlePost)
|
|
33
|
+
.put('*', getResourceAndId, validateResource, handlePut)
|
|
34
|
+
.delete('*', getResourceAndId, validateResource, handleDelete);
|
|
35
|
+
return resourceRouter;
|
|
36
|
+
}
|
|
37
|
+
exports.createResourceRouter = createResourceRouter;
|
|
38
|
+
// A GET to the root URL shows a default message.
|
|
39
|
+
const rootRouter = express_1.default.Router();
|
|
40
|
+
exports.rootRouter = rootRouter;
|
|
41
|
+
rootRouter.get('/', (_, res) => __awaiter(void 0, void 0, void 0, function* () {
|
|
42
|
+
return res.send('It works! ツ');
|
|
43
|
+
}));
|
|
44
|
+
// All other requests to the root URL are not allowed.
|
|
45
|
+
rootRouter.all('/', handleMethodNotAllowed);
|
|
46
|
+
// Route for handling not allowed methods.
|
|
47
|
+
function handleMethodNotAllowed(_, res) {
|
|
48
|
+
res.status(405).json({ message: 'Method Not Allowed' });
|
|
49
|
+
}
|
|
50
|
+
exports.handleMethodNotAllowed = handleMethodNotAllowed;
|
|
51
|
+
// Route for handling not found.
|
|
52
|
+
function handleNotFound(_, res) {
|
|
53
|
+
res.status(404).json({ message: 'Not Found' });
|
|
54
|
+
}
|
|
55
|
+
exports.handleNotFound = handleNotFound;
|
|
56
|
+
//# sourceMappingURL=routes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"routes.js","sourceRoot":"","sources":["../../src/routes/routes.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,+BAAuC;AACvC,iCAAyC;AACzC,+BAAuC;AACvC,qCAA6C;AAC7C,yDAG8B;AAE9B,sDAA6B;AAG7B,SAAS,oBAAoB,CAC3B,OAAO,EACP,EACE,iBAAiB,EACjB,aAAa,EACb,SA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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;AAUC,wDAAsB;AARxB,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;AAMC,wCAAc"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/routes/types.ts"],"names":[],"mappings":""}
|
package/dist/routes/validator.js
CHANGED
|
@@ -1,18 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.validateRequestBody = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
function validateRequestBody(validator, req) {
|
|
5
|
+
const { resourceName } = req.requestInfo;
|
|
6
|
+
let requestBody = req.body;
|
|
6
7
|
const validationResult = validator(resourceName, requestBody);
|
|
7
|
-
if (!validationResult)
|
|
8
|
+
if (!validationResult && typeof requestBody === 'object')
|
|
8
9
|
return requestBody;
|
|
9
|
-
if (typeof validationResult === 'string')
|
|
10
|
-
|
|
11
|
-
}
|
|
10
|
+
if (typeof validationResult === 'string')
|
|
11
|
+
return validationResult;
|
|
12
12
|
// The requestBody was replaced by something else.
|
|
13
13
|
if (validationResult)
|
|
14
14
|
requestBody = validationResult;
|
|
15
|
-
|
|
15
|
+
if (typeof requestBody === 'object') {
|
|
16
|
+
return requestBody;
|
|
17
|
+
}
|
|
18
|
+
else
|
|
19
|
+
return req.body;
|
|
16
20
|
}
|
|
17
21
|
exports.validateRequestBody = validateRequestBody;
|
|
18
22
|
//# sourceMappingURL=validator.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validator.js","sourceRoot":"","sources":["../../src/routes/validator.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"validator.js","sourceRoot":"","sources":["../../src/routes/validator.ts"],"names":[],"mappings":";;;AAEA,SAAS,mBAAmB,CAC1B,SAA4B,EAC5B,GAAG;IAEH,MAAM,EAAE,YAAY,EAAE,GAAG,GAAG,CAAC,WAAW,CAAA;IACxC,IAAI,WAAW,GAAG,GAAG,CAAC,IAAI,CAAA;IAE1B,MAAM,gBAAgB,GAAG,SAAS,CAAC,YAAY,EAAE,WAAW,CAAC,CAAA;IAE7D,IAAI,CAAC,gBAAgB,IAAI,OAAO,WAAW,KAAK,QAAQ;QAAE,OAAO,WAAW,CAAA;IAE5E,IAAI,OAAO,gBAAgB,KAAK,QAAQ;QAAE,OAAO,gBAAgB,CAAA;IAEjE,kDAAkD;IAClD,IAAI,gBAAgB;QAAE,WAAW,GAAG,gBAAgB,CAAA;IAEpD,IAAI,OAAO,WAAW,KAAK,QAAQ,EAAE;QACnC,OAAO,WAAW,CAAA;KACnB;;QAAM,OAAO,GAAG,CAAC,IAAI,CAAA;AACxB,CAAC;AAEQ,kDAAmB"}
|
package/dist/server.js
CHANGED
|
@@ -25,12 +25,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
25
25
|
exports.create = void 0;
|
|
26
26
|
const express_1 = __importStar(require("express"));
|
|
27
27
|
const morgan_1 = __importDefault(require("morgan"));
|
|
28
|
-
const
|
|
29
|
-
const routes_1 = require("./routes");
|
|
30
|
-
const queries_1 = require("./queries");
|
|
28
|
+
const errors_1 = require("./errors/errors");
|
|
29
|
+
const routes_1 = require("./routes/routes");
|
|
30
|
+
const queries_1 = require("./queries/queries");
|
|
31
31
|
const config_1 = require("./config");
|
|
32
32
|
const cors_1 = __importDefault(require("cors"));
|
|
33
|
-
const
|
|
33
|
+
const delayMiddleware_1 = require("./delay/delayMiddleware");
|
|
34
34
|
function createServer(userConfig) {
|
|
35
35
|
const config = (0, config_1.initConfig)(userConfig);
|
|
36
36
|
const queries = (0, queries_1.createQueries)(config.connectionString);
|
|
@@ -41,7 +41,7 @@ function createServer(userConfig) {
|
|
|
41
41
|
// Enable CORS for all requests.
|
|
42
42
|
app.use((0, cors_1.default)({ origin: true, credentials: true }));
|
|
43
43
|
if (config.delay > 0) {
|
|
44
|
-
const delayMiddleware = (0,
|
|
44
|
+
const delayMiddleware = (0, delayMiddleware_1.createDelayMiddleware)(config.delay);
|
|
45
45
|
app.use(delayMiddleware);
|
|
46
46
|
}
|
|
47
47
|
// Serve a static folder, if configured.
|
|
@@ -67,7 +67,7 @@ function createServer(userConfig) {
|
|
|
67
67
|
if (config.apiPrefix)
|
|
68
68
|
app.all(`${config.apiPrefix}*`, routes_1.handleMethodNotAllowed);
|
|
69
69
|
// Error middleware.
|
|
70
|
-
app.use(
|
|
70
|
+
app.use(errors_1.errorHandler);
|
|
71
71
|
return app;
|
|
72
72
|
}
|
|
73
73
|
function create(userConfig) {
|
package/dist/server.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,mDAAuC;AACvC,oDAA2B;AAC3B,
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,mDAAuC;AACvC,oDAA2B;AAC3B,4CAA8C;AAC9C,4CAKwB;AACxB,+CAAiD;AACjD,qCAA6C;AAC7C,gDAAuB;AACvB,6DAA+D;AAE/D,SAAS,YAAY,CAAC,UAAmB;IACvC,MAAM,MAAM,GAAG,IAAA,mBAAU,EAAC,UAAU,CAAC,CAAA;IAErC,MAAM,OAAO,GAAG,IAAA,uBAAa,EAAC,MAAM,CAAC,gBAAgB,CAAC,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uBAAc,CAAC,CAAA;QAC5B,GAAG,CAAC,IAAI,CAAC,GAAG,EAAE,uBAAc,CAAC,CAAA;QAC7B,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,uBAAc,CAAC,CAAA;QAC5B,GAAG,CAAC,MAAM,CAAC,GAAG,EAAE,uBAAc,CAAC,CAAA;KAChC;IAED,gDAAgD;IAChD,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,+BAAsB,CAAC,CAAA;IACpC,IAAI,MAAM,CAAC,SAAS;QAAE,GAAG,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,SAAS,GAAG,EAAE,+BAAsB,CAAC,CAAA;IAE7E,oBAAoB;IACpB,GAAG,CAAC,GAAG,CAAC,qBAAY,CAAC,CAAA;IAErB,OAAO,GAAG,CAAA;AACZ,CAAC;AAED,SAAgB,MAAM,CAAC,UAAmB;IACxC,OAAO,YAAY,CAAC,UAAU,CAAC,CAAA;AACjC,CAAC;AAFD,wBAEC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createValidateResourceMiddleware = exports.createResourceAndIdParser = void 0;
|
|
4
|
+
const errors_1 = require("../errors/errors");
|
|
5
|
+
const urlParser_1 = require("./urlParser");
|
|
6
|
+
function createResourceAndIdParser(apiPrefix) {
|
|
7
|
+
return function getResourceAndId(req, _, next) {
|
|
8
|
+
const url = req.baseUrl.replace(apiPrefix, '');
|
|
9
|
+
const urlInfo = (0, urlParser_1.parseUrl)(url);
|
|
10
|
+
req.requestInfo = Object.assign(Object.assign({}, req.requestInfo), urlInfo);
|
|
11
|
+
return next();
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
exports.createResourceAndIdParser = createResourceAndIdParser;
|
|
15
|
+
function createValidateResourceMiddleware(validateResources, resourceNames) {
|
|
16
|
+
return function validateResource(req, _, next) {
|
|
17
|
+
if (!validateResources)
|
|
18
|
+
return next();
|
|
19
|
+
const { resourceName } = req.requestInfo;
|
|
20
|
+
if (!resourceName)
|
|
21
|
+
return next();
|
|
22
|
+
if (!resourceNames.includes(resourceName.toLowerCase())) {
|
|
23
|
+
// TODO return a response instead of calling next
|
|
24
|
+
const error = (0, errors_1.new404NotFoundError)(`'${resourceName}' is an unknown resource`);
|
|
25
|
+
return next(error);
|
|
26
|
+
}
|
|
27
|
+
return next();
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
exports.createValidateResourceMiddleware = createValidateResourceMiddleware;
|
|
31
|
+
//# sourceMappingURL=urlMiddleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"urlMiddleware.js","sourceRoot":"","sources":["../../src/urls/urlMiddleware.ts"],"names":[],"mappings":";;;AAAA,6CAAsD;AACtD,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,OAAO,GAAG,IAAA,oBAAQ,EAAC,GAAG,CAAC,CAAA;QAE7B,GAAG,CAAC,WAAW,mCAAQ,GAAG,CAAC,WAAW,GAAK,OAAO,CAAE,CAAA;QAEpD,OAAO,IAAI,EAAE,CAAA;IACf,CAAC,CAAA;AACH,CAAC;AAsBQ,8DAAyB;AApBlC,SAAS,gCAAgC,CAAC,iBAAiB,EAAE,aAAa;IACxE,OAAO,SAAS,gBAAgB,CAAC,GAAG,EAAE,CAAC,EAAE,IAAI;QAC3C,IAAI,CAAC,iBAAiB;YAAE,OAAO,IAAI,EAAE,CAAA;QAErC,MAAM,EAAE,YAAY,EAAE,GAAG,GAAG,CAAC,WAAW,CAAA;QAExC,IAAI,CAAC,YAAY;YAAE,OAAO,IAAI,EAAE,CAAA;QAEhC,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,YAAY,CAAC,WAAW,EAAE,CAAC,EAAE;YACvD,iDAAiD;YACjD,MAAM,KAAK,GAAG,IAAA,4BAAmB,EAC/B,IAAI,YAAY,0BAA0B,CAC3C,CAAA;YACD,OAAO,IAAI,CAAC,KAAK,CAAC,CAAA;SACnB;QAED,OAAO,IAAI,EAAE,CAAA;IACf,CAAC,CAAA;AACH,CAAC;AAEmC,4EAAgC"}
|
package/package.json
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "temba",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "Get a simple MongoDB REST API with zero coding in less than 30 seconds (seriously).",
|
|
5
|
-
"main": "dist/server.
|
|
5
|
+
"main": "dist/server.ts",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"build": "tsc",
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
"lint": "eslint --ignore-path .eslintignore --ext .js,.ts .",
|
|
8
|
+
"test": "jest --watch",
|
|
9
|
+
"lint": "eslint --ignore-path .gitignore --ext .js,.ts .",
|
|
11
10
|
"format": "prettier --ignore-path .gitignore --write \"**/*.+(js|ts|json)\""
|
|
12
11
|
},
|
|
13
12
|
"repository": {
|
|
@@ -20,7 +19,7 @@
|
|
|
20
19
|
"files": [
|
|
21
20
|
"dist/**",
|
|
22
21
|
"package.json",
|
|
23
|
-
"!
|
|
22
|
+
"!tests"
|
|
24
23
|
],
|
|
25
24
|
"devDependencies": {
|
|
26
25
|
"@types/cors": "^2.8.12",
|