Readme

Imports Gists to Val Town

Import your TypeScript and JavaScript to Val Town

import-gists.png

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:

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

Example usage: https://www.val.town/v/maas.untitled_harlequinCrawdad

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
import { createVal } from "https://esm.town/v/stevekrouse/createVal?v=2";
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.`;
});
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.