@herdingbits/trailhead-types 0.0.2 → 0.0.4

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.
Files changed (3) hide show
  1. package/README.md +29 -5
  2. package/package.json +2 -2
  3. package/shell-api.d.ts +334 -2
package/README.md CHANGED
@@ -1,9 +1,13 @@
1
1
  # @herdingbits/trailhead-types
2
2
 
3
- TypeScript type definitions for the Trailhead micro-frontend framework.
3
+ TypeScript type definitions for Trailhead single page applications (SPAs).
4
4
 
5
5
  **Note:** This package is auto-generated from `@herdingbits/trailhead-core` during build. Do not edit these files directly.
6
6
 
7
+ ## What is this?
8
+
9
+ Provides TypeScript types for single page applications (SPAs) that integrate with the Trailhead shell. Install this as a dev dependency to get type checking and IntelliSense for the shell API.
10
+
7
11
  ## Installation
8
12
 
9
13
  ```bash
@@ -12,18 +16,38 @@ npm install --save-dev @herdingbits/trailhead-types
12
16
 
13
17
  ## Usage
14
18
 
19
+ ### In Single Page Applications (SPAs)
20
+
15
21
  ```typescript
16
22
  import type { ShellAPI } from '@herdingbits/trailhead-types';
17
- import type { DesignSystemAdapter } from '@herdingbits/trailhead-types/adapters';
18
23
 
19
- // Use in your SPA apps
20
24
  export function init(shell: ShellAPI) {
25
+ // TypeScript knows the shell API
21
26
  shell.feedback.success('App loaded!');
27
+
28
+ const result = await shell.http.get('/api/data');
29
+ if (result.success) {
30
+ console.log(result.data);
31
+ }
22
32
  }
33
+ ```
34
+
35
+ ### For Custom Adapter Development
36
+
37
+ ```typescript
38
+ import type { DesignSystemAdapter, FeedbackAdapter } from '@herdingbits/trailhead-types/adapters';
23
39
 
24
- // Use when creating custom adapters
25
40
  export class MyAdapter implements DesignSystemAdapter {
26
- // ...
41
+ name = 'my-adapter';
42
+ version = '1.0.0';
43
+
44
+ async init(basePath: string): Promise<void> {
45
+ // Initialize your design system
46
+ }
47
+
48
+ feedback: FeedbackAdapter = {
49
+ // Implement feedback methods
50
+ };
27
51
  }
28
52
  ```
29
53
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@herdingbits/trailhead-types",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "TypeScript type definitions for Trailhead micro-frontend framework",
5
5
  "type": "module",
6
6
  "types": "./public-api.d.ts",
@@ -33,4 +33,4 @@
33
33
  "url": "https://github.com/herdingbits/trailhead.git",
34
34
  "directory": "packages/types"
35
35
  }
36
- }
36
+ }
package/shell-api.d.ts CHANGED
@@ -1,96 +1,428 @@
1
1
  /**
2
2
  * Shell API Interface
3
- * Exposed to plugin applications via window.shell
3
+ *
4
+ * The main API exposed to single page applications (SPAs) via `window.shell`.
5
+ * Provides access to shared services including feedback, HTTP client, and navigation.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * export function init(shell: ShellAPI) {
10
+ * shell.feedback.success('App loaded!');
11
+ * const result = await shell.http.get('/api/data');
12
+ * }
13
+ * ```
4
14
  */
5
15
  export interface ShellAPI {
16
+ /** Shell version string (e.g., "1.0.0") */
6
17
  version: string;
18
+ /** User feedback system for toasts, dialogs, and loading states */
7
19
  feedback: FeedbackAPI;
20
+ /** HTTP client with automatic error handling and loading indicators */
8
21
  http: HttpAPI;
22
+ /** Navigation and routing utilities */
9
23
  navigation: NavigationAPI;
10
24
  }
11
25
  /**
12
26
  * Feedback system API
27
+ *
28
+ * Provides consistent user feedback across all SPAs using the shell's design system.
29
+ * Handles toasts, loading overlays, confirmation dialogs, and alerts.
13
30
  */
