@flipfeatureflag/vue 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ import { App, Ref } from 'vue';
2
+ import { FlipFlagClient, FlipFlagClientOptions, FlagsStatus, SdkFlagEvaluation } from '@flipfeatureflag/core';
3
+ export { FlipFlagClient, FlipFlagClientOptions } from '@flipfeatureflag/core';
4
+
5
+ interface FlipFlagPluginOptions {
6
+ client?: FlipFlagClient;
7
+ config?: FlipFlagClientOptions;
8
+ }
9
+ declare function createFlipFlag(options: FlipFlagPluginOptions): {
10
+ install(app: App): void;
11
+ };
12
+ declare function useFlipFlagClient(): FlipFlagClient;
13
+ declare function useFlagsStatus(): FlagsStatus;
14
+ declare function useFlag(flagKey: string, defaultValue?: boolean): Ref<boolean>;
15
+ declare function useVariant(flagKey: string, defaultValue?: SdkFlagEvaluation["value"]): Ref<SdkFlagEvaluation>;
16
+
17
+ export { type FlipFlagPluginOptions, createFlipFlag, useFlag, useFlagsStatus, useFlipFlagClient, useVariant };
@@ -0,0 +1,17 @@
1
+ import { App, Ref } from 'vue';
2
+ import { FlipFlagClient, FlipFlagClientOptions, FlagsStatus, SdkFlagEvaluation } from '@flipfeatureflag/core';
3
+ export { FlipFlagClient, FlipFlagClientOptions } from '@flipfeatureflag/core';
4
+
5
+ interface FlipFlagPluginOptions {
6
+ client?: FlipFlagClient;
7
+ config?: FlipFlagClientOptions;
8
+ }
9
+ declare function createFlipFlag(options: FlipFlagPluginOptions): {
10
+ install(app: App): void;
11
+ };
12
+ declare function useFlipFlagClient(): FlipFlagClient;
13
+ declare function useFlagsStatus(): FlagsStatus;
14
+ declare function useFlag(flagKey: string, defaultValue?: boolean): Ref<boolean>;
15
+ declare function useVariant(flagKey: string, defaultValue?: SdkFlagEvaluation["value"]): Ref<SdkFlagEvaluation>;
16
+
17
+ export { type FlipFlagPluginOptions, createFlipFlag, useFlag, useFlagsStatus, useFlipFlagClient, useVariant };
package/dist/index.js ADDED
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ FlipFlagClient: () => import_core.FlipFlagClient,
24
+ createFlipFlag: () => createFlipFlag,
25
+ useFlag: () => useFlag,
26
+ useFlagsStatus: () => useFlagsStatus,
27
+ useFlipFlagClient: () => useFlipFlagClient,
28
+ useVariant: () => useVariant
29
+ });
30
+ module.exports = __toCommonJS(index_exports);
31
+ var import_vue = require("vue");
32
+ var import_js = require("@flipfeatureflag/js");
33
+ var import_core = require("@flipfeatureflag/core");
34
+ var clientKey = /* @__PURE__ */ Symbol("FlipFlagClient");
35
+ var statusKey = /* @__PURE__ */ Symbol("FlipFlagStatus");
36
+ function createFlipFlag(options) {
37
+ return {
38
+ install(app) {
39
+ const client = options.client ?? (options.config ? (0, import_js.createClient)(options.config) : null);
40
+ if (!client) {
41
+ throw new Error("flipFeatureFlag plugin requires client or config");
42
+ }
43
+ const status = (0, import_vue.reactive)({ ...client.getStatus() });
44
+ const handleUpdate = () => {
45
+ Object.assign(status, client.getStatus());
46
+ };
47
+ client.on("ready", handleUpdate);
48
+ client.on("update", handleUpdate);
49
+ client.on("error", handleUpdate);
50
+ void client.start();
51
+ app.provide(clientKey, client);
52
+ app.provide(statusKey, (0, import_vue.readonly)(status));
53
+ }
54
+ };
55
+ }
56
+ function useFlipFlagClient() {
57
+ const client = (0, import_vue.inject)(clientKey, null);
58
+ if (!client) {
59
+ throw new Error("useFlipFlagClient must be used within flipFeatureFlag plugin");
60
+ }
61
+ return client;
62
+ }
63
+ function useFlagsStatus() {
64
+ const status = (0, import_vue.inject)(statusKey, null);
65
+ if (!status) {
66
+ throw new Error("useFlagsStatus must be used within flipFeatureFlag plugin");
67
+ }
68
+ return status;
69
+ }
70
+ function useFlag(flagKey, defaultValue = false) {
71
+ const client = useFlipFlagClient();
72
+ const state = (0, import_vue.ref)(client.isEnabled(flagKey, defaultValue));
73
+ const update = () => {
74
+ state.value = client.isEnabled(flagKey, defaultValue);
75
+ };
76
+ client.on("update", update);
77
+ (0, import_vue.onUnmounted)(() => client.off("update", update));
78
+ return state;
79
+ }
80
+ function useVariant(flagKey, defaultValue = false) {
81
+ const client = useFlipFlagClient();
82
+ const state = (0, import_vue.ref)(client.getVariant(flagKey, defaultValue));
83
+ const update = () => {
84
+ state.value = client.getVariant(flagKey, defaultValue);
85
+ };
86
+ client.on("update", update);
87
+ (0, import_vue.onUnmounted)(() => client.off("update", update));
88
+ return state;
89
+ }
90
+ // Annotate the CommonJS export names for ESM import in node:
91
+ 0 && (module.exports = {
92
+ FlipFlagClient,
93
+ createFlipFlag,
94
+ useFlag,
95
+ useFlagsStatus,
96
+ useFlipFlagClient,
97
+ useVariant
98
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,68 @@
1
+ // src/index.ts
2
+ import { inject, reactive, readonly, ref, onUnmounted } from "vue";
3
+ import { createClient as createBrowserClient } from "@flipfeatureflag/js";
4
+ import { FlipFlagClient } from "@flipfeatureflag/core";
5
+ var clientKey = /* @__PURE__ */ Symbol("FlipFlagClient");
6
+ var statusKey = /* @__PURE__ */ Symbol("FlipFlagStatus");
7
+ function createFlipFlag(options) {
8
+ return {
9
+ install(app) {
10
+ const client = options.client ?? (options.config ? createBrowserClient(options.config) : null);
11
+ if (!client) {
12
+ throw new Error("flipFeatureFlag plugin requires client or config");
13
+ }
14
+ const status = reactive({ ...client.getStatus() });
15
+ const handleUpdate = () => {
16
+ Object.assign(status, client.getStatus());
17
+ };
18
+ client.on("ready", handleUpdate);
19
+ client.on("update", handleUpdate);
20
+ client.on("error", handleUpdate);
21
+ void client.start();
22
+ app.provide(clientKey, client);
23
+ app.provide(statusKey, readonly(status));
24
+ }
25
+ };
26
+ }
27
+ function useFlipFlagClient() {
28
+ const client = inject(clientKey, null);
29
+ if (!client) {
30
+ throw new Error("useFlipFlagClient must be used within flipFeatureFlag plugin");
31
+ }
32
+ return client;
33
+ }
34
+ function useFlagsStatus() {
35
+ const status = inject(statusKey, null);
36
+ if (!status) {
37
+ throw new Error("useFlagsStatus must be used within flipFeatureFlag plugin");
38
+ }
39
+ return status;
40
+ }
41
+ function useFlag(flagKey, defaultValue = false) {
42
+ const client = useFlipFlagClient();
43
+ const state = ref(client.isEnabled(flagKey, defaultValue));
44
+ const update = () => {
45
+ state.value = client.isEnabled(flagKey, defaultValue);
46
+ };
47
+ client.on("update", update);
48
+ onUnmounted(() => client.off("update", update));
49
+ return state;
50
+ }
51
+ function useVariant(flagKey, defaultValue = false) {
52
+ const client = useFlipFlagClient();
53
+ const state = ref(client.getVariant(flagKey, defaultValue));
54
+ const update = () => {
55
+ state.value = client.getVariant(flagKey, defaultValue);
56
+ };
57
+ client.on("update", update);
58
+ onUnmounted(() => client.off("update", update));
59
+ return state;
60
+ }
61
+ export {
62
+ FlipFlagClient,
63
+ createFlipFlag,
64
+ useFlag,
65
+ useFlagsStatus,
66
+ useFlipFlagClient,
67
+ useVariant
68
+ };
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@flipfeatureflag/vue",
3
+ "version": "0.1.1",
4
+ "description": "flipFeatureFlag Vue SDK",
5
+ "main": "dist/index.cjs",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.cjs"
13
+ }
14
+ },
15
+ "sideEffects": false,
16
+ "scripts": {
17
+ "build": "tsup src/index.ts --format esm,cjs --dts --external vue"
18
+ },
19
+ "peerDependencies": {
20
+ "vue": ">=3"
21
+ },
22
+ "dependencies": {
23
+ "@flipfeatureflag/core": "0.1.0",
24
+ "@flipfeatureflag/js": "0.1.0"
25
+ }
26
+ }
package/src/index.ts ADDED
@@ -0,0 +1,87 @@
1
+ import { App, inject, InjectionKey, reactive, readonly, ref, onUnmounted, Ref } from "vue";
2
+ import { FlipFlagClient, FlipFlagClientOptions, FlagsStatus, SdkFlagEvaluation } from "@flipfeatureflag/core";
3
+ import { createClient as createBrowserClient } from "@flipfeatureflag/js";
4
+
5
+ const clientKey: InjectionKey<FlipFlagClient> = Symbol("FlipFlagClient");
6
+ const statusKey: InjectionKey<FlagsStatus> = Symbol("FlipFlagStatus");
7
+
8
+ export interface FlipFlagPluginOptions {
9
+ client?: FlipFlagClient;
10
+ config?: FlipFlagClientOptions;
11
+ }
12
+
13
+ export function createFlipFlag(options: FlipFlagPluginOptions) {
14
+ return {
15
+ install(app: App) {
16
+ const client = options.client ?? (options.config ? createBrowserClient(options.config) : null);
17
+ if (!client) {
18
+ throw new Error("flipFeatureFlag plugin requires client or config");
19
+ }
20
+
21
+ const status = reactive<FlagsStatus>({ ...client.getStatus() });
22
+
23
+ const handleUpdate = () => {
24
+ Object.assign(status, client.getStatus());
25
+ };
26
+
27
+ client.on("ready", handleUpdate);
28
+ client.on("update", handleUpdate);
29
+ client.on("error", handleUpdate);
30
+
31
+ void client.start();
32
+
33
+ app.provide(clientKey, client);
34
+ app.provide(statusKey, readonly(status));
35
+ },
36
+ };
37
+ }
38
+
39
+ export function useFlipFlagClient(): FlipFlagClient {
40
+ const client = inject(clientKey, null);
41
+ if (!client) {
42
+ throw new Error("useFlipFlagClient must be used within flipFeatureFlag plugin");
43
+ }
44
+ return client;
45
+ }
46
+
47
+ export function useFlagsStatus(): FlagsStatus {
48
+ const status = inject(statusKey, null);
49
+ if (!status) {
50
+ throw new Error("useFlagsStatus must be used within flipFeatureFlag plugin");
51
+ }
52
+ return status;
53
+ }
54
+
55
+ export function useFlag(flagKey: string, defaultValue = false): Ref<boolean> {
56
+ const client = useFlipFlagClient();
57
+ const state = ref(client.isEnabled(flagKey, defaultValue));
58
+
59
+ const update = () => {
60
+ state.value = client.isEnabled(flagKey, defaultValue);
61
+ };
62
+
63
+ client.on("update", update);
64
+ onUnmounted(() => client.off("update", update));
65
+
66
+ return state;
67
+ }
68
+
69
+ export function useVariant(
70
+ flagKey: string,
71
+ defaultValue: SdkFlagEvaluation["value"] = false,
72
+ ): Ref<SdkFlagEvaluation> {
73
+ const client = useFlipFlagClient();
74
+ const state = ref(client.getVariant(flagKey, defaultValue));
75
+
76
+ const update = () => {
77
+ state.value = client.getVariant(flagKey, defaultValue);
78
+ };
79
+
80
+ client.on("update", update);
81
+ onUnmounted(() => client.off("update", update));
82
+
83
+ return state;
84
+ }
85
+
86
+ export type { FlipFlagClientOptions } from "@flipfeatureflag/core";
87
+ export { FlipFlagClient } from "@flipfeatureflag/core";
package/tsconfig.json ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "include": ["src"]
4
+ }