@@ -4,6 +4,7 @@ const axios = require('axios');
44const name = 'remote' ;
55
66const outputMediaType = 'application/vnd.refract.parse-result+json; version=1.0' ;
7+ const defaultInputMediaType = 'text/vnd.apiblueprint' ;
78
89const defaultOptions = {
910 // the default value, but consumers should be able to override to use their own deployment
@@ -13,28 +14,33 @@ const defaultOptions = {
1314 validateEndpoint : '/validate' ,
1415 serializeEndpoint : '/composer' ,
1516
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' ,
17+ mediaTypes : {
18+ // the collection of "parse", media types we want this
19+ // instance of the adapter to handle.
20+ // NOTE, this allows you to use the API for one media type but
21+ // another local adapter for another.
22+ parse : [
23+ 'text/vnd.apiblueprint' ,
24+ 'application/swagger+json' ,
25+ 'application/swagger+yaml' ,
26+ 'application/vnd.oai.openapi' ,
27+ 'application/vnd.oai.openapi+json' ,
28+ ] ,
29+
30+ validate : [
31+ 'text/vnd.apiblueprint' ,
32+ 'application/swagger+json' ,
33+ 'application/swagger+yaml' ,
34+ 'application/vnd.oai.openapi' ,
35+ 'application/vnd.oai.openapi+json' ,
36+ ] ,
37+
38+ // the collection of "serialize", media types we want this
39+ // instance of the adapter to handle.
40+ serialize : [
41+ 'text/vnd.apiblueprint' ,
42+ ] ,
43+ } ,
3844} ;
3945
4046const detectMediaType = ( source , defaultMediaType ) => {
@@ -46,18 +52,28 @@ class FuryRemoteAdapter {
4652 constructor ( options ) {
4753 this . name = name ;
4854 this . options = options || defaultOptions ;
49- const parseMediaTypes = this . options . parseMediaTypes || [ ] ;
50- const serializeMediaTypes = this . options . serializeMediaTypes || [ ] ;
51-
52- this . mediaTypes = parseMediaTypes . concat ( serializeMediaTypes ) ;
55+ this . mediaTypes = this . options . mediaTypes || [ ] ;
5356 }
5457
55- detect ( source ) {
56- return this . mediaTypes . includes ( deckardcain . identify ( source ) ) ;
58+ detect ( source , method ) {
59+ const mediaType = deckardcain . identify ( source ) ;
60+ if ( Array . isArray ( this . mediaTypes ) && this . mediaTypes . includes ( mediaType ) && ( method === undefined || this [ method ] ) ) {
61+ return true ;
62+ } if ( typeof this . mediaTypes === 'object' ) {
63+ if ( method !== undefined ) {
64+ return ( this . mediaTypes [ method ] && this . mediaTypes [ method ] . includes ( mediaType ) && this [ method ] ) ;
65+ }
66+ const mediaTypes = [ ] ;
67+ Object . keys ( this . mediaTypes ) . forEach ( ( key ) => {
68+ mediaTypes . concat ( this . mediaTypes [ key ] ) ; // the value of the current key.
69+ } ) ;
70+ return mediaTypes . includes ( mediaType ) ;
71+ }
72+ return false ;
5773 }
5874
59- parse ( { source, minim } , cb ) {
60- const inputMediaType = detectMediaType ( source , this . options . defaultInputMediaType ) ;
75+ parse ( { source, minim, mediaType } , cb ) {
76+ const inputMediaType = mediaType || detectMediaType ( source , defaultInputMediaType ) ;
6177
6278 axios ( {
6379 method : 'post' ,
@@ -78,8 +94,8 @@ class FuryRemoteAdapter {
7894 } ) ;
7995 }
8096
81- validate ( { source, minim } , cb ) {
82- const inputMediaType = detectMediaType ( source , this . options . defaultInputMediaType ) ;
97+ validate ( { source, minim, mediaType } , cb ) {
98+ const inputMediaType = mediaType || detectMediaType ( source , defaultInputMediaType ) ;
8399
84100 axios ( {
85101 method : 'post' ,
@@ -102,13 +118,9 @@ class FuryRemoteAdapter {
102118 } ) ;
103119 }
104120
105- serialize ( { api, minim } , cb ) {
106- let inputMediaType = this . options . defaultSerializeMediaType ;
121+ serialize ( { api, minim, mediaType } , cb ) {
107122 const content = minim . serialiser . serialise ( api ) ;
108-
109- if ( content . element && content . element === 'parseResult' ) {
110- inputMediaType = 'application/vnd.refract.parse-result+json' ;
111- }
123+ const inputMediaType = ( content . element && content . element === 'parseResult' ) ? 'application/vnd.refract+json' : 'application/vnd.refract.parse-result+json' ;
112124
113125 axios ( {
114126 method : 'post' ,
@@ -117,7 +129,7 @@ class FuryRemoteAdapter {
117129 data : content ,
118130 headers : {
119131 'Content-Type' : inputMediaType ,
120- Accept : 'text/vnd.apiblueprint' ,
132+ Accept : mediaType ,
121133 } ,
122134 } )
123135 . then ( ( response ) => {
0 commit comments