temba 0.7.10 → 0.9.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 (39) hide show
  1. package/README.md +321 -0
  2. package/dist/config/index.js +66 -61
  3. package/dist/config/index.js.map +1 -0
  4. package/dist/delay/middleware.js +14 -14
  5. package/dist/delay/middleware.js.map +1 -0
  6. package/dist/errors/index.js +11 -11
  7. package/dist/errors/index.js.map +1 -0
  8. package/dist/errors/middleware.js +9 -12
  9. package/dist/errors/middleware.js.map +1 -0
  10. package/dist/errors/types.js +13 -0
  11. package/dist/errors/types.js.map +1 -0
  12. package/dist/logging/index.js +37 -36
  13. package/dist/logging/index.js.map +1 -0
  14. package/dist/queries/in-memory.js +48 -79
  15. package/dist/queries/in-memory.js.map +1 -0
  16. package/dist/queries/index.js +15 -19
  17. package/dist/queries/index.js.map +1 -0
  18. package/dist/queries/mongo.js +73 -277
  19. package/dist/queries/mongo.js.map +1 -0
  20. package/dist/routes/delete.js +37 -80
  21. package/dist/routes/delete.js.map +1 -0
  22. package/dist/routes/get.js +42 -83
  23. package/dist/routes/get.js.map +1 -0
  24. package/dist/routes/index.js +51 -89
  25. package/dist/routes/index.js.map +1 -0
  26. package/dist/routes/post.js +42 -59
  27. package/dist/routes/post.js.map +1 -0
  28. package/dist/routes/put.js +38 -85
  29. package/dist/routes/put.js.map +1 -0
  30. package/dist/routes/validator.js +18 -0
  31. package/dist/routes/validator.js.map +1 -0
  32. package/dist/server.js +72 -75
  33. package/dist/server.js.map +1 -0
  34. package/dist/urls/middleware.js +26 -38
  35. package/dist/urls/middleware.js.map +1 -0
  36. package/dist/urls/urlParser.js +11 -20
  37. package/dist/urls/urlParser.js.map +1 -0
  38. package/package.json +21 -11
  39. package/readme.md +0 -214
