UNPKG

@dgks/core

Version:

Core implementation for @dgks packages

158 lines 5.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Response = void 0; const errors_1 = require("./errors"); /** * Response wrapping data.go.kr common formats * * @template T */ class Response { constructor(rawResponse) { this.rawResponse = rawResponse; this.parsedResponse = this.parseRawResponse(rawResponse); } // ==================================== // Public Getters // ==================================== /** * Items of requested service * @type {T[]} */ get items() { return this.parsedResponse.body.items.item; } /** * Requested number of rows * @type {number} */ get numOfRows() { return this.parsedResponse.body.numOfRows; } /** * Current page * @type {number} */ get page() { return this.parsedResponse.body.pageNo; } /** * Total count * @type {number} */ get totalCount() { return this.parsedResponse.body.totalCount; } // ==================================== // Protected Functions // ==================================== /** * Parses raw json response to match response container interface * * @throws {ServiceKeyNotRegistered} Service key is not registed in 3rd party service * @throws {ResponseParseError} Failed to parse response to what we want * @param {Record<string, any>} obj - response to parse * @returns {ResponseContainer<T>} */ parseRawResponse(obj) { const openApiResonse = obj === null || obj === void 0 ? void 0 : obj.OpenAPI_ServiceResponse; if (this.isOpenApiServiceResponse(openApiResonse)) { const header = openApiResonse.cmmMsgHeader; if (header.returnAuthMsg === 'SERVICE_KEY_IS_NOT_REGISTERED_ERROR') { throw new errors_1.ServiceKeyNotRegistered(); } throw new errors_1.ServiceError(header.returnAuthMsg); } const response = obj.response; if (!response) throw new errors_1.ResponseParseError('response'); const header = this.parseHeader(response); const body = this.parseBody(response); return { header, body, }; } /** * Parses header object and checks result code * * @throws {ResponseParseError} Failed to parse given header * @throws {ResponseError} Given header's response code is not what we expected * @param {Record<string, any>} obj - header object to parse * @returns {ResponseContainer<T>['header']} */ parseHeader(obj) { var _a; const header = { ...((_a = obj.header) !== null && _a !== void 0 ? _a : {}) }; if (Object.keys(header).length === 0) { throw new errors_1.ResponseParseError('header'); } if (header.resultCode !== '00') { throw new errors_1.ResponseError(header === null || header === void 0 ? void 0 : header.resultMsg, header === null || header === void 0 ? void 0 : header.resultCode); } return header; } /** * Parses body object and transforms it * * @throws {ResponseParseError} Failed to parse given body * @param {Record<string, any>} obj - header object to parse * @returns {ResponseContainer<T>['body']} */ parseBody(obj) { var _a, _b; const body = { ...((_a = obj.body) !== null && _a !== void 0 ? _a : {}) }; if (Object.keys(body).length === 0) { throw new errors_1.ResponseParseError('body'); } if (typeof body.items === 'undefined' || body.items === null) { throw new errors_1.ResponseParseError('body.items'); } if (Array.isArray(body.items.item)) { return this.transformItems(body); } if (typeof body.items === 'string') { body.items = { item: [] }; return this.transformItems(body); } if (typeof ((_b = body.items) === null || _b === void 0 ? void 0 : _b.item) === 'object') { body.items = { item: [{ ...body.items.item }] }; return this.transformItems(body); } return this.transformItems(body); } /** * Replaces empty string to undefined * * @param {ResponseContainer<T>['body']} body - Body to transform * @returns {ResponseContainer<T>['body']} */ transformItems(body) { const items = body.items.item; return { items: { item: items.map((obj) => Object.entries(obj).reduce((acc, [key, val]) => { if (typeof val !== 'string') { return { ...acc, [key]: val }; } return { ...acc, [key]: val.trim() === '' ? undefined : val }; }, {})), }, numOfRows: body.numOfRows, pageNo: body.pageNo, totalCount: body.totalCount, }; } /** * Checks if object is typeof OpenApiServiceResponse * * @param {Record<string, any>} obj - Object to check * @returns {boolean} */ isOpenApiServiceResponse(obj) { var _a, _b, _c; return ((_a = obj === null || obj === void 0 ? void 0 : obj.cmmMsgHeader) === null || _a === void 0 ? void 0 : _a.errMsg) && ((_b = obj === null || obj === void 0 ? void 0 : obj.cmmMsgHeader) === null || _b === void 0 ? void 0 : _b.returnAuthMsg) && ((_c = obj === null || obj === void 0 ? void 0 : obj.cmmMsgHeader) === null || _c === void 0 ? void 0 : _c.returnAuthMsg); } } exports.Response = Response; //# sourceMappingURL=response.js.map