temba 0.7.2 → 0.7.6

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.
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.addCacheHeaders = addCacheHeaders;
7
+
8
+ function addCacheHeaders(req, res, next) {
9
+ if (req.method == 'GET') {
10
+ res.set('Cache-control', "public, max-age=300");
11
+ } else {
12
+ res.set('Cache-control', "no-store");
13
+ }
14
+
15
+ return next();
16
+ }
package/dist/server.js CHANGED
@@ -21,6 +21,10 @@ var _queries = require("./queries");
21
21
 
22
22
  var _config = require("./config");
23
23
 
24
+ var _cors = _interopRequireDefault(require("cors"));
25
+
26
+ var _middleware2 = require("./caching/middleware");
27
+
24
28
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
25
29
 
26
30
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
@@ -29,13 +33,21 @@ function createServer(userConfig) {
29
33
  var config = (0, _config.initConfig)(userConfig);
30
34
  var queries = (0, _queries.createQueries)(config.connectionString);
31
35
  var app = (0, _express["default"])();
32
- app.use((0, _express.json)());
33
- app.use((0, _morgan["default"])('tiny'));
36
+ app.use((0, _express.json)()); // Add HTTP request logging.
37
+
38
+ app.use((0, _morgan["default"])('tiny')); // Enable CORS for all requests.
39
+
40
+ app.use((0, _cors["default"])({
41
+ origin: true,
42
+ credentials: true
43
+ })); // Serve a static folder, if configured.
34
44
 
35
45
  if (config.staticFolder) {
36
46
  app.use(_express["default"]["static"](config.staticFolder));
37
- } // On the root URL (with apiPrefix if applicable) only a GET is allowed.
47
+ } // Add cache headers to every response.
48
+
38
49
 
50
+ app.use(_middleware2.addCacheHeaders); // On the root URL (with apiPrefix if applicable) only a GET is allowed.
39
51
 
40
52
  var rootPath = config.apiPrefix ? "".concat(config.apiPrefix) : '/';
41
53
  app.use(rootPath, _routes.rootRouter); // For all other URLs, only GET, POST, PUT and DELETE are allowed and handled.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "temba",
3
- "version": "0.7.2",
4
- "description": "",
3
+ "version": "0.7.6",
4
+ "description": "Get a simple MongoDB REST API with zero coding in less than 30 seconds (seriously).",
5
5
  "main": "dist/server.js",
6
6
  "scripts": {
7
7
  "build": "rm -rf dist && npx babel src --out-dir dist",
@@ -9,9 +9,10 @@
9
9
  },
10
10
  "repository": {
11
11
  "type": "git",
12
- "url": "temba"
12
+ "url": "https://github.com/bouwe77/temba.git"
13
13
  },
14
- "author": "Bouwe",
14
+ "homepage": "https://github.com/bouwe77/temba#readme",
15
+ "author": "Bouwe (https://bouwe.io)",
15
16
  "license": "ISC",
16
17
  "files": [
17
18
  "dist/**",
@@ -30,6 +31,7 @@
30
31
  "dependencies": {
31
32
  "@babel/runtime": "^7.15.4",
32
33
  "@rakered/mongo": "^1.6.0",
34
+ "cors": "^2.8.5",
33
35
  "express": "^4.17.1",
34
36
  "morgan": "^1.10.0"
35
37
  }
package/readme.md CHANGED
@@ -33,9 +33,13 @@ Prerequisites you need to have:
33
33
 
34
34
  > Wthout a database, Temba also works. However, then data is kept in memory and flushed every time the server restarts.
35
35
 
36
- ### Use the `temba-starter` project
36
+ ### Use `npx`
37
37
 
38
- Clone the [temba-starter](https://github.com/bouwe77/temba-starter) repo and you are up and running! Refer to the [Config settings overview](#config-settings-overview) section for configuration options.
38
+ Create your own Temba server with the following command and you are up and running!
39
+
40
+ ```bash
41
+ npx create-temba-server my-rest-api
42
+ ```
39
43
 
40
44
  ### Manually adding to an existing app
41
45
 
@@ -65,9 +69,9 @@ By passing a config object to the `create` function you can customize Temba's be
65
69
 
66
70
  Out of the box, Temba gives you a CRUD REST API to any resource name you can think of.
67
71
 
68
- 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 resource, then that will be returned upon a `GET` of that resource. You can also `DELETE`, or `PUT` resources by its ID, unless it does not exist of course.
72
+ 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.
69
73
 
70
- For a specific resource, in this case `/movies`, Temba supports the following requests:
74
+ For a every resource, for example `/movies`, Temba supports the following requests:
71
75
 
72
76
  - `GET /movies` - Get all movies
73
77
  - `GET /movies/:id` - Get a movie by its ID
@@ -78,7 +82,7 @@ For a specific resource, in this case `/movies`, Temba supports the following re
78
82
 
79
83
  ### Supported HTTP methods
80
84
 
81
- Requests with an HTTP method that is not supported, everything but `GET`, `POST`, `PUT` and `DELETE`, will return a `405 Method Not Allowed` response.
85
+ Requests with an HTTP method that is not supported, so everything but `GET`, `POST`, `PUT` and `DELETE`, a `405 Method Not Allowed` response will be returned.
82
86
 
83
87
  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.
84
88
 
@@ -108,12 +112,12 @@ Requests on these resources only give a `404 Not Found` if the ID does not exist
108
112
 
109
113
  ### JSON
110
114
 
115
+ Temba only supports JSON. If you send a request with invalid formatted JSON, a `400 Bad Request` response is returned.
116
+
111
117
  When sending JSON data (`POST` and `PUT` requests), adding a `Content-Type: application/json` header is required.
112
118
 
113
119
  IDs are auto generated when creating resources. IDs in the JSON request body are ignored.
114
120
 
115
- Temba only supports JSON. If you send a request with invalid formatted JSON, a `400 Bad Request` response is returned.
116
-
117
121
  ### Static assets
118
122
 
119
123
  If you want to host static assets next to the REST API, configure the `staticFolder`:
@@ -123,7 +127,7 @@ const config = { staticFolder: 'build' }
123
127
  const server = temba.create(config)
124
128
  ```
125
129
 
126
- This way, you can create a REST API, and the web app consuming it, in one project.
130
+ This way, you could create a REST API, and the web app consuming it, in one project.
127
131
 
128
132
  However, to avoid possible conflicts between the API resources and the routes in your web app you might want to add an `apiPrefix` to the REST API:
129
133
 
@@ -136,6 +140,10 @@ const config = { apiPrefix: 'api' }
136
140
  const server = temba.create(config)
137
141
  ```
138
142
 
143
+ 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.
144
+
145
+ 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.
146
+
139
147
  ### Config settings overview
140
148
 
141
149
  Configuring Temba is optional, it already works out of the box. None of the settings are applicable until you configure them:
@@ -193,10 +201,10 @@ Temba is built with JavaScript, Node, Express, Jest, Testing Library, Supertest,
193
201
 
194
202
  ## Which problem does Temba solve?
195
203
 
196
- The problem with JSON file solutions like json-server is the limitations you have when hosting your app.
204
+ 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.
197
205
 
198
- 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.
206
+ 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.
199
207
 
200
208
  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).
201
209
 
202
- These limitations are of course the whole idea behind json-server, it's for mocking and prototyping. But if you want more (persistence wise) and don't mind having a database, although not yet very feature rich, you might want to try Temba.
210
+ 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.