diff --git a/packages/vue/package.json b/packages/vue/package.json new file mode 100644 index 00000000..677bee4a --- /dev/null +++ b/packages/vue/package.json @@ -0,0 +1,32 @@ +{ + "name": "@tryabby/vue", + "version": "0.0.1", + "description": "Vue integration for Abby", + "main": "./dist/index.js", + "module": "./dist/index.mjs", + "types": "./dist/index.d.ts", + "files": [ + "dist" + ], + "scripts": { + "build": "tsup src/index.ts --format cjs,esm --dts", + "dev": "tsup src/index.ts --format cjs,esm --watch --dts", + "lint": "biome lint .", + "test": "vitest run", + "test:watch": "vitest" + }, + "dependencies": { + "@tryabby/core": "workspace:*", + "vue": "^3.3.0" + }, + "devDependencies": { + "@vue/test-utils": "^2.4.1", + "jsdom": "^22.1.0", + "tsup": "^7.2.0", + "typescript": "^5.2.2", + "vitest": "^0.34.4" + }, + "publishConfig": { + "access": "public" + } +} \ No newline at end of file diff --git a/packages/vue/src/index.ts b/packages/vue/src/index.ts new file mode 100644 index 00000000..c121a9a1 --- /dev/null +++ b/packages/vue/src/index.ts @@ -0,0 +1,86 @@ +import { Abby, type AbbyConfig, type RemoteConfigValueStringToType } from "@tryabby/core"; +import { inject, provide, ref, onMounted, type InjectionKey, type Ref } from "vue"; + +const ABBY_SYMBOL: InjectionKey> = Symbol("abby"); + +export function createAbby< + TKey extends string = string, + TTests extends Record = Record, + TFlags extends readonly string[] = readonly string[], + TRemoteConfig extends Record = Record +>(config: AbbyConfig) { + const abby = new Abby(config); + + const AbbyProvider = { + name: "AbbyProvider", + setup(_props: any, { slots }: any) { + provide(ABBY_SYMBOL, abby); + return () => slots.default?.(); + }, + }; + + function useAbby( + testName: T, + lookup?: Record + ) { + const abbyInstance = inject(ABBY_SYMBOL); + if (!abbyInstance) { + throw new Error("useAbby must be used within an AbbyProvider"); + } + + const variant = ref(abbyInstance.getTestVariant(testName as string)); + + if (lookup) { + variant.value = lookup[variant.value as TTests[T]["variants"][number]]; + } + + onMounted(() => { + // Abby core handles the pinging and persistence + }); + + const onAct = () => { + abbyInstance.onAct(testName as string); + }; + + return { + variant: variant as Ref, + onAct, + }; + } + + function useFeatureFlag(flagName: F): Ref { + const abbyInstance = inject(ABBY_SYMBOL); + if (!abbyInstance) { + throw new Error("useFeatureFlag must be used within an AbbyProvider"); + } + + const isEnabled = ref(abbyInstance.getFeatureFlag(flagName)); + + return isEnabled; + } + + function useRemoteConfig( + remoteConfigName: R + ): Ref> { + const abbyInstance = inject(ABBY_SYMBOL); + if (!abbyInstance) { + throw new Error("useRemoteConfig must be used within an AbbyProvider"); + } + + const value = ref(abbyInstance.getRemoteConfig(remoteConfigName as string)); + + return value as Ref>; + } + + return { + AbbyProvider, + useAbby, + useFeatureFlag, + useRemoteConfig, + getVariants: abby.getVariants.bind(abby), + getFeatureFlagValue: abby.getFeatureFlag.bind(abby), + getABTestValue: abby.getTestVariant.bind(abby), + getRemoteConfigValue: abby.getRemoteConfig.bind(abby), + updateUserProperties: abby.updateUserProperties.bind(abby), + }; +} \ No newline at end of file diff --git a/packages/vue/tests/types.test.ts b/packages/vue/tests/types.test.ts new file mode 100644 index 00000000..c318871a --- /dev/null +++ b/packages/vue/tests/types.test.ts @@ -0,0 +1,60 @@ +import { describe, it, expectTypeOf } from "vitest"; +import { createAbby } from "../src"; +import { type Ref } from "vue"; + +describe("Vue Integration Types", () => { + it("has correct types for useAbby", () => { + const { useAbby } = createAbby({ + environments: ["dev"], + currentEnvironment: "dev", + projectId: "test", + tests: { + test1: { variants: ["A", "B"] as const }, + }, + }); + + // This is a bit tricky to test without a full vue environment but we can check the return types + // In a real test we'd need to mock inject + }); + + it("has correct types for getVariants", () => { + const { getVariants } = createAbby({ + environments: ["dev"], + currentEnvironment: "dev", + projectId: "test", + tests: { + test1: { variants: ["A", "B"] as const }, + }, + }); + + expectTypeOf(getVariants("test1")).toEqualTypeOf(); + }); + + it("has correct types for useFeatureFlag", () => { + const { useFeatureFlag } = createAbby({ + environments: ["dev"], + currentEnvironment: "dev", + projectId: "test", + flags: ["flag1"] as const, + }); + + // @ts-expect-error - flag2 does not exist + () => useFeatureFlag("flag2"); + }); + + it("has correct types for useRemoteConfig", () => { + const { useRemoteConfig } = createAbby({ + environments: ["dev"], + currentEnvironment: "dev", + projectId: "test", + remoteConfig: { + rc1: "String", + rc2: "Number", + rc3: "JSON", + } as const, + }); + + // @ts-expect-error - rc4 does not exist + () => useRemoteConfig("rc4"); + }); +}); \ No newline at end of file diff --git a/packages/vue/tests/vue-integration.test.ts b/packages/vue/tests/vue-integration.test.ts new file mode 100644 index 00000000..8c40b26b --- /dev/null +++ b/packages/vue/tests/vue-integration.test.ts @@ -0,0 +1,121 @@ +import { describe, it, expect, vi, beforeEach } from "vitest"; +import { mount } from "@vue/test-utils"; +import { createAbby } from "../src"; +import { defineComponent, h } from "vue"; +import { HttpService } from "@tryabby/core"; + +vi.mock("@tryabby/core", async () => { + const actual = await vi.importActual("@tryabby/core") as any; + return { + ...actual, + HttpService: { + sendData: vi.fn().mockResolvedValue({}), + fetchData: vi.fn().mockResolvedValue({}), + }, + }; +}); + +describe("Vue Integration", () => { + const config = { + environments: ["dev"], + currentEnvironment: "dev", + projectId: "test-project", + tests: { + "test-1": { variants: ["A", "B"] }, + }, + flags: ["flag-1"] as const, + remoteConfig: { + "config-1": "String", + } as const, + }; + + beforeEach(() => { + vi.clearAllMocks(); + }); + + it("provides and consumes abby instance", async () => { + const { AbbyProvider, useAbby } = createAbby(config); + + const Consumer = defineComponent({ + setup() { + const { variant, onAct } = useAbby("test-1"); + return { variant, onAct }; + }, + template: "
{{ variant }}
", + }); + + const App = defineComponent({ + render() { + return h(AbbyProvider, {}, { default: () => h(Consumer) }); + }, + }); + + const wrapper = mount(App); + expect(["A", "B"]).toContain(wrapper.text()); + }); + + it("useFeatureFlag returns a boolean ref", () => { + const { AbbyProvider, useFeatureFlag } = createAbby(config); + + const Consumer = defineComponent({ + setup() { + const isEnabled = useFeatureFlag("flag-1"); + return { isEnabled }; + }, + template: "
{{ isEnabled }}
", + }); + + const App = defineComponent({ + render() { + return h(AbbyProvider, {}, { default: () => h(Consumer) }); + }, + }); + + const wrapper = mount(App); + expect(["true", "false"]).toContain(wrapper.text()); + }); + + it("useRemoteConfig returns the config value", () => { + const { AbbyProvider, useRemoteConfig } = createAbby(config); + + const Consumer = defineComponent({ + setup() { + const configValue = useRemoteConfig("config-1"); + return { configValue }; + }, + template: "
{{ configValue }}
", + }); + + const App = defineComponent({ + render() { + return h(AbbyProvider, {}, { default: () => h(Consumer) }); + }, + }); + + const wrapper = mount(App); + // Default value for String remote config in core seems to be empty string or something if not fetched + // But here it should return something. + expect(wrapper.text()).toBeDefined(); + }); + + it("useAbby with lookup works", () => { + const { AbbyProvider, useAbby } = createAbby(config); + + const Consumer = defineComponent({ + setup() { + const { variant } = useAbby("test-1", { A: "Variant A", B: "Variant B" }); + return { variant }; + }, + template: "
{{ variant }}
", + }); + + const App = defineComponent({ + render() { + return h(AbbyProvider, {}, { default: () => h(Consumer) }); + }, + }); + + const wrapper = mount(App); + expect(["Variant A", "Variant B"]).toContain(wrapper.text()); + }); +}); \ No newline at end of file diff --git a/packages/vue/tsconfig.json b/packages/vue/tsconfig.json new file mode 100644 index 00000000..f604ba25 --- /dev/null +++ b/packages/vue/tsconfig.json @@ -0,0 +1,23 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "outDir": "./dist", + "rootDir": "./src", + "composite": false, + "declaration": true, + "declarationMap": true, + "sourceMap": true, + "module": "ESNext", + "moduleResolution": "Bundler", + "target": "ESNext", + "lib": ["ESNext", "DOM"], + "strict": true, + "esModuleInterop": true, + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true, + "jsx": "preserve", + "types": ["vitest/globals"] + }, + "include": ["src/**/*", "tests/**/*"], + "exclude": ["node_modules", "dist"] +} \ No newline at end of file