| 1 | import { headers } from "next/headers"; |
| 2 | import { cache } |
| 3 | import createFetchClient from "openapi-fetch |
| 4 | |
| 5 | import { authClient } |
| 6 | import { administers } |
| 7 | import { INTERNAL_API_URL } |
| 8 | |
| 9 | import type { paths |
| 10 | |
| 11 | /** What the API needs from the browser's request: its cookies, and the client address the proxy put in x-forwarded-for, which Better Auth rate-limits by. */ |
| 12 | async function forwardedHeaders() |
| 13 | const incoming = |
| 14 | const forwarded: |
| 15 | for (const name |
| 16 | const value = |
| 17 | if (value) forwarded |
| 18 | } |
| 19 | return forwarded; |
| 20 | } |
| 21 | |
| 22 | /** Typed API client for server components. `credentials: "include"` is a browser concept, so the incoming request's cookies are forwarded explicitly. */ |
| 23 | export async function createServerClient |
| 24 | return createFetchClient<paths |
| 25 | baseUrl: INTERNAL_API_URL, |
| 26 | headers: await forwardedHeaders |
| 27 | }); |
| 28 | } |
| 29 | |
| 30 | /** The signed-in session, read over INTERNAL_API_URL: in Docker the public API origin can be this container's own localhost. */ |
| 31 | export async function getServerSession |
| 32 | typeof authClient.$Infer |
| 33 | > { |
| 34 | const forwarded = |
| 35 | if (!forwarded.cookie |
| 36 | |
| 37 | const res = |
| 38 | headers: forwarded, |
| 39 | cache: "no-store", |
| 40 | }); |
| 41 | return res.ok |
| 42 | } |
| 43 | |
| 44 | /** The viewer's role on a repository, fetched once per render however many components ask. */ |
| 45 | export const getViewerRole = |
| 46 | const client = |
| 47 | const { data |
| 48 | params: { path: { |
| 49 | }); |
| 50 | return data?.viewerRole |
| 51 | }); |
| 52 | |
| 53 | /** Slugs of the organizations the viewer administers, where they may create, fork and transfer repositories to. */ |
| 54 | export const getAdminOrganizations = |
| 55 | const client = |
| 56 | const { data |
| 57 | return (data?.organizations |
| 58 | .filter((organization) |
| 59 | .map((organization) |
| 60 | }); |