@dinoconfig/js-sdk 1.0.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/lib/types.d.ts ADDED
@@ -0,0 +1,364 @@
1
+ import { CacheConfig } from './cache/cache.types';
2
+ /**
3
+ * Configuration options for initializing the DinoConfig SDK.
4
+ *
5
+ * @interface DinoConfigSDKConfig
6
+ * @example
7
+ * ```typescript
8
+ * const config: DinoConfigSDKConfig = {
9
+ * apiKey: 'dino_your-api-key-here',
10
+ * baseUrl: 'https://api.dinoconfig.com',
11
+ * timeout: 15000,
12
+ * cache: {
13
+ * enabled: true,
14
+ * ttl: 60000,
15
+ * storage: 'localStorage',
16
+ * }
17
+ * };
18
+ * ```
19
+ */
20
+ export interface DinoConfigSDKConfig {
21
+ /**
22
+ * The API key for authentication.
23
+ * Obtain this from your DinoConfig dashboard under Settings > SDK & API Keys.
24
+ *
25
+ * @remarks
26
+ * - API keys are prefixed with `dino_`
27
+ * - Keep your API key secure and never expose it in client-side code
28
+ * - Use environment variables in production
29
+ *
30
+ * @example 'dino_abc123def456...'
31
+ */
32
+ apiKey: string;
33
+ /**
34
+ * The base URL of the DinoConfig API.
35
+ *
36
+ * @default 'http://localhost:3000'
37
+ * @example 'https://api.dinoconfig.com'
38
+ */
39
+ baseUrl?: string;
40
+ /**
41
+ * Request timeout in milliseconds.
42
+ * If a request takes longer than this, it will be aborted.
43
+ *
44
+ * @default 10000
45
+ * @example 15000 // 15 seconds
46
+ */
47
+ timeout?: number;
48
+ /**
49
+ * Cache configuration options.
50
+ * Enables multi-layer caching (memory + storage) for improved performance.
51
+ *
52
+ * @default { enabled: false }
53
+ * @example
54
+ * ```typescript
55
+ * cache: {
56
+ * enabled: true,
57
+ * ttl: 60000, // 1 minute
58
+ * maxSize: 1000,
59
+ * storage: 'localStorage',
60
+ * staleWhileRevalidate: true,
61
+ * }
62
+ * ```
63
+ */
64
+ cache?: Partial<CacheConfig>;
65
+ }
66
+ /**
67
+ * Response from the token exchange endpoint.
68
+ * Used internally when exchanging an API key for an access token.
69
+ *
70
+ * @interface TokenExchangeResponse
71
+ * @internal
72
+ */
73
+ export interface TokenExchangeResponse {
74
+ /**
75
+ * The JWT access token to use for API requests.
76
+ */
77
+ access_token: string;
78
+ /**
79
+ * Token expiration time in seconds.
80
+ */
81
+ expires_in: number;
82
+ /**
83
+ * The type of token (typically 'Bearer').
84
+ */
85
+ token_type: string;
86
+ /**
87
+ * The company/organization associated with the API key.
88
+ */
89
+ company?: string;
90
+ }
91
+ /**
92
+ * Represents a configuration object in DinoConfig.
93
+ *
94
+ * @interface Config
95
+ * @example
96
+ * ```typescript
97
+ * const config: Config = {
98
+ * id: 1,
99
+ * name: 'AppSettings',
100
+ * description: 'Main application settings',
101
+ * formData: { theme: 'dark', language: 'en' },
102
+ * version: 3,
103
+ * createdAt: new Date('2024-01-15'),
104
+ * brand: { id: 1, name: 'MyBrand' }
105
+ * };
106
+ * ```
107
+ */
108
+ export interface Config {
109
+ /**
110
+ * Unique identifier for the configuration.
111
+ */
112
+ id: number;
113
+ /**
114
+ * Human-readable name of the configuration.
115
+ */
116
+ name: string;
117
+ /**
118
+ * Optional description explaining the configuration's purpose.
119
+ */
120
+ description?: string;
121
+ /**
122
+ * The company/organization that owns this configuration.
123
+ */
124
+ company?: string;
125
+ /**
126
+ * The actual configuration data as key-value pairs.
127
+ * This can contain nested objects and arrays.
128
+ */
129
+ formData: Record<string, any>;
130
+ /**
131
+ * JSON Schema defining the structure of formData.
132
+ * Used for validation and form generation.
133
+ */
134
+ schema?: Record<string, any>;
135
+ /**
136
+ * UI Schema for customizing form rendering.
137
+ * Used by JSON Schema Form libraries.
138
+ */
139
+ uiSchema?: Record<string, any>;
140
+ /**
141
+ * Version number of this configuration.
142
+ * Incremented on each update.
143
+ */
144
+ version: number;
145
+ /**
146
+ * Timestamp when this configuration was created.
147
+ */
148
+ createdAt: Date;
149
+ /**
150
+ * The brand this configuration belongs to.
151
+ */
152
+ brand: {
153
+ /** Brand's unique identifier */
154
+ id: number;
155
+ /** Brand's name */
156
+ name: string;
157
+ };
158
+ }
159
+ /**
160
+ * Data transfer object for creating a new configuration.
161
+ *
162
+ * @interface CreateConfigDto
163
+ * @example
164
+ * ```typescript
165
+ * const newConfig: CreateConfigDto = {
166
+ * name: 'FeatureFlags',
167
+ * description: 'Feature flag settings',
168
+ * formData: { enableBeta: false, maxUsers: 100 }
169
+ * };
170
+ * ```
171
+ */
172
+ export interface CreateConfigDto {
173
+ /**
174
+ * Name for the new configuration.
175
+ * Must be unique within the brand.
176
+ */
177
+ name: string;
178
+ /**
179
+ * Optional description of the configuration.
180
+ */
181
+ description?: string;
182
+ /**
183
+ * The configuration data to store.
184
+ */
185
+ formData: Record<string, any>;
186
+ /**
187
+ * Optional JSON Schema for validation.
188
+ */
189
+ schema?: Record<string, any>;
190
+ /**
191
+ * Optional UI Schema for form rendering.
192
+ */
193
+ uiSchema?: Record<string, any>;
194
+ }
195
+ /**
196
+ * Data transfer object for updating an existing configuration.
197
+ * All fields are optional - only provided fields will be updated.
198
+ *
199
+ * @interface UpdateConfigDto
200
+ * @example
201
+ * ```typescript
202
+ * const updates: UpdateConfigDto = {
203
+ * formData: { enableBeta: true } // Only update formData
204
+ * };
205
+ * ```
206
+ */
207
+ export interface UpdateConfigDto {
208
+ /**
209
+ * New name for the configuration.
210
+ */
211
+ name?: string;
212
+ /**
213
+ * New description.
214
+ */
215
+ description?: string;
216
+ /**
217
+ * Updated configuration data.
218
+ */
219
+ formData?: Record<string, any>;
220
+ /**
221
+ * Updated JSON Schema.
222
+ */
223
+ schema?: Record<string, any>;
224
+ /**
225
+ * Updated UI Schema.
226
+ */
227
+ uiSchema?: Record<string, any>;
228
+ }
229
+ /**
230
+ * Standard API response wrapper.
231
+ * All SDK methods return data wrapped in this structure.
232
+ *
233
+ * @interface ApiResponse
234
+ * @typeParam T - The type of data contained in the response
235
+ * @example
236
+ * ```typescript
237
+ * const response: ApiResponse<string> = {
238
+ * data: 'dark',
239
+ * success: true
240
+ * };
241
+ *
242
+ * if (response.success) {
243
+ * console.log('Theme:', response.data);
244
+ * }
245
+ * ```
246
+ */
247
+ export interface ApiResponse<T = any> {
248
+ /**
249
+ * The response payload.
250
+ * Contains the requested data on success.
251
+ */
252
+ data: T;
253
+ /**
254
+ * Indicates whether the request was successful.
255
+ * `true` for 2xx responses, `false` otherwise.
256
+ */
257
+ success: boolean;
258
+ /**
259
+ * Optional message providing additional context.
260
+ * Typically populated for errors or warnings.
261
+ */
262
+ message?: string;
263
+ }
264
+ /**
265
+ * Structured error object thrown by the SDK.
266
+ * Provides detailed information about what went wrong.
267
+ *
268
+ * @interface ApiError
269
+ * @example
270
+ * ```typescript
271
+ * try {
272
+ * await dinoconfig.configs.getValue('brand', 'config', 'key');
273
+ * } catch (error) {
274
+ * const apiError = error as ApiError;
275
+ * console.error(`Error ${apiError.status}: ${apiError.message}`);
276
+ * if (apiError.code === 'CONFIG_NOT_FOUND') {
277
+ * // Handle specific error
278
+ * }
279
+ * }
280
+ * ```
281
+ */
282
+ export interface ApiError {
283
+ /**
284
+ * Human-readable error message describing what went wrong.
285
+ */
286
+ message: string;
287
+ /**
288
+ * HTTP status code of the error response.
289
+ *
290
+ * @example
291
+ * - 400: Bad Request
292
+ * - 401: Unauthorized
293
+ * - 403: Forbidden
294
+ * - 404: Not Found
295
+ * - 429: Too Many Requests
296
+ * - 500: Internal Server Error
297
+ */
298
+ status: number;
299
+ /**
300
+ * Optional error code for programmatic error handling.
301
+ *
302
+ * @example 'CONFIG_NOT_FOUND', 'INVALID_API_KEY', 'RATE_LIMITED'
303
+ */
304
+ code?: string;
305
+ }
306
+ /**
307
+ * Options for customizing individual API requests.
308
+ *
309
+ * @interface RequestOptions
310
+ * @example
311
+ * ```typescript
312
+ * const options: RequestOptions = {
313
+ * timeout: 5000,
314
+ * retries: 3,
315
+ * headers: { 'X-Request-ID': 'abc123' }
316
+ * };
317
+ *
318
+ * const response = await dinoconfig.configs.getValue(
319
+ * 'brand', 'config', 'key', options
320
+ * );
321
+ * ```
322
+ */
323
+ export interface RequestOptions {
324
+ /**
325
+ * Custom headers to include in this specific request.
326
+ * These are merged with default headers.
327
+ */
328
+ headers?: Record<string, string>;
329
+ /**
330
+ * Request timeout in milliseconds.
331
+ * Overrides the default timeout set during SDK initialization.
332
+ */
333
+ timeout?: number;
334
+ /**
335
+ * Number of retry attempts for failed requests.
336
+ * Uses exponential backoff between retries.
337
+ *
338
+ * @remarks
339
+ * - Only server errors (5xx) and network errors are retried
340
+ * - Client errors (4xx) are not retried
341
+ * - Backoff formula: `2^attempt * 1000ms`
342
+ *
343
+ * @default 0
344
+ * @example 3 // Retry up to 3 times
345
+ */
346
+ retries?: number;
347
+ /**
348
+ * Whether to use cache for this request.
349
+ *
350
+ * @default true
351
+ */
352
+ cache?: boolean;
353
+ /**
354
+ * Force refresh from API, bypassing cache.
355
+ *
356
+ * @default false
357
+ */
358
+ forceRefresh?: boolean;
359
+ /**
360
+ * Custom TTL (time-to-live) for this specific cached request in milliseconds.
361
+ * Overrides the default TTL from cache configuration.
362
+ */
363
+ ttl?: number;
364
+ }
package/package.json ADDED
@@ -0,0 +1,77 @@
1
+ {
2
+ "name": "@dinoconfig/js-sdk",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "main": "./index.js",
6
+ "module": "./index.js",
7
+ "types": "./index.d.ts",
8
+ "description": "Official JavaScript/TypeScript SDK for DinoConfig API - Simple, type-safe configuration management",
9
+ "keywords": [
10
+ "dinoconfig",
11
+ "sdk",
12
+ "javascript",
13
+ "typescript",
14
+ "api",
15
+ "configuration",
16
+ "config",
17
+ "feature-flags",
18
+ "remote-config",
19
+ "settings",
20
+ "config-management",
21
+ "environment-variables",
22
+ "app-config"
23
+ ],
24
+ "author": {
25
+ "name": "DinoConfig Team",
26
+ "email": "support@dinoconfig.com",
27
+ "url": "https://dinoconfig.com"
28
+ },
29
+ "license": "MIT",
30
+ "homepage": "https://dinoconfig.com/docs/javascript-sdk",
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git+https://github.com/dinoconfig/dinoconfig.git",
34
+ "directory": "libs/dinoconfig-js-sdk"
35
+ },
36
+ "bugs": {
37
+ "url": "https://github.com/dinoconfig/dinoconfig/issues",
38
+ "email": "support@dinoconfig.com"
39
+ },
40
+ "funding": {
41
+ "type": "github",
42
+ "url": "https://github.com/sponsors/dinoconfig"
43
+ },
44
+ "files": [
45
+ "index.js",
46
+ "index.js.map",
47
+ "index.d.ts",
48
+ "lib/**/*.js",
49
+ "lib/**/*.js.map",
50
+ "lib/**/*.d.ts",
51
+ "README.md",
52
+ "LICENSE",
53
+ "CHANGELOG.md"
54
+ ],
55
+ "exports": {
56
+ ".": {
57
+ "types": "./index.d.ts",
58
+ "import": "./index.js",
59
+ "default": "./index.js"
60
+ },
61
+ "./package.json": "./package.json"
62
+ },
63
+ "sideEffects": false,
64
+ "dependencies": {},
65
+ "peerDependencies": {},
66
+ "devDependencies": {
67
+ "@types/node": "^20.0.0",
68
+ "typescript": "^5.0.0"
69
+ },
70
+ "engines": {
71
+ "node": ">=18.0.0"
72
+ },
73
+ "publishConfig": {
74
+ "access": "public",
75
+ "registry": "https://registry.npmjs.org/"
76
+ }
77
+ }