@safercity/sdk-core 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,243 @@
1
+ /**
2
+ * Common types for SaferCity SDK
3
+ */
4
+ interface SaferCityConfig {
5
+ /**
6
+ * Base URL for the SaferCity API
7
+ * @example "https://api.safercity.com"
8
+ */
9
+ baseUrl: string;
10
+ /**
11
+ * Authentication token (JWT)
12
+ */
13
+ token?: string;
14
+ /**
15
+ * Tenant ID for multi-tenant operations
16
+ */
17
+ tenantId?: string;
18
+ /**
19
+ * Custom fetch implementation (useful for React Native)
20
+ */
21
+ fetch?: typeof fetch;
22
+ /**
23
+ * Request timeout in milliseconds
24
+ * @default 30000
25
+ */
26
+ timeout?: number;
27
+ /**
28
+ * Custom headers to include in all requests
29
+ */
30
+ headers?: Record<string, string>;
31
+ }
32
+ interface RequestOptions {
33
+ /**
34
+ * Additional headers for this request
35
+ */
36
+ headers?: Record<string, string>;
37
+ /**
38
+ * Request timeout override
39
+ */
40
+ timeout?: number;
41
+ /**
42
+ * Abort signal for cancellation
43
+ */
44
+ signal?: AbortSignal;
45
+ }
46
+ interface ApiResponse<T> {
47
+ data: T;
48
+ status: number;
49
+ headers: Headers;
50
+ }
51
+ interface ApiError {
52
+ error: string;
53
+ message: string;
54
+ status: number;
55
+ details?: unknown;
56
+ }
57
+ declare class SaferCityApiError extends Error {
58
+ readonly error: string;
59
+ readonly status: number;
60
+ readonly details?: unknown | undefined;
61
+ constructor(error: string, message: string, status: number, details?: unknown | undefined);
62
+ static fromResponse(response: Response, body: unknown): SaferCityApiError;
63
+ }
64
+ /**
65
+ * Server-Sent Event structure
66
+ */
67
+ interface ServerSentEvent {
68
+ id?: string;
69
+ event?: string;
70
+ data: string;
71
+ retry?: number;
72
+ }
73
+ /**
74
+ * Options for SSE connections
75
+ */
76
+ interface EventSourceOptions {
77
+ headers?: Record<string, string>;
78
+ signal?: AbortSignal;
79
+ onOpen?: () => void;
80
+ onError?: (error: Error) => void;
81
+ }
82
+
83
+ /**
84
+ * Authentication utilities for SaferCity SDK
85
+ */
86
+ interface AuthTokens {
87
+ accessToken: string;
88
+ refreshToken?: string;
89
+ expiresAt?: number;
90
+ tokenType: string;
91
+ }
92
+ interface TokenStorage {
93
+ get(): AuthTokens | null;
94
+ set(tokens: AuthTokens): void;
95
+ clear(): void;
96
+ }
97
+ /**
98
+ * In-memory token storage (default)
99
+ */
100
+ declare class MemoryTokenStorage implements TokenStorage {
101
+ private tokens;
102
+ get(): AuthTokens | null;
103
+ set(tokens: AuthTokens): void;
104
+ clear(): void;
105
+ }
106
+ /**
107
+ * Check if a token is expired (with buffer)
108
+ */
109
+ declare function isTokenExpired(expiresAt: number | undefined, bufferMs?: number): boolean;
110
+ /**
111
+ * Create Authorization header value
112
+ */
113
+ declare function createAuthHeader(token: string, type?: string): string;
114
+ /**
115
+ * Parse JWT payload (without verification)
116
+ * Only use for client-side display, not security decisions
117
+ */
118
+ declare function parseJwtPayload<T = Record<string, unknown>>(token: string): T | null;
119
+ /**
120
+ * Extract expiration from JWT
121
+ */
122
+ declare function getJwtExpiration(token: string): number | null;
123
+ interface SaferCityJwtPayload {
124
+ sub?: string;
125
+ tenantId?: string;
126
+ environment?: string;
127
+ scopes?: string[];
128
+ iat?: number;
129
+ exp?: number;
130
+ }
131
+
132
+ /**
133
+ * Cross-platform SSE/Streaming support for SaferCity SDK
134
+ *
135
+ * Provides adapters for:
136
+ * - Web browsers (native EventSource)
137
+ * - React Native (fetch-based streaming via expo-fetch or polyfill)
138
+ * - Node.js (fetch-based streaming)
139
+ */
140
+
141
+ /**
142
+ * Interface for stream adapters
143
+ */
144
+ interface StreamAdapter {
145
+ /**
146
+ * Create an async iterable for SSE events
147
+ */
148
+ createEventSource(url: string, options?: EventSourceOptions): AsyncIterable<ServerSentEvent>;
149
+ /**
150
+ * Check if native SSE is supported
151
+ */
152
+ supportsNativeSSE(): boolean;
153
+ }
154
+ /**
155
+ * Parse SSE data from a text buffer
156
+ */
157
+ declare function parseSSE(buffer: string): {
158
+ parsed: ServerSentEvent[];
159
+ remaining: string;
160
+ };
161
+ /**
162
+ * Web/Browser stream adapter using native EventSource
163
+ */
164
+ declare class WebStreamAdapter implements StreamAdapter {
165
+ supportsNativeSSE(): boolean;
166
+ createEventSource(url: string, options?: EventSourceOptions): AsyncIterable<ServerSentEvent>;
167
+ }
168
+ /**
169
+ * Fetch-based stream adapter for React Native and Node.js
170
+ * Uses ReadableStream to parse SSE from fetch response
171
+ */
172
+ declare class FetchStreamAdapter implements StreamAdapter {
173
+ private fetchFn;
174
+ constructor(fetchFn?: typeof fetch);
175
+ supportsNativeSSE(): boolean;
176
+ createEventSource(url: string, options?: EventSourceOptions): AsyncIterable<ServerSentEvent>;
177
+ }
178
+ /**
179
+ * Auto-detect and create the best stream adapter for the current environment
180
+ */
181
+ declare function createStreamAdapter(fetchFn?: typeof fetch): StreamAdapter;
182
+
183
+ /**
184
+ * Base HTTP client for SaferCity SDK
185
+ */
186
+
187
+ interface BaseClientOptions extends SaferCityConfig {
188
+ }
189
+ /**
190
+ * Base HTTP client with common functionality
191
+ */
192
+ declare class BaseClient {
193
+ protected config: Required<Pick<SaferCityConfig, 'baseUrl' | 'timeout'>> & SaferCityConfig;
194
+ protected fetchFn: typeof fetch;
195
+ constructor(options: BaseClientOptions);
196
+ /**
197
+ * Update authentication token
198
+ */
199
+ setToken(token: string | undefined): void;
200
+ /**
201
+ * Update tenant ID
202
+ */
203
+ setTenantId(tenantId: string | undefined): void;
204
+ /**
205
+ * Get current configuration (read-only)
206
+ */
207
+ getConfig(): Readonly<SaferCityConfig>;
208
+ /**
209
+ * Build full URL from path
210
+ */
211
+ protected buildUrl(path: string, query?: Record<string, string | number | boolean | undefined>): string;
212
+ /**
213
+ * Make HTTP request
214
+ */
215
+ protected request<T>(method: string, path: string, options?: RequestOptions & {
216
+ body?: unknown;
217
+ query?: Record<string, string | number | boolean | undefined>;
218
+ }): Promise<ApiResponse<T>>;
219
+ /**
220
+ * GET request
221
+ */
222
+ get<T>(path: string, options?: RequestOptions & {
223
+ query?: Record<string, string | number | boolean | undefined>;
224
+ }): Promise<ApiResponse<T>>;
225
+ /**
226
+ * POST request
227
+ */
228
+ post<T>(path: string, body?: unknown, options?: RequestOptions): Promise<ApiResponse<T>>;
229
+ /**
230
+ * PUT request
231
+ */
232
+ put<T>(path: string, body?: unknown, options?: RequestOptions): Promise<ApiResponse<T>>;
233
+ /**
234
+ * PATCH request
235
+ */
236
+ patch<T>(path: string, body?: unknown, options?: RequestOptions): Promise<ApiResponse<T>>;
237
+ /**
238
+ * DELETE request
239
+ */
240
+ delete<T>(path: string, options?: RequestOptions): Promise<ApiResponse<T>>;
241
+ }
242
+
243
+ export { type ApiError, type ApiResponse, type AuthTokens, BaseClient, type BaseClientOptions, type EventSourceOptions, FetchStreamAdapter, MemoryTokenStorage, type RequestOptions, SaferCityApiError, type SaferCityConfig, type SaferCityJwtPayload, type ServerSentEvent, type StreamAdapter, type TokenStorage, WebStreamAdapter, createAuthHeader, createStreamAdapter, getJwtExpiration, isTokenExpired, parseJwtPayload, parseSSE };
@@ -0,0 +1,243 @@
1
+ /**
2
+ * Common types for SaferCity SDK
3
+ */
4
+ interface SaferCityConfig {
5
+ /**
6
+ * Base URL for the SaferCity API
7
+ * @example "https://api.safercity.com"
8
+ */
9
+ baseUrl: string;
10
+ /**
11
+ * Authentication token (JWT)
12
+ */
13
+ token?: string;
14
+ /**
15
+ * Tenant ID for multi-tenant operations
16
+ */
17
+ tenantId?: string;
18
+ /**
19
+ * Custom fetch implementation (useful for React Native)
20
+ */
21
+ fetch?: typeof fetch;
22
+ /**
23
+ * Request timeout in milliseconds
24
+ * @default 30000
25
+ */
26
+ timeout?: number;
27
+ /**
28
+ * Custom headers to include in all requests
29
+ */
30
+ headers?: Record<string, string>;
31
+ }
32
+ interface RequestOptions {
33
+ /**
34
+ * Additional headers for this request
35
+ */
36
+ headers?: Record<string, string>;
37
+ /**
38
+ * Request timeout override
39
+ */
40
+ timeout?: number;
41
+ /**
42
+ * Abort signal for cancellation
43
+ */
44
+ signal?: AbortSignal;
45
+ }
46
+ interface ApiResponse<T> {
47
+ data: T;
48
+ status: number;
49
+ headers: Headers;
50
+ }
51
+ interface ApiError {
52
+ error: string;
53
+ message: string;
54
+ status: number;
55
+ details?: unknown;
56
+ }
57
+ declare class SaferCityApiError extends Error {
58
+ readonly error: string;
59
+ readonly status: number;
60
+ readonly details?: unknown | undefined;
61
+ constructor(error: string, message: string, status: number, details?: unknown | undefined);
62
+ static fromResponse(response: Response, body: unknown): SaferCityApiError;
63
+ }
64
+ /**
65
+ * Server-Sent Event structure
66
+ */
67
+ interface ServerSentEvent {
68
+ id?: string;
69
+ event?: string;
70
+ data: string;
71
+ retry?: number;
72
+ }
73
+ /**
74
+ * Options for SSE connections
75
+ */
76
+ interface EventSourceOptions {
77
+ headers?: Record<string, string>;
78
+ signal?: AbortSignal;
79
+ onOpen?: () => void;
80
+ onError?: (error: Error) => void;
81
+ }
82
+
83
+ /**
84
+ * Authentication utilities for SaferCity SDK
85
+ */
86
+ interface AuthTokens {
87
+ accessToken: string;
88
+ refreshToken?: string;
89
+ expiresAt?: number;
90
+ tokenType: string;
91
+ }
92
+ interface TokenStorage {
93
+ get(): AuthTokens | null;
94
+ set(tokens: AuthTokens): void;
95
+ clear(): void;
96
+ }
97
+ /**
98
+ * In-memory token storage (default)
99
+ */
100
+ declare class MemoryTokenStorage implements TokenStorage {
101
+ private tokens;
102
+ get(): AuthTokens | null;
103
+ set(tokens: AuthTokens): void;
104
+ clear(): void;
105
+ }
106
+ /**
107
+ * Check if a token is expired (with buffer)
108
+ */
109
+ declare function isTokenExpired(expiresAt: number | undefined, bufferMs?: number): boolean;
110
+ /**
111
+ * Create Authorization header value
112
+ */
113
+ declare function createAuthHeader(token: string, type?: string): string;
114
+ /**
115
+ * Parse JWT payload (without verification)
116
+ * Only use for client-side display, not security decisions
117
+ */
118
+ declare function parseJwtPayload<T = Record<string, unknown>>(token: string): T | null;
119
+ /**
120
+ * Extract expiration from JWT
121
+ */
122
+ declare function getJwtExpiration(token: string): number | null;
123
+ interface SaferCityJwtPayload {
124
+ sub?: string;
125
+ tenantId?: string;
126
+ environment?: string;
127
+ scopes?: string[];
128
+ iat?: number;
129
+ exp?: number;
130
+ }
131
+
132
+ /**
133
+ * Cross-platform SSE/Streaming support for SaferCity SDK
134
+ *
135
+ * Provides adapters for:
136
+ * - Web browsers (native EventSource)
137
+ * - React Native (fetch-based streaming via expo-fetch or polyfill)
138
+ * - Node.js (fetch-based streaming)
139
+ */
140
+
141
+ /**
142
+ * Interface for stream adapters
143
+ */
144
+ interface StreamAdapter {
145
+ /**
146
+ * Create an async iterable for SSE events
147
+ */
148
+ createEventSource(url: string, options?: EventSourceOptions): AsyncIterable<ServerSentEvent>;
149
+ /**
150
+ * Check if native SSE is supported
151
+ */
152
+ supportsNativeSSE(): boolean;
153
+ }
154
+ /**
155
+ * Parse SSE data from a text buffer
156
+ */
157
+ declare function parseSSE(buffer: string): {
158
+ parsed: ServerSentEvent[];
159
+ remaining: string;
160
+ };
161
+ /**
162
+ * Web/Browser stream adapter using native EventSource
163
+ */
164
+ declare class WebStreamAdapter implements StreamAdapter {
165
+ supportsNativeSSE(): boolean;
166
+ createEventSource(url: string, options?: EventSourceOptions): AsyncIterable<ServerSentEvent>;
167
+ }
168
+ /**
169
+ * Fetch-based stream adapter for React Native and Node.js
170
+ * Uses ReadableStream to parse SSE from fetch response
171
+ */
172
+ declare class FetchStreamAdapter implements StreamAdapter {
173
+ private fetchFn;
174
+ constructor(fetchFn?: typeof fetch);
175
+ supportsNativeSSE(): boolean;
176
+ createEventSource(url: string, options?: EventSourceOptions): AsyncIterable<ServerSentEvent>;
177
+ }
178
+ /**
179
+ * Auto-detect and create the best stream adapter for the current environment
180
+ */
181
+ declare function createStreamAdapter(fetchFn?: typeof fetch): StreamAdapter;
182
+
183
+ /**
184
+ * Base HTTP client for SaferCity SDK
185
+ */
186
+
187
+ interface BaseClientOptions extends SaferCityConfig {
188
+ }
189
+ /**
190
+ * Base HTTP client with common functionality
191
+ */
192
+ declare class BaseClient {
193
+ protected config: Required<Pick<SaferCityConfig, 'baseUrl' | 'timeout'>> & SaferCityConfig;
194
+ protected fetchFn: typeof fetch;
195
+ constructor(options: BaseClientOptions);
196
+ /**
197
+ * Update authentication token
198
+ */
199
+ setToken(token: string | undefined): void;
200
+ /**
201
+ * Update tenant ID
202
+ */
203
+ setTenantId(tenantId: string | undefined): void;
204
+ /**
205
+ * Get current configuration (read-only)
206
+ */
207
+ getConfig(): Readonly<SaferCityConfig>;
208
+ /**
209
+ * Build full URL from path
210
+ */
211
+ protected buildUrl(path: string, query?: Record<string, string | number | boolean | undefined>): string;
212
+ /**
213
+ * Make HTTP request
214
+ */
215
+ protected request<T>(method: string, path: string, options?: RequestOptions & {
216
+ body?: unknown;
217
+ query?: Record<string, string | number | boolean | undefined>;
218
+ }): Promise<ApiResponse<T>>;
219
+ /**
220
+ * GET request
221
+ */
222
+ get<T>(path: string, options?: RequestOptions & {
223
+ query?: Record<string, string | number | boolean | undefined>;
224
+ }): Promise<ApiResponse<T>>;
225
+ /**
226
+ * POST request
227
+ */
228
+ post<T>(path: string, body?: unknown, options?: RequestOptions): Promise<ApiResponse<T>>;
229
+ /**
230
+ * PUT request
231
+ */
232
+ put<T>(path: string, body?: unknown, options?: RequestOptions): Promise<ApiResponse<T>>;
233
+ /**
234
+ * PATCH request
235
+ */
236
+ patch<T>(path: string, body?: unknown, options?: RequestOptions): Promise<ApiResponse<T>>;
237
+ /**
238
+ * DELETE request
239
+ */
240
+ delete<T>(path: string, options?: RequestOptions): Promise<ApiResponse<T>>;
241
+ }
242
+
243
+ export { type ApiError, type ApiResponse, type AuthTokens, BaseClient, type BaseClientOptions, type EventSourceOptions, FetchStreamAdapter, MemoryTokenStorage, type RequestOptions, SaferCityApiError, type SaferCityConfig, type SaferCityJwtPayload, type ServerSentEvent, type StreamAdapter, type TokenStorage, WebStreamAdapter, createAuthHeader, createStreamAdapter, getJwtExpiration, isTokenExpired, parseJwtPayload, parseSSE };