@convex-dev/workos-authkit 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.
- package/LICENSE +201 -0
- package/README.md +288 -0
- package/dist/client/_generated/_ignore.d.ts +1 -0
- package/dist/client/_generated/_ignore.d.ts.map +1 -0
- package/dist/client/_generated/_ignore.js +3 -0
- package/dist/client/_generated/_ignore.js.map +1 -0
- package/dist/client/index.d.ts +83 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +177 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/types.d.ts +27 -0
- package/dist/client/types.d.ts.map +1 -0
- package/dist/client/types.js +2 -0
- package/dist/client/types.js.map +1 -0
- package/dist/component/_generated/api.d.ts +112 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +50 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +46 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/component/_generated/dataModel.js +11 -0
- package/dist/component/_generated/dataModel.js.map +1 -0
- package/dist/component/_generated/server.d.ts +121 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +78 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/convex.config.d.ts +3 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +6 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/lib.d.ts +43 -0
- package/dist/component/lib.d.ts.map +1 -0
- package/dist/component/lib.js +177 -0
- package/dist/component/lib.js.map +1 -0
- package/dist/component/schema.d.ts +44 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +24 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/react/index.d.ts +2 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +6 -0
- package/dist/react/index.js.map +1 -0
- package/package.json +108 -0
- package/src/client/_generated/_ignore.ts +1 -0
- package/src/client/index.ts +278 -0
- package/src/client/types.ts +71 -0
- package/src/component/_generated/api.ts +138 -0
- package/src/component/_generated/component.ts +63 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +161 -0
- package/src/component/convex.config.ts +8 -0
- package/src/component/lib.ts +189 -0
- package/src/component/schema.ts +24 -0
- package/src/component/setup.test.ts +5 -0
- package/src/react/index.ts +7 -0
- package/src/test.ts +17 -0
|
@@ -0,0 +1,161 @@
|
|
|
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
|
+
type GenericCtx =
|
|
111
|
+
| GenericActionCtx<DataModel>
|
|
112
|
+
| GenericMutationCtx<DataModel>
|
|
113
|
+
| GenericQueryCtx<DataModel>;
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* A set of services for use within Convex query functions.
|
|
117
|
+
*
|
|
118
|
+
* The query context is passed as the first argument to any Convex query
|
|
119
|
+
* function run on the server.
|
|
120
|
+
*
|
|
121
|
+
* If you're using code generation, use the `QueryCtx` type in `convex/_generated/server.d.ts` instead.
|
|
122
|
+
*/
|
|
123
|
+
export type QueryCtx = GenericQueryCtx<DataModel>;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* A set of services for use within Convex mutation functions.
|
|
127
|
+
*
|
|
128
|
+
* The mutation context is passed as the first argument to any Convex mutation
|
|
129
|
+
* function run on the server.
|
|
130
|
+
*
|
|
131
|
+
* If you're using code generation, use the `MutationCtx` type in `convex/_generated/server.d.ts` instead.
|
|
132
|
+
*/
|
|
133
|
+
export type MutationCtx = GenericMutationCtx<DataModel>;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* A set of services for use within Convex action functions.
|
|
137
|
+
*
|
|
138
|
+
* The action context is passed as the first argument to any Convex action
|
|
139
|
+
* function run on the server.
|
|
140
|
+
*/
|
|
141
|
+
export type ActionCtx = GenericActionCtx<DataModel>;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* An interface to read from the database within Convex query functions.
|
|
145
|
+
*
|
|
146
|
+
* The two entry points are {@link DatabaseReader.get}, which fetches a single
|
|
147
|
+
* document by its {@link Id}, or {@link DatabaseReader.query}, which starts
|
|
148
|
+
* building a query.
|
|
149
|
+
*/
|
|
150
|
+
export type DatabaseReader = GenericDatabaseReader<DataModel>;
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* An interface to read from and write to the database within Convex mutation
|
|
154
|
+
* functions.
|
|
155
|
+
*
|
|
156
|
+
* Convex guarantees that all writes within a single mutation are
|
|
157
|
+
* executed atomically, so you never have to worry about partial writes leaving
|
|
158
|
+
* your data in an inconsistent state. See [the Convex Guide](https://docs.convex.dev/understanding/convex-fundamentals/functions#atomicity-and-optimistic-concurrency-control)
|
|
159
|
+
* for the guarantees Convex provides your functions.
|
|
160
|
+
*/
|
|
161
|
+
export type DatabaseWriter = GenericDatabaseWriter<DataModel>;
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
import {
|
|
3
|
+
internalAction,
|
|
4
|
+
internalMutation,
|
|
5
|
+
internalQuery,
|
|
6
|
+
mutation,
|
|
7
|
+
query,
|
|
8
|
+
} from "./_generated/server.js";
|
|
9
|
+
import { components, internal } from "./_generated/api.js";
|
|
10
|
+
import { omit, withoutSystemFields } from "convex-helpers";
|
|
11
|
+
import { WorkOS, type Event as WorkOSEvent } from "@workos-inc/node";
|
|
12
|
+
import type { FunctionHandle } from "convex/server";
|
|
13
|
+
import { Workpool } from "@convex-dev/workpool";
|
|
14
|
+
import schema from "./schema.js";
|
|
15
|
+
|
|
16
|
+
const eventWorkpool = new Workpool(components.eventWorkpool, {
|
|
17
|
+
maxParallelism: 1,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
export const enqueueWebhookEvent = mutation({
|
|
21
|
+
args: {
|
|
22
|
+
apiKey: v.string(),
|
|
23
|
+
eventId: v.string(),
|
|
24
|
+
event: v.string(),
|
|
25
|
+
updatedAt: v.optional(v.string()),
|
|
26
|
+
onEventHandle: v.optional(v.string()),
|
|
27
|
+
eventTypes: v.optional(v.array(v.string())),
|
|
28
|
+
logLevel: v.optional(v.literal("DEBUG")),
|
|
29
|
+
},
|
|
30
|
+
handler: async (ctx, args) => {
|
|
31
|
+
await eventWorkpool.cancelAll(ctx);
|
|
32
|
+
await eventWorkpool.enqueueAction(ctx, internal.lib.updateEvents, {
|
|
33
|
+
apiKey: args.apiKey,
|
|
34
|
+
onEventHandle: args.onEventHandle,
|
|
35
|
+
eventTypes: args.eventTypes,
|
|
36
|
+
logLevel: args.logLevel,
|
|
37
|
+
});
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
export const getCursor = internalQuery({
|
|
42
|
+
args: {},
|
|
43
|
+
returns: v.union(v.string(), v.null()),
|
|
44
|
+
handler: async (ctx) => {
|
|
45
|
+
const lastProcessedEvent = await ctx.db
|
|
46
|
+
.query("events")
|
|
47
|
+
.order("desc")
|
|
48
|
+
.first();
|
|
49
|
+
return lastProcessedEvent?.eventId;
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
export const updateEvents = internalAction({
|
|
54
|
+
args: {
|
|
55
|
+
apiKey: v.string(),
|
|
56
|
+
onEventHandle: v.optional(v.string()),
|
|
57
|
+
eventTypes: v.optional(v.array(v.string())),
|
|
58
|
+
logLevel: v.optional(v.literal("DEBUG")),
|
|
59
|
+
},
|
|
60
|
+
handler: async (ctx, args) => {
|
|
61
|
+
const workos = new WorkOS(args.apiKey);
|
|
62
|
+
const cursor = await ctx.runQuery(internal.lib.getCursor);
|
|
63
|
+
let nextCursor = cursor ?? undefined;
|
|
64
|
+
const eventTypes = [
|
|
65
|
+
"user.created" as const,
|
|
66
|
+
"user.updated" as const,
|
|
67
|
+
"user.deleted" as const,
|
|
68
|
+
...((args.eventTypes as WorkOSEvent["event"][]) ?? []),
|
|
69
|
+
];
|
|
70
|
+
// No cursor should mean we haven't handled any events - set
|
|
71
|
+
// a start time of 5 minutes ago
|
|
72
|
+
let rangeStart = nextCursor
|
|
73
|
+
? undefined
|
|
74
|
+
: new Date(Date.now() - 1000 * 60 * 5).toISOString();
|
|
75
|
+
do {
|
|
76
|
+
const { data, listMetadata } = await workos.events.listEvents({
|
|
77
|
+
events: eventTypes,
|
|
78
|
+
after: nextCursor,
|
|
79
|
+
rangeStart,
|
|
80
|
+
});
|
|
81
|
+
for (const event of data) {
|
|
82
|
+
await ctx.runMutation(internal.lib.processEvent, {
|
|
83
|
+
event,
|
|
84
|
+
logLevel: args.logLevel,
|
|
85
|
+
onEventHandle: args.onEventHandle,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
nextCursor = listMetadata.after;
|
|
89
|
+
rangeStart = undefined;
|
|
90
|
+
} while (nextCursor);
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
export const processEvent = internalMutation({
|
|
95
|
+
args: {
|
|
96
|
+
event: v.object({
|
|
97
|
+
id: v.string(),
|
|
98
|
+
createdAt: v.string(),
|
|
99
|
+
event: v.string(),
|
|
100
|
+
data: v.record(v.string(), v.any()),
|
|
101
|
+
}),
|
|
102
|
+
logLevel: v.optional(v.literal("DEBUG")),
|
|
103
|
+
onEventHandle: v.optional(v.string()),
|
|
104
|
+
},
|
|
105
|
+
handler: async (ctx, args) => {
|
|
106
|
+
if (args.logLevel === "DEBUG") {
|
|
107
|
+
console.log("processing event", args.event);
|
|
108
|
+
}
|
|
109
|
+
const dbEvent = await ctx.db
|
|
110
|
+
.query("events")
|
|
111
|
+
.withIndex("eventId", (q) => q.eq("eventId", args.event.id))
|
|
112
|
+
.unique();
|
|
113
|
+
if (dbEvent) {
|
|
114
|
+
console.log("event already processed", args.event.id);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
await ctx.db.insert("events", {
|
|
118
|
+
eventId: args.event.id,
|
|
119
|
+
event: args.event.event,
|
|
120
|
+
updatedAt: args.event.data.updatedAt,
|
|
121
|
+
});
|
|
122
|
+
const event = args.event as WorkOSEvent;
|
|
123
|
+
switch (event.event) {
|
|
124
|
+
case "user.created": {
|
|
125
|
+
const data = omit(event.data, ["object"]);
|
|
126
|
+
const existingUser = await ctx.db
|
|
127
|
+
.query("users")
|
|
128
|
+
.withIndex("id", (q) => q.eq("id", data.id))
|
|
129
|
+
.unique();
|
|
130
|
+
if (existingUser) {
|
|
131
|
+
console.warn("user already exists", data.id);
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
await ctx.db.insert("users", data);
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
case "user.updated": {
|
|
138
|
+
const data = omit(event.data, ["object"]);
|
|
139
|
+
const user = await ctx.db
|
|
140
|
+
.query("users")
|
|
141
|
+
.withIndex("id", (q) => q.eq("id", data.id))
|
|
142
|
+
.unique();
|
|
143
|
+
if (!user) {
|
|
144
|
+
console.error("user not found", data.id);
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
if (user.updatedAt >= data.updatedAt) {
|
|
148
|
+
console.warn(`user already updated for event ${event.id}, skipping`);
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
await ctx.db.patch(user._id, data);
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
case "user.deleted": {
|
|
155
|
+
const data = omit(event.data, ["object"]);
|
|
156
|
+
const user = await ctx.db
|
|
157
|
+
.query("users")
|
|
158
|
+
.withIndex("id", (q) => q.eq("id", data.id))
|
|
159
|
+
.unique();
|
|
160
|
+
if (!user) {
|
|
161
|
+
console.warn("user not found", data.id);
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
await ctx.db.delete(user._id);
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (args.onEventHandle) {
|
|
169
|
+
await ctx.runMutation(args.onEventHandle as FunctionHandle<"mutation">, {
|
|
170
|
+
event: args.event.event,
|
|
171
|
+
data: args.event.data,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
export const getAuthUser = query({
|
|
178
|
+
args: {
|
|
179
|
+
id: v.string(),
|
|
180
|
+
},
|
|
181
|
+
returns: v.union(schema.tables.users.validator, v.null()),
|
|
182
|
+
handler: async (ctx, args) => {
|
|
183
|
+
const user = await ctx.db
|
|
184
|
+
.query("users")
|
|
185
|
+
.withIndex("id", (q) => q.eq("id", args.id))
|
|
186
|
+
.unique();
|
|
187
|
+
return user ? withoutSystemFields(user) : null;
|
|
188
|
+
},
|
|
189
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { defineSchema, defineTable } from "convex/server";
|
|
2
|
+
import { v } from "convex/values";
|
|
3
|
+
|
|
4
|
+
export default defineSchema({
|
|
5
|
+
events: defineTable({
|
|
6
|
+
eventId: v.string(),
|
|
7
|
+
event: v.string(),
|
|
8
|
+
updatedAt: v.optional(v.string()),
|
|
9
|
+
}).index("eventId", ["eventId"]),
|
|
10
|
+
users: defineTable({
|
|
11
|
+
id: v.string(),
|
|
12
|
+
email: v.string(),
|
|
13
|
+
firstName: v.optional(v.union(v.null(), v.string())),
|
|
14
|
+
lastName: v.optional(v.union(v.null(), v.string())),
|
|
15
|
+
emailVerified: v.boolean(),
|
|
16
|
+
profilePictureUrl: v.optional(v.union(v.null(), v.string())),
|
|
17
|
+
lastSignInAt: v.optional(v.union(v.null(), v.string())),
|
|
18
|
+
externalId: v.optional(v.union(v.null(), v.string())),
|
|
19
|
+
metadata: v.record(v.string(), v.any()),
|
|
20
|
+
locale: v.optional(v.union(v.null(), v.string())),
|
|
21
|
+
createdAt: v.string(),
|
|
22
|
+
updatedAt: v.string(),
|
|
23
|
+
}).index("id", ["id"]),
|
|
24
|
+
});
|
package/src/test.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { TestConvex } from "convex-test";
|
|
2
|
+
import type { GenericSchema, SchemaDefinition } from "convex/server";
|
|
3
|
+
import schema from "./component/schema.js";
|
|
4
|
+
const modules = import.meta.glob("./component/**/*.ts");
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Register the component with the test convex instance.
|
|
8
|
+
* @param t - The test convex instance, e.g. from calling `convexTest`.
|
|
9
|
+
* @param name - The name of the component, as registered in convex.config.ts.
|
|
10
|
+
*/
|
|
11
|
+
function register(
|
|
12
|
+
t: TestConvex<SchemaDefinition<GenericSchema, boolean>>,
|
|
13
|
+
name: string = "workOSAuthKit"
|
|
14
|
+
) {
|
|
15
|
+
t.registerComponent(name, schema, modules);
|
|
16
|
+
}
|
|
17
|
+
export default { register, schema, modules };
|