breaking: remove loading middlewares file in favor of routes-config files (#8274)

This commit is contained in:
Harminder Virk authored and GitHub committed 2024-07-25 12:53:35 +05:30
1 parent 13855e9167
commit 0bd46c97b7
5 files changed
+99 -95

No files matched your search

@@ -1,6 +1,6 @@
import { MiddlewaresConfig } from "../../types"
import { defineRoutesConfig } from "../../../../../utils/define-routes-config"
export const config: MiddlewaresConfig = {
export default defineRoutesConfig({
errorHandler: (err, _req, res, _next) => {
const { code, message } = err
@@ -36,4 +36,4 @@ export const config: MiddlewaresConfig = {
})
}
},
}
})
@@ -1,10 +1,10 @@
import { NextFunction, Request, Response, raw } from "express"
import { MiddlewaresConfig } from "../../types"
import {
customersCreateMiddlewareMock,
customersGlobalMiddlewareMock,
storeGlobalMiddlewareMock,
} from "../mocks"
import { defineRoutesConfig } from "../../../../../utils/define-routes-config"
const customersGlobalMiddleware = (
req: Request,
@@ -29,26 +29,24 @@ const storeGlobal = (req: Request, res: Response, next: NextFunction) => {
next()
}
export const config: MiddlewaresConfig = {
routes: [
{
matcher: "/customers",
middlewares: [customersGlobalMiddleware],
},
{
method: "POST",
matcher: "/customers",
middlewares: [customersCreateMiddleware],
},
{
matcher: "/store/*",
middlewares: [storeGlobal],
},
{
matcher: "/webhooks/*",
method: "POST",
bodyParser: false,
middlewares: [raw({ type: "application/json" })],
},
],
}
export default defineRoutesConfig([
{
matcher: "/customers",
middlewares: [customersGlobalMiddleware],
},
{
method: "POST",
matcher: "/customers",
middlewares: [customersCreateMiddleware],
},
{
matcher: "/store/*",
middlewares: [storeGlobal],
},
{
matcher: "/webhooks/*",
method: "POST",
bodyParser: false,
middlewares: [raw({ type: "application/json" })],
},
])
@@ -49,7 +49,7 @@ const AUTHTHENTICATE = "AUTHENTICATE"
/**
* File name for the global middlewares file
*/
const MIDDLEWARES_NAME = "middlewares"
const ROUTES_CONFIG_FILENAME = "routes-config"
const pathSegmentReplacer = {
"\\[\\.\\.\\.\\]": () => `*`,
@@ -216,7 +216,7 @@ export class RoutesLoader {
if (!config?.routes && !config?.errorHandler) {
log({
activityId: this.activityId,
message: `Empty middleware config. Skipping middleware application.`,
message: `Empty routes config. Skipping configuration.`,
})
return
@@ -419,7 +419,7 @@ export class RoutesLoader {
}) {
const files = await readdir(dirPath)
const middlewareFilePath = files
const routesConfigFiles = files
.filter((path) => {
if (
this.excludes.length &&
@@ -431,29 +431,29 @@ export class RoutesLoader {
return true
})
.find((file) => {
return file.replace(/\.[^/.]+$/, "") === MIDDLEWARES_NAME
return file.replace(/\.[^/.]+$/, "") === ROUTES_CONFIG_FILENAME
})
if (!middlewareFilePath) {
if (!routesConfigFiles) {
log({
activityId: this.activityId,
message: `No middleware files found in ${dirPath}. Skipping middleware configuration.`,
message: `No routes config file found in ${dirPath}. Skipping configuration.`,
})
return
}
const absolutePath = join(dirPath, middlewareFilePath)
const absolutePath = join(dirPath, routesConfigFiles)
try {
await import(absolutePath).then((import_) => {
const middlewaresConfig = import_.config as
const middlewaresConfig = import_.default as
| MiddlewaresConfig
| undefined
if (!middlewaresConfig) {
log({
activityId: this.activityId,
message: `No middleware configuration found in ${absolutePath}. Skipping middleware configuration.`,
message: `No routes config file found in ${absolutePath}. Skipping configuration.`,
})
return
}
@@ -476,7 +476,7 @@ export class RoutesLoader {
} catch (error) {
log({
activityId: this.activityId,
message: `Failed to load middleware configuration in ${absolutePath}. Skipping middleware configuration.`,
message: `Failed to load routes configuration from ${absolutePath}. Skipping configuration.`,
})
return