0.1.1 • Published 3 years ago
@happykit/remix v0.1.1
@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
.envand paste your key there
It should look like this:
HAPPYKIT_FLAGS_ENV_KEY=flags_pub_356397495635411543You 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-traitswhich shows how to target users by traitswith-userwhich shows how to target individual userswith-visitor-keywhich shows how to target anonymous userswith-everythingwhich shows how to combine it all together
API Documentation
import { generateVisitorKey, getFlags } from "@happykit/remix"getFlags(options: Options): FlagBag- Arguments
options.visitorKey: string | undefinedA 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 thevisitorKeypresent on the request's cookies or to generate a new one.options.user: User | undefinedA user to load the flags for. A user must at least have a key.option.usersupports 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> | undefinedAn object which you have access to in the flag's rules. Unlike withoptions.usertheoptions.traitsattribute 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 } | undefinedReturns an object containing the visitor key used to evaluate the feature flags. Only returned ifoptions.visitorKeywas set.flagBag.cookie: { "Set-Cookie": string } | undefined: Returns an object you can pass toRequest.headersto set the thevisitorKeycookie. Only returned ifoptions.visitorKeywas set.
- Arguments
generateVisitorKey(request: Request): stringIf the request contains a header for thevisitorKeythis function returns thatvisitorKey. Otherwise it generates a newvisitorKeyand returns that instead. The cookie for thevisitorKeyis 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.