@kyungseopk1m/holidays-kr
Version:
한국천문연구원 기반 대한민국 공휴일 데이터
102 lines (101 loc) • 3.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.holidays = exports.clearCache = void 0;
const DEFAULT_BASE = "https://kdata.kxxseop.workers.dev/api/v1/holidays";
const TIMEOUT_MS = 10000;
const MIN_YEAR = 2004;
const CACHE_TTL_MS = 24 * 60 * 60 * 1000;
const memoryCache = new Map();
const cacheKey = (baseUrl, year) => `${baseUrl}::${year}`;
const resolveBaseUrl = (override) => {
const raw = override ??
(typeof process !== "undefined"
? process.env?.HOLIDAYS_KR_BASE_URL
: undefined) ??
DEFAULT_BASE;
return raw.replace(/\/+$/, "");
};
const fetchYear = async (baseUrl, year, signal) => {
const key = cacheKey(baseUrl, year);
const hit = memoryCache.get(key);
if (hit && hit.expiresAt > Date.now())
return hit.data;
const response = await fetch(`${baseUrl}/${year}.json`, {
method: "GET",
signal,
});
if (response.status === 404) {
memoryCache.set(key, { data: [], expiresAt: Date.now() + CACHE_TTL_MS });
return [];
}
if (!response.ok) {
throw new Error("Failed to fetch holiday data");
}
const payload = await response.json();
const data = Array.isArray(payload?.data) ? payload.data : [];
memoryCache.set(key, { data, expiresAt: Date.now() + CACHE_TTL_MS });
return data;
};
const clearCache = () => {
memoryCache.clear();
};
exports.clearCache = clearCache;
const holidays = async (year, year2, options) => {
if (year2 === "")
year2 = undefined;
if (year.length !== 4 ||
(year2 && year2.length !== 4) ||
!/^\d+$/.test(year) ||
(year2 && !/^\d+$/.test(year2))) {
return {
success: false,
message: "Please enter the year correctly.",
data: [],
};
}
const yearNum = parseInt(year, 10);
const year2Num = year2 ? parseInt(year2, 10) : undefined;
if (yearNum < MIN_YEAR || (year2Num !== undefined && year2Num < MIN_YEAR)) {
return {
success: false,
message: "Invalid input range. We provide data from 2004 onwards.",
data: [],
};
}
if (year2Num !== undefined && year2Num < yearNum) {
return {
success: false,
message: "The end year must be greater than or equal to the start year.",
data: [],
};
}
try {
const baseUrl = resolveBaseUrl(options?.baseUrl);
const start = yearNum;
const end = year2Num ?? yearNum;
const signal = options?.signal ?? AbortSignal.timeout(TIMEOUT_MS);
const years = [];
for (let y = start; y <= end; y++)
years.push(y);
const buckets = await Promise.all(years.map((y) => fetchYear(baseUrl, y, signal)));
return {
success: true,
message: "Success",
data: buckets.flat(),
};
}
catch (error) {
return error instanceof Error
? {
success: false,
message: error.message,
data: [],
}
: {
success: false,
message: "Unknown error",
data: [],
};
}
};
exports.holidays = holidays;