@posthog/convex 0.0.1 → 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 (47) hide show
  1. package/LICENSE +299 -0
  2. package/README.md +237 -29
  3. package/dist/client/index.d.ts +126 -0
  4. package/dist/client/index.d.ts.map +1 -0
  5. package/dist/client/index.js +213 -0
  6. package/dist/client/index.js.map +1 -0
  7. package/dist/component/_generated/api.d.ts +34 -0
  8. package/dist/component/_generated/api.d.ts.map +1 -0
  9. package/dist/component/_generated/api.js +31 -0
  10. package/dist/component/_generated/api.js.map +1 -0
  11. package/dist/component/_generated/component.d.ts +134 -0
  12. package/dist/component/_generated/component.d.ts.map +1 -0
  13. package/dist/component/_generated/component.js +11 -0
  14. package/dist/component/_generated/component.js.map +1 -0
  15. package/dist/component/_generated/dataModel.d.ts +46 -0
  16. package/dist/component/_generated/dataModel.d.ts.map +1 -0
  17. package/dist/component/_generated/dataModel.js +11 -0
  18. package/dist/component/_generated/dataModel.js.map +1 -0
  19. package/dist/component/_generated/server.d.ts +121 -0
  20. package/dist/component/_generated/server.d.ts.map +1 -0
  21. package/dist/component/_generated/server.js +78 -0
  22. package/dist/component/_generated/server.js.map +1 -0
  23. package/dist/component/convex.config.d.ts +3 -0
  24. package/dist/component/convex.config.d.ts.map +1 -0
  25. package/dist/component/convex.config.js +3 -0
  26. package/dist/component/convex.config.js.map +1 -0
  27. package/dist/component/lib.d.ts +119 -0
  28. package/dist/component/lib.d.ts.map +1 -0
  29. package/dist/component/lib.js +221 -0
  30. package/dist/component/lib.js.map +1 -0
  31. package/dist/component/schema.d.ts +3 -0
  32. package/dist/component/schema.d.ts.map +1 -0
  33. package/dist/component/schema.js +3 -0
  34. package/dist/component/schema.js.map +1 -0
  35. package/package.json +78 -7
  36. package/src/client/index.test.ts +505 -0
  37. package/src/client/index.ts +363 -0
  38. package/src/client/setup.test.ts +20 -0
  39. package/src/component/_generated/api.ts +50 -0
  40. package/src/component/_generated/component.ts +203 -0
  41. package/src/component/_generated/dataModel.ts +60 -0
  42. package/src/component/_generated/server.ts +156 -0
  43. package/src/component/convex.config.ts +3 -0
  44. package/src/component/lib.ts +259 -0
  45. package/src/component/schema.ts +3 -0
  46. package/src/component/setup.test.ts +11 -0
  47. package/src/test.ts +15 -0
