@arinova-ai/spaces-sdk 0.1.2 → 0.1.3

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/dist/index.d.ts CHANGED
@@ -30,11 +30,33 @@ export interface TokenResponse {
30
30
  image: string | null;
31
31
  };
32
32
  }
33
+ export interface BalanceResponse {
34
+ balance: number;
35
+ }
36
+ export interface PurchaseResponse {
37
+ transactionId: string;
38
+ newBalance: number;
39
+ }
40
+ export interface TransactionRecord {
41
+ id: string;
42
+ type: string;
43
+ amount: number;
44
+ description: string | null;
45
+ createdAt: string;
46
+ }
47
+ export interface TransactionsResponse {
48
+ transactions: TransactionRecord[];
49
+ total: number;
50
+ limit: number;
51
+ offset: number;
52
+ }
33
53
  export declare class Arinova {
34
54
  private appId;
35
55
  private endpoint;
36
56
  private redirectUri;
37
57
  private scope;
58
+ /** The access token from the most recent login/handleCallback. */
59
+ accessToken: string | null;
38
60
  constructor(config: ArinovaConfig);
39
61
  /**
40
62
  * Start the OAuth PKCE login flow.
@@ -47,6 +69,17 @@ export declare class Arinova {
47
69
  * Reads code and state from URL, exchanges for token.
48
70
  */
49
71
  handleCallback(): Promise<TokenResponse>;
72
+ private getToken;
73
+ private apiFetch;
74
+ /** Get the current user's coin balance. */
75
+ balance(): Promise<BalanceResponse>;
76
+ /**
77
+ * Purchase / charge coins from the user's balance.
78
+ * Requires the "economy" scope.
79
+ */
80
+ purchase(productId: string, amount: number, description?: string): Promise<PurchaseResponse>;
81
+ /** Get the user's transaction history. */
82
+ transactions(limit?: number, offset?: number): Promise<TransactionsResponse>;
50
83
  private exchangeCode;
51
84
  }
52
85
  export default Arinova;
package/dist/index.js CHANGED
@@ -10,6 +10,8 @@
10
10
  */
11
11
  export class Arinova {
12
12
  constructor(config) {
13
+ /** The access token from the most recent login/handleCallback. */
14
+ this.accessToken = null;
13
15
  this.appId = config.appId;
14
16
  this.endpoint = (config.endpoint ?? "https://chat.arinova.ai").replace(/\/+$/, "");
15
17
  this.redirectUri =
@@ -66,7 +68,10 @@ export class Arinova {
66
68
  reject(new Error(url.searchParams.get("error_description") ?? "No code received"));
67
69
  return;
68
70
  }
69
- this.exchangeCode(code, verifier).then(resolve).catch(reject);
71
+ this.exchangeCode(code, verifier).then((token) => {
72
+ this.accessToken = token.access_token;
73
+ resolve(token);
74
+ }).catch(reject);
70
75
  }
71
76
  }
72
77
  catch {
@@ -105,7 +110,58 @@ export class Arinova {
105
110
  if (!verifier) {
106
111
  throw new Error("No PKCE verifier found — did you start login()?");
107
112
  }
108
- return this.exchangeCode(code, verifier);
113
+ const token = await this.exchangeCode(code, verifier);
114
+ this.accessToken = token.access_token;
115
+ return token;
116
+ }
117
+ // ── Economy Methods ───────────────────────────────────────────
118
+ getToken() {
119
+ if (!this.accessToken) {
120
+ throw new Error("Not logged in — call login() or handleCallback() first");
121
+ }
122
+ return this.accessToken;
123
+ }
124
+ async apiFetch(path, init) {
125
+ const token = this.getToken();
126
+ const res = await fetch(`${this.endpoint}${path}`, {
127
+ ...init,
128
+ headers: {
129
+ "Content-Type": "application/json",
130
+ Authorization: `Bearer ${token}`,
131
+ ...init?.headers,
132
+ },
133
+ });
134
+ if (!res.ok) {
135
+ const body = await res.json().catch(() => ({}));
136
+ throw new Error(body.error_description ??
137
+ body.error ??
138
+ `API error (${res.status})`);
139
+ }
140
+ return res.json();
141
+ }
142
+ /** Get the current user's coin balance. */
143
+ async balance() {
144
+ return this.apiFetch("/api/v1/economy/balance");
145
+ }
146
+ /**
147
+ * Purchase / charge coins from the user's balance.
148
+ * Requires the "economy" scope.
149
+ */
150
+ async purchase(productId, amount, description) {
151
+ return this.apiFetch("/api/v1/economy/purchase", {
152
+ method: "POST",
153
+ body: JSON.stringify({ productId, amount, description }),
154
+ });
155
+ }
156
+ /** Get the user's transaction history. */
157
+ async transactions(limit, offset) {
158
+ const params = new URLSearchParams();
159
+ if (limit != null)
160
+ params.set("limit", String(limit));
161
+ if (offset != null)
162
+ params.set("offset", String(offset));
163
+ const qs = params.toString();
164
+ return this.apiFetch(`/api/v1/economy/transactions${qs ? `?${qs}` : ""}`);
109
165
  }
110
166
  async exchangeCode(code, codeVerifier) {
111
167
  const res = await fetch(`${this.endpoint}/oauth/token`, {
package/dist/types.d.ts CHANGED
@@ -68,6 +68,28 @@ export interface AwardResponse {
68
68
  export interface BalanceResponse {
69
69
  balance: number;
70
70
  }
71
+ export interface PurchaseOptions {
72
+ productId: string;
73
+ amount: number;
74
+ description?: string;
75
+ }
76
+ export interface PurchaseResponse {
77
+ transactionId: string;
78
+ newBalance: number;
79
+ }
80
+ export interface TransactionRecord {
81
+ id: string;
82
+ type: string;
83
+ amount: number;
84
+ description: string | null;
85
+ createdAt: string;
86
+ }
87
+ export interface TransactionsResponse {
88
+ transactions: TransactionRecord[];
89
+ total: number;
90
+ limit: number;
91
+ offset: number;
92
+ }
71
93
  export interface SSEChunkEvent {
72
94
  type: "chunk";
73
95
  content: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arinova-ai/spaces-sdk",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Arinova Spaces SDK — OAuth PKCE login for third-party apps",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",