Wire frontend into Docker Compose with type-safe API client
- Add frontend service to docker-compose.yml (port 3000, BACKEND_URL env var) - Add frontend/Dockerfile using adapter-node for plain Node/Docker runtime - Switch svelte.config.js from adapter-auto to adapter-node - Generate OpenAPI types from backend spec (openapi-typescript + openapi-fetch) - Add src/lib/server/api.ts as server-only typed API client factory - Add generate:api script to regenerate types when backend spec changes Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1
frontend/src/lib/api/openapi.json
Normal file
1
frontend/src/lib/api/openapi.json
Normal file
File diff suppressed because one or more lines are too long
1992
frontend/src/lib/api/schema.d.ts
vendored
Normal file
1992
frontend/src/lib/api/schema.d.ts
vendored
Normal file
File diff suppressed because it is too large
Load Diff
19
frontend/src/lib/server/api.ts
Normal file
19
frontend/src/lib/server/api.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import createClient from 'openapi-fetch';
|
||||
import type { paths } from '$lib/api/schema.d.ts';
|
||||
import { env } from '$env/dynamic/private';
|
||||
|
||||
// Usage in +page.server.ts load functions and form actions:
|
||||
//
|
||||
// export const load = async ({ fetch }) => {
|
||||
// const api = apiClient(fetch);
|
||||
// const { data, error } = await api.GET('/v1/recipes', { ... });
|
||||
// };
|
||||
//
|
||||
// Always pass SvelteKit's `fetch` so session cookies are forwarded correctly.
|
||||
|
||||
export function apiClient(fetch?: typeof globalThis.fetch) {
|
||||
return createClient<paths>({
|
||||
baseUrl: env.BACKEND_URL ?? 'http://localhost:8080',
|
||||
fetch: fetch ?? globalThis.fetch
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user