create-disposabl-app 0.8.10 → 0.8.11

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 (66) hide show
  1. package/README.md +8 -1
  2. package/dist/disposabl.js +1 -1
  3. package/dist/index.js +1 -1
  4. package/dist/templates/default/bun.lock +1 -7
  5. package/dist/templates/default/packages/backend/README.md +4 -8
  6. package/dist/templates/default/packages/backend/convex/_generated/api.d.ts +0 -67
  7. package/dist/templates/default/packages/backend/convex/_generated/server.d.ts +0 -9
  8. package/dist/templates/default/packages/backend/convex/convex.config.ts +0 -11
  9. package/dist/templates/default/packages/backend/convex/http.ts +1 -9
  10. package/dist/templates/default/packages/backend/convex/schema.ts +1 -70
  11. package/dist/templates/default/packages/backend/generated/openapi.json +1 -1262
  12. package/dist/templates/default/packages/backend/generated/pages.json +0 -4
  13. package/dist/templates/default/packages/backend/generated/platforms.ts +2 -20
  14. package/dist/templates/default/packages/backend/package.json +1 -7
  15. package/dist/templates/default/packages/skill/SKILL.md +5 -5
  16. package/dist/templates/default/packages/skill/references/delivery.md +1 -1
  17. package/package.json +1 -1
  18. package/dist/templates/default/packages/backend/convex/apps/actions/cleanup.ts +0 -317
  19. package/dist/templates/default/packages/backend/convex/apps/actions/create.ts +0 -30
  20. package/dist/templates/default/packages/backend/convex/apps/actions/delete.ts +0 -15
  21. package/dist/templates/default/packages/backend/convex/apps/actions/expo.ts +0 -180
  22. package/dist/templates/default/packages/backend/convex/apps/actions/setup.ts +0 -48
  23. package/dist/templates/default/packages/backend/convex/apps/internal.ts +0 -211
  24. package/dist/templates/default/packages/backend/convex/apps/operations/internal.ts +0 -239
  25. package/dist/templates/default/packages/backend/convex/apps/validator.ts +0 -23
  26. package/dist/templates/default/packages/backend/convex/connections/actions/credentials.ts +0 -56
  27. package/dist/templates/default/packages/backend/convex/connections/actions/expo.ts +0 -79
  28. package/dist/templates/default/packages/backend/convex/connections/actions/github.ts +0 -119
  29. package/dist/templates/default/packages/backend/convex/connections/actions/oauth.ts +0 -128
  30. package/dist/templates/default/packages/backend/convex/connections/index.ts +0 -80
  31. package/dist/templates/default/packages/backend/convex/connections/internal.ts +0 -147
  32. package/dist/templates/default/packages/backend/convex/connections/validator.ts +0 -35
  33. package/dist/templates/default/packages/backend/convex/downloads/action.ts +0 -136
  34. package/dist/templates/default/packages/backend/convex/downloads/http.ts +0 -32
  35. package/dist/templates/default/packages/backend/convex/downloads/internal.ts +0 -58
  36. package/dist/templates/default/packages/backend/convex/github/actions/http.ts +0 -69
  37. package/dist/templates/default/packages/backend/convex/github/actions/internal.ts +0 -196
  38. package/dist/templates/default/packages/backend/convex/github/actions/oidc.ts +0 -75
  39. package/dist/templates/default/packages/backend/convex/github/actions/provision.ts +0 -76
  40. package/dist/templates/default/packages/backend/convex/github/actions/secrets.ts +0 -180
  41. package/dist/templates/default/packages/backend/convex/github/actions/signing.ts +0 -41
  42. package/dist/templates/default/packages/backend/convex/github/actions/tokens.ts +0 -145
  43. package/dist/templates/default/packages/backend/convex/github/identityCache.ts +0 -54
  44. package/dist/templates/default/packages/backend/convex/github/internal.ts +0 -33
  45. package/dist/templates/default/packages/backend/convex/previews/internal.ts +0 -257
  46. package/dist/templates/default/packages/backend/convex/previews/token/actions/create.ts +0 -83
  47. package/dist/templates/default/packages/backend/convex/previews/token/actions/management.ts +0 -182
  48. package/dist/templates/default/packages/backend/convex/previews/token/internal.ts +0 -32
  49. package/dist/templates/default/packages/backend/convex/releases/internal.ts +0 -28
  50. package/dist/templates/default/packages/backend/convex/validator.ts +0 -87
  51. package/dist/templates/default/packages/sdk/src/generated/client/client.gen.ts +0 -300
  52. package/dist/templates/default/packages/sdk/src/generated/client/index.ts +0 -27
  53. package/dist/templates/default/packages/sdk/src/generated/client/types.gen.ts +0 -238
  54. package/dist/templates/default/packages/sdk/src/generated/client/utils.gen.ts +0 -337
  55. package/dist/templates/default/packages/sdk/src/generated/client.gen.ts +0 -28
  56. package/dist/templates/default/packages/sdk/src/generated/core/auth.gen.ts +0 -49
  57. package/dist/templates/default/packages/sdk/src/generated/core/bodySerializer.gen.ts +0 -96
  58. package/dist/templates/default/packages/sdk/src/generated/core/params.gen.ts +0 -188
  59. package/dist/templates/default/packages/sdk/src/generated/core/pathSerializer.gen.ts +0 -186
  60. package/dist/templates/default/packages/sdk/src/generated/core/queryKeySerializer.gen.ts +0 -134
  61. package/dist/templates/default/packages/sdk/src/generated/core/serverSentEvents.gen.ts +0 -265
  62. package/dist/templates/default/packages/sdk/src/generated/core/types.gen.ts +0 -126
  63. package/dist/templates/default/packages/sdk/src/generated/core/utils.gen.ts +0 -146
  64. package/dist/templates/default/packages/sdk/src/generated/index.ts +0 -63
  65. package/dist/templates/default/packages/sdk/src/generated/sdk.gen.ts +0 -234
  66. package/dist/templates/default/packages/sdk/src/generated/types.gen.ts +0 -660
