@configdirector/nuxt-sdk 0.1.0

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.
Files changed (34) hide show
  1. package/README.md +136 -0
  2. package/dist/module.d.mts +85 -0
  3. package/dist/module.json +8 -0
  4. package/dist/module.mjs +66 -0
  5. package/dist/runtime/app/composables/useConfigDirectorClient.d.mts +8 -0
  6. package/dist/runtime/app/composables/useConfigDirectorClient.d.ts +8 -0
  7. package/dist/runtime/app/composables/useConfigDirectorClient.mjs +8 -0
  8. package/dist/runtime/app/composables/useConfigDirectorContext.d.mts +12 -0
  9. package/dist/runtime/app/composables/useConfigDirectorContext.d.ts +12 -0
  10. package/dist/runtime/app/composables/useConfigDirectorContext.mjs +30 -0
  11. package/dist/runtime/app/composables/useConfigDirectorStatus.d.mts +10 -0
  12. package/dist/runtime/app/composables/useConfigDirectorStatus.d.ts +10 -0
  13. package/dist/runtime/app/composables/useConfigDirectorStatus.mjs +13 -0
  14. package/dist/runtime/app/composables/useConfigDirectorValue.d.mts +12 -0
  15. package/dist/runtime/app/composables/useConfigDirectorValue.d.ts +12 -0
  16. package/dist/runtime/app/composables/useConfigDirectorValue.mjs +19 -0
  17. package/dist/runtime/nitro/middleware.d.mts +130 -0
  18. package/dist/runtime/nitro/middleware.d.ts +130 -0
  19. package/dist/runtime/nitro/middleware.mjs +8 -0
  20. package/dist/runtime/nitro/plugin.d.mts +12 -0
  21. package/dist/runtime/nitro/plugin.d.ts +12 -0
  22. package/dist/runtime/nitro/plugin.mjs +1397 -0
  23. package/dist/runtime/plugin.client.d.mts +4 -0
  24. package/dist/runtime/plugin.client.mjs +1162 -0
  25. package/dist/runtime/plugin.server.d.mts +4 -0
  26. package/dist/runtime/plugin.server.mjs +106 -0
  27. package/dist/runtime/server/composables/useConfigDirectorClient.d.mts +6 -0
  28. package/dist/runtime/server/composables/useConfigDirectorClient.d.ts +6 -0
  29. package/dist/runtime/server/composables/useConfigDirectorClient.mjs +9 -0
  30. package/dist/runtime/types-CQWNx-gI.d.mts +34 -0
  31. package/dist/runtime/types-CkzBXJwA.d.mts +4 -0
  32. package/dist/runtime/types-D9MFb76Q.d.mts +85 -0
  33. package/dist/runtime/types-KSnINlzh.d.mts +135 -0
  34. package/package.json +76 -0