@@ -0,0 +1,156 @@
1
+ /* eslint-disable */
2
+ /**
3
+ * Generated utilities for implementing server-side Convex query and mutation functions.
4
+ *
5
+ * THIS CODE IS AUTOMATICALLY GENERATED.
6
+ *
7
+ * To regenerate, run `npx convex dev`.
8
+ * @module
9
+ */
10
+
11
+ import type {
12
+ ActionBuilder,
13
+ HttpActionBuilder,
14
+ MutationBuilder,
15
+ QueryBuilder,
16
+ GenericActionCtx,
17
+ GenericMutationCtx,
18
+ GenericQueryCtx,
19
+ GenericDatabaseReader,
20
+ GenericDatabaseWriter,
21
+ } from "convex/server";
22
+ import {
23
+ actionGeneric,
24
+ httpActionGeneric,
25
+ queryGeneric,
26
+ mutationGeneric,
27
+ internalActionGeneric,
28
+ internalMutationGeneric,
29
+ internalQueryGeneric,
30
+ } from "convex/server";
31
+ import type { DataModel } from "./dataModel.js";
32
+
33
+ /**
34
+ * Define a query in this Convex app's public API.
35
+ *
36
+ * This function will be allowed to read your Convex database and will be accessible from the client.
37
+ *
38
+ * @param func - The query function. It receives a {@link QueryCtx} as its first argument.
39
+ * @returns The wrapped query. Include this as an `export` to name it and make it accessible.
40
+ */
41
+ export const query: QueryBuilder<DataModel, "public"> = queryGeneric;
42
+
43
+ /**
44
+ * Define a query that is only accessible from other Convex functions (but not from the client).
45
+ *
46
+ * This function will be allowed to read from your Convex database. It will not be accessible from the client.
47
+ *
48
+ * @param func - The query function. It receives a {@link QueryCtx} as its first argument.
49
+ * @returns The wrapped query. Include this as an `export` to name it and make it accessible.
50
+ */
51
+ export const internalQuery: QueryBuilder<DataModel, "internal"> =
52
+ internalQueryGeneric;
53
+
54
+ /**
55
+ * Define a mutation in this Convex app's public API.
56
+ *
57
+ * This function will be allowed to modify your Convex database and will be accessible from the client.
58
+ *
59
+ * @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
60
+ * @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
61
+ */
62
+ export const mutation: MutationBuilder<DataModel, "public"> = mutationGeneric;
63
+
64
+ /**
65
+ * Define a mutation that is only accessible from other Convex functions (but not from the client).
66
+ *
67
+ * This function will be allowed to modify your Convex database. It will not be accessible from the client.
68
+ *
69
+ * @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
70
+ * @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
71
+ */
72
+ export const internalMutation: MutationBuilder<DataModel, "internal"> =
73
+ internalMutationGeneric;
74
+
75
+ /**
76
+ * Define an action in this Convex app's public API.
77
+ *
78
+ * An action is a function which can execute any JavaScript code, including non-deterministic
79
+ * code and code with side-effects, like calling third-party services.
80
+ * They can be run in Convex's JavaScript environment or in Node.js using the "use node" directive.
81
+ * They can interact with the database indirectly by calling queries and mutations using the {@link ActionCtx}.
82
+ *
83
+ * @param func - The action. It receives an {@link ActionCtx} as its first argument.
84
+ * @returns The wrapped action. Include this as an `export` to name it and make it accessible.
85
+ */
86
+ export const action: ActionBuilder<DataModel, "public"> = actionGeneric;
87
+
88
+ /**
89
+ * Define an action that is only accessible from other Convex functions (but not from the client).
90
+ *
91
+ * @param func - The function. It receives an {@link ActionCtx} as its first argument.
92
+ * @returns The wrapped function. Include this as an `export` to name it and make it accessible.
93
+ */
94
+ export const internalAction: ActionBuilder<DataModel, "internal"> =
95
+ internalActionGeneric;
96
+
97
+ /**
98
+ * Define an HTTP action.
99
+ *
100
+ * The wrapped function will be used to respond to HTTP requests received
101
+ * by a Convex deployment if the requests matches the path and method where
102
+ * this action is routed. Be sure to route your httpAction in `convex/http.js`.
103
+ *
104
+ * @param func - The function. It receives an {@link ActionCtx} as its first argument
105
+ * and a Fetch API `Request` object as its second.
106
+ * @returns The wrapped function. Import this function from `convex/http.js` and route it to hook it up.
107
+ */
108
+ export const httpAction: HttpActionBuilder = httpActionGeneric;
109
+
110
+ /**
111
+ * A set of services for use within Convex query functions.
112
+ *
113
+ * The query context is passed as the first argument to any Convex query
114
+ * function run on the server.
115
+ *
116
+ * If you're using code generation, use the `QueryCtx` type in `convex/_generated/server.d.ts` instead.
117
+ */
118
+ export type QueryCtx = GenericQueryCtx<DataModel>;
119
+
120
+ /**
121
+ * A set of services for use within Convex mutation functions.
122
+ *
123
+ * The mutation context is passed as the first argument to any Convex mutation
124
+ * function run on the server.
125
+ *
126
+ * If you're using code generation, use the `MutationCtx` type in `convex/_generated/server.d.ts` instead.
127
+ */
128
+ export type MutationCtx = GenericMutationCtx<DataModel>;
129
+
130
+ /**
131
+ * A set of services for use within Convex action functions.
132
+ *
133
+ * The action context is passed as the first argument to any Convex action
134
+ * function run on the server.
135
+ */
136
+ export type ActionCtx = GenericActionCtx<DataModel>;
137
+
138
+ /**
139
+ * An interface to read from the database within Convex query functions.
140
+ *
141
+ * The two entry points are {@link DatabaseReader.get}, which fetches a single
142
+ * document by its {@link Id}, or {@link DatabaseReader.query}, which starts
143
+ * building a query.
144
+ */
145
+ export type DatabaseReader = GenericDatabaseReader<DataModel>;
146
+
147
+ /**
148
+ * An interface to read from and write to the database within Convex mutation
149
+ * functions.
150
+ *
151
+ * Convex guarantees that all writes within a single mutation are
152
+ * executed atomically, so you never have to worry about partial writes leaving
153
+ * your data in an inconsistent state. See [the Convex Guide](https://docs.convex.dev/understanding/convex-fundamentals/functions#atomicity-and-optimistic-concurrency-control)
154
+ * for the guarantees Convex provides your functions.
155
+ */
156
+ export type DatabaseWriter = GenericDatabaseWriter<DataModel>;
@@ -0,0 +1,3 @@
1
+ import { defineComponent } from 'convex/server'
2
+
3
+ export default defineComponent('posthog')
@@ -0,0 +1,259 @@
1
+ import type { FeatureFlagValue, JsonType } from '@posthog/core'
2
+ import { PostHog } from 'posthog-node/edge'
3
+ import { action } from './_generated/server.js'
4
+ import { v } from 'convex/values'
5
+
6
+ function createClient(apiKey: string, host: string) {
7
+ return new PostHog(apiKey, { host, flushAt: 1, flushInterval: 0 })
8
+ }
9
+
10
+ export const capture = action({
11
+ args: {
12
+ apiKey: v.string(),
13
+ host: v.string(),
14
+ distinctId: v.string(),
15
+ event: v.string(),
16
+ properties: v.optional(v.any()),
17
+ groups: v.optional(v.any()),
18
+ sendFeatureFlags: v.optional(v.boolean()),
19
+ timestamp: v.optional(v.number()),
20
+ uuid: v.optional(v.string()),
21
+ disableGeoip: v.optional(v.boolean()),
22
+ },
23
+ handler: async (_ctx, args) => {
24
+ const client = createClient(args.apiKey, args.host)
25
+ client.capture({
26
+ distinctId: args.distinctId,
27
+ event: args.event,
28
+ properties: args.properties,
29
+ groups: args.groups,
30
+ sendFeatureFlags: args.sendFeatureFlags,
31
+ timestamp: args.timestamp ? new Date(args.timestamp) : undefined,
32
+ uuid: args.uuid,
33
+ disableGeoip: args.disableGeoip,
34
+ })
35
+ await client.shutdown()
36
+ },
37
+ })
38
+
39
+ export const identify = action({
40
+ args: {
41
+ apiKey: v.string(),
42
+ host: v.string(),
43
+ distinctId: v.string(),
44
+ properties: v.optional(v.any()),
45
+ disableGeoip: v.optional(v.boolean()),
46
+ },
47
+ handler: async (_ctx, args) => {
48
+ const client = createClient(args.apiKey, args.host)
49
+ client.identify({
50
+ distinctId: args.distinctId,
51
+ properties: args.properties,
52
+ disableGeoip: args.disableGeoip,
53
+ })
54
+ await client.shutdown()
55
+ },
56
+ })
57
+
58
+ export const groupIdentify = action({
59
+ args: {
60
+ apiKey: v.string(),
61
+ host: v.string(),
62
+ groupType: v.string(),
63
+ groupKey: v.string(),
64
+ properties: v.optional(v.any()),
65
+ distinctId: v.optional(v.string()),
66
+ disableGeoip: v.optional(v.boolean()),
67
+ },
68
+ handler: async (_ctx, args) => {
69
+ const client = createClient(args.apiKey, args.host)
70
+ client.groupIdentify({
71
+ groupType: args.groupType,
72
+ groupKey: args.groupKey,
73
+ properties: args.properties,
74
+ distinctId: args.distinctId,
75
+ disableGeoip: args.disableGeoip,
76
+ })
77
+ await client.shutdown()
78
+ },
79
+ })
80
+
81
+ export const alias = action({
82
+ args: {
83
+ apiKey: v.string(),
84
+ host: v.string(),
85
+ distinctId: v.string(),
86
+ alias: v.string(),
87
+ disableGeoip: v.optional(v.boolean()),
88
+ },
89
+ handler: async (_ctx, args) => {
90
+ const client = createClient(args.apiKey, args.host)
91
+ client.alias({
92
+ distinctId: args.distinctId,
93
+ alias: args.alias,
94
+ disableGeoip: args.disableGeoip,
95
+ })
96
+ await client.shutdown()
97
+ },
98
+ })
99
+
100
+ export const captureException = action({
101
+ args: {
102
+ apiKey: v.string(),
103
+ host: v.string(),
104
+ distinctId: v.optional(v.string()),
105
+ errorMessage: v.string(),
106
+ errorStack: v.optional(v.string()),
107
+ errorName: v.optional(v.string()),
108
+ additionalProperties: v.optional(v.any()),
109
+ },
110
+ handler: async (_ctx, args) => {
111
+ const client = createClient(args.apiKey, args.host)
112
+ const error = new Error(args.errorMessage)
113
+ if (args.errorName) error.name = args.errorName
114
+ if (args.errorStack) error.stack = args.errorStack
115
+ client.captureException(error, args.distinctId, args.additionalProperties)
116
+ await client.shutdown()
117
+ },
118
+ })
119
+
120
+ // Feature flag actions — these return values and must be called via ctx.runAction
121
+
122
+ const featureFlagArgs = {
123
+ apiKey: v.string(),
124
+ host: v.string(),
125
+ key: v.string(),
126
+ distinctId: v.string(),
127
+ groups: v.optional(v.any()),
128
+ personProperties: v.optional(v.any()),
129
+ groupProperties: v.optional(v.any()),
130
+ sendFeatureFlagEvents: v.optional(v.boolean()),
131
+ disableGeoip: v.optional(v.boolean()),
132
+ }
133
+
134
+ function featureFlagOptions(args: {
135
+ groups?: Record<string, string>
136
+ personProperties?: Record<string, string>
137
+ groupProperties?: Record<string, Record<string, string>>
138
+ sendFeatureFlagEvents?: boolean
139
+ disableGeoip?: boolean
140
+ }) {
141
+ return {
142
+ groups: args.groups,
143
+ personProperties: args.personProperties,
144
+ groupProperties: args.groupProperties,
145
+ sendFeatureFlagEvents: args.sendFeatureFlagEvents,
146
+ disableGeoip: args.disableGeoip,
147
+ }
148
+ }
149
+
150
+ export const getFeatureFlag = action({
151
+ args: featureFlagArgs,
152
+ handler: async (_ctx, args): Promise<FeatureFlagValue | null> => {
153
+ const client = createClient(args.apiKey, args.host)
154
+ const result = await client.getFeatureFlag(args.key, args.distinctId, featureFlagOptions(args))
155
+ await client.shutdown()
156
+ return result ?? null
157
+ },
158
+ })
159
+
160
+ export const isFeatureEnabled = action({
161
+ args: featureFlagArgs,
162
+ handler: async (_ctx, args) => {
163
+ const client = createClient(args.apiKey, args.host)
164
+ const result = await client.isFeatureEnabled(args.key, args.distinctId, featureFlagOptions(args))
165
+ await client.shutdown()
166
+ return result ?? null
167
+ },
168
+ })
169
+
170
+ export const getFeatureFlagPayload = action({
171
+ args: {
172
+ ...featureFlagArgs,
173
+ matchValue: v.optional(v.union(v.string(), v.boolean())),
174
+ },
175
+ handler: async (_ctx, args): Promise<JsonType> => {
176
+ const client = createClient(args.apiKey, args.host)
177
+ const result = await client.getFeatureFlagPayload(
178
+ args.key,
179
+ args.distinctId,
180
+ args.matchValue,
181
+ featureFlagOptions(args)
182
+ )
183
+ await client.shutdown()
184
+ return result ?? null
185
+ },
186
+ })
187
+
188
+ export const getFeatureFlagResult = action({
189
+ args: featureFlagArgs,
190
+ handler: async (
191
+ _ctx,
192
+ args
193
+ ): Promise<{
194
+ key: string
195
+ enabled: boolean
196
+ variant: string | null
197
+ payload: JsonType | null
198
+ } | null> => {
199
+ const client = createClient(args.apiKey, args.host)
200
+ const result = await client.getFeatureFlagResult(args.key, args.distinctId, featureFlagOptions(args))
201
+ await client.shutdown()
202
+ if (!result) return null
203
+ return {
204
+ key: result.key,
205
+ enabled: result.enabled,
206
+ variant: result.variant ?? null,
207
+ payload: result.payload ?? null,
208
+ }
209
+ },
210
+ })
211
+
212
+ const allFlagsArgs = {
213
+ apiKey: v.string(),
214
+ host: v.string(),
215
+ distinctId: v.string(),
216
+ groups: v.optional(v.any()),
217
+ personProperties: v.optional(v.any()),
218
+ groupProperties: v.optional(v.any()),
219
+ disableGeoip: v.optional(v.boolean()),
220
+ flagKeys: v.optional(v.array(v.string())),
221
+ }
222
+
223
+ export const getAllFlags = action({
224
+ args: allFlagsArgs,
225
+ handler: async (_ctx, args): Promise<Record<string, FeatureFlagValue>> => {
226
+ const client = createClient(args.apiKey, args.host)
227
+ const result = await client.getAllFlags(args.distinctId, {
228
+ groups: args.groups,
229
+ personProperties: args.personProperties,
230
+ groupProperties: args.groupProperties,
231
+ disableGeoip: args.disableGeoip,
232
+ flagKeys: args.flagKeys,
233
+ })
234
+ await client.shutdown()
235
+ return result
236
+ },
237
+ })
238
+
239
+ export const getAllFlagsAndPayloads = action({
240
+ args: allFlagsArgs,
241
+ handler: async (
242
+ _ctx,
243
+ args
244
+ ): Promise<{
245
+ featureFlags?: Record<string, FeatureFlagValue>
246
+ featureFlagPayloads?: Record<string, JsonType>
247
+ }> => {
248
+ const client = createClient(args.apiKey, args.host)
249
+ const result = await client.getAllFlagsAndPayloads(args.distinctId, {
250
+ groups: args.groups,
251
+ personProperties: args.personProperties,
252
+ groupProperties: args.groupProperties,
253
+ disableGeoip: args.disableGeoip,
254
+ flagKeys: args.flagKeys,
255
+ })
256
+ await client.shutdown()
257
+ return result
258
+ },
259
+ })
@@ -0,0 +1,3 @@
1
+ import { defineSchema } from 'convex/server'
2
+
3
+ export default defineSchema({})
@@ -0,0 +1,11 @@
1
+ /// <reference types="vite/client" />
2
+ import { test } from '@jest/globals'
3
+ import schema from './schema.js'
4
+ import { convexTest } from 'convex-test'
5
+ export const modules = import.meta.glob('./**/*.*s')
6
+
7
+ export function initConvexTest() {
8
+ const t = convexTest(schema, modules)
9
+ return t
10
+ }
11
+ test('setup', () => {})
package/src/test.ts ADDED
@@ -0,0 +1,15 @@
1
+ /// <reference types="vite/client" />
2
+ import type { TestConvex } from 'convex-test'
3
+ import type { GenericSchema, SchemaDefinition } from 'convex/server'
4
+ import schema from './component/schema.js'
5
+ const modules = import.meta.glob('./component/**/*.ts')
6
+
7
+ /**
8
+ * Register the component with the test convex instance.
9
+ * @param t - The test convex instance, e.g. from calling `convexTest`.
10
+ * @param name - The name of the component, as registered in convex.config.ts.
11
+ */
12
+ export function register(t: TestConvex<SchemaDefinition<GenericSchema, boolean>>, name: string = 'posthog') {
13
+ t.registerComponent(name, schema, modules)
14
+ }
15
+ export default { register, schema, modules }