-
Notifications
You must be signed in to change notification settings - Fork 29
feat: add @tryabby/vue integration (#68) #189
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
fadhelcarlos
wants to merge
2
commits into
tryabby:main
Choose a base branch
from
fadhelcarlos:feat/vue-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 for Abby. It ships fully-typed `useAbby`, `useFeatureFlag`, and `useRemoteConfig` composables, an `AbbyProvider` plugin for data loading/SSR, and non-reactive getters — with the same API surface and type quality as the React and Svelte packages. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| dist |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| # @tryabby/vue | ||
|
|
||
| The official [Abby](https://www.tryabby.com) integration for **Vue 3** and **Nuxt**. | ||
|
|
||
| It gives you fully-typed composables for A/B tests, feature flags and remote config, with the same API surface and type quality as the React and Svelte packages. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| npm install @tryabby/vue | ||
| # or | ||
| pnpm add @tryabby/vue | ||
| ``` | ||
|
|
||
| ## Quick start | ||
|
|
||
| Create your Abby instance once and export the composables from it: | ||
|
|
||
| ```ts | ||
| // abby.ts | ||
| import { createAbby } from "@tryabby/vue"; | ||
|
|
||
| export const { | ||
| useAbby, | ||
| useFeatureFlag, | ||
| useRemoteConfig, | ||
| AbbyProvider, | ||
| getFeatureFlagValue, | ||
| getRemoteConfig, | ||
| getABTestValue, | ||
| } = createAbby({ | ||
| projectId: "<YOUR_PROJECT_ID>", | ||
| currentEnvironment: import.meta.env.MODE, | ||
| environments: ["development", "production"], | ||
| tests: { | ||
| "footer-test": { variants: ["old", "new"] }, | ||
| }, | ||
| flags: ["new-dashboard"], | ||
| remoteConfig: { theme: "String" }, | ||
| }); | ||
| ``` | ||
|
|
||
| Register the `AbbyProvider` plugin on your app. It loads your project data on | ||
| the client (and accepts server-fetched `initialData` for SSR): | ||
|
|
||
| ```ts | ||
| // main.ts | ||
| import { createApp } from "vue"; | ||
| import App from "./App.vue"; | ||
| import { AbbyProvider } from "./abby"; | ||
|
|
||
| createApp(App).use(AbbyProvider).mount("#app"); | ||
| ``` | ||
|
|
||
| ## Composables | ||
|
|
||
| ### `useAbby` | ||
|
|
||
| Returns the reactive variant for a test and an `onAct` callback to report an | ||
| interaction. The variant is a `ComputedRef`, so it works in templates directly. | ||
|
|
||
| ```vue | ||
| <script setup lang="ts"> | ||
| import { useAbby } from "./abby"; | ||
|
|
||
| const { variant, onAct } = useAbby("footer-test"); | ||
| </script> | ||
|
|
||
| <template> | ||
| <NewFooter v-if="variant === 'new'" @click="onAct" /> | ||
| <OldFooter v-else @click="onAct" /> | ||
| </template> | ||
| ``` | ||
|
|
||
| You can also pass a lookup object to map each variant to a value: | ||
|
|
||
| ```ts | ||
| const { variant } = useAbby("footer-test", { old: 0, new: 1 }); | ||
| // variant.value is 0 | 1 | ||
| ``` | ||
|
|
||
| ### `useFeatureFlag` | ||
|
|
||
| ```vue | ||
| <script setup lang="ts"> | ||
| import { useFeatureFlag } from "./abby"; | ||
|
|
||
| const showDashboard = useFeatureFlag("new-dashboard"); // Ref<boolean> | ||
| </script> | ||
| ``` | ||
|
|
||
| ### `useRemoteConfig` | ||
|
|
||
| ```vue | ||
| <script setup lang="ts"> | ||
| import { useRemoteConfig } from "./abby"; | ||
|
|
||
| const theme = useRemoteConfig("theme"); // Ref<string> | ||
| </script> | ||
| ``` | ||
|
|
||
| ## Non-reactive helpers | ||
|
|
||
| For places outside of a component's reactive scope (route guards, plain | ||
| modules, event handlers) the instance also exposes plain getters: | ||
|
|
||
| - `getFeatureFlagValue(name)` – the current value of a feature flag | ||
| - `getRemoteConfig(name)` – the current value of a remote config entry | ||
| - `getABTestValue(name, lookup?)` – the currently selected variant of a test | ||
| - `getVariants(name)` – all variants of a test | ||
| - `getABResetFunction(name)` – resets the persisted variant of a test | ||
| - `updateUserProperties(user)` – updates user properties for targeting | ||
|
|
||
| ## Nuxt / SSR | ||
|
|
||
| To avoid hydration mismatches, `useAbby` renders an empty variant on the server | ||
| and resolves the real variant on mount. When you fetch project data on the | ||
| server, hand it to the provider so flags and remote config are available during | ||
| the first render: | ||
|
|
||
| ```ts | ||
| app.use(AbbyProvider, { initialData }); | ||
| ``` | ||
|
|
||
| ## License | ||
|
|
||
| ISC |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| { | ||
| "name": "@tryabby/vue", | ||
| "version": "0.1.0", | ||
| "description": "Vue 3 & Nuxt integration for Abby", | ||
| "main": "dist/index.js", | ||
| "files": ["dist"], | ||
| "module": "dist/index.mjs", | ||
| "types": "dist/index.d.ts", | ||
| "homepage": "https://docs.tryabby.com", | ||
| "exports": { | ||
| ".": { | ||
| "types": "./dist/index.d.ts", | ||
| "import": "./dist/index.mjs", | ||
| "require": "./dist/index.js" | ||
| } | ||
| }, | ||
| "scripts": { | ||
| "build": "tsup src/index.ts", | ||
| "dev": "pnpm run build --watch", | ||
| "test": "vitest", | ||
| "typecheck": "tsc --noEmit" | ||
| }, | ||
| "keywords": ["abby", "vue", "nuxt", "feature-flags", "ab-testing"], | ||
| "author": "", | ||
| "license": "ISC", | ||
| "peerDependencies": { | ||
| "vue": "^3.3.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@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" | ||
| }, | ||
| "dependencies": { | ||
| "@tryabby/core": "workspace:*", | ||
| "js-cookie": "^3.0.5" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { | ||
| type IStorageService, | ||
| type StorageServiceOptions, | ||
| getABStorageKey, | ||
| getFFStorageKey, | ||
| getRCStorageKey, | ||
| } from "@tryabby/core"; | ||
| import Cookie from "js-cookie"; | ||
|
|
||
| class ABStorageService implements IStorageService { | ||
| get(projectId: string, testName: string): string | null { | ||
| const retrievedValue = Cookie.get(getABStorageKey(projectId, testName)); | ||
| if (!retrievedValue) return null; | ||
|
|
||
| return retrievedValue; | ||
| } | ||
|
|
||
| set( | ||
| projectId: string, | ||
| testName: string, | ||
| value: string, | ||
| options?: StorageServiceOptions | ||
| ): void { | ||
| Cookie.set(getABStorageKey(projectId, testName), value, { | ||
| expires: options?.expiresInDays ? options.expiresInDays : 365, | ||
| }); | ||
| } | ||
|
|
||
| remove(projectId: string, testName: string): void { | ||
| Cookie.remove(getABStorageKey(projectId, testName)); | ||
| } | ||
| } | ||
|
|
||
| class FFStorageService implements IStorageService { | ||
| get(projectId: string, flagName: string): string | null { | ||
| const retrievedValue = Cookie.get(getFFStorageKey(projectId, flagName)); | ||
| if (!retrievedValue) return null; | ||
|
|
||
| return retrievedValue; | ||
| } | ||
|
|
||
| set(projectId: string, flagName: string, value: string): void { | ||
| Cookie.set(getFFStorageKey(projectId, flagName), value); | ||
| } | ||
|
|
||
| remove(projectId: string, flagName: string): void { | ||
| Cookie.remove(getFFStorageKey(projectId, flagName)); | ||
| } | ||
| } | ||
|
|
||
| class RCStorageService implements IStorageService { | ||
| get(projectId: string, key: string): string | null { | ||
| const retrievedValue = Cookie.get(getRCStorageKey(projectId, key)); | ||
| return retrievedValue ?? null; | ||
| } | ||
|
|
||
| set(projectId: string, key: string, value: string): void { | ||
| Cookie.set(getRCStorageKey(projectId, key), value); | ||
| } | ||
|
|
||
| remove(projectId: string, key: string): void { | ||
| Cookie.remove(getRCStorageKey(projectId, key)); | ||
| } | ||
| } | ||
|
|
||
| export const TestStorageService = new ABStorageService(); | ||
| export const FlagStorageService = new FFStorageService(); | ||
| export const RemoteConfigStorageService = new RCStorageService(); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win
Feature-flag/remote-config cookies are session-only, unlike AB test cookies.
ABStorageService.setpersists cookies for 365 days by default, butFFStorageService.set(Line 42-44) andRCStorageService.set(Line 57-59) callCookie.setwithout anexpiresoption, so js-cookie creates session cookies that vanish on browser close. This is an inconsistent persistence contract across the three storage types with no apparent intent behind the difference.🍪 Proposed fix to align persistence behavior
class FFStorageService implements IStorageService { get(projectId: string, flagName: string): string | null { const retrievedValue = Cookie.get(getFFStorageKey(projectId, flagName)); if (!retrievedValue) return null; return retrievedValue; } - set(projectId: string, flagName: string, value: string): void { - Cookie.set(getFFStorageKey(projectId, flagName), value); + set( + projectId: string, + flagName: string, + value: string, + options?: StorageServiceOptions + ): void { + Cookie.set(getFFStorageKey(projectId, flagName), value, { + expires: options?.expiresInDays ?? 365, + }); } remove(projectId: string, flagName: string): void { Cookie.remove(getFFStorageKey(projectId, flagName)); } }Apply the same pattern to
RCStorageService.set.Also applies to: 51-64
🤖 Prompt for AI Agents