Skip to content

Commit 0a5ca3d

Browse files
committed
Replace @whatwg-node/router with FETS
1 parent 60c5c44 commit 0a5ca3d

File tree

10 files changed

+200
-199
lines changed

10 files changed

+200
-199
lines changed

examples/hive-example/sources/authors/.meshrc.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ sources:
22
- name: Authors
33
handler:
44
openapi:
5-
source: ./openapi.yaml
5+
source: http://localhost:4001/openapi.json

examples/hive-example/sources/authors/openapi.yaml

-49
This file was deleted.

examples/hive-example/sources/authors/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"start": "ts-node src/index.ts"
1010
},
1111
"dependencies": {
12-
"@whatwg-node/router": "0.3.0",
12+
"fets": "^0.1.4",
1313
"ts-node": "10.9.1",
1414
"typescript": "5.0.4"
1515
},

examples/hive-example/sources/authors/src/index.ts

+75-31
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
import { createServer } from 'http';
2-
import { createRouter, Response } from '@whatwg-node/router';
2+
import { createRouter, Response } from 'fets';
33

4-
interface Author {
5-
id: string;
6-
name: string;
7-
email: string;
8-
}
9-
10-
const authors: Author[] = [
4+
const authors = [
115
{
126
id: '1',
137
name: 'John Doe',
@@ -21,37 +15,87 @@ const authors: Author[] = [
2115
];
2216

2317
async function main() {
24-
const app = createRouter();
25-
26-
app.get(
27-
'/authors',
28-
() =>
29-
new Response(JSON.stringify(authors), {
30-
headers: {
31-
'Content-Type': 'application/json',
18+
const app = createRouter({
19+
components: {
20+
schemas: {
21+
Author: {
22+
type: 'object',
23+
properties: {
24+
id: {
25+
type: 'string',
26+
},
27+
name: {
28+
type: 'string',
29+
},
30+
email: {
31+
type: 'string',
32+
},
33+
},
34+
required: ['id', 'name', 'email'],
35+
additionalProperties: false,
3236
},
33-
}),
34-
);
35-
36-
app.get('/authors/:id', req => {
37-
const author = authors.find(author => author.id === req.params.id);
37+
},
38+
} as const,
39+
})
40+
.route({
41+
method: 'GET',
42+
path: '/authors',
43+
operationId: 'authors',
44+
description: 'Returns a list of authors',
45+
schemas: {
46+
responses: {
47+
200: {
48+
description: 'A list of authors',
49+
type: 'array',
50+
items: {
51+
$ref: '#/components/schemas/Author',
52+
},
53+
},
54+
},
55+
} as const,
56+
handler: () => Response.json(authors),
57+
})
58+
.route({
59+
method: 'GET',
60+
path: '/authors/:id',
61+
operationId: 'author',
62+
description: 'Returns a single author',
63+
schemas: {
64+
request: {
65+
params: {
66+
type: 'object',
67+
properties: {
68+
id: {
69+
type: 'string',
70+
},
71+
},
72+
additionalProperties: false,
73+
required: ['id'],
74+
},
75+
},
76+
responses: {
77+
200: {
78+
description: 'The author',
79+
$ref: '#/components/schemas/Author',
80+
},
81+
},
82+
} as const,
83+
handler(req) {
84+
const author = authors.find(author => author.id === req.params.id);
3885

39-
if (!author) {
40-
return new Response(null, {
41-
status: 404,
42-
});
43-
}
86+
if (!author) {
87+
return new Response(null, {
88+
status: 404,
89+
});
90+
}
4491

45-
return new Response(JSON.stringify(author), {
46-
headers: {
47-
'Content-Type': 'application/json',
92+
return Response.json(author, { status: 200 });
4893
},
4994
});
50-
});
5195

5296
const server = createServer(app);
5397
server.listen(4001, () => {
54-
console.log('Authors service listening on port 4001');
98+
console.log('Authors service Swagger UI; http://localhost:4001/docs');
5599
});
56100
}
57101

examples/hive-example/sources/books/.meshrc.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ sources:
22
- name: Books
33
handler:
44
openapi:
5-
source: ./openapi.yaml
5+
source: http://localhost:4002/openapi.json

examples/hive-example/sources/books/openapi.yaml

-70
This file was deleted.

examples/hive-example/sources/books/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"start": "ts-node src/index.ts"
1010
},
1111
"dependencies": {
12-
"@whatwg-node/router": "0.3.0",
12+
"fets": "^0.1.4",
1313
"ts-node": "10.9.1",
1414
"typescript": "5.0.4"
1515
},

0 commit comments

Comments
 (0)