| 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 | async function forwardedCookie() |
| 12 | return (await headers |
| 13 | } |
| 14 | |
| 15 | /** Typed API client for server components. `credentials: "include"` is a browser concept, so the incoming request's cookies are forwarded explicitly. */ |
| 16 | export async function createServerClient |
| 17 | const cookie = |
| 18 | |
| 19 | return createFetchClient<paths |
| 20 | baseUrl: INTERNAL_API_URL, |
| 21 | headers: cookie ? |
| 22 | }); |
| 23 | } |
| 24 | |
| 25 | export async function getServerSession |
| 26 | const cookie = |
| 27 | if (!cookie) |
| 28 | |
| 29 | const { data |
| 30 | fetchOptions: { headers: { |
| 31 | }); |
| 32 | |
| 33 | return data; |
| 34 | } |
| 35 | |
| 36 | /** The viewer's role on a repository, fetched once per render however many components ask. */ |
| 37 | export const getViewerRole = |
| 38 | const client = |
| 39 | const { data |
| 40 | params: { path: { |
| 41 | }); |
| 42 | return data?.viewerRole |
| 43 | }); |
| 44 | |
| 45 | /** Slugs of the organizations the viewer administers, where they may create, fork and transfer repositories to. */ |
| 46 | export const getAdminOrganizations = |
| 47 | const client = |
| 48 | const { data |
| 49 | return (data?.organizations |
| 50 | .filter((organization) |
| 51 | .map((organization) |
| 52 | }); |