superso-js-sdk 1.0.1
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/ai/ai.d.ts +76 -0
- package/dist/ai/ai.js +133 -0
- package/dist/ai/cache.d.ts +4 -0
- package/dist/ai/cache.js +29 -0
- package/dist/ai/events.d.ts +5 -0
- package/dist/ai/events.js +17 -0
- package/dist/ai/middleware.d.ts +3 -0
- package/dist/ai/middleware.js +39 -0
- package/dist/auth/auth.d.ts +66 -0
- package/dist/auth/auth.js +285 -0
- package/dist/core/client.d.ts +12 -0
- package/dist/core/client.js +15 -0
- package/dist/database/database.d.ts +53 -0
- package/dist/database/database.js +285 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/notifications/notifications.d.ts +79 -0
- package/dist/notifications/notifications.js +202 -0
- package/dist/realtime/realtime.d.ts +65 -0
- package/dist/realtime/realtime.js +365 -0
- package/dist/storage/storage.d.ts +69 -0
- package/dist/storage/storage.js +260 -0
- package/package.json +56 -0
package/dist/ai/ai.d.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { SupersoClient } from "../core/client";
|
|
2
|
+
export interface AIChatOptions {
|
|
3
|
+
agent: string;
|
|
4
|
+
message: string;
|
|
5
|
+
conversation_id?: string;
|
|
6
|
+
streaming?: boolean;
|
|
7
|
+
temperature?: number;
|
|
8
|
+
memory?: boolean;
|
|
9
|
+
metadata?: any;
|
|
10
|
+
}
|
|
11
|
+
export interface AIChatResponse {
|
|
12
|
+
response: string;
|
|
13
|
+
tokens_used?: number;
|
|
14
|
+
model?: string;
|
|
15
|
+
provider?: string;
|
|
16
|
+
agent?: string;
|
|
17
|
+
project_uid?: string;
|
|
18
|
+
}
|
|
19
|
+
export interface AIProvider {
|
|
20
|
+
id?: string;
|
|
21
|
+
provider_name: string;
|
|
22
|
+
base_url: string;
|
|
23
|
+
api_key?: string;
|
|
24
|
+
organization_id?: string;
|
|
25
|
+
headers_json?: any;
|
|
26
|
+
priority?: number;
|
|
27
|
+
enabled?: boolean;
|
|
28
|
+
created_at?: string;
|
|
29
|
+
}
|
|
30
|
+
export interface AIModel {
|
|
31
|
+
id?: string;
|
|
32
|
+
provider_id: string;
|
|
33
|
+
model_name: string;
|
|
34
|
+
context_window?: number;
|
|
35
|
+
input_cost?: number;
|
|
36
|
+
output_cost?: number;
|
|
37
|
+
supports_vision?: boolean;
|
|
38
|
+
supports_tools?: boolean;
|
|
39
|
+
supports_streaming?: boolean;
|
|
40
|
+
enabled?: boolean;
|
|
41
|
+
}
|
|
42
|
+
export interface AIAgent {
|
|
43
|
+
id?: string;
|
|
44
|
+
agent_name: string;
|
|
45
|
+
description?: string;
|
|
46
|
+
system_prompt?: string;
|
|
47
|
+
provider_id?: string;
|
|
48
|
+
model_id?: string;
|
|
49
|
+
temperature?: number;
|
|
50
|
+
memory_enabled?: boolean;
|
|
51
|
+
tool_permissions?: any;
|
|
52
|
+
visibility?: string;
|
|
53
|
+
rate_limit?: number;
|
|
54
|
+
status?: string;
|
|
55
|
+
}
|
|
56
|
+
export declare function aiChat(client: SupersoClient, options: AIChatOptions): Promise<AIChatResponse>;
|
|
57
|
+
export declare function aiStreamChat(client: SupersoClient, options: AIChatOptions, onChunk: (chunk: string) => void): Promise<void>;
|
|
58
|
+
export declare function getAIProviders(client: SupersoClient, token: string): Promise<any>;
|
|
59
|
+
export declare function createAIProvider(client: SupersoClient, data: AIProvider, token: string): Promise<any>;
|
|
60
|
+
export declare function updateAIProvider(client: SupersoClient, providerId: string, data: Partial<AIProvider>, token: string): Promise<any>;
|
|
61
|
+
export declare function deleteAIProvider(client: SupersoClient, providerId: string, token: string): Promise<any>;
|
|
62
|
+
export declare function getAIModels(client: SupersoClient, token: string): Promise<any>;
|
|
63
|
+
export declare function createAIModel(client: SupersoClient, data: AIModel, token: string): Promise<any>;
|
|
64
|
+
export declare function updateAIModel(client: SupersoClient, modelId: string, data: Partial<AIModel>, token: string): Promise<any>;
|
|
65
|
+
export declare function deleteAIModel(client: SupersoClient, modelId: string, token: string): Promise<any>;
|
|
66
|
+
export declare function getAIAgents(client: SupersoClient, token: string): Promise<any>;
|
|
67
|
+
export declare function createAIAgent(client: SupersoClient, data: AIAgent, token: string): Promise<any>;
|
|
68
|
+
export declare function updateAIAgent(client: SupersoClient, agentId: string, data: Partial<AIAgent>, token: string): Promise<any>;
|
|
69
|
+
export declare function deleteAIAgent(client: SupersoClient, agentId: string, token: string): Promise<any>;
|
|
70
|
+
export declare function getAISettings(client: SupersoClient, token: string): Promise<any>;
|
|
71
|
+
export declare function updateAISettings(client: SupersoClient, data: any, token: string): Promise<any>;
|
|
72
|
+
export declare function estimateTokens(text: string): number;
|
|
73
|
+
export declare function estimateCost(inputTokens: number, outputTokens: number, inputCost: number, outputCost: number): number;
|
|
74
|
+
export declare function supportsVision(model: AIModel): boolean;
|
|
75
|
+
export declare function supportsTools(model: AIModel): boolean;
|
|
76
|
+
export declare function supportsStreaming(model: AIModel): boolean;
|
package/dist/ai/ai.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// ======================================
|
|
2
|
+
// REQUEST
|
|
3
|
+
// ======================================
|
|
4
|
+
// ======================================
|
|
5
|
+
// REQUEST
|
|
6
|
+
// ======================================
|
|
7
|
+
async function request(client, endpoint, method = "GET", body, token) {
|
|
8
|
+
const headers = {
|
|
9
|
+
"Content-Type": "application/json",
|
|
10
|
+
"X-Superso-Project-Key": client.apiKey,
|
|
11
|
+
};
|
|
12
|
+
if (token) {
|
|
13
|
+
headers.Authorization =
|
|
14
|
+
`Bearer ${token}`;
|
|
15
|
+
}
|
|
16
|
+
const response = await fetch(`${client.baseUrl}/api/project/${client.projectId}${endpoint}`, {
|
|
17
|
+
method,
|
|
18
|
+
headers,
|
|
19
|
+
body: body
|
|
20
|
+
? JSON.stringify(body)
|
|
21
|
+
: undefined,
|
|
22
|
+
});
|
|
23
|
+
const data = await response.json();
|
|
24
|
+
if (!response.ok) {
|
|
25
|
+
throw new Error((data === null || data === void 0 ? void 0 : data.message) ||
|
|
26
|
+
"AI request failed");
|
|
27
|
+
}
|
|
28
|
+
return data;
|
|
29
|
+
}
|
|
30
|
+
// ======================================
|
|
31
|
+
// CHAT
|
|
32
|
+
// ======================================
|
|
33
|
+
export async function aiChat(client, options) {
|
|
34
|
+
return request(client, "/ai/chat", "POST", options);
|
|
35
|
+
}
|
|
36
|
+
// ======================================
|
|
37
|
+
// STREAM CHAT
|
|
38
|
+
// ======================================
|
|
39
|
+
export async function aiStreamChat(client, options, onChunk) {
|
|
40
|
+
const response = await fetch(`${client.baseUrl}/api/project/${client.projectId}/ai/chat`, {
|
|
41
|
+
method: "POST",
|
|
42
|
+
headers: {
|
|
43
|
+
"Content-Type": "application/json",
|
|
44
|
+
"X-Superso-Project-Key": client.apiKey,
|
|
45
|
+
},
|
|
46
|
+
body: JSON.stringify(Object.assign(Object.assign({}, options), { streaming: true })),
|
|
47
|
+
});
|
|
48
|
+
if (!response.body) {
|
|
49
|
+
throw new Error("Streaming not supported");
|
|
50
|
+
}
|
|
51
|
+
const reader = response.body.getReader();
|
|
52
|
+
const decoder = new TextDecoder();
|
|
53
|
+
while (true) {
|
|
54
|
+
const { done, value } = await reader.read();
|
|
55
|
+
if (done)
|
|
56
|
+
break;
|
|
57
|
+
const chunk = decoder.decode(value);
|
|
58
|
+
onChunk(chunk);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
// ======================================
|
|
62
|
+
// PROVIDERS
|
|
63
|
+
// ======================================
|
|
64
|
+
export async function getAIProviders(client, token) {
|
|
65
|
+
return request(client, "/ai/providers", "GET", undefined, token);
|
|
66
|
+
}
|
|
67
|
+
export async function createAIProvider(client, data, token) {
|
|
68
|
+
return request(client, "/ai/providers", "POST", data, token);
|
|
69
|
+
}
|
|
70
|
+
export async function updateAIProvider(client, providerId, data, token) {
|
|
71
|
+
return request(client, `/ai/providers/${providerId}`, "PUT", data, token);
|
|
72
|
+
}
|
|
73
|
+
export async function deleteAIProvider(client, providerId, token) {
|
|
74
|
+
return request(client, `/ai/providers/${providerId}`, "DELETE", undefined, token);
|
|
75
|
+
}
|
|
76
|
+
// ======================================
|
|
77
|
+
// MODELS
|
|
78
|
+
// ======================================
|
|
79
|
+
export async function getAIModels(client, token) {
|
|
80
|
+
return request(client, "/ai/models", "GET", undefined, token);
|
|
81
|
+
}
|
|
82
|
+
export async function createAIModel(client, data, token) {
|
|
83
|
+
return request(client, "/ai/models", "POST", data, token);
|
|
84
|
+
}
|
|
85
|
+
export async function updateAIModel(client, modelId, data, token) {
|
|
86
|
+
return request(client, `/ai/models/${modelId}`, "PUT", data, token);
|
|
87
|
+
}
|
|
88
|
+
export async function deleteAIModel(client, modelId, token) {
|
|
89
|
+
return request(client, `/ai/models/${modelId}`, "DELETE", undefined, token);
|
|
90
|
+
}
|
|
91
|
+
// ======================================
|
|
92
|
+
// AGENTS
|
|
93
|
+
// ======================================
|
|
94
|
+
export async function getAIAgents(client, token) {
|
|
95
|
+
return request(client, "/ai/agents", "GET", undefined, token);
|
|
96
|
+
}
|
|
97
|
+
export async function createAIAgent(client, data, token) {
|
|
98
|
+
return request(client, "/ai/agents", "POST", data, token);
|
|
99
|
+
}
|
|
100
|
+
export async function updateAIAgent(client, agentId, data, token) {
|
|
101
|
+
return request(client, `/ai/agents/${agentId}`, "PUT", data, token);
|
|
102
|
+
}
|
|
103
|
+
export async function deleteAIAgent(client, agentId, token) {
|
|
104
|
+
return request(client, `/ai/agents/${agentId}`, "DELETE", undefined, token);
|
|
105
|
+
}
|
|
106
|
+
// ======================================
|
|
107
|
+
// SETTINGS
|
|
108
|
+
// ======================================
|
|
109
|
+
export async function getAISettings(client, token) {
|
|
110
|
+
return request(client, "/ai/settings", "GET", undefined, token);
|
|
111
|
+
}
|
|
112
|
+
export async function updateAISettings(client, data, token) {
|
|
113
|
+
return request(client, "/ai/settings", "PUT", data, token);
|
|
114
|
+
}
|
|
115
|
+
// ======================================
|
|
116
|
+
// HELPERS
|
|
117
|
+
// ======================================
|
|
118
|
+
export function estimateTokens(text) {
|
|
119
|
+
return Math.ceil(text.length / 4);
|
|
120
|
+
}
|
|
121
|
+
export function estimateCost(inputTokens, outputTokens, inputCost, outputCost) {
|
|
122
|
+
return ((inputTokens / 1000000) * inputCost +
|
|
123
|
+
(outputTokens / 1000000) * outputCost);
|
|
124
|
+
}
|
|
125
|
+
export function supportsVision(model) {
|
|
126
|
+
return !!model.supports_vision;
|
|
127
|
+
}
|
|
128
|
+
export function supportsTools(model) {
|
|
129
|
+
return !!model.supports_tools;
|
|
130
|
+
}
|
|
131
|
+
export function supportsStreaming(model) {
|
|
132
|
+
return !!model.supports_streaming;
|
|
133
|
+
}
|
package/dist/ai/cache.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const PREFIX = "superso_ai_";
|
|
2
|
+
// ======================================
|
|
3
|
+
// CACHE
|
|
4
|
+
// ======================================
|
|
5
|
+
export function setAICache(key, value) {
|
|
6
|
+
localStorage.setItem(PREFIX + key, JSON.stringify(value));
|
|
7
|
+
}
|
|
8
|
+
export function getAICache(key) {
|
|
9
|
+
const raw = localStorage.getItem(PREFIX + key);
|
|
10
|
+
if (!raw)
|
|
11
|
+
return null;
|
|
12
|
+
try {
|
|
13
|
+
return JSON.parse(raw);
|
|
14
|
+
}
|
|
15
|
+
catch (_a) {
|
|
16
|
+
return raw;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export function removeAICache(key) {
|
|
20
|
+
localStorage.removeItem(PREFIX + key);
|
|
21
|
+
}
|
|
22
|
+
export function clearAICache() {
|
|
23
|
+
Object.keys(localStorage)
|
|
24
|
+
.forEach(key => {
|
|
25
|
+
if (key.startsWith(PREFIX)) {
|
|
26
|
+
localStorage.removeItem(key);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const listeners = {};
|
|
2
|
+
export function onAIEvent(event, callback) {
|
|
3
|
+
if (!listeners[event]) {
|
|
4
|
+
listeners[event] = [];
|
|
5
|
+
}
|
|
6
|
+
listeners[event]
|
|
7
|
+
.push(callback);
|
|
8
|
+
}
|
|
9
|
+
export function emitAIEvent(event, payload) {
|
|
10
|
+
if (!listeners[event])
|
|
11
|
+
return;
|
|
12
|
+
listeners[event]
|
|
13
|
+
.forEach(callback => callback(payload));
|
|
14
|
+
}
|
|
15
|
+
export function removeAIEvent(event) {
|
|
16
|
+
delete listeners[event];
|
|
17
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// ======================================
|
|
2
|
+
// RATE LIMIT
|
|
3
|
+
// ======================================
|
|
4
|
+
const requests = {};
|
|
5
|
+
export function rateLimit(key, limit = 60, windowMs = 60000) {
|
|
6
|
+
const now = Date.now();
|
|
7
|
+
if (!requests[key]) {
|
|
8
|
+
requests[key] = [];
|
|
9
|
+
}
|
|
10
|
+
requests[key] =
|
|
11
|
+
requests[key]
|
|
12
|
+
.filter(timestamp => now - timestamp <
|
|
13
|
+
windowMs);
|
|
14
|
+
if (requests[key]
|
|
15
|
+
.length >= limit) {
|
|
16
|
+
throw new Error("Rate limit exceeded");
|
|
17
|
+
}
|
|
18
|
+
requests[key]
|
|
19
|
+
.push(now);
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
// ======================================
|
|
23
|
+
// REQUIRE AUTH
|
|
24
|
+
// ======================================
|
|
25
|
+
export function requireAuth(token) {
|
|
26
|
+
if (!token) {
|
|
27
|
+
throw new Error("Authentication required");
|
|
28
|
+
}
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
// ======================================
|
|
32
|
+
// REQUIRE ROLE
|
|
33
|
+
// ======================================
|
|
34
|
+
export function requireRole(userRole, allowed) {
|
|
35
|
+
if (!allowed.includes(userRole)) {
|
|
36
|
+
throw new Error("Permission denied");
|
|
37
|
+
}
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { SupersoClient } from "../core/client";
|
|
2
|
+
type AuthListener = (payload: any) => void;
|
|
3
|
+
export declare function clearSession(): void;
|
|
4
|
+
export declare function getAccessToken(): string | null;
|
|
5
|
+
export declare function getRefreshToken(): string | null;
|
|
6
|
+
export declare function getStoredUser(): any;
|
|
7
|
+
export declare function onAuthChange(callback: AuthListener): () => void;
|
|
8
|
+
export declare function register(client: SupersoClient, payload: {
|
|
9
|
+
display_name?: string;
|
|
10
|
+
email: string;
|
|
11
|
+
password: string;
|
|
12
|
+
}): Promise<any>;
|
|
13
|
+
export declare function login(client: SupersoClient, payload: {
|
|
14
|
+
email: string;
|
|
15
|
+
password: string;
|
|
16
|
+
}): Promise<any>;
|
|
17
|
+
export declare function logout(client: SupersoClient): Promise<void>;
|
|
18
|
+
export declare function refreshToken(client: SupersoClient): Promise<any>;
|
|
19
|
+
export declare function restoreSession(client: SupersoClient): Promise<any>;
|
|
20
|
+
export declare function getCurrentUser(client: SupersoClient): Promise<any>;
|
|
21
|
+
export declare function updateProfile(client: SupersoClient, data: {
|
|
22
|
+
display_name?: string;
|
|
23
|
+
phone?: string;
|
|
24
|
+
bio?: string;
|
|
25
|
+
avatar_url?: string;
|
|
26
|
+
avatar_asset_id?: string;
|
|
27
|
+
}): Promise<any>;
|
|
28
|
+
export declare function changePassword(client: SupersoClient, data: {
|
|
29
|
+
current_password: string;
|
|
30
|
+
new_password: string;
|
|
31
|
+
}): Promise<any>;
|
|
32
|
+
export declare function changeEmail(client: SupersoClient, data: {
|
|
33
|
+
new_email: string;
|
|
34
|
+
password: string;
|
|
35
|
+
}): Promise<any>;
|
|
36
|
+
export declare function sendOtpLogin(client: SupersoClient, payload: {
|
|
37
|
+
email?: string;
|
|
38
|
+
phone?: string;
|
|
39
|
+
}): Promise<any>;
|
|
40
|
+
export declare function verifyEmail(client: SupersoClient, payload: {
|
|
41
|
+
email: string;
|
|
42
|
+
otp?: string;
|
|
43
|
+
}): Promise<any>;
|
|
44
|
+
export declare function verifyPhone(client: SupersoClient, payload: {
|
|
45
|
+
phone: string;
|
|
46
|
+
otp?: string;
|
|
47
|
+
}): Promise<any>;
|
|
48
|
+
export declare function sendPasswordReset(client: SupersoClient, payload: {
|
|
49
|
+
email: string;
|
|
50
|
+
}): Promise<any>;
|
|
51
|
+
export declare function confirmPasswordReset(client: SupersoClient, payload: {
|
|
52
|
+
email: string;
|
|
53
|
+
otp: string;
|
|
54
|
+
new_password: string;
|
|
55
|
+
}): Promise<any>;
|
|
56
|
+
export declare function sendMagicLink(client: SupersoClient, payload: {
|
|
57
|
+
email: string;
|
|
58
|
+
}): Promise<any>;
|
|
59
|
+
export declare function verifyMagicLink(client: SupersoClient, payload: {
|
|
60
|
+
email: string;
|
|
61
|
+
token: string;
|
|
62
|
+
}): Promise<any>;
|
|
63
|
+
export declare function getSessions(client: SupersoClient): Promise<any>;
|
|
64
|
+
export declare function revokeSession(client: SupersoClient, sessionId: string): Promise<any>;
|
|
65
|
+
export declare function revokeOtherSessions(client: SupersoClient): Promise<any>;
|
|
66
|
+
export {};
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
// ================================
|
|
2
|
+
// CONSTANTS
|
|
3
|
+
// ================================
|
|
4
|
+
const ACCESS_TOKEN_KEY = "superso_access_token";
|
|
5
|
+
const REFRESH_TOKEN_KEY = "superso_refresh_token";
|
|
6
|
+
const USER_KEY = "superso_auth_user";
|
|
7
|
+
const listeners = new Set();
|
|
8
|
+
function emitAuth(payload) {
|
|
9
|
+
listeners.forEach((callback) => callback(payload));
|
|
10
|
+
}
|
|
11
|
+
// ================================
|
|
12
|
+
// JWT HELPERS
|
|
13
|
+
// ================================
|
|
14
|
+
function parseJwt(token) {
|
|
15
|
+
try {
|
|
16
|
+
return JSON.parse(atob(token.split(".")[1]));
|
|
17
|
+
}
|
|
18
|
+
catch (_a) {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function isTokenExpired(token) {
|
|
23
|
+
const decoded = parseJwt(token);
|
|
24
|
+
if (!(decoded === null || decoded === void 0 ? void 0 : decoded.exp))
|
|
25
|
+
return true;
|
|
26
|
+
return (Date.now() >=
|
|
27
|
+
decoded.exp *
|
|
28
|
+
1000 -
|
|
29
|
+
5 * 60 * 1000);
|
|
30
|
+
}
|
|
31
|
+
// ================================
|
|
32
|
+
// SESSION HELPERS
|
|
33
|
+
// ================================
|
|
34
|
+
function saveSession(data) {
|
|
35
|
+
const token = data === null || data === void 0 ? void 0 : data.token;
|
|
36
|
+
const refreshToken = data === null || data === void 0 ? void 0 : data.refresh_token;
|
|
37
|
+
const user = data === null || data === void 0 ? void 0 : data.user;
|
|
38
|
+
if (token) {
|
|
39
|
+
localStorage.setItem(ACCESS_TOKEN_KEY, token);
|
|
40
|
+
}
|
|
41
|
+
if (refreshToken) {
|
|
42
|
+
localStorage.setItem(REFRESH_TOKEN_KEY, refreshToken);
|
|
43
|
+
}
|
|
44
|
+
if (user) {
|
|
45
|
+
localStorage.setItem(USER_KEY, JSON.stringify(user));
|
|
46
|
+
}
|
|
47
|
+
emitAuth({
|
|
48
|
+
user,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
export function clearSession() {
|
|
52
|
+
localStorage.removeItem(ACCESS_TOKEN_KEY);
|
|
53
|
+
localStorage.removeItem(REFRESH_TOKEN_KEY);
|
|
54
|
+
localStorage.removeItem(USER_KEY);
|
|
55
|
+
emitAuth({
|
|
56
|
+
user: null,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
export function getAccessToken() {
|
|
60
|
+
return localStorage.getItem(ACCESS_TOKEN_KEY);
|
|
61
|
+
}
|
|
62
|
+
export function getRefreshToken() {
|
|
63
|
+
return localStorage.getItem(REFRESH_TOKEN_KEY);
|
|
64
|
+
}
|
|
65
|
+
export function getStoredUser() {
|
|
66
|
+
const raw = localStorage.getItem(USER_KEY);
|
|
67
|
+
if (!raw)
|
|
68
|
+
return null;
|
|
69
|
+
try {
|
|
70
|
+
return JSON.parse(raw);
|
|
71
|
+
}
|
|
72
|
+
catch (_a) {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
// ================================
|
|
77
|
+
// CORE REQUEST
|
|
78
|
+
// ================================
|
|
79
|
+
async function request(client, endpoint, method = "GET", body = null, auth = false) {
|
|
80
|
+
let token = getAccessToken();
|
|
81
|
+
// auto refresh
|
|
82
|
+
if (auth &&
|
|
83
|
+
token &&
|
|
84
|
+
isTokenExpired(token)) {
|
|
85
|
+
try {
|
|
86
|
+
const refreshed = await refreshToken(client);
|
|
87
|
+
token =
|
|
88
|
+
refreshed === null || refreshed === void 0 ? void 0 : refreshed.token;
|
|
89
|
+
}
|
|
90
|
+
catch (_a) {
|
|
91
|
+
clearSession();
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const headers = {
|
|
95
|
+
"Content-Type": "application/json",
|
|
96
|
+
"X-Superso-Project-Key": client.apiKey,
|
|
97
|
+
};
|
|
98
|
+
if (auth && token) {
|
|
99
|
+
headers["Authorization"] =
|
|
100
|
+
`Bearer ${token}`;
|
|
101
|
+
}
|
|
102
|
+
const response = await fetch(`${client.baseUrl}/api/project/${client.projectId}${endpoint}`, {
|
|
103
|
+
method,
|
|
104
|
+
headers,
|
|
105
|
+
body: body
|
|
106
|
+
? JSON.stringify(body)
|
|
107
|
+
: undefined,
|
|
108
|
+
});
|
|
109
|
+
let data = {};
|
|
110
|
+
try {
|
|
111
|
+
data =
|
|
112
|
+
await response.json();
|
|
113
|
+
}
|
|
114
|
+
catch (_b) {
|
|
115
|
+
data = {};
|
|
116
|
+
}
|
|
117
|
+
if (!response.ok) {
|
|
118
|
+
throw new Error((data === null || data === void 0 ? void 0 : data.message) ||
|
|
119
|
+
"Request failed");
|
|
120
|
+
}
|
|
121
|
+
return data;
|
|
122
|
+
}
|
|
123
|
+
// ================================
|
|
124
|
+
// AUTH STATE
|
|
125
|
+
// ================================
|
|
126
|
+
export function onAuthChange(callback) {
|
|
127
|
+
listeners.add(callback);
|
|
128
|
+
return () => {
|
|
129
|
+
listeners.delete(callback);
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
// ================================
|
|
133
|
+
// MULTI TAB AUTH SYNC
|
|
134
|
+
// ================================
|
|
135
|
+
window.addEventListener("storage", (event) => {
|
|
136
|
+
if (event.key ===
|
|
137
|
+
ACCESS_TOKEN_KEY) {
|
|
138
|
+
emitAuth({
|
|
139
|
+
user: getStoredUser(),
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
// ================================
|
|
144
|
+
// REGISTER
|
|
145
|
+
// ================================
|
|
146
|
+
export async function register(client, payload) {
|
|
147
|
+
const res = await request(client, "/auth/register", "POST", payload);
|
|
148
|
+
if (res === null || res === void 0 ? void 0 : res.token) {
|
|
149
|
+
saveSession(res);
|
|
150
|
+
}
|
|
151
|
+
return res;
|
|
152
|
+
}
|
|
153
|
+
// ================================
|
|
154
|
+
// LOGIN
|
|
155
|
+
// ================================
|
|
156
|
+
export async function login(client, payload) {
|
|
157
|
+
const res = await request(client, "/auth/login", "POST", payload);
|
|
158
|
+
saveSession(res);
|
|
159
|
+
return res;
|
|
160
|
+
}
|
|
161
|
+
// ================================
|
|
162
|
+
// LOGOUT
|
|
163
|
+
// ================================
|
|
164
|
+
export async function logout(client) {
|
|
165
|
+
try {
|
|
166
|
+
await request(client, "/auth/logout", "POST", {
|
|
167
|
+
refresh_token: getRefreshToken(),
|
|
168
|
+
}, true);
|
|
169
|
+
}
|
|
170
|
+
catch (_a) { }
|
|
171
|
+
clearSession();
|
|
172
|
+
}
|
|
173
|
+
// ================================
|
|
174
|
+
// REFRESH TOKEN
|
|
175
|
+
// ================================
|
|
176
|
+
export async function refreshToken(client) {
|
|
177
|
+
const refresh_token = getRefreshToken();
|
|
178
|
+
if (!refresh_token) {
|
|
179
|
+
throw new Error("Missing refresh token");
|
|
180
|
+
}
|
|
181
|
+
const res = await request(client, "/auth/refresh-token", "POST", {
|
|
182
|
+
refresh_token,
|
|
183
|
+
});
|
|
184
|
+
saveSession(res);
|
|
185
|
+
emitAuth({
|
|
186
|
+
type: "token_rotated",
|
|
187
|
+
token: res === null || res === void 0 ? void 0 : res.token,
|
|
188
|
+
});
|
|
189
|
+
return res;
|
|
190
|
+
}
|
|
191
|
+
// ================================
|
|
192
|
+
// RESTORE SESSION
|
|
193
|
+
// ================================
|
|
194
|
+
export async function restoreSession(client) {
|
|
195
|
+
const token = getAccessToken();
|
|
196
|
+
if (!token)
|
|
197
|
+
return null;
|
|
198
|
+
try {
|
|
199
|
+
if (isTokenExpired(token)) {
|
|
200
|
+
await refreshToken(client);
|
|
201
|
+
}
|
|
202
|
+
const me = await getCurrentUser(client);
|
|
203
|
+
emitAuth({
|
|
204
|
+
user: (me === null || me === void 0 ? void 0 : me.user) ||
|
|
205
|
+
(me === null || me === void 0 ? void 0 : me.data),
|
|
206
|
+
});
|
|
207
|
+
return me;
|
|
208
|
+
}
|
|
209
|
+
catch (_a) {
|
|
210
|
+
clearSession();
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
// ================================
|
|
215
|
+
// CURRENT USER
|
|
216
|
+
// ================================
|
|
217
|
+
export async function getCurrentUser(client) {
|
|
218
|
+
return await request(client, "/auth/user/me", "GET", null, true);
|
|
219
|
+
}
|
|
220
|
+
// ================================
|
|
221
|
+
// UPDATE PROFILE
|
|
222
|
+
// ================================
|
|
223
|
+
export async function updateProfile(client, data) {
|
|
224
|
+
return await request(client, "/auth/user/profile", "PUT", data, true);
|
|
225
|
+
}
|
|
226
|
+
// ================================
|
|
227
|
+
// CHANGE PASSWORD
|
|
228
|
+
// ================================
|
|
229
|
+
export async function changePassword(client, data) {
|
|
230
|
+
return await request(client, "/auth/user/change-password", "POST", data, true);
|
|
231
|
+
}
|
|
232
|
+
// ================================
|
|
233
|
+
// CHANGE EMAIL
|
|
234
|
+
// ================================
|
|
235
|
+
export async function changeEmail(client, data) {
|
|
236
|
+
return await request(client, "/auth/user/change-email", "POST", data, true);
|
|
237
|
+
}
|
|
238
|
+
// ================================
|
|
239
|
+
// OTP LOGIN
|
|
240
|
+
// ================================
|
|
241
|
+
export async function sendOtpLogin(client, payload) {
|
|
242
|
+
return await request(client, "/auth/otp-login", "POST", payload);
|
|
243
|
+
}
|
|
244
|
+
// ================================
|
|
245
|
+
// VERIFY EMAIL
|
|
246
|
+
// ================================
|
|
247
|
+
export async function verifyEmail(client, payload) {
|
|
248
|
+
return await request(client, "/auth/verify-email", "POST", payload);
|
|
249
|
+
}
|
|
250
|
+
// ================================
|
|
251
|
+
// VERIFY PHONE
|
|
252
|
+
// ================================
|
|
253
|
+
export async function verifyPhone(client, payload) {
|
|
254
|
+
return await request(client, "/auth/verify-phone", "POST", payload);
|
|
255
|
+
}
|
|
256
|
+
// ================================
|
|
257
|
+
// PASSWORD RESET
|
|
258
|
+
// ================================
|
|
259
|
+
export async function sendPasswordReset(client, payload) {
|
|
260
|
+
return await request(client, "/auth/send-reset", "POST", payload);
|
|
261
|
+
}
|
|
262
|
+
export async function confirmPasswordReset(client, payload) {
|
|
263
|
+
return await request(client, "/auth/confirm-reset", "POST", payload);
|
|
264
|
+
}
|
|
265
|
+
// ================================
|
|
266
|
+
// MAGIC LINK
|
|
267
|
+
// ================================
|
|
268
|
+
export async function sendMagicLink(client, payload) {
|
|
269
|
+
return await request(client, "/auth/magic-link/send", "POST", payload);
|
|
270
|
+
}
|
|
271
|
+
export async function verifyMagicLink(client, payload) {
|
|
272
|
+
return await request(client, "/auth/magic-link/verify", "POST", payload);
|
|
273
|
+
}
|
|
274
|
+
// ================================
|
|
275
|
+
// SESSIONS
|
|
276
|
+
// ================================
|
|
277
|
+
export async function getSessions(client) {
|
|
278
|
+
return await request(client, "/auth/user/sessions", "GET", null, true);
|
|
279
|
+
}
|
|
280
|
+
export async function revokeSession(client, sessionId) {
|
|
281
|
+
return await request(client, `/auth/user/sessions/${sessionId}`, "DELETE", null, true);
|
|
282
|
+
}
|
|
283
|
+
export async function revokeOtherSessions(client) {
|
|
284
|
+
return await request(client, "/auth/user/sessions", "DELETE", null, true);
|
|
285
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface SupersoClientConfig {
|
|
2
|
+
projectId: string;
|
|
3
|
+
apiKey: string;
|
|
4
|
+
baseUrl?: string;
|
|
5
|
+
}
|
|
6
|
+
export declare class SupersoClient {
|
|
7
|
+
projectId: string;
|
|
8
|
+
apiKey: string;
|
|
9
|
+
baseUrl: string;
|
|
10
|
+
constructor(config: SupersoClientConfig);
|
|
11
|
+
}
|
|
12
|
+
export declare function createClient(config: SupersoClientConfig): SupersoClient;
|