Explorar o código

Merge pull request #56 from medusajs/feat/improve-api-example

feat: Improve API customisation example
Adrien de Peretti %!s(int64=2) %!d(string=hai) anos
pai
achega
724ecc3010
Modificáronse 5 ficheiros con 1223 adicións e 47 borrados
  1. 1 0
      package.json
  2. 49 4
      src/api/index.ts
  3. 10 17
      src/api/routes/admin/index.ts
  4. 9 12
      src/api/routes/store/index.ts
  5. 1154 14
      yarn.lock

+ 1 - 0
package.json

@@ -24,6 +24,7 @@
     "build:admin": "cross-env medusa-admin build"
   },
   "dependencies": {
+    "@medusajs/admin": "^6.0.0",
     "@medusajs/cache-inmemory": "^1.8.7",
     "@medusajs/cache-redis": "^1.8.7",
     "@medusajs/event-bus-local": "^1.9.4",

+ 49 - 4
src/api/index.ts

@@ -1,6 +1,51 @@
-import { Router } from "express"
+import { Router } from "express";
+import cors from "cors";
+import bodyParser from "body-parser";
+import { authenticate, ConfigModule } from "@medusajs/medusa";
+import { getConfigFile } from "medusa-core-utils";
+import { attachStoreRoutes } from "./routes/store";
+import { attachAdminRoutes } from "./routes/admin";
 
 export default (rootDirectory: string): Router | Router[] => {
-  // add your custom routes here
-  return []
-}
+  // Read currently-loaded medusa config
+  const { configModule } = getConfigFile<ConfigModule>(
+    rootDirectory,
+    "medusa-config"
+  );
+  const { projectConfig } = configModule;
+
+  // Set up our CORS options objects, based on config
+  const storeCorsOptions = {
+    origin: projectConfig.store_cors.split(","),
+    credentials: true,
+  };
+
+  const adminCorsOptions = {
+    origin: projectConfig.admin_cors.split(","),
+    credentials: true,
+  };
+
+  // Set up express router
+  const router = Router();
+
+  // Set up root routes for store and admin endpoints, with appropriate CORS settings
+  router.use("/store", cors(storeCorsOptions), bodyParser.json());
+  router.use("/admin", cors(adminCorsOptions), bodyParser.json());
+
+  // Add authentication to all admin routes *except* auth and account invite ones
+  router.use(/\/admin\/((?!auth)(?!invites).*)/, authenticate());
+
+  // Set up routers for store and admin endpoints
+  const storeRouter = Router();
+  const adminRouter = Router();
+
+  // Attach these routers to the root routes
+  router.use("/store", storeRouter);
+  router.use("/admin", adminRouter);
+
+  // Attach custom routes to these routers
+  attachStoreRoutes(storeRouter);
+  attachAdminRoutes(adminRouter);
+
+  return router;
+};

+ 10 - 17
src/api/routes/admin/index.ts

@@ -1,21 +1,14 @@
-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";
+import { Router } from "express";
+import customRouteHandler from "./custom-route-handler";
+import { wrapHandler } from "@medusajs/medusa";
 
-const adminRouter = Router()
-export function getAdminRouter(adminCorsOptions): Router {
-  adminRouter.use(
-    /\/admin\/((?!auth).*)/,
-    cors(adminCorsOptions),
-    bodyParser.json(), authenticate()
-  )
+// Initialize a custom router
+const router = Router();
 
-  adminRouter.post(
-    "/my-custom-path",
-    wrapHandler(customRouteHandler)
-  )
+export function attachAdminRoutes(adminRouter: Router) {
+  // Attach our router to a custom path on the admin router
+  adminRouter.use("/custom", router);
 
-  return adminRouter
+  // Define a GET endpoint on the root route of our custom path
+  router.get("/", wrapHandler(customRouteHandler));
 }

+ 9 - 12
src/api/routes/store/index.ts

@@ -1,17 +1,14 @@
-import cors from "cors"
-import { Router } from "express"
-import bodyParser from "body-parser"
-import customRouteHandler from "./custom-route-handler"
+import { Router } from "express";
+import customRouteHandler from "./custom-route-handler";
 import { wrapHandler } from "@medusajs/medusa";
 
-const storeRouter = Router()
-export function getStoreRouter(storeCorsOptions): Router {
-  storeRouter.use("/store", cors(storeCorsOptions), bodyParser.json())
+// Initialize a custom router
+const router = Router();
 
-  storeRouter.post(
-    "/my-custom-path",
-    wrapHandler(customRouteHandler)
-  )
+export function attachStoreRoutes(storeRouter: Router) {
+  // Attach our router to a custom path on the store router
+  storeRouter.use("/custom", router);
 
-  return storeRouter
+  // Define a GET endpoint on the root route of our custom path
+  router.get("/", wrapHandler(customRouteHandler));
 }

A diferenza do arquivo foi suprimida porque é demasiado grande
+ 1154 - 14
yarn.lock


Algúns arquivos non se mostraron porque demasiados arquivos cambiaron neste cambio