-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
68 lines (65 loc) · 1.9 KB
/
index.js
File metadata and controls
68 lines (65 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*!
* Copyright 2024 Digital Bazaar, Inc.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
import {Application} from '@curveball/core';
import browser from '@curveball/browser';
// MUST not end in a trailing slash and will be stripped in response bodies for
// to facilitate local browsing.
const apiBaseUrl = 'https://api.w3.org';
const title = 'W3C API';
// port for this browser app to listen on
const port = 8080;
const app = new Application();
app.use(browser({title, navigationLinks: {
previous: {priority: -1}, next: {priority: 1}}}));
app.use(async ctx => {
console.dir(ctx.request.requestTarget);
if(ctx.request.requestTarget === '/') {
// adding a home page response with some navigation
ctx.response.body = {
_links: {
groups: {
href: '/groups',
title: 'List Groups'
},
specifications: {
href: '/specifications',
title: 'List Specifications'
},
'code-repository': {
href: 'https://github.com/digitalbazaar/w3c-api-browser'
},
alternate: [
{
href: 'https://api.w3.org/doc',
type: 'text/html',
title: 'W3C API Docs'
},
{
href: 'https://api.w3.org/doc.json',
type: 'application/json',
title: 'W3C API Swagger'
}
]
}
};
} else {
const newUrl = new URL(`${apiBaseUrl}${ctx.request.requestTarget}`);
if(!newUrl.searchParams.has('embed')) {
newUrl.searchParams.append('embed', 'true');
}
const resp = await fetch(newUrl, {
headers: {
Accept: 'application/json'
}
});
const rv = await resp.json();
const localhosted = JSON.stringify(rv).replaceAll(apiBaseUrl, '');
ctx.response.body = localhosted;
ctx.response.type = 'application/hal+json';
}
});
app.listen(port);
console.log(`Listening on http://localhost:${port}`);