File size: 983 Bytes
0ad74ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { BROKEN_CONNECTION_MSG } from "../constants";
import type { PostResponse } from "../types";
import { Client } from "..";

export async function post_data(
	this: Client,
	url: string,
	body: unknown,
	additional_headers?: any
): Promise<[PostResponse, number]> {
	const headers: {
		Authorization?: string;
		"Content-Type": "application/json";
	} = { "Content-Type": "application/json" };
	if (this.options.hf_token) {
		headers.Authorization = `Bearer ${this.options.hf_token}`;
	}
	try {
		var response = await this.fetch(url, {
			method: "POST",
			body: JSON.stringify(body),
			headers: { ...headers, ...additional_headers },
			credentials: "include"
		});
	} catch (e) {
		return [{ error: BROKEN_CONNECTION_MSG }, 500];
	}
	let output: PostResponse;
	let status: number;
	try {
		output = await response.json();
		status = response.status;
	} catch (e) {
		output = { error: `Could not parse server response: ${e}` };
		status = 500;
	}
	return [output, status];
}