UNPKG

jswagger-client

Version:

This is jswagger's client package.

67 lines (66 loc) 2.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function leafConvertToClassValue(propInfo, input) { if (propInfo.type === 'string' && propInfo.format === 'date-time') { return new Date(input); } else if (propInfo.type === 'string' && propInfo.format === 'byte') { return Buffer.from(input, 'binary'); } return input; } exports.leafConvertToClassValue = leafConvertToClassValue; function leafConvertToJsonValue(propInfo, input) { if (propInfo.type === 'string' && propInfo.format === 'date-time' && input instanceof Date) { return input.toISOString(); } else if (propInfo.type === 'string' && propInfo.format === 'byte' && Buffer.isBuffer(input)) { return input.toString('binary'); } return input; } exports.leafConvertToJsonValue = leafConvertToJsonValue; function toClassValue(metadata, property, key, value) { const lastKey = key.split('/').reduce((prev, cur) => cur, ''); const propInfo = ((tmp) => { return tmp.$ref ? metadata.definitions[tmp.$ref] : tmp; })(property[lastKey]); if (typeof value === 'object' && propInfo.type === 'object') { if (propInfo.properties) { const _properties = propInfo.properties; return Object.entries(propInfo.properties).reduce((results, [curKey, curProp]) => { results[curKey] = toClassValue(metadata, _properties, key + '/' + curKey, value[curKey]); return results; }, {}); } } return leafConvertToClassValue(propInfo, value); } exports.toClassValue = toClassValue; function toJsonObject(metadata, property, key, value) { const lastKey = key.split('/').reduce((prev, cur) => cur, ''); const propInfo = ((tmp) => { return tmp.$ref ? metadata.definitions[tmp.$ref] : tmp; })(property[lastKey]); if (typeof value === 'object' && propInfo.type === 'object') { return Object.entries(propInfo.properties).reduce((results, [curKey, curProp]) => { results[curKey] = toJsonObject(metadata, propInfo.properties, key + '/' + curKey, value[curKey]); return results; }, {}); } return leafConvertToJsonValue(propInfo, value); } exports.toJsonObject = toJsonObject; const isArrayBufferSupported = (Buffer.from(new Uint8Array([1]).buffer)[0] === 1); function arrayBufferToBufferAsArgument(ab) { return Buffer.from(ab); } function arrayBufferToBufferCycle(ab) { const buffer = Buffer.alloc(ab.byteLength); const view = new Uint8Array(ab); for (let i = 0; i < buffer.length; ++i) { buffer[i] = view[i]; } return buffer; } exports.arrayBufferToBuffer = isArrayBufferSupported ? arrayBufferToBufferAsArgument : arrayBufferToBufferCycle;