@medusajs/framework 3.0.0-preview-20250211091350 → 3.0.0-preview-20250406180144

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.
Files changed (93) hide show
  1. package/dist/build-tools/compiler.d.ts +2 -1
  2. package/dist/build-tools/compiler.d.ts.map +1 -1
  3. package/dist/build-tools/compiler.js +13 -7
  4. package/dist/build-tools/compiler.js.map +1 -1
  5. package/dist/config/types.d.ts +25 -1
  6. package/dist/config/types.d.ts.map +1 -1
  7. package/dist/database/pg-connection-loader.d.ts.map +1 -1
  8. package/dist/database/pg-connection-loader.js +7 -1
  9. package/dist/database/pg-connection-loader.js.map +1 -1
  10. package/dist/http/express-loader.d.ts.map +1 -1
  11. package/dist/http/express-loader.js +42 -9
  12. package/dist/http/express-loader.js.map +1 -1
  13. package/dist/http/middleware-file-loader.d.ts +28 -0
  14. package/dist/http/middleware-file-loader.d.ts.map +1 -0
  15. package/dist/http/middleware-file-loader.js +140 -0
  16. package/dist/http/middleware-file-loader.js.map +1 -0
  17. package/dist/http/middlewares/apply-default-filters.d.ts +2 -2
  18. package/dist/http/middlewares/apply-default-filters.d.ts.map +1 -1
  19. package/dist/http/middlewares/apply-default-filters.js +1 -1
  20. package/dist/http/middlewares/apply-default-filters.js.map +1 -1
  21. package/dist/http/middlewares/apply-params-as-filters.d.ts +2 -2
  22. package/dist/http/middlewares/apply-params-as-filters.d.ts.map +1 -1
  23. package/dist/http/middlewares/apply-params-as-filters.js +1 -1
  24. package/dist/http/middlewares/apply-params-as-filters.js.map +1 -1
  25. package/dist/http/middlewares/authenticate-middleware.d.ts +2 -0
  26. package/dist/http/middlewares/authenticate-middleware.d.ts.map +1 -1
  27. package/dist/http/middlewares/authenticate-middleware.js +3 -2
  28. package/dist/http/middlewares/authenticate-middleware.js.map +1 -1
  29. package/dist/http/middlewares/bodyparser.d.ts +13 -0
  30. package/dist/http/middlewares/bodyparser.d.ts.map +1 -0
  31. package/dist/http/middlewares/bodyparser.js +62 -0
  32. package/dist/http/middlewares/bodyparser.js.map +1 -0
  33. package/dist/http/middlewares/clear-filters-by-key.d.ts +2 -2
  34. package/dist/http/middlewares/clear-filters-by-key.d.ts.map +1 -1
  35. package/dist/http/middlewares/clear-filters-by-key.js +1 -1
  36. package/dist/http/middlewares/clear-filters-by-key.js.map +1 -1
  37. package/dist/http/middlewares/ensure-publishable-api-key.d.ts +2 -2
  38. package/dist/http/middlewares/ensure-publishable-api-key.d.ts.map +1 -1
  39. package/dist/http/middlewares/ensure-publishable-api-key.js +3 -7
  40. package/dist/http/middlewares/ensure-publishable-api-key.js.map +1 -1
  41. package/dist/http/middlewares/error-handler.d.ts +2 -4
  42. package/dist/http/middlewares/error-handler.d.ts.map +1 -1
  43. package/dist/http/middlewares/error-handler.js +1 -1
  44. package/dist/http/middlewares/error-handler.js.map +1 -1
  45. package/dist/http/router.d.ts +4 -76
  46. package/dist/http/router.d.ts.map +1 -1
  47. package/dist/http/router.js +175 -717
  48. package/dist/http/router.js.map +1 -1
  49. package/dist/http/routes-finder.d.ts +22 -0
  50. package/dist/http/routes-finder.d.ts.map +1 -0
  51. package/dist/http/routes-finder.js +62 -0
  52. package/dist/http/routes-finder.js.map +1 -0
  53. package/dist/http/routes-loader.d.ts +30 -0
  54. package/dist/http/routes-loader.d.ts.map +1 -0
  55. package/dist/http/routes-loader.js +203 -0
  56. package/dist/http/routes-loader.js.map +1 -0
  57. package/dist/http/routes-sorter.d.ts +105 -0
  58. package/dist/http/routes-sorter.d.ts.map +1 -0
  59. package/dist/http/routes-sorter.js +146 -0
  60. package/dist/http/routes-sorter.js.map +1 -0
  61. package/dist/http/types.d.ts +33 -17
  62. package/dist/http/types.d.ts.map +1 -1
  63. package/dist/http/utils/define-middlewares.d.ts +4 -0
  64. package/dist/http/utils/define-middlewares.d.ts.map +1 -1
  65. package/dist/http/utils/define-middlewares.js +5 -1
  66. package/dist/http/utils/define-middlewares.js.map +1 -1
  67. package/dist/http/utils/maybe-apply-link-filter.d.ts +2 -2
  68. package/dist/http/utils/maybe-apply-link-filter.d.ts.map +1 -1
  69. package/dist/http/utils/maybe-apply-link-filter.js.map +1 -1
  70. package/dist/http/utils/wrap-handler.d.ts +3 -0
  71. package/dist/http/utils/wrap-handler.d.ts.map +1 -0
  72. package/dist/http/utils/wrap-handler.js +27 -0
  73. package/dist/http/utils/wrap-handler.js.map +1 -0
  74. package/dist/jobs/job-loader.d.ts +9 -4
  75. package/dist/jobs/job-loader.d.ts.map +1 -1
  76. package/dist/jobs/job-loader.js +15 -67
  77. package/dist/jobs/job-loader.js.map +1 -1
  78. package/dist/medusa-app-loader.d.ts.map +1 -1
  79. package/dist/medusa-app-loader.js +4 -0
  80. package/dist/medusa-app-loader.js.map +1 -1
  81. package/dist/subscribers/subscriber-loader.d.ts +4 -3
  82. package/dist/subscribers/subscriber-loader.d.ts.map +1 -1
  83. package/dist/subscribers/subscriber-loader.js +18 -67
  84. package/dist/subscribers/subscriber-loader.js.map +1 -1
  85. package/dist/utils/resource-loader.d.ts +26 -0
  86. package/dist/utils/resource-loader.d.ts.map +1 -0
  87. package/dist/utils/resource-loader.js +78 -0
  88. package/dist/utils/resource-loader.js.map +1 -0
  89. package/dist/workflows/workflow-loader.d.ts +4 -2
  90. package/dist/workflows/workflow-loader.d.ts.map +1 -1
  91. package/dist/workflows/workflow-loader.js +8 -58
  92. package/dist/workflows/workflow-loader.js.map +1 -1
  93. package/package.json +12 -11
