temba 0.30.0 → 0.30.2
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 +0 -7
- package/auth/auth.d.ts +6 -0
- package/auth/auth.d.ts.map +1 -0
- package/auth/auth.js +18 -0
- package/auth/auth.js.map +1 -0
- package/index.d.ts.map +1 -1
- package/index.js +20 -12
- package/index.js.map +1 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -280,16 +280,12 @@ const config = {
|
|
|
280
280
|
delete: ({ resource, id }) => {
|
|
281
281
|
//...
|
|
282
282
|
},
|
|
283
|
-
head: ({ resource, id }) => {
|
|
284
|
-
//...
|
|
285
|
-
},
|
|
286
283
|
},
|
|
287
284
|
}
|
|
288
285
|
|
|
289
286
|
const server = temba.create(config)
|
|
290
287
|
```
|
|
291
288
|
|
|
292
|
-
|
|
293
289
|
The `requestInterceptor` is an object with fields for each of the HTTP methods you might want to intercept, and the callback function you want Temba to call, before processing the request, i.e. going to the database.
|
|
294
290
|
|
|
295
291
|
Each callback function receives an object containing the `resource` (e.g. `"movies"`). Depending on the HTTP method, also the `id` from the URL, and the request `body` are provided. `body` is a JSON object of the request body.
|
|
@@ -461,9 +457,6 @@ const config = {
|
|
|
461
457
|
delete: ({ resource, id }) => {
|
|
462
458
|
//...
|
|
463
459
|
},
|
|
464
|
-
head: ({ resource, id }) => {
|
|
465
|
-
//...
|
|
466
|
-
},
|
|
467
460
|
},
|
|
468
461
|
resources: ['movies', 'actors'],
|
|
469
462
|
responseBodyInterceptor: ({ resource, body, id }) => {
|
package/auth/auth.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import 'dotenv/config';
|
|
2
|
+
import type { NextFunction, Request, Response } from 'express';
|
|
3
|
+
import type { Queries } from '../data/types.js';
|
|
4
|
+
export declare const isAuthEnabled: () => boolean;
|
|
5
|
+
export declare const createAuthMiddleware: (queries: Queries) => (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
6
|
+
//# sourceMappingURL=auth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../../src/auth/auth.ts"],"names":[],"mappings":"AAAA,OAAO,eAAe,CAAA;AACtB,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAC9D,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAE5C,eAAO,MAAM,aAAa,eAAwD,CAAA;AAElF,eAAO,MAAM,oBAAoB,YAAa,OAAO,WAChC,OAAO,OAAO,QAAQ,QAAQ,YAAY,kBAiB9D,CAAA"}
|
package/auth/auth.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import 'dotenv/config';
|
|
2
|
+
export const isAuthEnabled = () => process.env.FEATURE_FLAG_SIMPLE_AUTH === 'true';
|
|
3
|
+
export const createAuthMiddleware = (queries) => {
|
|
4
|
+
return async (req, res, next) => {
|
|
5
|
+
const token = req.headers['x-token'];
|
|
6
|
+
if (!token || typeof token !== 'string' || token.length === 0) {
|
|
7
|
+
res.status(401).json({ message: 'Unauthorized' });
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
const foundToken = await queries.getById('tokens', token);
|
|
11
|
+
if (!foundToken) {
|
|
12
|
+
res.status(401).json({ message: 'Unauthorized' });
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
next();
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
//# sourceMappingURL=auth.js.map
|
package/auth/auth.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../../../src/auth/auth.ts"],"names":[],"mappings":"AAAA,OAAO,eAAe,CAAA;AAItB,MAAM,CAAC,MAAM,aAAa,GAAG,GAAG,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,wBAAwB,KAAK,MAAM,CAAA;AAElF,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAC,OAAgB,EAAE,EAAE;IACvD,OAAO,KAAK,EAAE,GAAY,EAAE,GAAa,EAAE,IAAkB,EAAE,EAAE;QAC/D,MAAM,KAAK,GAAG,GAAG,CAAC,OAAO,CAAC,SAAS,CAAC,CAAA;QAEpC,IAAI,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC9D,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,cAAc,EAAE,CAAC,CAAA;YACjD,OAAM;QACR,CAAC;QAED,MAAM,UAAU,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAA;QAEzD,IAAI,CAAC,UAAU,EAAE,CAAC;YAChB,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,cAAc,EAAE,CAAC,CAAA;YACjD,OAAM;QACR,CAAC;QAED,IAAI,EAAE,CAAA;IACR,CAAC,CAAA;AACH,CAAC,CAAA"}
|
package/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAK1C,OAAO,EAAE,UAAU,IAAI,kBAAkB,EAAE,MAAM,iCAAiC,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAK1C,OAAO,EAAE,UAAU,IAAI,kBAAkB,EAAE,MAAM,iCAAiC,CAAA;AAsGlF,eAAO,MAAM,MAAM,gBAAiB,UAAU;;;CAA6B,CAAA;AAE3E,eAAO,MAAM,UAAU,2BAAqB,CAAA"}
|
package/index.js
CHANGED
|
@@ -7,6 +7,15 @@ import { createDelayMiddleware } from './delay/delayMiddleware.js';
|
|
|
7
7
|
import { compileSchemas } from './schema/compile.js';
|
|
8
8
|
import { createResourceRouter } from './resourceRouter.js';
|
|
9
9
|
import { TembaError as TembaErrorInternal } from './requestInterceptor/TembaError.js';
|
|
10
|
+
import { createAuthMiddleware, isAuthEnabled } from './auth/auth.js';
|
|
11
|
+
// Route for handling not allowed methods.
|
|
12
|
+
const handleMethodNotAllowed = (_, res) => {
|
|
13
|
+
res.status(405).json({ message: 'Method Not Allowed' });
|
|
14
|
+
};
|
|
15
|
+
// Route for handling not found.
|
|
16
|
+
const handleNotFound = (_, res) => {
|
|
17
|
+
res.status(404).json({ message: 'Not Found' });
|
|
18
|
+
};
|
|
10
19
|
const createServer = (userConfig) => {
|
|
11
20
|
const config = initConfig(userConfig);
|
|
12
21
|
const queries = createQueries(config.connectionString);
|
|
@@ -16,15 +25,22 @@ const createServer = (userConfig) => {
|
|
|
16
25
|
app.use(morgan('tiny'));
|
|
17
26
|
// Enable CORS for all requests.
|
|
18
27
|
app.use(cors({ origin: true, credentials: true }));
|
|
28
|
+
// Serve a static folder, if configured.
|
|
29
|
+
// Because it is defined before the auth middleware, the static folder is served without authentication,
|
|
30
|
+
// because it is not convenient to add an auth header to a web page in the browser.
|
|
31
|
+
if (config.staticFolder) {
|
|
32
|
+
app.use(express.static(config.staticFolder));
|
|
33
|
+
}
|
|
34
|
+
// If enabled, add auth middleware to all requests, and disable the tokens resource.
|
|
35
|
+
if (isAuthEnabled()) {
|
|
36
|
+
app.use(createAuthMiddleware(queries));
|
|
37
|
+
app.all('/tokens', handleNotFound);
|
|
38
|
+
}
|
|
19
39
|
// Add a delay to every request, if configured.
|
|
20
40
|
if (config.delay > 0) {
|
|
21
41
|
const delayMiddleware = createDelayMiddleware(config.delay);
|
|
22
42
|
app.use(delayMiddleware);
|
|
23
43
|
}
|
|
24
|
-
// Serve a static folder, if configured.
|
|
25
|
-
if (config.staticFolder) {
|
|
26
|
-
app.use(express.static(config.staticFolder));
|
|
27
|
-
}
|
|
28
44
|
// On the root URL (with apiPrefix, if applicable) only a GET is allowed.
|
|
29
45
|
const rootRouter = express.Router();
|
|
30
46
|
const rootPath = config.apiPrefix ? `${config.apiPrefix}` : '/';
|
|
@@ -44,14 +60,6 @@ const createServer = (userConfig) => {
|
|
|
44
60
|
rootRouter.get('/', async (_, res) => {
|
|
45
61
|
return res.send('It works! ツ');
|
|
46
62
|
});
|
|
47
|
-
// Route for handling not allowed methods.
|
|
48
|
-
const handleMethodNotAllowed = (_, res) => {
|
|
49
|
-
res.status(405).json({ message: 'Method Not Allowed' });
|
|
50
|
-
};
|
|
51
|
-
// Route for handling not found.
|
|
52
|
-
const handleNotFound = (_, res) => {
|
|
53
|
-
res.status(404).json({ message: 'Not Found' });
|
|
54
|
-
};
|
|
55
63
|
// All other requests to the root URL are not allowed.
|
|
56
64
|
rootRouter.all('/', handleMethodNotAllowed);
|
|
57
65
|
// In case of an API prefix, resource URLs outside of the API prefix return a 404 Not Found.
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,EAAE,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAEvC,OAAO,MAAM,MAAM,QAAQ,CAAA;AAC3B,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AAC9C,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAErC,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAA;AAC/D,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAA;AACjD,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAA;AACvD,OAAO,EAAE,UAAU,IAAI,kBAAkB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;YACV,IAAI,MAAM,CAAC,SAAS,EAAE,CAAC;gBACrB,OAAO,CAAC,GAAG,CAAC,sEAAsE,CAAC,CAAA;gBACnF,OAAM;YACR,CAAC;YAED,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE;gBAC3B,OAAO,CAAC,GAAG,CAAC,8BAA8B,MAAM,CAAC,IAAI,EAAE,CAAC,CAAA;YAC1D,CAAC,CAAC,CAAA;QACJ,CAAC;QACD,uEAAuE;QACvE,OAAO,EAAE,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,SAAS;KAC5C,CAAA;AACH,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,MAAM,GAAG,CAAC,UAAuB,EAAE,EAAE,CAAC,YAAY,CAAC,UAAU,CAAC,CAAA;AAE3E,MAAM,CAAC,MAAM,UAAU,GAAG,kBAAkB,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "temba",
|
|
3
|
-
"version": "0.30.
|
|
3
|
+
"version": "0.30.2",
|
|
4
4
|
"description": "Get a simple REST API with zero coding in less than 30 seconds (seriously).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/src/index.js",
|
|
@@ -39,6 +39,7 @@
|
|
|
39
39
|
"ajv": "^8.12.0",
|
|
40
40
|
"connect-pause": "^0.1.0",
|
|
41
41
|
"cors": "^2.8.5",
|
|
42
|
+
"dotenv": "^16.4.5",
|
|
42
43
|
"express": "^4.18.3",
|
|
43
44
|
"lowdb": "^7.0.1",
|
|
44
45
|
"morgan": "^1.10.0"
|