UNPKG

convex

Version:

Client for the Convex Cloud

151 lines (150 loc) 5.23 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var http_client_exports = {}; __export(http_client_exports, { ConvexHttpClient: () => ConvexHttpClient }); module.exports = __toCommonJS(http_client_exports); var import_values = require("../values/index.js"); var import_common = require("../common/index.js"); var import__ = require("../index.js"); var import_logging = require("./logging.js"); const hasFetch = typeof window !== "undefined" && typeof window.fetch !== "undefined"; const fetch = hasFetch ? window.fetch : (...args) => import("node-fetch").then( ({ default: fetch2 }) => fetch2(...args) ); class ConvexHttpClient { constructor(clientConfig) { this.address = `${clientConfig.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((0, import_values.convexToJson)(args)); const argsComponent = encodeURIComponent(argsJSON); const url = `${this.address}/${import__.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 != import_common.STATUS_CODE_UDF_FAILED) { throw new Error(await response.text()); } const respJSON = await response.json(); const value = (0, import_values.jsonToConvex)(respJSON.value); for (const line of respJSON.logs) { (0, import_logging.logToConsole)("info", "query", name, line); } if (!respJSON.success) { throw (0, import_logging.createError)("query", name, value); } return value; }; } mutation(name) { return async (...args) => { const body = JSON.stringify({ path: name, args: (0, import_values.convexToJson)(args), tokens: [] }); const headers = { "Content-Type": "application/json" }; if (this.auth) { headers["Authorization"] = `Bearer ${this.auth}`; } const response = await fetch(`${this.address}/${import__.version}/udf`, { body, method: "POST", headers, credentials: "include" }); if (!response.ok && response.status != import_common.STATUS_CODE_UDF_FAILED) { throw new Error(await response.text()); } const respJSON = await response.json(); const value = (0, import_values.jsonToConvex)(respJSON.value); for (const line of respJSON.logs) { (0, import_logging.logToConsole)("info", "mutation", name, line); } if (!respJSON.success) { throw (0, import_logging.createError)("mutation", name, value); } return value; }; } action(name) { return async (...args) => { const body = JSON.stringify({ path: name, args: (0, import_values.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 != import_common.STATUS_CODE_UDF_FAILED) { throw new Error(await response.text()); } const respJSON = await response.json(); for (const line of respJSON.logLines ?? []) { (0, import_logging.logToConsole)("info", "action", name, line); } switch (respJSON.status) { case "success": (0, import_values.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