Public
Script
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

Notehub

A set of helper functions for interacting with the Notehub API

Requires NOTEHUB_CLIENT_ID and NOTEHUB_CLIENT_SECRET environment variables to be configured.

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
// Login into the Notehub API with a programmatic access client ID and client secret
// Returns the api token to be used for subsequent requests
const authenticate = async (): Promise<string> => {
const body = new URLSearchParams();
body.append("grant_type", "client_credentials");
body.append("client_id", Deno.env.get("NOTEHUB_CLIENT_ID"));
body.append("client_secret", Deno.env.get("NOTEHUB_CLIENT_SECRET"));
const res = await fetch("https://notehub.io/oauth2/token", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body,
});
const data = await res.json();
return data.access_token;
};
// Push a note for the configured project, device, and file to Notehub
export const pushNote = async (projectUID, deviceUID, fileName, payload): Promise<boolean> => {
try {
const token = await authenticate();
const res = await fetch(
`https://api.notefile.net/v1/projects/${projectUID}/devices/${deviceUID}/notes/${fileName}`,
{
method: "POST",
headers: {
"Authorization": `Bearer ${token}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ body: payload }),
},
);
if (res.ok) return true;
throw new Error("Failed to push note to Notehub API");
} catch (e) {
console.log(e);
return false;
}
};
const notehub = { pushNote };
export default notehub;
April 11, 2024