temba 0.8.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.
Files changed (50) hide show
  1. package/README.md +38 -30
  2. package/dist/config/index.js +57 -73
  3. package/dist/config/index.js.map +1 -0
  4. package/dist/delay/delayMiddleware.js +16 -0
  5. package/dist/delay/delayMiddleware.js.map +1 -0
  6. package/dist/delay/middleware.js +14 -14
  7. package/dist/delay/middleware.js.map +1 -0
  8. package/dist/errors/errors.js +34 -0
  9. package/dist/errors/errors.js.map +1 -0
  10. package/dist/errors/index.js +10 -18
  11. package/dist/errors/index.js.map +1 -0
  12. package/dist/errors/middleware.js +9 -12
  13. package/dist/errors/middleware.js.map +1 -0
  14. package/dist/errors/types.js +13 -0
  15. package/dist/errors/types.js.map +1 -0
  16. package/dist/logging/index.js +37 -36
  17. package/dist/logging/index.js.map +1 -0
  18. package/dist/queries/in-memory.js +48 -79
  19. package/dist/queries/in-memory.js.map +1 -0
  20. package/dist/queries/index.js +15 -19
  21. package/dist/queries/index.js.map +1 -0
  22. package/dist/queries/mongo.js +73 -277
  23. package/dist/queries/mongo.js.map +1 -0
  24. package/dist/queries/queries.js +17 -0
  25. package/dist/queries/queries.js.map +1 -0
  26. package/dist/routes/delete.js +37 -80
  27. package/dist/routes/delete.js.map +1 -0
  28. package/dist/routes/get.js +42 -81
  29. package/dist/routes/get.js.map +1 -0
  30. package/dist/routes/index.js +51 -90
  31. package/dist/routes/index.js.map +1 -0
  32. package/dist/routes/post.js +43 -61
  33. package/dist/routes/post.js.map +1 -0
  34. package/dist/routes/put.js +41 -88
  35. package/dist/routes/put.js.map +1 -0
  36. package/dist/routes/routes.js +56 -0
  37. package/dist/routes/routes.js.map +1 -0
  38. package/dist/routes/types.js +3 -0
  39. package/dist/routes/types.js.map +1 -0
  40. package/dist/routes/validator.js +20 -19
  41. package/dist/routes/validator.js.map +1 -0
  42. package/dist/server.js +72 -75
  43. package/dist/server.js.map +1 -0
  44. package/dist/urls/middleware.js +26 -38
  45. package/dist/urls/middleware.js.map +1 -0
  46. package/dist/urls/urlMiddleware.js +31 -0
  47. package/dist/urls/urlMiddleware.js.map +1 -0
  48. package/dist/urls/urlParser.js +11 -20
  49. package/dist/urls/urlParser.js.map +1 -0
  50. package/package.json +22 -13
package/README.md CHANGED
@@ -1,9 +1,8 @@
1
1
  # Temba
2
2
 
