@ingestkorea/client-sens
Version:
INGESTKOREA SDK Naver Cloud Platform SENS Client for Node.js.
65 lines (64 loc) • 3.08 kB
JavaScript
;
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.deserializeMetadata = exports.parseErrorBody = exports.parseBody = void 0;
const util_error_handler_1 = require("@ingestkorea/util-error-handler");
const util_http_handler_1 = require("@ingestkorea/util-http-handler");
const parseBody = (output) => __awaiter(void 0, void 0, void 0, function* () {
const { statusCode, headers, body: streamBody } = output;
const isValid = yield verifyJsonHeader(headers["content-type"]);
if (!isValid) {
yield (0, util_http_handler_1.destroyStream)(streamBody);
let customError = new util_error_handler_1.IngestkoreaError({
code: 400,
type: "Bad Request",
message: "Invalid Request",
description: "content-type is not application/json",
});
if ((0, util_error_handler_1.ingestkoreaErrorCodeChecker)(statusCode))
customError.error.code = statusCode;
throw customError;
}
const data = yield (0, util_http_handler_1.collectBodyString)(streamBody);
if (data.length)
return JSON.parse(data);
return {};
});
exports.parseBody = parseBody;
const parseErrorBody = (output) => __awaiter(void 0, void 0, void 0, function* () {
const { statusCode, headers, body: streamBody } = output;
const isValid = yield verifyJsonHeader(headers["content-type"]);
yield (0, util_http_handler_1.destroyStream)(streamBody);
let customError = new util_error_handler_1.IngestkoreaError({
code: 400,
type: "Bad Request",
message: "Invalid Request",
description: "content-type is not application/json",
});
if ((0, util_error_handler_1.ingestkoreaErrorCodeChecker)(statusCode))
customError.error.code = statusCode;
if (!isValid)
throw customError;
const data = yield (0, util_http_handler_1.collectBodyString)(streamBody);
if (data.length)
customError.error.description = JSON.parse(data);
throw customError;
});
exports.parseErrorBody = parseErrorBody;
const verifyJsonHeader = (contentType) => __awaiter(void 0, void 0, void 0, function* () {
return /application\/json/gi.exec(contentType) ? true : false;
});
const deserializeMetadata = (response) => {
return {
httpStatusCode: response.statusCode,
};
};
exports.deserializeMetadata = deserializeMetadata;