Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/new-vue-integration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tryabby/vue": minor
---

Add `@tryabby/vue`, a Vue 3 integration with `useAbby`, `useFeatureFlag` and `useRemoteConfig`
114 changes: 114 additions & 0 deletions packages/vue/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# @tryabby/vue

Vue 3 integration for Abby: feature flags, A/B tests and remote config.

## Installation

```bash
npm install @tryabby/vue
```

## createAbby

`createAbby` takes the same config as `@tryabby/react` and `@tryabby/svelte` and returns the composables you use in your components.

```ts
// abby.ts
import { createAbby } from "@tryabby/vue";

export const {
abbyPlugin,
useAbby,
useFeatureFlag,
useRemoteConfig,
getABTestValue,
getFeatureFlagValue,
getRemoteConfig,
} = createAbby({
projectId: "<YOUR_PROJECT_ID>",
currentEnvironment: process.env.NODE_ENV,
environments: ["development", "production"],
tests: {
footer: { variants: ["OldFooter", "NewFooter"] },
},
flags: ["darkMode"],
remoteConfig: {
title: "String",
},
});
```

Install the plugin once. It loads the project data from Abby and keeps every composable up to date.

```ts
// main.ts
import { createApp } from "vue";
import App from "./App.vue";
import { abbyPlugin } from "./abby";

createApp(App).use(abbyPlugin).mount("#app");
```

If you already have the project data (for example on the server), pass it as `initialData` to skip the request:

```ts
app.use(abbyPlugin, { initialData });
```

## useAbby

Returns the selected variant of an A/B test as a `ComputedRef` and an `onAct` function.
The variant is an empty string until the component is mounted so server and client markup match.
Call `onAct` when something that uses the selected variant was shown or used, it sends an event to Abby.

```vue
<script setup lang="ts">
import { useAbby } from "./abby";

const { variant, onAct } = useAbby("footer");
</script>

<template>
<NewFooter v-if="variant === 'NewFooter'" @click="onAct" />
<OldFooter v-else-if="variant === 'OldFooter'" @click="onAct" />
</template>
```

You can pass a lookup object as the second argument to map variants to any value:

```ts
const { variant } = useAbby("footer", {
OldFooter: "old",
NewFooter: "new",
});
```

## useFeatureFlag

Returns the value of a feature flag as a `ComputedRef<boolean>`.

```vue
<script setup lang="ts">
import { useFeatureFlag } from "./abby";

const darkMode = useFeatureFlag("darkMode");
</script>

<template>
<div :class="{ dark: darkMode }">...</div>
</template>
```

`useFeatureFlags()` returns a `ComputedRef` with every flag as `{ name, value }`.

## useRemoteConfig

Returns the value of a remote config variable as a `ComputedRef` typed after your config (`"String"`, `"Number"` or `"JSON"`).

```ts
const title = useRemoteConfig("title");
```

## Outside of components

`getFeatureFlagValue(name)`, `getRemoteConfig(name)` and `getABTestValue(name, lookup?)` return the current value without reactivity. `getVariants(name)` returns all variants of a test and `getABResetFunction(name)` returns a function that clears the stored variant of a test.
46 changes: 46 additions & 0 deletions packages/vue/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "@tryabby/vue",
"version": "0.0.1",
"description": "Vue 3 integration for Abby (feature flags, A/B tests 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/index.ts",
"dev": "pnpm run build --watch",
"test": "vitest"
},
"homepage": "https://docs.tryabby.com",
"keywords": ["abby", "vue", "nuxt", "feature-flags", "ab-testing"],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/js-cookie": "^3.0.3",
"@vitejs/plugin-vue": "^5.1.2",
"@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"
}
}
76 changes: 76 additions & 0 deletions packages/vue/src/StorageService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import {
type IStorageService,
type StorageServiceOptions,
getABStorageKey,
getFFStorageKey,
getRCStorageKey,
} from "@tryabby/core";
import Cookie from "js-cookie";

const DEFAULT_COOKIE_AGE = 365;

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
: DEFAULT_COOKIE_AGE,
});
}

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, {
expires: DEFAULT_COOKIE_AGE,
});
}

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, {
expires: DEFAULT_COOKIE_AGE,
});
}

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();
Loading