UNPKG

@hirall/client

Version:

Official TypeScript/JavaScript client for Hirall - Backend-as-a-Service

384 lines (382 loc) 10.6 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { HirallClient: () => HirallClient, createClient: () => createClient, default: () => index_default }); module.exports = __toCommonJS(index_exports); var HirallClient = class { constructor(apiUrl, apiKey) { this.apiUrl = apiUrl; this.apiKey = apiKey; this.headers = { "Content-Type": "application/json", "Authorization": `Bearer ${apiKey}` }; } /** * Database operations */ get db() { return new DatabaseClient(this.apiUrl, this.headers); } /** * Authentication */ get auth() { return new AuthClient(this.apiUrl, this.headers); } /** * Storage operations */ get storage() { return new StorageClient(this.apiUrl, this.headers); } /** * Real-time subscriptions */ get realtime() { return new RealtimeClient(this.apiUrl.replace("http", "ws"), this.apiKey); } /** * Edge functions */ get functions() { return new FunctionsClient(this.apiUrl, this.headers); } /** * AI features */ get ai() { return new AIClient(this.apiUrl, this.headers); } }; var DatabaseClient = class { constructor(apiUrl, headers) { this.apiUrl = apiUrl; this.headers = headers; } from(table) { return new QueryBuilder(this.apiUrl, this.headers, table); } async query(sql, params) { const response = await fetch(`${this.apiUrl}/v1/sql/execute`, { method: "POST", headers: this.headers, body: JSON.stringify({ query: sql, params }) }); const data = await response.json(); return data.data.rows; } }; var QueryBuilder = class { constructor(apiUrl, headers, table) { this.apiUrl = apiUrl; this.headers = headers; this.table = table; this.selectColumns = "*"; this.whereConditions = []; } select(columns = "*") { this.selectColumns = columns; return this; } eq(column, value) { this.whereConditions.push({ column, operator: "=", value }); return this; } neq(column, value) { this.whereConditions.push({ column, operator: "!=", value }); return this; } gt(column, value) { this.whereConditions.push({ column, operator: ">", value }); return this; } lt(column, value) { this.whereConditions.push({ column, operator: "<", value }); return this; } order(column, options) { const direction = options?.ascending === false ? "DESC" : "ASC"; this.orderByClause = `${column} ${direction}`; return this; } limit(count) { this.limitValue = count; return this; } offset(count) { this.offsetValue = count; return this; } async execute() { let sql = `SELECT ${this.selectColumns} FROM ${this.table}`; if (this.whereConditions.length > 0) { const where = this.whereConditions.map((c, i) => `${c.column} ${c.operator} $${i + 1}`).join(" AND "); sql += ` WHERE ${where}`; } if (this.orderByClause) { sql += ` ORDER BY ${this.orderByClause}`; } if (this.limitValue) { sql += ` LIMIT ${this.limitValue}`; } if (this.offsetValue) { sql += ` OFFSET ${this.offsetValue}`; } const params = this.whereConditions.map((c) => c.value); const response = await fetch(`${this.apiUrl}/v1/sql/execute`, { method: "POST", headers: this.headers, body: JSON.stringify({ query: sql, params }) }); const data = await response.json(); return data.data.rows; } async single() { this.limitValue = 1; const results = await this.execute(); return results[0] || null; } async insert(data) { const columns = Object.keys(data); const values = Object.values(data); const placeholders = values.map((_, i) => `$${i + 1}`).join(", "); const sql = `INSERT INTO ${this.table} (${columns.join(", ")}) VALUES (${placeholders}) RETURNING *`; const response = await fetch(`${this.apiUrl}/v1/sql/execute`, { method: "POST", headers: this.headers, body: JSON.stringify({ query: sql, params: values }) }); const result = await response.json(); return result.data.rows[0]; } async update(data) { const updates = Object.entries(data).map(([key], i) => `${key} = $${i + 1}`).join(", "); let sql = `UPDATE ${this.table} SET ${updates}`; if (this.whereConditions.length > 0) { const where = this.whereConditions.map((c, i) => `${c.column} ${c.operator} $${Object.keys(data).length + i + 1}`).join(" AND "); sql += ` WHERE ${where}`; } sql += " RETURNING *"; const params = [...Object.values(data), ...this.whereConditions.map((c) => c.value)]; const response = await fetch(`${this.apiUrl}/v1/sql/execute`, { method: "POST", headers: this.headers, body: JSON.stringify({ query: sql, params }) }); const result = await response.json(); return result.data.rows; } async delete() { let sql = `DELETE FROM ${this.table}`; if (this.whereConditions.length > 0) { const where = this.whereConditions.map((c, i) => `${c.column} ${c.operator} $${i + 1}`).join(" AND "); sql += ` WHERE ${where}`; } const params = this.whereConditions.map((c) => c.value); await fetch(`${this.apiUrl}/v1/sql/execute`, { method: "POST", headers: this.headers, body: JSON.stringify({ query: sql, params }) }); } }; var AuthClient = class { constructor(apiUrl, headers) { this.apiUrl = apiUrl; this.headers = headers; } async signUp(email, password) { const response = await fetch(`${this.apiUrl}/v1/auth/signup`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email, password }) }); return response.json(); } async signIn(email, password) { const response = await fetch(`${this.apiUrl}/v1/auth/signin`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email, password }) }); return response.json(); } async signOut() { const response = await fetch(`${this.apiUrl}/v1/auth/signout`, { method: "POST", headers: this.headers }); return response.json(); } }; var StorageClient = class { constructor(apiUrl, headers) { this.apiUrl = apiUrl; this.headers = headers; } bucket(name) { return new BucketClient(this.apiUrl, this.headers, name); } }; var BucketClient = class { constructor(apiUrl, headers, bucketName) { this.apiUrl = apiUrl; this.headers = headers; this.bucketName = bucketName; } async upload(path, file) { const formData = new FormData(); formData.append("file", file); const response = await fetch( `${this.apiUrl}/v1/storage/${this.bucketName}/${path}`, { method: "POST", headers: { "Authorization": this.headers["Authorization"] }, body: formData } ); return response.json(); } async download(path) { const response = await fetch( `${this.apiUrl}/v1/storage/${this.bucketName}/${path}`, { headers: this.headers } ); return response.blob(); } async delete(path) { const response = await fetch( `${this.apiUrl}/v1/storage/${this.bucketName}/${path}`, { method: "DELETE", headers: this.headers } ); return response.json(); } }; var RealtimeClient = class { constructor(wsUrl, apiKey) { this.wsUrl = wsUrl; this.apiKey = apiKey; } channel(name) { return new Channel(this, name); } connect() { this.ws = new WebSocket(`${this.wsUrl}?apiKey=${this.apiKey}`); return this.ws; } send(message) { this.ws?.send(JSON.stringify(message)); } }; var Channel = class { constructor(client, name) { this.client = client; this.name = name; this.listeners = /* @__PURE__ */ new Map(); } on(event, callback) { if (!this.listeners.has(event)) { this.listeners.set(event, []); } this.listeners.get(event).push(callback); return this; } subscribe() { this.client.send({ type: "subscribe", channel: this.name }); return this; } send(event, payload) { this.client.send({ type: "broadcast", channel: this.name, event, payload }); } }; var FunctionsClient = class { constructor(apiUrl, headers) { this.apiUrl = apiUrl; this.headers = headers; } async invoke(name, payload) { const response = await fetch( `${this.apiUrl}/v1/functions/${name}/invoke`, { method: "POST", headers: this.headers, body: JSON.stringify(payload) } ); return response.json(); } }; var AIClient = class { constructor(apiUrl, headers) { this.apiUrl = apiUrl; this.headers = headers; } async generateEmbedding(text) { const response = await fetch(`${this.apiUrl}/v1/ai/embeddings`, { method: "POST", headers: this.headers, body: JSON.stringify({ text }) }); return response.json(); } async semanticSearch(query, limit) { const response = await fetch(`${this.apiUrl}/v1/ai/search`, { method: "POST", headers: this.headers, body: JSON.stringify({ query, limit }) }); return response.json(); } async chat(messages) { const response = await fetch(`${this.apiUrl}/v1/ai/chat`, { method: "POST", headers: this.headers, body: JSON.stringify({ messages }) }); return response.json(); } }; function createClient(apiUrl, apiKey) { return new HirallClient(apiUrl, apiKey); } var index_default = { createClient, HirallClient }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { HirallClient, createClient });