UNPKG

@chubbyts/chubbyts-decode-encode

Version:

A simple decode/encode solution for json / jsonx / url-encoded / xml / yaml.

25 lines (24 loc) 910 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createEncoder = exports.EncodeError = void 0; class EncodeError extends Error { constructor(message, stack) { super(message); this.stack = stack; } } exports.EncodeError = EncodeError; const createEncoder = (encoderTypes) => { const encoderTypeMap = new Map(encoderTypes.map((encoderType) => [encoderType.contentType, encoderType])); const contentTypes = Array.from(encoderTypeMap.keys()); return { encode: (data, contentType) => { if (encoderTypeMap.has(contentType)) { return encoderTypeMap.get(contentType).encode(data); } throw new Error(`Unsupported contentType "${contentType}", supported contentTypes are "${contentTypes.join('", "')}".`); }, contentTypes, }; }; exports.createEncoder = createEncoder;