Package Exports
- @ingestkorea/util-http-handler
- @ingestkorea/util-http-handler/dist-cjs/index.js
- @ingestkorea/util-http-handler/dist-es/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@ingestkorea/util-http-handler) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@ingestkorea/util-http-handler
An internal package
You probably shouldn't, at least directly.
Description
INGESTKOREA Utility HTTP Handler for Node.js.
Installing
npm install @ingestkorea/util-http-handler
Getting Started
Pre-requisites
- Use TypeScript v4.x
- Includes the TypeScript definitions for node.
npm install -D @types/node # save dev mode
- Includes the @ingestkorea/util-error-handler.
npm install @ingestkorea/util-error-handler
Import
import { IngestkoreaError, ingestkoreaErrorCodeChecker } from "@ingestkorea/util-error-handler";
import {
NodeHttpHandler,
HttpRequest,
HttpResponse,
collectBodyString,
destroyStream,
} from "@ingestkorea/util-http-handler";
Usage
Create Node Http Handler
const httpHandler = new NodeHttpHandler({
connectionTimeout: 3000,
socketTimeout: 3000,
});
Set Response Body Handler
const verifyJsonHeader = async (contentType: string, streamBody: any): Promise<void> => {
const isValid = /application\/json/gi.exec(contentType) ? true : false;
if (!isValid) {
destroyStream(streamBody);
throw new IngestkoreaError({
code: 400,
type: "Bad Request",
message: "Invalid Request",
description: "response content-type is not applicaion/json",
});
}
return;
};
const parseBody = async (output: HttpResponse): Promise<any> => {
const { headers, body: streamBody } = output;
await verifyJsonHeader(headers["content-type"], streamBody);
const data = await collectBodyString(streamBody);
if (data.length) return JSON.parse(data);
return {};
};
const parseErrorBody = async (output: HttpResponse): Promise<void> => {
const { statusCode, headers, body: streamBody } = output;
await verifyJsonHeader(headers["content-type"], streamBody);
const data = await collectBodyString(streamBody);
let customError = new IngestkoreaError({ code: 400, type: "Bad Request", message: "Invalid Request" });
if (ingestkoreaErrorCodeChecker(statusCode)) customError.error.code = statusCode;
if (data.length) customError.error.description = JSON.parse(data);
throw customError;
};
Set Serialize, Deserialize command
const serialize_command_01 = async () => {
return new HttpRequest({
protocol: "https:",
method: "GET",
hostname: "api.hello-world.com",
path: "/userInfo",
query: {
id: "12345",
},
});
};
const deserialize_command_01 = async (output: HttpResponse): Promise<any> => {
if (output.statusCode >= 300) await parseErrorBody(output);
let content = await parseBody(output);
return content;
};
Async/await
(async () => {
try {
let request = await serialize_command_01();
let { response } = await httpHandler.handle(request);
let output = await deserialize_command_01(response);
console.log(output);
} catch (err) {
console.log(err);
}
})();
License
This Utility is distributed under the MIT License, see LICENSE for more information.