@insforge/sdk 0.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.
@@ -0,0 +1,230 @@
1
+ import { UserSchema, CreateUserRequest, CreateSessionRequest, GetCurrentSessionResponse } from '@insforge/shared-schemas';
2
+ export { AuthErrorResponse, CreateSessionRequest, CreateUserRequest, UserSchema } from '@insforge/shared-schemas';
3
+
4
+ /**
5
+ * InsForge SDK Types - only SDK-specific types here
6
+ * Use @insforge/shared-schemas directly for API types
7
+ */
8
+
9
+ interface InsForgeConfig {
10
+ /**
11
+ * The URL of the InsForge backend API
12
+ * @default "http://localhost:7130"
13
+ */
14
+ url?: string;
15
+ /**
16
+ * API key (optional)
17
+ * Can be used for server-side operations or specific use cases
18
+ */
19
+ apiKey?: string;
20
+ /**
21
+ * Custom fetch implementation (useful for Node.js environments)
22
+ */
23
+ fetch?: typeof fetch;
24
+ /**
25
+ * Storage adapter for persisting tokens
26
+ */
27
+ storage?: TokenStorage;
28
+ /**
29
+ * Whether to automatically refresh tokens before they expire
30
+ * @default true
31
+ */
32
+ autoRefreshToken?: boolean;
33
+ /**
34
+ * Whether to persist session in storage
35
+ * @default true
36
+ */
37
+ persistSession?: boolean;
38
+ /**
39
+ * Custom headers to include with every request
40
+ */
41
+ headers?: Record<string, string>;
42
+ }
43
+ interface TokenStorage {
44
+ getItem(key: string): string | null | Promise<string | null>;
45
+ setItem(key: string, value: string): void | Promise<void>;
46
+ removeItem(key: string): void | Promise<void>;
47
+ }
48
+ interface AuthSession {
49
+ user: UserSchema;
50
+ accessToken: string;
51
+ expiresAt?: Date;
52
+ }
53
+ interface ApiError {
54
+ error: string;
55
+ message: string;
56
+ statusCode: number;
57
+ nextActions?: string;
58
+ }
59
+ declare class InsForgeError extends Error {
60
+ statusCode: number;
61
+ error: string;
62
+ nextActions?: string;
63
+ constructor(message: string, statusCode: number, error: string, nextActions?: string);
64
+ static fromApiError(apiError: ApiError): InsForgeError;
65
+ }
66
+
67
+ interface RequestOptions extends RequestInit {
68
+ params?: Record<string, string>;
69
+ }
70
+ declare class HttpClient {
71
+ private baseUrl;
72
+ private fetch;
73
+ private defaultHeaders;
74
+ constructor(config: InsForgeConfig);
75
+ private buildUrl;
76
+ request<T>(method: string, path: string, options?: RequestOptions): Promise<T>;
77
+ get<T>(path: string, options?: RequestOptions): Promise<T>;
78
+ post<T>(path: string, body?: any, options?: RequestOptions): Promise<T>;
79
+ put<T>(path: string, body?: any, options?: RequestOptions): Promise<T>;
80
+ patch<T>(path: string, body?: any, options?: RequestOptions): Promise<T>;
81
+ delete<T>(path: string, options?: RequestOptions): Promise<T>;
82
+ setAuthToken(token: string | null): void;
83
+ }
84
+
85
+ declare class TokenManager {
86
+ private storage;
87
+ constructor(storage?: TokenStorage);
88
+ saveSession(session: AuthSession): void;
89
+ getSession(): AuthSession | null;
90
+ getAccessToken(): string | null;
91
+ clearSession(): void;
92
+ }
93
+
94
+ /**
95
+ * Auth module for InsForge SDK
96
+ * Uses shared schemas for type safety
97
+ */
98
+
99
+ declare class Auth {
100
+ private http;
101
+ private tokenManager;
102
+ constructor(http: HttpClient, tokenManager: TokenManager);
103
+ /**
104
+ * Sign up a new user
105
+ */
106
+ signUp(request: CreateUserRequest): Promise<{
107
+ data: {
108
+ user: UserSchema | null;
109
+ session: AuthSession | null;
110
+ };
111
+ error: InsForgeError | null;
112
+ }>;
113
+ /**
114
+ * Sign in with email and password
115
+ */
116
+ signInWithPassword(request: CreateSessionRequest): Promise<{
117
+ data: {
118
+ user: UserSchema | null;
119
+ session: AuthSession | null;
120
+ };
121
+ error: InsForgeError | null;
122
+ }>;
123
+ /**
124
+ * Sign in with OAuth provider
125
+ */
126
+ signInWithOAuth(options: {
127
+ provider: 'google' | 'github';
128
+ redirectTo?: string;
129
+ skipBrowserRedirect?: boolean;
130
+ }): Promise<{
131
+ data: {
132
+ url?: string;
133
+ provider?: string;
134
+ };
135
+ error: InsForgeError | null;
136
+ }>;
137
+ /**
138
+ * Sign out the current user
139
+ */
140
+ signOut(): Promise<{
141
+ error: InsForgeError | null;
142
+ }>;
143
+ /**
144
+ * Get the current user from the API
145
+ * Returns exactly what the backend returns: {id, email, role}
146
+ */
147
+ getCurrentUser(): Promise<{
148
+ data: GetCurrentSessionResponse | null;
149
+ error: InsForgeError | null;
150
+ }>;
151
+ /**
152
+ * Get the stored session (no API call)
153
+ */
154
+ getSession(): Promise<{
155
+ data: {
156
+ session: AuthSession | null;
157
+ };
158
+ error: InsForgeError | null;
159
+ }>;
160
+ }
161
+
162
+ /**
163
+ * Main InsForge SDK Client
164
+ *
165
+ * @example
166
+ * ```typescript
167
+ * import { InsForgeClient } from '@insforge/sdk';
168
+ *
169
+ * const client = new InsForgeClient({
170
+ * baseUrl: 'http://localhost:7130'
171
+ * });
172
+ *
173
+ * // Register a new user
174
+ * const session = await client.auth.register({
175
+ * email: 'user@example.com',
176
+ * password: 'password123',
177
+ * name: 'John Doe'
178
+ * });
179
+ *
180
+ * // Or login
181
+ * const session = await client.auth.login({
182
+ * email: 'user@example.com',
183
+ * password: 'password123'
184
+ * });
185
+ *
186
+ * // Get current user
187
+ * const user = await client.auth.getCurrentUser();
188
+ * ```
189
+ */
190
+ declare class InsForgeClient {
191
+ private http;
192
+ private tokenManager;
193
+ /**
194
+ * Authentication module
195
+ */
196
+ readonly auth: Auth;
197
+ constructor(config?: InsForgeConfig);
198
+ /**
199
+ * Set a custom API key for authentication
200
+ * This is useful for server-to-server communication
201
+ *
202
+ * @param apiKey - The API key (should start with 'ik_')
203
+ *
204
+ * @example
205
+ * ```typescript
206
+ * client.setApiKey('ik_your_api_key_here');
207
+ * ```
208
+ */
209
+ setApiKey(apiKey: string): void;
210
+ /**
211
+ * Get the underlying HTTP client for custom requests
212
+ *
213
+ * @example
214
+ * ```typescript
215
+ * const httpClient = client.getHttpClient();
216
+ * const customData = await httpClient.get('/api/custom-endpoint');
217
+ * ```
218
+ */
219
+ getHttpClient(): HttpClient;
220
+ }
221
+
222
+ /**
223
+ * @insforge/sdk - TypeScript SDK for InsForge Backend-as-a-Service
224
+ *
225
+ * @packageDocumentation
226
+ */
227
+
228
+ declare function createClient(config: InsForgeConfig): InsForgeClient;
229
+
230
+ export { type ApiError, Auth, type AuthSession, type InsForgeConfig as ClientOptions, HttpClient, InsForgeClient, type InsForgeConfig, InsForgeError, TokenManager, type TokenStorage, createClient, InsForgeClient as default };
@@ -0,0 +1,230 @@
1
+ import { UserSchema, CreateUserRequest, CreateSessionRequest, GetCurrentSessionResponse } from '@insforge/shared-schemas';
2
+ export { AuthErrorResponse, CreateSessionRequest, CreateUserRequest, UserSchema } from '@insforge/shared-schemas';
3
+
4
+ /**
5
+ * InsForge SDK Types - only SDK-specific types here
6
+ * Use @insforge/shared-schemas directly for API types
7
+ */
8
+
9
+ interface InsForgeConfig {
10
+ /**
11
+ * The URL of the InsForge backend API
12
+ * @default "http://localhost:7130"
13
+ */
14
+ url?: string;
15
+ /**
16
+ * API key (optional)
17
+ * Can be used for server-side operations or specific use cases
18
+ */
19
+ apiKey?: string;
20
+ /**
21
+ * Custom fetch implementation (useful for Node.js environments)
22
+ */
23
+ fetch?: typeof fetch;
24
+ /**
25
+ * Storage adapter for persisting tokens
26
+ */
27
+ storage?: TokenStorage;
28
+ /**
29
+ * Whether to automatically refresh tokens before they expire
30
+ * @default true
31
+ */
32
+ autoRefreshToken?: boolean;
33
+ /**
34
+ * Whether to persist session in storage
35
+ * @default true
36
+ */
37
+ persistSession?: boolean;
38
+ /**
39
+ * Custom headers to include with every request
40
+ */
41
+ headers?: Record<string, string>;
42
+ }
43
+ interface TokenStorage {
44
+ getItem(key: string): string | null | Promise<string | null>;
45
+ setItem(key: string, value: string): void | Promise<void>;
46
+ removeItem(key: string): void | Promise<void>;
47
+ }
48
+ interface AuthSession {
49
+ user: UserSchema;
50
+ accessToken: string;
51
+ expiresAt?: Date;
52
+ }
53
+ interface ApiError {
54
+ error: string;
55
+ message: string;
56
+ statusCode: number;
57
+ nextActions?: string;
58
+ }
59
+ declare class InsForgeError extends Error {
60
+ statusCode: number;
61
+ error: string;
62
+ nextActions?: string;
63
+ constructor(message: string, statusCode: number, error: string, nextActions?: string);
64
+ static fromApiError(apiError: ApiError): InsForgeError;
65
+ }
66
+
67
+ interface RequestOptions extends RequestInit {
68
+ params?: Record<string, string>;
69
+ }
70
+ declare class HttpClient {
71
+ private baseUrl;
72
+ private fetch;
73
+ private defaultHeaders;
74
+ constructor(config: InsForgeConfig);
75
+ private buildUrl;
76
+ request<T>(method: string, path: string, options?: RequestOptions): Promise<T>;
77
+ get<T>(path: string, options?: RequestOptions): Promise<T>;
78
+ post<T>(path: string, body?: any, options?: RequestOptions): Promise<T>;
79
+ put<T>(path: string, body?: any, options?: RequestOptions): Promise<T>;
80
+ patch<T>(path: string, body?: any, options?: RequestOptions): Promise<T>;
81
+ delete<T>(path: string, options?: RequestOptions): Promise<T>;
82
+ setAuthToken(token: string | null): void;
83
+ }
84
+
85
+ declare class TokenManager {
86
+ private storage;
87
+ constructor(storage?: TokenStorage);
88
+ saveSession(session: AuthSession): void;
89
+ getSession(): AuthSession | null;
90
+ getAccessToken(): string | null;
91
+ clearSession(): void;
92
+ }
93
+
94
+ /**
95
+ * Auth module for InsForge SDK
96
+ * Uses shared schemas for type safety
97
+ */
98
+
99
+ declare class Auth {
100
+ private http;
101
+ private tokenManager;
102
+ constructor(http: HttpClient, tokenManager: TokenManager);
103
+ /**
104
+ * Sign up a new user
105
+ */
106
+ signUp(request: CreateUserRequest): Promise<{
107
+ data: {
108
+ user: UserSchema | null;
109
+ session: AuthSession | null;
110
+ };
111
+ error: InsForgeError | null;
112
+ }>;
113
+ /**
114
+ * Sign in with email and password
115
+ */
116
+ signInWithPassword(request: CreateSessionRequest): Promise<{
117
+ data: {
118
+ user: UserSchema | null;
119
+ session: AuthSession | null;
120
+ };
121
+ error: InsForgeError | null;
122
+ }>;
123
+ /**
124
+ * Sign in with OAuth provider
125
+ */
126
+ signInWithOAuth(options: {
127
+ provider: 'google' | 'github';
128
+ redirectTo?: string;
129
+ skipBrowserRedirect?: boolean;
130
+ }): Promise<{
131
+ data: {
132
+ url?: string;
133
+ provider?: string;
134
+ };
135
+ error: InsForgeError | null;
136
+ }>;
137
+ /**
138
+ * Sign out the current user
139
+ */
140
+ signOut(): Promise<{
141
+ error: InsForgeError | null;
142
+ }>;
143
+ /**
144
+ * Get the current user from the API
145
+ * Returns exactly what the backend returns: {id, email, role}
146
+ */
147
+ getCurrentUser(): Promise<{
148
+ data: GetCurrentSessionResponse | null;
149
+ error: InsForgeError | null;
150
+ }>;
151
+ /**
152
+ * Get the stored session (no API call)
153
+ */
154
+ getSession(): Promise<{
155
+ data: {
156
+ session: AuthSession | null;
157
+ };
158
+ error: InsForgeError | null;
159
+ }>;
160
+ }
161
+
162
+ /**
163
+ * Main InsForge SDK Client
164
+ *
165
+ * @example
166
+ * ```typescript
167
+ * import { InsForgeClient } from '@insforge/sdk';
168
+ *
169
+ * const client = new InsForgeClient({
170
+ * baseUrl: 'http://localhost:7130'
171
+ * });
172
+ *
173
+ * // Register a new user
174
+ * const session = await client.auth.register({
175
+ * email: 'user@example.com',
176
+ * password: 'password123',
177
+ * name: 'John Doe'
178
+ * });
179
+ *
180
+ * // Or login
181
+ * const session = await client.auth.login({
182
+ * email: 'user@example.com',
183
+ * password: 'password123'
184
+ * });
185
+ *
186
+ * // Get current user
187
+ * const user = await client.auth.getCurrentUser();
188
+ * ```
189
+ */
190
+ declare class InsForgeClient {
191
+ private http;
192
+ private tokenManager;
193
+ /**
194
+ * Authentication module
195
+ */
196
+ readonly auth: Auth;
197
+ constructor(config?: InsForgeConfig);
198
+ /**
199
+ * Set a custom API key for authentication
200
+ * This is useful for server-to-server communication
201
+ *
202
+ * @param apiKey - The API key (should start with 'ik_')
203
+ *
204
+ * @example
205
+ * ```typescript
206
+ * client.setApiKey('ik_your_api_key_here');
207
+ * ```
208
+ */
209
+ setApiKey(apiKey: string): void;
210
+ /**
211
+ * Get the underlying HTTP client for custom requests
212
+ *
213
+ * @example
214
+ * ```typescript
215
+ * const httpClient = client.getHttpClient();
216
+ * const customData = await httpClient.get('/api/custom-endpoint');
217
+ * ```
218
+ */
219
+ getHttpClient(): HttpClient;
220
+ }
221
+
222
+ /**
223
+ * @insforge/sdk - TypeScript SDK for InsForge Backend-as-a-Service
224
+ *
225
+ * @packageDocumentation
226
+ */
227
+
228
+ declare function createClient(config: InsForgeConfig): InsForgeClient;
229
+
230
+ export { type ApiError, Auth, type AuthSession, type InsForgeConfig as ClientOptions, HttpClient, InsForgeClient, type InsForgeConfig, InsForgeError, TokenManager, type TokenStorage, createClient, InsForgeClient as default };