fix(medusa): Make file based routing compatible with Windows pathing (#5497)

This commit is contained in:
Kasper Fabricius Kristensen
2023-10-31 14:46:24 +01:00
committed by GitHub
parent 4ce8279d25
commit 2548ea8e5e
3 changed files with 19 additions and 11 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---
fix(medusa): Ensures that file based routing is compatible with Windows pathing
@@ -2,7 +2,7 @@ import cors from "cors"
import { Express, json, urlencoded } from "express" import { Express, json, urlencoded } from "express"
import { readdir } from "fs/promises" import { readdir } from "fs/promises"
import { parseCorsOrigins } from "medusa-core-utils" import { parseCorsOrigins } from "medusa-core-utils"
import { extname, join } from "path" import { extname, join, sep } from "path"
import { import {
authenticate, authenticate,
authenticateCustomer, authenticateCustomer,
@@ -95,7 +95,7 @@ export class RoutesLoader {
protected excludes: RegExp[] = [ protected excludes: RegExp[] = [
/\.DS_Store/, /\.DS_Store/,
/(\.ts\.map|\.js\.map|\.d\.ts)/, /(\.ts\.map|\.js\.map|\.d\.ts)/,
/^_/, /^_[^/\\]*(\.[^/\\]+)?$/,
] ]
constructor({ constructor({
@@ -157,7 +157,7 @@ export class RoutesLoader {
* @param route - The route to parse * @param route - The route to parse
* *
* @example * @example
* "/admin/orders/[id]/index.ts" => "/admin/orders/:id/index.ts" * "/admin/orders/[id]/route.ts => "/admin/orders/:id/route.ts"
*/ */
protected parseRoute(route: string): string { protected parseRoute(route: string): string {
let route_ = route let route_ = route
@@ -308,7 +308,7 @@ export class RoutesLoader {
let routeToParse = childPath let routeToParse = childPath
const pathSegments = childPath.split("/") const pathSegments = childPath.split(sep)
const lastSegment = pathSegments[pathSegments.length - 1] const lastSegment = pathSegments[pathSegments.length - 1]
if (lastSegment.startsWith("route")) { if (lastSegment.startsWith("route")) {
@@ -404,8 +404,6 @@ export class RoutesLoader {
await readdir(dirPath, { withFileTypes: true }).then((entries) => { await readdir(dirPath, { withFileTypes: true }).then((entries) => {
return entries return entries
.filter((entry) => { .filter((entry) => {
const fullPath = join(dirPath, entry.name)
if ( if (
this.excludes.length && this.excludes.length &&
this.excludes.some((exclude) => exclude.test(entry.name)) this.excludes.some((exclude) => exclude.test(entry.name))
@@ -413,8 +411,13 @@ export class RoutesLoader {
return false return false
} }
// Get entry name without extension let name = entry.name
const name = entry.name.replace(/\.[^/.]+$/, "")
const extension = extname(name)
if (extension) {
name = name.replace(extension, "")
}
if (entry.isFile() && name !== ROUTE_NAME) { if (entry.isFile() && name !== ROUTE_NAME) {
return false return false
@@ -541,7 +544,7 @@ export class RoutesLoader {
/** /**
* Since the file based routing does not require a index file * Since the file based routing does not require a index file
* we can check if it exists using require. Instead we try * we can't check if it exists using require. Instead we try
* to read the directory and if it fails we know that the * to read the directory and if it fails we know that the
* directory does not exist. * directory does not exist.
*/ */
+2 -2
View File
@@ -38,8 +38,8 @@ import path from "path"
import { EntitySchema } from "typeorm" import { EntitySchema } from "typeorm"
import { MiddlewareService } from "../services" import { MiddlewareService } from "../services"
import { getModelExtensionsMap } from "./helpers/get-model-extension-map" import { getModelExtensionsMap } from "./helpers/get-model-extension-map"
import logger from "./logger"
import { RoutesLoader } from "./helpers/routing" import { RoutesLoader } from "./helpers/routing"
import logger from "./logger"
type Options = { type Options = {
rootDirectory: string rootDirectory: string
@@ -362,7 +362,7 @@ async function registerApi(
*/ */
await new RoutesLoader({ await new RoutesLoader({
app, app,
rootDir: `${pluginDetails.resolve}/api`, rootDir: path.join(pluginDetails.resolve, "api"),
activityId: activityId, activityId: activityId,
configModule: configmodule, configModule: configmodule,
}).load() }).load()