Преглед изворни кода

Merge pull request #54 from medusajs/fix/custom-api

Fix/custom api
Adrien de Peretti пре 2 година
родитељ
комит
ed2471de03

+ 5 - 0
src/api/routes/admin/custom-route-handler.ts

@@ -0,0 +1,5 @@
+import { Request, Response } from 'express'
+
+export default async (req: Request, res: Response): Promise<void> => {
+  res.sendStatus(200)
+}

+ 21 - 0
src/api/routes/admin/index.ts

@@ -0,0 +1,21 @@
+import cors from "cors"
+import { Router } from "express"
+import bodyParser from "body-parser"
+import customRouteHandler from "./custom-route-handler"
+import { authenticate, wrapHandler } from "@medusajs/medusa";
+
+const adminRouter = Router()
+export function getAdminRouter(adminCorsOptions): Router {
+  adminRouter.use(
+    /\/admin\/((?!auth).*)/,
+    cors(adminCorsOptions),
+    bodyParser.json(), authenticate()
+  )
+
+  adminRouter.post(
+    "/my-custom-path",
+    wrapHandler(customRouteHandler)
+  )
+
+  return adminRouter
+}

+ 2 - 2
src/api/routes/store/index.ts

@@ -6,10 +6,10 @@ import { wrapHandler } from "@medusajs/medusa";
 
 const storeRouter = Router()
 export function getStoreRouter(storeCorsOptions): Router {
-  storeRouter.use(cors(storeCorsOptions), bodyParser.json())
+  storeRouter.use("/store", cors(storeCorsOptions), bodyParser.json())
 
   storeRouter.post(
-    "/store/my-custom-path",
+    "/my-custom-path",
     wrapHandler(customRouteHandler)
   )