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

Commit d6d749f

Browse files
committed
feat(core): add serializeSync method
1 parent f7c9131 commit d6d749f

File tree

12 files changed

+138
-12
lines changed

12 files changed

+138
-12
lines changed

packages/core/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# API Elements: Core
22

3+
## 0.2.1 (2020-08-27)
4+
5+
### Enhancements
6+
7+
Added `serializeSync` method to Fury.
8+
39
## 0.2.0 (2020-08-05)
410

511
This package updates the version of `api-elements` being used. See

packages/core/lib/fury.js

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ const findAdapter = (adapters, mediaType, method) => {
8080
* @memberof FuryAdapter
8181
*/
8282

83+
/**
84+
* @function serializeSync
85+
*
86+
* @param {Object} options
87+
* @param {Category} options.api
88+
* @param {Namespace} options.namespace
89+
*
90+
* @returns {(String|null)}
91+
*
92+
* @memberof FuryAdapter
93+
*/
94+
8395
/**
8496
*/
8597
class Fury {
@@ -244,6 +256,33 @@ class Fury {
244256
return promise;
245257
}
246258

259+
/**
260+
* Serialize synchronously an API Description into the given output format.
261+
*
262+
* @param {Object} options
263+
* @param {Category} options.api
264+
* @param {string} [options.mediaType]
265+
*/
266+
serializeSync({ api, mediaType = 'text/vnd.apiblueprint' }) {
267+
const adapter = findAdapter(this.adapters, mediaType, 'serialize');
268+
269+
if (!adapter) {
270+
// eslint-disable-next-line no-console
271+
console.warn('Media type did not match any registered serializer!');
272+
273+
return null;
274+
}
275+
276+
if (!api) {
277+
// eslint-disable-next-line no-param-reassign
278+
api = new this.minim.elements.Category();
279+
}
280+
281+
return adapter.serialize({
282+
api, namespace: this.minim, mediaType, sync: true,
283+
});
284+
}
285+
247286
/**
248287
* @callback SerializeCallback
249288
*
@@ -252,7 +291,7 @@ class Fury {
252291
*/
253292

254293
/**
255-
* Serialize an API Description into the given output format.
294+
* Serialize asynchronously an API Description into the given output format.
256295
*
257296
* @param {Object} options
258297
* @param {Category} options.api
@@ -278,7 +317,9 @@ class Fury {
278317
api = new this.minim.elements.Category();
279318
}
280319

281-
const promise = adapter.serialize({ api, namespace: this.minim, mediaType });
320+
const promise = adapter.serialize({
321+
api, namespace: this.minim, mediaType, sync: false,
322+
});
282323

283324
if (done) {
284325
promise.then(result => done(null, result), done);

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@apielements/core",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "API Description SDK",
55
"author": "Apiary.io <[email protected]>",
66
"license": "MIT",

packages/core/test/serialize-test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,28 @@ describe('Serialize', () => {
108108
expect(result).to.equal('{"element":"category"}');
109109
});
110110
});
111+
112+
describe('using serializeSync', () => {
113+
it('returns null with unknown mediaType', () => {
114+
const fury = new Fury();
115+
const api = new fury.minim.elements.Category();
116+
117+
const result = fury.serializeSync({ api, mediaType: 'application/unregistered' });
118+
expect(result).to.equal(null);
119+
});
120+
121+
it('can serialize with matching adapter', async () => {
122+
const fury = new Fury();
123+
fury.use({
124+
name: 'json',
125+
mediaTypes: ['application/json'],
126+
serialize: ({ api, namespace }) => JSON.stringify(namespace.serialiser.serialise(api)),
127+
});
128+
129+
const api = new fury.minim.elements.Category();
130+
131+
const result = fury.serializeSync({ api, mediaType: 'application/json' });
132+
expect(result).to.equal('{"element":"category"}');
133+
});
134+
});
111135
});

packages/form-serializer/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Form Serializer Changelog
22

3+
## 0.1.3 (2020-08-27)
4+
5+
### Enhancements
6+
7+
- Enable synchronous serialization.
8+
39
## 0.1.2 (2020-08-19)
410

511
### Enhancements

packages/form-serializer/lib/adapter.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ const serializeForm = require('./serializeForm');
33
const name = 'form';
44
const mediaTypes = ['multipart/form-data', 'application/x-www-form-urlencoded'];
55

6-
function serialize({ api, mediaType }) {
6+
function serialize({ api, mediaType, sync }) {
7+
if (sync) {
8+
return serializeForm({ api, mediaType });
9+
}
10+
711
return new Promise(resolve => resolve(serializeForm({ api, mediaType })));
812
}
913

packages/form-serializer/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@apielements/form-serializer",
3-
"version": "0.1.2",
3+
"version": "0.1.3",
44
"description": "Multipart/form-data serializer for API Elements",
55
"author": "Apiary.io <[email protected]>",
66
"license": "MIT",

packages/form-serializer/test/adapter-test.js

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe('Form Serializer Adapter', () => {
1919
expect(adapter.mediaTypes).to.include('multipart/form-data');
2020
});
2121

22-
it('can serialize an object element', (done) => {
22+
it('can serialize an object element asynchronously', (done) => {
2323
const element = new fury.minim.elements.Object({ message: 'Hello world' });
2424

2525
fury.serialize({ api: element, mediaType: 'multipart/form-data' }, (error, result) => {
@@ -29,7 +29,14 @@ describe('Form Serializer Adapter', () => {
2929
});
3030
});
3131

32-
it('can serialize a string element', (done) => {
32+
it('can serialize an object element synchronously', () => {
33+
const element = new fury.minim.elements.Object({ message: 'Hello world' });
34+
const result = fury.serializeSync({ api: element, mediaType: 'multipart/form-data' });
35+
36+
expect(result).to.equal('--BOUNDARY\r\nContent-Disposition: form-data; name="message"\r\n\r\nHello world\r\n--BOUNDARY--\r\n');
37+
});
38+
39+
it('can serialize a string element asynchronously', (done) => {
3340
const element = new fury.minim.elements.String('Hello world');
3441

3542
fury.serialize({ api: element, mediaType: 'multipart/form-data' }, (error, result) => {
@@ -38,6 +45,13 @@ describe('Form Serializer Adapter', () => {
3845
done();
3946
});
4047
});
48+
49+
it('can serialize a string element synchronously', () => {
50+
const element = new fury.minim.elements.String('Hello world');
51+
const result = fury.serializeSync({ api: element, mediaType: 'multipart/form-data' });
52+
53+
expect(result).to.equal('--BOUNDARY\r\nContent-Disposition: form-data; name="undefined"\r\n\r\nHello world\r\n--BOUNDARY--\r\n');
54+
});
4155
});
4256

4357
describe('Application/x-www-form-urlencoded mediaType', () => {
@@ -49,7 +63,7 @@ describe('Form Serializer Adapter', () => {
4963
expect(adapter.mediaTypes).to.include('application/x-www-form-urlencoded');
5064
});
5165

52-
it('can serialize an object element', (done) => {
66+
it('can serialize an object element asynchronoulsy', (done) => {
5367
const element = new fury.minim.elements.Object({ message: 'Hello world' });
5468

5569
fury.serialize({ api: element, mediaType: 'application/x-www-form-urlencoded' }, (error, result) => {
@@ -59,7 +73,14 @@ describe('Form Serializer Adapter', () => {
5973
});
6074
});
6175

62-
it('can serialize a string element', (done) => {
76+
it('can serialize an object element synchronoulsy', () => {
77+
const element = new fury.minim.elements.Object({ message: 'Hello world' });
78+
const result = fury.serializeSync({ api: element, mediaType: 'application/x-www-form-urlencoded' });
79+
80+
expect(result).to.equal('message=Hello%20world');
81+
});
82+
83+
it('can serialize a string element asynchronously', (done) => {
6384
const element = new fury.minim.elements.String('Hello world');
6485

6586
fury.serialize({ api: element, mediaType: 'application/x-www-form-urlencoded' }, (error, result) => {
@@ -68,5 +89,12 @@ describe('Form Serializer Adapter', () => {
6889
done();
6990
});
7091
});
92+
93+
it('can serialize a string element synchronously', () => {
94+
const element = new fury.minim.elements.String('Hello world');
95+
const result = fury.serializeSync({ api: element, mediaType: 'application/x-www-form-urlencoded' });
96+
97+
expect(result).to.equal('undefined=Hello%20world');
98+
});
7199
});
72100
});

packages/json-serializer/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# JSON Serializer Changelog
22

3+
## 0.1.4 (2020-08-27)
4+
5+
### Enhancements
6+
7+
- Enable synchronous serialization.
8+
39
## 0.1.3 (2020-08-20)
410

511
### Enhancements

packages/json-serializer/lib/adapter.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ const mediaTypes = [
55
'application/json',
66
];
77

8-
function serialize({ api }) {
8+
function serialize({ api, sync }) {
9+
if (sync) {
10+
return serializeJSON(api);
11+
}
12+
913
return new Promise(resolve => resolve(serializeJSON(api)));
1014
}
1115

0 commit comments

Comments
 (0)