-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathcomponent.js
More file actions
34 lines (32 loc) · 1.02 KB
/
component.js
File metadata and controls
34 lines (32 loc) · 1.02 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
const DEFAULT_URL = "https://jsonplaceholder.typicode.com/posts/1";
/**
* Perform a GET request for JSON against a given URL
*
* NOTE: since the input is an option<string>, it is encoded as an optional param
* https://github.com/bytecodealliance/jco/blob/main/docs/src/wit-type-representations.md
*
* @param {string} [rawURL] - URL to send the request
*/
async function getJson(rawURL) {
const url = new URL(rawURL ?? DEFAULT_URL);
let responseJson = {};
const response = await fetch(url);
responseJson = await response.json();
return {
url: rawURL,
// NOTE: We have to stringify the JSON response here because WIT does
// not support recursive types natively.
responseJson: JSON.stringify(responseJson),
};
}
/**
* The exports of this JS module implicitly represent the
* `component` world.
*
* All interfaces exported from the component are expected as top level
* module exports, with relevant functions defined within.
*
*/
export const simpleRequest = {
getJson,
};