UNPKG

holidays-kr

Version:

data.go.kr 데이터 기반 공휴일 수집

72 lines 3.57 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getHolidaysByMonthCount = void 0; const axios_1 = __importDefault(require("axios")); const fast_xml_parser_1 = require("fast-xml-parser"); const config_1 = require("./config"); const utils_1 = require("./utils"); const getHolidaysByMonth = (year, month) => __awaiter(void 0, void 0, void 0, function* () { var _a, _b, _c; const { data } = yield axios_1.default.get(config_1.ENDPOINT, { params: { solYear: year, solMonth: (0, utils_1.zerofill)(month, 2), ServiceKey: config_1.config.serviceKey, }, }); if (!fast_xml_parser_1.XMLValidator.validate(data)) { throw new Error("XML 파싱에 실패앴습니다."); } const parser = new fast_xml_parser_1.XMLParser(); const parsedXml = parser.parse(data); if (parsedXml.OpenAPI_ServiceResponse && parsedXml.OpenAPI_ServiceResponse.cmmMsgHeader) { const header = parsedXml.OpenAPI_ServiceResponse.cmmMsgHeader; throw new Error(header.returnAuthMsg || header.errMsg || header.returnReasonCode); } if (!((_c = (_b = (_a = data.response) === null || _a === void 0 ? void 0 : _a.body) === null || _b === void 0 ? void 0 : _b.items) === null || _c === void 0 ? void 0 : _c.item)) { throw new Error("응답받은 포멧이 정상적이지 않습니다."); } const responseData = data; const { items: { item }, } = responseData.response.body; const holidays = []; const itemList = Array.isArray(item) ? item : [item]; itemList.forEach((item) => { (0, utils_1.addDay)(item, holidays); }); return holidays; }); const getHolidaysByMonthCount = (year, month, monthCount = 1) => __awaiter(void 0, void 0, void 0, function* () { const promiseList = []; if (monthCount > 13) { throw Error("최대 12개월을 넘을 수 없습니다. 어차피 실제 입력된 데이터도 1년을 넘지 않습니다."); } for (let i = 0; i < monthCount; i++) { const targetYear = year + Math.floor((month + i - 1) / 12); const targetMonth = (month + i) % 12 || 12; // 한번에 빨리 요청하면 막히기 때문에 100ms 간격으로 요청 promiseList.push(new Promise((resolve, reject) => { setTimeout(() => { getHolidaysByMonth(targetYear, targetMonth) .then((r) => resolve(r)) .catch((r) => reject(r)); }, 100 * Math.max(i - 10, 0)); })); } const result = yield Promise.all(promiseList); return result.flat(); }); exports.getHolidaysByMonthCount = getHolidaysByMonthCount; //# sourceMappingURL=getHolidays.js.map