The Coline App SDK: apps, tools, file types and surfaces that run inside Coline workspaces, and public websites with Coline Sites. TypeScript, with an in-memory test workspace. MIT.
| 4 |
| 5 | function createAuthenticatedFetch(options: ColineApiClientOptions): typeof fetch { |
| 6 | return async (input, init = {}) => { |
| 7 | const headers = new Headers(options.headers); |
| 8 | const requestHeaders = new Headers(init.headers); |
| 9 | requestHeaders.forEach((value, key) => headers.set(key, value)); |
| 10 |
| 11 | if (!headers.has("Content-Type") && init.body) { |
| 12 | headers.set("Content-Type", "application/json"); |
| 13 | } |
| 14 |
| 15 | if (options.apiKey && !headers.has("Authorization")) { |
| 16 | headers.set("Authorization", `Bearer ${options.apiKey}`); |
| 17 | } |
| 18 |
| 19 | return (options.fetch ?? fetch)(input, { |
| 20 | ...init, |
| 21 | headers, |
| 22 | }); |
| 23 | }; |
| 24 | } |
| 25 |
| 26 | export function createAppPlatformClient(options: ColineApiClientOptions) { |
| 27 | return createClient<paths>({ |
| 28 | baseUrl: options.baseUrl, |
| 29 | fetch: createAuthenticatedFetch(options), |
| 30 | }); |
| 31 | } |
| 32 |
| 33 | export type AppPlatformClient = ReturnType<typeof createAppPlatformClient>; |
| 34 |