spaps-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +164 -0
- package/dist/index.d.mts +82 -0
- package/dist/index.d.ts +82 -0
- package/dist/index.js +191 -0
- package/dist/index.mjs +154 -0
- package/package.json +62 -0
package/README.md
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
# @spaps/sdk
|
|
2
|
+
|
|
3
|
+
> Sweet Potato Authentication & Payment Service SDK
|
|
4
|
+
|
|
5
|
+
Zero-config client for SPAPS authentication and payments. Works automatically with local development mode.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @spaps/sdk
|
|
11
|
+
# or
|
|
12
|
+
yarn add @spaps/sdk
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
```javascript
|
|
18
|
+
import { SPAPSClient } from '@spaps/sdk';
|
|
19
|
+
// or
|
|
20
|
+
const { SPAPSClient } = require('@spaps/sdk');
|
|
21
|
+
|
|
22
|
+
// Auto-detects local mode - no API key needed for localhost!
|
|
23
|
+
const spaps = new SPAPSClient({
|
|
24
|
+
apiUrl: 'http://localhost:3300' // Optional, auto-detected
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// Login
|
|
28
|
+
const { data } = await spaps.login('user@example.com', 'password');
|
|
29
|
+
console.log('User:', data.user);
|
|
30
|
+
|
|
31
|
+
// Check authentication
|
|
32
|
+
if (spaps.isAuthenticated()) {
|
|
33
|
+
const user = await spaps.getUser();
|
|
34
|
+
console.log('Current user:', user.data);
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Features
|
|
39
|
+
|
|
40
|
+
### 🚀 Zero Configuration
|
|
41
|
+
- **Auto-detects local mode** - No API key needed for localhost
|
|
42
|
+
- **Auto-refreshes tokens** - Handles expired tokens automatically
|
|
43
|
+
- **TypeScript support** - Full type definitions included
|
|
44
|
+
|
|
45
|
+
### 🔐 Authentication Methods
|
|
46
|
+
```javascript
|
|
47
|
+
// Email/Password
|
|
48
|
+
await spaps.login(email, password);
|
|
49
|
+
await spaps.register(email, password);
|
|
50
|
+
|
|
51
|
+
// Wallet Authentication
|
|
52
|
+
await spaps.walletSignIn(walletAddress, signature, message, 'solana');
|
|
53
|
+
|
|
54
|
+
// Token Management
|
|
55
|
+
await spaps.refresh();
|
|
56
|
+
await spaps.logout();
|
|
57
|
+
|
|
58
|
+
// Get User
|
|
59
|
+
const user = await spaps.getUser();
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### 💳 Stripe Integration
|
|
63
|
+
```javascript
|
|
64
|
+
// Create checkout session
|
|
65
|
+
const session = await spaps.createCheckoutSession(priceId, successUrl);
|
|
66
|
+
window.location.href = session.data.url;
|
|
67
|
+
|
|
68
|
+
// Manage subscription
|
|
69
|
+
const subscription = await spaps.getSubscription();
|
|
70
|
+
await spaps.cancelSubscription();
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### 📊 Usage Tracking
|
|
74
|
+
```javascript
|
|
75
|
+
// Check balance
|
|
76
|
+
const balance = await spaps.getUsageBalance();
|
|
77
|
+
console.log(`Credits: ${balance.data.balance}`);
|
|
78
|
+
|
|
79
|
+
// Record usage
|
|
80
|
+
await spaps.recordUsage('api-call', 1);
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Configuration
|
|
84
|
+
|
|
85
|
+
### Production Mode
|
|
86
|
+
```javascript
|
|
87
|
+
const spaps = new SPAPSClient({
|
|
88
|
+
apiUrl: 'https://api.sweetpotato.com',
|
|
89
|
+
apiKey: 'spaps_your_api_key_here',
|
|
90
|
+
timeout: 10000 // Optional timeout in ms
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Local Development Mode (Auto-detected)
|
|
95
|
+
```javascript
|
|
96
|
+
const spaps = new SPAPSClient();
|
|
97
|
+
// Automatically uses http://localhost:3300 with no API key
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Environment Variables
|
|
101
|
+
```bash
|
|
102
|
+
# .env
|
|
103
|
+
SPAPS_API_URL=https://api.sweetpotato.com
|
|
104
|
+
SPAPS_API_KEY=spaps_your_api_key_here
|
|
105
|
+
|
|
106
|
+
# Next.js
|
|
107
|
+
NEXT_PUBLIC_SPAPS_API_URL=https://api.sweetpotato.com
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Helper Methods
|
|
111
|
+
|
|
112
|
+
```javascript
|
|
113
|
+
// Check if authenticated
|
|
114
|
+
spaps.isAuthenticated() // boolean
|
|
115
|
+
|
|
116
|
+
// Get current access token
|
|
117
|
+
spaps.getAccessToken() // string | undefined
|
|
118
|
+
|
|
119
|
+
// Set access token manually
|
|
120
|
+
spaps.setAccessToken(token)
|
|
121
|
+
|
|
122
|
+
// Check if in local mode
|
|
123
|
+
spaps.isLocalMode() // boolean
|
|
124
|
+
|
|
125
|
+
// Health check
|
|
126
|
+
await spaps.health()
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Import Styles
|
|
130
|
+
|
|
131
|
+
All these work:
|
|
132
|
+
```javascript
|
|
133
|
+
// ES6 Import
|
|
134
|
+
import { SPAPSClient } from '@spaps/sdk';
|
|
135
|
+
import SPAPSClient from '@spaps/sdk';
|
|
136
|
+
|
|
137
|
+
// CommonJS
|
|
138
|
+
const { SPAPSClient } = require('@spaps/sdk');
|
|
139
|
+
const SPAPSClient = require('@spaps/sdk');
|
|
140
|
+
|
|
141
|
+
// Alternative names
|
|
142
|
+
import { SPAPS } from '@spaps/sdk';
|
|
143
|
+
import { SweetPotatoSDK } from '@spaps/sdk';
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## Error Handling
|
|
147
|
+
|
|
148
|
+
```javascript
|
|
149
|
+
try {
|
|
150
|
+
await spaps.login(email, password);
|
|
151
|
+
} catch (error) {
|
|
152
|
+
if (error.response?.status === 401) {
|
|
153
|
+
console.error('Invalid credentials');
|
|
154
|
+
} else if (error.response?.status === 429) {
|
|
155
|
+
console.error('Rate limited');
|
|
156
|
+
} else {
|
|
157
|
+
console.error('Error:', error.message);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## License
|
|
163
|
+
|
|
164
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @spaps/sdk - Sweet Potato Authentication & Payment Service SDK
|
|
3
|
+
* Zero-config client for SPAPS authentication and payments
|
|
4
|
+
*/
|
|
5
|
+
interface SPAPSConfig {
|
|
6
|
+
apiUrl?: string;
|
|
7
|
+
apiKey?: string;
|
|
8
|
+
autoDetect?: boolean;
|
|
9
|
+
timeout?: number;
|
|
10
|
+
}
|
|
11
|
+
interface AuthResponse {
|
|
12
|
+
access_token: string;
|
|
13
|
+
refresh_token: string;
|
|
14
|
+
user: User;
|
|
15
|
+
}
|
|
16
|
+
interface User {
|
|
17
|
+
id: string;
|
|
18
|
+
email?: string;
|
|
19
|
+
wallet_address?: string;
|
|
20
|
+
chain_type?: string;
|
|
21
|
+
role: string;
|
|
22
|
+
created_at?: string;
|
|
23
|
+
}
|
|
24
|
+
interface CheckoutSession {
|
|
25
|
+
sessionId: string;
|
|
26
|
+
url: string;
|
|
27
|
+
}
|
|
28
|
+
interface Subscription {
|
|
29
|
+
id: string;
|
|
30
|
+
status: string;
|
|
31
|
+
plan: string;
|
|
32
|
+
current_period_end: string;
|
|
33
|
+
}
|
|
34
|
+
interface UsageBalance {
|
|
35
|
+
balance: number;
|
|
36
|
+
currency: string;
|
|
37
|
+
updated_at: string;
|
|
38
|
+
}
|
|
39
|
+
declare class SPAPSClient {
|
|
40
|
+
private client;
|
|
41
|
+
private apiKey?;
|
|
42
|
+
private accessToken?;
|
|
43
|
+
private refreshToken?;
|
|
44
|
+
private _isLocalMode;
|
|
45
|
+
constructor(config?: SPAPSConfig);
|
|
46
|
+
login(email: string, password: string): Promise<{
|
|
47
|
+
data: AuthResponse;
|
|
48
|
+
}>;
|
|
49
|
+
register(email: string, password: string): Promise<{
|
|
50
|
+
data: AuthResponse;
|
|
51
|
+
}>;
|
|
52
|
+
walletSignIn(walletAddress: string, signature: string, message: string, chainType?: 'solana' | 'ethereum'): Promise<{
|
|
53
|
+
data: AuthResponse;
|
|
54
|
+
}>;
|
|
55
|
+
refresh(refreshToken?: string): Promise<{
|
|
56
|
+
data: AuthResponse;
|
|
57
|
+
}>;
|
|
58
|
+
logout(): Promise<void>;
|
|
59
|
+
getUser(): Promise<{
|
|
60
|
+
data: User;
|
|
61
|
+
}>;
|
|
62
|
+
createCheckoutSession(priceId: string, successUrl: string, cancelUrl?: string): Promise<{
|
|
63
|
+
data: CheckoutSession;
|
|
64
|
+
}>;
|
|
65
|
+
getSubscription(): Promise<{
|
|
66
|
+
data: Subscription;
|
|
67
|
+
}>;
|
|
68
|
+
cancelSubscription(): Promise<void>;
|
|
69
|
+
getUsageBalance(): Promise<{
|
|
70
|
+
data: UsageBalance;
|
|
71
|
+
}>;
|
|
72
|
+
recordUsage(feature: string, amount: number): Promise<void>;
|
|
73
|
+
isAuthenticated(): boolean;
|
|
74
|
+
getAccessToken(): string | undefined;
|
|
75
|
+
setAccessToken(token: string): void;
|
|
76
|
+
isLocalMode(): boolean;
|
|
77
|
+
health(): Promise<{
|
|
78
|
+
data: any;
|
|
79
|
+
}>;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export { type AuthResponse, type CheckoutSession, SPAPSClient as SPAPS, SPAPSClient, type SPAPSConfig, type Subscription, SPAPSClient as SweetPotatoSDK, type UsageBalance, type User, SPAPSClient as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @spaps/sdk - Sweet Potato Authentication & Payment Service SDK
|
|
3
|
+
* Zero-config client for SPAPS authentication and payments
|
|
4
|
+
*/
|
|
5
|
+
interface SPAPSConfig {
|
|
6
|
+
apiUrl?: string;
|
|
7
|
+
apiKey?: string;
|
|
8
|
+
autoDetect?: boolean;
|
|
9
|
+
timeout?: number;
|
|
10
|
+
}
|
|
11
|
+
interface AuthResponse {
|
|
12
|
+
access_token: string;
|
|
13
|
+
refresh_token: string;
|
|
14
|
+
user: User;
|
|
15
|
+
}
|
|
16
|
+
interface User {
|
|
17
|
+
id: string;
|
|
18
|
+
email?: string;
|
|
19
|
+
wallet_address?: string;
|
|
20
|
+
chain_type?: string;
|
|
21
|
+
role: string;
|
|
22
|
+
created_at?: string;
|
|
23
|
+
}
|
|
24
|
+
interface CheckoutSession {
|
|
25
|
+
sessionId: string;
|
|
26
|
+
url: string;
|
|
27
|
+
}
|
|
28
|
+
interface Subscription {
|
|
29
|
+
id: string;
|
|
30
|
+
status: string;
|
|
31
|
+
plan: string;
|
|
32
|
+
current_period_end: string;
|
|
33
|
+
}
|
|
34
|
+
interface UsageBalance {
|
|
35
|
+
balance: number;
|
|
36
|
+
currency: string;
|
|
37
|
+
updated_at: string;
|
|
38
|
+
}
|
|
39
|
+
declare class SPAPSClient {
|
|
40
|
+
private client;
|
|
41
|
+
private apiKey?;
|
|
42
|
+
private accessToken?;
|
|
43
|
+
private refreshToken?;
|
|
44
|
+
private _isLocalMode;
|
|
45
|
+
constructor(config?: SPAPSConfig);
|
|
46
|
+
login(email: string, password: string): Promise<{
|
|
47
|
+
data: AuthResponse;
|
|
48
|
+
}>;
|
|
49
|
+
register(email: string, password: string): Promise<{
|
|
50
|
+
data: AuthResponse;
|
|
51
|
+
}>;
|
|
52
|
+
walletSignIn(walletAddress: string, signature: string, message: string, chainType?: 'solana' | 'ethereum'): Promise<{
|
|
53
|
+
data: AuthResponse;
|
|
54
|
+
}>;
|
|
55
|
+
refresh(refreshToken?: string): Promise<{
|
|
56
|
+
data: AuthResponse;
|
|
57
|
+
}>;
|
|
58
|
+
logout(): Promise<void>;
|
|
59
|
+
getUser(): Promise<{
|
|
60
|
+
data: User;
|
|
61
|
+
}>;
|
|
62
|
+
createCheckoutSession(priceId: string, successUrl: string, cancelUrl?: string): Promise<{
|
|
63
|
+
data: CheckoutSession;
|
|
64
|
+
}>;
|
|
65
|
+
getSubscription(): Promise<{
|
|
66
|
+
data: Subscription;
|
|
67
|
+
}>;
|
|
68
|
+
cancelSubscription(): Promise<void>;
|
|
69
|
+
getUsageBalance(): Promise<{
|
|
70
|
+
data: UsageBalance;
|
|
71
|
+
}>;
|
|
72
|
+
recordUsage(feature: string, amount: number): Promise<void>;
|
|
73
|
+
isAuthenticated(): boolean;
|
|
74
|
+
getAccessToken(): string | undefined;
|
|
75
|
+
setAccessToken(token: string): void;
|
|
76
|
+
isLocalMode(): boolean;
|
|
77
|
+
health(): Promise<{
|
|
78
|
+
data: any;
|
|
79
|
+
}>;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export { type AuthResponse, type CheckoutSession, SPAPSClient as SPAPS, SPAPSClient, type SPAPSConfig, type Subscription, SPAPSClient as SweetPotatoSDK, type UsageBalance, type User, SPAPSClient as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
SPAPS: () => SPAPSClient,
|
|
34
|
+
SPAPSClient: () => SPAPSClient,
|
|
35
|
+
SweetPotatoSDK: () => SPAPSClient,
|
|
36
|
+
default: () => index_default
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(index_exports);
|
|
39
|
+
var import_axios = __toESM(require("axios"));
|
|
40
|
+
var SPAPSClient = class {
|
|
41
|
+
client;
|
|
42
|
+
apiKey;
|
|
43
|
+
accessToken;
|
|
44
|
+
refreshToken;
|
|
45
|
+
_isLocalMode = false;
|
|
46
|
+
constructor(config = {}) {
|
|
47
|
+
const apiUrl = config.apiUrl || process.env.SPAPS_API_URL || process.env.NEXT_PUBLIC_SPAPS_API_URL;
|
|
48
|
+
if (!apiUrl || apiUrl.includes("localhost") || apiUrl.includes("127.0.0.1")) {
|
|
49
|
+
this._isLocalMode = true;
|
|
50
|
+
this.client = import_axios.default.create({
|
|
51
|
+
baseURL: apiUrl || "http://localhost:3300",
|
|
52
|
+
timeout: config.timeout || 1e4,
|
|
53
|
+
headers: {
|
|
54
|
+
"Content-Type": "application/json"
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
} else {
|
|
58
|
+
if (!config.apiKey && !process.env.SPAPS_API_KEY) {
|
|
59
|
+
console.warn("\u26A0\uFE0F SPAPS: No API key provided. Some features may not work.");
|
|
60
|
+
}
|
|
61
|
+
this.apiKey = config.apiKey || process.env.SPAPS_API_KEY;
|
|
62
|
+
this.client = import_axios.default.create({
|
|
63
|
+
baseURL: apiUrl,
|
|
64
|
+
timeout: config.timeout || 1e4,
|
|
65
|
+
headers: {
|
|
66
|
+
"Content-Type": "application/json",
|
|
67
|
+
...this.apiKey && { "X-API-Key": this.apiKey }
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
this.client.interceptors.request.use((config2) => {
|
|
72
|
+
if (this.accessToken && !config2.headers.Authorization) {
|
|
73
|
+
config2.headers.Authorization = `Bearer ${this.accessToken}`;
|
|
74
|
+
}
|
|
75
|
+
return config2;
|
|
76
|
+
});
|
|
77
|
+
this.client.interceptors.response.use(
|
|
78
|
+
(response) => response,
|
|
79
|
+
async (error) => {
|
|
80
|
+
if (error.response?.status === 401 && this.refreshToken) {
|
|
81
|
+
try {
|
|
82
|
+
const { data } = await this.refresh(this.refreshToken);
|
|
83
|
+
this.accessToken = data.access_token;
|
|
84
|
+
this.refreshToken = data.refresh_token;
|
|
85
|
+
if (error.config) {
|
|
86
|
+
error.config.headers.Authorization = `Bearer ${this.accessToken}`;
|
|
87
|
+
return this.client.request(error.config);
|
|
88
|
+
}
|
|
89
|
+
} catch (refreshError) {
|
|
90
|
+
this.accessToken = void 0;
|
|
91
|
+
this.refreshToken = void 0;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return Promise.reject(error);
|
|
95
|
+
}
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
// Authentication Methods
|
|
99
|
+
async login(email, password) {
|
|
100
|
+
const response = await this.client.post("/api/auth/login", {
|
|
101
|
+
email,
|
|
102
|
+
password
|
|
103
|
+
});
|
|
104
|
+
this.accessToken = response.data.access_token;
|
|
105
|
+
this.refreshToken = response.data.refresh_token;
|
|
106
|
+
return response;
|
|
107
|
+
}
|
|
108
|
+
async register(email, password) {
|
|
109
|
+
const response = await this.client.post("/api/auth/register", {
|
|
110
|
+
email,
|
|
111
|
+
password
|
|
112
|
+
});
|
|
113
|
+
this.accessToken = response.data.access_token;
|
|
114
|
+
this.refreshToken = response.data.refresh_token;
|
|
115
|
+
return response;
|
|
116
|
+
}
|
|
117
|
+
async walletSignIn(walletAddress, signature, message, chainType = "solana") {
|
|
118
|
+
const response = await this.client.post("/api/auth/wallet-sign-in", {
|
|
119
|
+
wallet_address: walletAddress,
|
|
120
|
+
signature,
|
|
121
|
+
message,
|
|
122
|
+
chain_type: chainType
|
|
123
|
+
});
|
|
124
|
+
this.accessToken = response.data.access_token;
|
|
125
|
+
this.refreshToken = response.data.refresh_token;
|
|
126
|
+
return response;
|
|
127
|
+
}
|
|
128
|
+
async refresh(refreshToken) {
|
|
129
|
+
const response = await this.client.post("/api/auth/refresh", {
|
|
130
|
+
refresh_token: refreshToken || this.refreshToken
|
|
131
|
+
});
|
|
132
|
+
this.accessToken = response.data.access_token;
|
|
133
|
+
this.refreshToken = response.data.refresh_token;
|
|
134
|
+
return response;
|
|
135
|
+
}
|
|
136
|
+
async logout() {
|
|
137
|
+
await this.client.post("/api/auth/logout");
|
|
138
|
+
this.accessToken = void 0;
|
|
139
|
+
this.refreshToken = void 0;
|
|
140
|
+
}
|
|
141
|
+
async getUser() {
|
|
142
|
+
return this.client.get("/api/auth/user");
|
|
143
|
+
}
|
|
144
|
+
// Stripe Methods
|
|
145
|
+
async createCheckoutSession(priceId, successUrl, cancelUrl) {
|
|
146
|
+
return this.client.post("/api/stripe/create-checkout-session", {
|
|
147
|
+
price_id: priceId,
|
|
148
|
+
success_url: successUrl,
|
|
149
|
+
cancel_url: cancelUrl
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
async getSubscription() {
|
|
153
|
+
return this.client.get("/api/stripe/subscription");
|
|
154
|
+
}
|
|
155
|
+
async cancelSubscription() {
|
|
156
|
+
await this.client.delete("/api/stripe/subscription");
|
|
157
|
+
}
|
|
158
|
+
// Usage Methods
|
|
159
|
+
async getUsageBalance() {
|
|
160
|
+
return this.client.get("/api/usage/balance");
|
|
161
|
+
}
|
|
162
|
+
async recordUsage(feature, amount) {
|
|
163
|
+
await this.client.post("/api/usage/record", {
|
|
164
|
+
feature,
|
|
165
|
+
amount
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
// Utility Methods
|
|
169
|
+
isAuthenticated() {
|
|
170
|
+
return !!this.accessToken;
|
|
171
|
+
}
|
|
172
|
+
getAccessToken() {
|
|
173
|
+
return this.accessToken;
|
|
174
|
+
}
|
|
175
|
+
setAccessToken(token) {
|
|
176
|
+
this.accessToken = token;
|
|
177
|
+
}
|
|
178
|
+
isLocalMode() {
|
|
179
|
+
return this._isLocalMode;
|
|
180
|
+
}
|
|
181
|
+
async health() {
|
|
182
|
+
return this.client.get("/health");
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
var index_default = SPAPSClient;
|
|
186
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
187
|
+
0 && (module.exports = {
|
|
188
|
+
SPAPS,
|
|
189
|
+
SPAPSClient,
|
|
190
|
+
SweetPotatoSDK
|
|
191
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import axios from "axios";
|
|
3
|
+
var SPAPSClient = class {
|
|
4
|
+
client;
|
|
5
|
+
apiKey;
|
|
6
|
+
accessToken;
|
|
7
|
+
refreshToken;
|
|
8
|
+
_isLocalMode = false;
|
|
9
|
+
constructor(config = {}) {
|
|
10
|
+
const apiUrl = config.apiUrl || process.env.SPAPS_API_URL || process.env.NEXT_PUBLIC_SPAPS_API_URL;
|
|
11
|
+
if (!apiUrl || apiUrl.includes("localhost") || apiUrl.includes("127.0.0.1")) {
|
|
12
|
+
this._isLocalMode = true;
|
|
13
|
+
this.client = axios.create({
|
|
14
|
+
baseURL: apiUrl || "http://localhost:3300",
|
|
15
|
+
timeout: config.timeout || 1e4,
|
|
16
|
+
headers: {
|
|
17
|
+
"Content-Type": "application/json"
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
} else {
|
|
21
|
+
if (!config.apiKey && !process.env.SPAPS_API_KEY) {
|
|
22
|
+
console.warn("\u26A0\uFE0F SPAPS: No API key provided. Some features may not work.");
|
|
23
|
+
}
|
|
24
|
+
this.apiKey = config.apiKey || process.env.SPAPS_API_KEY;
|
|
25
|
+
this.client = axios.create({
|
|
26
|
+
baseURL: apiUrl,
|
|
27
|
+
timeout: config.timeout || 1e4,
|
|
28
|
+
headers: {
|
|
29
|
+
"Content-Type": "application/json",
|
|
30
|
+
...this.apiKey && { "X-API-Key": this.apiKey }
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
this.client.interceptors.request.use((config2) => {
|
|
35
|
+
if (this.accessToken && !config2.headers.Authorization) {
|
|
36
|
+
config2.headers.Authorization = `Bearer ${this.accessToken}`;
|
|
37
|
+
}
|
|
38
|
+
return config2;
|
|
39
|
+
});
|
|
40
|
+
this.client.interceptors.response.use(
|
|
41
|
+
(response) => response,
|
|
42
|
+
async (error) => {
|
|
43
|
+
if (error.response?.status === 401 && this.refreshToken) {
|
|
44
|
+
try {
|
|
45
|
+
const { data } = await this.refresh(this.refreshToken);
|
|
46
|
+
this.accessToken = data.access_token;
|
|
47
|
+
this.refreshToken = data.refresh_token;
|
|
48
|
+
if (error.config) {
|
|
49
|
+
error.config.headers.Authorization = `Bearer ${this.accessToken}`;
|
|
50
|
+
return this.client.request(error.config);
|
|
51
|
+
}
|
|
52
|
+
} catch (refreshError) {
|
|
53
|
+
this.accessToken = void 0;
|
|
54
|
+
this.refreshToken = void 0;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return Promise.reject(error);
|
|
58
|
+
}
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
// Authentication Methods
|
|
62
|
+
async login(email, password) {
|
|
63
|
+
const response = await this.client.post("/api/auth/login", {
|
|
64
|
+
email,
|
|
65
|
+
password
|
|
66
|
+
});
|
|
67
|
+
this.accessToken = response.data.access_token;
|
|
68
|
+
this.refreshToken = response.data.refresh_token;
|
|
69
|
+
return response;
|
|
70
|
+
}
|
|
71
|
+
async register(email, password) {
|
|
72
|
+
const response = await this.client.post("/api/auth/register", {
|
|
73
|
+
email,
|
|
74
|
+
password
|
|
75
|
+
});
|
|
76
|
+
this.accessToken = response.data.access_token;
|
|
77
|
+
this.refreshToken = response.data.refresh_token;
|
|
78
|
+
return response;
|
|
79
|
+
}
|
|
80
|
+
async walletSignIn(walletAddress, signature, message, chainType = "solana") {
|
|
81
|
+
const response = await this.client.post("/api/auth/wallet-sign-in", {
|
|
82
|
+
wallet_address: walletAddress,
|
|
83
|
+
signature,
|
|
84
|
+
message,
|
|
85
|
+
chain_type: chainType
|
|
86
|
+
});
|
|
87
|
+
this.accessToken = response.data.access_token;
|
|
88
|
+
this.refreshToken = response.data.refresh_token;
|
|
89
|
+
return response;
|
|
90
|
+
}
|
|
91
|
+
async refresh(refreshToken) {
|
|
92
|
+
const response = await this.client.post("/api/auth/refresh", {
|
|
93
|
+
refresh_token: refreshToken || this.refreshToken
|
|
94
|
+
});
|
|
95
|
+
this.accessToken = response.data.access_token;
|
|
96
|
+
this.refreshToken = response.data.refresh_token;
|
|
97
|
+
return response;
|
|
98
|
+
}
|
|
99
|
+
async logout() {
|
|
100
|
+
await this.client.post("/api/auth/logout");
|
|
101
|
+
this.accessToken = void 0;
|
|
102
|
+
this.refreshToken = void 0;
|
|
103
|
+
}
|
|
104
|
+
async getUser() {
|
|
105
|
+
return this.client.get("/api/auth/user");
|
|
106
|
+
}
|
|
107
|
+
// Stripe Methods
|
|
108
|
+
async createCheckoutSession(priceId, successUrl, cancelUrl) {
|
|
109
|
+
return this.client.post("/api/stripe/create-checkout-session", {
|
|
110
|
+
price_id: priceId,
|
|
111
|
+
success_url: successUrl,
|
|
112
|
+
cancel_url: cancelUrl
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
async getSubscription() {
|
|
116
|
+
return this.client.get("/api/stripe/subscription");
|
|
117
|
+
}
|
|
118
|
+
async cancelSubscription() {
|
|
119
|
+
await this.client.delete("/api/stripe/subscription");
|
|
120
|
+
}
|
|
121
|
+
// Usage Methods
|
|
122
|
+
async getUsageBalance() {
|
|
123
|
+
return this.client.get("/api/usage/balance");
|
|
124
|
+
}
|
|
125
|
+
async recordUsage(feature, amount) {
|
|
126
|
+
await this.client.post("/api/usage/record", {
|
|
127
|
+
feature,
|
|
128
|
+
amount
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
// Utility Methods
|
|
132
|
+
isAuthenticated() {
|
|
133
|
+
return !!this.accessToken;
|
|
134
|
+
}
|
|
135
|
+
getAccessToken() {
|
|
136
|
+
return this.accessToken;
|
|
137
|
+
}
|
|
138
|
+
setAccessToken(token) {
|
|
139
|
+
this.accessToken = token;
|
|
140
|
+
}
|
|
141
|
+
isLocalMode() {
|
|
142
|
+
return this._isLocalMode;
|
|
143
|
+
}
|
|
144
|
+
async health() {
|
|
145
|
+
return this.client.get("/health");
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
var index_default = SPAPSClient;
|
|
149
|
+
export {
|
|
150
|
+
SPAPSClient as SPAPS,
|
|
151
|
+
SPAPSClient,
|
|
152
|
+
SPAPSClient as SweetPotatoSDK,
|
|
153
|
+
index_default as default
|
|
154
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "spaps-sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Sweet Potato Authentication & Payment Service SDK - Zero-config client for SPAPS",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.mjs",
|
|
11
|
+
"require": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./client": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.mjs",
|
|
16
|
+
"require": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
21
|
+
"dev": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
22
|
+
"test": "echo \"No tests yet\"",
|
|
23
|
+
"prepublishOnly": "npm run build"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"authentication",
|
|
27
|
+
"payments",
|
|
28
|
+
"stripe",
|
|
29
|
+
"spaps",
|
|
30
|
+
"sdk",
|
|
31
|
+
"client",
|
|
32
|
+
"sweet-potato",
|
|
33
|
+
"wallet-auth"
|
|
34
|
+
],
|
|
35
|
+
"author": "buildooor",
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "https://github.com/yourusername/sweet-potato"
|
|
40
|
+
},
|
|
41
|
+
"homepage": "https://sweetpotato.dev",
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"axios": "^1.6.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/node": "^20.10.0",
|
|
47
|
+
"tsup": "^8.0.1",
|
|
48
|
+
"typescript": "^5.3.2"
|
|
49
|
+
},
|
|
50
|
+
"peerDependencies": {
|
|
51
|
+
"typescript": ">=4.5.0"
|
|
52
|
+
},
|
|
53
|
+
"peerDependenciesMeta": {
|
|
54
|
+
"typescript": {
|
|
55
|
+
"optional": true
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
"files": [
|
|
59
|
+
"dist",
|
|
60
|
+
"README.md"
|
|
61
|
+
]
|
|
62
|
+
}
|