From 82d774b3950092a0a8f16f4480ec97695b9048e9 Mon Sep 17 00:00:00 2001 From: Shahed Nasser Date: Wed, 26 Mar 2025 13:28:23 +0200 Subject: [PATCH] docs-util: add a prefix for JS SDK examples in OAS (#11989) --- .../src/classes/examples/oas.ts | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/www/utils/packages/docs-generator/src/classes/examples/oas.ts b/www/utils/packages/docs-generator/src/classes/examples/oas.ts index e7d38b9ccf..b0264ff5e3 100644 --- a/www/utils/packages/docs-generator/src/classes/examples/oas.ts +++ b/www/utils/packages/docs-generator/src/classes/examples/oas.ts @@ -7,6 +7,35 @@ import { readFileSync } from "fs" type CodeSampleData = Omit +const JS_SDK_PREFIX = { + store: `import Medusa from "@medusajs/js-sdk" + +let MEDUSA_BACKEND_URL = "http://localhost:9000" + +if (process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL) { + MEDUSA_BACKEND_URL = process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL +} + +export const sdk = new Medusa({ + baseUrl: MEDUSA_BACKEND_URL, + debug: process.env.NODE_ENV === "development", + publishableKey: process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY, +}) + +`, + admin: `import Medusa from "@medusajs/js-sdk" + +export const sdk = new Medusa({ + baseUrl: import.meta.env.VITE_BACKEND_URL || "/", + debug: import.meta.env.DEV, + auth: { + type: "session", + }, +}) + +`, +} + /** * This class generates examples for OAS. */ @@ -50,7 +79,9 @@ class OasExamplesGenerator { if (!matchingRouteKey || !this.routeExamples[matchingRouteKey]["js-sdk"]) { return "" } - return this.routeExamples[matchingRouteKey]["js-sdk"] + + const area = path.startsWith("/store") ? "store" : "admin" + return `${JS_SDK_PREFIX[area]}${this.routeExamples[matchingRouteKey]["js-sdk"]}` } /**