Explorar el Código

Clean up naming slightly, add comments

Rares Capilnar hace 2 años
padre
commit
dd304adcec
Se han modificado 4 ficheros con 1209 adiciones y 59 borrados
  1. 35 27
      src/api/index.ts
  2. 10 9
      src/api/routes/admin/index.ts
  3. 10 9
      src/api/routes/store/index.ts
  4. 1154 14
      yarn.lock

+ 35 - 27
src/api/index.ts

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

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

@@ -1,13 +1,14 @@
-import { Router } from "express"
-import customRouteHandler from "./custom-route-handler"
+import { Router } from "express";
+import customRouteHandler from "./custom-route-handler";
 import { wrapHandler } from "@medusajs/medusa";
 
-const adminRouter = Router()
-export function attachAdminRouter(app: Router) {
-  app.use("/custom", adminRouter)
+// Initialize a custom router
+const router = Router();
 
-  adminRouter.get(
-    "/",
-    wrapHandler(customRouteHandler)
-  )
+export function attachAdminRoutes(adminRouter: Router) {
+  // Attach our router to a custom path on the admin router
+  adminRouter.use("/custom", router);
+
+  // Define a GET endpoint on the root route of our custom path
+  router.get("/", wrapHandler(customRouteHandler));
 }

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

@@ -1,13 +1,14 @@
-import { Router } from "express"
-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 attachStoreRouter(app: Router) {
-  app.use("/custom", storeRouter)
+// Initialize a custom router
+const router = Router();
 
-  storeRouter.get(
-    "/",
-    wrapHandler(customRouteHandler)
-  )
+export function attachStoreRoutes(storeRouter: Router) {
+  // Attach our router to a custom path on the store router
+  storeRouter.use("/custom", router);
+
+  // Define a GET endpoint on the root route of our custom path
+  router.get("/", wrapHandler(customRouteHandler));
 }

La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 1154 - 14
yarn.lock


Algunos archivos no se mostraron porque demasiados archivos cambiaron en este cambio