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
3 changes: 3 additions & 0 deletions packages/vue/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
dist
.turbo
122 changes: 122 additions & 0 deletions packages/vue/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# `@tryabby/vue`

Official Vue 3 & Nuxt 3 integration for [Abby](https://tryabby.com) — Feature Flags, A/B Testing, and Remote Config with full TypeScript inference.

## Installation

```bash
pnpm add @tryabby/vue
# or
npm install @tryabby/vue
```

## Quick Start (Vue 3)

### 1. Initialize Abby (`abby.ts`)

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

export const {
AbbyProvider,
abbyPlugin,
useAbby,
useFeatureFlag,
useFeatureFlags,
useRemoteConfig,
useRemoteConfigVariables,
getFeatureFlagValue,
getRemoteConfig,
getABTestValue,
getVariants,
getABResetFunction,
withDevtools,
withAbby,
} = createAbby({
projectId: "<YOUR_PROJECT_ID>",
currentEnvironment: "production",
environments: ["production", "development"],
tests: {
heroBanner: {
variants: ["control", "variantB"],
},
},
flags: ["darkMode", "betaCheckout"],
remoteConfig: {
welcomeHeading: "String",
maxItems: "Number",
},
});
```

### 2. Provide Abby in your application

Wrap your root component with `<AbbyProvider>` **or** register `abbyPlugin` on your Vue app instance:

```vue
<!-- App.vue -->
<script setup lang="ts">
import { AbbyProvider } from "./abby";
</script>

<template>
<AbbyProvider>
<RouterView />
</AbbyProvider>
</template>
```

Or via `app.use(abbyPlugin)` in `main.ts`:

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

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

### 3. Use Composables in Components

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

const { variant, onAct } = useAbby("heroBanner");
const isDarkMode = useFeatureFlag("darkMode");
const welcomeHeading = useRemoteConfig("welcomeHeading");
</script>

<template>
<section :class="{ dark: isDarkMode }">
<h1>{{ welcomeHeading }}</h1>
<button v-if="variant === 'variantB'" @click="onAct">
Try Variant B
</button>
<button v-else @click="onAct">
Default Action
</button>
</section>
</template>
```

## Nuxt 3 / SSR Integration

Use `withAbby` on the server or pass `initialData` to `abbyPlugin` / `<AbbyProvider>`:

```ts
// plugins/abby.ts
import { abbyPlugin, withAbby } from "~/utils/abby";

export default defineNuxtPlugin(async (nuxtApp) => {
if (import.meta.server) {
const loader = withAbby();
nuxtApp.payload.abby = await loader(nuxtApp.ssrContext?.event);
}

nuxtApp.vueApp.use(abbyPlugin, {
initialData: nuxtApp.payload.abby?.__abby__data,
cookies: nuxtApp.payload.abby?.__abby_cookie,
});
});
```
48 changes: 48 additions & 0 deletions packages/vue/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "@tryabby/vue",
"version": "1.0.0",
"description": "Vue 3 and Nuxt 3 integration for Abby",
"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"
},
"homepage": "https://docs.tryabby.com",
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@testing-library/jest-dom": "^5.16.5",
"@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.38"
},
"peerDependencies": {
"vue": "^3.3.0"
},
"dependencies": {
"@tryabby/core": "workspace:*",
"@tryabby/devtools": "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