@stacknet/stacks 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 +103 -0
- package/dist/billing-BaJlf_S8.d.cts +1154 -0
- package/dist/billing-BaJlf_S8.d.ts +1154 -0
- package/dist/clients/index.cjs +4 -0
- package/dist/clients/index.d.cts +752 -0
- package/dist/clients/index.d.ts +752 -0
- package/dist/clients/index.js +4 -0
- package/dist/index-DVzKiF_0.d.cts +198 -0
- package/dist/index-DVzKiF_0.d.ts +198 -0
- package/dist/index.cjs +17 -0
- package/dist/index.d.cts +309 -0
- package/dist/index.d.ts +309 -0
- package/dist/index.js +17 -0
- package/dist/proxy/index.cjs +2 -0
- package/dist/proxy/index.d.cts +1 -0
- package/dist/proxy/index.d.ts +1 -0
- package/dist/proxy/index.js +2 -0
- package/dist/streaming/index.cjs +13 -0
- package/dist/streaming/index.d.cts +145 -0
- package/dist/streaming/index.d.ts +145 -0
- package/dist/streaming/index.js +13 -0
- package/dist/types/index.cjs +1 -0
- package/dist/types/index.d.cts +335 -0
- package/dist/types/index.d.ts +335 -0
- package/dist/types/index.js +1 -0
- package/package.json +75 -0
- package/src/clients/agents.ts +233 -0
- package/src/clients/billing.ts +157 -0
- package/src/clients/coder.ts +655 -0
- package/src/clients/files.ts +86 -0
- package/src/clients/index.ts +93 -0
- package/src/clients/magma.ts +299 -0
- package/src/clients/mcp.ts +208 -0
- package/src/clients/network.ts +118 -0
- package/src/clients/points.ts +403 -0
- package/src/clients/skills.ts +236 -0
- package/src/clients/social.ts +286 -0
- package/src/clients/stack-management.ts +279 -0
- package/src/clients/task-network.ts +303 -0
- package/src/clients/user.ts +84 -0
- package/src/clients/widgets.ts +171 -0
- package/src/index.ts +387 -0
- package/src/managers/index.ts +10 -0
- package/src/managers/task-manager.ts +310 -0
- package/src/proxy/forwarder.ts +146 -0
- package/src/proxy/index.ts +32 -0
- package/src/proxy/route-handlers.ts +950 -0
- package/src/streaming/component-stream.ts +319 -0
- package/src/streaming/index.ts +21 -0
- package/src/streaming/sse.ts +241 -0
- package/src/types/agent.ts +106 -0
- package/src/types/billing.ts +87 -0
- package/src/types/chat.ts +58 -0
- package/src/types/coder.ts +345 -0
- package/src/types/credential.ts +111 -0
- package/src/types/file.ts +15 -0
- package/src/types/imagination.ts +50 -0
- package/src/types/index.ts +20 -0
- package/src/types/mcp.ts +35 -0
- package/src/types/network.ts +97 -0
- package/src/types/points.ts +250 -0
- package/src/types/skill.ts +107 -0
- package/src/types/social.ts +109 -0
- package/src/types/stack.ts +269 -0
- package/src/types/task.ts +41 -0
- package/src/types/user.ts +29 -0
- package/src/types/widget.ts +57 -0
- package/src/utils/constants.ts +26 -0
- package/src/utils/errors.ts +169 -0
- package/src/utils/helpers.ts +85 -0
- package/src/utils/index.ts +7 -0
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Billing Client
|
|
3
|
+
* Plans, checkout, usage, and payment management scoped to a stack
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { DEFAULT_GLAYER_NETWORK_URL, JSON_HEADERS } from '../utils/constants';
|
|
7
|
+
import { StacksSDKError } from '../utils/errors';
|
|
8
|
+
import type {
|
|
9
|
+
BillingState,
|
|
10
|
+
BillingPlansResponse,
|
|
11
|
+
CreateCheckoutSessionResponse,
|
|
12
|
+
BillingPortalResponse,
|
|
13
|
+
BillingClientConfig,
|
|
14
|
+
} from '../types/billing';
|
|
15
|
+
|
|
16
|
+
export type { BillingClientConfig } from '../types/billing';
|
|
17
|
+
|
|
18
|
+
const API_PATH = '/api/v2';
|
|
19
|
+
|
|
20
|
+
export class BillingClient {
|
|
21
|
+
private baseUrl: string;
|
|
22
|
+
private authToken: string | null;
|
|
23
|
+
|
|
24
|
+
constructor(config: BillingClientConfig = {}) {
|
|
25
|
+
this.baseUrl = config.baseUrl || DEFAULT_GLAYER_NETWORK_URL;
|
|
26
|
+
this.authToken = config.authToken || null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
setAuthToken(token: string | null) {
|
|
30
|
+
this.authToken = token;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
private get headers(): Record<string, string> {
|
|
34
|
+
const h: Record<string, string> = { ...JSON_HEADERS };
|
|
35
|
+
if (this.authToken) {
|
|
36
|
+
h['Authorization'] = `Bearer ${this.authToken}`;
|
|
37
|
+
}
|
|
38
|
+
return h;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
private url(path: string): string {
|
|
42
|
+
return `${this.baseUrl}${API_PATH}${path}`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
private async request<T>(method: string, path: string, body?: unknown): Promise<T> {
|
|
46
|
+
const opts: RequestInit = { method, headers: this.headers };
|
|
47
|
+
if (body && method !== 'GET') {
|
|
48
|
+
opts.body = JSON.stringify(body);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const response = await fetch(this.url(path), opts);
|
|
52
|
+
|
|
53
|
+
if (!response.ok) {
|
|
54
|
+
const data = await response.json().catch(() => ({}));
|
|
55
|
+
throw new StacksSDKError(
|
|
56
|
+
response.status === 402 ? 'payment_required:api' : 'bad_request:api',
|
|
57
|
+
data.error?.message || `Request failed: ${response.statusText}`
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const data = await response.json();
|
|
62
|
+
return (data && typeof data === 'object' && 'data' in data) ? data.data : data;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// =========================================================================
|
|
66
|
+
// Plans
|
|
67
|
+
// =========================================================================
|
|
68
|
+
|
|
69
|
+
async getPlans(stackId: string): Promise<BillingPlansResponse> {
|
|
70
|
+
return this.request<BillingPlansResponse>('GET', `/stacks/${stackId}/billing/plans`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// =========================================================================
|
|
74
|
+
// Billing State
|
|
75
|
+
// =========================================================================
|
|
76
|
+
|
|
77
|
+
async getBillingState(stackId: string, identityId: string): Promise<BillingState> {
|
|
78
|
+
return this.request<BillingState>('GET', `/stacks/${stackId}/identities/${identityId}/billing`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// =========================================================================
|
|
82
|
+
// Checkout & Portal
|
|
83
|
+
// =========================================================================
|
|
84
|
+
|
|
85
|
+
async createCheckoutSession(
|
|
86
|
+
stackId: string,
|
|
87
|
+
identityId: string,
|
|
88
|
+
planId: string,
|
|
89
|
+
successUrl: string,
|
|
90
|
+
cancelUrl: string
|
|
91
|
+
): Promise<CreateCheckoutSessionResponse> {
|
|
92
|
+
return this.request<CreateCheckoutSessionResponse>('POST', `/stacks/${stackId}/billing/checkout`, {
|
|
93
|
+
identity_id: identityId,
|
|
94
|
+
plan_id: planId,
|
|
95
|
+
success_url: successUrl,
|
|
96
|
+
cancel_url: cancelUrl,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async createBillingPortal(stackId: string, identityId: string, returnUrl: string): Promise<BillingPortalResponse> {
|
|
101
|
+
return this.request<BillingPortalResponse>('POST', `/stacks/${stackId}/billing/portal`, {
|
|
102
|
+
identity_id: identityId,
|
|
103
|
+
return_url: returnUrl,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// =========================================================================
|
|
108
|
+
// Usage
|
|
109
|
+
// =========================================================================
|
|
110
|
+
|
|
111
|
+
async recordUsage(
|
|
112
|
+
stackId: string,
|
|
113
|
+
identityId: string,
|
|
114
|
+
usageType: string,
|
|
115
|
+
quantity: number,
|
|
116
|
+
idempotencyKey?: string
|
|
117
|
+
): Promise<{ recorded: boolean; total_usage: number }> {
|
|
118
|
+
return this.request('POST', `/stacks/${stackId}/billing/usage`, {
|
|
119
|
+
identity_id: identityId,
|
|
120
|
+
usage_type: usageType,
|
|
121
|
+
quantity,
|
|
122
|
+
idempotency_key: idempotencyKey,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async getCurrentUsage(stackId: string, identityId: string): Promise<{ usage: { type: string; quantity: number; amount: number }[]; total_amount: number }> {
|
|
127
|
+
return this.request('GET', `/stacks/${stackId}/identities/${identityId}/billing/usage`);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// =========================================================================
|
|
131
|
+
// Payment Method
|
|
132
|
+
// =========================================================================
|
|
133
|
+
|
|
134
|
+
async linkStripeCustomer(stackId: string, identityId: string, stripeCustomerId: string, sessionId: string): Promise<{ linked: boolean }> {
|
|
135
|
+
return this.request('POST', `/stacks/${stackId}/billing/link`, {
|
|
136
|
+
identity_id: identityId,
|
|
137
|
+
stripe_customer_id: stripeCustomerId,
|
|
138
|
+
checkout_session_id: sessionId,
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async hasPaymentMethod(stackId: string, identityId: string): Promise<{ has_payment_method: boolean; card_last_four?: string; card_brand?: string }> {
|
|
143
|
+
return this.request('GET', `/stacks/${stackId}/identities/${identityId}/billing/payment-method`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Create a BillingClient instance
|
|
149
|
+
*/
|
|
150
|
+
export function createBillingClient(config: BillingClientConfig = {}): BillingClient {
|
|
151
|
+
return new BillingClient(config);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Default billing client instance
|
|
156
|
+
*/
|
|
157
|
+
export const billingClient = createBillingClient();
|