UNPKG

@ingestkorea/client-sens

Version:

INGESTKOREA SDK Naver Cloud Platform SENS Client for Node.js.

160 lines (159 loc) 6.83 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.convertToUtc = exports.compact = exports.parseErrorBody = exports.parseBody = exports.deserializeMetadata = void 0; const util_http_handler_1 = require("@ingestkorea/util-http-handler"); const index_js_1 = require("../models/index.js"); const constants_js_1 = require("../middleware/constants.js"); const deserializeMetadata = (response) => { const attempts = response.headers[constants_js_1.INGESTKOREA_RETRY] || "1"; const totalRetryDelay = response.headers[constants_js_1.INGESTKOREA_RETRY_DELAY] || "0"; return { httpStatusCode: response.statusCode, attempts: Number(attempts), totalRetryDelay: Number(totalRetryDelay), }; }; exports.deserializeMetadata = deserializeMetadata; const parseBody = (output) => __awaiter(void 0, void 0, void 0, function* () { const { statusCode, headers, body: streamBody } = output; if (!isJsonResponse(headers["content-type"])) { yield (0, util_http_handler_1.destroyStream)(streamBody); throw new index_js_1.SensError({ code: -1, type: "SDK.REQUEST_ERROR", message: "response content-type is not application/json", }); } try { const data = yield (0, util_http_handler_1.collectBodyString)(streamBody); return data.length ? JSON.parse(data) : {}; } catch (e) { throw new index_js_1.SensError({ code: -1, type: "SDK.REQUEST_ERROR", message: e instanceof Error ? e.message : "parse response body error", }); } }); exports.parseBody = parseBody; const parseErrorBody = (output) => __awaiter(void 0, void 0, void 0, function* () { const { statusCode, headers, body: streamBody } = output; if (!isJsonResponse(headers["content-type"])) { yield (0, util_http_handler_1.destroyStream)(streamBody); throw new index_js_1.SensError({ code: -1, type: "SDK.REQUEST_ERROR", message: "response content-type is not application/json", }); } const data = yield (0, util_http_handler_1.collectBodyString)(streamBody); const sensErrorBody = isSensErrorResponse(data); const nCloudErrorBody = isNCloudErrorResponse(data); if (sensErrorBody) { const errorType = isSensErrorCode(sensErrorBody.status) ? index_js_1.SENS_ERROR_CODE_SPEC[sensErrorBody.status] : "SDK.UNKNOWN_ERROR"; throw new index_js_1.SensError(Object.assign({ code: isSensErrorCode(sensErrorBody.status) ? sensErrorBody.status : statusCode, type: errorType, message: sensErrorBody.errorMessage || errorType }, (sensErrorBody.errors && Array.isArray(sensErrorBody.errors) && { errors: sensErrorBody.errors.filter((d) => !!d), }))); } if (nCloudErrorBody) { const { errorCode } = nCloudErrorBody.error; const errorType = isNCloudErrorCode(errorCode) ? index_js_1.NCLOUD_ERROR_CODE_SPEC[errorCode].type : "SDK.UNKNOWN_ERROR"; throw new index_js_1.SensError({ code: isNCloudErrorCode(errorCode) ? index_js_1.NCLOUD_ERROR_CODE_SPEC[errorCode].status : statusCode, type: errorType, message: nCloudErrorBody.error.details || "Something wrong", }); } throw new index_js_1.SensError({ code: -1, type: "SDK.UNKNOWN_ERROR", message: "Invalid Naver Cloud Platform response format", errors: [`${statusCode} - ${data.slice(0, 120)}...`], }); }); exports.parseErrorBody = parseErrorBody; const isSensErrorCode = (code) => { return code in index_js_1.SENS_ERROR_CODE_SPEC; }; const isNCloudErrorCode = (code) => { return code in index_js_1.NCLOUD_ERROR_CODE_SPEC; }; const isSensErrorResponse = (input) => { if (typeof input !== "string") return null; try { const data = JSON.parse(input); if (!data || typeof data !== "object") return null; const isValid = index_js_1.SENS_ERROR_BODY_SPEC.every(([key, type, required]) => { const value = data[key]; if (required && (value === undefined || value === null)) return false; if (value !== undefined && typeof value !== type) return false; return true; }); return isValid ? data : null; } catch (_a) { return null; } }; const isNCloudErrorResponse = (input) => { if (typeof input !== "string") return null; try { const data = JSON.parse(input); if (!(data && typeof data == "object" && typeof data.error == "object")) return null; const isValid = index_js_1.NCLOUD_ERROR_BODY_SPEC.every(([key, type, required]) => { const value = data.error[key]; if (required && (value === undefined || value === null)) return false; if (value !== undefined && typeof value !== type) return false; return true; }); return isValid ? data : null; } catch (_a) { return null; } }; const compact = (obj) => { if (Array.isArray(obj)) { return obj.map((item) => (0, exports.compact)(item)); } if (typeof obj === "object" && obj !== null) { return Object.fromEntries(Object.entries(obj) .filter(([_, value]) => value !== undefined) // undefined 필드 제거 .map(([key, value]) => [key, (0, exports.compact)(value)]) // 내부 값 재귀 처리 ); } return obj; }; exports.compact = compact; 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 isJsonResponse = (contentType) => { var _a; return (_a = contentType === null || contentType === void 0 ? void 0 : contentType.toLowerCase().includes("application/json")) !== null && _a !== void 0 ? _a : false; };