14
31
  export interface FeedbackAPI {
32
+ /**
33
+ * Show a loading overlay with a message.
34
+ * Call `clear()` to remove it.
35
+ *
36
+ * @param message - Loading message to display
37
+ * @example
38
+ * ```typescript
39
+ * shell.feedback.busy('Loading data...');
40
+ * await fetchData();
41
+ * shell.feedback.clear();
42
+ * ```
43
+ */
15
44
  busy(message: string): void;
45
+ /**
46
+ * Clear the loading overlay.
47
+ */
16
48
  clear(): void;
49
+ /**
50
+ * Show a success toast notification.
51
+ *
52
+ * @param message - Success message to display
53
+ * @param duration - Duration in milliseconds (default: 3000)
54
+ * @example
55
+ * ```typescript
56
+ * shell.feedback.success('User saved successfully!');
57
+ * ```
58
+ */
17
59
  success(message: string, duration?: number): void;
60
+ /**
61
+ * Show an error toast notification.
62
+ *
63
+ * @param message - Error message to display
64
+ * @param duration - Duration in milliseconds (default: 5000)
65
+ * @example
66
+ * ```typescript
67
+ * shell.feedback.error('Failed to save user');
68
+ * ```
69
+ */
18
70
  error(message: string, duration?: number): void;
71
+ /**
72
+ * Show a warning toast notification.
73
+ *
74
+ * @param message - Warning message to display
75
+ * @param duration - Duration in milliseconds (default: 4000)
76
+ */
19
77
  warning(message: string, duration?: number): void;
78
+ /**
79
+ * Show an info toast notification.
80
+ *
81
+ * @param message - Info message to display
82
+ * @param duration - Duration in milliseconds (default: 3000)
83
+ */
20
84
  info(message: string, duration?: number): void;
85
+ /**
86
+ * Show an alert toast with a specific variant.
87
+ *
88
+ * @param message - Alert message to display
89
+ * @param variant - Alert type: "success" | "error" | "warning" | "info"
90
+ * @param duration - Duration in milliseconds
91
+ */
21
92
  alert(message: string, variant?: AlertVariant, duration?: number): void;
93
+ /**
94
+ * Show a confirmation dialog with OK/Cancel buttons.
95
+ *
96
+ * @param message - Confirmation message
97
+ * @param title - Dialog title (optional)
98
+ * @returns Promise that resolves to `true` if confirmed, `false` if cancelled
99
+ * @example
100
+ * ```typescript
101
+ * const confirmed = await shell.feedback.confirm('Delete this user?', 'Confirm Delete');
102
+ * if (confirmed) {
103
+ * await deleteUser();
104
+ * }
105
+ * ```
106
+ */
22
107
  confirm(message: string, title?: string): Promise<boolean>;
108
+ /**
109
+ * Show an alert dialog with an OK button.
110
+ *
111
+ * @param message - Alert message
112
+ * @param title - Dialog title (optional)
113
+ * @returns Promise that resolves when OK is clicked
114
+ */
23
115
  ok(message: string, title?: string): Promise<void>;
116
+ /**
117
+ * Show a dialog with Yes/No buttons.
118
+ *
119
+ * @param message - Question message
120
+ * @param title - Dialog title (optional)
121
+ * @returns Promise that resolves to `true` for Yes, `false` for No
122
+ */
24
123
  yesNo(message: string, title?: string): Promise<boolean>;
124
+ /**
125
+ * Show a dialog with Yes/No/Cancel buttons.
126
+ *
127
+ * @param message - Question message
128
+ * @param title - Dialog title (optional)
129
+ * @returns Promise that resolves to "yes", "no", or "cancel"
130
+ * @example
131
+ * ```typescript
132
+ * const choice = await shell.feedback.yesNoCancel('Save changes?', 'Unsaved Changes');
133
+ * if (choice === 'yes') {
134
+ * await saveChanges();
135
+ * } else if (choice === 'no') {
136
+ * discardChanges();
137
+ * }
138
+ * ```
139
+ */
25
140
  yesNoCancel(message: string, title?: string): Promise<"yes" | "no" | "cancel">;
141
+ /**
142
+ * Show a custom dialog with user-defined buttons.
143
+ *
144
+ * @param message - Dialog message
145
+ * @param title - Dialog title
146
+ * @param buttons - Array of button configurations
147
+ * @returns Promise that resolves to the selected button's value, or null if cancelled
148
+ * @example
149
+ * ```typescript
150
+ * const action = await shell.feedback.custom(
151
+ * 'Choose an action',
152
+ * 'User Actions',
153
+ * [
154
+ * { label: 'Edit', value: 'edit', variant: 'primary' },
155
+ * { label: 'Delete', value: 'delete', variant: 'danger' },
156
+ * { label: 'Cancel', value: 'cancel' }
157
+ * ]
158
+ * );
159
+ * if (action === 'delete') {
160
+ * await deleteUser();
161
+ * }
162
+ * ```
163
+ */
26
164
  custom<T extends string>(message: string, title: string, buttons: Array<{
27
165
  label: string;
28
166
  value: T;
29
167
  variant?: string;
30
168
  }>): Promise<T | null>;
31
169
  }
