@orion-js/http 4.2.1 → 4.2.3
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 +41 -46
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +41 -46
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.cjs
CHANGED
|
@@ -70,15 +70,15 @@ var defaultOnError = async (req, res, error) => {
|
|
|
70
70
|
const hash = import_node_crypto.default.createHash("sha1").update(error.message, "utf8").digest("hex").substring(0, 10);
|
|
71
71
|
const statusCode = 500;
|
|
72
72
|
const data = { error: 500, message: "Internal server error", hash };
|
|
73
|
-
res.
|
|
74
|
-
res.
|
|
73
|
+
res.status(statusCode);
|
|
74
|
+
res.json(data);
|
|
75
75
|
error.hash = hash;
|
|
76
76
|
import_logger.logger.error("[route/handler] Internal server error", { error, url: req.url, hash });
|
|
77
77
|
}
|
|
78
78
|
};
|
|
79
79
|
var onErrorRef = defaultOnError;
|
|
80
80
|
var onError = async (req, res, error) => {
|
|
81
|
-
|
|
81
|
+
return onErrorRef(req, res, error);
|
|
82
82
|
};
|
|
83
83
|
var setOnError = (onErrorFunc) => {
|
|
84
84
|
onErrorRef = onErrorFunc;
|
|
@@ -128,48 +128,35 @@ var getServer = () => {
|
|
|
128
128
|
// src/index.ts
|
|
129
129
|
var import_express2 = __toESM(require("express"), 1);
|
|
130
130
|
|
|
131
|
-
// src/routes/
|
|
132
|
-
var
|
|
133
|
-
|
|
134
|
-
// ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/type.js
|
|
135
|
-
function type(input) {
|
|
136
|
-
if (input === null) {
|
|
137
|
-
return "Null";
|
|
138
|
-
} else if (input === void 0) {
|
|
139
|
-
return "Undefined";
|
|
140
|
-
} else if (Number.isNaN(input)) {
|
|
141
|
-
return "NaN";
|
|
142
|
-
}
|
|
143
|
-
const typeResult = Object.prototype.toString.call(input).slice(8, -1);
|
|
144
|
-
return typeResult === "AsyncFunction" ? "Promise" : typeResult;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
// ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/isNil.js
|
|
148
|
-
function isNil(x) {
|
|
149
|
-
return x === void 0 || x === null;
|
|
150
|
-
}
|
|
131
|
+
// src/routes/registerRoute.ts
|
|
132
|
+
var import_body_parser = __toESM(require("body-parser"), 1);
|
|
151
133
|
|
|
152
134
|
// src/routes/executeRequest.ts
|
|
153
135
|
var import_env2 = require("@orion-js/env");
|
|
154
136
|
var import_helpers2 = require("@orion-js/helpers");
|
|
137
|
+
var import_logger2 = require("@orion-js/logger");
|
|
155
138
|
var import_schema = require("@orion-js/schema");
|
|
156
139
|
var simulateLatency = (0, import_env2.internalGetEnv)("simulate_latency", "SIMULATE_LATENCY");
|
|
140
|
+
var simulateLatencyMs = Number.parseInt(simulateLatency, 10);
|
|
141
|
+
var hasSimulateLatency = Number.isFinite(simulateLatencyMs) && simulateLatencyMs > 0;
|
|
142
|
+
var latencyMinMs = hasSimulateLatency ? Math.floor(simulateLatencyMs * 0.9) : 0;
|
|
143
|
+
var latencyMaxMs = hasSimulateLatency ? Math.ceil(simulateLatencyMs * 1.1) : 0;
|
|
144
|
+
var objectToString = Object.prototype.toString;
|
|
145
|
+
function isPlainObject(value) {
|
|
146
|
+
return objectToString.call(value) === "[object Object]";
|
|
147
|
+
}
|
|
157
148
|
async function executeRequest(route2, req, res) {
|
|
158
|
-
if (simulateLatency) {
|
|
159
|
-
const time = Number.parseInt(simulateLatency);
|
|
160
|
-
if (time) {
|
|
161
|
-
const min = time * 0.9;
|
|
162
|
-
const max = time * 1.1;
|
|
163
|
-
await (0, import_helpers2.sleep)(Math.floor(Math.random() * (max - min + 1)) + min);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
149
|
try {
|
|
150
|
+
if (hasSimulateLatency) {
|
|
151
|
+
const delay = Math.floor(Math.random() * (latencyMaxMs - latencyMinMs + 1)) + latencyMinMs;
|
|
152
|
+
await (0, import_helpers2.sleep)(delay);
|
|
153
|
+
}
|
|
167
154
|
const viewer = await getViewer(req);
|
|
168
155
|
if (route2.queryParams) {
|
|
169
|
-
req.query = await (0, import_schema.cleanAndValidate)(route2.queryParams
|
|
156
|
+
req.query = await (0, import_schema.cleanAndValidate)(route2.queryParams, req.query);
|
|
170
157
|
}
|
|
171
158
|
if (route2.bodyParams) {
|
|
172
|
-
req.body = await (0, import_schema.cleanAndValidate)(route2.bodyParams
|
|
159
|
+
req.body = await (0, import_schema.cleanAndValidate)(route2.bodyParams, req.body);
|
|
173
160
|
}
|
|
174
161
|
const context = {
|
|
175
162
|
controllerType: "route",
|
|
@@ -182,25 +169,23 @@ async function executeRequest(route2, req, res) {
|
|
|
182
169
|
params: req.params
|
|
183
170
|
}
|
|
184
171
|
};
|
|
185
|
-
const result = await (0, import_logger2.runWithOrionAsyncContext)(context,
|
|
172
|
+
const result = await (0, import_logger2.runWithOrionAsyncContext)(context, () => {
|
|
186
173
|
(0, import_logger2.updateOrionAsyncContext)({ viewer });
|
|
187
|
-
return
|
|
174
|
+
return route2.resolve(req, res, viewer);
|
|
188
175
|
});
|
|
189
176
|
if (!result) return;
|
|
190
177
|
if (result.statusCode) {
|
|
191
178
|
res.status(result.statusCode);
|
|
192
179
|
}
|
|
193
180
|
if (result.headers) {
|
|
194
|
-
|
|
195
|
-
res.setHeader(key, result.headers[key]);
|
|
196
|
-
}
|
|
181
|
+
res.set(result.headers);
|
|
197
182
|
}
|
|
198
|
-
if (
|
|
183
|
+
if (result.body !== null && result.body !== void 0) {
|
|
199
184
|
let body = result.body;
|
|
200
185
|
if (route2.returns) {
|
|
201
186
|
body = await (0, import_schema.cleanAndValidate)(route2.returns, result.body);
|
|
202
187
|
}
|
|
203
|
-
if (
|
|
188
|
+
if (isPlainObject(body)) {
|
|
204
189
|
res.json(body);
|
|
205
190
|
} else {
|
|
206
191
|
res.send(body);
|
|
@@ -212,12 +197,22 @@ async function executeRequest(route2, req, res) {
|
|
|
212
197
|
}
|
|
213
198
|
|
|
214
199
|
// src/routes/registerRoute.ts
|
|
215
|
-
var
|
|
200
|
+
var appRegisteredRoutes = /* @__PURE__ */ new WeakMap();
|
|
201
|
+
function getRegisteredRoutes(app) {
|
|
202
|
+
if (appRegisteredRoutes.has(app)) {
|
|
203
|
+
return appRegisteredRoutes.get(app);
|
|
204
|
+
}
|
|
205
|
+
const routes = /* @__PURE__ */ new WeakSet();
|
|
206
|
+
appRegisteredRoutes.set(app, routes);
|
|
207
|
+
return routes;
|
|
208
|
+
}
|
|
216
209
|
function registerRoute(route2) {
|
|
217
210
|
const app = route2.app || getApp();
|
|
211
|
+
const registeredRoutes = getRegisteredRoutes(app);
|
|
212
|
+
if (registeredRoutes.has(route2)) return;
|
|
218
213
|
const method = route2.method;
|
|
219
|
-
const handler =
|
|
220
|
-
executeRequest(route2, req, res);
|
|
214
|
+
const handler = (req, res) => {
|
|
215
|
+
void executeRequest(route2, req, res);
|
|
221
216
|
};
|
|
222
217
|
const handlers = [handler];
|
|
223
218
|
if (route2.bodyParser) {
|
|
@@ -232,13 +227,13 @@ function registerRoute(route2) {
|
|
|
232
227
|
handlers.unshift(...route2.middlewares);
|
|
233
228
|
}
|
|
234
229
|
app[method](route2.path, ...handlers);
|
|
230
|
+
registeredRoutes.add(route2);
|
|
235
231
|
}
|
|
236
232
|
|
|
237
233
|
// src/routes/registerRoutes.ts
|
|
238
234
|
function registerRoutes(routesMap) {
|
|
239
|
-
const
|
|
240
|
-
|
|
241
|
-
registerRoute(route2);
|
|
235
|
+
for (const routeName in routesMap) {
|
|
236
|
+
registerRoute(routesMap[routeName]);
|
|
242
237
|
}
|
|
243
238
|
}
|
|
244
239
|
|
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/executeRequest.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/registerRoute.ts","../src/routes/registerRoutes.ts","../src/routes/route.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 registerRoute from './routes/registerRoute'\nimport registerRoutes from './routes/registerRoutes'\nimport bodyParserLib from 'body-parser'\nimport {RequestHandler} from 'express'\n\nexport * from './routes/route'\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 registerRoute,\n registerRoutes,\n bodyParser,\n}\n\nexport * from './types'\nexport * from './service'\n","import express from 'express'\nimport crypto from 'node:crypto'\nimport {logger} from '@orion-js/logger'\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 logger.error(`[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 logger.error(`[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 logger.error('[route/handler] Internal server error', {error, url: req.url, hash})\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 {OrionRequest, RouteType} from './../types'\nimport {getViewer} from './../viewer'\nimport {runWithOrionAsyncContext, updateOrionAsyncContext} from '@orion-js/logger'\nimport express from 'express'\nimport {isNil, type} from 'rambdax'\nimport {internalGetEnv} from '@orion-js/env'\nimport {sleep} from '@orion-js/helpers'\nimport {cleanAndValidate} from '@orion-js/schema'\nimport {onError} from '../errors'\nconst simulateLatency = internalGetEnv('simulate_latency', 'SIMULATE_LATENCY')\n\nexport async function executeRequest(\n route: RouteType<any>,\n req: OrionRequest,\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\n if (route.queryParams) {\n req.query = await cleanAndValidate(route.queryParams ?? {}, req.query)\n }\n\n if (route.bodyParams) {\n req.body = await cleanAndValidate(route.bodyParams ?? {}, req.body)\n }\n\n const context = {\n controllerType: 'route' as const,\n routeName: route.path,\n pathname: req.path,\n viewer,\n params: {\n body: req.body,\n query: req.query,\n params: req.params,\n },\n }\n\n const result = await runWithOrionAsyncContext(context, async () => {\n updateOrionAsyncContext({viewer})\n return await route.resolve(req, res, viewer)\n })\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 for (const key in result.headers) {\n res.setHeader(key, result.headers[key])\n }\n }\n\n // add body to response\n if (!isNil(result.body)) {\n let body = result.body\n\n if (route.returns) {\n body = await cleanAndValidate(route.returns, result.body)\n }\n\n if (type(body) === 'Object') {\n res.json(body)\n } else {\n res.send(body)\n }\n }\n } catch (error) {\n await onError(req, res, error)\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 {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.bodyParser && route.bodyParams) {\n const parser = bodyParser.json(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 {Schema, SchemaFieldType} from '@orion-js/schema'\nimport {RouteType, OrionRouteOptions} from '../types'\n\nexport function createRoute<\n TPath extends string,\n TQueryParamsSchema extends Schema | undefined,\n TBodyParamsSchema extends Schema | undefined,\n TReturnsSchema extends SchemaFieldType | undefined,\n>(\n options: OrionRouteOptions<TPath, TQueryParamsSchema, TBodyParamsSchema, TReturnsSchema>,\n): RouteType<TPath, TQueryParamsSchema, TBodyParamsSchema, TReturnsSchema> {\n return {\n ...options,\n }\n}\n\n/**\n * @deprecated Use createRoute instead\n */\nexport const route = createRoute\n","import {getInstance, Service} from '@orion-js/services'\nimport {createRoute} 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(): (method: any, context: ClassFieldDecoratorContext) => any\nexport function Route(\n options?: Omit<OrionRouteOptions<any, any, any, any>, 'resolve'>,\n): (method: any, context: ClassMethodDecoratorContext) => any\nexport function Route(options?: Omit<OrionRouteOptions<any, any, any, any>, 'resolve'>) {\n return (method: any, context: ClassFieldDecoratorContext | ClassMethodDecoratorContext) => {\n const propertyKey = String(context.name)\n\n context.addInitializer(function (this) {\n const routes = routeMetadata.get(this) || {}\n\n if (context.kind === 'method') {\n routes[propertyKey] = createRoute({\n ...options,\n resolve: this[propertyKey].bind(this),\n })\n }\n\n if (context.kind === 'field') {\n routes[propertyKey] = this[propertyKey]\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;AAAA,iCAAAC;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,yBAAmB;AACnB,oBAAqB;AAIrB,IAAM,iBAAkC,OAAO,KAAK,KAAK,UAAU;AACjE,MAAI,MAAM,cAAc;AACtB,QAAI,aAAa;AACjB,QAAI,MAAM,SAAS,aAAa;AAC9B,mBAAa;AAAA,IACf,OAAO;AACL,2BAAO,MAAM,iCAAiC,IAAI,IAAI,KAAK,EAAC,MAAK,CAAC;AAAA,IACpE;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,yBAAO,MAAM,mCAAmC,IAAI,IAAI,KAAK,EAAC,MAAK,CAAC;AAAA,EACtE,OAAO;AACL,UAAM,OAAO,mBAAAC,QACV,WAAW,MAAM,EACjB,OAAO,MAAM,SAAS,MAAM,EAC5B,OAAO,KAAK,EACZ,UAAU,GAAG,EAAE;AAClB,UAAM,a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BAAgB,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;;;AVnDA,IAAM,EAAC,MAAM,KAAK,MAAM,WAAU,IAAI,oBAAAC;AAEtC,IAAMC,cAKF,EAAC,MAAM,KAAK,MAAM,WAAU;","names":["bodyParser","express","crypto","express","import_express","import_logger","import_env","import_helpers","route","route","bodyParser","route","import_body_parser","bodyParserLib","bodyParser"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/errors.ts","../src/viewer.ts","../src/start.ts","../src/routes/registerRoute.ts","../src/routes/executeRequest.ts","../src/routes/registerRoutes.ts","../src/routes/route.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 registerRoute from './routes/registerRoute'\nimport registerRoutes from './routes/registerRoutes'\nimport bodyParserLib from 'body-parser'\nimport {RequestHandler} from 'express'\n\nexport * from './routes/route'\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 registerRoute,\n registerRoutes,\n bodyParser,\n}\n\nexport * from './types'\nexport * from './service'\n","import crypto from 'node:crypto'\nimport {logger} from '@orion-js/logger'\nimport express from 'express'\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 logger.error(`[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 logger.error(`[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.status(statusCode)\n res.json(data)\n\n error.hash = hash\n logger.error('[route/handler] Internal server error', {error, url: req.url, hash})\n }\n}\n\nlet onErrorRef: onErrorFunction = defaultOnError\n\nexport const onError: onErrorFunction = async (req, res, error) => {\n return 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 bodyParser from 'body-parser'\nimport express from 'express'\nimport {getApp} from './../start'\nimport {RouteType} from '../types'\nimport {executeRequest} from './executeRequest'\n\nconst appRegisteredRoutes = new WeakMap<express.Application, WeakSet<RouteType>>()\n\nfunction getRegisteredRoutes(app: express.Application) {\n if (appRegisteredRoutes.has(app)) {\n return appRegisteredRoutes.get(app)\n }\n\n const routes = new WeakSet<RouteType>()\n appRegisteredRoutes.set(app, routes)\n return routes\n}\n\nexport default function registerRoute(route: RouteType): void {\n const app = route.app || getApp()\n const registeredRoutes = getRegisteredRoutes(app)\n if (registeredRoutes.has(route)) return\n\n const method = route.method\n\n const handler: express.RequestHandler = (req, res) => {\n void executeRequest(route, req as any, 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.bodyParser && route.bodyParams) {\n const parser = bodyParser.json(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 registeredRoutes.add(route)\n}\n","import {internalGetEnv} from '@orion-js/env'\nimport {sleep} from '@orion-js/helpers'\nimport {runWithOrionAsyncContext, updateOrionAsyncContext} from '@orion-js/logger'\nimport {cleanAndValidate} from '@orion-js/schema'\nimport express from 'express'\nimport {onError} from '../errors'\nimport {OrionRequest, RouteType} from './../types'\nimport {getViewer} from './../viewer'\n\nconst simulateLatency = internalGetEnv('simulate_latency', 'SIMULATE_LATENCY')\nconst simulateLatencyMs = Number.parseInt(simulateLatency, 10)\nconst hasSimulateLatency = Number.isFinite(simulateLatencyMs) && simulateLatencyMs > 0\nconst latencyMinMs = hasSimulateLatency ? Math.floor(simulateLatencyMs * 0.9) : 0\nconst latencyMaxMs = hasSimulateLatency ? Math.ceil(simulateLatencyMs * 1.1) : 0\n\nconst objectToString = Object.prototype.toString\nfunction isPlainObject(value: unknown): value is Record<string, unknown> {\n return objectToString.call(value) === '[object Object]'\n}\n\nexport async function executeRequest(\n route: RouteType<any>,\n req: OrionRequest,\n res: express.Response,\n) {\n try {\n if (hasSimulateLatency) {\n const delay = Math.floor(Math.random() * (latencyMaxMs - latencyMinMs + 1)) + latencyMinMs\n await sleep(delay)\n }\n\n const viewer = await getViewer(req)\n\n if (route.queryParams) {\n req.query = await cleanAndValidate(route.queryParams, req.query)\n }\n\n if (route.bodyParams) {\n req.body = await cleanAndValidate(route.bodyParams, req.body)\n }\n\n const context = {\n controllerType: 'route' as const,\n routeName: route.path,\n pathname: req.path,\n viewer,\n params: {\n body: req.body,\n query: req.query,\n params: req.params,\n },\n }\n\n const result = await runWithOrionAsyncContext(context, () => {\n updateOrionAsyncContext({viewer})\n return route.resolve(req, res, viewer)\n })\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 res.set(result.headers)\n }\n\n // add body to response\n if (result.body !== null && result.body !== undefined) {\n let body = result.body\n\n if (route.returns) {\n body = await cleanAndValidate(route.returns, result.body)\n }\n\n if (isPlainObject(body)) {\n res.json(body)\n } else {\n res.send(body)\n }\n }\n } catch (error) {\n await onError(req, res, error)\n }\n}\n","import {RoutesMap} from '../types'\nimport registerRoute from './registerRoute'\n\nexport default function registerRoutes(routesMap: RoutesMap): void {\n for (const routeName in routesMap) {\n registerRoute(routesMap[routeName])\n }\n}\n","import {Schema, SchemaFieldType} from '@orion-js/schema'\nimport {RouteType, OrionRouteOptions} from '../types'\n\nexport function createRoute<\n TPath extends string,\n TQueryParamsSchema extends Schema | undefined,\n TBodyParamsSchema extends Schema | undefined,\n TReturnsSchema extends SchemaFieldType | undefined,\n>(\n options: OrionRouteOptions<TPath, TQueryParamsSchema, TBodyParamsSchema, TReturnsSchema>,\n): RouteType<TPath, TQueryParamsSchema, TBodyParamsSchema, TReturnsSchema> {\n return {\n ...options,\n }\n}\n\n/**\n * @deprecated Use createRoute instead\n */\nexport const route = createRoute\n","import {getInstance, Service} from '@orion-js/services'\nimport {createRoute} 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(): (method: any, context: ClassFieldDecoratorContext) => any\nexport function Route(\n options?: Omit<OrionRouteOptions<any, any, any, any>, 'resolve'>,\n): (method: any, context: ClassMethodDecoratorContext) => any\nexport function Route(options?: Omit<OrionRouteOptions<any, any, any, any>, 'resolve'>) {\n return (method: any, context: ClassFieldDecoratorContext | ClassMethodDecoratorContext) => {\n const propertyKey = String(context.name)\n\n context.addInitializer(function (this) {\n const routes = routeMetadata.get(this) || {}\n\n if (context.kind === 'method') {\n routes[propertyKey] = createRoute({\n ...options,\n resolve: this[propertyKey].bind(this),\n })\n }\n\n if (context.kind === 'field') {\n routes[propertyKey] = this[propertyKey]\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,oBAA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","express","import_express","import_env","import_helpers","import_logger","route","route","bodyParser","import_body_parser","bodyParserLib","bodyParser"]}
|
package/dist/index.js
CHANGED
|
@@ -20,15 +20,15 @@ var defaultOnError = async (req, res, error) => {
|
|
|
20
20
|
const hash = crypto.createHash("sha1").update(error.message, "utf8").digest("hex").substring(0, 10);
|
|
21
21
|
const statusCode = 500;
|
|
22
22
|
const data = { error: 500, message: "Internal server error", hash };
|
|
23
|
-
res.
|
|
24
|
-
res.
|
|
23
|
+
res.status(statusCode);
|
|
24
|
+
res.json(data);
|
|
25
25
|
error.hash = hash;
|
|
26
26
|
logger.error("[route/handler] Internal server error", { error, url: req.url, hash });
|
|
27
27
|
}
|
|
28
28
|
};
|
|
29
29
|
var onErrorRef = defaultOnError;
|
|
30
30
|
var onError = async (req, res, error) => {
|
|
31
|
-
|
|
31
|
+
return onErrorRef(req, res, error);
|
|
32
32
|
};
|
|
33
33
|
var setOnError = (onErrorFunc) => {
|
|
34
34
|
onErrorRef = onErrorFunc;
|
|
@@ -78,48 +78,35 @@ var getServer = () => {
|
|
|
78
78
|
// src/index.ts
|
|
79
79
|
import express2 from "express";
|
|
80
80
|
|
|
81
|
-
// src/routes/
|
|
82
|
-
import
|
|
83
|
-
|
|
84
|
-
// ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/type.js
|
|
85
|
-
function type(input) {
|
|
86
|
-
if (input === null) {
|
|
87
|
-
return "Null";
|
|
88
|
-
} else if (input === void 0) {
|
|
89
|
-
return "Undefined";
|
|
90
|
-
} else if (Number.isNaN(input)) {
|
|
91
|
-
return "NaN";
|
|
92
|
-
}
|
|
93
|
-
const typeResult = Object.prototype.toString.call(input).slice(8, -1);
|
|
94
|
-
return typeResult === "AsyncFunction" ? "Promise" : typeResult;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/isNil.js
|
|
98
|
-
function isNil(x) {
|
|
99
|
-
return x === void 0 || x === null;
|
|
100
|
-
}
|
|
81
|
+
// src/routes/registerRoute.ts
|
|
82
|
+
import bodyParser from "body-parser";
|
|
101
83
|
|
|
102
84
|
// src/routes/executeRequest.ts
|
|
103
85
|
import { internalGetEnv as internalGetEnv2 } from "@orion-js/env";
|
|
104
86
|
import { sleep } from "@orion-js/helpers";
|
|
87
|
+
import { runWithOrionAsyncContext, updateOrionAsyncContext } from "@orion-js/logger";
|
|
105
88
|
import { cleanAndValidate } from "@orion-js/schema";
|
|
106
89
|
var simulateLatency = internalGetEnv2("simulate_latency", "SIMULATE_LATENCY");
|
|
90
|
+
var simulateLatencyMs = Number.parseInt(simulateLatency, 10);
|
|
91
|
+
var hasSimulateLatency = Number.isFinite(simulateLatencyMs) && simulateLatencyMs > 0;
|
|
92
|
+
var latencyMinMs = hasSimulateLatency ? Math.floor(simulateLatencyMs * 0.9) : 0;
|
|
93
|
+
var latencyMaxMs = hasSimulateLatency ? Math.ceil(simulateLatencyMs * 1.1) : 0;
|
|
94
|
+
var objectToString = Object.prototype.toString;
|
|
95
|
+
function isPlainObject(value) {
|
|
96
|
+
return objectToString.call(value) === "[object Object]";
|
|
97
|
+
}
|
|
107
98
|
async function executeRequest(route2, req, res) {
|
|
108
|
-
if (simulateLatency) {
|
|
109
|
-
const time = Number.parseInt(simulateLatency);
|
|
110
|
-
if (time) {
|
|
111
|
-
const min = time * 0.9;
|
|
112
|
-
const max = time * 1.1;
|
|
113
|
-
await sleep(Math.floor(Math.random() * (max - min + 1)) + min);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
99
|
try {
|
|
100
|
+
if (hasSimulateLatency) {
|
|
101
|
+
const delay = Math.floor(Math.random() * (latencyMaxMs - latencyMinMs + 1)) + latencyMinMs;
|
|
102
|
+
await sleep(delay);
|
|
103
|
+
}
|
|
117
104
|
const viewer = await getViewer(req);
|
|
118
105
|
if (route2.queryParams) {
|
|
119
|
-
req.query = await cleanAndValidate(route2.queryParams
|
|
106
|
+
req.query = await cleanAndValidate(route2.queryParams, req.query);
|
|
120
107
|
}
|
|
121
108
|
if (route2.bodyParams) {
|
|
122
|
-
req.body = await cleanAndValidate(route2.bodyParams
|
|
109
|
+
req.body = await cleanAndValidate(route2.bodyParams, req.body);
|
|
123
110
|
}
|
|
124
111
|
const context = {
|
|
125
112
|
controllerType: "route",
|
|
@@ -132,25 +119,23 @@ async function executeRequest(route2, req, res) {
|
|
|
132
119
|
params: req.params
|
|
133
120
|
}
|
|
134
121
|
};
|
|
135
|
-
const result = await runWithOrionAsyncContext(context,
|
|
122
|
+
const result = await runWithOrionAsyncContext(context, () => {
|
|
136
123
|
updateOrionAsyncContext({ viewer });
|
|
137
|
-
return
|
|
124
|
+
return route2.resolve(req, res, viewer);
|
|
138
125
|
});
|
|
139
126
|
if (!result) return;
|
|
140
127
|
if (result.statusCode) {
|
|
141
128
|
res.status(result.statusCode);
|
|
142
129
|
}
|
|
143
130
|
if (result.headers) {
|
|
144
|
-
|
|
145
|
-
res.setHeader(key, result.headers[key]);
|
|
146
|
-
}
|
|
131
|
+
res.set(result.headers);
|
|
147
132
|
}
|
|
148
|
-
if (
|
|
133
|
+
if (result.body !== null && result.body !== void 0) {
|
|
149
134
|
let body = result.body;
|
|
150
135
|
if (route2.returns) {
|
|
151
136
|
body = await cleanAndValidate(route2.returns, result.body);
|
|
152
137
|
}
|
|
153
|
-
if (
|
|
138
|
+
if (isPlainObject(body)) {
|
|
154
139
|
res.json(body);
|
|
155
140
|
} else {
|
|
156
141
|
res.send(body);
|
|
@@ -162,12 +147,22 @@ async function executeRequest(route2, req, res) {
|
|
|
162
147
|
}
|
|
163
148
|
|
|
164
149
|
// src/routes/registerRoute.ts
|
|
165
|
-
|
|
150
|
+
var appRegisteredRoutes = /* @__PURE__ */ new WeakMap();
|
|
151
|
+
function getRegisteredRoutes(app) {
|
|
152
|
+
if (appRegisteredRoutes.has(app)) {
|
|
153
|
+
return appRegisteredRoutes.get(app);
|
|
154
|
+
}
|
|
155
|
+
const routes = /* @__PURE__ */ new WeakSet();
|
|
156
|
+
appRegisteredRoutes.set(app, routes);
|
|
157
|
+
return routes;
|
|
158
|
+
}
|
|
166
159
|
function registerRoute(route2) {
|
|
167
160
|
const app = route2.app || getApp();
|
|
161
|
+
const registeredRoutes = getRegisteredRoutes(app);
|
|
162
|
+
if (registeredRoutes.has(route2)) return;
|
|
168
163
|
const method = route2.method;
|
|
169
|
-
const handler =
|
|
170
|
-
executeRequest(route2, req, res);
|
|
164
|
+
const handler = (req, res) => {
|
|
165
|
+
void executeRequest(route2, req, res);
|
|
171
166
|
};
|
|
172
167
|
const handlers = [handler];
|
|
173
168
|
if (route2.bodyParser) {
|
|
@@ -182,13 +177,13 @@ function registerRoute(route2) {
|
|
|
182
177
|
handlers.unshift(...route2.middlewares);
|
|
183
178
|
}
|
|
184
179
|
app[method](route2.path, ...handlers);
|
|
180
|
+
registeredRoutes.add(route2);
|
|
185
181
|
}
|
|
186
182
|
|
|
187
183
|
// src/routes/registerRoutes.ts
|
|
188
184
|
function registerRoutes(routesMap) {
|
|
189
|
-
const
|
|
190
|
-
|
|
191
|
-
registerRoute(route2);
|
|
185
|
+
for (const routeName in routesMap) {
|
|
186
|
+
registerRoute(routesMap[routeName]);
|
|
192
187
|
}
|
|
193
188
|
}
|
|
194
189
|
|
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/executeRequest.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/registerRoute.ts","../src/routes/registerRoutes.ts","../src/routes/route.ts","../src/service/index.ts"],"sourcesContent":["import express from 'express'\nimport crypto from 'node:crypto'\nimport {logger} from '@orion-js/logger'\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 logger.error(`[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 logger.error(`[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 logger.error('[route/handler] Internal server error', {error, url: req.url, hash})\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 registerRoute from './routes/registerRoute'\nimport registerRoutes from './routes/registerRoutes'\nimport bodyParserLib from 'body-parser'\nimport {RequestHandler} from 'express'\n\nexport * from './routes/route'\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 registerRoute,\n registerRoutes,\n bodyParser,\n}\n\nexport * from './types'\nexport * from './service'\n","import {OrionRequest, RouteType} from './../types'\nimport {getViewer} from './../viewer'\nimport {runWithOrionAsyncContext, updateOrionAsyncContext} from '@orion-js/logger'\nimport express from 'express'\nimport {isNil, type} from 'rambdax'\nimport {internalGetEnv} from '@orion-js/env'\nimport {sleep} from '@orion-js/helpers'\nimport {cleanAndValidate} from '@orion-js/schema'\nimport {onError} from '../errors'\nconst simulateLatency = internalGetEnv('simulate_latency', 'SIMULATE_LATENCY')\n\nexport async function executeRequest(\n route: RouteType<any>,\n req: OrionRequest,\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\n if (route.queryParams) {\n req.query = await cleanAndValidate(route.queryParams ?? {}, req.query)\n }\n\n if (route.bodyParams) {\n req.body = await cleanAndValidate(route.bodyParams ?? {}, req.body)\n }\n\n const context = {\n controllerType: 'route' as const,\n routeName: route.path,\n pathname: req.path,\n viewer,\n params: {\n body: req.body,\n query: req.query,\n params: req.params,\n },\n }\n\n const result = await runWithOrionAsyncContext(context, async () => {\n updateOrionAsyncContext({viewer})\n return await route.resolve(req, res, viewer)\n })\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 for (const key in result.headers) {\n res.setHeader(key, result.headers[key])\n }\n }\n\n // add body to response\n if (!isNil(result.body)) {\n let body = result.body\n\n if (route.returns) {\n body = await cleanAndValidate(route.returns, result.body)\n }\n\n if (type(body) === 'Object') {\n res.json(body)\n } else {\n res.send(body)\n }\n }\n } catch (error) {\n await onError(req, res, error)\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 {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.bodyParser && route.bodyParams) {\n const parser = bodyParser.json(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 {Schema, SchemaFieldType} from '@orion-js/schema'\nimport {RouteType, OrionRouteOptions} from '../types'\n\nexport function createRoute<\n TPath extends string,\n TQueryParamsSchema extends Schema | undefined,\n TBodyParamsSchema extends Schema | undefined,\n TReturnsSchema extends SchemaFieldType | undefined,\n>(\n options: OrionRouteOptions<TPath, TQueryParamsSchema, TBodyParamsSchema, TReturnsSchema>,\n): RouteType<TPath, TQueryParamsSchema, TBodyParamsSchema, TReturnsSchema> {\n return {\n ...options,\n }\n}\n\n/**\n * @deprecated Use createRoute instead\n */\nexport const route = createRoute\n","import {getInstance, Service} from '@orion-js/services'\nimport {createRoute} 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(): (method: any, context: ClassFieldDecoratorContext) => any\nexport function Route(\n options?: Omit<OrionRouteOptions<any, any, any, any>, 'resolve'>,\n): (method: any, context: ClassMethodDecoratorContext) => any\nexport function Route(options?: Omit<OrionRouteOptions<any, any, any, any>, 'resolve'>) {\n return (method: any, context: ClassFieldDecoratorContext | ClassMethodDecoratorContext) => {\n const propertyKey = String(context.name)\n\n context.addInitializer(function (this) {\n const routes = routeMetadata.get(this) || {}\n\n if (context.kind === 'method') {\n routes[propertyKey] = createRoute({\n ...options,\n resolve: this[propertyKey].bind(this),\n })\n }\n\n if (context.kind === 'field') {\n routes[propertyKey] = this[propertyKey]\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;AACnB,SAAQ,cAAa;AAIrB,IAAM,iBAAkC,OAAO,KAAK,KAAK,UAAU;AACjE,MAAI,MAAM,cAAc;AACtB,QAAI,aAAa;AACjB,QAAI,MAAM,SAAS,aAAa;AAC9B,mBAAa;AAAA,IACf,OAAO;AACL,aAAO,MAAM,iCAAiC,IAAI,IAAI,KAAK,EAAC,MAAK,CAAC;AAAA,IACpE;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,WAAO,MAAM,mCAAmC,IAAI,IAAI,KAAK,EAAC,MAAK,CAAC;AAAA,EACtE,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,WAAO,MAAM,yCAAyC,EAAC,OAAO,KAAK,IAAI,KAAK,KAAI,CAAC;AAAA,EACnF;AACF;AAEA,IAAI,aAA8B;AAE3B,IAAM,UAA2B,OAAO,KAAK,KAAK,UAAU;AACjE,QAAM,WAAW,KAAK,KAAK,KAAK;AAClC;AAEO,IAAM,aAAa,CAAC,gBAAuC;AAChE,eAAa;AACf;;;AC/CA,SAAQ,iBAAgB;AAExB,OAAO,eAAe,MAAM;AAErB,IAAM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"sources":["../src/errors.ts","../src/viewer.ts","../src/start.ts","../src/index.ts","../src/routes/registerRoute.ts","../src/routes/executeRequest.ts","../src/routes/registerRoutes.ts","../src/routes/route.ts","../src/service/index.ts"],"sourcesContent":["import crypto from 'node:crypto'\nimport {logger} from '@orion-js/logger'\nimport express from 'express'\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 logger.error(`[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 logger.error(`[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.status(statusCode)\n res.json(data)\n\n error.hash = hash\n logger.error('[route/handler] Internal server error', {error, url: req.url, hash})\n }\n}\n\nlet onErrorRef: onErrorFunction = defaultOnError\n\nexport const onError: onErrorFunction = async (req, res, error) => {\n return 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 registerRoute from './routes/registerRoute'\nimport registerRoutes from './routes/registerRoutes'\nimport bodyParserLib from 'body-parser'\nimport {RequestHandler} from 'express'\n\nexport * from './routes/route'\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 registerRoute,\n registerRoutes,\n bodyParser,\n}\n\nexport * from './types'\nexport * from './service'\n","import bodyParser from 'body-parser'\nimport express from 'express'\nimport {getApp} from './../start'\nimport {RouteType} from '../types'\nimport {executeRequest} from './executeRequest'\n\nconst appRegisteredRoutes = new WeakMap<express.Application, WeakSet<RouteType>>()\n\nfunction getRegisteredRoutes(app: express.Application) {\n if (appRegisteredRoutes.has(app)) {\n return appRegisteredRoutes.get(app)\n }\n\n const routes = new WeakSet<RouteType>()\n appRegisteredRoutes.set(app, routes)\n return routes\n}\n\nexport default function registerRoute(route: RouteType): void {\n const app = route.app || getApp()\n const registeredRoutes = getRegisteredRoutes(app)\n if (registeredRoutes.has(route)) return\n\n const method = route.method\n\n const handler: express.RequestHandler = (req, res) => {\n void executeRequest(route, req as any, 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.bodyParser && route.bodyParams) {\n const parser = bodyParser.json(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 registeredRoutes.add(route)\n}\n","import {internalGetEnv} from '@orion-js/env'\nimport {sleep} from '@orion-js/helpers'\nimport {runWithOrionAsyncContext, updateOrionAsyncContext} from '@orion-js/logger'\nimport {cleanAndValidate} from '@orion-js/schema'\nimport express from 'express'\nimport {onError} from '../errors'\nimport {OrionRequest, RouteType} from './../types'\nimport {getViewer} from './../viewer'\n\nconst simulateLatency = internalGetEnv('simulate_latency', 'SIMULATE_LATENCY')\nconst simulateLatencyMs = Number.parseInt(simulateLatency, 10)\nconst hasSimulateLatency = Number.isFinite(simulateLatencyMs) && simulateLatencyMs > 0\nconst latencyMinMs = hasSimulateLatency ? Math.floor(simulateLatencyMs * 0.9) : 0\nconst latencyMaxMs = hasSimulateLatency ? Math.ceil(simulateLatencyMs * 1.1) : 0\n\nconst objectToString = Object.prototype.toString\nfunction isPlainObject(value: unknown): value is Record<string, unknown> {\n return objectToString.call(value) === '[object Object]'\n}\n\nexport async function executeRequest(\n route: RouteType<any>,\n req: OrionRequest,\n res: express.Response,\n) {\n try {\n if (hasSimulateLatency) {\n const delay = Math.floor(Math.random() * (latencyMaxMs - latencyMinMs + 1)) + latencyMinMs\n await sleep(delay)\n }\n\n const viewer = await getViewer(req)\n\n if (route.queryParams) {\n req.query = await cleanAndValidate(route.queryParams, req.query)\n }\n\n if (route.bodyParams) {\n req.body = await cleanAndValidate(route.bodyParams, req.body)\n }\n\n const context = {\n controllerType: 'route' as const,\n routeName: route.path,\n pathname: req.path,\n viewer,\n params: {\n body: req.body,\n query: req.query,\n params: req.params,\n },\n }\n\n const result = await runWithOrionAsyncContext(context, () => {\n updateOrionAsyncContext({viewer})\n return route.resolve(req, res, viewer)\n })\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 res.set(result.headers)\n }\n\n // add body to response\n if (result.body !== null && result.body !== undefined) {\n let body = result.body\n\n if (route.returns) {\n body = await cleanAndValidate(route.returns, result.body)\n }\n\n if (isPlainObject(body)) {\n res.json(body)\n } else {\n res.send(body)\n }\n }\n } catch (error) {\n await onError(req, res, error)\n }\n}\n","import {RoutesMap} from '../types'\nimport registerRoute from './registerRoute'\n\nexport default function registerRoutes(routesMap: RoutesMap): void {\n for (const routeName in routesMap) {\n registerRoute(routesMap[routeName])\n }\n}\n","import {Schema, SchemaFieldType} from '@orion-js/schema'\nimport {RouteType, OrionRouteOptions} from '../types'\n\nexport function createRoute<\n TPath extends string,\n TQueryParamsSchema extends Schema | undefined,\n TBodyParamsSchema extends Schema | undefined,\n TReturnsSchema extends SchemaFieldType | undefined,\n>(\n options: OrionRouteOptions<TPath, TQueryParamsSchema, TBodyParamsSchema, TReturnsSchema>,\n): RouteType<TPath, TQueryParamsSchema, TBodyParamsSchema, TReturnsSchema> {\n return {\n ...options,\n }\n}\n\n/**\n * @deprecated Use createRoute instead\n */\nexport const route = createRoute\n","import {getInstance, Service} from '@orion-js/services'\nimport {createRoute} 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(): (method: any, context: ClassFieldDecoratorContext) => any\nexport function Route(\n options?: Omit<OrionRouteOptions<any, any, any, any>, 'resolve'>,\n): (method: any, context: ClassMethodDecoratorContext) => any\nexport function Route(options?: Omit<OrionRouteOptions<any, any, any, any>, 'resolve'>) {\n return (method: any, context: ClassFieldDecoratorContext | ClassMethodDecoratorContext) => {\n const propertyKey = String(context.name)\n\n context.addInitializer(function (this) {\n const routes = routeMetadata.get(this) || {}\n\n if (context.kind === 'method') {\n routes[propertyKey] = createRoute({\n ...options,\n resolve: this[propertyKey].bind(this),\n })\n }\n\n if (context.kind === 'field') {\n routes[propertyKey] = this[propertyKey]\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,OAAO,YAAY;AACnB,SAAQ,cAAa;AAKrB,IAAM,iBAAkC,OAAO,KAAK,KAAK,UAAU;AACjE,MAAI,MAAM,cAAc;AACtB,QAAI,aAAa;AACjB,QAAI,MAAM,SAAS,aAAa;AAC9B,mBAAa;AAAA,IACf,OAAO;AACL,aAAO,MAAM,iCAAiC,IAAI,IAAI,KAAK,EAAC,MAAK,CAAC;AAAA,IACpE;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,WAAO,MAAM,mCAAmC,IAAI,IAAI,KAAK,EAAC,MAAK,CAAC;AAAA,EACtE,OAAO;AACL,UAAM,OAAO,OACV,WAAW,MAAM,EACjB,OA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,KAAK,WAAW;AAAA,MACxC;AAEA,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;;;ALnDA,IAAM,EAAC,MAAM,KAAK,MAAM,WAAU,IAAI;AAEtC,IAAMC,cAKF,EAAC,MAAM,KAAK,MAAM,WAAU;","names":["express","internalGetEnv","internalGetEnv","route","route","bodyParser"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orion-js/http",
|
|
3
|
-
"version": "4.2.
|
|
3
|
+
"version": "4.2.3",
|
|
4
4
|
"main": "./dist/index.cjs",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -11,11 +11,11 @@
|
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"body-parser": "1.20.3",
|
|
13
13
|
"express": "4.21.2",
|
|
14
|
-
"@orion-js/env": "4.2.
|
|
15
|
-
"@orion-js/helpers": "4.2.0",
|
|
16
|
-
"@orion-js/resolvers": "4.2.1",
|
|
14
|
+
"@orion-js/env": "4.2.1",
|
|
17
15
|
"@orion-js/schema": "4.2.1",
|
|
18
|
-
"@orion-js/
|
|
16
|
+
"@orion-js/resolvers": "4.2.1",
|
|
17
|
+
"@orion-js/services": "4.2.0",
|
|
18
|
+
"@orion-js/helpers": "4.2.0"
|
|
19
19
|
},
|
|
20
20
|
"peerDependencies": {
|
|
21
21
|
"@orion-js/logger": "4.2.0"
|