Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example with custom storage #36

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,4 @@ jobs:
run: pnpm build
env:
CI: true
NEXT_PUBLIC_FLAGS_ENDPOINT: https://happykit.dev/api/flags
NEXT_PUBLIC_FLAGS_ENV_KEY: flags_pub_development_289861443285680649
1 change: 0 additions & 1 deletion example/.env.development
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
NEXT_PUBLIC_FLAGS_ENV_KEY="flags_pub_289861443285680649"
NEXT_PUBLIC_FLAGS_ENDPOINT="https://happykit.dev/api/flags"
5 changes: 1 addition & 4 deletions example/components/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ export function Layout(props: {
.getEntriesByType("resource")
.filter((entry) => {
return entry.name.endsWith(
[
process.env.NEXT_PUBLIC_FLAGS_ENDPOINT!,
process.env.NEXT_PUBLIC_FLAGS_ENV_KEY!,
].join("/")
["/api/flags", process.env.NEXT_PUBLIC_FLAGS_ENV_KEY!].join("/")
);
});

Expand Down
5 changes: 1 addition & 4 deletions example/flags/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,5 @@ export type AppFlags = {

export const config: Configuration<AppFlags> = {
envKey: process.env.NEXT_PUBLIC_FLAGS_ENV_KEY!,

// You can just delete this line in your own application.
// It's only here because we use it while working on @happykit/flags itself.
endpoint: process.env.NEXT_PUBLIC_FLAGS_ENDPOINT,
endpoint: "/api/flags",
};
5 changes: 4 additions & 1 deletion example/flags/edge.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { createGetEdgeFlags } from "@happykit/flags/edge";
import { type AppFlags, config } from "./config";
import { getDefinitions } from "flags/storage";

export const getEdgeFlags = createGetEdgeFlags<AppFlags>(config);
export const getEdgeFlags = createGetEdgeFlags<AppFlags>(config, {
getDefinitions,
});
7 changes: 5 additions & 2 deletions example/flags/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import {
type GenericEvaluationResponseBody,
} from "@happykit/flags/server";
import { type AppFlags, config } from "./config";
import { getDefinitions } from "./storage";

export type EvaluationResponseBody = GenericEvaluationResponseBody<AppFlags>;

export const getFlags = createGetFlags<AppFlags>(config);
export const getFlags = createGetFlags<AppFlags>(config, {
getDefinitions,
serverTiming: true,
});
11 changes: 11 additions & 0 deletions example/flags/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { GetDefinitions, Definitions } from "@happykit/flags/api-route";
import { get } from "@vercel/edge-config";

export const getDefinitions: GetDefinitions = async (
projectId,
envKey,
environment
) => {
const definitions = await get<Definitions>(`happykit_v1_${projectId}`);
return definitions ?? null;
};
10 changes: 10 additions & 0 deletions example/pages/api/flags/[envKey].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createApiRoute } from "@happykit/flags/api-route";
import { getDefinitions } from "flags/storage";

export const config = { runtime: "experimental-edge" };

// This route can be used by the browser/client to evaluate feature flags
//
// Defining it on the application itself means even client-side evaluations
// will happen on the same domain, saving you DNS lookups.
export default createApiRoute({ getDefinitions, serverTiming: true });
2 changes: 1 addition & 1 deletion example/pages/docs/public-api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function curl(literals: TemplateStringsArray, ...placeholders: any[]) {

export default function Page() {
const endpoint = [
process.env.NEXT_PUBLIC_FLAGS_ENDPOINT,
"https://happykit.dev/api/flags",
process.env.NEXT_PUBLIC_FLAGS_ENV_KEY,
].join("/");

Expand Down