@schematichq/schematic-vue 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.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Schematic
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
package/README.md ADDED
@@ -0,0 +1,281 @@
1
+ # schematic-vue
2
+
3
+ `schematic-vue` is a client-side Vue library for [Schematic](https://schematichq.com) which provides composables to track events, check flags, and more. `schematic-vue` provides the same capabilities as [schematic-js](https://github.com/schematichq/schematic-js/tree/main/js), for Vue apps.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install @schematichq/schematic-vue
9
+ # or
10
+ yarn add @schematichq/schematic-vue
11
+ # or
12
+ pnpm add @schematichq/schematic-vue
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ### SchematicPlugin
18
+
19
+ You can use the `SchematicPlugin` to make Schematic available throughout your Vue application:
20
+
21
+ ```typescript
22
+ import { createApp } from "vue";
23
+ import { SchematicPlugin } from "@schematichq/schematic-vue";
24
+ import App from "./App.vue";
25
+
26
+ const app = createApp(App);
27
+ app.use(SchematicPlugin, { publishableKey: "your-publishable-key" });
28
+ app.mount("#app");
29
+ ```
30
+
31
+ ### Setting context
32
+
33
+ To set the user context for events and flag checks, you can use the `identify` function provided by the `useSchematicEvents` composable:
34
+
35
+ ```vue
36
+ <script setup lang="ts">
37
+ import { onMounted } from "vue";
38
+ import { useSchematicEvents } from "@schematichq/schematic-vue";
39
+
40
+ const { identify } = useSchematicEvents();
41
+
42
+ onMounted(() => {
43
+ identify({
44
+ keys: { id: "my-user-id" },
45
+ company: {
46
+ keys: { id: "my-company-id" },
47
+ traits: { location: "Atlanta, GA" },
48
+ },
49
+ });
50
+ });
51
+ </script>
52
+ ```
53
+
54
+ To learn more about identifying companies with the `keys` map, see [key management in Schematic public docs](https://docs.schematichq.com/developer_resources/key_management).
55
+
56
+ ### Tracking usage
57
+
58
+ Once you've set the context with `identify`, you can track events:
59
+
60
+ ```vue
61
+ <script setup lang="ts">
62
+ import { useSchematicEvents } from "@schematichq/schematic-vue";
63
+
64
+ const { track } = useSchematicEvents();
65
+
66
+ function handleQuery() {
67
+ track({ event: "query" });
68
+ }
69
+ </script>
70
+
71
+ <template>
72
+ <button @click="handleQuery">Run Query</button>
73
+ </template>
74
+ ```
75
+
76
+ If you want to record large numbers of the same event at once, or perhaps measure usage in terms of a unit like tokens or memory, you can optionally specify a quantity for your event:
77
+
78
+ ```typescript
79
+ track({ event: "query", quantity: 10 });
80
+ ```
81
+
82
+ ### Checking flags
83
+
84
+ To check a flag, you can use the `useSchematicFlag` composable:
85
+
86
+ ```vue
87
+ <script setup lang="ts">
88
+ import { useSchematicFlag } from "@schematichq/schematic-vue";
89
+
90
+ const isFeatureEnabled = useSchematicFlag("my-flag-key");
91
+ </script>
92
+
93
+ <template>
94
+ <div v-if="isFeatureEnabled">
95
+ <Feature />
96
+ </div>
97
+ <div v-else>
98
+ <Fallback />
99
+ </div>
100
+ </template>
101
+ ```
102
+
103
+ ### Checking entitlements
104
+
105
+ You can check entitlements (i.e., company access to a feature) using a flag check as well, and using the `useSchematicEntitlement` composable you can get additional data to render various feature states:
106
+
107
+ ```vue
108
+ <script setup lang="ts">
109
+ import {
110
+ useSchematicEntitlement,
111
+ useSchematicIsPending,
112
+ } from "@schematichq/schematic-vue";
113
+
114
+ const schematicIsPending = useSchematicIsPending();
115
+ const {
116
+ featureAllocation,
117
+ featureUsage,
118
+ featureUsageExceeded,
119
+ value: isFeatureEnabled,
120
+ } = useSchematicEntitlement("my-flag-key");
121
+ </script>
122
+
123
+ <template>
124
+ <!-- Loading state -->
125
+ <Loader v-if="schematicIsPending" />
126
+
127
+ <!-- Usage exceeded state -->
128
+ <div v-else-if="featureUsageExceeded">
129
+ You have used all of your usage ({{ featureUsage }} / {{ featureAllocation }})
130
+ </div>
131
+
132
+ <!-- Either feature state or "no access" state -->
133
+ <Feature v-else-if="isFeatureEnabled" />
134
+ <NoAccess v-else />
135
+ </template>
136
+ ```
137
+
138
+ _Note: `useSchematicIsPending` is checking if entitlement data has been loaded, typically via `identify`. It should, therefore, be used to wrap flag and entitlement checks, but never the initial call to `identify`._
139
+
140
+ ## Options API Support
141
+
142
+ While the primary API uses the Composition API, you can still use these composables in the Options API:
143
+
144
+ ```vue
145
+ <script>
146
+ import { useSchematicFlag, useSchematicEvents } from "@schematichq/schematic-vue";
147
+
148
+ export default {
149
+ setup() {
150
+ const isFeatureEnabled = useSchematicFlag("my-flag-key");
151
+ const { track } = useSchematicEvents();
152
+
153
+ return {
154
+ isFeatureEnabled,
155
+ track,
156
+ };
157
+ },
158
+ methods: {
159
+ handleAction() {
160
+ this.track({ event: "action" });
161
+ },
162
+ },
163
+ };
164
+ </script>
165
+ ```
166
+
167
+ ## Troubleshooting
168
+
169
+ For debugging and development, Schematic supports two special modes:
170
+
171
+ ### Debug Mode
172
+
173
+ Enables console logging of all Schematic operations:
174
+
175
+ ```typescript
176
+ // Enable at initialization
177
+ import { createApp } from "vue";
178
+ import { SchematicPlugin } from "@schematichq/schematic-vue";
179
+
180
+ const app = createApp(App);
181
+ app.use(SchematicPlugin, {
182
+ publishableKey: "your-publishable-key",
183
+ debug: true,
184
+ });
185
+
186
+ // Or via URL parameter
187
+ // https://yoursite.com/?schematic_debug=true
188
+ ```
189
+
190
+ ### Offline Mode
191
+
192
+ Prevents network requests and returns fallback values for all flag checks:
193
+
194
+ ```typescript
195
+ // Enable at initialization
196
+ import { createApp } from "vue";
197
+ import { SchematicPlugin } from "@schematichq/schematic-vue";
198
+
199
+ const app = createApp(App);
200
+ app.use(SchematicPlugin, {
201
+ publishableKey: "your-publishable-key",
202
+ offline: true,
203
+ });
204
+
205
+ // Or via URL parameter
206
+ // https://yoursite.com/?schematic_offline=true
207
+ ```
208
+
209
+ Offline mode automatically enables debug mode to help with troubleshooting.
210
+
211
+ ## Advanced Usage
212
+
213
+ ### Using a Pre-configured Client
214
+
215
+ If you need more control over the Schematic client initialization, you can create a client instance and pass it to the plugin:
216
+
217
+ ```typescript
218
+ import { createApp } from "vue";
219
+ import { Schematic } from "@schematichq/schematic-js";
220
+ import { SchematicPlugin } from "@schematichq/schematic-vue";
221
+ import App from "./App.vue";
222
+
223
+ const client = new Schematic("your-publishable-key", {
224
+ useWebSocket: true,
225
+ debug: true,
226
+ });
227
+
228
+ const app = createApp(App);
229
+ app.use(SchematicPlugin, { client });
230
+ app.mount("#app");
231
+ ```
232
+
233
+ ### Per-Component Client Override
234
+
235
+ You can override the client for a specific component by passing a `client` option to any composable:
236
+
237
+ ```typescript
238
+ import { useSchematicFlag } from "@schematichq/schematic-vue";
239
+ import { Schematic } from "@schematichq/schematic-js";
240
+
241
+ const customClient = new Schematic("different-api-key");
242
+ const isFeatureEnabled = useSchematicFlag("my-flag-key", {
243
+ client: customClient,
244
+ });
245
+ ```
246
+
247
+ ## Server-Side Rendering (SSR)
248
+
249
+ All composables are SSR-compatible and work seamlessly with Nuxt and other Vue SSR frameworks:
250
+
251
+ - Initial flag/entitlement values are retrieved synchronously for server-side rendering
252
+ - Real-time subscriptions are deferred to client-side hydration
253
+ - No special configuration needed - it just works!
254
+
255
+ ```typescript
256
+ // plugins/schematic.ts
257
+ import { SchematicPlugin } from '@schematichq/schematic-vue'
258
+
259
+ export default defineNuxtPlugin((nuxtApp) => {
260
+ const config = useRuntimeConfig()
261
+ nuxtApp.vueApp.use(SchematicPlugin, { publishableKey: config.public.schematicPublishableKey })
262
+ })
263
+ ```
264
+
265
+ ```vue
266
+ <!-- Works in Nuxt/SSR -->
267
+ <script setup lang="ts">
268
+ import { useSchematicFlag } from "@schematichq/schematic-vue";
269
+
270
+ // Initial value available on server, updates subscribed on client
271
+ const isFeatureEnabled = useSchematicFlag("my-feature");
272
+ </script>
273
+ ```
274
+
275
+ ## License
276
+
277
+ MIT
278
+
279
+ ## Support
280
+
281
+ Need help? Please open a GitHub issue or reach out to [support@schematichq.com](mailto:support@schematichq.com) and we'll be happy to assist.