Skip to content

feat: update OpenFeature React example #16

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions open-feature/react/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copy this file to a new file ".env" and update with
# the client-side key you create from your own account.
REACT_APP_FLAGSMITH_ENVIRONMENT_ID="YOUR_FLAGSMITH_CLIENT_SIDE_ENVIRONMENT_KEY"

# If you're using a different Flagsmith endpoint (for example,
# using a local docker compose container), set it here.
REACT_APP_FLAGSMITH_API="https://edge.api.flagsmith.com/api/v1/flags/"
1 change: 1 addition & 0 deletions open-feature/react/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

# misc
.DS_Store
.env
.env.local
.env.development.local
.env.test.local
Expand Down
1 change: 1 addition & 0 deletions open-feature/react/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v22
3 changes: 3 additions & 0 deletions open-feature/react/.prettierrc
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For prettier formatting (automatic line wrap) on .md files.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"proseWrap": "always"
}
27 changes: 18 additions & 9 deletions open-feature/react/README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
<img width="100%" src="https://raw.githubusercontent.com/Flagsmith/flagsmith/main/static-files/hero.png"/>

## Flagsmith with React and OpenFeature
# Flagsmith with React and OpenFeature

## Setup

## Available Scripts
To set up your local environment, first copy `.env.example` to `.env` and
populate your Flagsmith environment ID. Optionally, specify the API endpoint
that you want to use to retrieve the value of your feature flags.

> Note: if you attempt to run the development environment without populating the
> `.env` file correctly, you will encounter an error in the terminal.

In the project directory, you can run:
## Available Scripts

### `npm start`
In the project directory, you can run `npm start` to run the app in the
development mode.

Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
This should attempt to open [http://localhost:3000](http://localhost:3000) to
view it in the browser.

The page will reload if you make edits.\
You will also see any lint errors in the console.
The page will reload if you make edits. You will also see any lint errors in the
console.

## OpenFeature

Please refer to the OpenFeature Documentation](https://openfeature.dev/docs/reference/technologies/client/web/react) for further information.
Please refer to the OpenFeature
Documentation](https://openfeature.dev/docs/reference/technologies/client/web/react)
for further information.
38 changes: 38 additions & 0 deletions open-feature/react/checkEnv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const fs = require("fs");
const path = require("path");

try {
/**
* Checks if the .env file exists.
*/
const envPath = path.join(process.cwd(), ".env");
if (!fs.existsSync(envPath)) {
console.error("\x1b[31m%s\x1b[0m", "Error: .env file not found!");
console.error(
"\x1b[33m%s\x1b[0m",
"Please copy .env.example to .env and configure your environment variables."
);
console.error("\x1b[36m%s\x1b[0m", "You can do this by running:");
console.error("\x1b[37m%s\x1b[0m", "cp .env.example .env");
process.exit(1);
}

/**
* Checks if the Flagsmith environment key has been configured.
*/
const envContent = fs.readFileSync(envPath, "utf8");
if (envContent.includes("YOUR_FLAGSMITH_CLIENT_SIDE_ENVIRONMENT_KEY")) {
console.error(
"\x1b[31m%s\x1b[0m",
"Error: Flagsmith environment key not configured!"
);
console.error(
"\x1b[33m%s\x1b[0m",
"Please replace YOUR_FLAGSMITH_CLIENT_SIDE_ENVIRONMENT_KEY in your .env file with your actual Flagsmith environment key."
);
process.exit(1);
}
} catch (err) {
console.error("Error checking for .env file:", err);
process.exit(1);
}
Binary file added open-feature/react/favicon.ico
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I grabbed the favicon from the main Flagsmith repo.

Binary file not shown.
16 changes: 16 additions & 0 deletions open-feature/react/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta name="description" content="OpenFeature React Demo" />
<title>OpenFeature React Demo</title>
<script type="module" src="src/index"></script>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>
Loading