-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathindex.ts
More file actions
299 lines (273 loc) · 7.24 KB
/
Copy pathindex.ts
File metadata and controls
299 lines (273 loc) · 7.24 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import { Endpoints } from "@octokit/types";
import { csvParse, tsvParse } from "d3-dsv";
import store from "store2";
import wretch from "wretch";
import YAML from "yaml";
import { Repo } from "../types";
export type listCommitsResponse =
Endpoints["GET /repos/{owner}/{repo}/commits"]["response"];
const githubApiURL = `https://api.github.com`;
const cachedPat = store.get("flat-viewer-pat");
let githubWretch = cachedPat
? wretch(githubApiURL).auth(`token ${cachedPat}`)
: wretch(githubApiURL);
export async function fetchFlatYaml(repo: Repo) {
let res;
try {
res = await fetchFile(
`https://raw.githubusercontent.com/${repo.owner}/${repo.name}/main/.github/workflows/flat.yaml`
);
} catch (e) {
try {
res = await fetchFile(
`https://raw.githubusercontent.com/${repo.owner}/${repo.name}/main/.github/workflows/flat.yml`
);
} catch (e) {
throw new Error("Flat YAML not found");
}
}
return res && res.length > 0;
}
export function fetchFile(url: string) {
return wretch()
.url(url)
.get()
.notFound(() => {
throw new Error("File not found");
})
.text((res) => {
return res;
});
}
const ignoredFiles = ["package.json", "tsconfig.json"];
const ignoredFolders = [".vscode", ".github"];
const getFilesFromRes = (res: any) => {
return res.tree
.map((file: any) => file.path)
.filter((path: string) => {
const extension = path.split(".").pop() || "";
const validExtensions = [
"csv",
"tsv",
"json",
"geojson",
"topojson",
"yml",
"yaml",
];
return (
validExtensions.includes(extension) &&
!ignoredFiles.includes(path.split("/").slice(-1)[0]) &&
!ignoredFolders.includes(path.split("/")[0])
);
});
};
function tryBranch(owner: string, name: string, branch: string) {
return githubWretch
.url(`/repos/${owner}/${name}/git/trees/${branch}?recursive=1`)
.get()
.notFound((e) => {
throw new Error("File not found");
})
.error(401, () => {
// clear PAT
store.remove("flat-viewer-pat");
console.log("PAT expired");
githubWretch = wretch(githubApiURL);
})
.error(403, (e: any) => {
const message = JSON.parse(e.message).message;
if (message.includes("API rate limit exceeded")) {
throw new Error("Rate limit exceeded");
}
throw new Error(e);
})
.json((res) => {
return getFilesFromRes(res);
});
}
export async function fetchFilesFromRepo({ owner, name }: Repo) {
try {
const files = await tryBranch(owner, name, "main");
if (typeof files !== "string") return files;
} catch (e) {
try {
const files = await tryBranch(owner, name, "master");
if (typeof files !== "string") return files;
} catch (e) {
if (e.message == "Rate limit exceeded") {
throw new Error("Rate limit exceeded");
}
throw new Error(e);
}
}
}
export interface FileParams {
filename?: string | null;
owner: string;
name: string;
}
export interface FileParamsWithSHA extends FileParams {
sha: string;
}
export function fetchCommits(params: FileParams) {
const { name, owner, filename } = params;
return githubWretch
.url(`/repos/${owner}/${name}/commits`)
.query({
path: filename,
})
.get()
.json<listCommitsResponse["data"]>((res: any) => {
if (res.length === 0) {
throw new Error("No commits...");
}
return res;
});
}
export async function fetchDataFile(params: FileParamsWithSHA) {
const { filename, name, owner, sha } = params;
if (!filename) return [];
const fileType = filename.split(".").pop() || "";
const validTypes = [
"csv",
"tsv",
"json",
"geojson",
"topojson",
"yml",
"yaml",
];
if (!validTypes.includes(fileType)) return [];
const text = await wretch(
`https://raw.githubusercontent.com/${owner}/${name}/${sha}/${filename}`
)
.get()
.notFound(async () => {
if (cachedPat) {
const data = await githubWretch
.url(`/repos/${owner}/${name}/contents/${filename}`)
.get()
.json();
const content = atob(data.content);
return content;
} else {
throw new Error("Data file not found");
}
})
.text();
let data: any;
try {
if (fileType === "csv") {
data = csvParse(text);
} else if (
["geojson", "topojson"].includes(fileType) ||
filename.endsWith(".geo.json")
) {
data = JSON.parse(text);
if (data.features) {
const features = data.features.map((feature: any) => {
let geometry = {} as Record<string, any>;
Object.keys(feature?.geometry).forEach((key) => {
geometry[`geometry.${key}`] = feature.geometry[key];
});
let properties = {} as Record<string, any>;
Object.keys(feature?.properties).forEach((key) => {
properties[`properties.${key}`] = feature.properties[key];
});
const { geometry: g, properties: p, ...restOfKeys } = feature;
return { ...restOfKeys, ...geometry, ...properties };
});
// make features the first key of the object
const { features: f, ...restOfData } = data;
data = { features, ...restOfData };
}
} else if (fileType === "json") {
data = JSON.parse(text);
} else if (fileType === "tsv") {
data = tsvParse(text);
} else if (fileType === "yml" || fileType === "yaml") {
data = YAML.parse(text);
} else {
return [
{
content: text,
invalidValue: stringifyValue(text),
},
];
}
} catch (e) {
return [
{
content: text,
invalidValue: stringifyValue(text),
},
];
}
if (typeof data !== "object") {
return [
{
content: text,
invalidValue: stringifyValue(data),
},
];
}
const isArray = Array.isArray(data);
if (isArray) {
return [
{
content: text,
value: data,
},
];
}
const keys = Object.keys(data);
const isObjectOfObjects =
keys.length &&
!Object.values(data).find((d) => typeof d !== "object" || Array.isArray(d));
if (!isObjectOfObjects)
return keys.map((key) => {
const value = data[key];
if (!Array.isArray(value)) {
return {
key,
content: text,
invalidValue: stringifyValue(value),
};
}
if (typeof value[0] === "string") {
return {
key,
content: text,
value: value.map((d) => ({ value: d })),
};
}
return {
key,
content: text,
value,
};
});
let parsedData = <any[]>[];
keys.forEach((key) => {
parsedData = [...parsedData, { ...data[key], id: key }];
});
return [
{
content: text,
value: parsedData,
},
];
}
export async function fetchOrgRepos(orgName: string) {
const res = await githubWretch
.url(`/search/repositories`)
.query({ q: `topic:flat-data org:${orgName}`, per_page: 100 })
.get()
.json();
return res.items;
}
const stringifyValue = (data: any) => {
if (typeof data === "object") return JSON.stringify(data, undefined, 2);
return data.toString();
};