spaps-sdk
Version:
Sweet Potato Authentication & Payment Service SDK - Zero-config client for SPAPS
192 lines (190 loc) • 6.3 kB
JavaScript
"use strict";
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(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
SPAPS: () => SPAPSClient,
SPAPSClient: () => SPAPSClient,
SweetPotatoSDK: () => SPAPSClient,
default: () => index_default
});
module.exports = __toCommonJS(index_exports);
var import_axios = __toESM(require("axios"));
var SPAPSClient = class {
client;
apiKey;
accessToken;
refreshToken;
_isLocalMode = false;
constructor(config = {}) {
const apiUrl = config.apiUrl || process.env.SPAPS_API_URL || process.env.NEXT_PUBLIC_SPAPS_API_URL;
if (!apiUrl || apiUrl.includes("localhost") || apiUrl.includes("127.0.0.1")) {
this._isLocalMode = true;
this.client = import_axios.default.create({
baseURL: apiUrl || "http://localhost:3300",
timeout: config.timeout || 1e4,
headers: {
"Content-Type": "application/json"
}
});
} else {
if (!config.apiKey && !process.env.SPAPS_API_KEY) {
console.warn("\u26A0\uFE0F SPAPS: No API key provided. Some features may not work.");
}
this.apiKey = config.apiKey || process.env.SPAPS_API_KEY;
this.client = import_axios.default.create({
baseURL: apiUrl,
timeout: config.timeout || 1e4,
headers: {
"Content-Type": "application/json",
...this.apiKey && { "X-API-Key": this.apiKey }
}
});
}
this.client.interceptors.request.use((config2) => {
if (this.accessToken && !config2.headers.Authorization) {
config2.headers.Authorization = `Bearer ${this.accessToken}`;
}
return config2;
});
this.client.interceptors.response.use(
(response) => response,
async (error) => {
if (error.response?.status === 401 && this.refreshToken) {
try {
const { data } = await this.refresh(this.refreshToken);
this.accessToken = data.access_token;
this.refreshToken = data.refresh_token;
if (error.config) {
error.config.headers.Authorization = `Bearer ${this.accessToken}`;
return this.client.request(error.config);
}
} catch (refreshError) {
this.accessToken = void 0;
this.refreshToken = void 0;
}
}
return Promise.reject(error);
}
);
}
// Authentication Methods
async login(email, password) {
const response = await this.client.post("/api/auth/login", {
email,
password
});
this.accessToken = response.data.access_token;
this.refreshToken = response.data.refresh_token;
return response;
}
async register(email, password) {
const response = await this.client.post("/api/auth/register", {
email,
password
});
this.accessToken = response.data.access_token;
this.refreshToken = response.data.refresh_token;
return response;
}
async walletSignIn(walletAddress, signature, message, chainType = "solana") {
const response = await this.client.post("/api/auth/wallet-sign-in", {
wallet_address: walletAddress,
signature,
message,
chain_type: chainType
});
this.accessToken = response.data.access_token;
this.refreshToken = response.data.refresh_token;
return response;
}
async refresh(refreshToken) {
const response = await this.client.post("/api/auth/refresh", {
refresh_token: refreshToken || this.refreshToken
});
this.accessToken = response.data.access_token;
this.refreshToken = response.data.refresh_token;
return response;
}
async logout() {
await this.client.post("/api/auth/logout");
this.accessToken = void 0;
this.refreshToken = void 0;
}
async getUser() {
return this.client.get("/api/auth/user");
}
// Stripe Methods
async createCheckoutSession(priceId, successUrl, cancelUrl) {
return this.client.post("/api/stripe/create-checkout-session", {
price_id: priceId,
success_url: successUrl,
cancel_url: cancelUrl
});
}
async getSubscription() {
return this.client.get("/api/stripe/subscription");
}
async cancelSubscription() {
await this.client.delete("/api/stripe/subscription");
}
// Usage Methods
async getUsageBalance() {
return this.client.get("/api/usage/balance");
}
async recordUsage(feature, amount) {
await this.client.post("/api/usage/record", {
feature,
amount
});
}
// Utility Methods
isAuthenticated() {
return !!this.accessToken;
}
getAccessToken() {
return this.accessToken;
}
setAccessToken(token) {
this.accessToken = token;
}
isLocalMode() {
return this._isLocalMode;
}
async health() {
return this.client.get("/health");
}
};
var index_default = SPAPSClient;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
SPAPS,
SPAPSClient,
SweetPotatoSDK
});