prettier fix

This commit is contained in:
Danel Kungla
2025-09-19 17:22:36 +03:00
parent efa94b3322
commit 0c2cfe6d18
509 changed files with 17988 additions and 9920 deletions

View File

@@ -1,9 +1,10 @@
import { HttpTypes } from "@medusajs/types";
import { NextRequest, NextResponse } from "next/server";
import { NextRequest, NextResponse } from 'next/server';
import { HttpTypes } from '@medusajs/types';
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 || "ee";
const DEFAULT_REGION = process.env.NEXT_PUBLIC_DEFAULT_REGION || 'ee';
const regionMapCache = {
regionMap: new Map<string, HttpTypes.StoreRegion>(),
@@ -15,7 +16,7 @@ async function getRegionMap(cacheId: string) {
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."
'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.',
);
}
@@ -26,13 +27,13 @@ async function getRegionMap(cacheId: string) {
// 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: {
"x-publishable-api-key": PUBLISHABLE_API_KEY!,
'x-publishable-api-key': PUBLISHABLE_API_KEY!,
},
next: {
revalidate: 3600,
tags: [`regions-${cacheId}`],
},
cache: "force-cache",
cache: 'force-cache',
}).then(async (response) => {
const json = await response.json();
@@ -45,14 +46,14 @@ async function getRegionMap(cacheId: string) {
if (!regions?.length) {
throw new Error(
"No regions found. Please set up regions in your Medusa Admin."
'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);
});
});
@@ -69,17 +70,17 @@ async function getRegionMap(cacheId: string) {
*/
async function getCountryCode(
request: NextRequest,
regionMap: Map<string, HttpTypes.StoreRegion | number>
regionMap: Map<string, HttpTypes.StoreRegion | number>,
) {
try {
let countryCode;
const vercelCountryCode = request.headers
.get("x-vercel-ip-country")
.get('x-vercel-ip-country')
?.toLowerCase();
const urlCountryCode = request.nextUrl.pathname
.split("/")[1]
.split('/')[1]
?.toLowerCase();
if (urlCountryCode && regionMap.has(urlCountryCode)) {
@@ -94,9 +95,9 @@ async function getCountryCode(
return countryCode;
} catch (error) {
if (process.env.NODE_ENV === "development") {
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."
'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.',
);
}
}
@@ -110,7 +111,7 @@ export async function middleware(request: NextRequest) {
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();
@@ -118,7 +119,7 @@ export async function middleware(request: NextRequest) {
try {
regionMap = await getRegionMap(cacheId);
} catch (error) {
console.error("Error fetching regions", error);
console.error('Error fetching regions', error);
return {
redirect: {
destination: '/auth/sign-in',
@@ -130,7 +131,8 @@ export async function middleware(request: NextRequest) {
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) {
@@ -139,7 +141,7 @@ export async function middleware(request: NextRequest) {
// 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, {
response.cookies.set('_medusa_cache_id', cacheId, {
maxAge: 60 * 60 * 24,
});
@@ -147,14 +149,14 @@ export async function middleware(request: NextRequest) {
}
// check if the url is a static asset
if (request.nextUrl.pathname.includes(".")) {
if (request.nextUrl.pathname.includes('.')) {
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) {
@@ -167,6 +169,6 @@ export async function middleware(request: NextRequest) {
export const config = {
matcher: [
"/((?!api|_next/static|_next/image|favicon.ico|images|assets|png|svg|jpg|jpeg|gif|webp).*)",
'/((?!api|_next/static|_next/image|favicon.ico|images|assets|png|svg|jpg|jpeg|gif|webp).*)',
],
};