karfau/rawUrl

References

Referenced 2 times
1
2
3
4
5
6
7
8
9
10
11
12
import { rawUrl as rawUrl2 } from "https://esm.town/v/karfau/rawUrl";
import { getRaw } from "https://esm.town/v/karfau/getRaw";
export const test_getRaw = {
readme: await getRaw(["karfau", "getRaw"], "md"),
code: await getRaw(["karfau", "getRaw"], "ts"),
// without passing an extension we get a json with all data
json: await getRaw(["karfau", "getRaw"]),
// use the extension to access any existing key in the json
version: await getRaw(["karfau", "getRaw"], "version"),
rawUrl: rawUrl2(["karfau", "getRaw"]),
};

A helper to get the raw data of a val, using the very nice implementation from @pomdtr.raw.

Usage: https://www.val.town/v/karfau.test_getRaw

Also look at @karfau.rawUrl to just get the raw url of another val inside a val.

Readme
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
import { rawUrl } from "https://esm.town/v/karfau/rawUrl";
import { raw } from "https://esm.town/v/pomdtr/raw?v=90";
export const getRaw = async (ref: Ref, extension: Ext = "json") => {
const rawExt = ["ts", "md", "json"].includes(extension) ? extension : "json";
const res = await raw(
new Request(rawUrl(ref, rawExt)),
);
if (!res.ok || rawExt !== "json")
return res.text();
const json = await res.json();
return extension !== "json" && extension in json ? json[extension] : json;
};
type Ref = [
user: string,
valName: string,
];
type ValData = {
"id": string;
"author": {
"id": string;
"username": string;
};
"name": string;
"code": string;
"privacy": "public" | "unlisted";
"public": boolean;
"version": number;
"runStartAt": string;
"runEndAt": string;
"logs": string[];
"output": {
"type": string;
"value": string;
};
"error": string | null;
"readme": string | null;
"likeCount": number;
"referenceCount": number;
};
type Ext = "ts" | "json" | "md" | keyof ValData;
1
Next