currency.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. import type { NextRequest } from "next/server";
  2. import type { NextResponse } from "next/server";
  3. import {CURRENT_CURRENCY,CURRENT_LOCAL,CURRENT_CHANNEL} from "@/utils/constants";
  4. import {currencyCookieOptions} from "@/utils/constants/server";
  5. export function handleCurrency(
  6. request: NextRequest,
  7. response: NextResponse
  8. ) {
  9. const currency = request.cookies.get(CURRENT_CURRENCY);
  10. const local = request.cookies.get(CURRENT_LOCAL);
  11. const channel = request.cookies.get(CURRENT_CHANNEL);
  12. const defaultCurrency = process.env.DEFAULT_CURRENCY;
  13. const defaultLocal = process.env.DEFAULT_LOCAL;
  14. const defaultChannel = process.env.DEFAULT_CHANNEL;
  15. if (!currency && defaultCurrency) {
  16. response.cookies.set(CURRENT_CURRENCY,defaultCurrency,currencyCookieOptions);
  17. }
  18. if (!local && defaultLocal) {
  19. response.cookies.set(CURRENT_LOCAL,defaultLocal,currencyCookieOptions);
  20. }
  21. if (!channel && defaultChannel) {
  22. response.cookies.set(CURRENT_CHANNEL,defaultChannel,currencyCookieOptions);
  23. }
  24. return response;
  25. }