@happykit/remix
v0.1.1
Published
Feature Flags for your Remix applications
Downloads
9
Maintainers
Readme
@happykit/remix
Feature Flags for your Remix applications
Get started
npm install --save @happykit/remix
Example
- Create a free account on happykit.dev
- Create a project
- In your project, go to Keys
- Copy the Development Environment Key
- Create a file called
.env
and paste your key there
It should look like this:
HAPPYKIT_FLAGS_ENV_KEY=flags_pub_356397495635411543
You can now load your feature flags in your routes:
import { useLoaderData } from "@remix-run/react";
import { getFlags } from "@happykit/remix";
// You can fully type your flags
type AppFlags = {
textColor: string
}
export async function loader() {
// this is how you load flags from happykit
const flagBag = await getFlags<AppFlags>();
// pass them to your application however you like
return { flags: flagBag.flags };
}
export default function Index() {
// you can access your flags on loaderData
const loaderData = useLoaderData<typeof loader>();
return (
<div>
<p style={{ color: loaderData.flags.textColor }}>
You are seeing {loaderData.flags.textColor}
</p>
</div>
);
}
Example Application
See apps/example
for an example application.
Check out the pages in apps/example/app
like
with-traits
which shows how to target users by traitswith-user
which shows how to target individual userswith-visitor-key
which shows how to target anonymous userswith-everything
which shows how to combine it all together
API Documentation
import { generateVisitorKey, getFlags } from "@happykit/remix"
getFlags(options: Options): FlagBag
- Arguments
options.visitorKey: string | undefined
A unique id for the current visitor. This can be used to consistently target an anonymous user. Be aware of GDPR regulation when using this feature. You can usegenerateVisitorKey(request)
to either return thevisitorKey
present on the request's cookies or to generate a new one.options.user: User | undefined
A user to load the flags for. A user must at least have a key.option.user
supports known attriutes only, see supported user attributes. The user information you pass can be used for individual targeting or rules. This is usually used for authenticated users your application actually knows about.options.traits: Record<string, JsonValue> | undefined
An object which you have access to in the flag's rules. Unlike withoptions.user
theoptions.traits
attribute lets you specify completely custom traits as long as they are JSON.serialize'able. You can then target your audience based on those traits using rules in HappyKit.
- Return Value
flagBag.flags: Record<string, JsonValue>
The evaluated feature flags, for example{ textColor: "blue", showSignup: true }
.flagBag.visitor: { key: string } | undefined
Returns an object containing the visitor key used to evaluate the feature flags. Only returned ifoptions.visitorKey
was set.flagBag.cookie: { "Set-Cookie": string } | undefined
: Returns an object you can pass toRequest.headers
to set the thevisitorKey
cookie. Only returned ifoptions.visitorKey
was set.
- Arguments
generateVisitorKey(request: Request): string
If the request contains a header for thevisitorKey
this function returns thatvisitorKey
. Otherwise it generates a newvisitorKey
and returns that instead. The cookie for thevisitorKey
is calledhkvk
.
Supported user attributes
Provide any of these attributes to store them in HappyKit. You will be able to use them for targeting specific users based on rules later on (not yet available in HappyKit Flags).
key
(string) required: Unique key for this useremail
(string): Email-Addressname
(string): Full name or nicknameavatar
(string): URL to users profile picturecountry
(string): Two-letter uppercase country-code of user's county, see ISO 3166-1
Disclaimer
Even though this package is currently extremely simple, it is a great starting point for your feature flags.
Feel free to open an issue in case you have additional feature requests.