give out .json and .csv, not only .ndjson
This commit is contained in:
parent
2593be8911
commit
124507d074
2 changed files with 27 additions and 1 deletions
14
index.ts
14
index.ts
|
|
@ -1,5 +1,5 @@
|
|||
import { parseHTML } from "linkedom"
|
||||
import { ndjsonToJson, prettyStringify, requireBasicAuth, tripleBool, type Change, type Entry } from "./lib";
|
||||
import { jsonToCsv, ndjsonToJson, prettyStringify, requireBasicAuth, tripleBool, type Change, type Entry } from "./lib";
|
||||
import * as fs from "fs/promises"
|
||||
import { existsSync } from "fs";
|
||||
import { $ } from "bun"
|
||||
|
|
@ -193,6 +193,18 @@ export class ClientResponse extends Response {
|
|||
Bun.serve({
|
||||
routes: {
|
||||
"/": () => new ClientResponse("Sheets v2"),
|
||||
"/artists.json": async () => ClientResponse.json(
|
||||
ndjsonToJson((await fs.readFile("artists.ndjson")).toString("utf8"))
|
||||
),
|
||||
"/artists.csv": async () => new ClientResponse(
|
||||
jsonToCsv(
|
||||
ndjsonToJson((await fs.readFile("artists.ndjson")).toString("utf8")
|
||||
)
|
||||
), {
|
||||
headers: {
|
||||
"Content-Type": "text/csv"
|
||||
}
|
||||
}),
|
||||
"/artists.ndjson": async () => new ClientResponse(await fs.readFile("artists.ndjson")),
|
||||
"/th_artists.ndjson": async () => new ClientResponse(await fs.readFile("th_artists.ndjson")),
|
||||
"/ignore-th/:id": async (req) => {
|
||||
|
|
|
|||
14
lib.ts
14
lib.ts
|
|
@ -17,6 +17,20 @@ export const thIgnore = [
|
|||
"bpmkeytracker",
|
||||
]
|
||||
|
||||
export function jsonToCsv(data: unknown|unknown[]) {
|
||||
const arr = Array.isArray(data) ? data : [data];
|
||||
const headers = [...new Set(arr.flatMap(o => Object.keys(o)))];
|
||||
|
||||
const escape = (v: string )=>
|
||||
`"${String(v ?? "").replace(/"/g, '""')}"`;
|
||||
|
||||
const rows = arr.map(o =>
|
||||
headers.map(h => escape(o[h])).join(",")
|
||||
);
|
||||
|
||||
return [headers.join(","), ...rows].join("\n");
|
||||
}
|
||||
|
||||
export enum TripleBool {
|
||||
MOSTLY = 2,
|
||||
YES = 1,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue