@chubbyts/chubbyts-decode-encode
Version:
A simple decode/encode solution for json / jsonx / url-encoded / xml / yaml.
20 lines (19 loc) • 719 B
JavaScript
export class EncodeError extends Error {
constructor(message, stack) {
super(message);
this.stack = stack;
}
}
export 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,
};
};