Published
Edited
Feb 13, 2020
Importers
4 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
viewof ghKey = secret("GITHUB_TOKEN", {
description:
"This is a secret API token whose value is only available to you!",
submit: "Set secret"
})
Insert cell
Insert cell
Insert cell
Insert cell
github4`query {
repository(owner: "observablehq", name: "runtime") {
forkCount
stargazers {
count: totalCount
}
}
}`
Insert cell
Insert cell
ghEmojis = github3`emojis`
Insert cell
Insert cell
{
const rate = (await github3`rate_limit`).rate
const msRemaining = ((new Date(rate.reset*1000))- new Date())
const minsRemaining = (msRemaining/1000/60).toFixed(1)
return md` You have ${rate.remaining} api calls left! Restarts in ${minsRemaining} minutes!`
}
Insert cell
Insert cell
github3 = function(strings) {
let path = '';
if (Array.isArray(strings)) {
path = strings[0] + "";
let i = 0,
n = arguments.length;
while (++i < n) path += arguments[i] + "" + strings[i];
} else path = strings;
let ghToken = ghKey;
const opts = {
headers: {
Accept: `application/vnd.github.v3+json`
}
};
if (ghToken) opts.headers.Authorization = `token ${ghToken}`;
const url =
path.indexOf('https://api.github.com') >= 0
? path
: `https://api.github.com/${path}`;
return fetch(url, opts).then(async r => {
const data = await r.json();
const meta = {};
const rate_limit = {};

if (r.headers.has('Link')) meta.link = parseLH(r.headers.get('Link'));

rate_limit.limit = +r.headers.get('X-RateLimit-Limit');
rate_limit.remaining = +r.headers.get('X-RateLimit-Remaining');
rate_limit.reset = new Date(+r.headers.get('X-RateLimit-Reset') * 1000);

meta.rate_limit = rate_limit;
data.__meta = meta;
return data;
});
}
Insert cell
Insert cell
github4 = function (strings){
let query = strings[0] + "", i = 0, n = arguments.length;
while (++i < n) query += arguments[i] + "" + strings[i];

let ghToken = ghKey
const opts = {
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
method: 'post',
body: JSON.stringify({query})
};
if (ghToken)
opts.headers.Authorization = `token ${ghToken}`
const url = `https://api.github.com/graphql`
return fetch(url, opts).then(r=>r.json())
}
Insert cell
Insert cell
Insert cell
import {secret} from "@tmcw/secret"
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more