diff --git a/packages/vue/__tests__/index.test.ts b/packages/vue/__tests__/index.test.ts new file mode 100644 index 00000000..e4b50ad4 --- /dev/null +++ b/packages/vue/__tests__/index.test.ts @@ -0,0 +1,33 @@ +import { describe, it, expect, vi } from 'vitest' +import { AbbyProvider, useAbby, useFeatureFlag, useRemoteConfig } from '../index' + +describe('Vue Integration', () => { + it('should create AbbyProvider', () => { + const provider = AbbyProvider + expect(provider).toBeDefined() + expect(provider.name).toBe('AbbyProvider') + }) + + describe('useAbby', () => { + it('should return variant and onAct', () => { + const { variant, onAct } = useAbby() + expect(variant).toBeDefined() + expect(onAct).toBeDefined() + expect(typeof onAct).toBe('function') + }) + }) + + describe('useFeatureFlag', () => { + it('should return feature flag value', () => { + const flagValue = useFeatureFlag('test-flag') + expect(flagValue).toBeDefined() + }) + }) + + describe('useRemoteConfig', () => { + it('should return remote config value', () => { + const configValue = useRemoteConfig('test-config') + expect(configValue).toBeDefined() + }) + }) +}) \ No newline at end of file diff --git a/packages/vue/index.ts b/packages/vue/index.ts new file mode 100644 index 00000000..5e2c792c --- /dev/null +++ b/packages/vue/index.ts @@ -0,0 +1,95 @@ +import { defineComponent, h, ref, provide, inject, onMounted, onBeforeUnmount } from 'vue' +import { initAbby, AbbyClient, getVariant, getFeatureFlag, getRemoteConfig, trackAction } from '@tryabby/core' + +interface AbbyProviderProps { + projectId: string + environmentId: string + apiKey: string +} + +const ABBY_SYMBOL = Symbol('abbyClient') + +const AbbyProvider = defineComponent({ + name: 'AbbyProvider', + props: { + projectId: { type: String, required: true }, + environmentId: { type: String, required: true }, + apiKey: { type: String, required: true } + }, + setup(props, { slots }) { + const abbyClient = ref(null) + + onMounted(() => { + abbyClient.value = initAbby({ + projectId: props.projectId, + environmentId: props.environmentId, + apiKey: props.apiKey + }) + }) + + onBeforeUnmount(() => { + if (abbyClient.value && typeof abbyClient.value.destroy === 'function') { + abbyClient.value.destroy() + } + }) + + provide(ABBY_SYMBOL, abbyClient) + + return () => slots.default ? slots.default() : h('div') + } +}) + +const useAbby = (experimentName?: string) => { + const abbyClient = inject(ABBY_SYMBOL, ref(null)) + const variant = ref('control') + + onMounted(() => { + if (abbyClient.value && experimentName) { + const v = getVariant(abbyClient.value, experimentName) + if (v) variant.value = v + } + }) + + const onAct = (action: string, data?: Record) => { + if (abbyClient.value) { + trackAction(abbyClient.value, action, data) + } else { + console.log(`Tracking action: ${action}`, data) + } + } + + return { variant, onAct } +} + +const useFeatureFlag = (flagName: string) => { + const abbyClient = inject(ABBY_SYMBOL, ref(null)) + const flagValue = ref(false) + + onMounted(() => { + if (abbyClient.value) { + flagValue.value = getFeatureFlag(abbyClient.value, flagName) + } + }) + + return flagValue +} + +const useRemoteConfig = (configName: string) => { + const abbyClient = inject(ABBY_SYMBOL, ref(null)) + const configValue = ref({}) + + onMounted(() => { + if (abbyClient.value) { + configValue.value = getRemoteConfig(abbyClient.value, configName) + } + }) + + return configValue +} + +export { + AbbyProvider, + useAbby, + useFeatureFlag, + useRemoteConfig +} \ No newline at end of file diff --git a/packages/vue/package.json b/packages/vue/package.json new file mode 100644 index 00000000..763aba6c --- /dev/null +++ b/packages/vue/package.json @@ -0,0 +1,17 @@ +{ + "name": "@tryabby/vue", + "version": "1.0.0", + "description": "Vue 3 integration for Abby", + "main": "index.ts", + "types": "index.ts", + "dependencies": { + "@tryabby/core": "^1.0.0", + "vue": "^3.2.0" + }, + "devDependencies": { + "vitest": "^0.10.0" + }, + "scripts": { + "test": "vitest" + } +} \ No newline at end of file