Repository navigation
feat(vue): add @tryabby/vue integration (#68) #238
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
base: main
Are you sure you want to change the base?
Changes from all commits
c64ae9d
b2c50e1
f014623
df14d80
0d18709
582d186
53d3a65
50971d6
9dbbe9d
ef6e95a
db91bfb
acb9fb9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@tryabby/vue": minor | ||
| --- | ||
|
|
||
| Add `@tryabby/vue`, a Vue 3 / Nuxt integration with `useAbby`, `useFeatureFlag`, `useRemoteConfig` and an `AbbyPlugin` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,209 @@ | ||
| import { Tab, Tabs, Callout } from "nextra-theme-docs"; | ||
|
|
||
| # Vue | ||
|
|
||
| Our Vue integration works with Vue 3.3 or later. It works with SPAs as well as server side rendered apps (e.g. Nuxt). | ||
|
|
||
| ## Installation | ||
|
|
||
| To get started make sure to install the package using your favorite package manager. | ||
|
|
||
| <Tabs items={["npm", "yarn", "pnpm"]}> | ||
| <Tab> | ||
| ```bash | ||
| npm i @tryabby/vue | ||
| ``` | ||
| </Tab> | ||
| <Tab> | ||
| ```bash | ||
| yarn add @tryabby/vue | ||
| ``` | ||
| </Tab> | ||
| <Tab> | ||
| ```bash | ||
| pnpm i @tryabby/vue | ||
| ``` | ||
| </Tab> | ||
| </Tabs> | ||
|
|
||
| ### Create your config | ||
|
|
||
| To use Abby you need to create your config first. You can do this by creating a file called `abby.config.ts` in your `root` folder. | ||
|
|
||
| ```ts | ||
| // abby.config.ts | ||
| import { defineConfig } from "@tryabby/vue"; | ||
|
|
||
| export default defineConfig({ | ||
| projectId: "<YOUR_PROJECT_ID>", | ||
| currentEnvironment: process.env.NODE_ENV, | ||
| environments: ["production", "development"], | ||
| tests: { | ||
| footer: { variants: ["dark", "orange", "green"] }, | ||
| }, | ||
| flags: ["darkMode", "newFeature"], | ||
| remoteConfig: { | ||
| customButtonText: "String", | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| ### Create your Instance | ||
|
|
||
| `createAbby` returns a typed Vue plugin and typed composables. | ||
|
|
||
| ```ts | ||
| // abby.ts | ||
| import { createAbby } from "@tryabby/vue"; | ||
| import abbyConfig from "../abby.config"; | ||
|
|
||
| export const { | ||
| AbbyPlugin, | ||
| useAbby, | ||
| useFeatureFlag, | ||
| useRemoteConfig, | ||
| getInitialData, | ||
| withDevtools, | ||
| } = createAbby(abbyConfig); | ||
|
Comment on lines
+60
to
+67
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Export the advertised helpers. The guide says readers can use
|
||
| ``` | ||
|
|
||
| ### Install the plugin | ||
|
|
||
| ```ts | ||
| // main.ts | ||
| import { createApp } from "vue"; | ||
| import App from "./App.vue"; | ||
| import { AbbyPlugin } from "./abby"; | ||
|
|
||
| createApp(App).use(AbbyPlugin).mount("#app"); | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| All composables return refs (`ComputedRef`), so they update automatically once the project data is loaded or changes. | ||
|
|
||
| ### useAbby | ||
|
|
||
| `useAbby` returns an object with the following values: | ||
|
|
||
| - `variant` - A ref with the variant that is currently active | ||
| - `onAct` - A function that you can call to trigger any kind of interaction with the tested component (will be used to track conversions) | ||
|
|
||
| ```vue | ||
| <script setup lang="ts"> | ||
| import { useAbby } from "./abby"; | ||
|
|
||
| const { variant, onAct } = useAbby("footer"); | ||
| </script> | ||
|
|
||
| <template> | ||
| <button | ||
| :class="{ | ||
| 'bg-gray-900': variant === 'dark', | ||
| 'bg-orange-500': variant === 'orange', | ||
| 'bg-green-500': variant === 'green', | ||
| }" | ||
| @click="onAct" | ||
| > | ||
| Cool Button | ||
| </button> | ||
| </template> | ||
| ``` | ||
|
|
||
| Optionally, you can pass a lookup object to automatically map the selected variant to a custom value. | ||
|
|
||
| ```ts | ||
| const { variant, onAct } = useAbby("footer", { | ||
| dark: "bg-gray-900", | ||
| orange: "bg-orange-500", | ||
| green: "bg-green-500", | ||
| }); | ||
| ``` | ||
|
|
||
| <Callout> | ||
| The variant is selected after the component is mounted (it's an empty string on the server), the same way the React integration avoids hydration mismatches. | ||
| </Callout> | ||
|
|
||
| ### useFeatureFlag | ||
|
|
||
| `useFeatureFlag` returns a boolean ref that indicates if the flag is active or not. | ||
|
|
||
| ```vue | ||
| <script setup lang="ts"> | ||
| import { useFeatureFlag } from "./abby"; | ||
|
|
||
| const hasNewFeature = useFeatureFlag("newFeature"); | ||
| </script> | ||
|
|
||
| <template> | ||
| <p v-if="hasNewFeature">Here is the new feature</p> | ||
| </template> | ||
| ``` | ||
|
|
||
| ### useRemoteConfig | ||
|
|
||
| `useRemoteConfig` returns a ref with the value of the given remote config variable. | ||
|
|
||
| ```vue | ||
| <script setup lang="ts"> | ||
| import { useRemoteConfig } from "./abby"; | ||
|
|
||
| const buttonText = useRemoteConfig("customButtonText"); | ||
| </script> | ||
|
|
||
| <template> | ||
| <button>{{ buttonText }}</button> | ||
| </template> | ||
| ``` | ||
|
|
||
| `useFeatureFlags` and `useRemoteConfigVariables` return all flags / remote config variables, and | ||
| `getFeatureFlagValue`, `getRemoteConfig`, `getABTestValue`, `getVariants` and `getABResetFunction` can be used outside of components. | ||
|
|
||
| ## Nuxt | ||
|
|
||
| Fetch the project data on the server with `getInitialData` and pass it to the plugin, so the page is rendered with the right flags and the client doesn't fetch the data again. | ||
|
|
||
| ```ts | ||
| // plugins/abby.ts | ||
| import { AbbyPlugin, getInitialData } from "~/abby"; | ||
|
|
||
| export default defineNuxtPlugin(async (nuxtApp) => { | ||
| const { data } = await useAsyncData("abby", () => getInitialData()); | ||
| nuxtApp.vueApp.use(AbbyPlugin, { initialData: data.value ?? undefined }); | ||
| }); | ||
| ``` | ||
|
|
||
| ## Devtools | ||
|
|
||
| Install the devtools package. `@tryabby/vue` does not install it: | ||
|
|
||
| <Tabs items={["npm", "yarn", "pnpm"]}> | ||
| <Tab> | ||
| ```bash | ||
| npm i @tryabby/devtools | ||
| ``` | ||
| </Tab> | ||
| <Tab> | ||
| ```bash | ||
| yarn add @tryabby/devtools | ||
| ``` | ||
| </Tab> | ||
| <Tab> | ||
| ```bash | ||
| pnpm i @tryabby/devtools | ||
| ``` | ||
| </Tab> | ||
| </Tabs> | ||
|
|
||
| ```vue | ||
| <script setup lang="ts"> | ||
| import abbyDevtools from "@tryabby/devtools"; | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| import { withDevtools } from "./abby"; | ||
|
|
||
| const AbbyDevtools = withDevtools(abbyDevtools, {}); | ||
| </script> | ||
|
|
||
| <template> | ||
| <AbbyDevtools /> | ||
| </template> | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { parseCookies } from "../src/helpers"; | ||
|
|
||
| describe("parseCookies", () => { | ||
| it("skips cookies that fail to decode and keeps valid ones", () => { | ||
| const cookies = parseCookies("ok=hello; broken=100%; also=world"); | ||
| expect(cookies.ok).toBe("hello"); | ||
| expect(cookies.also).toBe("world"); | ||
| expect(cookies.broken).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("parses ordinary cookies", () => { | ||
| const cookies = parseCookies("foo=bar; baz=qux"); | ||
| expect(cookies).toEqual({ foo: "bar", baz: "qux" }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| dist |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # @tryabby/vue | ||
|
|
||
| Vue 3 (and Nuxt) integration for [Abby](https://www.tryabby.com): A/B tests, feature flags and remote config with full type inference. | ||
|
|
||
| ```ts | ||
| import { createAbby } from "@tryabby/vue"; | ||
|
|
||
| export const { AbbyPlugin, useAbby, useFeatureFlag, useRemoteConfig } = | ||
| createAbby({ | ||
| projectId: "<YOUR_PROJECT_ID>", | ||
| currentEnvironment: "development", | ||
| environments: ["development", "production"], | ||
| tests: { footer: { variants: ["dark", "light"] } }, | ||
| flags: ["newFeature"], | ||
| remoteConfig: { buttonText: "String" }, | ||
| }); | ||
|
|
||
| // main.ts | ||
| createApp(App).use(AbbyPlugin).mount("#app"); | ||
|
|
||
| // in a component | ||
| const { variant, onAct } = useAbby("footer"); // ComputedRef<"dark" | "light"> | ||
| const newFeature = useFeatureFlag("newFeature"); // ComputedRef<boolean> | ||
| const buttonText = useRemoteConfig("buttonText"); // ComputedRef<string> | ||
| ``` | ||
|
|
||
| See the [docs](https://docs.tryabby.com/integrations/vue) for SSR / Nuxt usage. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| { | ||
| "name": "@tryabby/vue", | ||
| "version": "0.0.1", | ||
| "description": "Vue 3 integration for Abby (A/B tests, feature flags and remote config)", | ||
| "main": "dist/index.js", | ||
| "files": ["dist"], | ||
| "module": "dist/index.mjs", | ||
| "types": "dist/index.d.ts", | ||
| "exports": { | ||
| ".": { | ||
| "types": "./dist/index.d.ts", | ||
| "import": "./dist/index.mjs", | ||
| "require": "./dist/index.js" | ||
| } | ||
| }, | ||
| "scripts": { | ||
| "build": "tsup src/", | ||
| "dev": "pnpm run build --watch", | ||
| "test": "vitest", | ||
| "test:types": "tsc --noEmit" | ||
| }, | ||
| "homepage": "https://docs.tryabby.com", | ||
| "keywords": ["abby", "vue", "nuxt", "ab-testing", "feature-flags"], | ||
| "author": "", | ||
| "license": "ISC", | ||
| "devDependencies": { | ||
| "@tryabby/devtools": "workspace:*", | ||
| "@types/js-cookie": "^3.0.3", | ||
| "@vue/test-utils": "^2.4.6", | ||
| "jsdom": "^20.0.3", | ||
| "msw": "^0.49.1", | ||
| "node-fetch": "^3.3.0", | ||
| "tsconfig": "workspace:*", | ||
| "tsup": "^6.5.0", | ||
| "typescript": "5.5.4", | ||
| "vite": "5.4.0", | ||
| "vitest": "2.0.5", | ||
| "vue": "^3.4.0" | ||
| }, | ||
| "peerDependencies": { | ||
| "vue": "^3.3.0" | ||
| }, | ||
| "dependencies": { | ||
| "@tryabby/core": "workspace:*", | ||
| "js-cookie": "^3.0.1" | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Vue config example passes one argument to
defineConfig.@tryabby/vuere-exportsdefineConfigfrom@tryabby/core. That function requires two arguments: a dynamic config and a second config object. The example passes one object, so TypeScript reports an arity error when a user type-checks this file. The runtime result is unchanged, but a user who copies the setup gets a compile error.Split the keys between the two arguments according to the
DynamicConfigKeystype in@tryabby/core.