@ingestkorea/client-sens
Version:
INGESTKOREA SDK Naver Cloud Platform SENS Client for Node.js.
64 lines (63 loc) • 2.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveRequestDuration = exports.getDefaultRequestDuration = exports.convertToKst = exports.convertToUtc = exports.getContentLength = exports.prettyPhoneNum = exports.trimText = void 0;
const index_js_1 = require("../models/index.js");
const trimText = (input) => input.trim();
exports.trimText = trimText;
const prettyPhoneNum = (input) => input.replace(/\-/gi, "");
exports.prettyPhoneNum = prettyPhoneNum;
/** @returns content-length(euc-kr) */
const getContentLength = (input) => {
return input.split("").reduce((acc, text) => {
let byte = Buffer.from(text).length;
let modulo = byte % 3;
modulo ? (acc += 1) : (acc += 2);
return acc;
}, 0);
};
exports.getContentLength = getContentLength;
const convertToUtc = (input) => {
const utcRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z$/;
return utcRegex.test(input) ? input : new Date(input.replace(" ", "T") + "+09:00").toISOString();
};
exports.convertToUtc = convertToUtc;
const convertToKst = (input) => {
const KST_OFFSET = 9 * 3600000;
return new Date(input + KST_OFFSET).toISOString().replace(/\.\d{3}Z$/, "");
};
exports.convertToKst = convertToKst;
// UTC
const getDefaultRequestDuration = (config) => {
const now = Date.now();
return {
startTime: new Date(now - config.durationLimitMs).toISOString(),
endTime: new Date(now).toISOString(),
};
};
exports.getDefaultRequestDuration = getDefaultRequestDuration;
// UTC
const resolveRequestDuration = (input, config) => {
const start = new Date(input.startTime).getTime();
const end = new Date(input.endTime).getTime();
const diff = end - start;
if (diff < 0) {
throw new index_js_1.SensError({
code: -1,
type: "SDK.GENERAL_ERROR",
message: "조회 가능 날짜 오류. 종료일이 시작일보다 빠를 수 없습니다.",
errors: [`종료일: ${input.endTime}`, `시작일: ${input.startTime}`],
});
}
if (diff > config.durationLimitMs) {
throw new index_js_1.SensError({
code: -1,
type: "SDK.GENERAL_ERROR",
message: `최대 ${config.durationLimitMs / (24 * 3600000)}일 이내에서 조회 가능합니다.`,
});
}
return {
startTimeMs: start,
endTimeMs: end,
};
};
exports.resolveRequestDuration = resolveRequestDuration;