feat(frontend): add typed backend API client (openapi-fetch) with codegen script
This commit is contained in:
Vendored
+4
@@ -0,0 +1,4 @@
|
||||
export type paths = Record<string, never>;
|
||||
export type webhooks = Record<string, never>;
|
||||
export type components = Record<string, never>;
|
||||
export type operations = Record<string, never>;
|
||||
@@ -0,0 +1,16 @@
|
||||
import createClient from 'openapi-fetch';
|
||||
import type { paths } from '$lib/api/schema';
|
||||
import { apiConfig } from '$lib/server/config';
|
||||
|
||||
interface ApiClientOptions {
|
||||
fetch: typeof fetch;
|
||||
accessToken?: string;
|
||||
}
|
||||
|
||||
export function createApiClient({ fetch, accessToken }: ApiClientOptions) {
|
||||
return createClient<paths>({
|
||||
baseUrl: apiConfig.baseUrl,
|
||||
fetch,
|
||||
headers: accessToken ? { Authorization: `Bearer ${accessToken}` } : undefined
|
||||
});
|
||||
}
|
||||
@@ -45,3 +45,9 @@ export const authConfig = {
|
||||
return env.AUTH_ALLOW_INSECURE_HTTP === 'true';
|
||||
}
|
||||
};
|
||||
|
||||
export const apiConfig = {
|
||||
get baseUrl() {
|
||||
return (env.API_BASE_URL ?? '').replace(/\/+$/, '');
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user