UNPKG

arcstrum-sdk

Version:

Arcstrum SDK for authentication and database management

126 lines (125 loc) 4.75 kB
const API_BASE = "https://api.arcstrum.com"; export class ArcstrumAuthClient { constructor({ userId, token, apiBase }) { this.userId = userId; this.token = token; this.apiBase = apiBase || API_BASE; } headers() { const h = { "Content-Type": "application/json" }; if (this.token) h.Authorization = `Bearer ${this.token}`; return h; } async post(path, body) { const res = await fetch(`${this.apiBase}${path}`, { method: "POST", headers: this.headers(), body: JSON.stringify({ user_id: this.userId, ...body }), }); if (!res.ok) throw new Error(await res.text()); return res.json(); } async get(path, params) { let url = `${this.apiBase}${path}`; const search = new URLSearchParams({ user_id: this.userId, ...params }).toString(); url += `?${search}`; const res = await fetch(url, { headers: this.headers() }); if (!res.ok) throw new Error(await res.text()); return res.json(); } async listAuths() { const data = await this.get("/auth/list_auths"); if (!Array.isArray(data.auths)) throw new Error("listAuths did not return an array"); return data.auths; } async createAuth(name) { await this.post("/auth/create_auth", { name }); } async deleteAuth(auth_id) { await this.post("/auth/delete_auth", { auth_id }); } async setAuthMetadata(auth_id, metadata) { await this.post("/auth/set_metadata", { auth_id, metadata }); } async getAuthMetadata(auth_id) { return this.get("/auth/get_metadata", { auth_id }); } async setAuthConfig(auth_id, config) { await this.post("/auth/set_config", { auth_id, config }); } async getAuthConfig(auth_id) { return this.get("/auth/get_config", { auth_id }); } async listAuthUsers(authId) { const res = await fetch(`${this.apiBase}/auth/${authId}/users`, { method: "GET", headers: this.headers(), }); if (!res.ok) throw new Error(await res.text()); const data = await res.json(); return data.users; } async deleteAuthUser(authId, userId) { const res = await fetch(`${this.apiBase}/auth/${authId}/users/${userId}`, { method: "DELETE", headers: this.headers(), }); if (!res.ok) throw new Error(await res.text()); } async getAuthUserMetadata(authId, userId) { const res = await fetch(`${this.apiBase}/auth/${encodeURIComponent(authId)}/users/${encodeURIComponent(userId)}/metadata`, { method: "GET", headers: this.headers() }); if (!res.ok) throw new Error(await res.text()); return res.json(); } async deleteAuthUserMetadata(authId, userId) { const res = await fetch(`${this.apiBase}/auth/${encodeURIComponent(authId)}/users/${encodeURIComponent(userId)}/metadata`, { method: "DELETE", headers: this.headers() }); if (!res.ok) throw new Error(await res.text()); } async setAuthUserMetadata(authId, userId, metadata) { const res = await fetch(`${this.apiBase}/auth/${encodeURIComponent(authId)}/users/${encodeURIComponent(userId)}/metadata`, { method: "PUT", headers: this.headers(), body: JSON.stringify(metadata), }); if (!res.ok) throw new Error(await res.text()); } async getAuthUserRoles(authId, userId) { const res = await fetch(`${this.apiBase}/auth/${authId}/users/${userId}/roles`, { method: "GET", headers: this.headers(), }); if (!res.ok) throw new Error(await res.text()); const data = await res.json(); return data.roles; } async setAuthUserRoles(authId, userId, roles) { const res = await fetch(`${this.apiBase}/auth/${authId}/users/${userId}/roles`, { method: "PUT", headers: this.headers(), body: JSON.stringify({ roles }), }); if (!res.ok) throw new Error(await res.text()); } } export async function exchangeAuthCode(code, authId, apiBase = API_BASE, redirect_uri) { console.log("[DEBUG] exchangeAuthCode payload:", { code, redirect_uri }); const res = await fetch(`${apiBase}/auth/${authId}/exchange`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ code, redirect_uri }), // ✅ INCLUDE IT }); if (!res.ok) throw new Error(await res.text()); return res.json(); }