JS Kernel Usage
Usage of the JS Kernel
Just the code snippets I created in this post adjusted to produce console output/show additional function/class usage
$$async$$ = true;
// github graphql query
const query = `query {
repositoryOwner(login: "safinsingh") {
... on ProfileOwner {
itemShowcase {
items(first: 4) {
edges {
node {
... on Repository {
name
description
url
}
}
}
}
}
}
}
}`;
// github api token base64 encoded
const token = decodeURIComponent(
atob("Z2hwX3V1YmN4UzNJZ3kwc1JqTkZFdWllZTZMQ2JFeXcwbzBlR1Njdw==")
);
const main = async () => {
// get projects as array
const projects = await fetch("https://api.github.com/graphql", {
// send query as request body
body: JSON.stringify({ query }),
headers: {
Accept: "application/json",
// use token for authorization
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
method: "POST",
})
.then((r) => r.json())
.then((r) =>
r.data.repositoryOwner.itemShowcase.items.edges.map(
(edge) => edge.node
)
);
const rowFrags = projects.map(
(p) => `
<tr>
<td>${p.name}</td>
<td>${p.description}</td>
<td><a href="${p.url}">${p.url}</a></td>
</tr>
`
);
const tableFrag = `
<table>
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>URL</th>
</tr>
</thead>
<tbody>
${rowFrags.join("\n")}
</tbody>
</table>
`;
$$.html(tableFrag);
};
main()
.then(() => $$done$$("finished"))
.catch((e) => $$done$$("err: " + e));