-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c79b76a
commit 9b693b7
Showing
18 changed files
with
109 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import useSWR, { Fetcher, SWRConfiguration, SWRResponse } from "swr"; | ||
import useSWRImmutable from "swr/immutable"; | ||
import { withSkewProtection } from "../util/withSkewProtection"; | ||
import { FernDocsApiRoute, useApiRoute } from "./useApiRoute"; | ||
|
||
interface Options<T> extends SWRConfiguration<T, Error, Fetcher<T>> { | ||
disabled?: boolean; | ||
request?: RequestInit; | ||
} | ||
|
||
function createFetcher<T>(init?: RequestInit): (url: string) => Promise<T> { | ||
return (url: string): Promise<T> => fetch(withSkewProtection(url), init).then((r) => r.json()); | ||
} | ||
|
||
export function useApiRouteSWR<T>(route: FernDocsApiRoute, options?: Options<T>): SWRResponse<T> { | ||
const key = useApiRoute(route); | ||
return useSWR(options?.disabled ? null : key, createFetcher(options?.request), options); | ||
} | ||
|
||
export function useApiRouteSWRImmutable<T>(route: FernDocsApiRoute, options?: Options<T>): SWRResponse<T> { | ||
const key = useApiRoute(route); | ||
return useSWRImmutable(options?.disabled ? null : key, createFetcher(options?.request), options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,11 @@ | ||
import type { APIKeyInjectionConfig } from "@fern-ui/fern-docs-auth"; | ||
import useSWR from "swr"; | ||
import { useApiRoute } from "../hooks/useApiRoute"; | ||
import { useApiRouteSWR } from "../hooks/useApiRouteSWR"; | ||
|
||
const DEFAULT = { enabled: false as const }; | ||
|
||
export function useApiKeyInjectionConfig(): APIKeyInjectionConfig { | ||
const key = useApiRoute("/api/fern-docs/auth/api-key-injection"); | ||
const { data } = useSWR<APIKeyInjectionConfig>( | ||
key, | ||
async (url: string) => { | ||
const res = await fetch(url); | ||
return res.json(); | ||
}, | ||
{ | ||
refreshInterval: (latestData) => (latestData?.enabled ? 1000 * 60 * 5 : 0), // refresh every 5 minutes | ||
}, | ||
); | ||
const { data } = useApiRouteSWR<APIKeyInjectionConfig>("/api/fern-docs/auth/api-key-injection", { | ||
refreshInterval: (latestData) => (latestData?.enabled ? 1000 * 60 * 5 : 0), // refresh every 5 minutes | ||
}); | ||
return data ?? DEFAULT; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const deploymentId = process.env.NEXT_DEPLOYMENT_ID; | ||
export function withSkewProtection(url: string): string { | ||
if (!deploymentId) { | ||
return url; | ||
} | ||
|
||
if (url.includes("?")) { | ||
return `${url}&dpl=${deploymentId}`; | ||
} else { | ||
return `${url}?dpl=${deploymentId}`; | ||
} | ||
} |
Oops, something went wrong.