문제 해결
예시 코드
//api.js
const jejuStoreUrl = import.meta.env.VITE_API_JEJU_STORE_URL;
const jejuStoreDecodingKey = import.meta.env.VITE_API_JEJU_STORE_DECODING_KEY;
const jejuStoreEncodingKey = import.meta.env.VITE_API_JEJU_STORE_ENCODING_KEY;
export const jejuStoreAPIDecoding = axios.create({
baseURL: jejuStoreUrl,
params: { serviceKey: jejuStoreDecodingKey, numOfRows: 11630, pageNo: 2 }
});
export const jejuStoreAPIEncoding = axios.create({
baseURL: jejuStoreUrl,
params: { serviceKey: jejuStoreEncodingKey, numOfRows: 11630, pageNo: 2 }
});
//jejuStore.api.js
export async function getJejuStores() {
const path = '/getGoodPirceStoreList';
try {
const response = await jejuStoreAPIEncoding.get(path);
const responseDataDecoding = response.data?.response?.body.items;
if (!responseDataDecoding) {
console.log('encoding key 실패, decoding key 시도');
try {
const responseEncoding = await jejuStoreAPIDecoding.get(path);
const responseDataEncoding = responseEncoding.data?.response?.body.items;
return responseDataEncoding;
} catch (encodingError) {
console.log('encoding key 에러:', encodingError);
throw encodingError;
}
}
return responseDataDecoding;
} catch (decodingError) {
console.log('decoding key 에러 :', decodingError);
}
}