@orion-js/http 4.0.0-next.4 → 4.0.0-next.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/dist/index.cjs +28 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -2
- package/dist/index.d.ts +4 -2
- package/dist/index.js +26 -10
- package/dist/index.js.map +1 -1
- package/package.json +14 -16
package/dist/index.cjs
CHANGED
|
@@ -29,8 +29,6 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
29
29
|
// src/index.ts
|
|
30
30
|
var index_exports = {};
|
|
31
31
|
__export(index_exports, {
|
|
32
|
-
Request: () => import_express3.Request,
|
|
33
|
-
Response: () => import_express3.Response,
|
|
34
32
|
Route: () => Route,
|
|
35
33
|
Routes: () => Routes,
|
|
36
34
|
bodyParser: () => bodyParser2,
|
|
@@ -135,16 +133,35 @@ function route(options) {
|
|
|
135
133
|
};
|
|
136
134
|
}
|
|
137
135
|
|
|
136
|
+
// ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/type.js
|
|
137
|
+
function type(input) {
|
|
138
|
+
if (input === null) {
|
|
139
|
+
return "Null";
|
|
140
|
+
} else if (input === void 0) {
|
|
141
|
+
return "Undefined";
|
|
142
|
+
} else if (Number.isNaN(input)) {
|
|
143
|
+
return "NaN";
|
|
144
|
+
}
|
|
145
|
+
const typeResult = Object.prototype.toString.call(input).slice(8, -1);
|
|
146
|
+
return typeResult === "AsyncFunction" ? "Promise" : typeResult;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/isNil.js
|
|
150
|
+
function isNil(x) {
|
|
151
|
+
return x === void 0 || x === null;
|
|
152
|
+
}
|
|
153
|
+
|
|
138
154
|
// src/routes/executeRequest.ts
|
|
139
|
-
var import_lodash = require("lodash");
|
|
140
155
|
var import_env2 = require("@orion-js/env");
|
|
141
156
|
var import_helpers2 = require("@orion-js/helpers");
|
|
142
157
|
var simulateLatency = (0, import_env2.internalGetEnv)("simulate_latency", "SIMULATE_LATENCY");
|
|
143
158
|
async function executeRequest(route2, req, res) {
|
|
144
159
|
if (simulateLatency) {
|
|
145
|
-
const time = parseInt(simulateLatency);
|
|
160
|
+
const time = Number.parseInt(simulateLatency);
|
|
146
161
|
if (time) {
|
|
147
|
-
|
|
162
|
+
const min = time * 0.9;
|
|
163
|
+
const max = time * 1.1;
|
|
164
|
+
await (0, import_helpers2.sleep)(Math.floor(Math.random() * (max - min + 1)) + min);
|
|
148
165
|
}
|
|
149
166
|
}
|
|
150
167
|
try {
|
|
@@ -159,8 +176,8 @@ async function executeRequest(route2, req, res) {
|
|
|
159
176
|
res.setHeader(key, result.headers[key]);
|
|
160
177
|
});
|
|
161
178
|
}
|
|
162
|
-
if (!
|
|
163
|
-
if ((
|
|
179
|
+
if (!isNil(result.body)) {
|
|
180
|
+
if (type(result.body) === "Object") {
|
|
164
181
|
res.json(result.body);
|
|
165
182
|
} else {
|
|
166
183
|
res.send(result.body);
|
|
@@ -191,17 +208,15 @@ function registerRoute(route2) {
|
|
|
191
208
|
}
|
|
192
209
|
|
|
193
210
|
// src/routes/registerRoutes.ts
|
|
194
|
-
var import_lodash2 = require("lodash");
|
|
195
211
|
function registerRoutes(routesMap) {
|
|
196
|
-
const routes =
|
|
212
|
+
const routes = Object.values(routesMap);
|
|
197
213
|
for (const route2 of routes) {
|
|
198
214
|
registerRoute(route2);
|
|
199
215
|
}
|
|
200
216
|
}
|
|
201
217
|
|
|
202
218
|
// src/index.ts
|
|
203
|
-
var import_body_parser2 = require("body-parser");
|
|
204
|
-
var import_express3 = require("express");
|
|
219
|
+
var import_body_parser2 = __toESM(require("body-parser"), 1);
|
|
205
220
|
|
|
206
221
|
// src/service/index.ts
|
|
207
222
|
var import_services = require("@orion-js/services");
|
|
@@ -243,11 +258,10 @@ function getServiceRoutes(target) {
|
|
|
243
258
|
}
|
|
244
259
|
|
|
245
260
|
// src/index.ts
|
|
246
|
-
var
|
|
261
|
+
var { json, raw, text, urlencoded } = import_body_parser2.default;
|
|
262
|
+
var bodyParser2 = { json, raw, text, urlencoded };
|
|
247
263
|
// Annotate the CommonJS export names for ESM import in node:
|
|
248
264
|
0 && (module.exports = {
|
|
249
|
-
Request,
|
|
250
|
-
Response,
|
|
251
265
|
Route,
|
|
252
266
|
Routes,
|
|
253
267
|
bodyParser,
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/errors.ts","../src/viewer.ts","../src/start.ts","../src/routes/route.ts","../src/routes/executeRequest.ts","../src/routes/registerRoute.ts","../src/routes/registerRoutes.ts","../src/service/index.ts"],"sourcesContent":["import {setOnError, onError} from './errors'\nimport {getViewer, setGetViewer} from './viewer'\nimport {startServer, getApp, getServer} from './start'\nimport express from 'express'\nimport route from './routes/route'\nimport registerRoute from './routes/registerRoute'\nimport registerRoutes from './routes/registerRoutes'\nimport {json, raw, text, urlencoded} from 'body-parser'\nimport {RequestHandler} from 'express'\n\nconst bodyParser: {\n json: () => RequestHandler\n raw: () => RequestHandler\n text: () => RequestHandler\n urlencoded: (options?: {extended: boolean}) => RequestHandler\n} = {json, raw, text, urlencoded}\n\nexport {\n express,\n startServer,\n getApp,\n getServer,\n getViewer,\n setGetViewer,\n setOnError,\n onError,\n route,\n registerRoute,\n registerRoutes,\n bodyParser,\n}\n\nexport {Request, Response} from 'express'\n\nexport * from './types'\nexport * from './service'\n","import express from 'express'\nimport crypto from 'crypto'\n\ntype onErrorFunction = (req: express.Request, res: express.Response, error: any) => Promise<void>\n\nconst defaultOnError: onErrorFunction = async (req, res, error) => {\n if (error.isOrionError) {\n let statusCode = 400\n if (error.code === 'AuthError') {\n statusCode = 401\n } else {\n console.warn(`[route/handler] OrionError in ${req.path}:`, error)\n }\n\n const data = error.getInfo()\n\n res.status(statusCode)\n res.json(data)\n } else if (error.isGraphQLError) {\n res.writeHead(error.statusCode)\n res.end(error.message)\n console.warn(`[route/handler] GraphQLError in ${req.path}:`, error)\n } else {\n const hash = crypto\n .createHash('sha1')\n .update(error.message, 'utf8')\n .digest('hex')\n .substring(0, 10)\n const statusCode = 500\n const data = {error: 500, message: 'Internal server error', hash}\n\n res.writeHead(statusCode)\n res.end(JSON.stringify(data, null, 2))\n\n error.hash = hash\n console.error(`[route/handler] Internal server error in ${req.url}:`, error)\n }\n}\n\nlet onErrorRef: onErrorFunction = defaultOnError\n\nexport const onError: onErrorFunction = async (req, res, error) => {\n await onErrorRef(req, res, error)\n}\n\nexport const setOnError = (onErrorFunc: onErrorFunction): void => {\n onErrorRef = onErrorFunc\n}\n","import express from 'express'\nimport {UserError} from '@orion-js/helpers'\n\nglobal.getViewerRef = () => null\n\nexport const getViewer = async (req: express.Request): Promise<any> => {\n try {\n const viewer = await global.getViewerRef(req)\n if (!viewer) return {}\n return viewer\n } catch (err) {\n throw new UserError('AuthError', err.message)\n }\n}\n\nexport const setGetViewer = (getViewerFunc: (req: express.Request) => any): void => {\n global.getViewerRef = getViewerFunc\n}\n","import {internalGetEnv} from '@orion-js/env'\nimport express from 'express'\n\nglobal.appRef = null\nglobal.serverRef = null\n\nexport interface StartOrionOptions {\n keepAliveTimeout?: number\n}\n\nexport const startServer = (\n port: number = Number(internalGetEnv('http_port', 'PORT')),\n otherOptions: StartOrionOptions = {},\n) => {\n const app = getApp()\n\n const server = app.listen(port)\n global.serverRef = server\n\n if (otherOptions.keepAliveTimeout) {\n server.keepAliveTimeout = otherOptions.keepAliveTimeout // Ensure all inactive connections are terminated by the ALB, by setting this a few seconds higher than the ALB idle timeout\n server.headersTimeout = otherOptions.keepAliveTimeout + 1000\n }\n\n return app\n}\n\nexport const getApp = (): express.Express => {\n if (global.appRef) return global.appRef as express.Express\n\n const app = express()\n\n global.appRef = app\n\n return app\n}\n\nexport const getServer = () => {\n return global.serverRef\n}\n","import {RouteType, OrionRouteOptions} from '../types'\n\nexport default function route(options: OrionRouteOptions): RouteType {\n return {\n ...options,\n }\n}\n","import {RouteType} from './../types'\nimport {onError} from '../errors'\nimport {getViewer} from './../viewer'\nimport express from 'express'\nimport {isNil, isPlainObject, random} from 'lodash'\nimport {internalGetEnv} from '@orion-js/env'\nimport {sleep} from '@orion-js/helpers'\n\nconst simulateLatency = internalGetEnv('simulate_latency', 'SIMULATE_LATENCY')\n\nexport async function executeRequest(\n route: RouteType,\n req: express.Request,\n res: express.Response,\n) {\n if (simulateLatency) {\n const time = parseInt(simulateLatency)\n if (time) {\n await sleep(random(time * 0.9, time * 1.1))\n }\n }\n\n try {\n const viewer = await getViewer(req)\n const result = await route.resolve(req, res, viewer)\n if (!result) return\n\n // add status code to response\n if (result.statusCode) {\n res.status(result.statusCode)\n }\n\n // add headers to response\n if (result.headers) {\n Object.keys(result.headers).forEach(key => {\n res.setHeader(key, result.headers[key])\n })\n }\n\n // add body to response\n if (!isNil(result.body)) {\n if (isPlainObject(result.body)) {\n res.json(result.body)\n } else {\n res.send(result.body)\n }\n }\n } catch (error) {\n await onError(req, res, error)\n }\n}\n","import {getApp} from './../start'\nimport {RouteType} from '../types'\nimport {executeRequest} from './executeRequest'\nimport express from 'express'\nimport bodyParser from 'body-parser'\n\nexport default function registerRoute(route: RouteType): void {\n const app = route.app || getApp()\n const method = route.method\n\n const handler = async (req, res) => {\n executeRequest(route, req, res)\n }\n\n const handlers: Array<express.RequestHandler> = [handler]\n\n if (route.bodyParser) {\n const parser = bodyParser[route.bodyParser](route.bodyParserOptions)\n handlers.unshift(parser)\n }\n\n if (route.middlewares) {\n handlers.unshift(...route.middlewares)\n }\n\n app[method](route.path, ...handlers)\n}\n","import {values} from 'lodash'\nimport registerRoute from './registerRoute'\nimport {RoutesMap} from '../types'\n\nexport default function registerRoutes(routesMap: RoutesMap): void {\n const routes = values(routesMap)\n\n for (const route of routes) {\n registerRoute(route)\n }\n}\n","import {getInstance, Service} from '@orion-js/services'\nimport route from '../routes/route'\nimport {OrionRouteOptions, RoutesMap} from '../types'\n\n// Define metadata storage using WeakMaps\nconst serviceMetadata = new WeakMap<any, {_serviceType: string}>()\nconst routeMetadata = new WeakMap<any, Record<string, any>>()\n\nexport function Routes() {\n return (target: any, context: ClassDecoratorContext<any>) => {\n Service()(target, context)\n\n context.addInitializer(function (this) {\n serviceMetadata.set(this, {_serviceType: 'routes'})\n })\n }\n}\n\nexport function Route<\n This,\n TArgs extends Parameters<OrionRouteOptions['resolve']>,\n TReturn extends ReturnType<OrionRouteOptions['resolve']>,\n>(options: Omit<OrionRouteOptions, 'resolve'>) {\n return (\n method: (this: This, ...args: TArgs) => TReturn,\n context: ClassMethodDecoratorContext<This, typeof method>,\n ) => {\n const propertyKey = String(context.name)\n\n context.addInitializer(function (this: This) {\n const routes = routeMetadata.get(this) || {}\n\n routes[propertyKey] = route({\n ...options,\n resolve: this[propertyKey].bind(this),\n })\n\n routeMetadata.set(this, routes)\n })\n\n return method\n }\n}\n\nexport function getServiceRoutes(target: any): RoutesMap {\n const instance = getInstance(target)\n\n if (!serviceMetadata.has(instance.constructor)) {\n throw new Error('You must pass a class decorated with @Routes to getServiceRoutes')\n }\n\n const instanceMetadata = serviceMetadata.get(instance.constructor)\n if (instanceMetadata._serviceType !== 'routes') {\n throw new Error('You must pass a class decorated with @Routes to getServiceRoutes')\n }\n\n const routesMap = routeMetadata.get(instance) || {}\n\n return routesMap\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAAA;AAAA,EAAA,+BAAAC;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,oBAAmB;AAInB,IAAM,iBAAkC,OAAO,KAAK,KAAK,UAAU;AACjE,MAAI,MAAM,cAAc;AACtB,QAAI,aAAa;AACjB,QAAI,MAAM,SAAS,aAAa;AAC9B,mBAAa;AAAA,IACf,OAAO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AAM,cAAc,OAAO,QAAQ,IAAI;AAEvC,YAAQ,eAAe,WAAsB;AAC3C,YAAM,SAAS,cAAc,IAAI,IAAI,KAAK,CAAC;AAE3C,aAAO,WAAW,IAAI,MAAM;AAAA,QAC1B,GAAG;AAAA,QACH,SAAS,KAAK,WAAW,EAAE,KAAK,IAAI;AAAA,MACtC,CAAC;AAED,oBAAc,IAAI,MAAM,MAAM;AAAA,IAChC,CAAC;AAED,WAAO;AAAA,EACT;AACF;AAEO,SAAS,iBAAiB,QAAwB;AACvD,QAAM,eAAW,6BAAY,MAAM;AAEnC,MAAI,CAAC,gBAAgB,IAAI,SAAS,WAAW,GAAG;AAC9C,UAAM,IAAI,MAAM,kEAAkE;AAAA,EACpF;AAEA,QAAM,mBAAmB,gBAAgB,IAAI,SAAS,WAAW;AACjE,MAAI,iBAAiB,iBAAiB,UAAU;AAC9C,UAAM,IAAI,MAAM,kEAAkE;AAAA,EACpF;AAEA,QAAM,YAAY,cAAc,IAAI,QAAQ,KAAK,CAAC;AAElD,SAAO;AACT;;;ARjDA,IAAMC,cAKF,EAAC,gCAAM,8BAAK,gCAAM,2CAAU;","names":["bodyParser","express","crypto","express","import_express","import_env","import_helpers","route","route","bodyParser","import_lodash","route","import_body_parser","import_express","bodyParser"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/errors.ts","../src/viewer.ts","../src/start.ts","../src/routes/route.ts","../../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/type.js","../../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/isNil.js","../src/routes/executeRequest.ts","../src/routes/registerRoute.ts","../src/routes/registerRoutes.ts","../src/service/index.ts"],"sourcesContent":["import {setOnError, onError} from './errors'\nimport {getViewer, setGetViewer} from './viewer'\nimport {startServer, getApp, getServer} from './start'\nimport express from 'express'\nimport route from './routes/route'\nimport registerRoute from './routes/registerRoute'\nimport registerRoutes from './routes/registerRoutes'\nimport bodyParserLib from 'body-parser'\nimport {RequestHandler} from 'express'\n\nconst {json, raw, text, urlencoded} = bodyParserLib\n\nconst bodyParser: {\n json: () => RequestHandler\n raw: () => RequestHandler\n text: () => RequestHandler\n urlencoded: (options?: {extended: boolean}) => RequestHandler\n} = {json, raw, text, urlencoded}\n\nexport {\n express,\n startServer,\n getApp,\n getServer,\n getViewer,\n setGetViewer,\n setOnError,\n onError,\n route,\n registerRoute,\n registerRoutes,\n bodyParser,\n}\n\nexport * from './types'\nexport * from './service'\n","import express from 'express'\nimport crypto from 'crypto'\n\ntype onErrorFunction = (req: express.Request, res: express.Response, error: any) => Promise<void>\n\nconst defaultOnError: onErrorFunction = async (req, res, error) => {\n if (error.isOrionError) {\n let statusCode = 400\n if (error.code === 'AuthError') {\n statusCode = 401\n } else {\n console.warn(`[route/handler] OrionError in ${req.path}:`, error)\n }\n\n const data = error.getInfo()\n\n res.status(statusCode)\n res.json(data)\n } else if (error.isGraphQLError) {\n res.writeHead(error.statusCode)\n res.end(error.message)\n console.warn(`[route/handler] GraphQLError in ${req.path}:`, error)\n } else {\n const hash = crypto\n .createHash('sha1')\n .update(error.message, 'utf8')\n .digest('hex')\n .substring(0, 10)\n const statusCode = 500\n const data = {error: 500, message: 'Internal server error', hash}\n\n res.writeHead(statusCode)\n res.end(JSON.stringify(data, null, 2))\n\n error.hash = hash\n console.error(`[route/handler] Internal server error in ${req.url}:`, error)\n }\n}\n\nlet onErrorRef: onErrorFunction = defaultOnError\n\nexport const onError: onErrorFunction = async (req, res, error) => {\n await onErrorRef(req, res, error)\n}\n\nexport const setOnError = (onErrorFunc: onErrorFunction): void => {\n onErrorRef = onErrorFunc\n}\n","import express from 'express'\nimport {UserError} from '@orion-js/helpers'\n\nglobal.getViewerRef = () => null\n\nexport const getViewer = async (req: express.Request): Promise<any> => {\n try {\n const viewer = await global.getViewerRef(req)\n if (!viewer) return {}\n return viewer\n } catch (err) {\n throw new UserError('AuthError', err.message)\n }\n}\n\nexport const setGetViewer = (getViewerFunc: (req: express.Request) => any): void => {\n global.getViewerRef = getViewerFunc\n}\n","import {internalGetEnv} from '@orion-js/env'\nimport express from 'express'\n\nglobal.appRef = null\nglobal.serverRef = null\n\nexport interface StartOrionOptions {\n keepAliveTimeout?: number\n}\n\nexport const startServer = (\n port: number = Number(internalGetEnv('http_port', 'PORT')),\n otherOptions: StartOrionOptions = {},\n) => {\n const app = getApp()\n\n const server = app.listen(port)\n global.serverRef = server\n\n if (otherOptions.keepAliveTimeout) {\n server.keepAliveTimeout = otherOptions.keepAliveTimeout // Ensure all inactive connections are terminated by the ALB, by setting this a few seconds higher than the ALB idle timeout\n server.headersTimeout = otherOptions.keepAliveTimeout + 1000\n }\n\n return app\n}\n\nexport const getApp = (): express.Express => {\n if (global.appRef) return global.appRef as express.Express\n\n const app = express()\n\n global.appRef = app\n\n return app\n}\n\nexport const getServer = () => {\n return global.serverRef\n}\n","import {RouteType, OrionRouteOptions} from '../types'\n\nexport default function route(options: OrionRouteOptions): RouteType {\n return {\n ...options,\n }\n}\n","export function type(input){\n if (input === null){\n return 'Null'\n } else if (input === undefined){\n return 'Undefined'\n } else if (Number.isNaN(input)){\n return 'NaN'\n }\n const typeResult = Object.prototype.toString.call(input).slice(8, -1)\n\n return typeResult === 'AsyncFunction' ? 'Promise' : typeResult\n}\n","export function isNil(x){\n return x === undefined || x === null\n}\n","import {RouteType} from './../types'\nimport {onError} from '../errors'\nimport {getViewer} from './../viewer'\nimport express from 'express'\nimport {isNil, type} from 'rambdax'\nimport {internalGetEnv} from '@orion-js/env'\nimport {sleep} from '@orion-js/helpers'\n\nconst simulateLatency = internalGetEnv('simulate_latency', 'SIMULATE_LATENCY')\n\nexport async function executeRequest(\n route: RouteType,\n req: express.Request,\n res: express.Response,\n) {\n if (simulateLatency) {\n const time = Number.parseInt(simulateLatency)\n if (time) {\n const min = time * 0.9\n const max = time * 1.1\n await sleep(Math.floor(Math.random() * (max - min + 1)) + min)\n }\n }\n\n try {\n const viewer = await getViewer(req)\n const result = await route.resolve(req, res, viewer)\n if (!result) return\n\n // add status code to response\n if (result.statusCode) {\n res.status(result.statusCode)\n }\n\n // add headers to response\n if (result.headers) {\n Object.keys(result.headers).forEach(key => {\n res.setHeader(key, result.headers[key])\n })\n }\n\n // add body to response\n if (!isNil(result.body)) {\n if (type(result.body) === 'Object') {\n res.json(result.body)\n } else {\n res.send(result.body)\n }\n }\n } catch (error) {\n await onError(req, res, error)\n }\n}\n","import {getApp} from './../start'\nimport {RouteType} from '../types'\nimport {executeRequest} from './executeRequest'\nimport express from 'express'\nimport bodyParser from 'body-parser'\n\nexport default function registerRoute(route: RouteType): void {\n const app = route.app || getApp()\n const method = route.method\n\n const handler = async (req, res) => {\n executeRequest(route, req, res)\n }\n\n const handlers: Array<express.RequestHandler> = [handler]\n\n if (route.bodyParser) {\n const parser = bodyParser[route.bodyParser](route.bodyParserOptions)\n handlers.unshift(parser)\n }\n\n if (route.middlewares) {\n handlers.unshift(...route.middlewares)\n }\n\n app[method](route.path, ...handlers)\n}\n","import registerRoute from './registerRoute'\nimport {RoutesMap} from '../types'\n\nexport default function registerRoutes(routesMap: RoutesMap): void {\n const routes = Object.values(routesMap)\n\n for (const route of routes) {\n registerRoute(route)\n }\n}\n","import {getInstance, Service} from '@orion-js/services'\nimport route from '../routes/route'\nimport {OrionRouteOptions, RoutesMap} from '../types'\n\n// Define metadata storage using WeakMaps\nconst serviceMetadata = new WeakMap<any, {_serviceType: string}>()\nconst routeMetadata = new WeakMap<any, Record<string, any>>()\n\nexport function Routes() {\n return (target: any, context: ClassDecoratorContext<any>) => {\n Service()(target, context)\n\n context.addInitializer(function (this) {\n serviceMetadata.set(this, {_serviceType: 'routes'})\n })\n }\n}\n\nexport function Route<\n This,\n TArgs extends Parameters<OrionRouteOptions['resolve']>,\n TReturn extends ReturnType<OrionRouteOptions['resolve']>,\n>(options: Omit<OrionRouteOptions, 'resolve'>) {\n return (\n method: (this: This, ...args: TArgs) => TReturn,\n context: ClassMethodDecoratorContext<This, typeof method>,\n ) => {\n const propertyKey = String(context.name)\n\n context.addInitializer(function (this: This) {\n const routes = routeMetadata.get(this) || {}\n\n routes[propertyKey] = route({\n ...options,\n resolve: this[propertyKey].bind(this),\n })\n\n routeMetadata.set(this, routes)\n })\n\n return method\n }\n}\n\nexport function getServiceRoutes(target: any): RoutesMap {\n const instance = getInstance(target)\n\n if (!serviceMetadata.has(instance.constructor)) {\n throw new Error('You must pass a class decorated with @Routes to getServiceRoutes')\n }\n\n const instanceMetadata = serviceMetadata.get(instance.constructor)\n if (instanceMetadata._serviceType !== 'routes') {\n throw new Error('You must pass a class decorated with @Routes to getServiceRoutes')\n }\n\n const routesMap = routeMetadata.get(instance) || {}\n\n return routesMap\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAAA;AAAA,EAAA,+BAAAC;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,oBAAmB;AAInB,IAAM,iBAAkC,OAAO,KAAK,KAAK,UAAU;AACjE,MAAI,MAAM,cAAc;AACtB,QAAI,aAAa;AACjB,QAAI,MAAM,SAAS,aAAa;AAC9B,mBAAa;AAAA,IACf,OAAO;AACL,cAAQ,KAAK,iCAAiC,IAAI,IAAI,KAAK,KAAK;AAAA,IAClE;AAEA,UAAM,OAAO,MAAM,QAAQ;AAE3B,QAAI,OAAO,UAAU;AACrB,QAAI,KAAK,IAAI;AAAA,EACf,WAAW,MAAM,gBAAgB;AAC/B,QAAI,UAAU,MAAM,UAAU;AAC9B,QAAI,IAAI,MAAM,OAAO;AACrB,YAAQ,KAAK,mCAAmC,IAAI,IAAI,KAAK,KAAK;AAAA,EACpE,OAAO;AACL,UAAM,OAAO,cAAAC,QACV,WAAW,MAAM,EACjB,OAAO,MAAM,SAAS,MAAM,EAC5B,OAAO,KAAK,EACZ,UAAU,GAAG,EAAE;AAClB,UAAM,aAAa;AACnB,UAAM,OAA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IAAI,KAAK,CAAC;AAE3C,aAAO,WAAW,IAAI,MAAM;AAAA,QAC1B,GAAG;AAAA,QACH,SAAS,KAAK,WAAW,EAAE,KAAK,IAAI;AAAA,MACtC,CAAC;AAED,oBAAc,IAAI,MAAM,MAAM;AAAA,IAChC,CAAC;AAED,WAAO;AAAA,EACT;AACF;AAEO,SAAS,iBAAiB,QAAwB;AACvD,QAAM,eAAW,6BAAY,MAAM;AAEnC,MAAI,CAAC,gBAAgB,IAAI,SAAS,WAAW,GAAG;AAC9C,UAAM,IAAI,MAAM,kEAAkE;AAAA,EACpF;AAEA,QAAM,mBAAmB,gBAAgB,IAAI,SAAS,WAAW;AACjE,MAAI,iBAAiB,iBAAiB,UAAU;AAC9C,UAAM,IAAI,MAAM,kEAAkE;AAAA,EACpF;AAEA,QAAM,YAAY,cAAc,IAAI,QAAQ,KAAK,CAAC;AAElD,SAAO;AACT;;;AVjDA,IAAM,EAAC,MAAM,KAAK,MAAM,WAAU,IAAI,oBAAAC;AAEtC,IAAMC,cAKF,EAAC,MAAM,KAAK,MAAM,WAAU;","names":["bodyParser","express","crypto","express","import_express","import_env","import_helpers","route","route","bodyParser","route","import_body_parser","bodyParserLib","bodyParser"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import express, { RequestHandler } from 'express';
|
|
2
|
-
export {
|
|
2
|
+
export { default as express } from 'express';
|
|
3
3
|
import { OptionsJson, OptionsText, OptionsUrlencoded } from 'body-parser';
|
|
4
4
|
|
|
5
5
|
type onErrorFunction = (req: express.Request, res: express.Response, error: any) => Promise<void>;
|
|
@@ -59,6 +59,8 @@ interface RouteType extends OrionRouteOptions {
|
|
|
59
59
|
interface RoutesMap {
|
|
60
60
|
[key: string]: RouteType;
|
|
61
61
|
}
|
|
62
|
+
type Request = express.Request;
|
|
63
|
+
type Response = express.Response;
|
|
62
64
|
|
|
63
65
|
declare function route(options: OrionRouteOptions): RouteType;
|
|
64
66
|
|
|
@@ -79,4 +81,4 @@ declare const bodyParser: {
|
|
|
79
81
|
}) => RequestHandler;
|
|
80
82
|
};
|
|
81
83
|
|
|
82
|
-
export { type OrionRouteOptions, Route, type RouteResolve, type RouteResponse, type RouteResponseObject, type RouteType, Routes, type RoutesMap, bodyParser, getApp, getServer, getServiceRoutes, getViewer, onError, registerRoute, registerRoutes, route, setGetViewer, setOnError, startServer };
|
|
84
|
+
export { type OrionRouteOptions, type Request, type Response, Route, type RouteResolve, type RouteResponse, type RouteResponseObject, type RouteType, Routes, type RoutesMap, bodyParser, getApp, getServer, getServiceRoutes, getViewer, onError, registerRoute, registerRoutes, route, setGetViewer, setOnError, startServer };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import express, { RequestHandler } from 'express';
|
|
2
|
-
export {
|
|
2
|
+
export { default as express } from 'express';
|
|
3
3
|
import { OptionsJson, OptionsText, OptionsUrlencoded } from 'body-parser';
|
|
4
4
|
|
|
5
5
|
type onErrorFunction = (req: express.Request, res: express.Response, error: any) => Promise<void>;
|
|
@@ -59,6 +59,8 @@ interface RouteType extends OrionRouteOptions {
|
|
|
59
59
|
interface RoutesMap {
|
|
60
60
|
[key: string]: RouteType;
|
|
61
61
|
}
|
|
62
|
+
type Request = express.Request;
|
|
63
|
+
type Response = express.Response;
|
|
62
64
|
|
|
63
65
|
declare function route(options: OrionRouteOptions): RouteType;
|
|
64
66
|
|
|
@@ -79,4 +81,4 @@ declare const bodyParser: {
|
|
|
79
81
|
}) => RequestHandler;
|
|
80
82
|
};
|
|
81
83
|
|
|
82
|
-
export { type OrionRouteOptions, Route, type RouteResolve, type RouteResponse, type RouteResponseObject, type RouteType, Routes, type RoutesMap, bodyParser, getApp, getServer, getServiceRoutes, getViewer, onError, registerRoute, registerRoutes, route, setGetViewer, setOnError, startServer };
|
|
84
|
+
export { type OrionRouteOptions, type Request, type Response, Route, type RouteResolve, type RouteResponse, type RouteResponseObject, type RouteType, Routes, type RoutesMap, bodyParser, getApp, getServer, getServiceRoutes, getViewer, onError, registerRoute, registerRoutes, route, setGetViewer, setOnError, startServer };
|
package/dist/index.js
CHANGED
|
@@ -84,16 +84,35 @@ function route(options) {
|
|
|
84
84
|
};
|
|
85
85
|
}
|
|
86
86
|
|
|
87
|
+
// ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/type.js
|
|
88
|
+
function type(input) {
|
|
89
|
+
if (input === null) {
|
|
90
|
+
return "Null";
|
|
91
|
+
} else if (input === void 0) {
|
|
92
|
+
return "Undefined";
|
|
93
|
+
} else if (Number.isNaN(input)) {
|
|
94
|
+
return "NaN";
|
|
95
|
+
}
|
|
96
|
+
const typeResult = Object.prototype.toString.call(input).slice(8, -1);
|
|
97
|
+
return typeResult === "AsyncFunction" ? "Promise" : typeResult;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/isNil.js
|
|
101
|
+
function isNil(x) {
|
|
102
|
+
return x === void 0 || x === null;
|
|
103
|
+
}
|
|
104
|
+
|
|
87
105
|
// src/routes/executeRequest.ts
|
|
88
|
-
import { isNil, isPlainObject, random } from "lodash";
|
|
89
106
|
import { internalGetEnv as internalGetEnv2 } from "@orion-js/env";
|
|
90
107
|
import { sleep } from "@orion-js/helpers";
|
|
91
108
|
var simulateLatency = internalGetEnv2("simulate_latency", "SIMULATE_LATENCY");
|
|
92
109
|
async function executeRequest(route2, req, res) {
|
|
93
110
|
if (simulateLatency) {
|
|
94
|
-
const time = parseInt(simulateLatency);
|
|
111
|
+
const time = Number.parseInt(simulateLatency);
|
|
95
112
|
if (time) {
|
|
96
|
-
|
|
113
|
+
const min = time * 0.9;
|
|
114
|
+
const max = time * 1.1;
|
|
115
|
+
await sleep(Math.floor(Math.random() * (max - min + 1)) + min);
|
|
97
116
|
}
|
|
98
117
|
}
|
|
99
118
|
try {
|
|
@@ -109,7 +128,7 @@ async function executeRequest(route2, req, res) {
|
|
|
109
128
|
});
|
|
110
129
|
}
|
|
111
130
|
if (!isNil(result.body)) {
|
|
112
|
-
if (
|
|
131
|
+
if (type(result.body) === "Object") {
|
|
113
132
|
res.json(result.body);
|
|
114
133
|
} else {
|
|
115
134
|
res.send(result.body);
|
|
@@ -140,17 +159,15 @@ function registerRoute(route2) {
|
|
|
140
159
|
}
|
|
141
160
|
|
|
142
161
|
// src/routes/registerRoutes.ts
|
|
143
|
-
import { values } from "lodash";
|
|
144
162
|
function registerRoutes(routesMap) {
|
|
145
|
-
const routes = values(routesMap);
|
|
163
|
+
const routes = Object.values(routesMap);
|
|
146
164
|
for (const route2 of routes) {
|
|
147
165
|
registerRoute(route2);
|
|
148
166
|
}
|
|
149
167
|
}
|
|
150
168
|
|
|
151
169
|
// src/index.ts
|
|
152
|
-
import
|
|
153
|
-
import { Request, Response } from "express";
|
|
170
|
+
import bodyParserLib from "body-parser";
|
|
154
171
|
|
|
155
172
|
// src/service/index.ts
|
|
156
173
|
import { getInstance, Service } from "@orion-js/services";
|
|
@@ -192,10 +209,9 @@ function getServiceRoutes(target) {
|
|
|
192
209
|
}
|
|
193
210
|
|
|
194
211
|
// src/index.ts
|
|
212
|
+
var { json, raw, text, urlencoded } = bodyParserLib;
|
|
195
213
|
var bodyParser2 = { json, raw, text, urlencoded };
|
|
196
214
|
export {
|
|
197
|
-
Request,
|
|
198
|
-
Response,
|
|
199
215
|
Route,
|
|
200
216
|
Routes,
|
|
201
217
|
bodyParser2 as bodyParser,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/errors.ts","../src/viewer.ts","../src/start.ts","../src/index.ts","../src/routes/route.ts","../src/routes/executeRequest.ts","../src/routes/registerRoute.ts","../src/routes/registerRoutes.ts","../src/service/index.ts"],"sourcesContent":["import express from 'express'\nimport crypto from 'crypto'\n\ntype onErrorFunction = (req: express.Request, res: express.Response, error: any) => Promise<void>\n\nconst defaultOnError: onErrorFunction = async (req, res, error) => {\n if (error.isOrionError) {\n let statusCode = 400\n if (error.code === 'AuthError') {\n statusCode = 401\n } else {\n console.warn(`[route/handler] OrionError in ${req.path}:`, error)\n }\n\n const data = error.getInfo()\n\n res.status(statusCode)\n res.json(data)\n } else if (error.isGraphQLError) {\n res.writeHead(error.statusCode)\n res.end(error.message)\n console.warn(`[route/handler] GraphQLError in ${req.path}:`, error)\n } else {\n const hash = crypto\n .createHash('sha1')\n .update(error.message, 'utf8')\n .digest('hex')\n .substring(0, 10)\n const statusCode = 500\n const data = {error: 500, message: 'Internal server error', hash}\n\n res.writeHead(statusCode)\n res.end(JSON.stringify(data, null, 2))\n\n error.hash = hash\n console.error(`[route/handler] Internal server error in ${req.url}:`, error)\n }\n}\n\nlet onErrorRef: onErrorFunction = defaultOnError\n\nexport const onError: onErrorFunction = async (req, res, error) => {\n await onErrorRef(req, res, error)\n}\n\nexport const setOnError = (onErrorFunc: onErrorFunction): void => {\n onErrorRef = onErrorFunc\n}\n","import express from 'express'\nimport {UserError} from '@orion-js/helpers'\n\nglobal.getViewerRef = () => null\n\nexport const getViewer = async (req: express.Request): Promise<any> => {\n try {\n const viewer = await global.getViewerRef(req)\n if (!viewer) return {}\n return viewer\n } catch (err) {\n throw new UserError('AuthError', err.message)\n }\n}\n\nexport const setGetViewer = (getViewerFunc: (req: express.Request) => any): void => {\n global.getViewerRef = getViewerFunc\n}\n","import {internalGetEnv} from '@orion-js/env'\nimport express from 'express'\n\nglobal.appRef = null\nglobal.serverRef = null\n\nexport interface StartOrionOptions {\n keepAliveTimeout?: number\n}\n\nexport const startServer = (\n port: number = Number(internalGetEnv('http_port', 'PORT')),\n otherOptions: StartOrionOptions = {},\n) => {\n const app = getApp()\n\n const server = app.listen(port)\n global.serverRef = server\n\n if (otherOptions.keepAliveTimeout) {\n server.keepAliveTimeout = otherOptions.keepAliveTimeout // Ensure all inactive connections are terminated by the ALB, by setting this a few seconds higher than the ALB idle timeout\n server.headersTimeout = otherOptions.keepAliveTimeout + 1000\n }\n\n return app\n}\n\nexport const getApp = (): express.Express => {\n if (global.appRef) return global.appRef as express.Express\n\n const app = express()\n\n global.appRef = app\n\n return app\n}\n\nexport const getServer = () => {\n return global.serverRef\n}\n","import {setOnError, onError} from './errors'\nimport {getViewer, setGetViewer} from './viewer'\nimport {startServer, getApp, getServer} from './start'\nimport express from 'express'\nimport route from './routes/route'\nimport registerRoute from './routes/registerRoute'\nimport registerRoutes from './routes/registerRoutes'\nimport {json, raw, text, urlencoded} from 'body-parser'\nimport {RequestHandler} from 'express'\n\nconst bodyParser: {\n json: () => RequestHandler\n raw: () => RequestHandler\n text: () => RequestHandler\n urlencoded: (options?: {extended: boolean}) => RequestHandler\n} = {json, raw, text, urlencoded}\n\nexport {\n express,\n startServer,\n getApp,\n getServer,\n getViewer,\n setGetViewer,\n setOnError,\n onError,\n route,\n registerRoute,\n registerRoutes,\n bodyParser,\n}\n\nexport {Request, Response} from 'express'\n\nexport * from './types'\nexport * from './service'\n","import {RouteType, OrionRouteOptions} from '../types'\n\nexport default function route(options: OrionRouteOptions): RouteType {\n return {\n ...options,\n }\n}\n","import {RouteType} from './../types'\nimport {onError} from '../errors'\nimport {getViewer} from './../viewer'\nimport express from 'express'\nimport {isNil, isPlainObject, random} from 'lodash'\nimport {internalGetEnv} from '@orion-js/env'\nimport {sleep} from '@orion-js/helpers'\n\nconst simulateLatency = internalGetEnv('simulate_latency', 'SIMULATE_LATENCY')\n\nexport async function executeRequest(\n route: RouteType,\n req: express.Request,\n res: express.Response,\n) {\n if (simulateLatency) {\n const time = parseInt(simulateLatency)\n if (time) {\n await sleep(random(time * 0.9, time * 1.1))\n }\n }\n\n try {\n const viewer = await getViewer(req)\n const result = await route.resolve(req, res, viewer)\n if (!result) return\n\n // add status code to response\n if (result.statusCode) {\n res.status(result.statusCode)\n }\n\n // add headers to response\n if (result.headers) {\n Object.keys(result.headers).forEach(key => {\n res.setHeader(key, result.headers[key])\n })\n }\n\n // add body to response\n if (!isNil(result.body)) {\n if (isPlainObject(result.body)) {\n res.json(result.body)\n } else {\n res.send(result.body)\n }\n }\n } catch (error) {\n await onError(req, res, error)\n }\n}\n","import {getApp} from './../start'\nimport {RouteType} from '../types'\nimport {executeRequest} from './executeRequest'\nimport express from 'express'\nimport bodyParser from 'body-parser'\n\nexport default function registerRoute(route: RouteType): void {\n const app = route.app || getApp()\n const method = route.method\n\n const handler = async (req, res) => {\n executeRequest(route, req, res)\n }\n\n const handlers: Array<express.RequestHandler> = [handler]\n\n if (route.bodyParser) {\n const parser = bodyParser[route.bodyParser](route.bodyParserOptions)\n handlers.unshift(parser)\n }\n\n if (route.middlewares) {\n handlers.unshift(...route.middlewares)\n }\n\n app[method](route.path, ...handlers)\n}\n","import {values} from 'lodash'\nimport registerRoute from './registerRoute'\nimport {RoutesMap} from '../types'\n\nexport default function registerRoutes(routesMap: RoutesMap): void {\n const routes = values(routesMap)\n\n for (const route of routes) {\n registerRoute(route)\n }\n}\n","import {getInstance, Service} from '@orion-js/services'\nimport route from '../routes/route'\nimport {OrionRouteOptions, RoutesMap} from '../types'\n\n// Define metadata storage using WeakMaps\nconst serviceMetadata = new WeakMap<any, {_serviceType: string}>()\nconst routeMetadata = new WeakMap<any, Record<string, any>>()\n\nexport function Routes() {\n return (target: any, context: ClassDecoratorContext<any>) => {\n Service()(target, context)\n\n context.addInitializer(function (this) {\n serviceMetadata.set(this, {_serviceType: 'routes'})\n })\n }\n}\n\nexport function Route<\n This,\n TArgs extends Parameters<OrionRouteOptions['resolve']>,\n TReturn extends ReturnType<OrionRouteOptions['resolve']>,\n>(options: Omit<OrionRouteOptions, 'resolve'>) {\n return (\n method: (this: This, ...args: TArgs) => TReturn,\n context: ClassMethodDecoratorContext<This, typeof method>,\n ) => {\n const propertyKey = String(context.name)\n\n context.addInitializer(function (this: This) {\n const routes = routeMetadata.get(this) || {}\n\n routes[propertyKey] = route({\n ...options,\n resolve: this[propertyKey].bind(this),\n })\n\n routeMetadata.set(this, routes)\n })\n\n return method\n }\n}\n\nexport function getServiceRoutes(target: any): RoutesMap {\n const instance = getInstance(target)\n\n if (!serviceMetadata.has(instance.constructor)) {\n throw new Error('You must pass a class decorated with @Routes to getServiceRoutes')\n }\n\n const instanceMetadata = serviceMetadata.get(instance.constructor)\n if (instanceMetadata._serviceType !== 'routes') {\n throw new Error('You must pass a class decorated with @Routes to getServiceRoutes')\n }\n\n const routesMap = routeMetadata.get(instance) || {}\n\n return routesMap\n}\n"],"mappings":";AACA,OAAO,YAAY;AAInB,IAAM,iBAAkC,OAAO,KAAK,KAAK,UAAU;AACjE,MAAI,MAAM,cAAc;AACtB,QAAI,aAAa;AACjB,QAAI,MAAM,SAAS,aAAa;AAC9B,mBAAa;AAAA,IACf,OAAO;AACL,cAAQ,KAAK,iCAAiC,IAAI,IAAI,KAAK,KAAK;AAAA,IAClE;AAEA,UAAM,OAAO,MAAM,QAAQ;AAE3B,QAAI,OAAO,UAAU;AACrB,QAAI,KAAK,IAAI;AAAA,EACf,WAAW,MAAM,gBAAgB;AAC/B,QAAI,UAAU,MAAM,UAAU;AAC9B,QAAI,IAAI,MAAM,OAAO;AACrB,YAAQ,KAAK,mCAAmC,IAAI,IAAI,KAAK,KAAK;AAAA,EACpE,OAAO;AACL,UAAM,OAAO,OACV,WAAW,MAAM,EACjB,OAAO,MAAM,SAAS,MAAM,EAC5B,OAAO,KAAK,EACZ,UAAU,GAAG,EAAE;AAClB,UAAM,aAAa;AACnB,UAAM,OAAO,EAAC,OAAO,KAAK,SAAS,yBAAyB,KAAI;AAEhE,QAAI,UAAU,UAAU;AACxB,QAAI,IAAI,KAAK,UAAU,MAAM,MAAM,CAAC,CAAC;AAErC,UAAM,OAAO;AACb,YAAQ,MAAM,4CAA4C,IAAI,GAAG,KAAK,KAAK;AAAA,EAC7E;AACF;AAEA,IAAI,aAA8B;AAE3B,IAAM,UAA2B,OAAO,KAAK,KAAK,UAAU;AACjE,QAAM,WAAW,KAAK,KAAK,KAAK;AAClC;AAEO,IAAM,aAAa,CAAC,gB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
+
{"version":3,"sources":["../src/errors.ts","../src/viewer.ts","../src/start.ts","../src/index.ts","../src/routes/route.ts","../../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/type.js","../../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/isNil.js","../src/routes/executeRequest.ts","../src/routes/registerRoute.ts","../src/routes/registerRoutes.ts","../src/service/index.ts"],"sourcesContent":["import express from 'express'\nimport crypto from 'crypto'\n\ntype onErrorFunction = (req: express.Request, res: express.Response, error: any) => Promise<void>\n\nconst defaultOnError: onErrorFunction = async (req, res, error) => {\n if (error.isOrionError) {\n let statusCode = 400\n if (error.code === 'AuthError') {\n statusCode = 401\n } else {\n console.warn(`[route/handler] OrionError in ${req.path}:`, error)\n }\n\n const data = error.getInfo()\n\n res.status(statusCode)\n res.json(data)\n } else if (error.isGraphQLError) {\n res.writeHead(error.statusCode)\n res.end(error.message)\n console.warn(`[route/handler] GraphQLError in ${req.path}:`, error)\n } else {\n const hash = crypto\n .createHash('sha1')\n .update(error.message, 'utf8')\n .digest('hex')\n .substring(0, 10)\n const statusCode = 500\n const data = {error: 500, message: 'Internal server error', hash}\n\n res.writeHead(statusCode)\n res.end(JSON.stringify(data, null, 2))\n\n error.hash = hash\n console.error(`[route/handler] Internal server error in ${req.url}:`, error)\n }\n}\n\nlet onErrorRef: onErrorFunction = defaultOnError\n\nexport const onError: onErrorFunction = async (req, res, error) => {\n await onErrorRef(req, res, error)\n}\n\nexport const setOnError = (onErrorFunc: onErrorFunction): void => {\n onErrorRef = onErrorFunc\n}\n","import express from 'express'\nimport {UserError} from '@orion-js/helpers'\n\nglobal.getViewerRef = () => null\n\nexport const getViewer = async (req: express.Request): Promise<any> => {\n try {\n const viewer = await global.getViewerRef(req)\n if (!viewer) return {}\n return viewer\n } catch (err) {\n throw new UserError('AuthError', err.message)\n }\n}\n\nexport const setGetViewer = (getViewerFunc: (req: express.Request) => any): void => {\n global.getViewerRef = getViewerFunc\n}\n","import {internalGetEnv} from '@orion-js/env'\nimport express from 'express'\n\nglobal.appRef = null\nglobal.serverRef = null\n\nexport interface StartOrionOptions {\n keepAliveTimeout?: number\n}\n\nexport const startServer = (\n port: number = Number(internalGetEnv('http_port', 'PORT')),\n otherOptions: StartOrionOptions = {},\n) => {\n const app = getApp()\n\n const server = app.listen(port)\n global.serverRef = server\n\n if (otherOptions.keepAliveTimeout) {\n server.keepAliveTimeout = otherOptions.keepAliveTimeout // Ensure all inactive connections are terminated by the ALB, by setting this a few seconds higher than the ALB idle timeout\n server.headersTimeout = otherOptions.keepAliveTimeout + 1000\n }\n\n return app\n}\n\nexport const getApp = (): express.Express => {\n if (global.appRef) return global.appRef as express.Express\n\n const app = express()\n\n global.appRef = app\n\n return app\n}\n\nexport const getServer = () => {\n return global.serverRef\n}\n","import {setOnError, onError} from './errors'\nimport {getViewer, setGetViewer} from './viewer'\nimport {startServer, getApp, getServer} from './start'\nimport express from 'express'\nimport route from './routes/route'\nimport registerRoute from './routes/registerRoute'\nimport registerRoutes from './routes/registerRoutes'\nimport bodyParserLib from 'body-parser'\nimport {RequestHandler} from 'express'\n\nconst {json, raw, text, urlencoded} = bodyParserLib\n\nconst bodyParser: {\n json: () => RequestHandler\n raw: () => RequestHandler\n text: () => RequestHandler\n urlencoded: (options?: {extended: boolean}) => RequestHandler\n} = {json, raw, text, urlencoded}\n\nexport {\n express,\n startServer,\n getApp,\n getServer,\n getViewer,\n setGetViewer,\n setOnError,\n onError,\n route,\n registerRoute,\n registerRoutes,\n bodyParser,\n}\n\nexport * from './types'\nexport * from './service'\n","import {RouteType, OrionRouteOptions} from '../types'\n\nexport default function route(options: OrionRouteOptions): RouteType {\n return {\n ...options,\n }\n}\n","export function type(input){\n if (input === null){\n return 'Null'\n } else if (input === undefined){\n return 'Undefined'\n } else if (Number.isNaN(input)){\n return 'NaN'\n }\n const typeResult = Object.prototype.toString.call(input).slice(8, -1)\n\n return typeResult === 'AsyncFunction' ? 'Promise' : typeResult\n}\n","export function isNil(x){\n return x === undefined || x === null\n}\n","import {RouteType} from './../types'\nimport {onError} from '../errors'\nimport {getViewer} from './../viewer'\nimport express from 'express'\nimport {isNil, type} from 'rambdax'\nimport {internalGetEnv} from '@orion-js/env'\nimport {sleep} from '@orion-js/helpers'\n\nconst simulateLatency = internalGetEnv('simulate_latency', 'SIMULATE_LATENCY')\n\nexport async function executeRequest(\n route: RouteType,\n req: express.Request,\n res: express.Response,\n) {\n if (simulateLatency) {\n const time = Number.parseInt(simulateLatency)\n if (time) {\n const min = time * 0.9\n const max = time * 1.1\n await sleep(Math.floor(Math.random() * (max - min + 1)) + min)\n }\n }\n\n try {\n const viewer = await getViewer(req)\n const result = await route.resolve(req, res, viewer)\n if (!result) return\n\n // add status code to response\n if (result.statusCode) {\n res.status(result.statusCode)\n }\n\n // add headers to response\n if (result.headers) {\n Object.keys(result.headers).forEach(key => {\n res.setHeader(key, result.headers[key])\n })\n }\n\n // add body to response\n if (!isNil(result.body)) {\n if (type(result.body) === 'Object') {\n res.json(result.body)\n } else {\n res.send(result.body)\n }\n }\n } catch (error) {\n await onError(req, res, error)\n }\n}\n","import {getApp} from './../start'\nimport {RouteType} from '../types'\nimport {executeRequest} from './executeRequest'\nimport express from 'express'\nimport bodyParser from 'body-parser'\n\nexport default function registerRoute(route: RouteType): void {\n const app = route.app || getApp()\n const method = route.method\n\n const handler = async (req, res) => {\n executeRequest(route, req, res)\n }\n\n const handlers: Array<express.RequestHandler> = [handler]\n\n if (route.bodyParser) {\n const parser = bodyParser[route.bodyParser](route.bodyParserOptions)\n handlers.unshift(parser)\n }\n\n if (route.middlewares) {\n handlers.unshift(...route.middlewares)\n }\n\n app[method](route.path, ...handlers)\n}\n","import registerRoute from './registerRoute'\nimport {RoutesMap} from '../types'\n\nexport default function registerRoutes(routesMap: RoutesMap): void {\n const routes = Object.values(routesMap)\n\n for (const route of routes) {\n registerRoute(route)\n }\n}\n","import {getInstance, Service} from '@orion-js/services'\nimport route from '../routes/route'\nimport {OrionRouteOptions, RoutesMap} from '../types'\n\n// Define metadata storage using WeakMaps\nconst serviceMetadata = new WeakMap<any, {_serviceType: string}>()\nconst routeMetadata = new WeakMap<any, Record<string, any>>()\n\nexport function Routes() {\n return (target: any, context: ClassDecoratorContext<any>) => {\n Service()(target, context)\n\n context.addInitializer(function (this) {\n serviceMetadata.set(this, {_serviceType: 'routes'})\n })\n }\n}\n\nexport function Route<\n This,\n TArgs extends Parameters<OrionRouteOptions['resolve']>,\n TReturn extends ReturnType<OrionRouteOptions['resolve']>,\n>(options: Omit<OrionRouteOptions, 'resolve'>) {\n return (\n method: (this: This, ...args: TArgs) => TReturn,\n context: ClassMethodDecoratorContext<This, typeof method>,\n ) => {\n const propertyKey = String(context.name)\n\n context.addInitializer(function (this: This) {\n const routes = routeMetadata.get(this) || {}\n\n routes[propertyKey] = route({\n ...options,\n resolve: this[propertyKey].bind(this),\n })\n\n routeMetadata.set(this, routes)\n })\n\n return method\n }\n}\n\nexport function getServiceRoutes(target: any): RoutesMap {\n const instance = getInstance(target)\n\n if (!serviceMetadata.has(instance.constructor)) {\n throw new Error('You must pass a class decorated with @Routes to getServiceRoutes')\n }\n\n const instanceMetadata = serviceMetadata.get(instance.constructor)\n if (instanceMetadata._serviceType !== 'routes') {\n throw new Error('You must pass a class decorated with @Routes to getServiceRoutes')\n }\n\n const routesMap = routeMetadata.get(instance) || {}\n\n return routesMap\n}\n"],"mappings":";AACA,OAAO,YAAY;AAInB,IAAM,iBAAkC,OAAO,KAAK,KAAK,UAAU;AACjE,MAAI,MAAM,cAAc;AACtB,QAAI,aAAa;AACjB,QAAI,MAAM,SAAS,aAAa;AAC9B,mBAAa;AAAA,IACf,OAAO;AACL,cAAQ,KAAK,iCAAiC,IAAI,IAAI,KAAK,KAAK;AAAA,IAClE;AAEA,UAAM,OAAO,MAAM,QAAQ;AAE3B,QAAI,OAAO,UAAU;AACrB,QAAI,KAAK,IAAI;AAAA,EACf,WAAW,MAAM,gBAAgB;AAC/B,QAAI,UAAU,MAA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,OAAO;AAEzB,YAAQ,eAAe,WAAgB;AACrC,sBAAgB,IAAI,MAAM,EAAC,cAAc,SAAQ,CAAC;AAAA,IACpD,CAAC;AAAA,EACH;AACF;AAEO,SAAS,MAId,SAA6C;AAC7C,SAAO,CACL,QACA,YACG;AACH,UAAM,cAAc,OAAO,QAAQ,IAAI;AAEvC,YAAQ,eAAe,WAAsB;AAC3C,YAAM,SAAS,cAAc,IAAI,IAAI,KAAK,CAAC;AAE3C,aAAO,WAAW,IAAI,MAAM;AAAA,QAC1B,GAAG;AAAA,QACH,SAAS,KAAK,WAAW,EAAE,KAAK,IAAI;AAAA,MACtC,CAAC;AAED,oBAAc,IAAI,MAAM,MAAM;AAAA,IAChC,CAAC;AAED,WAAO;AAAA,EACT;AACF;AAEO,SAAS,iBAAiB,QAAwB;AACvD,QAAM,WAAW,YAAY,MAAM;AAEnC,MAAI,CAAC,gBAAgB,IAAI,SAAS,WAAW,GAAG;AAC9C,UAAM,IAAI,MAAM,kEAAkE;AAAA,EACpF;AAEA,QAAM,mBAAmB,gBAAgB,IAAI,SAAS,WAAW;AACjE,MAAI,iBAAiB,iBAAiB,UAAU;AAC9C,UAAM,IAAI,MAAM,kEAAkE;AAAA,EACpF;AAEA,QAAM,YAAY,cAAc,IAAI,QAAQ,KAAK,CAAC;AAElD,SAAO;AACT;;;APjDA,IAAM,EAAC,MAAM,KAAK,MAAM,WAAU,IAAI;AAEtC,IAAMC,cAKF,EAAC,MAAM,KAAK,MAAM,WAAU;","names":["express","internalGetEnv","route","route","route","bodyParser"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orion-js/http",
|
|
3
|
-
"version": "4.0.0-next.
|
|
3
|
+
"version": "4.0.0-next.6",
|
|
4
4
|
"main": "./dist/index.cjs",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -9,26 +9,24 @@
|
|
|
9
9
|
"author": "nicolaslopezj",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"dependencies": {
|
|
12
|
-
"body-parser": "1.
|
|
13
|
-
"express": "4.
|
|
14
|
-
"
|
|
15
|
-
"@orion-js/
|
|
16
|
-
"@orion-js/helpers": "4.0.0-next.
|
|
17
|
-
"@orion-js/
|
|
18
|
-
"@orion-js/
|
|
19
|
-
"@orion-js/services": "4.0.0-next.4"
|
|
12
|
+
"body-parser": "1.20.3",
|
|
13
|
+
"express": "4.21.2",
|
|
14
|
+
"@orion-js/env": "4.0.0-next.6",
|
|
15
|
+
"@orion-js/schema": "4.0.0-next.6",
|
|
16
|
+
"@orion-js/helpers": "4.0.0-next.6",
|
|
17
|
+
"@orion-js/services": "4.0.0-next.6",
|
|
18
|
+
"@orion-js/resolvers": "4.0.0-next.6"
|
|
20
19
|
},
|
|
21
20
|
"devDependencies": {
|
|
22
21
|
"@types/body-parser": "^1.19.1",
|
|
23
|
-
"@types/express": "4.17.
|
|
24
|
-
"@types/
|
|
25
|
-
"@types/
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"supertest": "^6.1.6",
|
|
22
|
+
"@types/express": "4.17.21",
|
|
23
|
+
"@types/node": "^22.13.10",
|
|
24
|
+
"@types/supertest": "6.0.2",
|
|
25
|
+
"superagent": "^10.2.0",
|
|
26
|
+
"supertest": "^7.0.0",
|
|
29
27
|
"tsup": "^8.0.1",
|
|
30
28
|
"typescript": "^5.4.5",
|
|
31
|
-
"vitest": "^
|
|
29
|
+
"vitest": "^3.0.8"
|
|
32
30
|
},
|
|
33
31
|
"publishConfig": {
|
|
34
32
|
"access": "public"
|