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

Commit e3fe71b

Browse files
klokanekylef
authored andcommitted
feat(core): pass mediaType to adapters
1 parent 7c93b9e commit e3fe71b

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

packages/fury/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
`parse`, or `serialize`.
99
[#158](https://github.com/apiaryio/api-elements.js/issues/158)
1010

11+
### Enhancement
12+
13+
- Fury now pass into operations parse(), validate(), serialize() mediaType as one of adapter options
14+
1115
## 3.0.0-beta.9 (2019-02-26)
1216

1317
### Breaking

packages/fury/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,15 @@ export function detect(source) {
129129
return source.match(/some-test/i) !== null;
130130
}
131131

132-
export function parse({minim, generateSourceMap, source}, done) {
132+
export function parse({minim, generateSourceMap, mediaType, source}, done) {
133133
// Here you convert the source into refract elements. Use the `minim`
134134
// variable to access refract element classes.
135135
const Resource = minim.getElementByClass('resource');
136136
// ...
137137
done(null, elements);
138138
}
139139

140-
export function validate({minim, source}, done) {
140+
export function validate({minim, mediaType, source}, done) {
141141
// Here you validate the source and return a parse result for any warnings or
142142
// errors.
143143
//
@@ -146,7 +146,7 @@ export function validate({minim, source}, done) {
146146
done(null, null);
147147
}
148148

149-
export function serialize({api, minim}, done) {
149+
export function serialize({api, mediaType, minim}, done) {
150150
// Here you convert `api` from javascript element objects to the serialized
151151
// source format.
152152
// ...

packages/fury/lib/fury.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class Fury {
146146
});
147147
}
148148

149-
let options = { minim: this.minim, source };
149+
let options = { minim: this.minim, mediaType, source };
150150

151151
if (adapterOptions) {
152152
options = Object.assign(options, adapterOptions);
@@ -187,7 +187,9 @@ class Fury {
187187
return done(new Error('Document did not match any registered parsers!'));
188188
}
189189

190-
let options = { generateSourceMap, minim: this.minim, source };
190+
let options = {
191+
generateSourceMap, minim: this.minim, mediaType, source,
192+
};
191193

192194
if (adapterOptions) {
193195
options = Object.assign(options, adapterOptions);
@@ -225,7 +227,7 @@ class Fury {
225227
const adapter = findAdapter(this.adapters, mediaType, 'serialize');
226228

227229
if (adapter) {
228-
return adapter.serialize({ api, minim: this.minim }, done);
230+
return adapter.serialize({ api, minim: this.minim, mediaType }, done);
229231
}
230232

231233
return done(new Error('Media type did not match any registered serializer!'));

packages/fury/test/fury-test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,42 @@ describe('Fury class', () => {
312312
assert.equal(localFury.detect('none').length, 0);
313313
});
314314
});
315+
316+
describe('pass mediaType into adapter operations', () => {
317+
const fury = new Fury();
318+
319+
function test(first) {
320+
return function impl(second) { assert.equal(first, second); };
321+
}
322+
323+
before(() => {
324+
fury.use({
325+
name: 'AssertionAdapter',
326+
mediaTypes: ['text/vnd.parse', 'text/vnd.validate', 'text/vnd.serialize'],
327+
parse({ mediaType }, test) {
328+
test(mediaType);
329+
},
330+
validate({ mediaType }, test) {
331+
test(mediaType);
332+
},
333+
serialize({ mediaType }, test) {
334+
test(mediaType);
335+
},
336+
});
337+
});
338+
339+
it('into parse', () => {
340+
fury.parse({ mediaType: 'text/vnd.parse' }, test('text/vnd.parse'));
341+
});
342+
343+
it('into validate', () => {
344+
fury.parse({ mediaType: 'text/vnd.validate' }, test('text/vnd.validate'));
345+
});
346+
347+
it('into serialize', () => {
348+
fury.parse({ mediaType: 'text/vnd.serialize' }, test('text/vnd.serialize'));
349+
});
350+
});
315351
});
316352

317353
describe('Parser', () => {

0 commit comments

Comments
 (0)