convex
Version:
Client for the Convex Cloud
108 lines (107 loc) • 3.88 kB
JavaScript
;
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_common = require("@convex-dev/common");
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/${import_common.version}`;
}
backendUrl() {
return this.address;
}
setAuth(value) {
this.auth = value;
}
clearAuth() {
this.auth = void 0;
}
query(name) {
return async (...args) => {
const argsJSON = JSON.stringify((0, import_common.convexToJson)(args));
const argsComponent = encodeURIComponent(argsJSON);
const url = `${this.address}/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_common.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_common.convexToJson)(args),
tokens: []
});
const headers = {
"Content-Type": "application/json"
};
if (this.auth) {
headers["Authorization"] = `Bearer ${this.auth}`;
}
const response = await fetch(`${this.address}/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_common.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;
};
}
}
//# sourceMappingURL=http_client.js.map