@@ -13,752 +13,210 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
13
13
  var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  return (mod && mod.__esModule) ? mod : { "default": mod };
15
15
  };
16
- var _ApiRoutesLoader_routesMap, _ApiRoutesLoader_globalMiddlewaresDescriptor, _ApiRoutesLoader_app, _ApiRoutesLoader_router, _ApiRoutesLoader_activityId, _ApiRoutesLoader_excludes, _ApiRoutesLoader_sourceDir, _RoutesLoader_instances, _RoutesLoader_app, _RoutesLoader_activityId, _RoutesLoader_sourceDir, _RoutesLoader_assignRestrictedFields;
16
+ var _ApiLoader_instances, _a, _ApiLoader_app, _ApiLoader_sourceDirs, _ApiLoader_loadHttpResources, _ApiLoader_registerExpressHandler, _ApiLoader_assignRestrictedFields, _ApiLoader_createCorsOptions, _ApiLoader_applyCorsMiddleware, _ApiLoader_applyAuthMiddleware, _ApiLoader_applyBodyParserMiddleware, _ApiLoader_applyStorePublishableKeyMiddleware;
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.RoutesLoader = exports.ApiRoutesLoader = void 0;
19
- const utils_1 = require("@medusajs/utils");
18
+ exports.ApiLoader = void 0;
19
+ const reporter_1 = __importDefault(require("@medusajs/cli/dist/reporter"));
20
20
  const cors_1 = __importDefault(require("cors"));
21
- const express_1 = require("express");
22
- const promises_1 = require("fs/promises");
23
- const path_1 = require("path");
24
- const config_1 = require("../config");
25
- const logger_1 = require("../logger");
21
+ const utils_1 = require("@medusajs/utils");
22
+ const routes_loader_1 = require("./routes-loader");
23
+ const routes_finder_1 = require("./routes-finder");
24
+ const routes_sorter_1 = require("./routes-sorter");
25
+ const wrap_handler_1 = require("./utils/wrap-handler");
26
26
  const middlewares_1 = require("./middlewares");
27
- const ensure_publishable_api_key_1 = require("./middlewares/ensure-publishable-api-key");
28
- const types_1 = require("./types");
27
+ const error_handler_1 = require("./middlewares/error-handler");
29
28
  const restricted_fields_1 = require("./utils/restricted-fields");
