add medreport schema

This commit is contained in:
Danel Kungla
2025-07-09 13:31:37 +03:00
parent 9371ff7710
commit d9198a8a12
73 changed files with 420 additions and 270 deletions

View File

@@ -1,29 +1,28 @@
import { HttpTypes } from "@medusajs/types"
import { NextRequest, NextResponse } from "next/server"
import { HttpTypes } from "@medusajs/types";
import { NextRequest, NextResponse } from "next/server";
const BACKEND_URL = process.env.MEDUSA_BACKEND_URL
const PUBLISHABLE_API_KEY = process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY
const DEFAULT_REGION = process.env.NEXT_PUBLIC_DEFAULT_REGION || "us"
const BACKEND_URL = process.env.MEDUSA_BACKEND_URL;
const PUBLISHABLE_API_KEY = process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY;
const DEFAULT_REGION = process.env.NEXT_PUBLIC_DEFAULT_REGION || "us";
const regionMapCache = {
regionMap: new Map<string, HttpTypes.StoreRegion>(),
regionMapUpdated: Date.now(),
}
};
async function getRegionMap(cacheId: string) {
const { regionMap, regionMapUpdated } = regionMapCache
const { regionMap, regionMapUpdated } = regionMapCache;
if (!BACKEND_URL) {
throw new Error(
"Middleware.ts: Error fetching regions. Did you set up regions in your Medusa Admin and define a MEDUSA_BACKEND_URL environment variable? Note that the variable is no longer named NEXT_PUBLIC_MEDUSA_BACKEND_URL."
)
);
}
if (
!regionMap.keys().next().value ||
regionMapUpdated < Date.now() - 3600 * 1000
) {
console.log("PUBLISHABLE_API_KEY", PUBLISHABLE_API_KEY)
// Fetch regions from Medusa. We can't use the JS client here because middleware is running on Edge and the client needs a Node environment.
const { regions } = await fetch(`${BACKEND_URL}/store/regions`, {
headers: {
@@ -35,32 +34,32 @@ async function getRegionMap(cacheId: string) {
},
cache: "force-cache",
}).then(async (response) => {
const json = await response.json()
const json = await response.json();
if (!response.ok) {
throw new Error(json.message)
throw new Error(json.message);
}
return json
})
return json;
});
if (!regions?.length) {
throw new Error(
"No regions found. Please set up regions in your Medusa Admin."
)
);
}
// Create a map of country codes to regions.
regions.forEach((region: HttpTypes.StoreRegion) => {
region.countries?.forEach((c) => {
regionMapCache.regionMap.set(c.iso_2 ?? "", region)
})
})
regionMapCache.regionMap.set(c.iso_2 ?? "", region);
});
});
regionMapCache.regionMapUpdated = Date.now()
regionMapCache.regionMapUpdated = Date.now();
}
return regionMapCache.regionMap
return regionMapCache.regionMap;
}
/**
@@ -73,30 +72,32 @@ async function getCountryCode(
regionMap: Map<string, HttpTypes.StoreRegion | number>
) {
try {
let countryCode
let countryCode;
const vercelCountryCode = request.headers
.get("x-vercel-ip-country")
?.toLowerCase()
?.toLowerCase();
const urlCountryCode = request.nextUrl.pathname.split("/")[1]?.toLowerCase()
const urlCountryCode = request.nextUrl.pathname
.split("/")[1]
?.toLowerCase();
if (urlCountryCode && regionMap.has(urlCountryCode)) {
countryCode = urlCountryCode
countryCode = urlCountryCode;
} else if (vercelCountryCode && regionMap.has(vercelCountryCode)) {
countryCode = vercelCountryCode
countryCode = vercelCountryCode;
} else if (regionMap.has(DEFAULT_REGION)) {
countryCode = DEFAULT_REGION
countryCode = DEFAULT_REGION;
} else if (regionMap.keys().next().value) {
countryCode = regionMap.keys().next().value
countryCode = regionMap.keys().next().value;
}
return countryCode
return countryCode;
} catch (error) {
if (process.env.NODE_ENV === "development") {
console.error(
"Middleware.ts: Error getting the country code. Did you set up regions in your Medusa Admin and define a MEDUSA_BACKEND_URL environment variable? Note that the variable is no longer named NEXT_PUBLIC_MEDUSA_BACKEND_URL."
)
);
}
}
}
@@ -105,56 +106,56 @@ async function getCountryCode(
* Middleware to handle region selection and onboarding status.
*/
export async function middleware(request: NextRequest) {
let redirectUrl = request.nextUrl.href
let redirectUrl = request.nextUrl.href;
let response = NextResponse.redirect(redirectUrl, 307)
let response = NextResponse.redirect(redirectUrl, 307);
let cacheIdCookie = request.cookies.get("_medusa_cache_id")
let cacheIdCookie = request.cookies.get("_medusa_cache_id");
let cacheId = cacheIdCookie?.value || crypto.randomUUID()
let cacheId = cacheIdCookie?.value || crypto.randomUUID();
const regionMap = await getRegionMap(cacheId)
const regionMap = await getRegionMap(cacheId);
const countryCode = regionMap && (await getCountryCode(request, regionMap))
const countryCode = regionMap && (await getCountryCode(request, regionMap));
const urlHasCountryCode =
countryCode && request.nextUrl.pathname.split("/")[1].includes(countryCode)
countryCode && request.nextUrl.pathname.split("/")[1].includes(countryCode);
// if one of the country codes is in the url and the cache id is set, return next
if (urlHasCountryCode && cacheIdCookie) {
return NextResponse.next()
return NextResponse.next();
}
// if one of the country codes is in the url and the cache id is not set, set the cache id and redirect
if (urlHasCountryCode && !cacheIdCookie) {
response.cookies.set("_medusa_cache_id", cacheId, {
maxAge: 60 * 60 * 24,
})
});
return response
return response;
}
// check if the url is a static asset
if (request.nextUrl.pathname.includes(".")) {
return NextResponse.next()
return NextResponse.next();
}
const redirectPath =
request.nextUrl.pathname === "/" ? "" : request.nextUrl.pathname
request.nextUrl.pathname === "/" ? "" : request.nextUrl.pathname;
const queryString = request.nextUrl.search ? request.nextUrl.search : ""
const queryString = request.nextUrl.search ? request.nextUrl.search : "";
// If no country code is set, we redirect to the relevant region.
if (!urlHasCountryCode && countryCode) {
redirectUrl = `${request.nextUrl.origin}/${countryCode}${redirectPath}${queryString}`
response = NextResponse.redirect(`${redirectUrl}`, 307)
redirectUrl = `${request.nextUrl.origin}/${countryCode}${redirectPath}${queryString}`;
response = NextResponse.redirect(`${redirectUrl}`, 307);
}
return response
return response;
}
export const config = {
matcher: [
"/((?!api|_next/static|_next/image|favicon.ico|images|assets|png|svg|jpg|jpeg|gif|webp).*)",
],
}
};