|
| 1 | +/* |
| 2 | +MIT License |
| 3 | +
|
| 4 | +Copyright (c) 2022 Dmitry Dutikov |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +of this software and associated documentation files (the "Software"), to deal |
| 8 | +in the Software without restriction, including without limitation the rights |
| 9 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +copies of the Software, and to permit persons to whom the Software is |
| 11 | +furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in all |
| 14 | +copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +SOFTWARE. |
| 23 | +*/ |
| 24 | + |
| 25 | +const { JSON22 } = require('json22'); |
| 26 | +const utils = require('axios/lib/utils'); |
| 27 | + |
| 28 | +const PARSE_OPTIONS = Symbol('Json22ParseOptions'); |
| 29 | +const STRINGIFY_OPTIONS = Symbol('Json22StringifyOptions'); |
| 30 | + |
| 31 | +/** |
| 32 | + * @param {any} data |
| 33 | + * @param {AxiosRequestHeaders} headers |
| 34 | + * */ |
| 35 | +function transformDataToJson22String(data, headers) { |
| 36 | + const unsupported = [ |
| 37 | + utils.isFormData, |
| 38 | + utils.isArrayBuffer, |
| 39 | + utils.isBuffer, |
| 40 | + utils.isStream, |
| 41 | + utils.isFile, |
| 42 | + utils.isBlob, |
| 43 | + utils.isArrayBufferView, |
| 44 | + utils.isURLSearchParams, |
| 45 | + ]; |
| 46 | + |
| 47 | + if (!unsupported.some(fn => fn(data))) { |
| 48 | + const contentType = headers['Content-Type'] ?? headers['content-type']; |
| 49 | + if (contentType !== 'multipart/form-data' && contentType !== 'application/json') { |
| 50 | + const isObjectPayload = utils.isObject(data); |
| 51 | + if (isObjectPayload) { |
| 52 | + /** @type {AxiosRequestConfig} */ |
| 53 | + const config = this; |
| 54 | + headers['Content-Type'] = JSON22.mimeType; |
| 55 | + // TODO: combine both stringify options objects |
| 56 | + return JSON22.stringify(data, config[STRINGIFY_OPTIONS] ?? config.json22Options?.json22StringifyOptions); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + return data; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * @param {any} data |
| 65 | + * @param {AxiosResponseHeaders} [headers] |
| 66 | + * */ |
| 67 | +function transformJson22StringToData(data, headers) { |
| 68 | + /** @type {AxiosRequestConfig} */ |
| 69 | + const config = this; |
| 70 | + |
| 71 | + if ((headers?.['content-type'] ?? headers?.['Content-Type']) === JSON22.mimeType) { |
| 72 | + // TODO: combine both parse options objects |
| 73 | + return JSON22.parse(data, config[PARSE_OPTIONS] ?? config.json22Options?.json22ParseOptions); |
| 74 | + } |
| 75 | + |
| 76 | + return data; |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * @param {Json22AxiosOptions} [options={}] |
| 81 | + * */ |
| 82 | +function Json22RequestInterceptor(options = {}) { |
| 83 | + /** |
| 84 | + * @param {AxiosRequestConfig} config |
| 85 | + * */ |
| 86 | + function json22RqIntercept(config) { |
| 87 | + config[PARSE_OPTIONS] = options.json22ParseOptions; |
| 88 | + config[STRINGIFY_OPTIONS] = options.json22StringifyOptions; |
| 89 | + config.transformRequest.unshift(transformDataToJson22String); |
| 90 | + config.transformResponse.unshift(transformJson22StringToData); |
| 91 | + return config; |
| 92 | + } |
| 93 | + return json22RqIntercept; |
| 94 | +} |
| 95 | + |
| 96 | +module.exports = { transformDataToJson22String, transformJson22StringToData, Json22RequestInterceptor }; |
0 commit comments