30
- const log = ({ activityId, message, }) => {
31
- if (activityId) {
32
- logger_1.logger.progress(activityId, message);
33
- return;
34
- }
35
- logger_1.logger.debug(message);
36
- };
37
- /**
38
- * File name that is used to indicate that the file is a route file
39
- */
40
- const ROUTE_NAME = "route";
41
- /**
42
- * Flag that developers can export from their route files to indicate
43
- * whether or not the route should be authenticated or not.
44
- */
45
- const AUTHTHENTICATE = "AUTHENTICATE";
46
- /**
47
- * File name for the global middlewares file
48
- */
49
- const MIDDLEWARES_NAME = "middlewares";
50
- const pathSegmentReplacer = {
51
- "\\[\\.\\.\\.\\]": () => `*`,
52
- "\\[(\\w+)?": (param) => `:${param}`,
53
- "\\]": () => ``,
54
- };
55
- /**
56
- * @param routes - The routes to prioritize
57
- *
58
- * @return An array of sorted
59
- * routes based on their priority
60
- */
61
- const prioritize = (routes) => {
62
- return routes.sort((a, b) => {
63
- return a.priority - b.priority;
64
- });
65
- };
66
- /**
67
- * The smaller the number the higher the priority with zero indicating
68
- * highest priority
69
- *
70
- * @param path - The path to calculate the priority for
71
- *
72
- * @return An integer ranging from `0` to `Infinity`
73
- */
74
- function calculatePriority(path) {
75
- const depth = path.match(/\/.+?/g)?.length || 0;
76
- const specifity = path.match(/\/:.+?/g)?.length || 0;
77
- const catchall = (path.match(/\/\*/g)?.length || 0) > 0 ? Infinity : 0;
78
- return depth + specifity + catchall;
79
- }
80
- function matchMethod(method, configMethod) {
81
- if (!configMethod || configMethod === "USE" || configMethod === "ALL") {
82
- return true;
83
- }
84
- else if (Array.isArray(configMethod)) {
85
- return (configMethod.includes(method) ||
86
- configMethod.includes("ALL") ||
87
- configMethod.includes("USE"));
88
- }
89
- else {
90
- return method === configMethod;
91
- }
92
- }
93
- /**
94
- * Function that looks though the global middlewares and returns the first
95
- * complete match for the given path and method.
96
- *
97
- * @param path - The path to match
98
- * @param method - The method to match
99
- * @param routes - The routes to match against
100
- * @returns The first complete match or undefined if no match is found
101
- */
102
- function findMatch(path, method, routes) {
103
- for (const route of routes) {
104
- const { matcher, method: configMethod } = route;
105
- if (matchMethod(method, configMethod)) {
106
- let isMatch = false;
107
- if (typeof matcher === "string") {
108
- // Convert wildcard expressions to proper regex for matching entire path
109
- // The '.*' will match any character sequence including '/'
110
- const regex = new RegExp(`^${matcher.split("*").join(".*")}$`);
111
- isMatch = regex.test(path);
112
- }
113
- else if (matcher instanceof RegExp) {
114
- // Ensure that the regex matches the entire path
115
- const match = path.match(matcher);
116
- isMatch = match !== null && match[0] === path;
117
- }
118
- if (isMatch) {
119
- return route; // Return the first complete match
120
- }
121
- }
122
- }
123
- return undefined; // Return undefined if no complete match is found
124
- }
125
- /**
126
- * Returns an array of body parser middlewares that are applied on routes
127
- * out-of-the-box.
128
- */
129
- function getBodyParserMiddleware(args) {
130
- const sizeLimit = args?.sizeLimit;
131
- const preserveRawBody = args?.preserveRawBody;
132
- return [
133
- (0, express_1.json)({
134
- limit: sizeLimit,
135
- verify: preserveRawBody
136
- ? (req, res, buf) => {
137
- req.rawBody = buf;
138
- }
139
- : undefined,
140
- }),
141
- (0, express_1.text)({ limit: sizeLimit }),
142
- (0, express_1.urlencoded)({ limit: sizeLimit, extended: true }),
143
- ];
144
- }
145
- function createCorsOptions(origin) {
146
- return {
147
- origin: (0, utils_1.parseCorsOrigins)(origin),
148
- credentials: true,
149
- };
150
- }
151
- function applyCors(router, route, corsConfig) {
152
- router.use(route, (0, cors_1.default)(corsConfig));
153
- }
154
- function getRouteContext(path) {
155
- /**
156
- * We cannot reliably guess the route context from a regex, so we skip it.
157
- */
158
- if (path instanceof RegExp) {
159
- return null;
160
- }
161
- if (path.startsWith("/admin")) {
162
- return "admin";
163
- }
164
- if (path.startsWith("/store")) {
165
- return "store";
166
- }
167
- if (path.startsWith("/auth")) {
168
- return "auth";
169
- }
170
- return null;
171
- }
172
- // TODO this router would need a proper rework, but it is out of scope right now
173
- class ApiRoutesLoader {
174
- constructor({ app, activityId, sourceDir, }) {
175
- /**
176
- * Map of router path and its descriptor
177
- * @private
178
- */
179
- _ApiRoutesLoader_routesMap.set(this, new Map()
180
- /**
181
- * Global middleware descriptors
182
- * @private
183
- */
184
- );
185
- /**
186
- * Global middleware descriptors
187
- * @private
188
- */
189
- _ApiRoutesLoader_globalMiddlewaresDescriptor.set(this, void 0);
29
+ const middleware_file_loader_1 = require("./middleware-file-loader");
30
+ const bodyparser_1 = require("./middlewares/bodyparser");
31
+ const ensure_publishable_api_key_1 = require("./middlewares/ensure-publishable-api-key");
32
+ const config_1 = require("../config");
33
+ class ApiLoader {
34
+ constructor({ app, sourceDir, baseRestrictedFields = [], }) {
35
+ _ApiLoader_instances.add(this);
190
36
  /**
191
37
  * An express instance
192
38
  * @private
193
39
  */
194
- _ApiRoutesLoader_app.set(this, void 0);
195
- /**
196
- * A router to assign the route to
197
- * @private
198
- */
199
- _ApiRoutesLoader_router.set(this, void 0);
200
- /**
201
- * An eventual activity id for information tracking
202
- * @private
203
- */
204
- _ApiRoutesLoader_activityId.set(this, void 0);
205
- /**
206
- * The list of file names to exclude from the routes scan
207
- * @private
208
- */
209
- _ApiRoutesLoader_excludes.set(this, [
210
- /\.DS_Store/,
211
- /(\.ts\.map|\.js\.map|\.d\.ts|\.md)/,
212
- /^_[^/\\]*(\.[^/\\]+)?$/,
213
- ]
214
- /**
215
- * Path from where to load the routes from
216
- * @private
217
- */
218
- );
40
+ _ApiLoader_app.set(this, void 0);
219
41
  /**
220
42
  * Path from where to load the routes from
221
43
  * @private
222
44
  */
223
- _ApiRoutesLoader_sourceDir.set(this, void 0);
224
- __classPrivateFieldSet(this, _ApiRoutesLoader_app, app, "f");
225
- __classPrivateFieldSet(this, _ApiRoutesLoader_router, (0, express_1.Router)(), "f");
226
- __classPrivateFieldSet(this, _ApiRoutesLoader_activityId, activityId, "f");
227
- __classPrivateFieldSet(this, _ApiRoutesLoader_sourceDir, sourceDir, "f");
228
- }
229
- /**
230
- * Validate the route config and display a log info if
231
- * it should be ignored or skipped.
232
- *
233
- * @param {GlobalMiddlewareDescriptor} descriptor
234
- * @param {MiddlewaresConfig} config
235
- *
236
- * @return {void}
237
- */
238
- validateMiddlewaresConfig({ config, }) {
239
- if (!config?.routes && !config?.errorHandler) {
240
- log({
241
- activityId: __classPrivateFieldGet(this, _ApiRoutesLoader_activityId, "f"),
242
- message: `Empty middleware config. Skipping middleware application.`,
243
- });
244
- return;
245
- }
246
- for (const route of config.routes ?? []) {
247
- if (!route.matcher) {
248
- throw new Error(`Route is missing a \`matcher\` field. The 'matcher' field is required when applying middleware to this route.`);
249
- }
250
- }
251
- }
252
- /**
253
- * Take care of replacing the special path segments
254
- * to an express specific path segment
255
- *
256
- * @param route - The route to parse
257
- *
258
- * @example
259
- * "/admin/orders/[id]/route.ts => "/admin/orders/:id/route.ts"
260
- */
261
- parseRoute(route) {
262
- let route_ = route;
263
- for (const config of Object.entries(pathSegmentReplacer)) {
264
- const [searchFor, replacedByFn] = config;
265
- const replacer = new RegExp(searchFor, "g");
266
- const matches = [...route_.matchAll(replacer)];
267
- const parameters = new Set();
268
- for (const match of matches) {
269
- if (match?.[1] && !Number.isInteger(match?.[1])) {
270
- if (parameters.has(match?.[1])) {
271
- log({
272
- activityId: __classPrivateFieldGet(this, _ApiRoutesLoader_activityId, "f"),
273
- message: `Duplicate parameters found in route ${route} (${match?.[1]})`,
274
- });
275
- throw new Error(`Duplicate parameters found in route ${route} (${match?.[1]}). Make sure that all parameters are unique.`);
276
- }
277
- parameters.add(match?.[1]);
278
- }
279
- route_ = route_.replace(match[0], replacedByFn(match?.[1]));
280
- }
281
- const extension = (0, path_1.extname)(route_);
282
- if (extension) {
283
- route_ = route_.replace(extension, "");
284
- }
285
- }
286
- route = route_;
287
- return route;
288
- }
289
- /**
290
- * Load the file content from a descriptor and retrieve the verbs and handlers
291
- * to be assigned to the descriptor
292
- *
293
- * @return {Promise<void>}
294
- */
295
- async createRoutesConfig() {
296
- await (0, utils_1.promiseAll)([...__classPrivateFieldGet(this, _ApiRoutesLoader_routesMap, "f").values()].map(async (descriptor) => {
297
- const absolutePath = descriptor.absolutePath;
298
- const route = descriptor.route;
299
- return await (0, utils_1.dynamicImport)(absolutePath).then((import_) => {
300
- const map = __classPrivateFieldGet(this, _ApiRoutesLoader_routesMap, "f");
301
- const config = {
302
- routes: [],
303
- };
304
- /**
305
- * If the developer has not exported the
306
- * AUTHENTICATE flag we default to true.
307
- */
308
- const shouldRequireAuth = import_[AUTHTHENTICATE] !== undefined
309
- ? import_[AUTHTHENTICATE]
310
- : true;
311
- config.optedOutOfAuth = !shouldRequireAuth;
312
- /**
313
- * If the developer has not exported the
314
- * CORS flag we default to true.
315
- */
316
- const shouldAddCors = import_["CORS"] !== undefined ? import_["CORS"] : true;
317
- if (route.startsWith("/admin")) {
318
- config.routeType = "admin";
319
- if (shouldAddCors) {
320
- config.shouldAppendAdminCors = true;
321
- }
322
- }
323
- if (route.startsWith("/store")) {
324
- config.routeType = "store";
325
- if (shouldAddCors) {
326
- config.shouldAppendStoreCors = true;
327
- }
328
- }
329
- if (route.startsWith("/auth") && shouldAddCors) {
330
- config.routeType = "auth";
331
- if (shouldAddCors) {
332
- config.shouldAppendAuthCors = true;
333
- }
334
- }
335
- const handlers = Object.keys(import_).filter((key) => {
336
- /**
337
- * Filter out any export that is not a function
338
- */
339
- return typeof import_[key] === "function";
340
- });
341
- for (const handler of handlers) {
342
- if (types_1.HTTP_METHODS.includes(handler)) {
343
- config.routes?.push({
344
- method: handler,
345
- handler: import_[handler],
346
- });
347
- }
348
- else {
349
- log({
350
- activityId: __classPrivateFieldGet(this, _ApiRoutesLoader_activityId, "f"),
351
- message: `Skipping handler ${handler} in ${absolutePath}. Invalid HTTP method: ${handler}.`,
352
- });
353
- }
354
- }
355
- if (!config.routes?.length) {
356
- log({
357
- activityId: __classPrivateFieldGet(this, _ApiRoutesLoader_activityId, "f"),
358
- message: `No valid route handlers detected in ${absolutePath}. Skipping route configuration.`,
359
- });
360
- map.delete(absolutePath);
361
- return;
362
- }
363
- descriptor.config = config;
364
- map.set(absolutePath, descriptor);
365
- });
366
- }));
45
+ _ApiLoader_sourceDirs.set(this, void 0);
46
+ __classPrivateFieldSet(this, _ApiLoader_app, app, "f");
47
+ __classPrivateFieldSet(this, _ApiLoader_sourceDirs, Array.isArray(sourceDir) ? sourceDir : [sourceDir], "f");
48
+ __classPrivateFieldGet(this, _ApiLoader_instances, "m", _ApiLoader_assignRestrictedFields).call(this, baseRestrictedFields ?? []);
367
49
  }
368
- createRoutesDescriptor(path) {
369
- const descriptor = {
370
- absolutePath: path,
371
- relativePath: path,
372
- route: "",
373
- priority: Infinity,
374
- };
375
- const childPath = path.replace(__classPrivateFieldGet(this, _ApiRoutesLoader_sourceDir, "f"), "");
376
- descriptor.relativePath = childPath;
377
- let routeToParse = childPath;
378
- const pathSegments = routeToParse.split(path_1.sep);
379
- const lastSegment = pathSegments[pathSegments.length - 1];
380
- if (lastSegment.startsWith("route")) {
381
- pathSegments.pop();
382
- routeToParse = pathSegments.join("/");
383
- }
384
- descriptor.route = this.parseRoute(routeToParse);
385
- descriptor.priority = calculatePriority(descriptor.route);
386
- __classPrivateFieldGet(this, _ApiRoutesLoader_routesMap, "f").set(path, descriptor);
387
- }
388
- async createMiddlewaresDescriptor() {
389
- const filePaths = await (0, promises_1.readdir)(__classPrivateFieldGet(this, _ApiRoutesLoader_sourceDir, "f"));
390
- const filteredFilePaths = filePaths.filter((path) => {
391
- const pathToCheck = path.replace(__classPrivateFieldGet(this, _ApiRoutesLoader_sourceDir, "f"), "");
392
- return !pathToCheck
393
- .split(path_1.sep)
394
- .some((segment) => __classPrivateFieldGet(this, _ApiRoutesLoader_excludes, "f").some((exclude) => exclude.test(segment)));
395
- });
396
- const middlewareFilePath = filteredFilePaths.find((file) => {
397
- return file.replace(/\.[^/.]+$/, "") === MIDDLEWARES_NAME;
398
- });
399
- if (!middlewareFilePath) {
400
- log({
401
- activityId: __classPrivateFieldGet(this, _ApiRoutesLoader_activityId, "f"),
402
- message: `No middleware files found in ${__classPrivateFieldGet(this, _ApiRoutesLoader_sourceDir, "f")}. Skipping middleware configuration.`,
403
- });
404
- return;
405
- }
406
- const absolutePath = (0, path_1.join)(__classPrivateFieldGet(this, _ApiRoutesLoader_sourceDir, "f"), middlewareFilePath);
407
- await (0, utils_1.dynamicImport)(absolutePath).then((import_) => {
408
- const middlewaresConfig = (0, utils_1.resolveExports)(import_).default;
409
- if (!middlewaresConfig) {
410
- log({
411
- activityId: __classPrivateFieldGet(this, _ApiRoutesLoader_activityId, "f"),
412
- message: `No middleware configuration found in ${absolutePath}. Skipping middleware configuration.`,
413
- });
414
- return;
415
- }
416
- middlewaresConfig.routes = middlewaresConfig.routes?.map((route) => {
417
- return {
418
- ...route,
419
- method: route.method ?? "USE",
420
- };
421
- });
422
- const descriptor = {
423
- config: middlewaresConfig,
424
- };
425
- this.validateMiddlewaresConfig(descriptor);
426
- __classPrivateFieldSet(this, _ApiRoutesLoader_globalMiddlewaresDescriptor, descriptor, "f");
50
+ async load() {
51
+ const { errorHandler: sourceErrorHandler, middlewares, routes, routesFinder, bodyParserConfigRoutes, } = await __classPrivateFieldGet(this, _ApiLoader_instances, "m", _ApiLoader_loadHttpResources).call(this);
52
+ /**
53
+ * Parse request body on all the requests and use the routes finder
54
+ * to pick the best matching config for the given route.
55
+ */
56
+ const bodyParserRoutesFinder = new routes_finder_1.RoutesFinder(new routes_sorter_1.RoutesSorter(bodyParserConfigRoutes).sort([
57
+ "static",
58
+ "params",
59
+ "regex",
60
+ "wildcard",
61
+ "global",
62
+ ]));
63
+ __classPrivateFieldGet(this, _ApiLoader_instances, "m", _ApiLoader_applyBodyParserMiddleware).call(this, "/", bodyParserRoutesFinder);
64
+ /**
65
+ * CORS and Auth setup for admin routes
66
+ */
67
+ __classPrivateFieldGet(this, _ApiLoader_instances, "m", _ApiLoader_applyCorsMiddleware).call(this, routesFinder, "/admin", "shouldAppendAdminCors", __classPrivateFieldGet(this, _ApiLoader_instances, "m", _ApiLoader_createCorsOptions).call(this, config_1.configManager.config.projectConfig.http.adminCors));
68
+ __classPrivateFieldGet(this, _ApiLoader_instances, "m", _ApiLoader_applyAuthMiddleware).call(this, routesFinder, "/admin", "user", [
69
+ "bearer",
70
+ "session",
71
+ "api-key",
72
+ ]);
73
+ __classPrivateFieldGet(this, _ApiLoader_instances, "m", _ApiLoader_applyCorsMiddleware).call(this, routesFinder, "/store", "shouldAppendStoreCors", __classPrivateFieldGet(this, _ApiLoader_instances, "m", _ApiLoader_createCorsOptions).call(this, config_1.configManager.config.projectConfig.http.storeCors));
74
+ /**
75
+ * Publishable key check, CORS and auth setup for store routes.
76
+ */
77
+ __classPrivateFieldGet(this, _ApiLoader_instances, "m", _ApiLoader_applyStorePublishableKeyMiddleware).call(this, "/store");
78
+ __classPrivateFieldGet(this, _ApiLoader_instances, "m", _ApiLoader_applyAuthMiddleware).call(this, routesFinder, "/store", "customer", ["bearer", "session"], {
79
+ allowUnauthenticated: true,
427
80
  });
428
- }
429
- async createRoutesMap() {
430
- await (0, utils_1.promiseAll)(await (0, utils_1.readDirRecursive)(__classPrivateFieldGet(this, _ApiRoutesLoader_sourceDir, "f")).then((entries) => {
431
- const fileEntries = entries.filter((entry) => {
432
- const fullPathFromSource = (0, path_1.join)(entry.path, entry.name).replace(__classPrivateFieldGet(this, _ApiRoutesLoader_sourceDir, "f"), "");
433
- const isExcluded = fullPathFromSource
434
- .split(path_1.sep)
435
- .some((segment) => __classPrivateFieldGet(this, _ApiRoutesLoader_excludes, "f").some((exclude) => exclude.test(segment)));
436
- return (!entry.isDirectory() &&
437
- !isExcluded &&
438
- (0, path_1.parse)(entry.name).name === ROUTE_NAME);
439
- });
440
- return fileEntries.map(async (entry) => {
441
- const path = (0, path_1.join)(entry.path, entry.name);
442
- return this.createRoutesDescriptor(path);
443
- });
444
- }));
445
- }
446
- /**
447
- * Apply the most specific body parser middleware to the router
448
- */
449
- applyBodyParserMiddleware(path, method) {
450
- const middlewareDescriptor = __classPrivateFieldGet(this, _ApiRoutesLoader_globalMiddlewaresDescriptor, "f");
451
- const mostSpecificConfig = findMatch(path, method, middlewareDescriptor?.config?.routes ?? []);
452
- if (!mostSpecificConfig || mostSpecificConfig?.bodyParser === undefined) {
453
- __classPrivateFieldGet(this, _ApiRoutesLoader_router, "f")[method.toLowerCase()](path, ...getBodyParserMiddleware());
454
- return;
455
- }
456
- if (mostSpecificConfig?.bodyParser) {
457
- __classPrivateFieldGet(this, _ApiRoutesLoader_router, "f")[method.toLowerCase()](path, ...getBodyParserMiddleware(mostSpecificConfig?.bodyParser));
458
- return;
459
- }
460
- return;
461
- }
462
- /**
463
- * Applies middleware that checks if a valid publishable key is set on store request
464
- */
465
- applyStorePublishableKeyMiddleware(route) {
466
- let middleware = ensure_publishable_api_key_1.ensurePublishableApiKeyMiddleware;
467
- if (ApiRoutesLoader.traceMiddleware) {
468
- middleware = ApiRoutesLoader.traceMiddleware(middleware, {
469
- route: String(route),
470
- });
471
- }
472
- __classPrivateFieldGet(this, _ApiRoutesLoader_router, "f").use(route, middleware);
473
- }
474
- /**
475
- * Applies the route middleware on a route. Encapsulates the logic
476
- * needed to pass the middleware via the trace calls
477
- */
478
- applyAuthMiddleware(route, actorType, authType, options) {
479
- let authenticateMiddleware = (0, middlewares_1.authenticate)(actorType, authType, options);
480
- if (ApiRoutesLoader.traceMiddleware) {
481
- authenticateMiddleware = ApiRoutesLoader.traceMiddleware(authenticateMiddleware, { route: String(route) });
482
- }
483
- __classPrivateFieldGet(this, _ApiRoutesLoader_router, "f").use(route, authenticateMiddleware);
484
- }
485
- /**
486
- * Apply the route specific middlewares to the router,
487
- * this includes the cors, authentication and
488
- * body parsing. These are applied first to ensure
489
- * that they are applied before any other middleware.
490
- */
491
- applyRouteSpecificMiddlewares() {
492
- const prioritizedRoutes = prioritize([...__classPrivateFieldGet(this, _ApiRoutesLoader_routesMap, "f").values()]);
493
- const handledPaths = new Set();
494
- const middlewarePaths = new Set();
495
- const globalRoutes = __classPrivateFieldGet(this, _ApiRoutesLoader_globalMiddlewaresDescriptor, "f")?.config?.routes ?? [];
496
- for (const route of globalRoutes) {
497
- middlewarePaths.add(route.matcher);
498
- }
499
- for (const descriptor of prioritizedRoutes) {
500
- if (!descriptor.config?.routes?.length) {
501
- continue;
502
- }
503
- const config = descriptor.config;
504
- handledPaths.add(descriptor.route);
505
- if (config.shouldAppendAdminCors) {
506
- applyCors(__classPrivateFieldGet(this, _ApiRoutesLoader_router, "f"), descriptor.route, createCorsOptions(config_1.configManager.config.projectConfig.http.adminCors));
507
- }
508
- if (config.shouldAppendAuthCors) {
509
- applyCors(__classPrivateFieldGet(this, _ApiRoutesLoader_router, "f"), descriptor.route, createCorsOptions(config_1.configManager.config.projectConfig.http.authCors));
510
- }
511
- if (config.shouldAppendStoreCors) {
512
- applyCors(__classPrivateFieldGet(this, _ApiRoutesLoader_router, "f"), descriptor.route, createCorsOptions(config_1.configManager.config.projectConfig.http.storeCors));
513
- }
514
- // Apply other middlewares
515
- if (config.routeType === "store") {
516
- this.applyStorePublishableKeyMiddleware(descriptor.route);
517
- }
518
- // We only apply the auth middleware to store routes to populate the auth context. For actual authentication, users can just reapply the middleware.
519
- if (!config.optedOutOfAuth && config.routeType === "store") {
520
- this.applyAuthMiddleware(descriptor.route, "customer", ["bearer", "session"], {
521
- allowUnauthenticated: true,
522
- });
523
- }
524
- if (!config.optedOutOfAuth && config.routeType === "admin") {
525
- this.applyAuthMiddleware(descriptor.route, "user", [
526
- "bearer",
527
- "session",
528
- "api-key",
529
- ]);
530
- }
531
- for (const route of descriptor.config.routes) {
532
- /**
533
- * Apply the body parser middleware if the route
534
- * has not opted out of it.
535
- */
536
- this.applyBodyParserMiddleware(descriptor.route, route.method);
537
- }
538
- }
539
81
  /**
540
- * Apply CORS and auth middleware for paths defined in global middleware but not already handled by routes.
82
+ * Apply CORS middleware for "/auth" routes
541
83
  */
542
- for (const path of middlewarePaths) {
543
- if (typeof path === "string" && handledPaths.has(path)) {
544
- continue;
84
+ __classPrivateFieldGet(this, _ApiLoader_instances, "m", _ApiLoader_applyCorsMiddleware).call(this, routesFinder, "/auth", "shouldAppendAuthCors", __classPrivateFieldGet(this, _ApiLoader_instances, "m", _ApiLoader_createCorsOptions).call(this, config_1.configManager.config.projectConfig.http.authCors));
85
+ const collectionToSort = []
86
+ .concat(middlewares)
87
+ .concat(routes);
88
+ const sortedRoutes = new routes_sorter_1.RoutesSorter(collectionToSort).sort();
89
+ sortedRoutes.forEach((route) => {
90
+ if ("isRoute" in route) {
91
+ routesFinder.add(route);
545
92
  }
546
- const context = getRouteContext(path);
547
- if (!context) {
548
- continue;
549
- }
550
- switch (context) {
551
- case "admin":
552
- applyCors(__classPrivateFieldGet(this, _ApiRoutesLoader_router, "f"), path, createCorsOptions(config_1.configManager.config.projectConfig.http.adminCors));
553
- this.applyAuthMiddleware(path, "user", [
554
- "bearer",
555
- "session",
556
- "api-key",
557
- ]);
558
- break;
559
- case "store":
560
- applyCors(__classPrivateFieldGet(this, _ApiRoutesLoader_router, "f"), path, createCorsOptions(config_1.configManager.config.projectConfig.http.storeCors));
561
- this.applyStorePublishableKeyMiddleware(path);
562
- break;
563
- case "auth":
564
- applyCors(__classPrivateFieldGet(this, _ApiRoutesLoader_router, "f"), path, createCorsOptions(config_1.configManager.config.projectConfig.http.authCors));
565
- break;
566
- }
567
- }
568
- }
569
- /**
570
- * Apply the error handler middleware to the router
571
- */
572
- applyErrorHandlerMiddleware() {
573
- const middlewareDescriptor = __classPrivateFieldGet(this, _ApiRoutesLoader_globalMiddlewaresDescriptor, "f");
574
- const errorHandlerFn = middlewareDescriptor?.config?.errorHandler;
575
- /**
576
- * If the user has opted out of the error handler then return
577
- */
578
- if (errorHandlerFn === false) {
579
- return;
580
- }
581
- /**
582
- * If the user has provided a custom error handler then use it
583
- */
584
- if (errorHandlerFn) {
585
- __classPrivateFieldGet(this, _ApiRoutesLoader_router, "f").use(errorHandlerFn);
586
- return;
587
- }
588
- /**
589
- * If the user has not provided a custom error handler then use the
590
- * default one.
591
- */
592
- __classPrivateFieldGet(this, _ApiRoutesLoader_router, "f").use((0, middlewares_1.errorHandler)());
593
- }
594
- async registerRoutes() {
595
- const middlewareDescriptor = __classPrivateFieldGet(this, _ApiRoutesLoader_globalMiddlewaresDescriptor, "f");
596
- const shouldWrapHandler = middlewareDescriptor?.config
597
- ? middlewareDescriptor.config.errorHandler !== false
598
- : true;
599
- const prioritizedRoutes = prioritize([...__classPrivateFieldGet(this, _ApiRoutesLoader_routesMap, "f").values()]);
600
- for (const descriptor of prioritizedRoutes) {
601
- if (!descriptor.config?.routes?.length) {
602
- continue;
603
- }
604
- const routes = descriptor.config.routes;
605
- for (const route of routes) {
606
- log({
607
- activityId: __classPrivateFieldGet(this, _ApiRoutesLoader_activityId, "f"),
608
- message: `Registering route [${route.method?.toUpperCase()}] - ${descriptor.route}`,
609
- });
610
- let handler = route.handler;
611
- /**
612
- * Give handler to the trace route handler for instrumentation
613
- * from outside-in.
614
- */
615
- if (ApiRoutesLoader.traceRoute) {
616
- handler = ApiRoutesLoader.traceRoute(handler, {
617
- method: route.method,
618
- route: descriptor.route,
619
- });
620
- }
621
- /**
622
- * If the user hasn't opted out of error handling then
623
- * we wrap the handler in a try/catch block.
624
- */
625
- if (shouldWrapHandler) {
626
- handler = (0, utils_1.wrapHandler)(handler);
627
- }
628
- __classPrivateFieldGet(this, _ApiRoutesLoader_router, "f")[route.method.toLowerCase()](descriptor.route, handler);
629
- }
630
- }
631
- }
632
- async registerMiddlewares() {
633
- const descriptor = __classPrivateFieldGet(this, _ApiRoutesLoader_globalMiddlewaresDescriptor, "f");
634
- if (!descriptor) {
635
- return;
636
- }
637
- if (!descriptor.config?.routes?.length) {
638
- return;
639
- }
640
- const routes = descriptor.config.routes;
93
+ __classPrivateFieldGet(this, _ApiLoader_instances, "m", _ApiLoader_registerExpressHandler).call(this, route);
94
+ });
641
95
  /**
642
- * We don't prioritize the middlewares to preserve the order
643
- * in which they are defined in the 'middlewares.ts'. This is to
644
- * maintain the same behavior as how middleware is applied
645
- * in Express.
96
+ * Registering error handler as the final handler
646
97
  */
647
- for (const route of routes) {
648
- if (!route.middlewares || !route.middlewares.length) {
649
- continue;
650
- }
651
- const methods = (Array.isArray(route.method) ? route.method : [route.method]).filter(Boolean);
652
- for (const method of methods) {
653
- log({
654
- activityId: __classPrivateFieldGet(this, _ApiRoutesLoader_activityId, "f"),
655
- message: `Registering middleware [${method}] - ${route.matcher}`,
656
- });
657
- let middlewares = route.middlewares;
658
- if (ApiRoutesLoader.traceMiddleware) {
659
- middlewares = middlewares.map((middleware) => ApiRoutesLoader.traceMiddleware(middleware, {
660
- route: String(route.matcher),
661
- method,
662
- }));
663
- }
664
- __classPrivateFieldGet(this, _ApiRoutesLoader_router, "f")[method.toLowerCase()](route.matcher, ...middlewares);
665
- }
666
- }
667
- }
668
- async load() {
669
- performance && performance.mark("file-base-routing-start" + __classPrivateFieldGet(this, _ApiRoutesLoader_sourceDir, "f"));
670
- let apiExists = true;
671
- /**
672
- * Since the file based routing does not require a index file
673
- * we can't check if it exists using require. Instead we try
674
- * to read the directory and if it fails we know that the
675
- * directory does not exist.
676
- */
677
- try {
678
- await (0, promises_1.readdir)(__classPrivateFieldGet(this, _ApiRoutesLoader_sourceDir, "f"));
679
- }
680
- catch (_error) {
681
- apiExists = false;
682
- }
683
- if (apiExists) {
684
- await this.createMiddlewaresDescriptor();
685
- await this.createRoutesMap();
686
- await this.createRoutesConfig();
687
- this.applyRouteSpecificMiddlewares();
688
- await this.registerMiddlewares();
689
- await this.registerRoutes();
690
- this.applyErrorHandlerMiddleware();
691
- /**
692
- * Apply the router to the app.
693
- *
694
- * This prevents middleware from a plugin from
695
- * bleeding into the global middleware stack.
696
- */
697
- __classPrivateFieldGet(this, _ApiRoutesLoader_app, "f").use("/", __classPrivateFieldGet(this, _ApiRoutesLoader_router, "f"));
698
- }
699
- performance && performance.mark("file-base-routing-end" + __classPrivateFieldGet(this, _ApiRoutesLoader_sourceDir, "f"));
700
- const timeSpent = performance &&
701
- performance
702
- .measure("file-base-routing-measure" + __classPrivateFieldGet(this, _ApiRoutesLoader_sourceDir, "f"), "file-base-routing-start" + __classPrivateFieldGet(this, _ApiRoutesLoader_sourceDir, "f"), "file-base-routing-end" + __classPrivateFieldGet(this, _ApiRoutesLoader_sourceDir, "f"))
703
- ?.duration?.toFixed(2);
704
- log({
705
- activityId: __classPrivateFieldGet(this, _ApiRoutesLoader_activityId, "f"),
706
- message: `Routes loaded in ${timeSpent} ms`,
707
- });
708
- __classPrivateFieldGet(this, _ApiRoutesLoader_routesMap, "f").clear();
709
- __classPrivateFieldSet(this, _ApiRoutesLoader_globalMiddlewaresDescriptor, undefined, "f");
98
+ __classPrivateFieldGet(this, _ApiLoader_app, "f").use(sourceErrorHandler ?? (0, error_handler_1.errorHandler)());
710
99
  }
711
100
  }
712
- exports.ApiRoutesLoader = ApiRoutesLoader;
713
- _ApiRoutesLoader_routesMap = new WeakMap(), _ApiRoutesLoader_globalMiddlewaresDescriptor = new WeakMap(), _ApiRoutesLoader_app = new WeakMap(), _ApiRoutesLoader_router = new WeakMap(), _ApiRoutesLoader_activityId = new WeakMap(), _ApiRoutesLoader_excludes = new WeakMap(), _ApiRoutesLoader_sourceDir = new WeakMap();
714
- class RoutesLoader {
715
- constructor({ app, activityId, sourceDir, baseRestrictedFields = [], }) {
716
- _RoutesLoader_instances.add(this);
717
- /**
718
- * An express instance
719
- * @private
720
- */
721
- _RoutesLoader_app.set(this, void 0);
722
- /**
723
- * An eventual activity id for information tracking
724
- * @private
725
- */
726
- _RoutesLoader_activityId.set(this, void 0);
727
- /**
728
- * Path from where to load the routes from
729
- * @private
730
- */
731
- _RoutesLoader_sourceDir.set(this, void 0);
732
- __classPrivateFieldSet(this, _RoutesLoader_app, app, "f");
733
- __classPrivateFieldSet(this, _RoutesLoader_activityId, activityId, "f");
734
- __classPrivateFieldSet(this, _RoutesLoader_sourceDir, sourceDir, "f");
735
- __classPrivateFieldGet(this, _RoutesLoader_instances, "m", _RoutesLoader_assignRestrictedFields).call(this, baseRestrictedFields);
101
+ exports.ApiLoader = ApiLoader;
102
+ _a = ApiLoader, _ApiLoader_app = new WeakMap(), _ApiLoader_sourceDirs = new WeakMap(), _ApiLoader_instances = new WeakSet(), _ApiLoader_loadHttpResources =
103
+ /**
104
+ * Loads routes, middleware, bodyParserConfig routes, routes that have
105
+ * opted out for Auth and CORS and the error handler.
106
+ */
107
+ async function _ApiLoader_loadHttpResources() {
108
+ const routesLoader = new routes_loader_1.RoutesLoader();
109
+ const middlewareLoader = new middleware_file_loader_1.MiddlewareFileLoader();
110
+ for (let dir of __classPrivateFieldGet(this, _ApiLoader_sourceDirs, "f")) {
111
+ await routesLoader.scanDir(dir);
112
+ await middlewareLoader.scanDir(dir);
736
113
  }
737
- async load() {
738
- const normalizedSourcePath = Array.isArray(__classPrivateFieldGet(this, _RoutesLoader_sourceDir, "f"))
739
- ? __classPrivateFieldGet(this, _RoutesLoader_sourceDir, "f")
740
- : [__classPrivateFieldGet(this, _RoutesLoader_sourceDir, "f")];
741
- const promises = normalizedSourcePath.map(async (sourcePath) => {
742
- const apiRoutesLoader = new ApiRoutesLoader({
743
- app: __classPrivateFieldGet(this, _RoutesLoader_app, "f"),
744
- activityId: __classPrivateFieldGet(this, _RoutesLoader_activityId, "f"),
745
- sourceDir: sourcePath,
746
- });
747
- await apiRoutesLoader.load();
748
- });
749
- await (0, utils_1.promiseAll)(promises);
114
+ return {
115
+ routes: routesLoader.getRoutes(),
116
+ routesFinder: new routes_finder_1.RoutesFinder(),
117
+ middlewares: middlewareLoader.getMiddlewares(),
118
+ errorHandler: middlewareLoader.getErrorHandler(),
119
+ bodyParserConfigRoutes: middlewareLoader.getBodyParserConfigRoutes(),
120
+ };
121
+ }, _ApiLoader_registerExpressHandler = function _ApiLoader_registerExpressHandler(route) {
122
+ if ("isRoute" in route) {
123
+ reporter_1.default.debug(`registering route ${route.method} ${route.matcher}`);
124
+ const handler = _a.traceRoute
125
+ ? _a.traceRoute(route.handler, {
126
+ route: route.matcher,
127
+ method: route.method,
128
+ })
129
+ : route.handler;
130
+ __classPrivateFieldGet(this, _ApiLoader_app, "f")[route.method.toLowerCase()](route.matcher, (0, wrap_handler_1.wrapHandler)(handler));
131
+ return;
750
132
  }
751
- }
752
- exports.RoutesLoader = RoutesLoader;
753
- _RoutesLoader_app = new WeakMap(), _RoutesLoader_activityId = new WeakMap(), _RoutesLoader_sourceDir = new WeakMap(), _RoutesLoader_instances = new WeakSet(), _RoutesLoader_assignRestrictedFields = function _RoutesLoader_assignRestrictedFields(baseRestrictedFields) {
754
- __classPrivateFieldGet(this, _RoutesLoader_app, "f").use("/store", ((req, _, next) => {
133
+ if (!route.methods) {
134
+ reporter_1.default.debug(`registering global middleware for ${route.matcher}`);
135
+ const handler = _a.traceMiddleware
136
+ ? _a.traceMiddleware(route.handler, {
137
+ route: route.matcher,
138
+ })
139
+ : route.handler;
140
+ __classPrivateFieldGet(this, _ApiLoader_app, "f").use(route.matcher, (0, wrap_handler_1.wrapHandler)(handler));
141
+ return;
142
+ }
143
+ const methods = Array.isArray(route.methods)
144
+ ? route.methods
145
+ : [route.methods];
146
+ methods.forEach((method) => {
147
+ reporter_1.default.debug(`registering route middleware ${method} ${route.matcher}`);
148
+ const handler = _a.traceMiddleware
149
+ ? _a.traceMiddleware((0, wrap_handler_1.wrapHandler)(route.handler), {
150
+ route: route.matcher,
151
+ method: method,
152
+ })
153
+ : (0, wrap_handler_1.wrapHandler)(route.handler);
154
+ __classPrivateFieldGet(this, _ApiLoader_app, "f")[method.toLowerCase()](route.matcher, handler);
155
+ });
156
+ }, _ApiLoader_assignRestrictedFields = function _ApiLoader_assignRestrictedFields(baseRestrictedFields) {
157
+ __classPrivateFieldGet(this, _ApiLoader_app, "f").use("/store", ((req, _, next) => {
755
158
  req.restrictedFields = new restricted_fields_1.RestrictedFields();
756
159
  req.restrictedFields.add(baseRestrictedFields);
757
160
  next();
758
161
  }));
759
- __classPrivateFieldGet(this, _RoutesLoader_app, "f").use("/admin", ((req, _, next) => {
162
+ __classPrivateFieldGet(this, _ApiLoader_app, "f").use("/admin", ((req, _, next) => {
760
163
  req.restrictedFields = new restricted_fields_1.RestrictedFields();
761
164
  next();
762
165
  }));
166
+ }, _ApiLoader_createCorsOptions = function _ApiLoader_createCorsOptions(origin) {
167
+ return {
168
+ origin: (0, utils_1.parseCorsOrigins)(origin),
169
+ credentials: true,
170
+ preflightContinue: false,
171
+ };
172
+ }, _ApiLoader_applyCorsMiddleware = function _ApiLoader_applyCorsMiddleware(routesFinder, namespace, toggleKey, corsOptions) {
173
+ const corsFn = (0, cors_1.default)(corsOptions);
174
+ const corsMiddleware = function corsMiddleware(req, res, next) {
175
+ let method = req.method;
176
+ if (req.method === "OPTIONS") {
177
+ method = req.headers["access-control-request-method"] ?? req.method;
178
+ }
179
+ const path = `${namespace}${req.path}`;
180
+ const matchingRoute = routesFinder.find(path, method);
181
+ if (matchingRoute && matchingRoute[toggleKey] === true) {
182
+ return corsFn(req, res, next);
183
+ }
184
+ reporter_1.default.debug(`Skipping CORS middleware ${req.method} ${path}`);
185
+ return next();
186
+ };
187
+ __classPrivateFieldGet(this, _ApiLoader_app, "f").use(namespace, _a.traceMiddleware
188
+ ? _a.traceMiddleware(corsMiddleware, {
189
+ route: namespace,
190
+ })
191
+ : corsMiddleware);
192
+ }, _ApiLoader_applyAuthMiddleware = function _ApiLoader_applyAuthMiddleware(routesFinder, namespace, actorType, authType, options) {
193
+ reporter_1.default.debug(`Registering auth middleware for prefix ${namespace}`);
194
+ const originalFn = (0, middlewares_1.authenticate)(actorType, authType, options);
195
+ const authMiddleware = function authMiddleware(req, res, next) {
196
+ const path = `${namespace}${req.path}`;
197
+ const matchingRoute = routesFinder.find(path, req.method);
198
+ if (matchingRoute && matchingRoute.optedOutOfAuth) {
199
+ reporter_1.default.debug(`Skipping auth ${req.method} ${path}`);
200
+ return next();
201
+ }
202
+ reporter_1.default.debug(`Authenticating route ${req.method} ${path}`);
203
+ return originalFn(req, res, next);
204
+ };
205
+ __classPrivateFieldGet(this, _ApiLoader_app, "f").use(namespace, _a.traceMiddleware
206
+ ? _a.traceMiddleware(authMiddleware, {
207
+ route: namespace,
208
+ })
209
+ : authMiddleware);
210
+ }, _ApiLoader_applyBodyParserMiddleware = function _ApiLoader_applyBodyParserMiddleware(namespace, routesFinder) {
211
+ reporter_1.default.debug(`Registering bodyparser middleware for prefix ${namespace}`);
212
+ __classPrivateFieldGet(this, _ApiLoader_app, "f").use(namespace, (0, bodyparser_1.createBodyParserMiddlewaresStack)(namespace, routesFinder, _a.traceMiddleware));
213
+ }, _ApiLoader_applyStorePublishableKeyMiddleware = function _ApiLoader_applyStorePublishableKeyMiddleware(namespace) {
214
+ reporter_1.default.debug(`Registering publishable key middleware for namespace ${namespace}`);
215
+ let middleware = _a.traceMiddleware
216
+ ? _a.traceMiddleware(ensure_publishable_api_key_1.ensurePublishableApiKeyMiddleware, {
217
+ route: namespace,
218
+ })
219
+ : ensure_publishable_api_key_1.ensurePublishableApiKeyMiddleware;
220
+ __classPrivateFieldGet(this, _ApiLoader_app, "f").use(namespace, middleware);
763
221
  };
764
222
  //# sourceMappingURL=router.js.map