|
| 1 | +// BucketPlugin.ts |
| 2 | +import { reactive, ref } from "vue"; |
| 3 | +import type { App, InjectionKey } from "vue"; |
| 4 | +import type { |
| 5 | + CompanyContext, |
| 6 | + Feedback, |
| 7 | + FeedbackOptions, |
| 8 | + FlagsOptions, |
| 9 | + RequestFeedbackOptions, |
| 10 | + UserContext, |
| 11 | +} from "@bucketco/browser-sdk"; |
| 12 | +import { BucketClient } from "@bucketco/browser-sdk"; |
| 13 | + |
| 14 | +import { version } from "../package.json"; |
| 15 | + |
| 16 | +const SDK_VERSION = `vue-sdk/${version}`; |
| 17 | + |
| 18 | +type OtherContext = Record<string, any>; |
| 19 | + |
| 20 | +export interface Flags {} |
| 21 | + |
| 22 | +export type BucketFlags = keyof (keyof Flags extends never |
| 23 | + ? Record<string, boolean> |
| 24 | + : Flags); |
| 25 | + |
| 26 | +export type FlagsResult = { [k in BucketFlags]?: boolean }; |
| 27 | + |
| 28 | +export interface BucketState { |
| 29 | + flags: FlagsResult; |
| 30 | + isLoading: boolean; |
| 31 | + user: UserContext | null; |
| 32 | + company: CompanyContext | null; |
| 33 | + otherContext: OtherContext | null; |
| 34 | +} |
| 35 | + |
| 36 | +export interface BucketPluginOptions { |
| 37 | + publishableKey: string; |
| 38 | + flagOptions?: Omit<FlagsOptions, "fallbackFlags"> & { |
| 39 | + fallbackFlags?: BucketFlags[]; |
| 40 | + }; |
| 41 | + feedback?: FeedbackOptions; |
| 42 | + host?: string; |
| 43 | + sseHost?: string; |
| 44 | + debug?: boolean; |
| 45 | +} |
| 46 | + |
| 47 | +type ProvideType = { |
| 48 | + state: BucketState; |
| 49 | + updateUser: (newUser?: UserContext) => void; |
| 50 | + updateCompany: (newCompany?: CompanyContext) => void; |
| 51 | + updateOtherContext: (otherContext?: OtherContext) => void; |
| 52 | + track: (eventName: string, attributes?: Record<string, any>) => Promise<void>; |
| 53 | + sendFeedback: (opts: Omit<Feedback, "userId" | "companyId">) => Promise<void>; |
| 54 | + requestFeedback: ( |
| 55 | + opts: Omit<RequestFeedbackOptions, "userId" | "companyId">, |
| 56 | + ) => void; |
| 57 | +}; |
| 58 | + |
| 59 | +export const BucketInjectionKey = Symbol() as InjectionKey<ProvideType>; |
| 60 | + |
| 61 | +export const BucketPlugin = { |
| 62 | + install(app: App, options: BucketPluginOptions) { |
| 63 | + const bucketState = reactive<BucketState>({ |
| 64 | + flags: {}, |
| 65 | + isLoading: true, |
| 66 | + user: null, |
| 67 | + company: null, |
| 68 | + otherContext: null, |
| 69 | + }); |
| 70 | + |
| 71 | + const client = ref<BucketClient | null>(null); |
| 72 | + |
| 73 | + const updateUser = (newUser?: UserContext) => { |
| 74 | + bucketState.user = newUser ?? null; |
| 75 | + updateClient(); |
| 76 | + }; |
| 77 | + |
| 78 | + const updateCompany = (newCompany?: CompanyContext) => { |
| 79 | + bucketState.company = newCompany ?? null; |
| 80 | + updateClient(); |
| 81 | + }; |
| 82 | + |
| 83 | + const updateOtherContext = (otherContext?: OtherContext) => { |
| 84 | + bucketState.otherContext = otherContext ?? null; |
| 85 | + updateClient(); |
| 86 | + }; |
| 87 | + |
| 88 | + const updateClient = () => { |
| 89 | + if (client.value) { |
| 90 | + client.value.stop(); |
| 91 | + } |
| 92 | + |
| 93 | + client.value = new BucketClient( |
| 94 | + options.publishableKey, |
| 95 | + { |
| 96 | + user: bucketState.user ?? undefined, |
| 97 | + company: bucketState.company ?? undefined, |
| 98 | + otherContext: bucketState.otherContext ?? undefined, |
| 99 | + }, |
| 100 | + { |
| 101 | + host: options.host, |
| 102 | + sseHost: options.sseHost, |
| 103 | + flags: { |
| 104 | + ...options.flagOptions, |
| 105 | + onUpdatedFlags: (flags) => { |
| 106 | + bucketState.flags = flags; |
| 107 | + }, |
| 108 | + }, |
| 109 | + feedback: options.feedback, |
| 110 | + logger: options.debug ? console : undefined, |
| 111 | + sdkVersion: SDK_VERSION, |
| 112 | + }, |
| 113 | + ); |
| 114 | + |
| 115 | + client.value |
| 116 | + .initialize() |
| 117 | + .then(() => { |
| 118 | + bucketState.flags = client.value!.getFlags() ?? {}; |
| 119 | + bucketState.isLoading = false; |
| 120 | + |
| 121 | + // Update user attributes |
| 122 | + const { id: userId, ...userAttributes } = bucketState.user || {}; |
| 123 | + if (userId) { |
| 124 | + client.value!.user(userAttributes).catch(() => { |
| 125 | + // ignore rejections. Logged inside |
| 126 | + }); |
| 127 | + } |
| 128 | + |
| 129 | + // Update company attributes |
| 130 | + const { id: companyId, ...companyAttributes } = |
| 131 | + bucketState.company || {}; |
| 132 | + if (companyId) { |
| 133 | + client.value!.company(companyAttributes).catch(() => { |
| 134 | + // ignore rejections. Logged inside |
| 135 | + }); |
| 136 | + } |
| 137 | + }) |
| 138 | + .catch(() => { |
| 139 | + // initialize cannot actually throw, but this fixes lint warnings |
| 140 | + }); |
| 141 | + }; |
| 142 | + |
| 143 | + const track = async ( |
| 144 | + eventName: string, |
| 145 | + attributes?: Record<string, any>, |
| 146 | + ) => { |
| 147 | + if (!bucketState.user?.id) { |
| 148 | + console.error("User is required to send events"); |
| 149 | + return; |
| 150 | + } |
| 151 | + await client.value?.track(eventName, attributes); |
| 152 | + }; |
| 153 | + |
| 154 | + const sendFeedback = async ( |
| 155 | + opts: Omit<Feedback, "userId" | "companyId">, |
| 156 | + ) => { |
| 157 | + if (!bucketState.user?.id) { |
| 158 | + console.error("User is required to send feedback"); |
| 159 | + return; |
| 160 | + } |
| 161 | + await client.value?.feedback({ |
| 162 | + ...opts, |
| 163 | + userId: String(bucketState.user.id), |
| 164 | + companyId: bucketState.company?.id |
| 165 | + ? String(bucketState.company.id) |
| 166 | + : undefined, |
| 167 | + }); |
| 168 | + }; |
| 169 | + |
| 170 | + const requestFeedback = ( |
| 171 | + opts: Omit<RequestFeedbackOptions, "userId" | "companyId">, |
| 172 | + ) => { |
| 173 | + if (!bucketState.user?.id) { |
| 174 | + console.error("User is required to request feedback"); |
| 175 | + return; |
| 176 | + } |
| 177 | + client.value?.requestFeedback({ |
| 178 | + ...opts, |
| 179 | + userId: String(bucketState.user.id), |
| 180 | + companyId: bucketState.company?.id |
| 181 | + ? String(bucketState.company.id) |
| 182 | + : undefined, |
| 183 | + }); |
| 184 | + }; |
| 185 | + |
| 186 | + app.provide(BucketInjectionKey, { |
| 187 | + state: bucketState, |
| 188 | + updateUser, |
| 189 | + updateCompany, |
| 190 | + updateOtherContext, |
| 191 | + track, |
| 192 | + sendFeedback, |
| 193 | + requestFeedback, |
| 194 | + }); |
| 195 | + |
| 196 | + updateClient(); |
| 197 | + }, |
| 198 | +}; |
0 commit comments