@safercity/sdk-core 0.1.0 → 0.1.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/README.md +119 -2
- package/package.json +1 -6
package/README.md
CHANGED
|
@@ -10,15 +10,111 @@ Core utilities for SaferCity SDK including fetch abstraction, streaming support,
|
|
|
10
10
|
npm install @safercity/sdk-core
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
+
## What's New in v0.1.0
|
|
14
|
+
|
|
15
|
+
- **Auth Mode Types** - Three authentication modes: proxy, direct, and cookie
|
|
16
|
+
- **Token Manager** - Server-side OAuth token management with automatic refresh and request deduplication
|
|
17
|
+
- **Token Storage** - Pluggable token storage interface with in-memory default
|
|
18
|
+
|
|
13
19
|
## Features
|
|
14
20
|
|
|
15
|
-
- **Type definitions** - Common types for API requests/responses
|
|
16
|
-
- **Authentication utilities** - Token management, JWT parsing
|
|
21
|
+
- **Type definitions** - Common types for API requests/responses and auth mode configuration
|
|
22
|
+
- **Authentication utilities** - Token management, JWT parsing, OAuth token lifecycle
|
|
17
23
|
- **Streaming/SSE support** - Cross-platform Server-Sent Events
|
|
18
24
|
- **Base HTTP client** - Fetch wrapper with timeout and error handling
|
|
19
25
|
|
|
20
26
|
## Usage
|
|
21
27
|
|
|
28
|
+
### Auth Mode Types
|
|
29
|
+
|
|
30
|
+
The SDK supports three authentication modes for different deployment scenarios:
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
import type {
|
|
34
|
+
AuthMode,
|
|
35
|
+
ProxyModeConfig,
|
|
36
|
+
DirectModeConfig,
|
|
37
|
+
CookieModeConfig,
|
|
38
|
+
ClientModeConfig,
|
|
39
|
+
} from '@safercity/sdk-core';
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
| Mode | Flow | Best For |
|
|
43
|
+
|------|------|----------|
|
|
44
|
+
| `"proxy"` | Client -> Your Backend -> SaferCity API | Production web apps (most secure) |
|
|
45
|
+
| `"direct"` | Client -> SaferCity API with external token | White-label apps with external auth (Clerk, Auth0, better-auth) |
|
|
46
|
+
| `"cookie"` | Browser with `credentials: include` | First-party web apps using session cookies |
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
// Proxy mode (default) - backend adds tenant credentials
|
|
50
|
+
const proxyConfig: ProxyModeConfig = {
|
|
51
|
+
mode: 'proxy',
|
|
52
|
+
proxyBaseUrl: '/api/safercity', // defaults to "/api/safercity"
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// Direct mode - external auth provider supplies the token
|
|
56
|
+
const directConfig: DirectModeConfig = {
|
|
57
|
+
mode: 'direct',
|
|
58
|
+
baseUrl: 'https://api.safercity.com',
|
|
59
|
+
tenantId: 'tenant-123',
|
|
60
|
+
getAccessToken: () => session?.accessToken,
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// Cookie mode - session cookies sent automatically
|
|
64
|
+
const cookieConfig: CookieModeConfig = {
|
|
65
|
+
mode: 'cookie',
|
|
66
|
+
baseUrl: 'https://api.safercity.com',
|
|
67
|
+
tenantId: 'tenant-123', // optional, can come from cookie
|
|
68
|
+
};
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Token Manager
|
|
72
|
+
|
|
73
|
+
Server-side OAuth token management with automatic refresh and concurrent request deduplication:
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
import { TokenManager, MemoryTokenStorage } from '@safercity/sdk-core';
|
|
77
|
+
|
|
78
|
+
const tokenManager = new TokenManager({
|
|
79
|
+
credentials: {
|
|
80
|
+
clientId: process.env.SAFERCITY_CLIENT_ID!,
|
|
81
|
+
clientSecret: process.env.SAFERCITY_CLIENT_SECRET!,
|
|
82
|
+
tenantId: 'tenant-123', // optional
|
|
83
|
+
},
|
|
84
|
+
baseUrl: 'https://api.safercity.com',
|
|
85
|
+
refreshBuffer: 60000, // refresh 1 minute before expiry (default)
|
|
86
|
+
storage: new MemoryTokenStorage(), // default, or provide custom
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// Get token (auto-refreshes if expired, dedupes concurrent calls)
|
|
90
|
+
const token = await tokenManager.getToken();
|
|
91
|
+
|
|
92
|
+
// Force refresh (useful for error recovery)
|
|
93
|
+
const freshToken = await tokenManager.forceRefresh();
|
|
94
|
+
|
|
95
|
+
// Check state
|
|
96
|
+
tokenManager.hasToken(); // boolean
|
|
97
|
+
tokenManager.clear(); // remove stored tokens
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Token Storage
|
|
101
|
+
|
|
102
|
+
Implement the `TokenStorage` interface for custom persistence:
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
import type { TokenStorage, AuthTokens } from '@safercity/sdk-core';
|
|
106
|
+
|
|
107
|
+
// Built-in: MemoryTokenStorage (default, no persistence)
|
|
108
|
+
import { MemoryTokenStorage } from '@safercity/sdk-core';
|
|
109
|
+
|
|
110
|
+
// Custom implementation
|
|
111
|
+
class MyTokenStorage implements TokenStorage {
|
|
112
|
+
get(): AuthTokens | null { /* ... */ }
|
|
113
|
+
set(tokens: AuthTokens): void { /* ... */ }
|
|
114
|
+
clear(): void { /* ... */ }
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
22
118
|
### Stream Adapters
|
|
23
119
|
|
|
24
120
|
```typescript
|
|
@@ -116,6 +212,27 @@ interface SaferCityConfig {
|
|
|
116
212
|
}
|
|
117
213
|
```
|
|
118
214
|
|
|
215
|
+
### AuthTokens
|
|
216
|
+
|
|
217
|
+
```typescript
|
|
218
|
+
interface AuthTokens {
|
|
219
|
+
accessToken: string;
|
|
220
|
+
refreshToken?: string;
|
|
221
|
+
expiresAt?: number;
|
|
222
|
+
tokenType: string;
|
|
223
|
+
}
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
### OAuthCredentials
|
|
227
|
+
|
|
228
|
+
```typescript
|
|
229
|
+
interface OAuthCredentials {
|
|
230
|
+
clientId: string;
|
|
231
|
+
clientSecret: string;
|
|
232
|
+
tenantId?: string;
|
|
233
|
+
}
|
|
234
|
+
```
|
|
235
|
+
|
|
119
236
|
### ServerSentEvent
|
|
120
237
|
|
|
121
238
|
```typescript
|
package/package.json
CHANGED
|
@@ -1,16 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@safercity/sdk-core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Core utilities for SaferCity SDK - fetch abstraction, streaming, and authentication",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "SaferCity"
|
|
8
8
|
},
|
|
9
|
-
"repository": {
|
|
10
|
-
"type": "git",
|
|
11
|
-
"url": "https://github.com/safercity/safercity-v2.git",
|
|
12
|
-
"directory": "packages/sdk/core"
|
|
13
|
-
},
|
|
14
9
|
"keywords": ["safercity", "sdk", "core", "streaming", "sse"],
|
|
15
10
|
"sideEffects": false,
|
|
16
11
|
"type": "module",
|