-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ module.exports = { | |
'apib', | ||
'apiaryb', | ||
'remote', | ||
'json', | ||
'deps', | ||
'deps-dev', | ||
]], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# API Elements: JSON Serializer | ||
|
||
## Usage | ||
|
||
```javascript | ||
const jsonSerializer = require('@apielements/json-serializer'); | ||
fury.use(jsonSerializer); | ||
|
||
const mediaType = 'application/json'; | ||
fury.serialize({ api, mediaType }, (error, body) => { | ||
console.log(body); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const serializeJSON = require('./serializeJSON'); | ||
|
||
const name = 'json'; | ||
const mediaTypes = [ | ||
'application/json', | ||
]; | ||
|
||
function serialize({ api }) { | ||
return new Promise(resolve => resolve(serializeJSON(api))); | ||
} | ||
|
||
module.exports = { name, mediaTypes, serialize }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
function collectElementByIDs(element) { | ||
const dataStructures = {}; | ||
|
||
const { parents } = element; | ||
if (parents) { | ||
const rootElement = parents.get(0); | ||
|
||
if (rootElement) { | ||
rootElement.recursiveChildren.forEach((element) => { | ||
if (element.id) { | ||
dataStructures[element.id.toValue()] = element; | ||
} | ||
}); | ||
} | ||
} | ||
|
||
return dataStructures; | ||
} | ||
|
||
function serializeJSON(element) { | ||
const dataStructures = collectElementByIDs(element); | ||
const value = element.valueOf(undefined, dataStructures); | ||
return JSON.stringify(value); | ||
} | ||
|
||
module.exports = serializeJSON; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "@apielements/json-serializer", | ||
"version": "0.1.0", | ||
"description": "JSON serializer for API Elements", | ||
"author": "Apiary.io <support@apiary.io>", | ||
"license": "MIT", | ||
"main": "./lib/adapter.js", | ||
"files": [ | ||
"lib/adapter.js" | ||
], | ||
"homepage": "https://github.com/apiaryio/api-elements.js/tree/master/packages/json-serializer", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/apiaryio/api-elements.js.git", | ||
"directory": "packages/json-serializer" | ||
}, | ||
"scripts": { | ||
"lint": "eslint .", | ||
"lint:fix": "eslint . --fix", | ||
"test": "mocha test" | ||
}, | ||
"peerDependencies": { | ||
"fury": "3.0.0-beta.14" | ||
}, | ||
"devDependencies": { | ||
"chai": "^4.2.0", | ||
"eslint": "^5.16.0", | ||
"fury": "3.0.0-beta.14", | ||
"mocha": "^7.1.1" | ||
}, | ||
"engines": { | ||
"node": ">=8" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const { Fury } = require('fury'); | ||
const { expect } = require('chai'); | ||
const adapter = require('../lib/adapter'); | ||
|
||
describe('JSON Serialiser Adapter', () => { | ||
let fury; | ||
|
||
before(() => { | ||
fury = new Fury(); | ||
fury.use(adapter); | ||
}); | ||
|
||
it('has a name', () => { | ||
expect(adapter.name).to.equal('json'); | ||
}); | ||
|
||
it('has a JSON media type', () => { | ||
expect(adapter.mediaTypes).to.deep.equal(['application/json']); | ||
}); | ||
|
||
it('can serialize an element', (done) => { | ||
const element = new fury.minim.elements.String('hello world'); | ||
|
||
fury.serialize({ api: element, mediaType: 'application/json' }, (error, result) => { | ||
expect(error).to.be.null; | ||
expect(result).to.equal('"hello world"'); | ||
done(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const { Fury } = require('fury'); | ||
const { expect } = require('chai'); | ||
const serializeJSON = require('../lib/serializeJSON'); | ||
|
||
const { minim: namespace } = new Fury(); | ||
|
||
describe('#serializeJSON', () => { | ||
it('can serialize a primitive element with value', () => { | ||
const element = new namespace.elements.String('hello world'); | ||
|
||
expect(serializeJSON(element)).to.equal('"hello world"'); | ||
}); | ||
|
||
it('can serialize a primitive element with default value', () => { | ||
const element = new namespace.elements.String(); | ||
element.attributes.set('default', 'hello world'); | ||
|
||
expect(serializeJSON(element)).to.equal('"hello world"'); | ||
}); | ||
|
||
it('can serialize an element with references via parent tree', () => { | ||
const element = new namespace.elements.Element(); | ||
element.element = 'message'; | ||
|
||
new namespace.elements.Category([ | ||
new namespace.elements.Category([ | ||
new namespace.elements.String('hello world', { id: 'message' }), | ||
], { classes: ['dataStructures'] }), | ||
element, | ||
]).freeze(); | ||
|
||
expect(serializeJSON(element)).to.equal('"hello world"'); | ||
}); | ||
}); |