170
+ /** Alert variant types for toast notifications */
32
171
  export type AlertVariant = "success" | "error" | "warning" | "info";
33
172
  /**
34
173
  * HTTP client API with automatic feedback orchestration
174
+ *
175
+ * Provides HTTP methods with built-in error handling, loading indicators,
176
+ * and success/error feedback. All requests return a discriminated union
177
+ * for type-safe error handling.
35
178
  */
36
179
  export interface HttpAPI {
180
+ /**
181
+ * Perform a GET request.
182
+ *
183
+ * @param url - Request URL (relative to apiUrl or absolute)
184
+ * @param options - Request options for feedback and headers
185
+ * @returns Promise with success/error result
186
+ * @example
187
+ * ```typescript
188
+ * const result = await shell.http.get('/api/users', {
189
+ * busyMessage: 'Loading users...',
190
+ * successMessage: 'Users loaded!',
191
+ * showSuccess: true
192
+ * });
193
+ *
194
+ * if (result.success) {
195
+ * console.log(result.data);
196
+ * } else {
197
+ * console.error(result.error.message);
198
+ * }
199
+ * ```
200
+ */
37
201
  get<T = any>(url: string, options?: RequestOptions): Promise<Result<T>>;
202
+ /**
203
+ * Perform a POST request.
204
+ *
205
+ * @param url - Request URL
206
+ * @param data - Request body data
207
+ * @param options - Request options for feedback and headers
208
+ * @returns Promise with success/error result
209
+ * @example
210
+ * ```typescript
211
+ * const result = await shell.http.post('/api/users', userData, {
212
+ * busyMessage: 'Creating user...',
213
+ * successMessage: 'User created!',
214
+ * showSuccess: true
215
+ * });
216
+ * ```
217
+ */
38
218
  post<T = any>(url: string, data?: any, options?: RequestOptions): Promise<Result<T>>;
219
+ /**
220
+ * Perform a PUT request.
221
+ *
222
+ * @param url - Request URL
223
+ * @param data - Request body data
224
+ * @param options - Request options for feedback and headers
225
+ * @returns Promise with success/error result
226
+ */
39
227
  put<T = any>(url: string, data?: any, options?: RequestOptions): Promise<Result<T>>;
228
+ /**
229
+ * Perform a PATCH request.
230
+ *
231
+ * @param url - Request URL
232
+ * @param data - Request body data
233
+ * @param options - Request options for feedback and headers
234
+ * @returns Promise with success/error result
235
+ */
40
236
  patch<T = any>(url: string, data?: any, options?: RequestOptions): Promise<Result<T>>;
237
+ /**
238
+ * Perform a DELETE request.
239
+ *
240
+ * @param url - Request URL
241
+ * @param options - Request options for feedback and headers
242
+ * @returns Promise with success/error result
243
+ * @example
244
+ * ```typescript
245
+ * const result = await shell.http.delete(`/api/users/${userId}`, {
246
+ * busyMessage: 'Deleting user...',
247
+ * successMessage: 'User deleted!',
248
+ * showSuccess: true
249
+ * });
250
+ * ```
251
+ */
41
252
  delete<T = any>(url: string, options?: RequestOptions): Promise<Result<T>>;
42
253
  }
254
+ /**
255
+ * HTTP request options
256
+ */
43
257
  export interface RequestOptions {
258
+ /**
259
+ * Unique key to prevent duplicate concurrent requests.
260
+ * If a request with the same key is in progress, subsequent requests are ignored.
261
+ *
262
+ * @example
263
+ * ```typescript
264
+ * // Only one save request at a time
265
+ * await shell.http.post('/api/save', data, { requestKey: 'save-user' });
266
+ * ```
267
+ */
44
268
  requestKey?: string;
269
+ /**
270
+ * Message to show in loading overlay while request is in progress.
271
+ * If not provided, no loading overlay is shown.
272
+ */
45
273
  busyMessage?: string;
274
+ /**
275
+ * Message to show in success toast when request completes successfully.
276
+ * Only shown if `showSuccess` is true.
277
+ */
46
278
  successMessage?: string;
279
+ /**
280
+ * Whether to show success toast on successful request.
281
+ * Default: false
282
+ */
47
283
  showSuccess?: boolean;
284
+ /**
285
+ * Disable all automatic feedback (loading, success, error).
286
+ * Useful when you want to handle feedback manually.
287
+ * Default: false
288
+ */
48
289
  noFeedback?: boolean;
290
+ /**
291
+ * Additional HTTP headers to include in the request.
292
+ *
293
+ * @example
294
+ * ```typescript
295
+ * await shell.http.get('/api/data', {
296
+ * headers: { 'X-Custom-Header': 'value' }
297
+ * });
298
+ * ```
299
+ */
49
300
  headers?: Record<string, string>;
50
301
  }
