Skip to content

Commit 1e2e692

Browse files
committed
add component(s)
1 parent df50b25 commit 1e2e692

File tree

6 files changed

+701
-25
lines changed

6 files changed

+701
-25
lines changed

__tests__/__snapshots__/index.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Array [
1212
exports[`expect res setHeader and end to match snapshot 2`] = `
1313
Array [
1414
Array [
15-
"{\\"data\\":{\\"registry\\":{\\"href\\":\\"http://mock:3000/\\",\\"ocVersion\\":\\"8.1.5\\",\\"type\\":\\"mock-registry\\"}}}",
15+
"{\\"data\\":{\\"registry\\":{\\"href\\":\\"http://mock:3000/\\",\\"ocVersion\\":\\"1.2.3\\",\\"type\\":\\"mock-registry\\"},\\"components\\":[{\\"name\\":\\"oc-a-component\\",\\"description\\":\\"Awesome OpenComponent\\",\\"version\\":\\"4.5.6\\"}],\\"component\\":{\\"name\\":\\"oc-a-component\\",\\"description\\":\\"Awesome OpenComponent\\",\\"version\\":\\"4.5.6\\"}}}",
1616
],
1717
]
1818
`;

__tests__/component.info.sample.json

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"name": "oc-timeago",
3+
"description": "This is sometimes called timeago or relative time.",
4+
"version": "1.0.0",
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/mattiaerre/oc-hub/tree/master/oc-timeago"
8+
},
9+
"author": {
10+
"name": "Mattia Richetto",
11+
"email": "[email protected]"
12+
},
13+
"oc": {
14+
"files": {
15+
"template": {
16+
"type": "handlebars",
17+
"hashKey": "3fbbf532ab7ae9bceff951d6ef106c802cc66e9b",
18+
"src": "template.js"
19+
},
20+
"dataProvider": {
21+
"type": "node.js",
22+
"hashKey": "d8b90f82f573d57ec32e9c268642e6709b3b073d",
23+
"src": "server.js"
24+
},
25+
"static": []
26+
},
27+
"parameters": {
28+
"start": {
29+
"type": "string",
30+
"mandatory": true,
31+
"example": "2016-02-22",
32+
"description": "The start date as string in a YYYY-MM-DD format"
33+
},
34+
"end": {
35+
"type": "string",
36+
"mandatory": false,
37+
"example": "2017-02-22",
38+
"description": "The end date as string in a YYYY-MM-DD format"
39+
}
40+
},
41+
"version": "0.36.1",
42+
"packaged": true,
43+
"date": 1489239097651
44+
},
45+
"scripts": {
46+
"test": "jest"
47+
},
48+
"dependencies": {
49+
"jest": "^19.0.1",
50+
"moment": "^2.17.1"
51+
},
52+
"allVersions": [
53+
"1.0.0"
54+
],
55+
"requestVersion": ""
56+
}

__tests__/index.test.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@ test('expect type of middleware to be function', () => {
1515
test('expect res setHeader and end to match snapshot', async () => {
1616
fetch.mockResponse(JSON.stringify({
1717
href: options.baseUrl,
18-
ocVersion: '8.1.5',
19-
type: 'mock-registry'
18+
ocVersion: '1.2.3',
19+
type: 'mock-registry',
20+
components: [
21+
`${options.baseUrl}oc-a-component`
22+
],
23+
name: 'oc-a-component',
24+
description: 'Awesome OpenComponent',
25+
version: '4.5.6'
2026
}));
2127

2228
const req = {
@@ -29,6 +35,16 @@ test('expect res setHeader and end to match snapshot', async () => {
2935
ocVersion
3036
type
3137
}
38+
components {
39+
name
40+
description
41+
version
42+
}
43+
component(name: "oc-a-component") {
44+
name
45+
description
46+
version
47+
}
3248
}
3349
`
3450
};

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "OpenComponent Registry GraphQL Express middleware.",
55
"main": "index.js",
66
"scripts": {
7+
"npm-check": "npm-check",
78
"test": "jest --coverage"
89
},
910
"repository": {
@@ -36,9 +37,11 @@
3637
"eslint-config-airbnb": "^14.1.0",
3738
"eslint-plugin-import": "^2.2.0",
3839
"eslint-plugin-jsx-a11y": "^5.0.1",
40+
"eslint-plugin-react": "^7.0.0",
3941
"isomorphic-fetch": "^2.2.1",
4042
"jest": "^20.0.0",
41-
"jest-fetch-mock": "^1.1.1"
43+
"jest-fetch-mock": "^1.1.1",
44+
"npm-check": "^5.4.0"
4245
},
4346
"dependencies": {
4447
"express-graphql": "^0.6.4",

src/index.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,37 @@ const schema = buildSchema(`
1111
type: String
1212
}
1313
14+
type Component {
15+
name: String
16+
description: String
17+
version: String
18+
}
19+
1420
type Query {
1521
registry: Registry
22+
component(name: String): Component
23+
components: [Component]
1624
}
1725
`);
1826

27+
const fetchComponent = async (url) => {
28+
return fetch(url)
29+
.then(response => response.json())
30+
.then((data) => {
31+
return data;
32+
});
33+
};
34+
35+
const makeComponent = async (baseUrl, name) => {
36+
const url = `${baseUrl}${name}/~info`;
37+
const info = await fetchComponent(url);
38+
const copy = Object.assign({}, info);
39+
return copy;
40+
};
41+
1942
const root = (options) => {
2043
return {
21-
registry: () => {
44+
registry: async () => {
2245
return fetch(options.baseUrl)
2346
.then(response => response.json())
2447
.then((data) => {
@@ -28,6 +51,20 @@ const root = (options) => {
2851
type: data.type
2952
};
3053
});
54+
},
55+
components: async () => {
56+
return fetch(options.baseUrl)
57+
.then(response => response.json())
58+
.then((data) => {
59+
return data.components
60+
.map((component) => {
61+
const name = component.replace(options.baseUrl, '');
62+
return makeComponent(options.baseUrl, name);
63+
});
64+
});
65+
},
66+
component: async ({ name }) => {
67+
return makeComponent(options.baseUrl, name);
3168
}
3269
};
3370
};

0 commit comments

Comments
 (0)