|
| 1 | +const deckardcain = require('deckardcain'); |
| 2 | +const axios = require('axios'); |
| 3 | + |
| 4 | +const name = 'remote'; |
| 5 | + |
| 6 | +const outputMediaType = 'application/vnd.refract.parse-result+json; version=1.0'; |
| 7 | + |
| 8 | +const defaultOptions = { |
| 9 | + // the default value, but consumers should be able to override to use their own deployment |
| 10 | + url: 'https://api.apielements.org', |
| 11 | + |
| 12 | + parseEndpoint: '/parser', |
| 13 | + validateEndpoint: '/validate', |
| 14 | + serializeEndpoint: '/composer', |
| 15 | + |
| 16 | + // the collection of "parse", media types we want this |
| 17 | + // instance of the adapter to handle. |
| 18 | + // NOTE, this allows you to use the API for one media type but |
| 19 | + // another local adapter for another. |
| 20 | + parseMediaTypes: [ |
| 21 | + 'text/vnd.apiblueprint', |
| 22 | + 'application/swagger+json', |
| 23 | + 'application/swagger+yaml', |
| 24 | + 'application/vnd.oai.openapi', |
| 25 | + 'application/vnd.oai.openapi+json', |
| 26 | + ], |
| 27 | + |
| 28 | + // the collection of "serialize", media types we want this |
| 29 | + // instance of the adapter to handle. |
| 30 | + serializeMediaTypes: [ |
| 31 | + 'application/vnd.refract+json', |
| 32 | + 'application/vnd.refract.parse-result+json', |
| 33 | + ], |
| 34 | + |
| 35 | + // fallback to try send input, if not indentified by deckardcain |
| 36 | + defaultParseMediaType: 'text/vnd.apiblueprint', |
| 37 | + defaultSerializeMediaType: 'application/vnd.refract+json', |
| 38 | +}; |
| 39 | + |
| 40 | +const detectMediaType = (source, defaultMediaType) => { |
| 41 | + const mediaType = deckardcain.identify(source); |
| 42 | + return mediaType || defaultMediaType; |
| 43 | +}; |
| 44 | + |
| 45 | +class FuryRemoteAdapter { |
| 46 | + constructor(options) { |
| 47 | + this.name = name; |
| 48 | + this.options = options || defaultOptions; |
| 49 | + const parseMediaTypes = this.options.parseMediaTypes || []; |
| 50 | + const serializeMediaTypes = this.options.serializeMediaTypes || []; |
| 51 | + |
| 52 | + this.mediaTypes = parseMediaTypes.concat(serializeMediaTypes); |
| 53 | + } |
| 54 | + |
| 55 | + detect(source) { |
| 56 | + return this.mediaTypes.includes(deckardcain.identify(source)); |
| 57 | + } |
| 58 | + |
| 59 | + parse({ source, minim }, cb) { |
| 60 | + const inputMediaType = detectMediaType(source, this.options.defaultInputMediaType); |
| 61 | + |
| 62 | + axios({ |
| 63 | + method: 'post', |
| 64 | + url: this.options.parseEndpoint, |
| 65 | + baseURL: this.options.url, |
| 66 | + data: source, |
| 67 | + headers: { |
| 68 | + 'Content-Type': inputMediaType, |
| 69 | + Accept: outputMediaType, |
| 70 | + }, |
| 71 | + // allow code 422 to be identified as valid response |
| 72 | + validateStatus: status => ((status >= 200 && status < 300) || status === 422), |
| 73 | + }) |
| 74 | + .then((response) => { |
| 75 | + cb(null, minim.serialiser.deserialise(response.data)); |
| 76 | + }, (err) => { |
| 77 | + cb(err, undefined); |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + validate({ source, minim }, cb) { |
| 82 | + const inputMediaType = detectMediaType(source, this.options.defaultInputMediaType); |
| 83 | + |
| 84 | + axios({ |
| 85 | + method: 'post', |
| 86 | + url: this.options.validateEndpoint, |
| 87 | + baseURL: this.options.url, |
| 88 | + data: { |
| 89 | + input_document: source, |
| 90 | + input_type: inputMediaType, |
| 91 | + output_type: outputMediaType, |
| 92 | + }, |
| 93 | + headers: { |
| 94 | + 'Content-Type': 'application/json', |
| 95 | + Accept: outputMediaType, |
| 96 | + }, |
| 97 | + }) |
| 98 | + .then((response) => { |
| 99 | + cb(null, minim.serialiser.deserialise(response.data)); |
| 100 | + }, (err) => { |
| 101 | + cb(err, undefined); |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + serialize({ api, minim }, cb) { |
| 106 | + let inputMediaType = this.options.defaultSerializeMediaType; |
| 107 | + const content = minim.serialiser.serialise(api); |
| 108 | + |
| 109 | + if (content.element && content.element === 'parseResult') { |
| 110 | + inputMediaType = 'application/vnd.refract.parse-result+json'; |
| 111 | + } |
| 112 | + |
| 113 | + axios({ |
| 114 | + method: 'post', |
| 115 | + url: this.options.serializeEndpoint, |
| 116 | + baseURL: this.options.url, |
| 117 | + data: content, |
| 118 | + headers: { |
| 119 | + 'Content-Type': inputMediaType, |
| 120 | + Accept: 'text/vnd.apiblueprint', |
| 121 | + }, |
| 122 | + }) |
| 123 | + .then((response) => { |
| 124 | + cb(null, response.data); |
| 125 | + }, (err) => { |
| 126 | + cb(err, undefined); |
| 127 | + }); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +module.exports = { |
| 132 | + FuryRemoteAdapter, |
| 133 | +}; |
0 commit comments