yangku
yangku
NNuxt
Created by yangku on 2/19/2025 in #❓・help
useRequestHeaders returned undefined after deployed to the server.
I tried to create a wrapper for every fetch request on my project like this
// @/service/config.ts
const getAPIRequestConfig = (): RequestConfigType => {
const config = useRuntimeConfig()
const { cookie } = useRequestHeaders(['cookie'])

const API_BASE_URL = import.meta.server
? config.baseUrl
: config.public.baseUrl
const AUTHORIZATION = import.meta.server
? config.authorization
: config.public.authorization

return {
baseURL: API_BASE_URL,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json; charset=utf-8',
authorization: AUTHORIZATION,
cookie
},
server: true
}
}

export default getAPIRequestConfig
// @/service/config.ts
const getAPIRequestConfig = (): RequestConfigType => {
const config = useRuntimeConfig()
const { cookie } = useRequestHeaders(['cookie'])

const API_BASE_URL = import.meta.server
? config.baseUrl
: config.public.baseUrl
const AUTHORIZATION = import.meta.server
? config.authorization
: config.public.authorization

return {
baseURL: API_BASE_URL,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json; charset=utf-8',
authorization: AUTHORIZATION,
cookie
},
server: true
}
}

export default getAPIRequestConfig
---
// @/service/makeRequest.ts
import getAPIRequestConfig from './config.ts'

const makeSSRRequest = async (
method: keyof APIMethodType,
url: string,
options?: APIOptions
): Promise<any> => {
try {
const {
status,
data: { value: response },
execute
} = await useFetch(url, {
...getAPIRequestConfig(),
method,
...options,
onResponseError: ({ response }) => {
if (response.status === 401) {
// do smt
}
}
})

return { status, response, execute }
} catch (err: any) {
// do smth

return false
}
}

export default makeSSRRequest
// @/service/makeRequest.ts
import getAPIRequestConfig from './config.ts'

const makeSSRRequest = async (
method: keyof APIMethodType,
url: string,
options?: APIOptions
): Promise<any> => {
try {
const {
status,
data: { value: response },
execute
} = await useFetch(url, {
...getAPIRequestConfig(),
method,
...options,
onResponseError: ({ response }) => {
if (response.status === 401) {
// do smt
}
}
})

return { status, response, execute }
} catch (err: any) {
// do smth

return false
}
}

export default makeSSRRequest
---
// @/service/endpoints/endpointsSample.ts
import makeRequest from '../makeRequest.ts'

const endpointsSample = {
getSomething: () => makeRequest('GET', '/url-sample/'),
postSomething: () => makeRequest('POST', '/url-sample/'
}

export default endpointsSample
// @/service/endpoints/endpointsSample.ts
import makeRequest from '../makeRequest.ts'

const endpointsSample = {
getSomething: () => makeRequest('GET', '/url-sample/'),
postSomething: () => makeRequest('POST', '/url-sample/'
}

export default endpointsSample
---
// @/pages/something.vue
import endpointsSample from '@/service/endpoints/endpointsSample'

const { data: dataResponse } = await endpointsSample.getSomething()
// @/pages/something.vue
import endpointsSample from '@/service/endpoints/endpointsSample'

const { data: dataResponse } = await endpointsSample.getSomething()
worked just fine during local development, the cookies also attached to the request header, but after I deployed it to the server it returned undefined, why? thx
20 replies