@@ -0,0 +1,4 @@
1
+ //#region src/runtime/plugin.server.d.ts
2
+ declare const _default: any;
3
+ //#endregion
4
+ export { _default as default };
@@ -0,0 +1,106 @@
1
+ import { consola } from "consola";
2
+ import { readonly, shallowRef, toRaw } from "vue";
3
+ import { defineNuxtPlugin, useNuxtApp, useRuntimeConfig, useState } from "#app";
4
+ //#region src/runtime/logger.ts
5
+ const createDefaultLogger = (level) => {
6
+ const logger = consola.withTag("configdirector-nuxt-sdk");
7
+ if (level !== void 0) logger.level = level;
8
+ return logger;
9
+ };
10
+ //#endregion
11
+ //#region src/runtime/app/composables/useConfigDirectorClient.ts
12
+ const useConfigDirectorClient = () => {
13
+ const { $configDirectorClient } = useNuxtApp();
14
+ return { client: $configDirectorClient };
15
+ };
16
+ //#endregion
17
+ //#region src/runtime/app/composables/useConfigDirectorContext.ts
18
+ const DEFAULT_TIMEOUT = 3e3;
19
+ const getEffectiveTimeout = (option) => {
20
+ return option && option > 0 ? option : DEFAULT_TIMEOUT;
21
+ };
22
+ const useConfigDirectorContext = () => {
23
+ const state = useState("configdirector-context");
24
+ const { client } = useConfigDirectorClient();
25
+ const updateContext = async (context, options) => {
26
+ const rawContext = toRaw(context);
27
+ const oldContext = JSON.stringify(state.value);
28
+ const newContext = JSON.stringify(rawContext);
29
+ state.value = rawContext;
30
+ if (oldContext != newContext) {
31
+ const timeout = getEffectiveTimeout(options?.timeoutMilliseconds);
32
+ await Promise.race([client.updateContext(rawContext), new Promise((resolve) => {
33
+ setTimeout(() => resolve(), timeout);
34
+ })]);
35
+ }
36
+ };
37
+ return {
38
+ context: readonly(state),
39
+ updateContext
40
+ };
41
+ };
42
+ //#endregion
43
+ //#region src/runtime/plugin.server.ts
44
+ var plugin_server_default = defineNuxtPlugin((nuxtApp) => {
45
+ const logger = createDefaultLogger(useRuntimeConfig().configdirector?.logLevel);
46
+ logger.debug("Installed ConfigDirector Nuxt SSR plugin");
47
+ const serverSdkClient = nuxtApp.ssrContext?.event?.context.configDirectorClient;
48
+ logger.debug("Nitro ConfigDirector client isReady: ", serverSdkClient?.isReady);
49
+ if (!serverSdkClient) throw "The nitro ConfigDirector plugin was not initialized";
50
+ const client = createSsrClient(serverSdkClient);
51
+ const readyStatus = shallowRef(serverSdkClient.isReady ? "ready" : "default");
52
+ nuxtApp.hooks.hook("app:created", async () => {
53
+ logger.debug("Updating ConfigDirector context for SSR evaluation");
54
+ const { context } = useConfigDirectorContext();
55
+ await client.initialize(context.value);
56
+ });
57
+ return { provide: {
58
+ configDirectorClient: client,
59
+ configDirectorClientReadyStatus: readonly(readyStatus)
60
+ } };
61
+ });
62
+ var SsrClient = class {
63
+ currentContext;
64
+ constructor(serverClient) {
65
+ this.serverClient = serverClient;
66
+ this.serverClient = serverClient;
67
+ }
68
+ async initialize(context) {
69
+ this.currentContext = context;
70
+ }
71
+ async updateContext(context) {
72
+ this.currentContext = context;
73
+ }
74
+ get context() {
75
+ return this.currentContext;
76
+ }
77
+ get isReady() {
78
+ return this.serverClient.isReady;
79
+ }
80
+ get isInitializing() {
81
+ return false;
82
+ }
83
+ getValue(configKey, defaultValue) {
84
+ return this.serverClient.getValue(configKey, defaultValue, this.currentContext);
85
+ }
86
+ watch(configKey, defaultValue, callback) {
87
+ return this.serverClient.watch(configKey, defaultValue, callback, this.currentContext);
88
+ }
89
+ unwatch(configKey, callback) {
90
+ return this.serverClient.unwatch(configKey, callback);
91
+ }
92
+ unwatchAll() {
93
+ return this.serverClient.unwatchAll();
94
+ }
95
+ pauseNetwork() {}
96
+ async resumeNetwork() {}
97
+ dispose() {}
98
+ on() {}
99
+ off() {}
100
+ clear() {}
101
+ };
102
+ const createSsrClient = (serverClient) => {
103
+ return new SsrClient(serverClient);
104
+ };
105
+ //#endregion
106
+ export { plugin_server_default as default };
@@ -0,0 +1,6 @@
1
+ import { t as ConfigDirectorClient } from "../../types-D9MFb76Q.mjs";
2
+
3
+ //#region src/runtime/server/composables/useConfigDirectorClient.d.ts
4
+ declare const useConfigDirectorClient: () => ConfigDirectorClient;
5
+ //#endregion
6
+ export { useConfigDirectorClient };
@@ -0,0 +1,6 @@
1
+ import { t as ConfigDirectorClient } from "../../types-D9MFb76Q.mjs";
2
+
3
+ //#region src/runtime/server/composables/useConfigDirectorClient.d.ts
4
+ declare const useConfigDirectorClient: () => ConfigDirectorClient;
5
+ //#endregion
6
+ export { useConfigDirectorClient };
@@ -0,0 +1,9 @@
1
+ import { useNitroApp } from "nitropack/runtime";
2
+ //#region src/runtime/server/composables/useConfigDirectorClient.ts
3
+ const useConfigDirectorClient = () => {
4
+ const serverSdkClient = useNitroApp().configDirectorClient;
5
+ if (!serverSdkClient) throw "The nitro ConfigDirector plugin was not initialized";
6
+ return serverSdkClient;
7
+ };
8
+ //#endregion
9
+ export { useConfigDirectorClient };
@@ -0,0 +1,34 @@
1
+ //#region ../shared/src/types.d.ts
2
+ type ConfigEnumLikeType = {
3
+ [key: string]: string | number;
4
+ };
5
+ type ConfigValueType = string | number | boolean | object | ConfigEnumLikeType;
6
+ /**
7
+ * The user's context to be sent to ConfigDirector. This context will be used for targeting
8
+ * rules evaluation.
9
+ */
10
+ type ConfigDirectorContext = {
11
+ /**
12
+ * The user's identifier. This should be a value that uniquely identifies an application
13
+ * user.
14
+ * In the case of anonymous users, you could generate a UUID or alternatively not provide
15
+ * the {@link id} and the SDK will generate a random UUID. However, keep in mind that this
16
+ * value is used for segmenting users in percentage rollouts, and changes to the {@link id}
17
+ * could result in the user being assigned to a different percentile.
18
+ */
19
+ id?: string;
20
+ /**
21
+ * The user's display name. This will be shown in the ConfigDirector dashboard and may be
22
+ * used for targeting rules.
23
+ */
24
+ name?: string;
25
+ /**
26
+ * Any arbitrary traits for the current user. They will be shown in the ConfigDirector
27
+ * dashboard and may be used for targeting rules.
28
+ */
29
+ traits?: {
30
+ [key: string]: unknown;
31
+ };
32
+ };
33
+ //#endregion
34
+ export { ConfigValueType as n, ConfigDirectorContext as t };
@@ -0,0 +1,4 @@
1
+ //#region src/runtime/types.d.ts
2
+ type ClientStatus = "loading" | "ready" | "default";
3
+ //#endregion
4
+ export { ClientStatus as t };
@@ -0,0 +1,85 @@
1
+ import { n as ConfigValueType, t as ConfigDirectorContext } from "./types-CQWNx-gI.mjs";
2
+
3
+ //#region ../js-server-sdk/src/types.d.ts
4
+ type ClientEvents = {
5
+ configsUpdated: {
6
+ keys: string[];
7
+ };
8
+ clientReady: undefined;
9
+ };
10
+ type WatchHandler<T extends ConfigValueType> = (message: T) => void;
11
+ /**
12
+ * The ConfigDirector SDK client object.
13
+ *
14
+ * Applications should create a single instance of `ConfigDirectorClient`, and call
15
+ * {@link initialize} during application initialization.
16
+ */
17
+ interface ConfigDirectorClient {
18
+ /**
19
+ * Initializes the connection to ConfigDirector to retrieve config definitions. Until
20
+ * initialization is successful, all flags will return their default value provided to
21
+ * {@link watch} or {@link getValue}.
22
+ *
23
+ * If the connection fails or is interrupted with a transient error (network error,
24
+ * internal server error, etc) the client will continue to attempt to connect. However,
25
+ * if the connection fails with a persistent error, like an invalid SDK key, the client will
26
+ * not attempt to re-connect and an error will be logged to the console or the provided
27
+ * logger.
28
+ */
29
+ initialize(): Promise<void>;
30
+ /**
31
+ * Returns whether or not the client is ready after calling {@link initialize}
32
+ *
33
+ * The definition of ready is that the connection to the server was successful, and config definitions
34
+ * were received.
35
+ */
36
+ get isReady(): boolean;
37
+ /**
38
+ * Evaluates a config and returns its value based on the current context and targeting rules
39
+ *
40
+ * @returns The evaluated config value, or the `defaultValue` if the config state was unavailable
41
+ * @param configKey The config key to evaluate
42
+ * @param defaultValue The default value to be returned if the config state is unavailable. For
43
+ * example, if the client cannot connect to the server due to network conditions, or if getValue
44
+ * is called before initialization is done.
45
+ * @param context The user's context to be used for targeting rules evaluation (optional)
46
+ */
47
+ getValue<T extends ConfigValueType>(configKey: string, defaultValue: T, context?: ConfigDirectorContext): T;
48
+ /**
49
+ * Watches for changes to a a config evaluation value. Whenever the config value changes, the
50
+ * provided callback function will be called with the new value. Changes can happen due to updates
51
+ * to the config in the ConfigDirector dashboard, or if the context is updated via {@link updateContext}.
52
+ *
53
+ * @returns An 'unwatch' function that can be called to remove the subscriber
54
+ *
55
+ * @param configKey The config key to watch
56
+ * @param defaultValue The default value to be referenced if the config state is unavailable
57
+ * @param callback The callback function to be called whenever the config value is updated
58
+ * @param context The user's context to be used for targeting rules evaluation (optional)
59
+ */
60
+ watch<T extends ConfigValueType>(configKey: string, defaultValue: T, callback: WatchHandler<T>, context?: ConfigDirectorContext): () => void;
61
+ /**
62
+ * Removes a particular subscriber to the given `configKey`, or all subscribers if no callback
63
+ * is provided.
64
+ *
65
+ * @param configKey The config key to remove subscribers from
66
+ * @param callback The subscriber to be removed. If not provided, all subscribers are removed for
67
+ * the given `configKey`.
68
+ */
69
+ unwatch<T extends ConfigValueType>(configKey: string, callback?: WatchHandler<T>): void;
70
+ /**
71
+ * Removes all subscribers from all config keys
72
+ */
73
+ unwatchAll(): void;
74
+ on<T extends keyof ClientEvents>(eventName: T, handler: (args: ClientEvents[T]) => void): void;
75
+ off<T extends keyof ClientEvents>(eventName: T, handler?: (args: ClientEvents[T]) => void): void;
76
+ /**
77
+ * Disposes of the client. All connections are closed, and all event and config key subscribers
78
+ * are removed.
79
+ *
80
+ * Intended to be called when your application shuts down
81
+ */
82
+ dispose(): void;
83
+ }
84
+ //#endregion
85
+ export { ConfigDirectorClient as t };
@@ -0,0 +1,135 @@
1
+ import { n as ConfigValueType, t as ConfigDirectorContext } from "./types-CQWNx-gI.mjs";
2
+
3
+ //#region ../js-client-core/src/Emitter.d.ts
4
+ type EventType = string | symbol;
5
+ type EventsRecord = Record<EventType, any>;
6
+ type Handler<TEventsRecord extends EventsRecord, TKey extends keyof TEventsRecord> = (payload: TEventsRecord[TKey]) => void;
7
+ interface EventProvider<TEventsRecord extends EventsRecord> {
8
+ on<TName extends keyof TEventsRecord>(name: TName, handler: Handler<TEventsRecord, TName>): void;
9
+ off<TName extends keyof TEventsRecord>(name: TName, handler?: Handler<TEventsRecord, TName>): void;
10
+ clear(): void;
11
+ }
12
+ //#endregion
13
+ //#region ../js-client-core/src/types.d.ts
14
+ type ClientConnectAction = "initialization" | "context update" | "network resume";
15
+ type ClientEvents = {
16
+ configsUpdated: {
17
+ keys: string[];
18
+ };
19
+ clientReady: {
20
+ action: ClientConnectAction;
21
+ };
22
+ contextUpdated: {
23
+ context: ConfigDirectorContext | undefined;
24
+ };
25
+ };
26
+ type WatchHandler<T extends ConfigValueType> = (message: T) => void;
27
+ /**
28
+ * The ConfigDirector SDK client object.
29
+ *
30
+ * Applications should create a single instance of `ConfigDirectorClient`, and call
31
+ * {@link initialize} during application initialization.
32
+ *
33
+ * After initialization, to update the user's context, so that targeting rules are evaluated
34
+ * with the updated context, call {@link updateContext}.
35
+ */
36
+ interface ConfigDirectorClient extends EventProvider<ClientEvents> {
37
+ /**
38
+ * Initializes the connection to ConfigDirector to retrieve config evaluations. Until
39
+ * initialization is successful, all flags will return their default value provided to
40
+ * {@link watch} or {@link getValue}.
41
+ *
42
+ * If the connection fails or is interrupted with a transient error (network error,
43
+ * internal server error, etc) the client will continue to attempt to connect. However,
44
+ * if the connection fails with a persistent error, like an invalid SDK key, the client will
45
+ * not attempt to re-connect and an error will be logged to the console or the provided
46
+ * logger.
47
+ *
48
+ * @param context The current user's context to be used for evaluating targeting rules (optional).
49
+ */
50
+ initialize(context?: ConfigDirectorContext): Promise<void>;
51
+ /**
52
+ * Updates the user's context and re-evaluates all config and flag values based on the new context.
53
+ *
54
+ * @param context The current user's context to be used for evaluating targeting rules (required).
55
+ */
56
+ updateContext(context: ConfigDirectorContext): Promise<void>;
57
+ /**
58
+ * Returns the current context the client is configured with, or undefined if there is no context.
59
+ *
60
+ * Note that the value returned does not updated immediately after calling {@link updateContext}. Only
61
+ * once the underlying connection was successful or timed out, the current context is updated. This is
62
+ * because while the context update is in progress, configs are still evaluated on the previous context.
63
+ *
64
+ */
65
+ get context(): ConfigDirectorContext | undefined;
66
+ /**
67
+ * Returns whether or not the client is ready after calling {@link initialize} or {@link updateContext}
68
+ *
69
+ * The definition of ready is that the connection to the server was successful, and config state
70
+ * was received.
71
+ */
72
+ get isReady(): boolean;
73
+ /**
74
+ * Returns whether or not the client is currently initializing. Upon client creation, it is `false`.
75
+ * It is `true` after calling `initialize` and becomes `false` again once initialization is completed.
76
+ */
77
+ get isInitializing(): boolean;
78
+ /**
79
+ * Evaluates a config and returns its value based on the current context and targeting rules
80
+ *
81
+ * @returns The evaluated config value, or the `defaultValue` if the config state was unavailable
82
+ * @param configKey The config key to evaluate
83
+ * @param defaultValue The default value to be returned if the config state is unavailable. For
84
+ * example, if the client cannot connect to the server due to network conditions, or if getValue
85
+ * is called before initialization is done.
86
+ */
87
+ getValue<T extends ConfigValueType>(configKey: string, defaultValue: T): T;
88
+ /**
89
+ * Watches for changes to a a config evaluation value. Whenever the config value changes, the
90
+ * provided callback function will be called with the new value. Changes can happen due to updates
91
+ * to the config in the ConfigDirector dashboard, or if the context is updated via {@link updateContext}.
92
+ *
93
+ * @returns An 'unwatch' function that can be called to remove the subscriber
94
+ *
95
+ * @param configKey The config key to watch
96
+ * @param defaultValue The default value to be referenced if the config state is unavailable
97
+ * @param callback The callback function to be called whenever the config value is updated
98
+ */
99
+ watch<T extends ConfigValueType>(configKey: string, defaultValue: T, callback: WatchHandler<T>): () => void;
100
+ /**
101
+ * Removes a particular subscriber to the given `configKey`, or all subscribers if no callback
102
+ * is provided.
103
+ *
104
+ * @param configKey The config key to remove subscribers from
105
+ * @param callback The subscriber to be removed. If not provided, all subscribers are removed for
106
+ * the given `configKey`.
107
+ */
108
+ unwatch<T extends ConfigValueType>(configKey: string, callback?: WatchHandler<T>): void;
109
+ /**
110
+ * Removes all subscribers from all config keys
111
+ */
112
+ unwatchAll(): void;
113
+ /**
114
+ * Pauses the network connection without clearing event handlers, watch handlers, or config
115
+ * state. Intended for mobile environments where the OS may kill background connections (e.g.
116
+ * when the app is backgrounded on iOS/Android).
117
+ *
118
+ * Call {@link resumeNetwork} to re-establish the connection.
119
+ */
120
+ pauseNetwork(): void;
121
+ /**
122
+ * Resumes a connection that was paused via {@link pauseNetwork}, using the last context that
123
+ * was provided to {@link initialize} or {@link updateContext}.
124
+ */
125
+ resumeNetwork(): Promise<void>;
126
+ /**
127
+ * Disposes of the client. All connections are closed, and all event and config key subscribers
128
+ * are removed.
129
+ *
130
+ * Intended to be called when your application shuts down
131
+ */
132
+ dispose(): void;
133
+ }
134
+ //#endregion
135
+ export { ConfigDirectorClient as t };
package/package.json ADDED
@@ -0,0 +1,76 @@
1
+ {
2
+ "name": "@configdirector/nuxt-sdk",
3
+ "version": "0.1.0",
4
+ "description": "Nuxt SDK for ConfigDirector. ConfigDirector is a remote configuration and feature flag service.",
5
+ "license": "MIT",
6
+ "author": "ConfigDirector",
7
+ "keywords": [
8
+ "configdirector",
9
+ "configdirector vue",
10
+ "config",
11
+ "configuration",
12
+ "remote config",
13
+ "remote configuration",
14
+ "feature flag",
15
+ "feature toggle",
16
+ "feature switch"
17
+ ],
18
+ "homepage": "https://www.configdirector.com",
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "https://github.com/ConfigDirector/js-sdks"
22
+ },
23
+ "type": "module",
24
+ "exports": {
25
+ ".": {
26
+ "types": "./dist/module.d.mts",
27
+ "import": "./dist/module.mjs"
28
+ }
29
+ },
30
+ "main": "./dist/module.mjs",
31
+ "typesVersions": {
32
+ "*": {
33
+ ".": [
34
+ "./dist/module.d.mts"
35
+ ]
36
+ }
37
+ },
38
+ "files": [
39
+ "dist"
40
+ ],
41
+ "scripts": {
42
+ "build": "nuxt prepare && tsdown",
43
+ "prepack": "nuxt prepare && tsdown",
44
+ "release": "yarn lint && yarn ci:test && yarn build && npm publish --access public",
45
+ "lint": "eslint .",
46
+ "ci:test": "vitest run",
47
+ "test": "vitest watch",
48
+ "test:types": "vue-tsc --noEmit && cd playground && vue-tsc --noEmit"
49
+ },
50
+ "dependencies": {
51
+ "@jsonjoy.com/json-pointer": "^18.1.0",
52
+ "@nuxt/kit": "^4.4.2",
53
+ "consola": "^3.4.2",
54
+ "defu": "^6.1.7",
55
+ "rapidhash-js": "^3.0.0",
56
+ "semver": "^7.7.4"
57
+ },
58
+ "devDependencies": {
59
+ "@nuxt/devtools": "^3.2.4",
60
+ "@nuxt/eslint-config": "^1.15.2",
61
+ "@nuxt/schema": "^4.4.2",
62
+ "@nuxt/test-utils": "^4.0.2",
63
+ "@types/node": "latest",
64
+ "changelogen": "^0.6.2",
65
+ "eslint": "^10.2.1",
66
+ "nuxt": "^4.4.2",
67
+ "playwright": "^1.52.0",
68
+ "rimraf": "^6.1.3",
69
+ "tsdown": "^0.21.7",
70
+ "tslib": "^2.8.1",
71
+ "typescript": "~6.0.3",
72
+ "vite": "^8.0.10",
73
+ "vitest": "^4.1.4",
74
+ "vue-tsc": "^3.2.7"
75
+ }
76
+ }