Skip to content

Commit

Permalink
feat: support feature flags
Browse files Browse the repository at this point in the history
  • Loading branch information
typeofweb committed Jan 5, 2023
1 parent 7f77a5d commit 4118874
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 77 deletions.
2 changes: 2 additions & 0 deletions apps/app/.env
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
NEXT_PUBLIC_APP_URL=https://${VERCEL_URL}

FLAGSMITH_SERVER_SIDE_ENVIRONMENT_KEY=
2 changes: 2 additions & 0 deletions apps/app/.env.local-example
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
NEXT_PUBLIC_API_URL=http://api.devfaq.localhost:3002
NEXT_PUBLIC_APP_URL=http://app.devfaq.localhost:3000

FLAGSMITH_SERVER_SIDE_ENVIRONMENT_KEY=
3 changes: 3 additions & 0 deletions apps/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"@tanstack/react-query-devtools": "4.20.4",
"client-only": "0.0.1",
"easymde": "2.18.0",
"flagsmith": "3.15.1",
"flagsmith-nodejs": "2.5.0",
"next": "13.1.1",
"next-mdx-remote": "4.2.0",
"openapi-typescript-fetch": "1.1.3",
Expand All @@ -29,6 +31,7 @@
"react-focus-lock": "2.9.2",
"rehype-prism-plus": "1.5.0",
"remark": "14.0.2",
"server-only": "0.0.1",
"strip-markdown": "5.0.0",
"tailwind-merge": "1.8.1"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { SingleQuestion } from "../../../../../components/SingleQuestion";
import { serializeQuestionToMarkdown } from "../../../../../lib/question";
import { getQuestionAnswers, getQuestionById } from "../../../../../services/questions.service";
import { Params } from "../../../../../types";
import { isFlagEnabled } from "../../../../../services/flagsmith.service";

export default async function SingleQuestionPage({ params }: { params: Params<"questionId"> }) {
const questionId = Number.parseInt(params.questionId);
Expand All @@ -13,14 +14,20 @@ export default async function SingleQuestionPage({ params }: { params: Params<"q
return redirect("/");
}

const [questionData, answersData] = await Promise.all([
const [questionData, answersData, questionAnswersEnabled] = await Promise.all([
getQuestionById({
id: questionId,
}),
getQuestionAnswers({ id: questionId }),
isFlagEnabled("question_answers"),
]);

const question = await serializeQuestionToMarkdown(questionData.data.data);

if (!questionAnswersEnabled) {
return <SingleQuestion question={question} />;
}

const answers = await Promise.all(
answersData.data.data.map(async ({ content, ...rest }) => {
const mdxContent = await serializeSource(content);
Expand Down
34 changes: 34 additions & 0 deletions apps/app/src/services/flagsmith.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import "server-only";

import Flagsmith from "flagsmith-nodejs";
import { BaseFlag } from "flagsmith-nodejs/build/sdk/models";

type Flag = "question_answers";
const defaultFlags: Record<Flag, BaseFlag> = {
question_answers: { enabled: true, value: undefined, isDefault: true },
};

const getFlagsmith = async () => {
if (!process.env.FLAGSMITH_SERVER_SIDE_ENVIRONMENT_KEY) {
console.warn(`FLAGSMITH_SERVER_SIDE_ENVIRONMENT_KEY not provided.`);
return null;
}

const flagsmith = new Flagsmith({
environmentKey: process.env.FLAGSMITH_SERVER_SIDE_ENVIRONMENT_KEY,
});
const flags = await flagsmith.getEnvironmentFlags();
return flags;
};

const getFlag = async <F extends Flag>(flag: F) => {
const flagsmith = await getFlagsmith();
if (!flagsmith) {
return defaultFlags[flag];
}
return flagsmith.getFlag(flag);
};

export const isFlagEnabled = async <F extends Flag>(flag: F) => {
return (await getFlag(flag)).enabled;
};
Loading

0 comments on commit 4118874

Please sign in to comment.