diff options
author | Vika <vika@fireburn.ru> | 2022-09-29 22:39:28 +0300 |
---|---|---|
committer | Vika <vika@fireburn.ru> | 2022-09-29 22:39:28 +0300 |
commit | b7d4e5c4686bc8aac41d832567190002542a1743 (patch) | |
tree | 6a42a990fbb06057a64de5f68175f94c3637c2c7 /kittybox-rs/companion-lite/src/micropub_api.ts | |
parent | b3508ccb146648950ed392b517d12354203c4347 (diff) | |
download | kittybox-b7d4e5c4686bc8aac41d832567190002542a1743.tar.zst |
companion-lite: port to TypeScript
Diffstat (limited to 'kittybox-rs/companion-lite/src/micropub_api.ts')
-rw-r--r-- | kittybox-rs/companion-lite/src/micropub_api.ts | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/kittybox-rs/companion-lite/src/micropub_api.ts b/kittybox-rs/companion-lite/src/micropub_api.ts new file mode 100644 index 0000000..9eb65a2 --- /dev/null +++ b/kittybox-rs/companion-lite/src/micropub_api.ts @@ -0,0 +1,58 @@ +export interface MicropubChannel { + uid: string, + name: string +} + +export interface MF2 { + type: string[], + properties: { [key:string]: (string | MF2 | {[key:string]: string})[] | undefined } +} + +export interface MicropubConfig { + channels: MicropubChannel[], + "media-endpoint": string +} + +export async function query_channels(endpoint: string, token: string): Promise<MicropubChannel[]> { + const response = await fetch(endpoint + "?q=config", { + headers: { + "Authorization": `Bearer ${token}` + } + }) + + if (response.ok) { + const config = await response.json() as MicropubConfig; + + return config["channels"] + } else { + throw new Error(`Micropub endpoint returned ${response.status}: ${await response.json()}`) + } + +} + +export async function submit(endpoint: string, token: string, mf2: MF2) { + try { + const response = await fetch(endpoint, { + method: "POST", + headers: { + "Authorization": `Bearer ${token}`, + "Content-Type": "application/json" + }, + body: JSON.stringify(mf2) + }) + + if (response.status != 201 && response.status != 202) { + let err = await response.json(); + console.error("Micropub error!", err); + + return err; + } else { + return { + "location": response.headers.get("Location") + } + } + } catch (e) { + console.error("Network error!", e) + throw e + } +} |