UNPKG

@pompeii-labs/aqueducts

Version:

The Aqueducts integration SDK from Pompeii Labs

262 lines (257 loc) 7.6 kB
import dotenv from 'dotenv'; var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); var __async = (__this, __arguments, generator) => { return new Promise((resolve, reject) => { var fulfilled = (value) => { try { step(generator.next(value)); } catch (e) { reject(e); } }; var rejected = (value) => { try { step(generator.throw(value)); } catch (e) { reject(e); } }; var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); step((generator = generator.apply(__this, __arguments)).next()); }); }; // src/types/connection.ts var Connection = class { constructor(data, client) { Object.assign(this, data); this.client = client; } updateMetadata(metadata) { return __async(this, null, function* () { if (!this.client) return this; const response = yield this.client.request(`/connections/${this.id}`, { method: "PUT", body: JSON.stringify({ metadata }) }); if (!response.ok) { throw yield response.json(); } this.metadata = metadata; return this; }); } delete() { return __async(this, null, function* () { if (!this.client) return; const response = yield this.client.request(`/connections/${this.id}`, { method: "DELETE" }); if (!response.ok) { throw yield response.json(); } }); } toJSON() { return { id: this.id, connectionId: this.connectionId, userId: this.userId, createdAt: this.createdAt, integrationId: this.integrationId, projectId: this.projectId, provider: this.provider, status: this.status, credentials: this.credentials, expiresAt: this.expiresAt, metadata: this.metadata }; } }; // src/client.ts dotenv.config(); var endpoint = "https://api.aqueducts.dev/v1"; var Aqueduct = class { constructor(apiKey = process.env.AQUEDUCT_API_KEY) { if (!apiKey) { throw new Error("AQUEDUCT_API_KEY is not set"); } this.apiKey = apiKey; } /** * Make a request to the Aqueduct API * * @param route The route to make the request to * @param args The arguments to pass to the request * @returns The response from the request */ request(_0) { return __async(this, arguments, function* (route, args = {}) { const requestArgs = __spreadProps(__spreadValues({}, args), { headers: __spreadValues({ "x-api-key": this.apiKey, "Content-Type": "application/json" }, (args == null ? void 0 : args.headers) || {}) }); return yield fetch(`${endpoint}${route}`, requestArgs); }); } /** * Test the connection to the Aqueduct API * * @return nothing if successful * @throws Error if the request fails */ test() { return __async(this, null, function* () { const response = yield this.request("/projects"); if (!response.ok) { throw new Error("Failed to test connection"); } }); } /** * List all integrations set up for the project * * @returns The integrations */ listIntegrations() { return __async(this, null, function* () { try { const response = yield this.request("/integrations"); if (!response.ok) { throw yield response.json(); } const data = yield response.json(); return data.map((i) => { delete i.credentials; return i; }); } catch (error) { console.log(`Failed to list integrations: ${error.message || error}`); return []; } }); } getConnection(_0) { return __async(this, arguments, function* ({ provider, connectionId, userId, force_refresh, metadata }) { try { const searchParams = new URLSearchParams(); searchParams.set("provider", provider); if (connectionId) { searchParams.set("connection_id", connectionId); } if (userId) { searchParams.set("user_id", userId); } if (force_refresh) { searchParams.set("force_refresh", String(force_refresh)); } if (metadata) { searchParams.set("metadata", JSON.stringify(metadata)); } const response = yield this.request(`/connections?${searchParams.toString()}`); const data = yield response.json(); if (!response.ok) { throw data; } return new Connection(data, this); } catch (error) { console.log(`Failed to get connection for (${provider}): ${error.message || error}`); return null; } }); } listConnections(_0) { return __async(this, arguments, function* ({ provider, connectionId, userId, metadata, forceRefresh = false }) { try { const searchParams = new URLSearchParams(); if (provider) { searchParams.set("provider", provider); } if (connectionId) { searchParams.set("connection_id", connectionId); } if (userId) { searchParams.set("user_id", userId); } if (metadata) { searchParams.set("metadata", JSON.stringify(metadata)); } if (forceRefresh) { searchParams.set("force_refresh", String(forceRefresh)); } const response = yield this.request(`/connections/list?${searchParams.toString()}`); const data = yield response.json(); if (!response.ok) { throw data; } return data.map((c) => new Connection(c, this)); } catch (error) { console.log(`Failed to list connections: ${error.message || error}`); return null; } }); } /** * Get the auth URL for a connection * * @param options The options for the auth URL * @returns The auth URL */ authUrl(_0) { return __async(this, arguments, function* ({ provider, userId, metadata, redirectUri }) { try { const urlParams = new URLSearchParams(); urlParams.set("provider", provider); urlParams.set("user_id", userId); if (redirectUri) { urlParams.set("redirect_uri", redirectUri); } if (metadata) { urlParams.set("metadata", JSON.stringify(metadata)); } const response = yield this.request(`/oauth/url?${urlParams.toString()}`); const data = yield response.json(); if (!response.ok) { throw data; } return data.url; } catch (error) { console.log(`Failed to get auth URL: ${error.message || error}`); } }); } }; export { Aqueduct, Connection };