UNPKG

maplestory-openapi

Version:

This JavaScript library enables the use of the MapleStory OpenAPI of Nexon.

122 lines (115 loc) 4.26 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var axios = require('axios'); var dayjs = require('dayjs'); var timezone = require('../../../node_modules/dayjs/plugin/timezone.js'); var utc = require('../../../node_modules/dayjs/plugin/utc.js'); var mapleStoryApiError = require('./mapleStoryApiError.js'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios); var dayjs__default = /*#__PURE__*/_interopDefaultLegacy(dayjs); dayjs__default["default"].extend(timezone); dayjs__default["default"].extend(utc); /** * MapleStory OpenAPI client. */ class MapleStoryApi { apiKey; client; static BASE_URL = 'https://open.api.nexon.com/'; static DEFAULT_TIMEOUT = 5000; get timeout() { return this.client.defaults.timeout; } set timeout(value) { this.client.defaults.timeout = value; } constructor(apiKey) { this.apiKey = apiKey; this.client = axios__default["default"].create({ baseURL: MapleStoryApi.BASE_URL, timeout: MapleStoryApi.DEFAULT_TIMEOUT, headers: { 'x-nxopen-api-key': this.apiKey, }, }); this.client.interceptors.response.use((response) => response, (error) => { if (error instanceof axios.AxiosError) { const errorBody = error.response .data; throw new mapleStoryApiError.MapleStoryApiError(errorBody); } throw error; }); } //#endregion /** * API 서버의 데이터 갱신 시간에 따라 데이터가 조회 가능한 최신 날짜를 반환합니다. * * @param options */ getProperDefaultDateOptions(options) { const { hour, minute, dateOffset } = options; const nowInTimezone = dayjs__default["default"]().utcOffset(this.timezoneOffset); const updateDate = dayjs__default["default"]().utcOffset(this.timezoneOffset).hour(hour).minute(minute); let adjustedDate; if (nowInTimezone.isAfter(updateDate)) { adjustedDate = nowInTimezone; } else { adjustedDate = nowInTimezone.subtract(1, 'day'); } adjustedDate = adjustedDate.subtract(dateOffset ?? 0, 'day'); return { year: adjustedDate.year(), month: adjustedDate.month() + 1, day: adjustedDate.date(), }; } ; /** * 날짜 정보를 API 서버에서 요구하는 포맷으로 변환합니다. * * @param dateOptions 조회 하려는 날짜 * @param minDateOptions API 호출 가능한 최소 날짜 */ toDateString(dateOptions, minDateOptions) { const convert = (dateOptions) => { let year; let month; let day; let d; if (dateOptions instanceof Date) { d = dayjs__default["default"](dateOptions).utcOffset(this.timezoneOffset); year = d.year(); month = d.month() + 1; day = d.date(); } else { year = dateOptions.year; month = dateOptions.month; day = dateOptions.day; d = dayjs__default["default"](`${year}-${month}-${day}`).utcOffset(this.timezoneOffset); } return { year, month, day, d, }; }; const { year, month, day, d } = convert(dateOptions); const str = d.format('YYYY-MM-DD'); if (minDateOptions) { const { year: minYear, month: minMonth, day: minDay, } = convert(minDateOptions); if (year < minYear || (year === minYear && month < minMonth) || (year === minYear && month === minMonth && day < minDay)) { throw new Error(`You can only retrieve data after ${dayjs__default["default"](`${minYear}-${minMonth}-${minDay}`).format('YYYY-MM-DD')}.`); } } return str; } ; } exports.MapleStoryApi = MapleStoryApi;