apeframework 0.0.0-dev.1
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/LICENSE +21 -0
- package/README.md +55 -0
- package/api/api.d.ts +7 -0
- package/api/api.js +2 -0
- package/api/api.ts +8 -0
- package/api/auth.d.ts +9 -0
- package/api/auth.js +11 -0
- package/api/auth.ts +9 -0
- package/api/config.d.ts +13 -0
- package/api/config.js +59 -0
- package/api/config.ts +69 -0
- package/api/endpoint.d.ts +20 -0
- package/api/endpoint.js +5 -0
- package/api/endpoint.ts +23 -0
- package/api/handler.d.ts +8 -0
- package/api/handler.js +13 -0
- package/api/handler.ts +23 -0
- package/api/index.d.ts +7 -0
- package/api/index.js +187 -0
- package/api/index.ts +97 -0
- package/api/route.d.ts +6 -0
- package/api/route.js +2 -0
- package/api/route.ts +4 -0
- package/api/router.d.ts +4 -0
- package/api/router.js +111 -0
- package/api/router.ts +36 -0
- package/api/schema.d.ts +29 -0
- package/api/schema.js +69 -0
- package/api/schema.ts +98 -0
- package/app/app.d.ts +6 -0
- package/app/app.js +2 -0
- package/app/app.ts +7 -0
- package/app/boot.d.ts +4 -0
- package/app/boot.js +2 -0
- package/app/boot.ts +5 -0
- package/app/bundle.d.ts +14 -0
- package/app/bundle.js +2 -0
- package/app/bundle.ts +15 -0
- package/app/config.d.ts +4 -0
- package/app/config.js +9 -0
- package/app/config.ts +9 -0
- package/app/index.d.ts +9 -0
- package/app/index.js +103 -0
- package/app/index.ts +32 -0
- package/bin/ape-cli-ts.js +4 -0
- package/bin/ape-cli.js +2 -0
- package/cli/command.d.ts +4 -0
- package/cli/command.js +2 -0
- package/cli/command.ts +7 -0
- package/cli/index.d.ts +2 -0
- package/cli/index.js +103 -0
- package/cli/index.ts +41 -0
- package/cli/internal/api.d.ts +3 -0
- package/cli/internal/api.js +110 -0
- package/cli/internal/api.ts +38 -0
- package/cli/internal/index.d.ts +2 -0
- package/cli/internal/index.js +13 -0
- package/cli/internal/index.ts +9 -0
- package/cli/internal/migration.d.ts +3 -0
- package/cli/internal/migration.js +214 -0
- package/cli/internal/migration.ts +110 -0
- package/cli/internal/queue.d.ts +3 -0
- package/cli/internal/queue.js +147 -0
- package/cli/internal/queue.ts +60 -0
- package/cli/utils.d.ts +18 -0
- package/cli/utils.js +52 -0
- package/cli/utils.ts +48 -0
- package/config/config.d.ts +5 -0
- package/config/config.js +2 -0
- package/config/config.ts +6 -0
- package/config/configuration.d.ts +63 -0
- package/config/configuration.js +2 -0
- package/config/configuration.ts +72 -0
- package/config/default.d.ts +33 -0
- package/config/default.js +34 -0
- package/config/default.ts +40 -0
- package/config/env.d.ts +66 -0
- package/config/env.js +72 -0
- package/config/env.ts +79 -0
- package/config/index.d.ts +8 -0
- package/config/index.js +91 -0
- package/config/index.ts +101 -0
- package/config/loadFile.d.ts +2 -0
- package/config/loadFile.js +13 -0
- package/config/loadFile.ts +9 -0
- package/config/node.d.ts +5 -0
- package/config/node.js +11 -0
- package/config/node.ts +7 -0
- package/config/store.d.ts +6 -0
- package/config/store.js +20 -0
- package/config/store.ts +15 -0
- package/db/config/index.d.ts +10 -0
- package/db/config/index.js +43 -0
- package/db/config/index.ts +35 -0
- package/db/config/memory.d.ts +3 -0
- package/db/config/memory.js +15 -0
- package/db/config/memory.ts +13 -0
- package/db/config/mysql.d.ts +3 -0
- package/db/config/mysql.js +56 -0
- package/db/config/mysql.ts +58 -0
- package/db/config/postgres.d.ts +3 -0
- package/db/config/postgres.js +59 -0
- package/db/config/postgres.ts +61 -0
- package/db/config/sqlite.d.ts +3 -0
- package/db/config/sqlite.js +21 -0
- package/db/config/sqlite.ts +20 -0
- package/db/database.d.ts +3 -0
- package/db/database.js +3 -0
- package/db/database.ts +5 -0
- package/db/index.d.ts +10 -0
- package/db/index.js +32 -0
- package/db/index.ts +26 -0
- package/db/postProcess.d.ts +3 -0
- package/db/postProcess.js +25 -0
- package/db/postProcess.ts +27 -0
- package/db/schema/builder/_.d.ts +30 -0
- package/db/schema/builder/_.js +87 -0
- package/db/schema/builder/_.ts +101 -0
- package/db/schema/builder/columnBuilder.d.ts +7 -0
- package/db/schema/builder/columnBuilder.js +14 -0
- package/db/schema/builder/columnBuilder.ts +15 -0
- package/db/schema/builder/dataType.d.ts +28 -0
- package/db/schema/builder/dataType.js +170 -0
- package/db/schema/builder/dataType.ts +184 -0
- package/db/schema/builder/index.d.ts +15 -0
- package/db/schema/builder/index.js +42 -0
- package/db/schema/builder/index.ts +50 -0
- package/db/schema/builder/tableBuilder.d.ts +45 -0
- package/db/schema/builder/tableBuilder.js +307 -0
- package/db/schema/builder/tableBuilder.ts +409 -0
- package/db/schema/config.d.ts +4 -0
- package/db/schema/config.js +5 -0
- package/db/schema/config.ts +3 -0
- package/db/schema/index.d.ts +7 -0
- package/db/schema/index.js +252 -0
- package/db/schema/index.ts +88 -0
- package/db/schema/migration.d.ts +6 -0
- package/db/schema/migration.js +2 -0
- package/db/schema/migration.ts +7 -0
- package/db/schema/migrationList.d.ts +5 -0
- package/db/schema/migrationList.js +2 -0
- package/db/schema/migrationList.ts +5 -0
- package/db/schema/migrationSource.d.ts +10 -0
- package/db/schema/migrationSource.js +60 -0
- package/db/schema/migrationSource.ts +24 -0
- package/db/schema/schema.d.ts +6 -0
- package/db/schema/schema.js +2 -0
- package/db/schema/schema.ts +7 -0
- package/db/utils/index.d.ts +7 -0
- package/db/utils/index.js +10 -0
- package/db/utils/index.ts +8 -0
- package/db/utils/insertGetKey.d.ts +8 -0
- package/db/utils/insertGetKey.js +109 -0
- package/db/utils/insertGetKey.ts +80 -0
- package/i18n/config.d.ts +4 -0
- package/i18n/config.js +9 -0
- package/i18n/config.ts +9 -0
- package/i18n/i18n.d.ts +5 -0
- package/i18n/i18n.js +2 -0
- package/i18n/i18n.ts +6 -0
- package/i18n/index.d.ts +8 -0
- package/i18n/index.js +155 -0
- package/i18n/index.ts +42 -0
- package/i18n/internationalization.d.ts +3 -0
- package/i18n/internationalization.js +2 -0
- package/i18n/internationalization.ts +5 -0
- package/i18n/translation.d.ts +4 -0
- package/i18n/translation.js +2 -0
- package/i18n/translation.ts +4 -0
- package/jwt/config.d.ts +5 -0
- package/jwt/config.js +12 -0
- package/jwt/config.ts +11 -0
- package/jwt/index.d.ts +8 -0
- package/jwt/index.js +90 -0
- package/jwt/index.ts +60 -0
- package/jwt/jwt.d.ts +6 -0
- package/jwt/jwt.js +2 -0
- package/jwt/jwt.ts +19 -0
- package/jwt/user.d.ts +4 -0
- package/jwt/user.js +2 -0
- package/jwt/user.ts +4 -0
- package/log/config.d.ts +23 -0
- package/log/config.js +47 -0
- package/log/config.ts +43 -0
- package/log/index.d.ts +4 -0
- package/log/index.js +9 -0
- package/log/index.ts +9 -0
- package/log/logger.d.ts +3 -0
- package/log/logger.js +2 -0
- package/log/logger.ts +5 -0
- package/mail/config.d.ts +4 -0
- package/mail/config.js +5 -0
- package/mail/config.ts +3 -0
- package/mail/email.d.ts +20 -0
- package/mail/email.js +2 -0
- package/mail/email.ts +21 -0
- package/mail/index.d.ts +2 -0
- package/mail/index.js +4 -0
- package/mail/index.ts +3 -0
- package/mail/mail.d.ts +12 -0
- package/mail/mail.js +2 -0
- package/mail/mail.ts +13 -0
- package/mail/module/bypass/index.d.ts +6 -0
- package/mail/module/bypass/index.js +74 -0
- package/mail/module/bypass/index.ts +9 -0
- package/mail/module/config.d.ts +11 -0
- package/mail/module/config.js +24 -0
- package/mail/module/config.ts +25 -0
- package/mail/module/index.d.ts +8 -0
- package/mail/module/index.js +107 -0
- package/mail/module/index.ts +61 -0
- package/mail/module/smtp/config.d.ts +6 -0
- package/mail/module/smtp/config.js +25 -0
- package/mail/module/smtp/config.ts +25 -0
- package/mail/module/smtp/index.d.ts +9 -0
- package/mail/module/smtp/index.js +104 -0
- package/mail/module/smtp/index.ts +36 -0
- package/mq/index.d.ts +3 -0
- package/mq/index.js +27 -0
- package/mq/index.ts +4 -0
- package/mq/module/bypass/index.d.ts +13 -0
- package/mq/module/bypass/index.js +86 -0
- package/mq/module/bypass/index.ts +20 -0
- package/mq/module/config.d.ts +6 -0
- package/mq/module/config.js +15 -0
- package/mq/module/config.ts +14 -0
- package/mq/module/index.d.ts +9 -0
- package/mq/module/index.js +93 -0
- package/mq/module/index.ts +24 -0
- package/mq/module/redis/config.d.ts +27 -0
- package/mq/module/redis/config.js +37 -0
- package/mq/module/redis/config.ts +34 -0
- package/mq/module/redis/index.d.ts +15 -0
- package/mq/module/redis/index.js +132 -0
- package/mq/module/redis/index.ts +60 -0
- package/mq/mq.d.ts +15 -0
- package/mq/mq.js +2 -0
- package/mq/mq.ts +18 -0
- package/mq/queue.d.ts +9 -0
- package/mq/queue.js +2 -0
- package/mq/queue.ts +11 -0
- package/package.json +53 -0
- package/pwd/config.d.ts +4 -0
- package/pwd/config.js +9 -0
- package/pwd/config.ts +9 -0
- package/pwd/index.d.ts +6 -0
- package/pwd/index.js +21 -0
- package/pwd/index.ts +20 -0
- package/pwd/pwd.d.ts +4 -0
- package/pwd/pwd.js +2 -0
- package/pwd/pwd.ts +4 -0
- package/utils/index.d.ts +25 -0
- package/utils/index.js +72 -0
- package/utils/index.ts +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Matthieu Symoens
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# Ape Framework
|
|
2
|
+
|
|
3
|
+
[Node.js](https://nodejs.org) API framework.
|
|
4
|
+
|
|
5
|
+
NPM package: [apeframework](https://www.npmjs.com/package/apeframework)
|
|
6
|
+
|
|
7
|
+
GitHub repository: [ApeCommerce/ape-framework](https://github.com/ApeCommerce/ape-framework)
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
npm install apeframework
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Hello Ape!
|
|
16
|
+
|
|
17
|
+
Let's make an API serving a `/hello` endpoint, in TypeScript.
|
|
18
|
+
|
|
19
|
+
Create a `boot.ts` file at the root of the project:
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import type { Boot, Bundle } from 'apeframework/app';
|
|
23
|
+
|
|
24
|
+
const welcome: Bundle = {
|
|
25
|
+
bundleId: 'welcome',
|
|
26
|
+
name: 'Welcome',
|
|
27
|
+
routes: async () => [
|
|
28
|
+
{
|
|
29
|
+
endpoint: {
|
|
30
|
+
method: 'GET',
|
|
31
|
+
path: '/hello',
|
|
32
|
+
},
|
|
33
|
+
handler: async (request, reply) => reply.send('Hello Ape!'),
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const boot: Boot = {
|
|
39
|
+
bundles: async () => [welcome],
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export default boot;
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Start the API using Ape Framework's CLI:
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
npx ape-cli-ts api start
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Request the API:
|
|
52
|
+
|
|
53
|
+
```
|
|
54
|
+
curl http://localhost:3000/hello
|
|
55
|
+
```
|
package/api/api.d.ts
ADDED
package/api/api.js
ADDED
package/api/api.ts
ADDED
package/api/auth.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare const tokenType = "authorization";
|
|
2
|
+
export declare const headerRegex: RegExp;
|
|
3
|
+
export declare const bearerPrefix = "Bearer ";
|
|
4
|
+
declare const _default: {
|
|
5
|
+
bearerPrefix: string;
|
|
6
|
+
headerRegex: RegExp;
|
|
7
|
+
tokenType: string;
|
|
8
|
+
};
|
|
9
|
+
export default _default;
|
package/api/auth.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.bearerPrefix = exports.headerRegex = exports.tokenType = void 0;
|
|
4
|
+
exports.tokenType = 'authorization';
|
|
5
|
+
exports.headerRegex = /^Bearer [0-9A-Za-z]+\.[0-9A-Za-z]+\.[0-9A-Za-z-_]+$/;
|
|
6
|
+
exports.bearerPrefix = 'Bearer ';
|
|
7
|
+
exports.default = {
|
|
8
|
+
bearerPrefix: exports.bearerPrefix,
|
|
9
|
+
headerRegex: exports.headerRegex,
|
|
10
|
+
tokenType: exports.tokenType,
|
|
11
|
+
};
|
package/api/auth.ts
ADDED
package/api/config.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { FastifyCorsOptions as CorsOptions } from '@fastify/cors';
|
|
2
|
+
import type { FastifyListenOptions as ListenOptions, FastifyServerOptions as ServerOptions } from 'fastify';
|
|
3
|
+
import type { Options as ResponseValidationOptions } from '@fastify/response-validation';
|
|
4
|
+
declare const _default: {
|
|
5
|
+
name: string;
|
|
6
|
+
version: string;
|
|
7
|
+
listenOptions: ListenOptions;
|
|
8
|
+
serverOptions: ServerOptions;
|
|
9
|
+
corsOptions: CorsOptions;
|
|
10
|
+
swaggerOptions: import("@fastify/swagger").FastifyDynamicSwaggerOptions;
|
|
11
|
+
responseValidationOptions: ResponseValidationOptions;
|
|
12
|
+
};
|
|
13
|
+
export default _default;
|
package/api/config.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var config_1 = require("../config");
|
|
4
|
+
var config = (0, config_1.getConfig)();
|
|
5
|
+
if (!config.apiName) {
|
|
6
|
+
throw new Error('api: name not provided');
|
|
7
|
+
}
|
|
8
|
+
if (!config.apiVersion) {
|
|
9
|
+
throw new Error('api: version not provided');
|
|
10
|
+
}
|
|
11
|
+
if (!config.apiHost) {
|
|
12
|
+
throw new Error('api: host not provided');
|
|
13
|
+
}
|
|
14
|
+
if (!config.apiPort) {
|
|
15
|
+
throw new Error('api: port not provided');
|
|
16
|
+
}
|
|
17
|
+
if (!config.apiConnectionTimeout) {
|
|
18
|
+
throw new Error('api: connection timeout not provided');
|
|
19
|
+
}
|
|
20
|
+
if (!config.apiRequestTimeout) {
|
|
21
|
+
throw new Error('api: request timeout not provided');
|
|
22
|
+
}
|
|
23
|
+
if (!config.apiKeepAliveTimeout) {
|
|
24
|
+
throw new Error('api: keep alive timeout not provided');
|
|
25
|
+
}
|
|
26
|
+
if (!config.apiBodyLimit) {
|
|
27
|
+
throw new Error('api: body limit not provided');
|
|
28
|
+
}
|
|
29
|
+
var listenOptions = {
|
|
30
|
+
host: config.apiHost,
|
|
31
|
+
port: config.apiRandomPort ? undefined : config.apiPort,
|
|
32
|
+
};
|
|
33
|
+
var serverOptions = {
|
|
34
|
+
connectionTimeout: config.apiConnectionTimeout,
|
|
35
|
+
requestTimeout: config.apiRequestTimeout,
|
|
36
|
+
keepAliveTimeout: config.apiKeepAliveTimeout,
|
|
37
|
+
bodyLimit: config.apiBodyLimit,
|
|
38
|
+
};
|
|
39
|
+
var corsOptions = {};
|
|
40
|
+
var swaggerOptions = {
|
|
41
|
+
openapi: {
|
|
42
|
+
info: {
|
|
43
|
+
title: "".concat(config.apiName, " API Documentation"),
|
|
44
|
+
version: config.apiVersion,
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
var responseValidationOptions = {
|
|
49
|
+
responseValidation: config.apiResponseValidation,
|
|
50
|
+
};
|
|
51
|
+
exports.default = {
|
|
52
|
+
name: config.apiName,
|
|
53
|
+
version: config.apiVersion,
|
|
54
|
+
listenOptions: listenOptions,
|
|
55
|
+
serverOptions: serverOptions,
|
|
56
|
+
corsOptions: corsOptions,
|
|
57
|
+
swaggerOptions: swaggerOptions,
|
|
58
|
+
responseValidationOptions: responseValidationOptions,
|
|
59
|
+
};
|
package/api/config.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { FastifyCorsOptions as CorsOptions } from '@fastify/cors'
|
|
2
|
+
import type { FastifyListenOptions as ListenOptions, FastifyServerOptions as ServerOptions } from 'fastify'
|
|
3
|
+
import type { Options as ResponseValidationOptions } from '@fastify/response-validation'
|
|
4
|
+
import type { SwaggerOptions } from '@fastify/swagger'
|
|
5
|
+
import { getConfig } from '../config'
|
|
6
|
+
|
|
7
|
+
const config = getConfig()
|
|
8
|
+
|
|
9
|
+
if (!config.apiName) {
|
|
10
|
+
throw new Error('api: name not provided')
|
|
11
|
+
}
|
|
12
|
+
if (!config.apiVersion) {
|
|
13
|
+
throw new Error('api: version not provided')
|
|
14
|
+
}
|
|
15
|
+
if (!config.apiHost) {
|
|
16
|
+
throw new Error('api: host not provided')
|
|
17
|
+
}
|
|
18
|
+
if (!config.apiPort) {
|
|
19
|
+
throw new Error('api: port not provided')
|
|
20
|
+
}
|
|
21
|
+
if (!config.apiConnectionTimeout) {
|
|
22
|
+
throw new Error('api: connection timeout not provided')
|
|
23
|
+
}
|
|
24
|
+
if (!config.apiRequestTimeout) {
|
|
25
|
+
throw new Error('api: request timeout not provided')
|
|
26
|
+
}
|
|
27
|
+
if (!config.apiKeepAliveTimeout) {
|
|
28
|
+
throw new Error('api: keep alive timeout not provided')
|
|
29
|
+
}
|
|
30
|
+
if (!config.apiBodyLimit) {
|
|
31
|
+
throw new Error('api: body limit not provided')
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const listenOptions: ListenOptions = {
|
|
35
|
+
host: config.apiHost,
|
|
36
|
+
port: config.apiRandomPort ? undefined : config.apiPort,
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const serverOptions: ServerOptions = {
|
|
40
|
+
connectionTimeout: config.apiConnectionTimeout,
|
|
41
|
+
requestTimeout: config.apiRequestTimeout,
|
|
42
|
+
keepAliveTimeout: config.apiKeepAliveTimeout,
|
|
43
|
+
bodyLimit: config.apiBodyLimit,
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const corsOptions: CorsOptions = {}
|
|
47
|
+
|
|
48
|
+
const swaggerOptions: SwaggerOptions = {
|
|
49
|
+
openapi: {
|
|
50
|
+
info: {
|
|
51
|
+
title: `${config.apiName} API Documentation`,
|
|
52
|
+
version: config.apiVersion,
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const responseValidationOptions: ResponseValidationOptions = {
|
|
58
|
+
responseValidation: config.apiResponseValidation,
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export default {
|
|
62
|
+
name: config.apiName,
|
|
63
|
+
version: config.apiVersion,
|
|
64
|
+
listenOptions,
|
|
65
|
+
serverOptions,
|
|
66
|
+
corsOptions,
|
|
67
|
+
swaggerOptions,
|
|
68
|
+
responseValidationOptions,
|
|
69
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface Endpoint {
|
|
2
|
+
path: string;
|
|
3
|
+
method: Method;
|
|
4
|
+
requiredRoles?: string[];
|
|
5
|
+
forbidden?: boolean;
|
|
6
|
+
requestSchema?: Schema;
|
|
7
|
+
replySchema?: Schema;
|
|
8
|
+
description?: string;
|
|
9
|
+
requestExample?: any;
|
|
10
|
+
replyExample?: any;
|
|
11
|
+
}
|
|
12
|
+
export interface Schema {
|
|
13
|
+
type: 'object';
|
|
14
|
+
additionalProperties: false;
|
|
15
|
+
required: string[];
|
|
16
|
+
properties: any;
|
|
17
|
+
}
|
|
18
|
+
export type Method = 'DELETE' | 'GET' | 'PATCH' | 'POST' | 'PUT';
|
|
19
|
+
export declare const querystringMethods: Method[];
|
|
20
|
+
export declare const bodyMethods: Method[];
|
package/api/endpoint.js
ADDED
package/api/endpoint.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export interface Endpoint {
|
|
2
|
+
path: string,
|
|
3
|
+
method: Method,
|
|
4
|
+
requiredRoles?: string[],
|
|
5
|
+
forbidden?: boolean,
|
|
6
|
+
requestSchema?: Schema,
|
|
7
|
+
replySchema?: Schema,
|
|
8
|
+
description?: string,
|
|
9
|
+
requestExample?: any,
|
|
10
|
+
replyExample?: any,
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface Schema {
|
|
14
|
+
type: 'object',
|
|
15
|
+
additionalProperties: false,
|
|
16
|
+
required: string[],
|
|
17
|
+
properties: any,
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type Method = 'DELETE' | 'GET' | 'PATCH' | 'POST' | 'PUT'
|
|
21
|
+
|
|
22
|
+
export const querystringMethods: Method[] = ['DELETE', 'GET']
|
|
23
|
+
export const bodyMethods: Method[] = ['PATCH', 'POST', 'PUT']
|
package/api/handler.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { FastifyError as Error, FastifyReply as Reply, FastifyRequest } from 'fastify';
|
|
2
|
+
import type { User } from '../jwt/user';
|
|
3
|
+
export type Handler = (request: Request, reply: Reply) => Promise<void>;
|
|
4
|
+
export type ErrorHandler = (error: Error, request: Request, reply: Reply) => Promise<void>;
|
|
5
|
+
export interface Request extends FastifyRequest {
|
|
6
|
+
user?: User;
|
|
7
|
+
}
|
|
8
|
+
export declare const send: (reply: Reply, value?: unknown) => void;
|
package/api/handler.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.send = void 0;
|
|
4
|
+
var send = function (reply, value) {
|
|
5
|
+
if (value === void 0) { value = true; }
|
|
6
|
+
if (value === true)
|
|
7
|
+
reply.send();
|
|
8
|
+
else if (value === false)
|
|
9
|
+
reply.status(403).send();
|
|
10
|
+
else
|
|
11
|
+
reply.send(value);
|
|
12
|
+
};
|
|
13
|
+
exports.send = send;
|
package/api/handler.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { FastifyError as Error, FastifyReply as Reply, FastifyRequest } from 'fastify'
|
|
2
|
+
import type { User } from '../jwt/user'
|
|
3
|
+
|
|
4
|
+
export type Handler = (
|
|
5
|
+
request: Request,
|
|
6
|
+
reply: Reply
|
|
7
|
+
) => Promise<void>
|
|
8
|
+
|
|
9
|
+
export type ErrorHandler = (
|
|
10
|
+
error: Error,
|
|
11
|
+
request: Request,
|
|
12
|
+
reply: Reply
|
|
13
|
+
) => Promise<void>
|
|
14
|
+
|
|
15
|
+
export interface Request extends FastifyRequest {
|
|
16
|
+
user?: User,
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const send = (reply: Reply, value: unknown = true) => {
|
|
20
|
+
if (value === true) reply.send()
|
|
21
|
+
else if (value === false) reply.status(403).send()
|
|
22
|
+
else reply.send(value)
|
|
23
|
+
}
|
package/api/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Api } from './api';
|
|
2
|
+
export declare const start: () => Promise<void>;
|
|
3
|
+
export declare const close: () => Promise<void>;
|
|
4
|
+
export declare const getUrl: () => string;
|
|
5
|
+
export declare const doc: () => import("openapi-types").OpenAPI.Document<{}>;
|
|
6
|
+
declare const api: Api;
|
|
7
|
+
export default api;
|
package/api/index.js
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
35
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
36
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
37
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
38
|
+
function step(op) {
|
|
39
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
40
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
41
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
42
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
43
|
+
switch (op[0]) {
|
|
44
|
+
case 0: case 1: t = op; break;
|
|
45
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
46
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
47
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
48
|
+
default:
|
|
49
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
50
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
51
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
52
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
53
|
+
if (t[2]) _.ops.pop();
|
|
54
|
+
_.trys.pop(); continue;
|
|
55
|
+
}
|
|
56
|
+
op = body.call(thisArg, _);
|
|
57
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
58
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
62
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
63
|
+
};
|
|
64
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
65
|
+
exports.doc = exports.getUrl = exports.close = exports.start = void 0;
|
|
66
|
+
var cors_1 = __importDefault(require("@fastify/cors"));
|
|
67
|
+
var fastify_1 = __importDefault(require("fastify"));
|
|
68
|
+
var response_validation_1 = __importDefault(require("@fastify/response-validation"));
|
|
69
|
+
var swagger_1 = __importDefault(require("@fastify/swagger"));
|
|
70
|
+
var utils_1 = require("../utils");
|
|
71
|
+
var auth_1 = require("./auth");
|
|
72
|
+
var jwt_1 = require("../jwt");
|
|
73
|
+
var config_1 = __importDefault(require("./config"));
|
|
74
|
+
var log_1 = __importDefault(require("../log"));
|
|
75
|
+
var router_1 = __importStar(require("./router"));
|
|
76
|
+
var onRequest = function (request, reply) { return __awaiter(void 0, void 0, void 0, function () {
|
|
77
|
+
var roles, authorized, user;
|
|
78
|
+
var _a;
|
|
79
|
+
return __generator(this, function (_b) {
|
|
80
|
+
switch (_b.label) {
|
|
81
|
+
case 0:
|
|
82
|
+
roles = (0, router_1.getRequiredRoles)((0, utils_1.basePath)(request.routerPath));
|
|
83
|
+
if (!roles) return [3 /*break*/, 3];
|
|
84
|
+
authorized = false;
|
|
85
|
+
if (!((_a = request.headers.authorization) === null || _a === void 0 ? void 0 : _a.match(auth_1.headerRegex))) return [3 /*break*/, 2];
|
|
86
|
+
return [4 /*yield*/, (0, jwt_1.verifyToken)(request.headers.authorization.substring(auth_1.bearerPrefix.length), auth_1.tokenType, (0, utils_1.timestamp)())];
|
|
87
|
+
case 1:
|
|
88
|
+
user = _b.sent();
|
|
89
|
+
if (user && (0, jwt_1.hasRoles)(user, roles)) {
|
|
90
|
+
authorized = true;
|
|
91
|
+
request.user = user;
|
|
92
|
+
}
|
|
93
|
+
_b.label = 2;
|
|
94
|
+
case 2:
|
|
95
|
+
if (!authorized) {
|
|
96
|
+
reply.status(401).send();
|
|
97
|
+
}
|
|
98
|
+
_b.label = 3;
|
|
99
|
+
case 3: return [2 /*return*/];
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}); };
|
|
103
|
+
var onResponse = function (request, reply) { return __awaiter(void 0, void 0, void 0, function () {
|
|
104
|
+
return __generator(this, function (_a) {
|
|
105
|
+
log_1.default.debug([
|
|
106
|
+
"api: ".concat(request.method, " ").concat((0, utils_1.basePath)(request.url, '?')),
|
|
107
|
+
"".concat(reply.statusCode, " ").concat(reply.getResponseTime().toFixed(0), "ms"),
|
|
108
|
+
].join(' '));
|
|
109
|
+
return [2 /*return*/];
|
|
110
|
+
});
|
|
111
|
+
}); };
|
|
112
|
+
var onTimeout = function (request) { return __awaiter(void 0, void 0, void 0, function () {
|
|
113
|
+
return __generator(this, function (_a) {
|
|
114
|
+
log_1.default.error([
|
|
115
|
+
"api: ".concat(request.method, " ").concat(request.routerPath),
|
|
116
|
+
"timeout ".concat(config_1.default.serverOptions.connectionTimeout, "ms"),
|
|
117
|
+
].join(' '));
|
|
118
|
+
return [2 /*return*/];
|
|
119
|
+
});
|
|
120
|
+
}); };
|
|
121
|
+
var notFoundHandler = function (request, reply) { return __awaiter(void 0, void 0, void 0, function () {
|
|
122
|
+
return __generator(this, function (_a) {
|
|
123
|
+
reply.status(404).send();
|
|
124
|
+
return [2 /*return*/];
|
|
125
|
+
});
|
|
126
|
+
}); };
|
|
127
|
+
var errorHandler = function (error, request, reply) { return __awaiter(void 0, void 0, void 0, function () {
|
|
128
|
+
var status;
|
|
129
|
+
var _a;
|
|
130
|
+
return __generator(this, function (_b) {
|
|
131
|
+
status = (_a = error.statusCode) !== null && _a !== void 0 ? _a : 500;
|
|
132
|
+
reply.status(status).send();
|
|
133
|
+
if (status === 400) {
|
|
134
|
+
log_1.default.debug(error);
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
log_1.default.error(error);
|
|
138
|
+
}
|
|
139
|
+
return [2 /*return*/];
|
|
140
|
+
});
|
|
141
|
+
}); };
|
|
142
|
+
var server = (0, fastify_1.default)(config_1.default.serverOptions)
|
|
143
|
+
.addHook('onRequest', onRequest)
|
|
144
|
+
.addHook('onResponse', onResponse)
|
|
145
|
+
.addHook('onTimeout', onTimeout)
|
|
146
|
+
.setNotFoundHandler(notFoundHandler)
|
|
147
|
+
.setErrorHandler(errorHandler)
|
|
148
|
+
.register(cors_1.default, config_1.default.corsOptions)
|
|
149
|
+
.register(swagger_1.default, config_1.default.swaggerOptions)
|
|
150
|
+
.register(response_validation_1.default, config_1.default.responseValidationOptions)
|
|
151
|
+
.register(router_1.default);
|
|
152
|
+
var url;
|
|
153
|
+
var start = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
154
|
+
return __generator(this, function (_a) {
|
|
155
|
+
switch (_a.label) {
|
|
156
|
+
case 0: return [4 /*yield*/, server.listen(config_1.default.listenOptions)];
|
|
157
|
+
case 1:
|
|
158
|
+
url = _a.sent();
|
|
159
|
+
log_1.default.info("api: started v".concat(config_1.default.version, " @").concat(url));
|
|
160
|
+
return [2 /*return*/];
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
}); };
|
|
164
|
+
exports.start = start;
|
|
165
|
+
var close = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
166
|
+
return __generator(this, function (_a) {
|
|
167
|
+
switch (_a.label) {
|
|
168
|
+
case 0: return [4 /*yield*/, server.close()];
|
|
169
|
+
case 1:
|
|
170
|
+
_a.sent();
|
|
171
|
+
log_1.default.info('api: closed');
|
|
172
|
+
return [2 /*return*/];
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}); };
|
|
176
|
+
exports.close = close;
|
|
177
|
+
var getUrl = function () { return url; };
|
|
178
|
+
exports.getUrl = getUrl;
|
|
179
|
+
var doc = function () { return server.swagger(); };
|
|
180
|
+
exports.doc = doc;
|
|
181
|
+
var api = {
|
|
182
|
+
start: exports.start,
|
|
183
|
+
close: exports.close,
|
|
184
|
+
getUrl: exports.getUrl,
|
|
185
|
+
doc: exports.doc,
|
|
186
|
+
};
|
|
187
|
+
exports.default = api;
|
package/api/index.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import cors from '@fastify/cors'
|
|
2
|
+
import fastify from 'fastify'
|
|
3
|
+
import responseValidation from '@fastify/response-validation'
|
|
4
|
+
import swagger from '@fastify/swagger'
|
|
5
|
+
import { basePath, timestamp } from '../utils'
|
|
6
|
+
import { bearerPrefix, headerRegex, tokenType } from './auth'
|
|
7
|
+
import { hasRoles, verifyToken } from '../jwt'
|
|
8
|
+
import config from './config'
|
|
9
|
+
import log from '../log'
|
|
10
|
+
import router, { getRequiredRoles } from './router'
|
|
11
|
+
import type { Api } from './api'
|
|
12
|
+
import type { ErrorHandler, Handler } from './handler'
|
|
13
|
+
|
|
14
|
+
const onRequest: Handler = async (request, reply) => {
|
|
15
|
+
const roles = getRequiredRoles(basePath(request.routerPath))
|
|
16
|
+
if (roles) {
|
|
17
|
+
let authorized = false
|
|
18
|
+
if (request.headers.authorization?.match(headerRegex)) {
|
|
19
|
+
const user = await verifyToken(
|
|
20
|
+
request.headers.authorization.substring(bearerPrefix.length),
|
|
21
|
+
tokenType,
|
|
22
|
+
timestamp(),
|
|
23
|
+
)
|
|
24
|
+
if (user && hasRoles(user, roles)) {
|
|
25
|
+
authorized = true
|
|
26
|
+
request.user = user
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (!authorized) {
|
|
30
|
+
reply.status(401).send()
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const onResponse: Handler = async (request, reply) => {
|
|
36
|
+
log.debug([
|
|
37
|
+
`api: ${request.method} ${basePath(request.url, '?')}`,
|
|
38
|
+
`${reply.statusCode} ${reply.getResponseTime().toFixed(0)}ms`,
|
|
39
|
+
].join(' '))
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const onTimeout: Handler = async (request) => {
|
|
43
|
+
log.error([
|
|
44
|
+
`api: ${request.method} ${request.routerPath}`,
|
|
45
|
+
`timeout ${config.serverOptions.connectionTimeout}ms`,
|
|
46
|
+
].join(' '))
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const notFoundHandler: Handler = async (request, reply) => {
|
|
50
|
+
reply.status(404).send()
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const errorHandler: ErrorHandler = async (error, request, reply) => {
|
|
54
|
+
const status = error.statusCode ?? 500
|
|
55
|
+
reply.status(status).send()
|
|
56
|
+
if (status === 400) {
|
|
57
|
+
log.debug(error)
|
|
58
|
+
} else {
|
|
59
|
+
log.error(error)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const server = fastify(config.serverOptions)
|
|
64
|
+
.addHook('onRequest', onRequest)
|
|
65
|
+
.addHook('onResponse', onResponse)
|
|
66
|
+
.addHook('onTimeout', onTimeout)
|
|
67
|
+
.setNotFoundHandler(notFoundHandler)
|
|
68
|
+
.setErrorHandler(errorHandler)
|
|
69
|
+
.register(cors, config.corsOptions)
|
|
70
|
+
.register(swagger, config.swaggerOptions)
|
|
71
|
+
.register(responseValidation, config.responseValidationOptions)
|
|
72
|
+
.register(router)
|
|
73
|
+
|
|
74
|
+
let url: string
|
|
75
|
+
|
|
76
|
+
export const start = async () => {
|
|
77
|
+
url = await server.listen(config.listenOptions)
|
|
78
|
+
log.info(`api: started v${config.version} @${url}`)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export const close = async () => {
|
|
82
|
+
await server.close()
|
|
83
|
+
log.info('api: closed')
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export const getUrl = () => url
|
|
87
|
+
|
|
88
|
+
export const doc = () => server.swagger()
|
|
89
|
+
|
|
90
|
+
const api: Api = {
|
|
91
|
+
start,
|
|
92
|
+
close,
|
|
93
|
+
getUrl,
|
|
94
|
+
doc,
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export default api
|