Open
Description
Hello, I have used KY in a project where I defined a single api file and handled most of the CRUD calls in there, also added token to the header which is great, everything works fine but when I want to pass down some nextjs cache options to handle caching I get a bit of headache.
Here is a simple flow of my app;
Example Server Action;
export const testFetch = authActionClient
.schema(testSchema)
.action(async ({ parsedInput }) => {
const url = `v1/TestEndPoint/${parsedInput.testInput}`
const res = await parametreService.testService(url, {cache:"force-cache"})
return {
success: true,
data: res.data,
notification: res.notification,
}
})
Example parametre service;
async testService(url: string, options: any) {
const res = await api.get(url,options)
if (!res.success) {
throw new ApiError(res.notification)
}
return res
}
From the above parametre service, I make use of api.get;
Here is the api.ts, I am excluding some stuff for brevity;
const createInstance = async () => {
const session = await auth()
const token = session?.accessToken
return ky.create({
prefixUrl: process.env.API_URL,
headers: token ? { Authorization: `Bearer ${token}` } : {},
hooks: {
beforeRequest: [
request => {
console.log('req header', request.headers)
},
],
afterResponse: [
async (_request, _options, response) => {
if (!response.ok) {
const errorData = (await response.json()) as Record<string, unknown>
},
],
},
retry: {
limit: 2,
methods: ['get', 'post', 'put', 'patch', 'delete'],
statusCodes: [408, 413, 429, 500, 502, 503, 504],
},
})
}
const performRequest = async <T>(
method: 'get' | 'post' | 'put' | 'patch' | 'delete',
url: string,
options,
): => {
try {
const instance = await createInstance()
const response = await instance[method](url, options)
data = await response.json()
return {
success: true,
data: data as T,
notification: {
type: 'success',
message: data.message
},
}
}
export const api = {
get: <T>(url: string, options?: KyOptions) =>
performRequest<T>('get', url, options),
post: <T>(url: string, options?: KyOptions) =>
performRequest<T>('post', url, options),
put: <T>(url: string, options?: KyOptions) =>
performRequest<T>('put', url, options),
patch: <T>(url: string, options?: KyOptions) =>
performRequest<T>('patch', url, options),
delete: <T>(url: string, options?: KyOptions) =>
performRequest<T>('delete', url, options),
}
I tried passing options parametres, I can see it is passing, I can see options as {cache: "force-cache"}, and I event tried {next: cache: "force-cache"} but I could not change any caching headers in the production, it is always the same.
Any insight how can I make it work?
Metadata
Metadata
Assignees
Labels
No labels