fetchers updated
This commit is contained in:
@@ -1,7 +1,75 @@
|
||||
"use server";
|
||||
import { DEFAULT_RESPONSE, defaultHeaders, DEFAULT_TIMEOUT } from "./base";
|
||||
import {
|
||||
DEFAULT_RESPONSE,
|
||||
defaultHeaders,
|
||||
DEFAULT_TIMEOUT,
|
||||
nextCrypto,
|
||||
} from "./base";
|
||||
import { FetchOptions, HttpMethod, ApiResponse } from "./types";
|
||||
import { retrieveAccessToken } from "./mutual/cookies/token";
|
||||
import { cookies } from "next/headers";
|
||||
import { cookieObject } from "@/fetchers/base";
|
||||
|
||||
/**
|
||||
* Retrieves user selection from cookies with graceful fallback
|
||||
* @returns User selection object or default selection if not found
|
||||
*/
|
||||
const functionRetrieveUserSelection = async () => {
|
||||
try {
|
||||
const cookieStore = await cookies();
|
||||
const encrpytUserSelection = cookieStore.get("eys-sel")?.value || "";
|
||||
if (!encrpytUserSelection) {
|
||||
return {
|
||||
redisKey: "default",
|
||||
uuid: "",
|
||||
timestamp: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
try {
|
||||
const decrpytUserSelection = await nextCrypto.decrypt(
|
||||
encrpytUserSelection
|
||||
);
|
||||
if (!decrpytUserSelection) {
|
||||
return {
|
||||
redisKey: "default",
|
||||
uuid: "",
|
||||
timestamp: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
return JSON.parse(decrpytUserSelection);
|
||||
} catch (decryptError) {
|
||||
return {
|
||||
redisKey: "default",
|
||||
uuid: "",
|
||||
timestamp: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
redisKey: "default",
|
||||
uuid: "",
|
||||
timestamp: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
const functionSetUserSelection = async (userSelection: any) => {
|
||||
const cookieStore = await cookies();
|
||||
const encrpytUserSelection = await nextCrypto.encrypt(
|
||||
JSON.stringify(userSelection)
|
||||
);
|
||||
if (!encrpytUserSelection) throw new Error("No user selection found");
|
||||
cookieStore.set({
|
||||
name: "eys-sel",
|
||||
value: encrpytUserSelection,
|
||||
...cookieObject,
|
||||
} as any);
|
||||
};
|
||||
|
||||
const functionRemoveUserSelection = async () => {
|
||||
const cookieStore = await cookies();
|
||||
cookieStore.delete("eys-sel");
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a promise that rejects after a specified timeout
|
||||
@@ -124,4 +192,11 @@ async function updateDataWithToken<T>(
|
||||
);
|
||||
}
|
||||
|
||||
export { fetchData, fetchDataWithToken, updateDataWithToken };
|
||||
export {
|
||||
fetchData,
|
||||
fetchDataWithToken,
|
||||
updateDataWithToken,
|
||||
functionRetrieveUserSelection,
|
||||
functionSetUserSelection,
|
||||
functionRemoveUserSelection,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user