302
+ /**
303
+ * Successful HTTP response
304
+ */
51
305
  export interface SuccessResult<T> {
306
+ /** Always true for successful responses */
52
307
  success: true;
308
+ /** Response data */
53
309
  data: T;
310
+ /** Request key if provided in options */
54
311
  requestKey?: string;
55
312
  }
313
+ /**
314
+ * Failed HTTP response
315
+ */
56
316
  export interface ErrorResult {
317
+ /** Always false for error responses */
57
318
  success: false;
319
+ /** Error details */
58
320
  error: HttpError;
321
+ /** Request key if provided in options */
59
322
  requestKey?: string;
60
323
  }
324
+ /**
325
+ * Discriminated union of success or error result.
326
+ * Use the `success` property to narrow the type.
327
+ *
328
+ * @example
329
+ * ```typescript
330
+ * const result = await shell.http.get('/api/data');
331
+ *
332
+ * if (result.success) {
333
+ * // TypeScript knows result.data exists
334
+ * console.log(result.data);
335
+ * } else {
336
+ * // TypeScript knows result.error exists
337
+ * console.error(result.error.message);
338
+ * }
339
+ * ```
340
+ */
61
341
  export type Result<T> = SuccessResult<T> | ErrorResult;
342
+ /**
343
+ * HTTP error details
344
+ */
62
345
  export interface HttpError {
346
+ /** Error name (e.g., "HTTPError", "TimeoutError") */
63
347
  name: string;
348
+ /** Human-readable error message */
64
349
  message: string;
350
+ /** HTTP status code if available (e.g., 404, 500) */
65
351
  status?: number;
352
+ /** Additional error data from server response */
66
353
  data?: any;
67
354
  }
68
355
  /**
69
356
  * Navigation API
357
+ *
358
+ * Provides navigation between SPAs and route change notifications.
70
359
  */
71
360
  export interface NavigationAPI {
361
+ /**
362
+ * Navigate to a different path.
363
+ * Triggers a full page reload to load the target SPA.
364
+ *
365
+ * @param path - Target path (e.g., "/demo", "/users")
366
+ * @example
367
+ * ```typescript
368
+ * shell.navigation.navigate('/demo');
369
+ * ```
370
+ */
72
371
  navigate(path: string): void;
372
+ /**
373
+ * Get the current path.
374
+ *
375
+ * @returns Current path (e.g., "/demo")
376
+ */
73
377
  getCurrentPath(): string;
378
+ /**
379
+ * Subscribe to route changes.
380
+ *
381
+ * @param callback - Function called when route changes
382
+ * @returns Unsubscribe function
383
+ * @example
384
+ * ```typescript
385
+ * const unsubscribe = shell.navigation.onRouteChange((path) => {
386
+ * console.log('Route changed to:', path);
387
+ * });
388
+ *
389
+ * // Later, unsubscribe
390
+ * unsubscribe();
391
+ * ```
392
+ */
74
393
  onRouteChange(callback: (path: string) => void): () => void;
75
394
  }
76
395
  /**
77
- * Navigation configuration
396
+ * Navigation item configuration
397
+ *
398
+ * Defines a navigation menu item in `navigation.json`.
78
399
  */
79
400
  export interface NavItem {
401
+ /** Unique identifier for the navigation item */
80
402
  id: string;
403
+ /** URL path for the navigation item (e.g., "/demo") */
81
404
  path: string;
405
+ /** SPA identifier (matches directory name) */
82
406
  app: string;
407
+ /** Icon name (design system specific) */
83
408
  icon: string;
409
+ /** Display label for the navigation item */
84
410
  label: string;
411
+ /** Sort order (lower numbers appear first) */
85
412
  order: number;
413
+ /** Optional badge function that returns a count to display */
86
414
  badge?: () => number;
87
415
  }
88
416
  /**
89
417
  * Global window extension
418
+ *
419
+ * Extends the Window interface to include Trailhead shell API.
90
420
  */
91
421
  declare global {
92
422
  interface Window {
423
+ /** Trailhead shell API available to all SPAs */
93
424
  shell: ShellAPI;
425
+ /** Legacy mount function (deprecated, use init() export instead) */
94
426
  AppMount?: (container: HTMLElement) => void;
95
427
  }
96
428
  }