@ingestkorea/client-sens
Version:
INGESTKOREA SDK Naver Cloud Platform SENS Client for Node.js.
156 lines (155 loc) • 6.13 kB
JavaScript
;
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 = async (output) => {
const { statusCode, headers, body: streamBody } = output;
if (!isJsonResponse(headers["content-type"])) {
await (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 = await (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 = async (output) => {
const { statusCode, headers, body: streamBody } = output;
if (!isJsonResponse(headers["content-type"])) {
await (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 = await (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({
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;
};