reduct-js
Version:
ReductStore Client SDK for Javascript/NodeJS/Typescript
131 lines (130 loc) • 4.7 kB
JavaScript
import JSONbig from "json-bigint";
import { APIError } from "../APIError";
import { isBrowser } from "../utils/env";
import { Buffer } from "buffer";
import { PACKAGE_VERSION } from "../version";
const bigJson = JSONbig({ alwaysParseAsBig: false, useNativeBigInt: true });
let undiciAgent = null;
if (!isBrowser) {
import("undici").then((undici) => {
const { Agent } = undici;
undiciAgent = new Agent({
connect: {
rejectUnauthorized: false,
},
});
});
}
export class HttpClient {
constructor(url, options = {}) {
this.baseURL = `${url}/api/v1`;
this.timeout = options.timeout;
this.headers = { Authorization: `Bearer ${options.apiToken}` };
if (!isBrowser && options.verifySSL === false) {
this.dispatcher = undiciAgent;
}
}
// ---------- request implementation ----------
async request(method, url, body, headers) {
const controller = new AbortController();
const { signal } = controller;
const { timeout } = this;
let abortedByTimeout = false;
if (timeout) {
setTimeout(() => {
abortedByTimeout = true;
controller.abort();
}, timeout);
}
const init = {
method,
headers: { ...this.headers, ...headers },
body: this.encodeBody(body),
signal: signal,
// @ts-ignore Node.js only
dispatcher: this.dispatcher,
duplex: body instanceof ReadableStream ? "half" : undefined,
};
const response = await fetch(`${this.baseURL}${url}`, init).catch((err) => {
if (abortedByTimeout)
throw new APIError(`timeout of ${this.timeout}ms exceeded`, undefined, err);
if (signal.aborted)
throw new APIError("Request aborted", undefined, err);
throw new APIError(err.message, undefined, err);
});
const apiVersionHeader = response.headers.get("x-reduct-api");
if (!apiVersionHeader)
throw new APIError("Server did not provide API version", undefined, {
response,
});
checkServeApiVersion(apiVersionHeader);
if (!response.ok) {
const message = response.headers.get("x-reduct-error") || response.statusText;
throw new APIError(message, response.status, { response });
}
const data = (await this.parseResponse(response));
return {
data,
headers: response.headers,
status: response.status,
};
}
encodeBody(data) {
if (data === undefined ||
typeof data === "string" ||
Buffer.isBuffer(data) ||
data instanceof Uint8Array ||
data instanceof ArrayBuffer ||
data instanceof Blob ||
data instanceof ReadableStream) {
return data;
}
return bigJson.stringify(data);
}
async parseResponse(res) {
if (res.status === 204)
return {};
const ct = res.headers.get("content-type") ?? "";
if (!res.body)
return {};
if (ct.startsWith("application/json")) {
const text = await res.text();
return text ? bigJson.parse(text) : {};
}
if (ct.startsWith("text/")) {
return res.text();
}
return res.body;
}
// ---------- helpers ----------
get(url) {
return this.request("GET", url);
}
post(url, data, headers) {
return this.request("POST", url, data, headers);
}
put(url, data, headers) {
return this.request("PUT", url, data, headers);
}
patch(url, data, headers) {
return this.request("PATCH", url, data, headers);
}
delete(url, headers) {
return this.request("DELETE", url, undefined, headers);
}
head(url) {
return this.request("HEAD", url);
}
}
const checkServeApiVersion = (serverApiVersion) => {
const [server_major, server_minor] = serverApiVersion
.split(".")
.map((v) => parseInt(v));
const [client_major, client_minor] = PACKAGE_VERSION.split(".").map((v) => parseInt(v));
if (server_major !== client_major) {
throw new APIError(`Incompatible server API version: ${serverApiVersion}. Client version: ${PACKAGE_VERSION}. Please update your client.`);
}
if (server_minor + 2 < client_minor) {
console.error(`Server API version ${serverApiVersion} is too old for this client version ${PACKAGE_VERSION}. Please update your server.`);
}
};