@donkeylabs/server 0.4.6 → 0.4.7
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/docs/sveltekit-adapter.md +64 -35
- package/package.json +1 -1
|
@@ -88,27 +88,19 @@ import { createHandle } from "@donkeylabs/adapter-sveltekit/hooks";
|
|
|
88
88
|
export const handle = createHandle();
|
|
89
89
|
```
|
|
90
90
|
|
|
91
|
-
### 4.
|
|
91
|
+
### 4. Generate the API Client
|
|
92
92
|
|
|
93
|
-
|
|
94
|
-
// src/lib/api.ts
|
|
95
|
-
import { UnifiedApiClientBase } from "@donkeylabs/adapter-sveltekit/client";
|
|
96
|
-
|
|
97
|
-
interface DataResponse {
|
|
98
|
-
message: string;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export class ApiClient extends UnifiedApiClientBase {
|
|
102
|
-
data = {
|
|
103
|
-
get: () => this.request<{}, DataResponse>("api.data.get", {}),
|
|
104
|
-
};
|
|
105
|
-
}
|
|
93
|
+
Run the generator to create a fully-typed client:
|
|
106
94
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
95
|
+
```bash
|
|
96
|
+
bun run donkeylabs generate
|
|
110
97
|
```
|
|
111
98
|
|
|
99
|
+
This creates `src/lib/api.ts` with:
|
|
100
|
+
- Typed methods for all your routes
|
|
101
|
+
- `Routes` namespace with `Input` and `Output` types
|
|
102
|
+
- `createApi()` factory function
|
|
103
|
+
|
|
112
104
|
---
|
|
113
105
|
|
|
114
106
|
## Usage
|
|
@@ -322,36 +314,73 @@ Add path aliases in `tsconfig.json`:
|
|
|
322
314
|
|
|
323
315
|
---
|
|
324
316
|
|
|
325
|
-
##
|
|
317
|
+
## Auto-Generated Client
|
|
326
318
|
|
|
327
|
-
|
|
319
|
+
When you run `donkeylabs generate`, it creates a fully-typed client at `src/lib/api.ts`.
|
|
328
320
|
|
|
329
|
-
|
|
321
|
+
### Generated Structure
|
|
330
322
|
|
|
331
323
|
```ts
|
|
332
|
-
// src/lib/api.ts
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
324
|
+
// src/lib/api.ts (auto-generated)
|
|
325
|
+
|
|
326
|
+
// Route types - use these for forms, props, etc.
|
|
327
|
+
export namespace Routes {
|
|
328
|
+
export namespace Api {
|
|
329
|
+
export namespace Users {
|
|
330
|
+
export namespace List {
|
|
331
|
+
export type Input = {};
|
|
332
|
+
export type Output = { users: User[] };
|
|
333
|
+
}
|
|
334
|
+
export namespace Create {
|
|
335
|
+
export type Input = { name: string; email: string };
|
|
336
|
+
export type Output = { id: string; name: string; email: string };
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
338
341
|
|
|
342
|
+
// API Client with typed methods
|
|
339
343
|
export class ApiClient extends UnifiedApiClientBase {
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
create: (input: { name: string }) =>
|
|
346
|
-
this.request<typeof input, User>("api.users.create", input),
|
|
344
|
+
api = {
|
|
345
|
+
users: {
|
|
346
|
+
list: (input: Routes.Api.Users.List.Input): Promise<Routes.Api.Users.List.Output> => ...,
|
|
347
|
+
create: (input: Routes.Api.Users.Create.Input): Promise<Routes.Api.Users.Create.Output> => ...,
|
|
348
|
+
}
|
|
347
349
|
};
|
|
348
350
|
}
|
|
349
351
|
|
|
350
|
-
export function createApi(options?: {
|
|
351
|
-
|
|
352
|
+
export function createApi(options?: ClientOptions) { ... }
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
### Using Route Types
|
|
356
|
+
|
|
357
|
+
Import types for forms, validation, or props:
|
|
358
|
+
|
|
359
|
+
```ts
|
|
360
|
+
// In a Svelte component or server file
|
|
361
|
+
import { type Routes } from '$lib/api';
|
|
362
|
+
|
|
363
|
+
// Use route types
|
|
364
|
+
type CreateUserInput = Routes.Api.Users.Create.Input;
|
|
365
|
+
type CreateUserOutput = Routes.Api.Users.Create.Output;
|
|
366
|
+
|
|
367
|
+
// Example: typed form handler
|
|
368
|
+
function handleSubmit(data: CreateUserInput) {
|
|
369
|
+
api.api.users.create(data);
|
|
352
370
|
}
|
|
353
371
|
```
|
|
354
372
|
|
|
373
|
+
### When to Regenerate
|
|
374
|
+
|
|
375
|
+
Run `donkeylabs generate` after:
|
|
376
|
+
- Adding new routes
|
|
377
|
+
- Changing route input/output schemas
|
|
378
|
+
- Adding new plugins with routes
|
|
379
|
+
|
|
380
|
+
---
|
|
381
|
+
|
|
382
|
+
## Common Patterns
|
|
383
|
+
|
|
355
384
|
### Error Handling
|
|
356
385
|
|
|
357
386
|
```ts
|