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

Imports Gists to Val Town

Import your TypeScript and JavaScript to Val Town

Authentication

This function requires two keys: one from Github to get your gists, and one from Val Town to make the vals in your account:

  1. Github token: https://github.com/settings/tokens
  2. Val Town key: https://www.val.town/settings/api

Usage

You can use this function by calling it and passing your keys like so:

@stevekrouse.importGists({
  githubToken: @me.secrets.githubGists,
  valTownKey: @me.secrets.valtown,
});

Example usage: https://www.val.town/v/stevekrouse.untitled_redAardvark

Credits

This val was almost entirely made by @maas. I just forked it and fixed a couple bugs.

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
import { createVal } from "https://esm.town/v/stevekrouse/createVal";
export const importGists = (async ({ githubToken, valTownKey }: {
githubToken: string;
valTownKey: string;
}) => {
const supportedLanguages = ["JavaScript", "TypeScript"];
let imported = [];
const headers = { "X-GitHub-Api-Version": "2022-11-28" };
const { Octokit } = await import("npm:@octokit/core");
const octokit = new Octokit({ auth: githubToken });
let { data } = await octokit.request("GET /gists", { headers });
for (let i in data) {
let files = Object.values(data[i].files);
let { filename, language } = files[0];
if (!supportedLanguages.includes(language)) {
continue;
}
let gist = await octokit.request("GET /gists/{gist_id}", {
gist_id: data[i].id,
});
let { content } = Object.values(gist.data.files)[0];
createVal({ code: content, valTownKey: valTownKey });
imported.push(filename);
}
return `Imported ${imported.length} vals (${
imported.join(", ")
}) from GitHub Gists.`;
});
// Forked from @maas.importGists
October 23, 2023