-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: rework edge configs and flag support
Signed-off-by: Griko Nibras <griko@nibras.co>
- Loading branch information
Showing
7 changed files
with
91 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { ExperimentalFeature } from "@skip-router/core"; | ||
import { useQuery } from "@tanstack/react-query"; | ||
|
||
export function useExperimentalFeatures() { | ||
return useQuery({ | ||
queryKey: ["USE_EXPERIMENTAL_FEATURES"] as const, | ||
queryFn: async () => { | ||
try { | ||
const response = await fetch("/api/flags"); | ||
const data = (await response.json()) as ExperimentalFeature[]; | ||
return data; | ||
} catch { | ||
// | ||
} | ||
}, | ||
refetchOnMount: false, | ||
refetchOnReconnect: false, | ||
refetchOnWindowFocus: false, | ||
retry: false, | ||
}); | ||
} |
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,35 +1,45 @@ | ||
if (typeof window !== "undefined") { | ||
throw new Error("edge-config.ts should only be imported on the server"); | ||
} | ||
|
||
import { ExperimentalFeature } from "@skip-router/core"; | ||
import { createClient } from "@vercel/edge-config"; | ||
|
||
function getClient() { | ||
return createClient(process.env.NEXT_PUBLIC_EDGE_CONFIG!); | ||
} | ||
const client = createClient(process.env.NEXT_PUBLIC_EDGE_CONFIG!); | ||
|
||
export async function getCorsDomains(): Promise<string[]> { | ||
export async function getCorsDomains() { | ||
try { | ||
const client = getClient(); | ||
const value = await client.get<string[]>("domains"); | ||
if (Array.isArray(value)) return value; | ||
return []; | ||
const key = "domains"; | ||
const value = await client.get<string[]>(key); | ||
if (Array.isArray(value)) { | ||
return value; | ||
} | ||
} catch (error) { | ||
console.error(error); | ||
return []; | ||
} | ||
} | ||
|
||
type ExperimentalFeature = /* import('@skip-router/core').ExperimentalFeature */ string; | ||
|
||
export async function getClientFlags(): Promise<ExperimentalFeature[]> { | ||
export async function getExperimentalFeatures() { | ||
try { | ||
if (process.env.NEXT_PUBLIC_FLAGS_OVERRIDE) { | ||
const value = process.env.NEXT_PUBLIC_FLAGS_OVERRIDE.split(",").filter(Boolean) as ExperimentalFeature[]; | ||
const branch = process.env.NEXT_PUBLIC_VERCEL_GIT_COMMIT_REF; | ||
const key = (() => { | ||
switch (branch) { | ||
case "main": | ||
return "experimental-features"; | ||
case "staging": | ||
return "experimental-features-staging"; | ||
case "dev": | ||
return "experimental-features-dev"; | ||
default: | ||
return "experimental-features"; | ||
} | ||
})(); | ||
|
||
const value = await client.get<ExperimentalFeature[]>(key); | ||
if (Array.isArray(value)) { | ||
return value; | ||
} | ||
const client = getClient(); | ||
const value = await client.get<ExperimentalFeature[]>("experimentalFeatures"); | ||
if (Array.isArray(value)) return value; | ||
return []; | ||
} catch (error) { | ||
console.error(error); | ||
return []; | ||
} | ||
} |
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,22 @@ | ||
import { PageConfig } from "next"; | ||
|
||
import { getExperimentalFeatures } from "@/lib/edge-config"; | ||
|
||
export const config: PageConfig = { | ||
runtime: "edge", | ||
}; | ||
|
||
export default async function handler() { | ||
try { | ||
const flags = await getExperimentalFeatures(); | ||
return new Response(JSON.stringify(flags), { | ||
headers: { | ||
"cache-control": "public, s-maxage=1, stale-while-revalidate", | ||
"content-type": "application/json", | ||
}, | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
return new Response(null, { status: 500 }); // Internal Server Error | ||
} | ||
} |
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