string-sdk
Version:
JavaScript/TypeScript client for the String SmartLink platform (Context OS)
110 lines (109 loc) • 4.84 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.sdk = exports.StringSDK = void 0;
const cross_fetch_1 = __importDefault(require("cross-fetch"));
const signals_1 = require("./signals");
const promptSurfaces_1 = require("./promptSurfaces");
const aeo_1 = require("./aeo");
const agentSeo_1 = require("./agentSeo");
const campaignIq_1 = require("./campaignIq");
const DEFAULT_BASE = (_a = process.env.NEXT_PUBLIC_SUPABASE_URL) !== null && _a !== void 0 ? _a : '';
class StringSDK {
constructor(opts = {}) {
var _a;
if (!opts.baseUrl && !DEFAULT_BASE) {
throw new Error('baseUrl missing – pass it in constructor or set NEXT_PUBLIC_SUPABASE_URL');
}
this.base = (_a = opts.baseUrl) !== null && _a !== void 0 ? _a : DEFAULT_BASE;
this.anonKey = opts.anonKey;
}
/* ---------------------------------------------------------- */
/* Domain sub-clients */
/* ---------------------------------------------------------- */
get signals() {
return new signals_1.SignalsClient({ baseUrl: this.base, anonKey: this.anonKey });
}
get promptSurfaces() {
return new promptSurfaces_1.PromptSurfacesClient({ baseUrl: this.base, anonKey: this.anonKey });
}
get aeo() {
return new aeo_1.AeoClient({ baseUrl: this.base, anonKey: this.anonKey });
}
get seo() {
return new agentSeo_1.AgentSeoClient({ baseUrl: this.base, anonKey: this.anonKey });
}
get campaign() {
return new campaignIq_1.CampaignIqClient({ baseUrl: this.base, anonKey: this.anonKey });
}
/* ---------------------------------------------------------- */
/* SmartLink helpers */
/* ---------------------------------------------------------- */
async createSmartLink(payload) {
const res = await (0, cross_fetch_1.default)(`${this.base}/api/generate-smartlink`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', ...(this.anonKey ? { apikey: this.anonKey } : {}) },
body: JSON.stringify(payload),
});
if (!res.ok)
throw new Error(`createSmartLink failed: ${await res.text()}`);
return res.json();
}
async labelSmartLink(id, stage, source = 'sdk') {
const body = {
object_type: 'smartlink',
object_id: id,
labels: { funnel_stage: stage },
source,
};
const res = await (0, cross_fetch_1.default)(`${this.base}/functions/v1/apply_labels`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', ...(this.anonKey ? { apikey: this.anonKey } : {}) },
body: JSON.stringify(body),
});
if (!res.ok)
throw new Error(`labelSmartLink failed: ${await res.text()}`);
}
async getSmartLink(id) {
const res = await (0, cross_fetch_1.default)(`${this.base}/rest/v1/smartlinks?id=eq.${id}&select=*`, {
headers: { apikey: this.anonKey || '' },
});
if (!res.ok)
throw new Error(`getSmartLink failed: ${await res.text()}`);
const json = await res.json();
return json[0];
}
async searchSmartCards(query, limit = 10) {
const res = await (0, cross_fetch_1.default)(`${this.base}/rest/v1/smartcards?summary=ilike.*${encodeURIComponent(query)}*&limit=${limit}`, {
headers: { apikey: this.anonKey || '' },
});
if (!res.ok)
throw new Error(`searchSmartCards failed: ${await res.text()}`);
return res.json();
}
async getSmartCard(id) {
const res = await (0, cross_fetch_1.default)(`${this.base}/rest/v1/smartcards?id=eq.${id}&select=*`, {
headers: { apikey: this.anonKey || '' },
});
if (!res.ok)
throw new Error(`getSmartCard failed: ${await res.text()}`);
const json = await res.json();
return json[0];
}
async generateSmartCard(targetId, context, metadata = {}) {
const res = await (0, cross_fetch_1.default)(`${this.base}/api/smartcards/generate`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', ...(this.anonKey ? { apikey: this.anonKey } : {}) },
body: JSON.stringify({ target_id: targetId, context, metadata }),
});
if (!res.ok)
throw new Error(`generateSmartCard failed: ${await res.text()}`);
return res.json();
}
}
exports.StringSDK = StringSDK;
// Convenience default export using env base URL
exports.sdk = new StringSDK();