UNPKG

@ingestkorea/client-sens

Version:

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

50 lines (49 loc) 1.85 kB
import { IngestkoreaError, ingestkoreaErrorCodeChecker } from "@ingestkorea/util-error-handler"; import { collectBodyString, destroyStream } from "@ingestkorea/util-http-handler"; export const parseBody = async (output) => { const { statusCode, headers, body: streamBody } = output; const isValid = await verifyJsonHeader(headers["content-type"]); if (!isValid) { await destroyStream(streamBody); let customError = new IngestkoreaError({ code: 400, type: "Bad Request", message: "Invalid Request", description: "content-type is not application/json", }); if (ingestkoreaErrorCodeChecker(statusCode)) customError.error.code = statusCode; throw customError; } const data = await collectBodyString(streamBody); if (data.length) return JSON.parse(data); return {}; }; export const parseErrorBody = async (output) => { const { statusCode, headers, body: streamBody } = output; const isValid = await verifyJsonHeader(headers["content-type"]); await destroyStream(streamBody); let customError = new IngestkoreaError({ code: 400, type: "Bad Request", message: "Invalid Request", description: "content-type is not application/json", }); if (ingestkoreaErrorCodeChecker(statusCode)) customError.error.code = statusCode; if (!isValid) throw customError; const data = await collectBodyString(streamBody); if (data.length) customError.error.description = JSON.parse(data); throw customError; }; const verifyJsonHeader = async (contentType) => { return /application\/json/gi.exec(contentType) ? true : false; }; export const deserializeMetadata = (response) => { return { httpStatusCode: response.statusCode, }; };