UNPKG

convex

Version:

Client for the Convex Cloud

122 lines (121 loc) 3.63 kB
"use strict"; import { convexToJson, jsonToConvex } from "../values/index.js"; import { STATUS_CODE_UDF_FAILED } from "../common/index.js"; import { version } from "../index.js"; import { createError, logToConsole } from "./logging.js"; const fetch = globalThis.fetch || ((...args) => import("node-fetch").then( ({ default: fetch2 }) => fetch2(...args) )); export class ConvexHttpClient { constructor(address) { this.address = `${address}/api`; this.debug = true; } backendUrl() { return this.address; } setAuth(value) { this.auth = value; } clearAuth() { this.auth = void 0; } setDebug(debug) { this.debug = debug; } query(name) { return async (...args) => { const argsJSON = JSON.stringify(convexToJson(args)); const argsComponent = encodeURIComponent(argsJSON); const url = `${this.address}/${version}/udf?path=${name}&args=${argsComponent}`; const headers = this.auth ? { Authorization: `Bearer ${this.auth}` } : {}; const response = await fetch(url, { credentials: "include", headers }); if (!response.ok && response.status !== STATUS_CODE_UDF_FAILED) { throw new Error(await response.text()); } const respJSON = await response.json(); const value = jsonToConvex(respJSON.value); for (const line of respJSON.logs) { logToConsole("info", "query", name, line); } if (!respJSON.success) { throw createError("query", name, value); } return value; }; } mutation(name) { return async (...args) => { const body = JSON.stringify({ path: name, args: convexToJson(args), tokens: [] }); const headers = { "Content-Type": "application/json" }; if (this.auth) { headers["Authorization"] = `Bearer ${this.auth}`; } const response = await fetch(`${this.address}/${version}/udf`, { body, method: "POST", headers, credentials: "include" }); if (!response.ok && response.status !== STATUS_CODE_UDF_FAILED) { throw new Error(await response.text()); } const respJSON = await response.json(); const value = jsonToConvex(respJSON.value); for (const line of respJSON.logs) { logToConsole("info", "mutation", name, line); } if (!respJSON.success) { throw createError("mutation", name, value); } return value; }; } action(name) { return async (...args) => { const body = JSON.stringify({ path: name, args: convexToJson(args), debug: this.debug }); const headers = { "Content-Type": "application/json" }; if (this.auth) { headers["Authorization"] = `Bearer ${this.auth}`; } const response = await fetch(`${this.address}/action`, { body, method: "POST", headers, credentials: "include" }); if (!response.ok && response.status !== STATUS_CODE_UDF_FAILED) { throw new Error(await response.text()); } const respJSON = await response.json(); for (const line of respJSON.logLines ?? []) { logToConsole("info", "action", name, line); } switch (respJSON.status) { case "success": jsonToConvex(respJSON.value); return respJSON.value; case "error": throw new Error(respJSON.errorMessage); default: throw new Error(`Invalid response: ${JSON.stringify(respJSON)}`); } }; } } //# sourceMappingURL=http_client.js.map