package/README.md ADDED
@@ -0,0 +1,321 @@
1
+ # Temba
2
+
3
+ <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
4
+
5
+ [![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors-)
6
+
7
+ <!-- ALL-CONTRIBUTORS-BADGE:END -->
8
+
9
+ **Get a simple MongoDB REST API with zero coding in less than 30 seconds (seriously).**
10
+
11
+ For developers who need a quick backend for small projects.
12
+
13
+ Powered by NodeJS, Express and MongoDB.
14
+
15
+ This project is inspired by the fantastic [json-server](https://github.com/typicode/json-server) project, but instead of a JSON file Temba uses a real database. The goal, however, is the same: Get you started with a REST API very quickly.
16
+
17
+ ## Table of contents
18
+
19
+ [Temba?](#temba-1)
20
+
21
+ [Getting Started](#getting-started)
22
+
23
+ [Usage](#usage)
24
+
25
+ ## Temba?
26
+
27
+ > _"Temba, at rest"_
28
+
29
+ A metaphor for the declining of a gift, from the [Star Trek - The Next Generation, episode "Darmok"](https://memory-alpha.fandom.com/wiki/Temba).
30
+
31
+ In the fictional Tamarian language the word _"Temba"_ means something like _"gift"_.
32
+
33
+ ## Getting Started
34
+
35
+ Prerequisites you need to have:
36
+
37
+ - Node, NPM
38
+ - Optional: A MongoDB database, either locally or in the cloud
39
+
40
+ > Wthout a database, Temba also works. However, then data is kept in memory and flushed every time the server restarts.
41
+
42
+ ### Use `npx`
43
+
44
+ Create your own Temba server with the following command and you are up and running!
45
+
46
+ ```bash
47
+ npx create-temba-server my-rest-api
48
+ ```
49
+
50
+ With this command you clone the [Temba-starter](https://github.com/bouwe77/temba-starter) repository and install all dependencies.
51
+
52
+ ### Manually adding to an existing app
53
+
54
+ Alternatively, add Temba to your app manually:
55
+
56
+ 1. `npm i temba`
57
+
58
+ 2. Example code to create a Temba server:
59
+
60
+ ```js
61
+ import temba from ("temba");
62
+ const server = temba.create();
63
+
64
+ const port = process.env.PORT || 3000;
65
+ server.listen(port, () => {
66
+ console.log(`Temba is running on port ${port}`);
67
+ });
68
+ ```
69
+
70
+ ### Configuration
71
+
72
+ 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
+ ## Usage
75
+
76
+ ### Introduction
77
+
78
+ Out of the box, Temba gives you a CRUD REST API to any resource name you can think of.
79
+
80
+ Whether you `GET` either `/people`, `/movies`, `/pokemons`, or whatever, it all returns a `200 OK` with a `[]` JSON response. As soon as you `POST` a new resource, followed by a `GET` of that resource, the new resource will be returned. You can also `DELETE`, or `PUT` resources by its ID.
81
+
82
+ For every resource (`movies` is just an example), Temba supports the following requests:
83
+
84
+ - `GET /movies` - Get all movies
85
+ - `GET /movies/:id` - Get a movie by its ID
86
+ - `POST /movies` - Create a new movie
87
+ - `PUT /movies/:id` - Update (fully replace) a movie by its ID
88
+ - `DELETE /movies` - Delete all movies
89
+ - `DELETE /movies/:id` - Delete a movie by its ID
90
+
91
+ ### Supported HTTP methods
92
+
93
+ The HTTP methods that are supported are `GET`, `POST`, `PUT` and `DELETE`. For any other HTTP method a `405 Method Not Allowed` response will be returned.
94
+
95
+ 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
+
97
+ ### MongoDB
98
+
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:
100
+
101
+ ```js
102
+ const config = {
103
+ connectionString: 'mongodb://localhost:27017',
104
+ }
105
+ const server = temba.create(config)
106
+ ```
107
+
108
+ For every resource you use in your requests, a collection is created in the database. However, not until you actually store (create) a resource with a `POST`.
109
+
110
+ ### Allowing specific resources only
111
+
112
+ If you only want to allow specific resource names, configure them by providing a `resourceNames` key in the config object when creating the Temba server:
113
+
114
+ ```js
115
+ const config = { resourceNames: ['movies', 'actors'] }
116
+ const server = temba.create(config)
117
+ ```
118
+
119
+ 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
+
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
+ ### Static assets
130
+
131
+ If you want to host static assets next to the REST API, configure the `staticFolder`:
132
+
133
+ ```js
134
+ const config = { staticFolder: 'build' }
135
+ const server = temba.create(config)
136
+ ```
137
+
138
+ This way, you could create a REST API, and the web app consuming it, in one project.
139
+
140
+ However, to avoid possible conflicts between the API resources and the web app routes you might want to add an `apiPrefix` to the REST API:
141
+
142
+ ### REST URIs prefixes
143
+
144
+ With the `apiPrefix` config setting, all REST resources get an extra path segment in front of them. If the `apiPrefix` is `'api'`, then `/movies/12345` becomes `/api/movies/12345`:
145
+
146
+ ```js
147
+ const config = { apiPrefix: 'api' }
148
+ const server = temba.create(config)
149
+ ```
150
+
151
+ After configuring the `apiPrefix`, requests to the root URL will either return a `404 Not Found` or a `405 Method Not Allowed`, depending on the HTTP method.
152
+
153
+ If you have configured both an `apiPrefix` and a `staticFolder`, a `GET` on the root URL will return the `index.html` in the `staticFolder`, if there is one.
154
+
155
+ ### Request body validation or mutation
156
+
157
+ Temba does not validate request bodies.
158
+
159
+ This means you can store your resources in any format you like. So creating the following two (very different) _movies_ is perfectly fine:
160
+
161
+ ```
162
+ POST /movies
163
+ {
164
+ "title": "O Brother, Where Art Thou?",
165
+ "description": "In the deep south during the 1930s, three escaped convicts search for hidden treasure while a relentless lawman pursues them."
166
+ }
167
+
168
+ POST /movies
169
+ {
170
+ "foo": "bar",
171
+ "baz": "boo"
172
+ }
173
+ ```
174
+
175
+ You can even omit a request body when doing a `POST` or `PUT`. If you don't want that, and build proper validation, use the `requestBodyValidator` config setting:
176
+
177
+ If you want to do input validation before the `POST` or `PUT` request body is saved to the database, configure Temba as follows:
178
+
179
+ ```js
180
+ const config = {
181
+ requestBodyValidator: {
182
+ post: (resourceName, requestBody) => {
183
+ // Validate, or even change the requestBody
184
+ },
185
+ put: (resourceName, requestBody) => {
186
+ // Validate, or even change the requestBody
187
+ },
188
+ },
189
+ }
190
+
191
+ const server = temba.create(config)
192
+ ```
193
+
194
+ The `requestBodyValidator` is an object with a `post` and/or `put` field, which contains the callback function you want Temba to call before the JSON is saved to the database.
195
+
196
+ The callback function receives two arguments: The `resourceName`, which for example is `movies` if you request `POST /movies`. The second argument is the `requestBody`, which is the JSON object in the request body.
197
+
198
+ Your callback function can return the following things:
199
+
200
+ - `void`: Temba will just save the request body as-is. An example of this is when you have validated the request body and everything looks fine.
201
+ - `string`: If you return a string Temba will return a `400 Bad Request` with the string as error message.
202
+ - `object`: Return an object if you want to change the request body. Temba will save the returned object instead of the original request body.
203
+
204
+ Example:
205
+
206
+ ```js
207
+ const config = {
208
+ requestBodyValidator: {
209
+ post: (resourceName, requestBody) => {
210
+ // Do not allow Pokemons to be created: 400 Bad Request
211
+ if (resourceName === 'pokemons')
212
+ return 'You are not allowed to create new Pokemons'
213
+
214
+ // Add a genre to Star Trek films:
215
+ if (
216
+ resourceName === 'movies' &&
217
+ requestBody.title.startsWith('Star Trek')
218
+ )
219
+ return { ...requestBody, genre: 'Science Fiction' }
220
+
221
+ // If you end up here, void will be returned, so the request will just be saved.
222
+ },
223
+ },
224
+ }
225
+
226
+ const server = temba.create(config)
227
+ ```
228
+
229
+ ### Config settings overview
230
+
231
+ Configuring Temba is optional, it already works out of the box.
232
+
233
+ Here is an example of the config settings for Temba, and how you define them:
234
+
235
+ ```js
236
+ const config = {
237
+ resourceNames: ['movies', 'actors'],
238
+ connectionString: 'mongodb://localhost:27017',
239
+ staticFolder: 'build',
240
+ apiPrefix: 'api',
241
+ cacheControl: 'public, max-age=300',
242
+ delay: 500,
243
+ requestBodyValidator: {
244
+ post: (resourceName, requestBody) => {
245
+ // Validate, or even change the requestBody
246
+ },
247
+ put: (resourceName, requestBody) => {
248
+ // Validate, or even change the requestBody
249
+ },
250
+ },
251
+ }
252
+ const server = temba.create(config)
253
+ ```
254
+
255
+ None of the settings are required, and none of them have default values that actually do something. So only the settings you define are used.
256
+
257
+ These are all the possible settings:
258
+
259
+ | Config setting | Description |
260
+ | :--------------------- | :----------------------------------------------------------------------------------------- |
261
+ | `resourceNames` | See [Allowing specific resources only](#allowing-specific-resources-only) |
262
+ | `connectionString` | See [MongoDB](#mongodb) |
263
+ | `staticFolder` | See [Static assets](#static-assets) |
264
+ | `apiPrefix` | See [REST URIs prefixes](#rest-uris-prefixes) |
265
+ | `cacheControl` | The `Cache-control` response header value for each GET request. |
266
+ | `delay` | After processing the request, the delay in milliseconds before the request should be sent. |
267
+ | `requestBodyValidator` | See [Request body validation or mutation](#request-body-validation-or-mutation) |
268
+
269
+ ## Not supported (yet?)
270
+
271
+ The following features would be very nice for Temba to support:
272
+
273
+ ### `PATCH` requests
274
+
275
+ Partial updates using `PATCH`, or other HTTP methods are not (yet?) supported.
276
+
277
+ ### Auth
278
+
279
+ Temba offers no ways for authentication or authorization (yet?), so if someone knows how to reach the API, they can read and mutate all your data, unless you restrict this in another way.
280
+
281
+ ### Nested parent-child resources
282
+
283
+ Also nested (parent-child) routes are not supported (yet?), so every URI has the /:resource/:id structure and there is no way to indicate any relation, apart from within the JSON itself perhaps.
284
+
285
+ ### Filtering and sorting
286
+
287
+ And there is no filtering, sorting, searching, etc. (yet?).
288
+
289
+ ## Under the hood
290
+
291
+ Temba is built with JavaScript, [Node](https://nodejs.org), [Express](https://expressjs.com/), [Jest](https://jestjs.io/), [Testing Library](https://testing-library.com/), [Supertest](https://www.npmjs.com/package/supertest), and [@rakered/mongo](https://www.npmjs.com/package/@rakered/mongo).
292
+
293
+ ## Which problem does Temba solve?
294
+
295
+ The problem with JSON file solutions like json-server is the limitations you have when hosting your app, because your data is stored in a file.
296
+
297
+ For example, hosting json-server on GitHub Pages means your API is essentially readonly, because, although mutations are supported, your data is not really persisted.
298
+
299
+ And hosting json-server on Heroku does give you persistence, but is not reliable because of its [ephemeral filesystem](https://devcenter.heroku.com/articles/dynos#ephemeral-filesystem).
300
+
301
+ These limitations are of course the whole idea behind json-server, it's for simple mocking and prototyping. But if you want more (persistence wise) and don't mind having a database, you might want to try Temba.
302
+
303
+ ## Contributors ✨
304
+
305
+ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
306
+
307
+ <!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
308
+ <!-- prettier-ignore-start -->
309
+ <!-- markdownlint-disable -->
310
+ <table>
311
+ <tr>
312
+ <td align="center"><a href="https://bouwe.io"><img src="https://avatars.githubusercontent.com/u/4126793?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Bouwe K. Westerdijk</b></sub></a><br /><a href="https://github.com/bouwe77/temba/commits?author=bouwe77" title="Code">💻</a> <a href="https://github.com/bouwe77/temba/issues?q=author%3Abouwe77" title="Bug reports">🐛</a> <a href="https://github.com/bouwe77/temba/commits?author=bouwe77" title="Documentation">📖</a> <a href="#ideas-bouwe77" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/bouwe77/temba/commits?author=bouwe77" title="Tests">⚠️</a></td>
313
+ </tr>
314
+ </table>
315
+
316
+ <!-- markdownlint-restore -->
317
+ <!-- prettier-ignore-end -->
318
+
319
+ <!-- ALL-CONTRIBUTORS-LIST:END -->
320
+
321
+ This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
@@ -1,64 +1,69 @@
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
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.initConfig = void 0;
4
+ const logging_1 = require("../logging");
5
+ const defaultConfig = {
6
+ resourceNames: [],
7
+ validateResources: false,
8
+ logLevel: logging_1.logLevels.DEBUG,
9
+ staticFolder: null,
10
+ apiPrefix: '',
11
+ connectionString: null,
12
+ cacheControl: 'no-store',
13
+ delay: 0,
14
+ requestBodyValidator: {
15
+ post: () => {
16
+ // do nothing
17
+ },
18
+ put: () => {
19
+ // do nothing
20
+ },
21
+ },
27
22
  };
28
-
29
23
  function initConfig(userConfig) {
30
- if (!userConfig) return defaultConfig;
31
-
32
- var config = _objectSpread({}, defaultConfig);
33
-
34
- if (userConfig.resourceNames && userConfig.resourceNames.length > 0) {
35
- config.resourceNames = userConfig.resourceNames;
36
- config.validateResources = true;
37
- }
38
-
39
- if (userConfig.logLevel && userConfig.logLevel.length !== 0 && Object.keys(_logging.logLevels).includes(userConfig.logLevel.toUpperCase())) {
40
- config.logLevel = userConfig.logLevel.toUpperCase();
41
- }
42
-
43
- if (userConfig.staticFolder) {
44
- config.staticFolder = userConfig.staticFolder.replace(/[^a-zA-Z0-9]/g, '');
45
- }
46
-
47
- if (userConfig.apiPrefix) {
48
- config.apiPrefix = '/' + userConfig.apiPrefix.replace(/[^a-zA-Z0-9]/g, '') + '/';
49
- }
50
-
51
- if (userConfig.connectionString && userConfig.connectionString.length > 0) {
52
- config.connectionString = userConfig.connectionString;
53
- }
54
-
55
- if (userConfig.cacheControl && userConfig.cacheControl.length > 0) {
56
- config.cacheControl = userConfig.cacheControl;
57
- }
58
-
59
- if (userConfig.delay && userConfig.delay.length !== 0 && typeof Number(userConfig.delay) === 'number' && Number(userConfig.delay) > 0 && Number(userConfig.delay) < 10000) {
60
- config.delay = Number(userConfig.delay);
61
- }
62
-
63
- return config;
64
- }
24
+ if (!userConfig)
25
+ return defaultConfig;
26
+ const config = Object.assign({}, defaultConfig);
27
+ if (userConfig.resourceNames && userConfig.resourceNames.length > 0) {
28
+ config.resourceNames = userConfig.resourceNames;
29
+ config.validateResources = true;
30
+ }
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
+ if (userConfig.staticFolder) {
37
+ config.staticFolder = userConfig.staticFolder.replace(/[^a-zA-Z0-9]/g, '');
38
+ }
39
+ if (userConfig.apiPrefix) {
40
+ config.apiPrefix =
41
+ '/' + userConfig.apiPrefix.replace(/[^a-zA-Z0-9]/g, '') + '/';
42
+ }
43
+ if (userConfig.connectionString && userConfig.connectionString.length > 0) {
44
+ config.connectionString = userConfig.connectionString;
45
+ }
46
+ if (userConfig.cacheControl && userConfig.cacheControl.length > 0) {
47
+ config.cacheControl = userConfig.cacheControl;
48
+ }
49
+ if (userConfig.delay &&
50
+ userConfig.delay.length !== 0 &&
51
+ typeof Number(userConfig.delay) === 'number' &&
52
+ Number(userConfig.delay) > 0 &&
53
+ Number(userConfig.delay) < 10000) {
54
+ config.delay = Number(userConfig.delay);
55
+ }
56
+ if (userConfig.requestBodyValidator) {
57
+ if (userConfig.requestBodyValidator.post &&
58
+ typeof userConfig.requestBodyValidator.post === 'function') {
59
+ config.requestBodyValidator.post = userConfig.requestBodyValidator.post;
60
+ }
61
+ if (userConfig.requestBodyValidator.put &&
62
+ typeof userConfig.requestBodyValidator.put === 'function') {
63
+ config.requestBodyValidator.put = userConfig.requestBodyValidator.put;
64
+ }
65
+ }
66
+ return config;
67
+ }
68
+ exports.initConfig = initConfig;
69
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/config/index.ts"],"names":[],"mappings":"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"}
@@ -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"}
@@ -1,13 +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
-
8
- function new404NotFoundError() {
9
- var message = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'Not Found';
10
- var error = new Error(message);
11
- error.status = 404;
12
- return error;
13
- }
9
+ function new400BadRequestError(message = 'Bad Request') {
10
+ return new types_1.TembaError(message, 400);
11
+ }
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,MAAc;QAC5C,KAAK,CAAC,GAAG,CAAC,CAAA;QADoB,WAAM,GAAN,MAAM,CAAQ;QAE5C,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"}