Skip to content

Commit 552ef19

Browse files
committed
feat: make Api, Field and Resource more flexible
feat: add Flow type feat: add .editorconfig
1 parent 324b92a commit 552ef19

File tree

8 files changed

+127
-36
lines changed

8 files changed

+127
-36
lines changed

.babelrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"plugins": [
3-
"transform-runtime"
3+
"transform-runtime",
4+
"transform-flow-strip-types"
45
],
56
"presets": [
67
"es2015",

.editorconfig

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# EditorConfig helps developers define and maintain consistent
2+
# coding styles between different editors and IDEs
3+
# editorconfig.org
4+
5+
root = true
6+
7+
8+
[*]
9+
10+
# Change these settings to your own preference
11+
indent_style = space
12+
indent_size = 2
13+
14+
# We recommend you to keep these unchanged
15+
end_of_line = lf
16+
charset = utf-8
17+
trim_trailing_whitespace = true
18+
insert_final_newline = true
19+
20+
[*.md]
21+
trim_trailing_whitespace = false

.flowconfig

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[ignore]
2+
3+
[include]
4+
5+
[libs]
6+
7+
[options]

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
"babel-core": "^6.24.0",
2020
"babel-eslint": "^7.2.0",
2121
"babel-jest": "^19.0.0",
22+
"babel-plugin-transform-flow-strip-types": "^6.22.0",
2223
"babel-plugin-transform-runtime": "^6.23.0",
2324
"babel-preset-es2015": "^6.24.0",
2425
"babel-preset-stage-0": "^6.22.0",
2526
"eslint": "^3.18.0",
2627
"eslint-plugin-import": "^2.2.0",
28+
"flow-bin": "^0.42.0",
2729
"jest": "^19.0.2",
2830
"jest-fetch-mock": "^1.0.8"
2931
},
@@ -33,7 +35,7 @@
3335
},
3436
"scripts": {
3537
"test": "jest",
36-
"lint": "eslint src",
38+
"lint": "eslint src && flow check",
3739
"build": "babel src -d lib --ignore '*.test.js'"
3840
},
3941
"jest": {

src/Api.js

+26-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
1+
// @flow
2+
3+
import Resource from './Resource';
4+
5+
type ApiOptions = {
6+
title?: string,
7+
resource?: Map<string, Resource>,
8+
}
9+
110
/**
2-
* @property {string} entrypoint - The URL of the API's entrypoint
3-
* @property {string} title - The title
4-
* @property {Map.<string, Resource>} resources - Resources of this API
11+
* @property {string} entrypoint - The URL of the API's entrypoint
512
*/
613
export default class Api {
7-
constructor(entrypoint, title = '', resources = new Map()) {
14+
entrypoint: string;
15+
16+
/**
17+
* @param {string} entrypoint
18+
* @param {?ApiOptions} options
19+
*/
20+
constructor(entrypoint: string, options: ApiOptions = {}) {
821
this.entrypoint = entrypoint;
9-
this.title = title;
10-
this.resources = resources;
22+
23+
Object.keys(options).forEach((key) => {
24+
Object.defineProperty(this, key, {
25+
readable: true,
26+
writable: true,
27+
enumerable: true,
28+
value: options[key],
29+
});
30+
});
1131
}
1232
}

src/Field.js

+27-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
1+
// @flow
2+
3+
type FieldOptions = {
4+
id?: string,
5+
range?: string,
6+
reference?: string,
7+
required?: boolean,
8+
description?: string,
9+
}
10+
111
/**
2-
* @property {string} name - The name of this field
3-
* @property {?string} id - The IRI identifying this field (e.g. http://schema.org/email)
4-
* @property {?string} range - The range of this field (e.g. http://www.w3.org/2001/XMLSchema#string or http://schema.org/Book)
5-
* @property {?string} reference - The name of the referenced resource
6-
* @property {boolean} required - Is this field mandatory?
7-
* @property {string} description - A description of this field
12+
* @property {string} name - The name of this field
813
*/
914
export default class Field {
10-
constructor(name, id = null, range = null, reference = null, required = false, description = '') {
15+
name: string;
16+
17+
/**
18+
* @param {string} name
19+
* @param {?FieldOptions} options
20+
*/
21+
constructor(name: string, options: FieldOptions = {}) {
1122
this.name = name;
12-
this.id = id;
13-
this.range = range;
14-
this.reference = reference;
15-
this.required = required;
16-
this.description = description;
23+
24+
Object.keys(options).forEach((key) => {
25+
Object.defineProperty(this, key, {
26+
readable: true,
27+
writable: true,
28+
enumerable: true,
29+
value: options[key],
30+
});
31+
});
1732
}
1833
}

src/Resource.js

+28-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,37 @@
1+
// @flow
2+
3+
import Field from './Field';
4+
5+
type ResourceOptions = {
6+
id?: string,
7+
readableFields?: Field[],
8+
writableFields?: Field[],
9+
}
10+
111
/**
212
* @property {string} name - The name of the resource
313
* @property {string} url - The base URL for this resource
4-
* @property {?string} id - The id of the resource (e.g. http://schema.org/Book)
5-
* @property {Field[]} readableFields - The list of readable fields
6-
* @property {Field[]} writableFields - The list of writable fields
714
*/
815
export default class Resource {
9-
constructor(name, url, id = null, readableFields = [], writableFields = []) {
16+
name: string;
17+
url: string;
18+
19+
/**
20+
* @param {string} name
21+
* @param {string} url
22+
* @param {ResourceOptions} options
23+
*/
24+
constructor(name: string, url: string, options: ResourceOptions = {}) {
1025
this.name = name;
1126
this.url = url;
12-
this.id = id;
13-
this.readableFields = readableFields;
14-
this.writableFields = writableFields;
27+
28+
Object.keys(options).forEach((key) => {
29+
Object.defineProperty(this, key, {
30+
readable: true,
31+
writable: true,
32+
enumerable: true,
33+
value: options[key],
34+
});
35+
});
1536
}
1637
}

src/hydra/parseHydraDocumentation.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,13 @@ export default function parseHydraDocumentation(entrypointUrl) {
118118

119119
const field = new Field(
120120
supportedProperty['http://www.w3.org/ns/hydra/core#property'][0]['http://www.w3.org/2000/01/rdf-schema#label'][0]['@value'],
121-
supportedProperty['http://www.w3.org/ns/hydra/core#property'][0]['@id'],
122-
range,
123-
'http://www.w3.org/ns/hydra/core#Link' === supportedProperty['http://www.w3.org/ns/hydra/core#property'][0]['@type'][0] ? range : null, // Will be updated in a subsequent pass
124-
supportedProperty['http://www.w3.org/ns/hydra/core#required'] ? supportedProperty['http://www.w3.org/ns/hydra/core#required'][0]['@value'] : false,
125-
supportedProperty['http://www.w3.org/ns/hydra/core#description'] ? supportedProperty['http://www.w3.org/ns/hydra/core#description'][0]['@value'] : ''
121+
{
122+
id: supportedProperty['http://www.w3.org/ns/hydra/core#property'][0]['@id'],
123+
range,
124+
reference: 'http://www.w3.org/ns/hydra/core#Link' === supportedProperty['http://www.w3.org/ns/hydra/core#property'][0]['@type'][0] ? range : null, // Will be updated in a subsequent pass
125+
required: supportedProperty['http://www.w3.org/ns/hydra/core#required'] ? supportedProperty['http://www.w3.org/ns/hydra/core#required'][0]['@value'] : false,
126+
description: supportedProperty['http://www.w3.org/ns/hydra/core#description'] ? supportedProperty['http://www.w3.org/ns/hydra/core#description'][0]['@value'] : ''
127+
},
126128
);
127129

128130
fields.push(field);
@@ -139,9 +141,11 @@ export default function parseHydraDocumentation(entrypointUrl) {
139141
resources.push(new Resource(
140142
guessNameFromUrl(entrypoint[0][property['@id']][0]['@id'], entrypointUrl),
141143
entrypoint[0][property['@id']][0]['@id'],
142-
supportedClass['@id'],
143-
readableFields,
144-
writableFields
144+
{
145+
id: supportedClass['@id'],
146+
readableFields,
147+
writableFields
148+
}
145149
));
146150

147151
break;
@@ -155,6 +159,6 @@ export default function parseHydraDocumentation(entrypointUrl) {
155159
}
156160
}
157161

158-
return new Api(entrypointUrl, title, resources);
162+
return new Api(entrypointUrl, {title, resources});
159163
});
160164
}

0 commit comments

Comments
 (0)