|
| 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 | +const { JSON22 } = require('json22'); |
| 25 | + |
| 26 | +const METHODS_WITH_BODY = ['POST', 'PUT', 'PATCH']; |
| 27 | + |
| 28 | +module.exports = { json22Plugin }; |
| 29 | + |
| 30 | +/** |
| 31 | + * @param {Json22SuperagentPluginOptions} [options] |
| 32 | + * @return {request.Plugin} |
| 33 | + * */ |
| 34 | +function json22Plugin(options) { |
| 35 | + return requestPlugin.bind(options ?? {}); |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * @this Json22SuperagentPluginOptions |
| 40 | + * @param {SuperAgentRequest} request |
| 41 | + **/ |
| 42 | +function requestPlugin(request) { |
| 43 | + if (METHODS_WITH_BODY.indexOf(request.method) > -1) { |
| 44 | + request.type(JSON22.mimeType); |
| 45 | + request.serialize(data => JSON22.stringify(data, this.json22StringifyOptions)); |
| 46 | + } |
| 47 | + request.accept(JSON22.mimeType); |
| 48 | + request.buffer(true); // required for parsing in node environment |
| 49 | + request.parse((textOrResp, cb) => { |
| 50 | + if (cb == null) { |
| 51 | + return JSON22.parse(textOrResp, this.json22ParseOptions); |
| 52 | + } else { |
| 53 | + json22Parse(textOrResp, cb, this.json22ParseOptions); |
| 54 | + } |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +function json22Parse(res, fn, json22ParseOptions) { |
| 60 | + const chunks = []; |
| 61 | + res.setEncoding('utf8'); |
| 62 | + res.on('data', chunk => chunks.push(chunk)); |
| 63 | + res.on('end', function () { |
| 64 | + try { |
| 65 | + const text = chunks.join(''); |
| 66 | + const body = JSON22.parse(text, json22ParseOptions); |
| 67 | + res.text = text |
| 68 | + fn(undefined, body); |
| 69 | + } catch (e) { |
| 70 | + e.rawResponse = chunks.join(''); |
| 71 | + e.statusCode = res.statusCode; |
| 72 | + fn(e); |
| 73 | + } |
| 74 | + }); |
| 75 | +} |
0 commit comments