@qelos/api-kit 3.3.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.
package/.nvmrc ADDED
@@ -0,0 +1 @@
1
+ 16
@@ -0,0 +1,7 @@
1
+ # Contribute to Greenpress api-kit
2
+
3
+ ## Greenpress [Contribute Guide](https://docs.greenpress.info/guide/contribute/getting-started.html)
4
+
5
+ ## Greenpress [Installation Guide](https://docs.greenpress.info/guide/getting-started.html)
6
+
7
+ ## [Open Issues](https://github.com/greenpress/api-kit/issues)
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Qelos
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,13 @@
1
+ # Qelos API Kit
2
+
3
+ This code brings together all Qelos services
4
+
5
+ ## Main Features
6
+
7
+ ### Express
8
+
9
+ `api-kit` creates Express apps to make the development easier.
10
+
11
+ ### Auth Middlewares
12
+
13
+ `api-kit` provides simple authentication middlewares from the authentication service, without having to worry about fetching.
package/dist/app.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import type { ApiConfig } from './types';
2
+ export declare const config: (updatedConfig?: any) => ApiConfig;
3
+ export declare const app: () => any;
4
+ export declare const start: typeof startApp;
5
+ export declare function getBodyParser(): any;
6
+ declare function startApp(serviceName?: string, port?: string | number, ip?: string): Promise<void>;
7
+ export {};
package/dist/app.js ADDED
@@ -0,0 +1,62 @@
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.getBodyParser = exports.start = exports.app = exports.config = void 0;
7
+ const express_1 = __importDefault(require("express"));
8
+ const shutdown_1 = __importDefault(require("./shutdown"));
9
+ const config = (updatedConfig = _config) => {
10
+ _config = { ..._config, ...updatedConfig };
11
+ return _config;
12
+ };
13
+ exports.config = config;
14
+ const app = () => _app || createApp();
15
+ exports.app = app;
16
+ exports.start = startApp;
17
+ let _app;
18
+ let _config = {
19
+ cors: !!process.env.API_CORS || false,
20
+ bodyParser: process.env.API_BODY_PARSER || 'json',
21
+ showLogs: !!process.env.SHOW_LOGS,
22
+ port: process.env?.PORT || '3000',
23
+ ip: process.env.IP || '127.0.0.1'
24
+ };
25
+ function createApp() {
26
+ _app = (0, express_1.default)();
27
+ configureApp(_app);
28
+ return _app;
29
+ }
30
+ function configureApp(app) {
31
+ if (process.env.NODE_ENV !== 'production') {
32
+ app.use(require('morgan')('combined'));
33
+ app.get('/api/shutdown', () => {
34
+ (0, shutdown_1.default)();
35
+ });
36
+ }
37
+ else if (_config.showLogs) {
38
+ app.use(require('morgan')('combined'));
39
+ }
40
+ if (_config.cors) {
41
+ app.use(require('cors')());
42
+ }
43
+ if (_config.bodyParser) {
44
+ app.use(express_1.default[_config.bodyParser]());
45
+ }
46
+ }
47
+ function getBodyParser() {
48
+ return express_1.default.json();
49
+ }
50
+ exports.getBodyParser = getBodyParser;
51
+ function startApp(serviceName = 'APP', port = _config.port, ip = _config.ip) {
52
+ return new Promise((resolve) => {
53
+ _app.listen(parseInt(port), ip, () => {
54
+ console.log(`${serviceName} is running on port ${port}`);
55
+ if (_config.showLogs) {
56
+ console.log('logs are available');
57
+ }
58
+ resolve();
59
+ });
60
+ });
61
+ }
62
+ //# sourceMappingURL=app.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"app.js","sourceRoot":"","sources":["../src/app.ts"],"names":[],"mappings":";;;;;;AAAA,sDAAyC;AAGzC,0DAAkC;AAG3B,MAAM,MAAM,GAAG,CAAC,gBAAqB,OAAO,EAAa,EAAE;IAChE,OAAO,GAAG,EAAC,GAAG,OAAO,EAAE,GAAG,aAAa,EAAC,CAAC;IACzC,OAAO,OAAO,CAAC;AACjB,CAAC,CAAA;AAHY,QAAA,MAAM,UAGlB;AACM,MAAM,GAAG,GAAG,GAAG,EAAE,CAAC,IAAI,IAAI,SAAS,EAAE,CAAC;AAAhC,QAAA,GAAG,OAA6B;AAChC,QAAA,KAAK,GAAG,QAAQ,CAAC;AAE9B,IAAI,IAAa,CAAC;AAClB,IAAI,OAAO,GAAc;IACvB,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,IAAI,KAAK;IACrC,UAAU,EAAG,OAAO,CAAC,GAAG,CAAC,eAAkC,IAAI,MAAM;IACrE,QAAQ,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,SAAS;IACjC,IAAI,EAAE,OAAO,CAAC,GAAG,EAAE,IAAI,IAAI,MAAM;IACjC,EAAE,EAAE,OAAO,CAAC,GAAG,CAAC,EAAE,IAAI,WAAW;CAClC,CAAC;AAEF,SAAS,SAAS;IAChB,IAAI,GAAG,IAAA,iBAAO,GAAE,CAAA;IAEhB,YAAY,CAAC,IAAI,CAAC,CAAC;IAEnB,OAAO,IAAI,CAAC;AACd,CAAC;AAED,SAAS,YAAY,CAAC,GAAY;IAChC,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,EAAE;QACzC,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,UAAU,CAAC,CAAC,CAAA;QACtC,GAAG,CAAC,GAAG,CAAC,eAAe,EAAE,GAAG,EAAE;YAC5B,IAAA,kBAAQ,GAAE,CAAA;QACZ,CAAC,CAAC,CAAA;KACH;SAAM,IAAI,OAAO,CAAC,QAAQ,EAAE;QAC3B,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC;KACxC;IACD,IAAI,OAAO,CAAC,IAAI,EAAE;QAChB,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;KAC3B;IACD,IAAI,OAAO,CAAC,UAAU,EAAE;QACtB,GAAG,CAAC,GAAG,CAAC,iBAAO,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,CAAC,CAAA;KACvC;AACH,CAAC;AAED,SAAgB,aAAa;IAC3B,OAAO,iBAAO,CAAC,IAAI,EAAE,CAAC;AACxB,CAAC;AAFD,sCAEC;AAED,SAAS,QAAQ,CAAC,WAAW,GAAG,KAAK,EAAE,IAAI,GAAG,OAAO,CAAC,IAAI,EAAE,EAAE,GAAG,OAAO,CAAC,EAAE;IACzE,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC7B,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAc,CAAC,EAAE,EAAE,EAAE,GAAG,EAAE;YAC7C,OAAO,CAAC,GAAG,CAAC,GAAG,WAAW,uBAAuB,IAAI,EAAE,CAAC,CAAC;YACzD,IAAI,OAAO,CAAC,QAAQ,EAAE;gBACpB,OAAO,CAAC,GAAG,CAAC,oBAAoB,CAAC,CAAC;aACnC;YACD,OAAO,EAAE,CAAC;QACZ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAA;AACJ,CAAC"}
@@ -0,0 +1,11 @@
1
+ export interface PlatformEvent {
2
+ tenant: string;
3
+ user?: string;
4
+ source: string;
5
+ kind: string;
6
+ eventName: string;
7
+ description: string;
8
+ metadata: any;
9
+ created?: Date;
10
+ }
11
+ export declare function emitPlatformEvent(platformEvent: PlatformEvent): Promise<import("axios").AxiosResponse<any>>;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.emitPlatformEvent = void 0;
4
+ const internal_service_1 = require("./internal-service");
5
+ const callPluginsService = (0, internal_service_1.service)('PLUGINS', { port: 9006 });
6
+ function emitPlatformEvent(platformEvent) {
7
+ return callPluginsService({
8
+ method: 'POST',
9
+ url: '/internal-api/events',
10
+ data: platformEvent
11
+ })
12
+ .catch();
13
+ }
14
+ exports.emitPlatformEvent = emitPlatformEvent;
15
+ //# sourceMappingURL=emit-internal-event.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"emit-internal-event.js","sourceRoot":"","sources":["../src/emit-internal-event.ts"],"names":[],"mappings":";;;AAAA,yDAA2C;AAE3C,MAAM,kBAAkB,GAAG,IAAA,0BAAO,EAAC,SAAS,EAAE,EAAC,IAAI,EAAE,IAAI,EAAC,CAAC,CAAC;AAa5D,SAAgB,iBAAiB,CAAC,aAA4B;IAC5D,OAAO,kBAAkB,CAAC;QACxB,MAAM,EAAE,MAAM;QACd,GAAG,EAAE,sBAAsB;QAC3B,IAAI,EAAE,aAAa;KACpB,CAAC;SACC,KAAK,EAAE,CAAA;AACZ,CAAC;AAPD,8CAOC"}
@@ -0,0 +1,6 @@
1
+ export * from './shutdown';
2
+ export * from './app';
3
+ export * from './router';
4
+ export * from './internal-service';
5
+ export * from './user-middlewares';
6
+ export * from './emit-internal-event';
package/dist/index.js ADDED
@@ -0,0 +1,23 @@
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./shutdown"), exports);
18
+ __exportStar(require("./app"), exports);
19
+ __exportStar(require("./router"), exports);
20
+ __exportStar(require("./internal-service"), exports);
21
+ __exportStar(require("./user-middlewares"), exports);
22
+ __exportStar(require("./emit-internal-event"), exports);
23
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6CAA0B;AAC1B,wCAAqB;AACrB,2CAAwB;AACxB,qDAAkC;AAClC,qDAAkC;AAClC,wDAAqC"}
@@ -0,0 +1,4 @@
1
+ import { AxiosPromise, AxiosRequestConfig } from 'axios';
2
+ import type { Service } from './types';
3
+ export declare function service(name: string, descriptorOverrides?: Partial<Service>): (options: AxiosRequestConfig) => AxiosPromise;
4
+ export declare function callInternalService(service: Service, options: AxiosRequestConfig): AxiosPromise;
@@ -0,0 +1,28 @@
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.callInternalService = exports.service = void 0;
7
+ const axios_1 = __importDefault(require("axios"));
8
+ function service(name, descriptorOverrides = {}) {
9
+ const service = createServiceDescriptor(name, descriptorOverrides);
10
+ return (options) => callInternalService(service, options);
11
+ }
12
+ exports.service = service;
13
+ function createServiceDescriptor(name, descriptorOverrides = {}) {
14
+ name = name.toUpperCase();
15
+ return {
16
+ protocol: process.env[`${name}_SERVICE_PROTOCOL`] || descriptorOverrides.protocol || 'http',
17
+ url: process.env[`${name}_SERVICE_URL`] || descriptorOverrides.url || 'localhost',
18
+ port: process.env[`${name}_SERVICE_PORT`] || descriptorOverrides.port || 8080,
19
+ };
20
+ }
21
+ function callInternalService(service, options) {
22
+ return (0, axios_1.default)({
23
+ ...options,
24
+ url: `${service.protocol}://${service.url}:${service.port}${options.url}`,
25
+ });
26
+ }
27
+ exports.callInternalService = callInternalService;
28
+ //# sourceMappingURL=internal-service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"internal-service.js","sourceRoot":"","sources":["../src/internal-service.ts"],"names":[],"mappings":";;;;;;AAAA,kDAAgE;AAIhE,SAAgB,OAAO,CAAC,IAAY,EAAE,sBAAwC,EAAE;IAC9E,MAAM,OAAO,GAAG,uBAAuB,CAAC,IAAI,EAAE,mBAAmB,CAAC,CAAC;IAEnE,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,mBAAmB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AAC5D,CAAC;AAJD,0BAIC;AAED,SAAS,uBAAuB,CAAC,IAAY,EAAE,sBAAwC,EAAE;IACvF,IAAI,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC;IAE1B,OAAO;QACL,QAAQ,EAAG,OAAO,CAAC,GAAG,CAAC,GAAG,IAAI,mBAAmB,CAAiC,IAAI,mBAAmB,CAAC,QAAQ,IAAI,MAAM;QAC5H,GAAG,EAAE,OAAO,CAAC,GAAG,CAAC,GAAG,IAAI,cAAc,CAAC,IAAI,mBAAmB,CAAC,GAAG,IAAI,WAAW;QACjF,IAAI,EAAE,OAAO,CAAC,GAAG,CAAC,GAAG,IAAI,eAAe,CAAC,IAAI,mBAAmB,CAAC,IAAI,IAAI,IAAI;KAC9E,CAAC;AACJ,CAAC;AAED,SAAgB,mBAAmB,CAAC,OAAgB,EAAE,OAA2B;IAC/E,OAAO,IAAA,eAAK,EAAC;QACX,GAAG,OAAO;QACV,GAAG,EAAE,GAAG,OAAO,CAAC,QAAQ,MAAM,OAAO,CAAC,GAAG,IAAI,OAAO,CAAC,IAAI,GAAG,OAAO,CAAC,GAAG,EAAE;KAC1E,CAAC,CAAC;AACL,CAAC;AALD,kDAKC"}
@@ -0,0 +1,2 @@
1
+ import { Router } from 'express';
2
+ export declare function getRouter(): Router;
package/dist/router.js ADDED
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getRouter = void 0;
4
+ const express_1 = require("express");
5
+ function getRouter() {
6
+ return (0, express_1.Router)();
7
+ }
8
+ exports.getRouter = getRouter;
9
+ //# sourceMappingURL=router.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"router.js","sourceRoot":"","sources":["../src/router.ts"],"names":[],"mappings":";;;AAAA,qCAA+B;AAE/B,SAAgB,SAAS;IACvB,OAAO,IAAA,gBAAM,GAAE,CAAC;AAClB,CAAC;AAFD,8BAEC"}
@@ -0,0 +1,2 @@
1
+ declare let shutdown: () => never;
2
+ export default shutdown;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ let shutdown = () => {
4
+ process.exit();
5
+ };
6
+ if (process.env.NODE_ENV !== 'production') {
7
+ shutdown = () => {
8
+ const fs = require('fs');
9
+ const cwd = process.cwd();
10
+ fs.readdirSync(cwd).forEach(filename => {
11
+ if (filename.includes('index.') || filename.includes('server.')) {
12
+ const filePath = require('path').join(cwd, filename);
13
+ fs.writeFileSync(filePath, fs.readFileSync(filePath));
14
+ }
15
+ });
16
+ process.exit();
17
+ };
18
+ process.on('exit', shutdown);
19
+ }
20
+ globalThis.shutdown = shutdown;
21
+ exports.default = shutdown;
22
+ //# sourceMappingURL=shutdown.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shutdown.js","sourceRoot":"","sources":["../src/shutdown.ts"],"names":[],"mappings":";;AAAA,IAAI,QAAQ,GAAG,GAAG,EAAE;IAClB,OAAO,CAAC,IAAI,EAAE,CAAC;AACjB,CAAC,CAAC;AAEF,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,EAAE;IACzC,QAAQ,GAAG,GAAG,EAAE;QACd,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;QACzB,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC;QAC1B,EAAE,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE;YACrC,IAAI,QAAQ,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,QAAQ,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE;gBAC/D,MAAM,QAAQ,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;gBACrD,EAAE,CAAC,aAAa,CAAC,QAAQ,EAAE,EAAE,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC,CAAA;aACtD;QACH,CAAC,CAAC,CAAA;QACF,OAAO,CAAC,IAAI,EAAE,CAAC;IACjB,CAAC,CAAA;IAED,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;CAC9B;AAED,UAAU,CAAC,QAAQ,GAAG,QAAQ,CAAC;AAE/B,kBAAe,QAAQ,CAAC"}
@@ -0,0 +1,23 @@
1
+ import type { Request } from 'express';
2
+ export interface ApiConfig {
3
+ cors: boolean;
4
+ bodyParser?: BodyParserType | false | null;
5
+ port: string | number;
6
+ ip: string;
7
+ showLogs: boolean;
8
+ }
9
+ export type BodyParserType = 'json' | 'urlencoded' | 'raw' | 'text';
10
+ export interface Service {
11
+ url: string;
12
+ port: number | string;
13
+ protocol: ServiceProtocol;
14
+ }
15
+ export type ServiceProtocol = 'http' | 'https';
16
+ export interface RequestWithUser extends Omit<Request, 'headers'> {
17
+ user?: Record<string, any>;
18
+ headers?: RequestHeadersWithUser;
19
+ }
20
+ type RequestHeadersWithUser = Request['headers'] | {
21
+ user?: string;
22
+ };
23
+ export {};
package/dist/types.js ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,4 @@
1
+ import type { Response, NextFunction } from 'express';
2
+ import type { RequestWithUser } from './types';
3
+ export declare function populateUser(req: RequestWithUser, res: Response, next: NextFunction): void;
4
+ export declare function verifyUser(req: RequestWithUser, res: Response, next: NextFunction): void;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.verifyUser = exports.populateUser = void 0;
4
+ function populateUser(req, res, next) {
5
+ try {
6
+ req.user = req.headers.user ? JSON.parse(req.headers.user) : null;
7
+ next();
8
+ }
9
+ catch (e) {
10
+ res.status(400).json({ code: 'INVALID_USER' }).end();
11
+ }
12
+ }
13
+ exports.populateUser = populateUser;
14
+ function verifyUser(req, res, next) {
15
+ if (req.user) {
16
+ next();
17
+ }
18
+ else {
19
+ res.status(401).end();
20
+ }
21
+ }
22
+ exports.verifyUser = verifyUser;
23
+ //# sourceMappingURL=user-middlewares.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"user-middlewares.js","sourceRoot":"","sources":["../src/user-middlewares.ts"],"names":[],"mappings":";;;AAIA,SAAgB,YAAY,CAAC,GAAoB,EAAE,GAAa,EAAE,IAAkB;IAClF,IAAI;QACF,GAAG,CAAC,IAAI,GAAG,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,IAAc,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;QAC5E,IAAI,EAAE,CAAC;KACR;IAAC,OAAO,CAAC,EAAE;QACV,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,cAAc,EAAE,CAAC,CAAC,GAAG,EAAE,CAAC;KACtD;AACH,CAAC;AAPD,oCAOC;AAED,SAAgB,UAAU,CAAC,GAAoB,EAAE,GAAa,EAAE,IAAkB;IAChF,IAAI,GAAG,CAAC,IAAI,EAAE;QACZ,IAAI,EAAE,CAAC;KACR;SAAM;QACL,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC;KACvB;AACH,CAAC;AAND,gCAMC"}
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@qelos/api-kit",
3
+ "version": "3.3.6",
4
+ "description": "API-Kit package to help with qelos infrastructure and reuse capabilities across services",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc"
9
+ },
10
+ "author": "David Meir-Levy <davidmeirlevy@gmail.com>",
11
+ "license": "MIT",
12
+ "publishConfig": {
13
+ "access": "public"
14
+ },
15
+ "dependencies": {
16
+ "axios": "^0.21.1",
17
+ "cors": "^2.8.5",
18
+ "express": "^4.17.1",
19
+ "morgan": "^1.10.0"
20
+ },
21
+ "gitHead": "c4a69b318689cb5c7d35a05f057e0ed6c9676594",
22
+ "devDependencies": {
23
+ "@types/node": "^17.0.5",
24
+ "typescript": "^4.9.5"
25
+ }
26
+ }
package/src/app.ts ADDED
@@ -0,0 +1,63 @@
1
+ import express, {Express} from 'express';
2
+
3
+ import type {ApiConfig, BodyParserType} from './types';
4
+ import shutdown from './shutdown';
5
+
6
+
7
+ export const config = (updatedConfig: any = _config): ApiConfig => {
8
+ _config = {..._config, ...updatedConfig};
9
+ return _config;
10
+ }
11
+ export const app = () => _app || createApp();
12
+ export const start = startApp;
13
+
14
+ let _app: Express;
15
+ let _config: ApiConfig = {
16
+ cors: !!process.env.API_CORS || false,
17
+ bodyParser: (process.env.API_BODY_PARSER as BodyParserType) || 'json',
18
+ showLogs: !!process.env.SHOW_LOGS,
19
+ port: process.env?.PORT || '3000',
20
+ ip: process.env.IP || '127.0.0.1'
21
+ };
22
+
23
+ function createApp() {
24
+ _app = express()
25
+
26
+ configureApp(_app);
27
+
28
+ return _app;
29
+ }
30
+
31
+ function configureApp(app: Express) {
32
+ if (process.env.NODE_ENV !== 'production') {
33
+ app.use(require('morgan')('combined'))
34
+ app.get('/api/shutdown', () => {
35
+ shutdown()
36
+ })
37
+ } else if (_config.showLogs) {
38
+ app.use(require('morgan')('combined'));
39
+ }
40
+ if (_config.cors) {
41
+ app.use(require('cors')())
42
+ }
43
+ if (_config.bodyParser) {
44
+ app.use(express[_config.bodyParser]())
45
+ }
46
+ }
47
+
48
+ export function getBodyParser() {
49
+ return express.json();
50
+ }
51
+
52
+ function startApp(serviceName = 'APP', port = _config.port, ip = _config.ip): Promise<void> {
53
+ return new Promise((resolve) => {
54
+ _app.listen(parseInt(port as string), ip, () => {
55
+ console.log(`${serviceName} is running on port ${port}`);
56
+ if (_config.showLogs) {
57
+ console.log('logs are available');
58
+ }
59
+ resolve();
60
+ });
61
+ })
62
+ }
63
+
@@ -0,0 +1,23 @@
1
+ import {service} from './internal-service';
2
+
3
+ const callPluginsService = service('PLUGINS', {port: 9006});
4
+
5
+ export interface PlatformEvent {
6
+ tenant: string;
7
+ user?: string;
8
+ source: string;
9
+ kind: string;
10
+ eventName: string;
11
+ description: string;
12
+ metadata: any;
13
+ created?: Date;
14
+ }
15
+
16
+ export function emitPlatformEvent(platformEvent: PlatformEvent) {
17
+ return callPluginsService({
18
+ method: 'POST',
19
+ url: '/internal-api/events',
20
+ data: platformEvent
21
+ })
22
+ .catch()
23
+ }
package/src/index.ts ADDED
@@ -0,0 +1,6 @@
1
+ export * from './shutdown'
2
+ export * from './app'
3
+ export * from './router'
4
+ export * from './internal-service'
5
+ export * from './user-middlewares'
6
+ export * from './emit-internal-event'
@@ -0,0 +1,26 @@
1
+ import axios, { AxiosPromise, AxiosRequestConfig } from 'axios';
2
+
3
+ import type { Service, ServiceProtocol } from './types';
4
+
5
+ export function service(name: string, descriptorOverrides: Partial<Service> = {}): (options: AxiosRequestConfig) => AxiosPromise {
6
+ const service = createServiceDescriptor(name, descriptorOverrides);
7
+
8
+ return (options) => callInternalService(service, options);
9
+ }
10
+
11
+ function createServiceDescriptor(name: string, descriptorOverrides: Partial<Service> = {}): Service {
12
+ name = name.toUpperCase();
13
+
14
+ return {
15
+ protocol: (process.env[`${name}_SERVICE_PROTOCOL`] as ServiceProtocol | undefined) || descriptorOverrides.protocol || 'http',
16
+ url: process.env[`${name}_SERVICE_URL`] || descriptorOverrides.url || 'localhost',
17
+ port: process.env[`${name}_SERVICE_PORT`] || descriptorOverrides.port || 8080,
18
+ };
19
+ }
20
+
21
+ export function callInternalService(service: Service, options: AxiosRequestConfig): AxiosPromise {
22
+ return axios({
23
+ ...options,
24
+ url: `${service.protocol}://${service.url}:${service.port}${options.url}`,
25
+ });
26
+ }
package/src/router.ts ADDED
@@ -0,0 +1,5 @@
1
+ import {Router} from 'express';
2
+
3
+ export function getRouter(): Router {
4
+ return Router();
5
+ }
@@ -0,0 +1,23 @@
1
+ let shutdown = () => {
2
+ process.exit();
3
+ };
4
+
5
+ if (process.env.NODE_ENV !== 'production') {
6
+ shutdown = () => {
7
+ const fs = require('fs');
8
+ const cwd = process.cwd();
9
+ fs.readdirSync(cwd).forEach(filename => {
10
+ if (filename.includes('index.') || filename.includes('server.')) {
11
+ const filePath = require('path').join(cwd, filename);
12
+ fs.writeFileSync(filePath, fs.readFileSync(filePath))
13
+ }
14
+ })
15
+ process.exit();
16
+ }
17
+
18
+ process.on('exit', shutdown);
19
+ }
20
+
21
+ globalThis.shutdown = shutdown;
22
+
23
+ export default shutdown;
package/src/types.ts ADDED
@@ -0,0 +1,29 @@
1
+ import type { Request } from 'express';
2
+
3
+ export interface ApiConfig {
4
+ cors: boolean;
5
+ bodyParser?: BodyParserType | false | null;
6
+ port: string | number;
7
+ ip: string;
8
+ showLogs: boolean
9
+ }
10
+
11
+ export type BodyParserType = 'json' | 'urlencoded' | 'raw' | 'text';
12
+
13
+ export interface Service {
14
+ url: string;
15
+ port: number | string;
16
+ protocol: ServiceProtocol;
17
+ }
18
+
19
+ export type ServiceProtocol = 'http' | 'https';
20
+
21
+ export interface RequestWithUser extends Omit<Request, 'headers'> {
22
+ user?: Record<string, any>;
23
+ headers?: RequestHeadersWithUser
24
+ }
25
+
26
+ type RequestHeadersWithUser = Request['headers'] | {
27
+ user?: string
28
+ };
29
+
@@ -0,0 +1,21 @@
1
+ import type { Response, NextFunction } from 'express';
2
+
3
+ import type { RequestWithUser } from './types';
4
+
5
+ export function populateUser(req: RequestWithUser, res: Response, next: NextFunction): void {
6
+ try {
7
+ req.user = req.headers.user ? JSON.parse(req.headers.user as string) : null;
8
+ next();
9
+ } catch (e) {
10
+ res.status(400).json({ code: 'INVALID_USER' }).end();
11
+ }
12
+ }
13
+
14
+ export function verifyUser(req: RequestWithUser, res: Response, next: NextFunction): void {
15
+ if (req.user) {
16
+ next();
17
+ } else {
18
+ res.status(401).end();
19
+ }
20
+ }
21
+
package/tsconfig.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "commonjs",
4
+ "target": "esnext",
5
+ "outDir": "./dist",
6
+ "sourceMap": true,
7
+ "declaration": true,
8
+ "skipLibCheck": true,
9
+ "esModuleInterop": true,
10
+ "allowSyntheticDefaultImports": true,
11
+ "baseUrl": ".",
12
+ "paths": {
13
+ "*": [
14
+ "dist/*"
15
+ ]
16
+ }
17
+ },
18
+ "exclude": [
19
+ "node_modules",
20
+ "dist"
21
+ ],
22
+ }