@proma-dev/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/LICENSE +21 -0
- package/README.md +155 -0
- package/dist/index.cjs +409 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +186 -0
- package/dist/index.d.ts +186 -0
- package/dist/index.js +381 -0
- package/dist/index.js.map +1 -0
- package/dist/react/index.cjs +540 -0
- package/dist/react/index.cjs.map +1 -0
- package/dist/react/index.d.cts +227 -0
- package/dist/react/index.d.ts +227 -0
- package/dist/react/index.js +519 -0
- package/dist/react/index.js.map +1 -0
- package/package.json +73 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
type OAuthScope = 'profile' | 'credits' | 'ai:chat';
|
|
2
|
+
interface PromaClientConfig {
|
|
3
|
+
/** Your app's client ID from the Proma developer portal. */
|
|
4
|
+
clientId: string;
|
|
5
|
+
/** The URL Proma will redirect to after login. Must be registered in your app settings. */
|
|
6
|
+
redirectUri: string;
|
|
7
|
+
/** Scopes to request. Defaults to ['profile']. */
|
|
8
|
+
scopes?: OAuthScope[];
|
|
9
|
+
/** Override the Proma base URL. Defaults to https://proma.dev */
|
|
10
|
+
baseUrl?: string;
|
|
11
|
+
/** Custom storage for tokens. Defaults to localStorage. */
|
|
12
|
+
storage?: TokenStorage;
|
|
13
|
+
}
|
|
14
|
+
interface TokenStorage {
|
|
15
|
+
getItem(key: string): string | null;
|
|
16
|
+
setItem(key: string, value: string): void;
|
|
17
|
+
removeItem(key: string): void;
|
|
18
|
+
}
|
|
19
|
+
interface Session {
|
|
20
|
+
accessToken: string;
|
|
21
|
+
refreshToken: string;
|
|
22
|
+
/** Unix timestamp (ms) when the access token expires. */
|
|
23
|
+
expiresAt: number;
|
|
24
|
+
scope: string;
|
|
25
|
+
}
|
|
26
|
+
/** Raw token response from the Proma token endpoint. */
|
|
27
|
+
interface TokenResponse {
|
|
28
|
+
access_token: string;
|
|
29
|
+
token_type: 'Bearer';
|
|
30
|
+
expires_in: number;
|
|
31
|
+
refresh_token: string;
|
|
32
|
+
scope: string;
|
|
33
|
+
}
|
|
34
|
+
interface UserInfo {
|
|
35
|
+
sub: string;
|
|
36
|
+
email?: string;
|
|
37
|
+
name?: string;
|
|
38
|
+
picture?: string | null;
|
|
39
|
+
}
|
|
40
|
+
interface BalanceResponse {
|
|
41
|
+
balance: number;
|
|
42
|
+
/** Human-readable balance, e.g. "$1.23" */
|
|
43
|
+
formatted: string;
|
|
44
|
+
}
|
|
45
|
+
interface SpendCreditsResponse {
|
|
46
|
+
success: true;
|
|
47
|
+
amount_spent: number;
|
|
48
|
+
new_balance: number;
|
|
49
|
+
}
|
|
50
|
+
interface ChatMessage {
|
|
51
|
+
role: 'user' | 'assistant' | 'system';
|
|
52
|
+
content: string;
|
|
53
|
+
}
|
|
54
|
+
interface ChatOptions {
|
|
55
|
+
messages: ChatMessage[];
|
|
56
|
+
/** Gemini model to use. Defaults to gemini-2.0-flash. */
|
|
57
|
+
model?: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
declare class PromaClient {
|
|
61
|
+
private readonly config;
|
|
62
|
+
readonly baseUrl: string;
|
|
63
|
+
private readonly store;
|
|
64
|
+
private readonly defaultScopes;
|
|
65
|
+
/** Credits API — requires the `credits` scope. */
|
|
66
|
+
readonly credits: CreditsApi;
|
|
67
|
+
/** AI gateway API — requires the `ai:chat` scope. */
|
|
68
|
+
readonly ai: AiApi;
|
|
69
|
+
constructor(config: PromaClientConfig);
|
|
70
|
+
/**
|
|
71
|
+
* Redirects the user to Proma's login page.
|
|
72
|
+
* Call this on a button click — it will navigate away from the current page.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* button.onclick = () => proma.login()
|
|
76
|
+
*/
|
|
77
|
+
login(scopes?: OAuthScope[]): Promise<void>;
|
|
78
|
+
/**
|
|
79
|
+
* Builds the authorization URL without navigating.
|
|
80
|
+
* Useful if you want to control the redirect yourself.
|
|
81
|
+
*/
|
|
82
|
+
buildAuthorizeUrl(scopes?: OAuthScope[]): Promise<string>;
|
|
83
|
+
/**
|
|
84
|
+
* Handles the OAuth callback. Call this on your redirect page.
|
|
85
|
+
* Reads the `code` from the URL, exchanges it for tokens, and stores the session.
|
|
86
|
+
*
|
|
87
|
+
* @param url - Defaults to `window.location.href`
|
|
88
|
+
* @returns The new session
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* // pages/callback.tsx
|
|
92
|
+
* useEffect(() => {
|
|
93
|
+
* proma.handleCallback().then(session => {
|
|
94
|
+
* router.push('/dashboard')
|
|
95
|
+
* })
|
|
96
|
+
* }, [])
|
|
97
|
+
*/
|
|
98
|
+
handleCallback(url?: string): Promise<Session>;
|
|
99
|
+
/**
|
|
100
|
+
* Returns the current session (access token, refresh token, expiry).
|
|
101
|
+
* Automatically refreshes the access token if it is expired.
|
|
102
|
+
* Returns `null` if the user is not logged in.
|
|
103
|
+
*/
|
|
104
|
+
getSession(): Promise<Session | null>;
|
|
105
|
+
/**
|
|
106
|
+
* Returns `true` if the user has a valid (or refreshable) session.
|
|
107
|
+
*/
|
|
108
|
+
isAuthenticated(): Promise<boolean>;
|
|
109
|
+
/**
|
|
110
|
+
* Fetches the logged-in user's profile.
|
|
111
|
+
* Requires the `profile` scope.
|
|
112
|
+
*/
|
|
113
|
+
getUser(): Promise<UserInfo>;
|
|
114
|
+
/**
|
|
115
|
+
* Clears the stored session and logs the user out.
|
|
116
|
+
* Does not revoke the token server-side.
|
|
117
|
+
*/
|
|
118
|
+
logout(): void;
|
|
119
|
+
requireAccessToken(): Promise<string>;
|
|
120
|
+
private refresh;
|
|
121
|
+
private fetchTokens;
|
|
122
|
+
private tokensToSession;
|
|
123
|
+
}
|
|
124
|
+
declare class CreditsApi {
|
|
125
|
+
private readonly client;
|
|
126
|
+
constructor(client: PromaClient);
|
|
127
|
+
/**
|
|
128
|
+
* Returns the user's current credit balance.
|
|
129
|
+
* Requires scope: `credits`
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* const { balance, formatted } = await proma.credits.getBalance()
|
|
133
|
+
* console.log(`You have ${formatted}`) // "You have $1.23"
|
|
134
|
+
*/
|
|
135
|
+
getBalance(): Promise<BalanceResponse>;
|
|
136
|
+
/**
|
|
137
|
+
* Deducts credits from the user's account.
|
|
138
|
+
* Requires scope: `credits`
|
|
139
|
+
*
|
|
140
|
+
* @param amount - Micro-credits to spend. 1,000,000 = $1.00
|
|
141
|
+
* @param description - Optional description for the transaction ledger.
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* await proma.credits.spend(500_000, 'Generated a report')
|
|
145
|
+
*/
|
|
146
|
+
spend(amount: number, description?: string): Promise<SpendCreditsResponse>;
|
|
147
|
+
}
|
|
148
|
+
declare class AiApi {
|
|
149
|
+
private readonly client;
|
|
150
|
+
constructor(client: PromaClient);
|
|
151
|
+
/**
|
|
152
|
+
* Sends a chat request through the Proma AI gateway (Gemini).
|
|
153
|
+
* Credits are deducted automatically per token used.
|
|
154
|
+
* Requires scope: `ai:chat`
|
|
155
|
+
*
|
|
156
|
+
* Returns a streaming `Response` — iterate SSE chunks or use a helper library.
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* const stream = await proma.ai.chat({
|
|
160
|
+
* messages: [{ role: 'user', content: 'Explain quantum entanglement simply.' }]
|
|
161
|
+
* })
|
|
162
|
+
* const reader = stream.body.getReader()
|
|
163
|
+
*/
|
|
164
|
+
chat(options: ChatOptions | ChatMessage[]): Promise<Response>;
|
|
165
|
+
/**
|
|
166
|
+
* Convenience wrapper around `chat` that collects the full streamed text.
|
|
167
|
+
* Use this when you don't need streaming and just want the final string.
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* const text = await proma.ai.chatText({
|
|
171
|
+
* messages: [{ role: 'user', content: 'Hello!' }]
|
|
172
|
+
* })
|
|
173
|
+
* console.log(text)
|
|
174
|
+
*/
|
|
175
|
+
chatText(options: ChatOptions | ChatMessage[]): Promise<string>;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Default in-memory storage for environments without localStorage (SSR, Node). */
|
|
179
|
+
declare class MemoryStorage implements TokenStorage {
|
|
180
|
+
private map;
|
|
181
|
+
getItem(key: string): string | null;
|
|
182
|
+
setItem(key: string, value: string): void;
|
|
183
|
+
removeItem(key: string): void;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export { type BalanceResponse, type ChatMessage, type ChatOptions, MemoryStorage, type OAuthScope, PromaClient, type PromaClientConfig, type Session, type SpendCreditsResponse, type TokenResponse, type TokenStorage, type UserInfo };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
type OAuthScope = 'profile' | 'credits' | 'ai:chat';
|
|
2
|
+
interface PromaClientConfig {
|
|
3
|
+
/** Your app's client ID from the Proma developer portal. */
|
|
4
|
+
clientId: string;
|
|
5
|
+
/** The URL Proma will redirect to after login. Must be registered in your app settings. */
|
|
6
|
+
redirectUri: string;
|
|
7
|
+
/** Scopes to request. Defaults to ['profile']. */
|
|
8
|
+
scopes?: OAuthScope[];
|
|
9
|
+
/** Override the Proma base URL. Defaults to https://proma.dev */
|
|
10
|
+
baseUrl?: string;
|
|
11
|
+
/** Custom storage for tokens. Defaults to localStorage. */
|
|
12
|
+
storage?: TokenStorage;
|
|
13
|
+
}
|
|
14
|
+
interface TokenStorage {
|
|
15
|
+
getItem(key: string): string | null;
|
|
16
|
+
setItem(key: string, value: string): void;
|
|
17
|
+
removeItem(key: string): void;
|
|
18
|
+
}
|
|
19
|
+
interface Session {
|
|
20
|
+
accessToken: string;
|
|
21
|
+
refreshToken: string;
|
|
22
|
+
/** Unix timestamp (ms) when the access token expires. */
|
|
23
|
+
expiresAt: number;
|
|
24
|
+
scope: string;
|
|
25
|
+
}
|
|
26
|
+
/** Raw token response from the Proma token endpoint. */
|
|
27
|
+
interface TokenResponse {
|
|
28
|
+
access_token: string;
|
|
29
|
+
token_type: 'Bearer';
|
|
30
|
+
expires_in: number;
|
|
31
|
+
refresh_token: string;
|
|
32
|
+
scope: string;
|
|
33
|
+
}
|
|
34
|
+
interface UserInfo {
|
|
35
|
+
sub: string;
|
|
36
|
+
email?: string;
|
|
37
|
+
name?: string;
|
|
38
|
+
picture?: string | null;
|
|
39
|
+
}
|
|
40
|
+
interface BalanceResponse {
|
|
41
|
+
balance: number;
|
|
42
|
+
/** Human-readable balance, e.g. "$1.23" */
|
|
43
|
+
formatted: string;
|
|
44
|
+
}
|
|
45
|
+
interface SpendCreditsResponse {
|
|
46
|
+
success: true;
|
|
47
|
+
amount_spent: number;
|
|
48
|
+
new_balance: number;
|
|
49
|
+
}
|
|
50
|
+
interface ChatMessage {
|
|
51
|
+
role: 'user' | 'assistant' | 'system';
|
|
52
|
+
content: string;
|
|
53
|
+
}
|
|
54
|
+
interface ChatOptions {
|
|
55
|
+
messages: ChatMessage[];
|
|
56
|
+
/** Gemini model to use. Defaults to gemini-2.0-flash. */
|
|
57
|
+
model?: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
declare class PromaClient {
|
|
61
|
+
private readonly config;
|
|
62
|
+
readonly baseUrl: string;
|
|
63
|
+
private readonly store;
|
|
64
|
+
private readonly defaultScopes;
|
|
65
|
+
/** Credits API — requires the `credits` scope. */
|
|
66
|
+
readonly credits: CreditsApi;
|
|
67
|
+
/** AI gateway API — requires the `ai:chat` scope. */
|
|
68
|
+
readonly ai: AiApi;
|
|
69
|
+
constructor(config: PromaClientConfig);
|
|
70
|
+
/**
|
|
71
|
+
* Redirects the user to Proma's login page.
|
|
72
|
+
* Call this on a button click — it will navigate away from the current page.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* button.onclick = () => proma.login()
|
|
76
|
+
*/
|
|
77
|
+
login(scopes?: OAuthScope[]): Promise<void>;
|
|
78
|
+
/**
|
|
79
|
+
* Builds the authorization URL without navigating.
|
|
80
|
+
* Useful if you want to control the redirect yourself.
|
|
81
|
+
*/
|
|
82
|
+
buildAuthorizeUrl(scopes?: OAuthScope[]): Promise<string>;
|
|
83
|
+
/**
|
|
84
|
+
* Handles the OAuth callback. Call this on your redirect page.
|
|
85
|
+
* Reads the `code` from the URL, exchanges it for tokens, and stores the session.
|
|
86
|
+
*
|
|
87
|
+
* @param url - Defaults to `window.location.href`
|
|
88
|
+
* @returns The new session
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* // pages/callback.tsx
|
|
92
|
+
* useEffect(() => {
|
|
93
|
+
* proma.handleCallback().then(session => {
|
|
94
|
+
* router.push('/dashboard')
|
|
95
|
+
* })
|
|
96
|
+
* }, [])
|
|
97
|
+
*/
|
|
98
|
+
handleCallback(url?: string): Promise<Session>;
|
|
99
|
+
/**
|
|
100
|
+
* Returns the current session (access token, refresh token, expiry).
|
|
101
|
+
* Automatically refreshes the access token if it is expired.
|
|
102
|
+
* Returns `null` if the user is not logged in.
|
|
103
|
+
*/
|
|
104
|
+
getSession(): Promise<Session | null>;
|
|
105
|
+
/**
|
|
106
|
+
* Returns `true` if the user has a valid (or refreshable) session.
|
|
107
|
+
*/
|
|
108
|
+
isAuthenticated(): Promise<boolean>;
|
|
109
|
+
/**
|
|
110
|
+
* Fetches the logged-in user's profile.
|
|
111
|
+
* Requires the `profile` scope.
|
|
112
|
+
*/
|
|
113
|
+
getUser(): Promise<UserInfo>;
|
|
114
|
+
/**
|
|
115
|
+
* Clears the stored session and logs the user out.
|
|
116
|
+
* Does not revoke the token server-side.
|
|
117
|
+
*/
|
|
118
|
+
logout(): void;
|
|
119
|
+
requireAccessToken(): Promise<string>;
|
|
120
|
+
private refresh;
|
|
121
|
+
private fetchTokens;
|
|
122
|
+
private tokensToSession;
|
|
123
|
+
}
|
|
124
|
+
declare class CreditsApi {
|
|
125
|
+
private readonly client;
|
|
126
|
+
constructor(client: PromaClient);
|
|
127
|
+
/**
|
|
128
|
+
* Returns the user's current credit balance.
|
|
129
|
+
* Requires scope: `credits`
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* const { balance, formatted } = await proma.credits.getBalance()
|
|
133
|
+
* console.log(`You have ${formatted}`) // "You have $1.23"
|
|
134
|
+
*/
|
|
135
|
+
getBalance(): Promise<BalanceResponse>;
|
|
136
|
+
/**
|
|
137
|
+
* Deducts credits from the user's account.
|
|
138
|
+
* Requires scope: `credits`
|
|
139
|
+
*
|
|
140
|
+
* @param amount - Micro-credits to spend. 1,000,000 = $1.00
|
|
141
|
+
* @param description - Optional description for the transaction ledger.
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* await proma.credits.spend(500_000, 'Generated a report')
|
|
145
|
+
*/
|
|
146
|
+
spend(amount: number, description?: string): Promise<SpendCreditsResponse>;
|
|
147
|
+
}
|
|
148
|
+
declare class AiApi {
|
|
149
|
+
private readonly client;
|
|
150
|
+
constructor(client: PromaClient);
|
|
151
|
+
/**
|
|
152
|
+
* Sends a chat request through the Proma AI gateway (Gemini).
|
|
153
|
+
* Credits are deducted automatically per token used.
|
|
154
|
+
* Requires scope: `ai:chat`
|
|
155
|
+
*
|
|
156
|
+
* Returns a streaming `Response` — iterate SSE chunks or use a helper library.
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* const stream = await proma.ai.chat({
|
|
160
|
+
* messages: [{ role: 'user', content: 'Explain quantum entanglement simply.' }]
|
|
161
|
+
* })
|
|
162
|
+
* const reader = stream.body.getReader()
|
|
163
|
+
*/
|
|
164
|
+
chat(options: ChatOptions | ChatMessage[]): Promise<Response>;
|
|
165
|
+
/**
|
|
166
|
+
* Convenience wrapper around `chat` that collects the full streamed text.
|
|
167
|
+
* Use this when you don't need streaming and just want the final string.
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* const text = await proma.ai.chatText({
|
|
171
|
+
* messages: [{ role: 'user', content: 'Hello!' }]
|
|
172
|
+
* })
|
|
173
|
+
* console.log(text)
|
|
174
|
+
*/
|
|
175
|
+
chatText(options: ChatOptions | ChatMessage[]): Promise<string>;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Default in-memory storage for environments without localStorage (SSR, Node). */
|
|
179
|
+
declare class MemoryStorage implements TokenStorage {
|
|
180
|
+
private map;
|
|
181
|
+
getItem(key: string): string | null;
|
|
182
|
+
setItem(key: string, value: string): void;
|
|
183
|
+
removeItem(key: string): void;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export { type BalanceResponse, type ChatMessage, type ChatOptions, MemoryStorage, type OAuthScope, PromaClient, type PromaClientConfig, type Session, type SpendCreditsResponse, type TokenResponse, type TokenStorage, type UserInfo };
|