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
44 changes: 44 additions & 0 deletions packages/vue/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "@tryabby/vue",
"version": "0.1.0",
"description": "Abby Vue integration",
"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",
"peerDependencies": {
"vue": "^3.0.0"
},
"devDependencies": {
"@types/js-cookie": "^3.0.3",
"jsdom": "^20.0.3",
"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"
}
}
65 changes: 65 additions & 0 deletions packages/vue/src/StorageService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {
type IStorageService,
getABStorageKey,
getFFStorageKey,
getRCStorageKey,
} from "@tryabby/core";
import type { StorageServiceOptions } 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();
226 changes: 226 additions & 0 deletions packages/vue/src/createAbby.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
import {
type ABConfig,
Abby,
type AbbyConfig,
AbbyEventType,
HttpService,
type RemoteConfigValueString,
type RemoteConfigValueStringToType,
type StorageServiceOptions,
} from "@tryabby/core";
import { type ComputedRef, computed, ref } from "vue";
import {
FlagStorageService,
RemoteConfigStorageService,
TestStorageService,
} from "./StorageService";

type ABTestReturnValue<Lookup, TestVariant> = Lookup extends undefined
? TestVariant
: TestVariant extends keyof Lookup
? Lookup[TestVariant]
: never;

export function createAbby<
const FlagName extends string,
const TestName extends string,
const Tests extends Record<TestName, ABConfig>,
const RemoteConfig extends Record<RemoteConfigName, RemoteConfigValueString>,
const RemoteConfigName extends Extract<keyof RemoteConfig, string>,
>(
config: AbbyConfig<FlagName, Tests, string[], RemoteConfigName, RemoteConfig>
) {
const abby = new Abby<
FlagName,
TestName,
Tests,
RemoteConfig,
RemoteConfigName
>(
config,
{
get: (key: string) => {
if (typeof window === "undefined") return null;
return TestStorageService.get(config.projectId, key);
},
set: (key: string, value: any, options?: StorageServiceOptions) => {
if (typeof window === "undefined") return;
TestStorageService.set(config.projectId, key, value, options);
},
},
{
get: (key: string) => {
if (typeof window === "undefined") return null;
return FlagStorageService.get(config.projectId, key);
},
set: (key: string, value: any) => {
if (typeof window === "undefined") return;
FlagStorageService.set(config.projectId, key, value);
},
},
{
get: (key: string) => {
if (typeof window === "undefined") return null;
return RemoteConfigStorageService.get(config.projectId, key);
},
set: (key: string, value: any) => {
if (typeof window === "undefined") return;
RemoteConfigStorageService.set(config.projectId, key, value);
},
}
);

// Bump a global tick whenever the underlying Abby data changes so all
// composables recompute. Vue's reactivity cannot observe the Abby class
// directly, so the tick bridges the two worlds.
const tick = ref(0);
abby.subscribe(() => {
tick.value++;
});

const notify = <N extends keyof Tests>(name: N, selectedVariant: string) => {
if (!name || !selectedVariant) return;
HttpService.sendData({
url: config.apiUrl,
type: AbbyEventType.PING,
data: {
projectId: config.projectId,
selectedVariant,
testName: name as string,
},
});
};

const useAbby = <
TestName extends keyof Tests,
TestVariant extends Tests[TestName]["variants"][number],
LookupValue,
const Lookup extends
| Record<TestVariant, LookupValue>
| undefined = undefined,
>(
testName: TestName,
lookupObject?: Lookup
): {
variant: ComputedRef<ABTestReturnValue<Lookup, TestVariant>>;
onAct: () => void;
} => {
let cachedVariant:
| { tick: number; value: Tests[TestName]["variants"][number] }
| undefined;
const selectedVariant = computed(() => {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
const currentTick = tick.value;
if (!cachedVariant || cachedVariant.tick !== currentTick) {
cachedVariant = {
tick: currentTick,
value: abby.getTestVariant(testName),
};
}
return cachedVariant.value;
});

const variant = computed<ABTestReturnValue<Lookup, TestVariant>>(() => {
return lookupObject
? (lookupObject as any)[selectedVariant.value]
: (selectedVariant.value as any);
});

const onAct = () => {
HttpService.sendData({
url: config.apiUrl,
type: AbbyEventType.ACT,
data: {
projectId: config.projectId,
selectedVariant: selectedVariant.value,
testName: testName as string,
},
});
};

notify(testName, selectedVariant.value);

return { variant, onAct };
};

const getVariants = <T extends keyof Tests>(testName: T) => {
return computed<Readonly<string[]>>(() => {
tick.value;
return abby.getVariants(testName) as unknown as Readonly<string[]>;
});
};

/**
* helper function to reset an Ab Test
*/
const getABResetFunction = <T extends keyof Tests>(testName: T) => {
return () => {
TestStorageService.remove(config.projectId, testName as string);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
tick.value++;
};
};

const getABTestValue = <
TestName extends keyof Tests,
TestVariant extends Tests[TestName]["variants"][number],
LookupValue,
const Lookup extends
| Record<TestVariant, LookupValue>
| undefined = undefined,
>(
testName: TestName,
lookupObject?: Lookup
): ABTestReturnValue<Lookup, TestVariant> => {
const variant = abby.getTestVariant(testName);
// Typescript loses its typing here, so we cast as any in favor of having
// better type inference for the user
if (lookupObject === undefined) {
return variant as any;
}

return (lookupObject as any)[variant] as any;
};

const getFeatureFlagValue = (featureFlagName: FlagName) => {
return abby.getFeatureFlag(featureFlagName);
};

const useFeatureFlag = (flagName: FlagName): ComputedRef<boolean> => {
return computed(() => {
tick.value;
return abby.getFeatureFlag(flagName);
});
};

const getRemoteConfig = <
T extends RemoteConfigName,
Config extends RemoteConfig[T],
>(
remoteConfigName: T
): RemoteConfigValueStringToType<Config> => {
return abby.getRemoteConfig(remoteConfigName);
};

const useRemoteConfig = <
T extends RemoteConfigName,
Config extends RemoteConfig[T],
>(
remoteConfigName: T
): ComputedRef<RemoteConfigValueStringToType<Config>> => {
return computed(() => {
tick.value;
return abby.getRemoteConfig(remoteConfigName);
});
};

return {
useAbby,
useFeatureFlag,
getFeatureFlagValue,
getRemoteConfig,
useRemoteConfig,
getABTestValue,
__abby__: abby,
getABResetFunction,
getVariants,
};
}
11 changes: 11 additions & 0 deletions packages/vue/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export { createAbby } from "./createAbby";
export {
FlagStorageService,
RemoteConfigStorageService,
TestStorageService,
} from "./StorageService";
export {
type ABConfig,
type AbbyConfig,
defineConfig,
} from "@tryabby/core";
Loading