사용한 로직
/**
* 정부혜택 서비스 관련 API
*/
export class PublicService {
/**
* @param {string} authkey - data.go.kr 인증 키
*/
constructor(authkey) {
this.authkey = authkey;
}
async getList(opts) {
const { startDate, endDate, page=1, perPage=9, organ } = opts;
let conds = [];
if(startDate) {
const strDate = DateUtil.format('yyyyMMdd', startDate);
conds.push(encodeURIComponent('cond[등록일시::GTE]')+`=${strDate}`);
} else {
const date = new Date();
date.setFullYear(date.getFullYear() - 1);
date.setMonth(0);
date.setDate(1);
const strDate = DateUtil.format('yyyyMMdd', date);
conds.push(encodeURIComponent('cond[등록일시::GTE]')+`=${strDate}`);
}
if(endDate) {
const strDate = DateUtil.format('yyyyMMdd', endDate);
conds.push(encodeURIComponent('cond[등록일시::LTE]')+`=${strDate}`);
}
if(organ) {
conds.push(encodeURIComponent('cond[소관기관유형::LIKE]')+`=${organ}`);
}
const cond = conds.join('&');
const fetchUrl = 'https://api.odcloud.kr/api/gov24/v3/serviceList';
const url = `${fetchUrl}?serviceKey=${this.authkey}&page=${page}&perPage=${perPage}&${cond}`;
const res = await fetch(url, {
headers: {
'accept': 'application/json'
}
});
if (!res.ok) {
throw new Error('API 응답 실패');
}
return await res.json();
}
}