3
+ [![Temba on NPM](https://img.shields.io/npm/v/temba)](https://www.npmjs.com/package/temba)
3
4
  <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
4
-
5
5
  [![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#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
- With this command you clone the [Temba-starter](https://github.com/bouwe77/temba-starter) repository and install all dependencies.
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
- ## Usage
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
- ### MongoDB
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.
103
+
104
+ IDs are auto generated when creating resources. IDs in the JSON request body are ignored for any request.
98
105
 
99
- When starting Temba, you can send your requests to it immediately. However, then the data resides in memory and is flushed as soon as the server restarts. To persist your data, provide the `connectionString` config setting for your MongoDB database:
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`:
@@ -179,13 +182,13 @@ If you want to do input validation before the `POST` or `PUT` request body is sa
179
182
  ```js
180
183
  const config = {
181
184
  requestBodyValidator: {
182
- post: (resourceName, requestBody) {
185
+ post: (resourceName, requestBody) => {
183
186
  // Validate, or even change the requestBody
184
187
  },
185
- put: (resourceName, requestBody) {
188
+ put: (resourceName, requestBody) => {
186
189
  // Validate, or even change the requestBody
187
- }
188
- }
190
+ },
191
+ },
189
192
  }
190
193
 
191
194
  const server = temba.create(config)
@@ -206,16 +209,21 @@ Example:
206
209
  ```js
207
210
  const config = {
208
211
  requestBodyValidator: {
209
- post: (resourceName, requestBody) {
212
+ post: (resourceName, requestBody) => {
210
213
  // Do not allow Pokemons to be created: 400 Bad Request
211
- if (resourceName === 'pokemons') return 'You are not allowed to create new Pokemons'
214
+ if (resourceName === 'pokemons')
215
+ return 'You are not allowed to create new Pokemons'
212
216
 
213
217
  // Add a genre to Star Trek films:
214
- if (resourceName === 'movies' && requestBody.title.startsWith('Star Trek')) return {...requestBody, genre: 'Science Fiction'}
218
+ if (
219
+ resourceName === 'movies' &&
220
+ requestBody.title.startsWith('Star Trek')
221
+ )
222
+ return { ...requestBody, genre: 'Science Fiction' }
215
223
 
216
224
  // If you end up here, void will be returned, so the request will just be saved.
217
225
  },
218
- }
226
+ },
219
227
  }
220
228
 
221
229
  const server = temba.create(config)
@@ -236,13 +244,13 @@ const config = {
236
244
  cacheControl: 'public, max-age=300',
237
245
  delay: 500,
238
246
  requestBodyValidator: {
239
- post: (resourceName, requestBody) {
247
+ post: (resourceName, requestBody) => {
240
248
  // Validate, or even change the requestBody
241
249
  },
242
- put: (resourceName, requestBody) {
250
+ put: (resourceName, requestBody) => {
243
251
  // Validate, or even change the requestBody
244
- }
245
- }
252
+ },
253
+ },
246
254
  }
247
255
  const server = temba.create(config)
248
256
  ```
@@ -259,7 +267,7 @@ These are all the possible settings:
259
267
  | `apiPrefix` | See [REST URIs prefixes](#rest-uris-prefixes) |
260
268
  | `cacheControl` | The `Cache-control` response header value for each GET request. |
261
269
  | `delay` | After processing the request, the delay in milliseconds before the request should be sent. |
262
- | `requestBodyValidator` | Bla |
270
+ | `requestBodyValidator` | See [Request body validation or mutation](#request-body-validation-or-mutation) |
263
271
 
264
272
  ## Not supported (yet?)
265
273
 
@@ -1,78 +1,62 @@
1
1
  "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
- Object.defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- exports.initConfig = initConfig;
9
-
10
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
-
12
- var _logging = require("../logging");
13
-
14
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
15
-
16
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
17
-
18
- var defaultConfig = {
19
- resourceNames: [],
20
- validateResources: false,
21
- logLevel: _logging.logLevels.DEBUG,
22
- staticFolder: null,
23
- apiPrefix: '',
24
- connectionString: null,
25
- cacheControl: 'no-store',
26
- delay: 0,
27
- requestBodyValidator: {
28
- post: function post() {},
29
- put: function put() {}
30
- }
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.initConfig = void 0;
4
+ const defaultConfig = {
5
+ resourceNames: [],
6
+ validateResources: false,
7
+ staticFolder: null,
8
+ apiPrefix: '',
9
+ connectionString: null,
10
+ cacheControl: 'no-store',
11
+ delay: 0,
12
+ requestBodyValidator: {
13
+ post: () => {
14
+ // do nothing
15
+ },
16
+ put: () => {
17
+ // do nothing
18
+ },
19
+ },
31
20
  };
32
-
33
21
  function initConfig(userConfig) {
34
- if (!userConfig) return defaultConfig;
35
-
36
- var config = _objectSpread({}, defaultConfig);
37
-
38
- if (userConfig.resourceNames && userConfig.resourceNames.length > 0) {
39
- config.resourceNames = userConfig.resourceNames;
40
- config.validateResources = true;
41
- }
42
-
43
- if (userConfig.logLevel && userConfig.logLevel.length !== 0 && Object.keys(_logging.logLevels).includes(userConfig.logLevel.toUpperCase())) {
44
- config.logLevel = userConfig.logLevel.toUpperCase();
45
- }
46
-
47
- if (userConfig.staticFolder) {
48
- config.staticFolder = userConfig.staticFolder.replace(/[^a-zA-Z0-9]/g, '');
49
- }
50
-
51
- if (userConfig.apiPrefix) {
52
- config.apiPrefix = '/' + userConfig.apiPrefix.replace(/[^a-zA-Z0-9]/g, '') + '/';
53
- }
54
-
55
- if (userConfig.connectionString && userConfig.connectionString.length > 0) {
56
- config.connectionString = userConfig.connectionString;
57
- }
58
-
59
- if (userConfig.cacheControl && userConfig.cacheControl.length > 0) {
60
- config.cacheControl = userConfig.cacheControl;
61
- }
62
-
63
- if (userConfig.delay && userConfig.delay.length !== 0 && typeof Number(userConfig.delay) === 'number' && Number(userConfig.delay) > 0 && Number(userConfig.delay) < 10000) {
64
- config.delay = Number(userConfig.delay);
65
- }
66
-
67
- if (userConfig.requestBodyValidator) {
68
- if (userConfig.requestBodyValidator.post && typeof userConfig.requestBodyValidator.post === 'function') {
69
- config.requestBodyValidator.post = userConfig.requestBodyValidator.post;
22
+ if (!userConfig)
23
+ return defaultConfig;
24
+ const config = Object.assign({}, defaultConfig);
25
+ if (userConfig.resourceNames && userConfig.resourceNames.length > 0) {
26
+ config.resourceNames = userConfig.resourceNames;
27
+ config.validateResources = true;
28
+ }
29
+ if (userConfig.staticFolder) {
30
+ config.staticFolder = userConfig.staticFolder.replace(/[^a-zA-Z0-9]/g, '');
31
+ }
32
+ if (userConfig.apiPrefix) {
33
+ config.apiPrefix =
34
+ '/' + userConfig.apiPrefix.replace(/[^a-zA-Z0-9]/g, '') + '/';
35
+ }
36
+ if (userConfig.connectionString && userConfig.connectionString.length > 0) {
37
+ config.connectionString = userConfig.connectionString;
70
38
  }
71
-
72
- if (userConfig.requestBodyValidator.put && typeof userConfig.requestBodyValidator.put === 'function') {
73
- config.requestBodyValidator.put = userConfig.requestBodyValidator.put;
39
+ if (userConfig.cacheControl && userConfig.cacheControl.length > 0) {
40
+ config.cacheControl = userConfig.cacheControl;
74
41
  }
75
- }
76
-
77
- return config;
78
- }
42
+ if (userConfig.delay &&
43
+ userConfig.delay !== 0 &&
44
+ typeof Number(userConfig.delay) === 'number' &&
45
+ Number(userConfig.delay) > 0 &&
46
+ Number(userConfig.delay) < 10000) {
47
+ config.delay = Number(userConfig.delay);
48
+ }
49
+ if (userConfig.requestBodyValidator) {
50
+ if (userConfig.requestBodyValidator.post &&
51
+ typeof userConfig.requestBodyValidator.post === 'function') {
52
+ config.requestBodyValidator.post = userConfig.requestBodyValidator.post;
53
+ }
54
+ if (userConfig.requestBodyValidator.put &&
55
+ typeof userConfig.requestBodyValidator.put === 'function') {
56
+ config.requestBodyValidator.put = userConfig.requestBodyValidator.put;
57
+ }
58
+ }
59
+ return config;
60
+ }
61
+ exports.initConfig = initConfig;
62
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
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,CAAA;IAEnC,IAAI,UAAU,CAAC,aAAa,IAAI,UAAU,CAAC,aAAa,CAAC,MAAM,GAAG,CAAC,EAAE;QACnE,MAAM,CAAC,aAAa,GAAG,UAAU,CAAC,aAAa,CAAA;QAC/C,MAAM,CAAC,iBAAiB,GAAG,IAAI,CAAA;KAChC;IAED,IAAI,UAAU,CAAC,YAAY,EAAE;QAC3B,MAAM,CAAC,YAAY,GAAG,UAAU,CAAC,YAAY,CAAC,OAAO,CAAC,eAAe,EAAE,EAAE,CAAC,CAAA;KAC3E;IAED,IAAI,UAAU,CAAC,SAAS,EAAE;QACxB,MAAM,CAAC,SAAS;YACd,GAAG,GAAG,UAAU,CAAC,SAAS,CAAC,OAAO,CAAC,eAAe,EAAE,EAAE,CAAC,GAAG,GAAG,CAAA;KAChE;IAED,IAAI,UAAU,CAAC,gBAAgB,IAAI,UAAU,CAAC,gBAAgB,CAAC,MAAM,GAAG,CAAC,EAAE;QACzE,MAAM,CAAC,gBAAgB,GAAG,UAAU,CAAC,gBAAgB,CAAA;KACtD;IAED,IAAI,UAAU,CAAC,YAAY,IAAI,UAAU,CAAC,YAAY,CAAC,MAAM,GAAG,CAAC,EAAE;QACjE,MAAM,CAAC,YAAY,GAAG,UAAU,CAAC,YAAY,CAAA;KAC9C;IAED,IACE,UAAU,CAAC,KAAK;QAChB,UAAU,CAAC,KAAK,KAAK,CAAC;QACtB,OAAO,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,KAAK,QAAQ;QAC5C,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC;QAC5B,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,GAAG,KAAK,EAChC;QACA,MAAM,CAAC,KAAK,GAAG,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,CAAA;KACxC;IAED,IAAI,UAAU,CAAC,oBAAoB,EAAE;QACnC,IACE,UAAU,CAAC,oBAAoB,CAAC,IAAI;YACpC,OAAO,UAAU,CAAC,oBAAoB,CAAC,IAAI,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"}
@@ -1,16 +1,16 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.createDelayMiddleware = createDelayMiddleware;
7
-
8
- var pause = require('connect-pause');
9
-
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"));
10
8
  function createDelayMiddleware(delay) {
11
- return function (req, res, next) {
12
- console.log('Start delay...');
13
- pause(delay)(req, res, next);
14
- console.log('Delay finished!');
15
- };
16
- }
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=middleware.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"middleware.js","sourceRoot":"","sources":["../../src/delay/middleware.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"}
@@ -1,21 +1,13 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.new400BadRequestError = exports.new404NotFoundError = void 0;
4
+ const types_1 = require("./types");
5
+ function new404NotFoundError(message = 'Not Found') {
6
+ return new types_1.TembaError(message, 404);
7
+ }
6
8
  exports.new404NotFoundError = new404NotFoundError;
7
- exports.new400BadRequestError = new400BadRequestError;
8
-
9
- function new404NotFoundError() {
10
- var message = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'Not Found';
11
- var error = new Error(message);
12
- error.status = 404;
13
- return error;
9
+ function new400BadRequestError(message = 'Bad Request') {
10
+ return new types_1.TembaError(message, 400);
14
11
  }
15
-
16
- function new400BadRequestError() {
17
- var message = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'Bad Request';
18
- var error = new Error(message);
19
- error.status = 400;
20
- return error;
21
- }
12
+ exports.new400BadRequestError = new400BadRequestError;
13
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/errors/index.ts"],"names":[],"mappings":";;;AAAA,mCAAoC;AAEpC,SAAS,mBAAmB,CAAC,OAAO,GAAG,WAAW;IAChD,OAAO,IAAI,kBAAU,CAAC,OAAO,EAAE,GAAG,CAAC,CAAA;AACrC,CAAC;AAMQ,kDAAmB;AAJ5B,SAAS,qBAAqB,CAAC,OAAO,GAAG,aAAa;IACpD,OAAO,IAAI,kBAAU,CAAC,OAAO,EAAE,GAAG,CAAC,CAAA;AACrC,CAAC;AAE6B,sDAAqB"}
@@ -1,14 +1,11 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.errorHandler = void 0;
4
+ function errorHandler(err, _, res) {
5
+ console.log('errorHandler: ' + err.message);
6
+ if (!err.status)
7
+ err.status = 500;
8
+ return res.status(err.status).json({ message: err.message });
9
+ }
6
10
  exports.errorHandler = errorHandler;
7
-
8
- function errorHandler(err, _, res, next) {
9
- console.log('errorHandler: ' + err.message);
10
- if (!err.status) err.status = 500;
11
- return res.status(err.status).json({
12
- message: err.message
13
- });
14
- }
11
+ //# sourceMappingURL=middleware.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"middleware.js","sourceRoot":"","sources":["../../src/errors/middleware.ts"],"names":[],"mappings":";;;AAAA,SAAS,YAAY,CAAC,GAAG,EAAE,CAAC,EAAE,GAAG;IAC/B,OAAO,CAAC,GAAG,CAAC,gBAAgB,GAAG,GAAG,CAAC,OAAO,CAAC,CAAA;IAE3C,IAAI,CAAC,GAAG,CAAC,MAAM;QAAE,GAAG,CAAC,MAAM,GAAG,GAAG,CAAA;IAEjC,OAAO,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,CAAC,CAAA;AAC9D,CAAC;AAEQ,oCAAY"}
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TembaError = void 0;
4
+ class TembaError extends Error {
5
+ constructor(msg, status) {
6
+ super(msg);
7
+ this.status = status;
8
+ Object.setPrototypeOf(this, TembaError.prototype);
9
+ this.status = status;
10
+ }
11
+ }
12
+ exports.TembaError = TembaError;
13
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
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"}
@@ -1,38 +1,39 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.createLogger = createLogger;
7
- exports.logLevels = void 0;
8
- var DEBUG = 'DEBUG';
9
- var INFO = 'INFO'; //TODO Add 'NONE'
10
-
11
- var logLevels = {
12
- DEBUG: DEBUG,
13
- INFO: INFO
14
- };
15
- exports.logLevels = logLevels;
16
-
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createLogger = exports.logLevels = void 0;
4
+ const DEBUG = 'DEBUG';
5
+ const INFO = 'INFO';
6
+ //TODO Add 'NONE'
7
+ exports.logLevels = { DEBUG, INFO };
17
8
  function createLogger(logLevel) {
18
- return {
19
- debug: function debug(message) {
20
- try {
21
- if (logLevel === DEBUG) console.log(new Date(), 'DEBUG -', message);
22
- } catch (_unused) {//swallow exceptions during logging
23
- }
24
- },
25
- info: function info(message) {
26
- try {
27
- if (logLevel === DEBUG || logLevel === INFO) console.info(new Date(), 'INFO -', message);
28
- } catch (_unused2) {//swallow exceptions during logging
29
- }
30
- },
31
- error: function error(message) {
32
- try {
33
- console.error(new Date(), 'ERROR -', message);
34
- } catch (_unused3) {//swallow exceptions during logging
35
- }
36
- }
37
- };
38
- }
9
+ return {
10
+ debug: function (message) {
11
+ try {
12
+ if (logLevel === DEBUG)
13
+ console.log(new Date(), 'DEBUG -', message);
14
+ }
15
+ catch (_a) {
16
+ //swallow exceptions during logging
17
+ }
18
+ },
19
+ info: function (message) {
20
+ try {
21
+ if (logLevel === DEBUG || logLevel === INFO)
22
+ console.info(new Date(), 'INFO -', message);
23
+ }
24
+ catch (_a) {
25
+ //swallow exceptions during logging
26
+ }
27
+ },
28
+ error: function (message) {
29
+ try {
30
+ console.error(new Date(), 'ERROR -', message);
31
+ }
32
+ catch (_a) {
33
+ //swallow exceptions during logging
34
+ }
35
+ },
36
+ };
37
+ }
38
+ exports.createLogger = createLogger;
39
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/logging/index.ts"],"names":[],"mappings":";;;AAAA,MAAM,KAAK,GAAG,OAAO,CAAA;AACrB,MAAM,IAAI,GAAG,MAAM,CAAA;AACnB,iBAAiB;AAEJ,QAAA,SAAS,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,CAAA;AAExC,SAAgB,YAAY,CAAC,QAAQ;IACnC,OAAO;QACL,KAAK,EAAE,UAAU,OAAO;YACtB,IAAI;gBACF,IAAI,QAAQ,KAAK,KAAK;oBAAE,OAAO,CAAC,GAAG,CAAC,IAAI,IAAI,EAAE,EAAE,SAAS,EAAE,OAAO,CAAC,CAAA;aACpE;YAAC,WAAM;gBACN,mCAAmC;aACpC;QACH,CAAC;QAED,IAAI,EAAE,UAAU,OAAO;YACrB,IAAI;gBACF,IAAI,QAAQ,KAAK,KAAK,IAAI,QAAQ,KAAK,IAAI;oBACzC,OAAO,CAAC,IAAI,CAAC,IAAI,IAAI,EAAE,EAAE,SAAS,EAAE,OAAO,CAAC,CAAA;aAC/C;YAAC,WAAM;gBACN,mCAAmC;aACpC;QACH,CAAC;QAED,KAAK,EAAE,UAAU,OAAO;YACtB,IAAI;gBACF,OAAO,CAAC,KAAK,CAAC,IAAI,IAAI,EAAE,EAAE,SAAS,EAAE,OAAO,CAAC,CAAA;aAC9C;YAAC,WAAM;gBACN,mCAAmC;aACpC;QACH,CAAC;KACF,CAAA;AACH,CAAC;AA3BD,oCA2BC"}