Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit 6a533c5

Browse files
klokanekylef
authored andcommitted
fix(remote): use mediatype from Fury
1 parent 0a657c8 commit 6a533c5

File tree

3 files changed

+110
-68
lines changed

3 files changed

+110
-68
lines changed

packages/fury-adapter-remote/README.md

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,40 @@ By default the [API Elements Web Service](http://api.apielements.org/) is used.
1010

1111
```js
1212
const defaultOptions = {
13+
// the default value, but consumers should be able to override to use their own deployment
1314
url: 'https://api.apielements.org',
1415

1516
parseEndpoint: '/parser',
1617
validateEndpoint: '/validate',
1718
serializeEndpoint: '/composer',
1819

19-
// the collection of "parse", media types we want this
20-
// instance of the adapter to handle.
21-
// NOTE, this allows you to use the API for one media type but
22-
// another local adapter for another.
23-
parseMediaTypes: [
24-
'text/vnd.apiblueprint',
25-
'application/swagger+json',
26-
'application/swagger+yaml',
27-
'application/vnd.oai.openapi',
28-
'application/vnd.oai.openapi+json',
29-
],
30-
31-
// the collection of "serialize", media types we want this
32-
// instance of the adapter to handle.
33-
serializeMediaTypes: [
34-
'application/vnd.refract+json',
35-
'application/vnd.refract.parse-result+json',
36-
],
37-
38-
// fallback to try send input, if not indentified by deckardcain
39-
defaultParseMediaType: 'text/vnd.apiblueprint',
40-
defaultSerializeMediaType: 'application/vnd.refract+json',
20+
mediaTypes: {
21+
// the collection of "parse", media types we want this
22+
// instance of the adapter to handle.
23+
// NOTE, this allows you to use the API for one media type but
24+
// another local adapter for another.
25+
parse: [
26+
'text/vnd.apiblueprint',
27+
'application/swagger+json',
28+
'application/swagger+yaml',
29+
'application/vnd.oai.openapi',
30+
'application/vnd.oai.openapi+json',
31+
],
32+
33+
validate: [
34+
'text/vnd.apiblueprint',
35+
'application/swagger+json',
36+
'application/swagger+yaml',
37+
'application/vnd.oai.openapi',
38+
'application/vnd.oai.openapi+json',
39+
],
40+
41+
// the collection of "serialize", media types we want this
42+
// instance of the adapter to handle.
43+
serialize: [
44+
'text/vnd.apiblueprint',
45+
],
46+
}
4147
};
4248
```
4349

packages/fury-adapter-remote/lib/adapter.js

Lines changed: 51 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const axios = require('axios');
44
const name = 'remote';
55

66
const outputMediaType = 'application/vnd.refract.parse-result+json; version=1.0';
7+
const defaultInputMediaType = 'text/vnd.apiblueprint';
78

89
const 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

4046
const 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) => {

packages/fury-adapter-remote/test/fury-test.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,9 @@ describe('Adapter works with Fury interface', () => {
167167
fury.use(new FuryRemoteAdapter({
168168
url: 'https://api.apielements.org',
169169
parseEndpoint: '/parser',
170-
parseMediaTypes: [
170+
mediaTypes: [
171171
'text/vnd.apiblueprint',
172172
],
173-
defaultInputMediaType: 'text/vnd.apiblueprint',
174173
}));
175174
});
176175

@@ -197,17 +196,43 @@ describe('Adapter works with Fury interface', () => {
197196
});
198197
});
199198

199+
describe('#detect', () => {
200+
const fury = new Fury();
201+
const defaultAdapater = new FuryRemoteAdapter();
202+
const arrayMediaTypesAdapater = new FuryRemoteAdapter({
203+
mediaTypes: ['text/vnd.apiblueprint'],
204+
});
205+
const objectMediaTypesAdapater = new FuryRemoteAdapter({
206+
mediaTypes: {
207+
parse: ['text/vnd.apiblueprint'],
208+
validate: ['application/swagger+json'],
209+
},
210+
});
211+
212+
before(() => {
213+
fury.use(defaultAdapater);
214+
fury.use(arrayMediaTypesAdapater);
215+
fury.use(objectMediaTypesAdapater);
216+
});
217+
218+
it('works with different mediaTypes configurations', () => {
219+
expect(fury.detect(blueprintSource, 'parse')).to.be.deep.equal([defaultAdapater, arrayMediaTypesAdapater, objectMediaTypesAdapater]);
220+
expect(fury.detect(swaggerSource, 'parse')).to.be.deep.equal([defaultAdapater]);
221+
expect(fury.detect(blueprintSource, 'validate')).to.be.deep.equal([defaultAdapater, arrayMediaTypesAdapater]);
222+
expect(fury.detect(swaggerSource, 'validate')).to.be.deep.equal([defaultAdapater, objectMediaTypesAdapater]);
223+
});
224+
});
225+
200226
describe('with nonexisting domain', () => {
201227
const fury = new Fury();
202228

203229
before(() => {
204230
fury.use(new FuryRemoteAdapter({
205231
url: 'http://some.stupid.non.existing.domain',
206232
parseEndpoint: '/parser',
207-
parseMediaTypes: [
233+
mediaTypes: [
208234
'text/vnd.apiblueprint',
209235
],
210-
defaultInputMediaType: 'text/vnd.apiblueprint',
211236
}));
212237
});
213238

@@ -227,18 +252,17 @@ describe('Adapter works with Fury interface', () => {
227252
fury.use(new FuryRemoteAdapter({
228253
url: 'https://api.apielements.org',
229254
parseEndpoint: '/some.weird.endpoint',
230-
parseMediaTypes: [
255+
mediaTypes: [
231256
'text/vnd.apiblueprint',
232257
],
233-
defaultInputMediaType: 'text/vnd.apiblueprint',
234258
}));
235259
});
236260

237261
it('parses blueprint with mediaType', () => {
238262
fury.parse({ source: blueprintSource, mediaType: 'text/vnd.apiblueprint' }, (err, result) => {
239263
expect(result).to.be.undefined;
240264
expect(err).to.be.instanceof(Error);
241-
expect(err).to.have.property('message', 'Request failed with status code 500');
265+
expect(err).to.have.property('message', 'Request failed with status code 415');
242266
});
243267
});
244268
});

0 commit comments

Comments
 (0)