@@ -1,48 +0,0 @@
1
- "use node";
2
-
3
- import {
4
- identityFields,
5
- internalAction,
6
- } from "@disposabl/disposabl-kit/server/lib/functions";
7
- import { v } from "convex/values";
8
- import { internal } from "../../_generated/api";
9
- import { credential } from "../../connections/actions/credentials";
10
- import {
11
- ensureProject,
12
- management,
13
- } from "../../previews/token/actions/management";
14
-
15
- export const setup = internalAction({
16
- args: { ...identityFields, appId: v.string() },
17
- returns: v.number(),
18
- handler: async (ctx, args): Promise<number> => {
19
- const saved = await ctx.runQuery(internal.apps.internal.project, args);
20
- const { token, accountId: teamId } = await credential(
21
- ctx,
22
- args.userId,
23
- "convex",
24
- );
25
- if (saved !== null) {
26
- const projects = await management<{ items: { id: number }[] }>(
27
- token,
28
- `/teams/${teamId}/projects?q=${encodeURIComponent(`disposabl-${args.appId}`)}&limit=100`,
29
- );
30
- if (!projects.items.some((project) => project.id === saved))
31
- throw new Error(
32
- "Reconnect the Convex team that owns this app before continuing setup.",
33
- );
34
- }
35
- const projectId = saved ?? (await ensureProject(token, teamId, args.appId));
36
- if (saved === null)
37
- await ctx.runMutation(internal.apps.internal.saveProject, {
38
- ...args,
39
- projectId,
40
- });
41
- await ctx.runAction(internal.github.actions.provision.run, args);
42
- await ctx.runAction(internal.github.actions.secrets.configure, {
43
- ...args,
44
- projectId,
45
- });
46
- return projectId;
47
- },
48
- });
@@ -1,211 +0,0 @@
1
- import {
2
- identityFields,
3
- internalMutation,
4
- internalQuery,
5
- } from "@disposabl/disposabl-kit/server/lib/functions";
6
- import {
7
- paginationOptsValidator,
8
- paginationResultValidator,
9
- } from "convex/server";
10
- import { ConvexError, v, type Infer } from "convex/values";
11
- import { appArgs, appResult, platform } from "../validator";
12
- import { validatePlatforms } from "@disposabl/disposabl-kit/platforms";
13
- import { githubConnection } from "../validator";
14
- import type { Doc } from "../_generated/dataModel";
15
- import type { QueryCtx } from "../_generated/server";
16
- type AppResult = Infer<typeof appResult>;
17
-
18
- export { create } from "./actions/create";
19
- export const insert = internalMutation({
20
- args: { ...identityFields, ...appArgs, appId: v.string() },
21
- returns: appResult,
22
- handler: async (ctx, args) => {
23
- assertAppId(args.appId);
24
- const name = args.name.trim();
25
- if (!name || name.length > 214)
26
- throw new ConvexError("App name must contain 1–214 characters.");
27
- const platforms = [...new Set(args.platforms)];
28
- const id = await ctx.db.insert("apps", {
29
- userId: args.userId,
30
- appId: args.appId,
31
- name,
32
- platforms,
33
- expoAccountId: null,
34
- });
35
- return presentApp((await ctx.db.get("apps", id))!);
36
- },
37
- });
38
- export const list = internalQuery({
39
- platforms: true,
40
- args: { ...identityFields, paginationOpts: paginationOptsValidator },
41
- returns: paginationResultValidator(appResult),
42
- handler: async (ctx, { userId, paginationOpts }) => {
43
- if (
44
- !Number.isInteger(paginationOpts.numItems) ||
45
- paginationOpts.numItems < 1 ||
46
- paginationOpts.numItems > 100
47
- )
48
- throw new ConvexError("Page size must be between 1 and 100.");
49
- const result = await ctx.db
50
- .query("apps")
51
- .withIndex("by_userId", (q) => q.eq("userId", userId))
52
- .order("desc")
53
- .paginate(paginationOpts);
54
- return { ...result, page: result.page.map(presentApp) };
55
- },
56
- });
57
- export const get = internalQuery({
58
- platforms: true,
59
- args: { ...identityFields, appId: v.string() },
60
- returns: appResult,
61
- handler: async (ctx, { userId, appId }) =>
62
- presentApp(await ownedApp(ctx, userId, appId)),
63
- });
64
- export const update = internalMutation({
65
- platforms: true,
66
- args: { ...identityFields, appId: v.string(), platforms: v.array(platform) },
67
- returns: appResult,
68
- handler: async (ctx, { userId, appId, platforms }) => {
69
- const app = await ownedApp(ctx, userId, appId);
70
- if (app.operation)
71
- throw new ConvexError(
72
- "Wait for app setup to finish before changing platforms.",
73
- );
74
- const selected = validatePlatforms(platforms);
75
- await ctx.db.patch("apps", app._id, { platforms: selected });
76
- return presentApp({ ...app, platforms: selected });
77
- },
78
- });
79
- export { remove as delete } from "./actions/delete";
80
-
81
- function assertAppId(appId: string) {
82
- if (
83
- !/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(
84
- appId,
85
- )
86
- )
87
- throw new ConvexError("appId must be a lowercase UUID v4.");
88
- }
89
- export async function ownedApp(
90
- ctx: Pick<QueryCtx, "db">,
91
- userId: string,
92
- appId: string,
93
- ) {
94
- assertAppId(appId);
95
- const app = await ctx.db
96
- .query("apps")
97
- .withIndex("by_appId", (q) => q.eq("appId", appId))
98
- .unique();
99
- if (!app || app.userId !== userId) throw new ConvexError("App not found.");
100
- return app;
101
- }
102
- function presentApp(app: Doc<"apps">): AppResult {
103
- return {
104
- appId: app.appId,
105
- name: app.name,
106
- platforms: app.platforms,
107
- ...(app.githubOwner ? { githubOwner: app.githubOwner } : {}),
108
- ...(app.githubRepo ? { githubRepo: app.githubRepo } : {}),
109
- createdAt: app._creationTime,
110
- };
111
- }
112
- export const githubApp = internalQuery({
113
- args: { appId: v.string() },
114
- returns: v.union(
115
- v.null(),
116
- v.object({
117
- githubOwner: v.string(),
118
- githubRepo: v.string(),
119
- installationId: v.string(),
120
- userId: v.string(),
121
- }),
122
- ),
123
- handler: async (ctx, { appId }) => {
124
- assertAppId(appId);
125
- const app = await ctx.db
126
- .query("apps")
127
- .withIndex("by_appId", (q) => q.eq("appId", appId))
128
- .unique();
129
- if (!app?.githubOwner || !app.githubRepo) return null;
130
- const connection = await ctx.db
131
- .query("githubConnections")
132
- .withIndex("by_userId_owner", (q) =>
133
- q.eq("userId", app.userId).eq("owner", app.githubOwner!),
134
- )
135
- .unique();
136
- const installationId = connection?.installationId;
137
- return installationId
138
- ? {
139
- githubOwner: app.githubOwner,
140
- githubRepo: app.githubRepo,
141
- installationId,
142
- userId: app.userId,
143
- }
144
- : null;
145
- },
146
- });
147
- /** Only trusted repository provisioning can link an app. */
148
- export const connectGithub = internalMutation({
149
- args: {
150
- ...identityFields,
151
- appId: v.string(),
152
- githubOwner: v.string(),
153
- githubRepo: v.string(),
154
- },
155
- returns: v.null(),
156
- handler: async (ctx, { userId, appId, githubOwner, githubRepo }) => {
157
- const app = await ownedApp(ctx, userId, appId);
158
- githubOwner = githubOwner.toLowerCase();
159
- if (
160
- app.githubOwner &&
161
- (app.githubOwner !== githubOwner || app.githubRepo !== githubRepo)
162
- )
163
- throw new ConvexError("App is already linked to a different repository.");
164
- await ctx.db.patch("apps", app._id, { githubOwner, githubRepo });
165
- return null;
166
- },
167
- });
168
- export const githubSetup = internalQuery({
169
- args: { ...identityFields, appId: v.string() },
170
- returns: v.object({
171
- app: appResult,
172
- connection: v.union(githubConnection, v.null()),
173
- }),
174
- handler: async (ctx, { userId, appId }) => {
175
- const app = await ownedApp(ctx, userId, appId);
176
- const connection = await ctx.db
177
- .query("githubConnections")
178
- .withIndex("by_userId", (q) => q.eq("userId", userId))
179
- .order("desc")
180
- .first();
181
- return {
182
- app: presentApp(app),
183
- connection: connection
184
- ? {
185
- owner: connection.owner,
186
- installationId: connection.installationId,
187
- }
188
- : null,
189
- };
190
- },
191
- });
192
-
193
- export const project = internalQuery({
194
- args: { ...identityFields, appId: v.string() },
195
- returns: v.union(v.number(), v.null()),
196
- handler: async (ctx, { userId, appId }) =>
197
- (await ownedApp(ctx, userId, appId)).convexProjectId ?? null,
198
- });
199
- export const saveProject = internalMutation({
200
- args: {
201
- ...identityFields,
202
- appId: v.string(),
203
- projectId: v.number(),
204
- },
205
- returns: v.null(),
206
- handler: async (ctx, args) => {
207
- const app = await ownedApp(ctx, args.userId, args.appId);
208
- await ctx.db.patch("apps", app._id, { convexProjectId: args.projectId });
209
- return null;
210
- },
211
- });
@@ -1,239 +0,0 @@
1
- import {
2
- identityFields,
3
- internalMutation,
4
- internalQuery,
5
- } from "@disposabl/disposabl-kit/server/lib/functions";
6
- import { ConvexError, v } from "convex/values";
7
- import type { Doc } from "../../_generated/dataModel";
8
- import type { QueryCtx } from "../../_generated/server";
9
- import { ownedApp } from "../internal";
10
- import { pendingDelete, resources } from "../validator";
11
-
12
- export function assertActive(app: Doc<"apps">) {
13
- if (app.operation?.kind === "delete")
14
- throw new ConvexError(
15
- "App deletion is in progress. Retry apps delete to finish cleanup.",
16
- );
17
- }
18
- async function assertPreviewOnly(ctx: Pick<QueryCtx, "db">, appId: string) {
19
- const release = await ctx.db
20
- .query("appReleases")
21
- .withIndex("by_appId", (q) => q.eq("appId", appId))
22
- .unique();
23
- if (release)
24
- throw new ConvexError("Apps with production releases cannot be deleted.");
25
- }
26
-
27
- // One lease prevents setup, deletion and production publishing from racing.
28
- export const begin = internalMutation({
29
- args: {
30
- ...identityFields,
31
- appId: v.string(),
32
- kind: v.union(v.literal("setup"), v.literal("delete")),
33
- },
34
- returns: v.object({
35
- until: v.number(),
36
- resuming: v.boolean(),
37
- pending: v.optional(pendingDelete),
38
- expoManual: v.optional(v.boolean()),
39
- }),
40
- handler: async (ctx, { userId, appId, kind }) => {
41
- const app = await ownedApp(ctx, userId, appId);
42
- if (kind === "setup") assertActive(app);
43
- else await assertPreviewOnly(ctx, appId);
44
- if (app.operation && app.operation.until > Date.now())
45
- throw new ConvexError(
46
- "This app has an operation in progress. Try again when it finishes.",
47
- );
48
- // Longer than Convex's maximum action runtime, including nested actions.
49
- const until = Date.now() + 15 * 60_000;
50
- await ctx.db.patch("apps", app._id, {
51
- operation: { ...app.operation, kind, until },
52
- });
53
- return {
54
- until,
55
- resuming: app.operation?.kind === "delete",
56
- pending: app.operation?.pending,
57
- expoManual: app.operation?.expoManual,
58
- };
59
- },
60
- });
61
- export const finish = internalMutation({
62
- args: {
63
- ...identityFields,
64
- appId: v.string(),
65
- until: v.number(),
66
- retryDelete: v.boolean(),
67
- },
68
- returns: v.null(),
69
- handler: async (ctx, { userId, appId, until, retryDelete }) => {
70
- const app = await ownedApp(ctx, userId, appId);
71
- if (app.operation?.until === until)
72
- await ctx.db.patch("apps", app._id, {
73
- operation: retryDelete
74
- ? { ...app.operation, kind: "delete", until: 0 }
75
- : undefined,
76
- });
77
- return null;
78
- },
79
- });
80
- export const linked = internalQuery({
81
- args: { ...identityFields, appId: v.string() },
82
- returns: resources,
83
- handler: async (ctx, { userId, appId }) => {
84
- const {
85
- githubOwner,
86
- githubRepo,
87
- convexProjectId,
88
- expoProjectId,
89
- expoAccountId,
90
- } = await ownedApp(ctx, userId, appId);
91
- return {
92
- githubOwner,
93
- githubRepo,
94
- convexProjectId,
95
- expoProjectId,
96
- expoAccountId,
97
- };
98
- },
99
- });
100
- export const saveExpo = internalMutation({
101
- args: {
102
- ...identityFields,
103
- appId: v.string(),
104
- accountId: v.string(),
105
- projectId: v.optional(v.string()),
106
- },
107
- returns: v.null(),
108
- handler: async (ctx, { userId, appId, accountId, projectId }) => {
109
- const app = await ownedApp(ctx, userId, appId);
110
- assertActive(app);
111
- if (
112
- (app.expoAccountId && app.expoAccountId !== accountId) ||
113
- (projectId && app.expoProjectId && app.expoProjectId !== projectId)
114
- )
115
- throw new ConvexError("App is already linked to another Expo project.");
116
- await ctx.db.patch("apps", app._id, {
117
- expoAccountId: accountId,
118
- ...(projectId ? { expoProjectId: projectId } : {}),
119
- });
120
- return null;
121
- },
122
- });
123
- export const forget = internalMutation({
124
- args: {
125
- ...identityFields,
126
- appId: v.string(),
127
- until: v.number(),
128
- provider: v.union(
129
- v.literal("github"),
130
- v.literal("convex"),
131
- v.literal("expo"),
132
- ),
133
- },
134
- returns: v.null(),
135
- handler: async (ctx, { userId, appId, until, provider }) => {
136
- const app = await ownedApp(ctx, userId, appId);
137
- if (app.operation?.kind !== "delete" || app.operation.until !== until)
138
- throw new ConvexError("Deletion must be restarted.");
139
- await ctx.db.patch(
140
- "apps",
141
- app._id,
142
- provider === "github"
143
- ? { githubOwner: undefined, githubRepo: undefined }
144
- : provider === "convex"
145
- ? { convexProjectId: undefined }
146
- : { expoProjectId: undefined, expoAccountId: null },
147
- );
148
- return null;
149
- },
150
- });
151
- export const deletingResource = internalMutation({
152
- args: {
153
- ...identityFields,
154
- appId: v.string(),
155
- until: v.number(),
156
- pending: pendingDelete,
157
- },
158
- returns: v.null(),
159
- handler: async (ctx, { userId, appId, until, pending }) => {
160
- const app = await ownedApp(ctx, userId, appId);
161
- if (app.operation?.kind !== "delete" || app.operation.until !== until)
162
- throw new ConvexError("Deletion must be restarted.");
163
- await ctx.db.patch("apps", app._id, {
164
- operation: { ...app.operation, pending },
165
- });
166
- return null;
167
- },
168
- });
169
- export const requireExpoDeletion = internalMutation({
170
- args: {
171
- ...identityFields,
172
- appId: v.string(),
173
- until: v.number(),
174
- projectId: v.string(),
175
- },
176
- returns: v.null(),
177
- handler: async (ctx, { userId, appId, until, projectId }) => {
178
- const app = await ownedApp(ctx, userId, appId);
179
- if (
180
- app.operation?.kind !== "delete" ||
181
- app.operation.until !== until ||
182
- (app.expoProjectId && app.expoProjectId !== projectId) ||
183
- !app.expoAccountId
184
- )
185
- throw new ConvexError("Expo deletion must be restarted.");
186
- await ctx.db.patch("apps", app._id, {
187
- expoProjectId: projectId,
188
- operation: { ...app.operation, expoManual: true },
189
- });
190
- return null;
191
- },
192
- });
193
- export const erase = internalMutation({
194
- args: { ...identityFields, appId: v.string(), until: v.number() },
195
- returns: v.boolean(),
196
- handler: async (ctx, { userId, appId, until }) => {
197
- const app = await ownedApp(ctx, userId, appId);
198
- await assertPreviewOnly(ctx, appId);
199
- if (
200
- app.operation?.kind !== "delete" ||
201
- app.operation.until !== until ||
202
- app.githubRepo ||
203
- app.convexProjectId ||
204
- app.expoProjectId ||
205
- app.expoAccountId
206
- )
207
- throw new ConvexError("Linked projects must be deleted first.");
208
- const previews = await ctx.db
209
- .query("previews")
210
- .withIndex("by_appId_reference", (q) => q.eq("appId", appId))
211
- .take(100);
212
- for (const preview of previews)
213
- await ctx.db.delete("previews", preview._id);
214
- if (previews.length === 100) return false;
215
- await ctx.db.delete("apps", app._id);
216
- return true;
217
- },
218
- });
219
- export const workflow = internalMutation({
220
- args: { ...identityFields, appId: v.string(), production: v.boolean() },
221
- returns: v.null(),
222
- handler: async (ctx, { userId, appId, production }) => {
223
- const app = await ownedApp(ctx, userId, appId);
224
- assertActive(app);
225
- if (
226
- production &&
227
- !(await ctx.db
228
- .query("appReleases")
229
- .withIndex("by_appId", (q) => q.eq("appId", appId))
230
- .unique())
231
- )
232
- await ctx.db.insert("appReleases", {
233
- appId,
234
- releases: {},
235
- updatedAt: Date.now(),
236
- });
237
- return null;
238
- },
239
- });
@@ -1,23 +0,0 @@
1
- import { v } from "convex/values";
2
-
3
- export const deleteArgs = {
4
- appId: v.string(),
5
- dangerouslyDeleteApp: v.literal(true),
6
- };
7
- export const resources = v.object({
8
- githubOwner: v.optional(v.string()),
9
- githubRepo: v.optional(v.string()),
10
- convexProjectId: v.optional(v.number()),
11
- expoProjectId: v.optional(v.string()),
12
- expoAccountId: v.optional(v.union(v.string(), v.null())),
13
- });
14
-
15
- export const pendingDelete = v.object({
16
- provider: v.union(
17
- v.literal("github"),
18
- v.literal("convex"),
19
- v.literal("expo"),
20
- ),
21
- accountId: v.string(),
22
- receipt: v.optional(v.string()),
23
- });
@@ -1,56 +0,0 @@
1
- /// <reference types="node" />
2
- "use node";
3
-
4
- import { createCipheriv, createDecipheriv, randomBytes } from "node:crypto";
5
- import { ConvexError } from "convex/values";
6
- import { env } from "../../_generated/server";
7
- import type { ActionCtx } from "../../_generated/server";
8
- import { internal } from "../../_generated/api";
9
-
10
- export const connectionsUrl = () =>
11
- `${env.CONNECTIONS_ORIGIN || "https://www.disposabl.dev"}/dashboard/connections`;
12
- function encryptionKey() {
13
- const key = Buffer.from(env.CONNECTIONS_ENCRYPTION_KEY || "", "base64");
14
- if (key.length !== 32)
15
- throw new ConvexError("Account connections are not configured yet.");
16
- return key;
17
- }
18
- export function seal(value: string, context: string) {
19
- const iv = randomBytes(12);
20
- const cipher = createCipheriv("aes-256-gcm", encryptionKey(), iv);
21
- cipher.setAAD(Buffer.from(context));
22
- const data = Buffer.concat([cipher.update(value, "utf8"), cipher.final()]);
23
- return Buffer.concat([iv, cipher.getAuthTag(), data]).toString("base64url");
24
- }
25
- export function unseal(value: string, context: string) {
26
- const data = Buffer.from(value, "base64url");
27
- const decipher = createDecipheriv(
28
- "aes-256-gcm",
29
- encryptionKey(),
30
- data.subarray(0, 12),
31
- );
32
- decipher.setAAD(Buffer.from(context));
33
- decipher.setAuthTag(data.subarray(12, 28));
34
- return Buffer.concat([
35
- decipher.update(data.subarray(28)),
36
- decipher.final(),
37
- ]).toString("utf8");
38
- }
39
- export async function credential(
40
- ctx: ActionCtx,
41
- userId: string,
42
- provider: "convex" | "expo",
43
- ) {
44
- const connection = await ctx.runQuery(internal.connections.internal.get, {
45
- userId,
46
- provider,
47
- });
48
- if (!connection)
49
- throw new ConvexError(
50
- `Connect ${provider === "convex" ? "Convex" : "Expo"} at ${connectionsUrl()} to continue setup.`,
51
- );
52
- return {
53
- ...connection,
54
- token: unseal(connection.token, `${userId}:${provider}`),
55
- };
56
- }
@@ -1,79 +0,0 @@
1
- "use node";
2
-
3
- import {
4
- identityFields,
5
- internalAction,
6
- } from "@disposabl/disposabl-kit/server/lib/functions";
7
- import { ConvexError, v } from "convex/values";
8
- import { internal } from "../../_generated/api";
9
- import { account } from "../validator";
10
- import { seal } from "./credentials";
11
-
12
- export async function validateExpo(token: string) {
13
- if (!token || token.length > 4096 || /\s/.test(token))
14
- throw new ConvexError("Enter a valid Expo access token.");
15
- const response = await fetch("https://api.expo.dev/graphql", {
16
- method: "POST",
17
- redirect: "error",
18
- signal: AbortSignal.timeout(30_000),
19
- headers: {
20
- Authorization: `Bearer ${token}`,
21
- "Content-Type": "application/json",
22
- },
23
- body: JSON.stringify({
24
- query:
25
- "query { meActor { id accounts { id name viewerUserPermission2 { effectivePermissions } } } }",
26
- }),
27
- });
28
- const result = (await response.json()) as {
29
- errors?: unknown[];
30
- data?: {
31
- meActor?: {
32
- id: string;
33
- accounts: {
34
- id: string;
35
- name: string;
36
- viewerUserPermission2: { effectivePermissions: string[] };
37
- }[];
38
- };
39
- };
40
- };
41
- if (!response.ok || result.errors?.length || !result.data?.meActor)
42
- throw new ConvexError(
43
- "Expo could not verify this token. Check that it is active and try again.",
44
- );
45
- const accounts = result.data.meActor.accounts
46
- .filter((a) =>
47
- a.viewerUserPermission2.effectivePermissions.includes("PUBLISH"),
48
- )
49
- .map(({ id, name }) => ({ id, name }));
50
- if (!accounts.length)
51
- throw new ConvexError(
52
- "This Expo token needs permission to create projects and publish updates.",
53
- );
54
- return accounts;
55
- }
56
- export const expoAccounts = internalAction({
57
- args: { ...identityFields, token: v.string() },
58
- returns: v.array(account),
59
- handler: async (_ctx, { token }) => validateExpo(token.trim()),
60
- });
61
- export const connectExpo = internalAction({
62
- args: { ...identityFields, token: v.string(), accountId: v.string() },
63
- returns: v.null(),
64
- handler: async (ctx, { userId, token, accountId }) => {
65
- token = token.trim();
66
- const accounts = await validateExpo(token);
67
- const account = accounts.find((a) => a.id === accountId);
68
- if (!account)
69
- throw new ConvexError("Choose an Expo account available to this token.");
70
- await ctx.runMutation(internal.connections.internal.save, {
71
- userId,
72
- provider: "expo",
73
- accountId,
74
- name: account.name,
75
- token: seal(token, `${userId}:expo`),
76
- });
77
- return null;
78
- },
79
- });