-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from armada-ths/hampus/feature-flag
Hampus/feature flag
- Loading branch information
Showing
8 changed files
with
52 additions
and
43 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,10 +1,14 @@ | ||
import { type ApiData } from "@vercel/flags" | ||
import { NextResponse } from "next/server" | ||
import { verifyAccess, type ApiData } from "@vercel/flags" | ||
import { NextRequest, NextResponse } from "next/server" | ||
import { FEATURE_FLAG_DEFINITIONS } from "../../../../feature_flags" | ||
|
||
export async function GET() { | ||
export async function GET(request: NextRequest) { | ||
const access = await verifyAccess(request.headers.get("Authorization")) | ||
if (!access) return NextResponse.json(null, { status: 401 }) | ||
|
||
const apiData = { | ||
definitions: FEATURE_FLAG_DEFINITIONS | ||
} | ||
|
||
return NextResponse.json<ApiData>(apiData) | ||
} |
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,29 +1,18 @@ | ||
import featureFlags from "@/feature_flags" | ||
import featureFlags, { FEATURE_FLAGS } from "@/feature_flags" | ||
import { decrypt, FlagOverridesType } from "@vercel/flags" | ||
import { cookies } from "next/headers" | ||
|
||
export function feature(feature: keyof typeof featureFlags) { | ||
const cookie = cookies() | ||
|
||
const result = cookie.get("vercel-flag-overrides") | ||
if (result != null) { | ||
return parseCookie(feature, result.value) | ||
export async function features() { | ||
const overrideCookie = cookies().get("vercel-flag-overrides")?.value | ||
const overrides = overrideCookie | ||
? await decrypt<FlagOverridesType>(overrideCookie) | ||
: {} | ||
return { | ||
...FEATURE_FLAGS, | ||
...overrides | ||
} | ||
return false | ||
} | ||
|
||
function parseCookie( | ||
feature: keyof typeof featureFlags, | ||
rawOverrides: string | undefined | ||
) { | ||
const overrides = | ||
rawOverrides == null | ||
? {} | ||
: (JSON.parse(rawOverrides) as Record<string, boolean>) | ||
|
||
if (overrides[feature] != null) { | ||
return overrides[feature] | ||
} else if (featureFlags[feature] != null) { | ||
return featureFlags[feature] | ||
} | ||
return false | ||
export async function feature(feature: keyof typeof featureFlags) { | ||
return (await features())[feature] ?? 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { features } from "@/components/shared/feature" | ||
import FEATURE_FLAGS from "@/feature_flags" | ||
import { useQuery } from "@tanstack/react-query" | ||
|
||
export function useFeature(feature?: keyof typeof FEATURE_FLAGS) { | ||
const { data, isLoading } = useQuery({ | ||
queryKey: ["feature-flags"], | ||
queryFn: async () => await features() | ||
}) | ||
|
||
return { | ||
isLoading, | ||
enabled: feature ? data?.[feature] : null, | ||
flags: data | ||
} | ||
} |