@nordsym/apiclaw 1.2.2 → 1.2.3
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/AGENTS.md +50 -33
- package/README.md +22 -12
- package/SOUL.md +60 -19
- package/STATUS.md +91 -169
- package/convex/_generated/api.d.ts +6 -0
- package/convex/directCall.ts +598 -0
- package/convex/providers.ts +341 -26
- package/convex/schema.ts +87 -0
- package/convex/usage.ts +260 -0
- package/convex/waitlist.ts +55 -0
- package/data/combined-02-26.json +22102 -0
- package/data/night-expansion-02-26-06-batch2.json +1898 -0
- package/data/night-expansion-02-26-06-batch3.json +1410 -0
- package/data/night-expansion-02-26-06.json +3146 -0
- package/data/night-expansion-02-26-full.json +9726 -0
- package/data/night-expansion-02-26-v2.json +330 -0
- package/data/night-expansion-02-26.json +171 -0
- package/dist/crypto.d.ts +7 -0
- package/dist/crypto.d.ts.map +1 -0
- package/dist/crypto.js +67 -0
- package/dist/crypto.js.map +1 -0
- package/dist/execute-dynamic.d.ts +116 -0
- package/dist/execute-dynamic.d.ts.map +1 -0
- package/dist/execute-dynamic.js +456 -0
- package/dist/execute-dynamic.js.map +1 -0
- package/dist/execute.d.ts +2 -1
- package/dist/execute.d.ts.map +1 -1
- package/dist/execute.js +35 -5
- package/dist/execute.js.map +1 -1
- package/dist/index.js +33 -4
- package/dist/index.js.map +1 -1
- package/dist/registry/apis.json +2081 -3
- package/docs/PRD-customer-key-passthrough.md +184 -0
- package/landing/public/badges/available-on-apiclaw.svg +14 -0
- package/landing/scripts/generate-stats.js +75 -4
- package/landing/src/app/admin/page.tsx +1 -1
- package/landing/src/app/api/auth/magic-link/route.ts +1 -1
- package/landing/src/app/api/auth/session/route.ts +1 -1
- package/landing/src/app/api/auth/verify/route.ts +1 -1
- package/landing/src/app/api/og/route.tsx +5 -3
- package/landing/src/app/docs/page.tsx +5 -4
- package/landing/src/app/earn/page.tsx +14 -11
- package/landing/src/app/globals.css +16 -15
- package/landing/src/app/layout.tsx +2 -2
- package/landing/src/app/page.tsx +425 -254
- package/landing/src/app/providers/dashboard/[apiId]/actions/[actionId]/edit/page.tsx +600 -0
- package/landing/src/app/providers/dashboard/[apiId]/actions/new/page.tsx +583 -0
- package/landing/src/app/providers/dashboard/[apiId]/actions/page.tsx +301 -0
- package/landing/src/app/providers/dashboard/[apiId]/direct-call/page.tsx +659 -0
- package/landing/src/app/providers/dashboard/[apiId]/page.tsx +381 -0
- package/landing/src/app/providers/dashboard/[apiId]/test/page.tsx +418 -0
- package/landing/src/app/providers/dashboard/layout.tsx +292 -0
- package/landing/src/app/providers/dashboard/page.tsx +353 -290
- package/landing/src/app/providers/register/page.tsx +87 -10
- package/landing/src/components/AiClientDropdown.tsx +85 -0
- package/landing/src/components/ConfigHelperModal.tsx +113 -0
- package/landing/src/components/HeroTabs.tsx +187 -0
- package/landing/src/components/ShareIntegrationModal.tsx +198 -0
- package/landing/src/hooks/useDashboardData.ts +53 -1
- package/landing/src/lib/apis.json +46554 -174
- package/landing/src/lib/convex-client.ts +22 -3
- package/landing/src/lib/stats.json +4 -4
- package/landing/tsconfig.tsbuildinfo +1 -1
- package/night-expansion-02-26-06-batch2.py +368 -0
- package/night-expansion-02-26-06-batch3.py +299 -0
- package/night-expansion-02-26-06.py +756 -0
- package/package.json +1 -1
- package/scripts/bulk-add-public-apis-v2.py +418 -0
- package/scripts/night-expansion-02-26-v2.py +296 -0
- package/scripts/night-expansion-02-26.py +890 -0
- package/scripts/seed-complete-api.js +181 -0
- package/scripts/seed-demo-api.sh +44 -0
- package/src/crypto.ts +75 -0
- package/src/execute-dynamic.ts +589 -0
- package/src/execute.ts +41 -5
- package/src/index.ts +38 -4
- package/src/registry/apis.json +2081 -3
|
@@ -0,0 +1,598 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
import { mutation, query } from "./_generated/server";
|
|
3
|
+
|
|
4
|
+
// ============================================
|
|
5
|
+
// MUTATIONS
|
|
6
|
+
// ============================================
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Save/update provider's Direct Call configuration
|
|
10
|
+
*/
|
|
11
|
+
export const saveDirectCallConfig = mutation({
|
|
12
|
+
args: {
|
|
13
|
+
id: v.optional(v.id("providerDirectCall")),
|
|
14
|
+
providerId: v.id("providers"),
|
|
15
|
+
apiId: v.optional(v.id("providerAPIs")),
|
|
16
|
+
baseUrl: v.string(),
|
|
17
|
+
authType: v.string(),
|
|
18
|
+
authHeader: v.string(),
|
|
19
|
+
authPrefix: v.string(),
|
|
20
|
+
encryptedMasterKey: v.string(),
|
|
21
|
+
rateLimitPerUser: v.number(),
|
|
22
|
+
rateLimitPerDay: v.number(),
|
|
23
|
+
pricePerRequest: v.number(),
|
|
24
|
+
},
|
|
25
|
+
handler: async (ctx, args) => {
|
|
26
|
+
const now = Date.now();
|
|
27
|
+
|
|
28
|
+
if (args.id) {
|
|
29
|
+
// Update existing
|
|
30
|
+
await ctx.db.patch(args.id, {
|
|
31
|
+
baseUrl: args.baseUrl,
|
|
32
|
+
authType: args.authType,
|
|
33
|
+
authHeader: args.authHeader,
|
|
34
|
+
authPrefix: args.authPrefix,
|
|
35
|
+
encryptedMasterKey: args.encryptedMasterKey,
|
|
36
|
+
rateLimitPerUser: args.rateLimitPerUser,
|
|
37
|
+
rateLimitPerDay: args.rateLimitPerDay,
|
|
38
|
+
pricePerRequest: args.pricePerRequest,
|
|
39
|
+
updatedAt: now,
|
|
40
|
+
});
|
|
41
|
+
return args.id;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Create new
|
|
45
|
+
return await ctx.db.insert("providerDirectCall", {
|
|
46
|
+
providerId: args.providerId,
|
|
47
|
+
apiId: args.apiId,
|
|
48
|
+
baseUrl: args.baseUrl,
|
|
49
|
+
authType: args.authType,
|
|
50
|
+
authHeader: args.authHeader,
|
|
51
|
+
authPrefix: args.authPrefix,
|
|
52
|
+
encryptedMasterKey: args.encryptedMasterKey,
|
|
53
|
+
rateLimitPerUser: args.rateLimitPerUser,
|
|
54
|
+
rateLimitPerDay: args.rateLimitPerDay,
|
|
55
|
+
pricePerRequest: args.pricePerRequest,
|
|
56
|
+
status: "draft",
|
|
57
|
+
createdAt: now,
|
|
58
|
+
updatedAt: now,
|
|
59
|
+
});
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Save Direct Call config with token auth (used by frontend)
|
|
65
|
+
*/
|
|
66
|
+
export const saveConfig = mutation({
|
|
67
|
+
args: {
|
|
68
|
+
token: v.string(),
|
|
69
|
+
config: v.object({
|
|
70
|
+
apiId: v.string(),
|
|
71
|
+
baseUrl: v.string(),
|
|
72
|
+
authType: v.string(),
|
|
73
|
+
authHeader: v.string(),
|
|
74
|
+
authPrefix: v.string(),
|
|
75
|
+
masterApiKey: v.optional(v.string()),
|
|
76
|
+
rateLimitPerUser: v.number(),
|
|
77
|
+
rateLimitPerDay: v.number(),
|
|
78
|
+
pricePerRequest: v.number(),
|
|
79
|
+
status: v.string(),
|
|
80
|
+
allowCustomerKeys: v.optional(v.boolean()),
|
|
81
|
+
requireCustomerKeys: v.optional(v.boolean()),
|
|
82
|
+
}),
|
|
83
|
+
},
|
|
84
|
+
handler: async (ctx, args) => {
|
|
85
|
+
// Verify session
|
|
86
|
+
const session = await ctx.db
|
|
87
|
+
.query("sessions")
|
|
88
|
+
.withIndex("by_token", (q) => q.eq("token", args.token))
|
|
89
|
+
.first();
|
|
90
|
+
|
|
91
|
+
if (!session || session.expiresAt < Date.now()) {
|
|
92
|
+
throw new Error("Invalid or expired session");
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const now = Date.now();
|
|
96
|
+
const { config } = args;
|
|
97
|
+
|
|
98
|
+
// Check if config already exists for this API
|
|
99
|
+
const existing = await ctx.db
|
|
100
|
+
.query("providerDirectCall")
|
|
101
|
+
.filter((q) => q.eq(q.field("apiId"), config.apiId as any))
|
|
102
|
+
.first();
|
|
103
|
+
|
|
104
|
+
if (existing) {
|
|
105
|
+
// Update existing
|
|
106
|
+
const updateData: Record<string, unknown> = {
|
|
107
|
+
baseUrl: config.baseUrl,
|
|
108
|
+
authType: config.authType,
|
|
109
|
+
authHeader: config.authHeader,
|
|
110
|
+
authPrefix: config.authPrefix,
|
|
111
|
+
rateLimitPerUser: config.rateLimitPerUser,
|
|
112
|
+
rateLimitPerDay: config.rateLimitPerDay,
|
|
113
|
+
pricePerRequest: config.pricePerRequest,
|
|
114
|
+
status: config.status,
|
|
115
|
+
allowCustomerKeys: config.allowCustomerKeys ?? true,
|
|
116
|
+
requireCustomerKeys: config.requireCustomerKeys ?? false,
|
|
117
|
+
updatedAt: now,
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
// Only update master key if provided (not empty)
|
|
121
|
+
if (config.masterApiKey) {
|
|
122
|
+
updateData.encryptedMasterKey = config.masterApiKey; // In prod: encrypt this
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
await ctx.db.patch(existing._id, updateData);
|
|
126
|
+
return existing._id;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Create new config
|
|
130
|
+
return await ctx.db.insert("providerDirectCall", {
|
|
131
|
+
providerId: session.providerId,
|
|
132
|
+
apiId: config.apiId as any,
|
|
133
|
+
baseUrl: config.baseUrl,
|
|
134
|
+
authType: config.authType,
|
|
135
|
+
authHeader: config.authHeader,
|
|
136
|
+
authPrefix: config.authPrefix,
|
|
137
|
+
encryptedMasterKey: config.masterApiKey || "",
|
|
138
|
+
rateLimitPerUser: config.rateLimitPerUser,
|
|
139
|
+
rateLimitPerDay: config.rateLimitPerDay,
|
|
140
|
+
pricePerRequest: config.pricePerRequest,
|
|
141
|
+
status: config.status as "draft" | "testing" | "live",
|
|
142
|
+
allowCustomerKeys: config.allowCustomerKeys ?? true,
|
|
143
|
+
requireCustomerKeys: config.requireCustomerKeys ?? false,
|
|
144
|
+
createdAt: now,
|
|
145
|
+
updatedAt: now,
|
|
146
|
+
});
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Create/update an action for a Direct Call config
|
|
152
|
+
*/
|
|
153
|
+
export const saveAction = mutation({
|
|
154
|
+
args: {
|
|
155
|
+
id: v.optional(v.id("providerActions")),
|
|
156
|
+
directCallId: v.id("providerDirectCall"),
|
|
157
|
+
name: v.string(),
|
|
158
|
+
displayName: v.string(),
|
|
159
|
+
description: v.string(),
|
|
160
|
+
method: v.string(),
|
|
161
|
+
path: v.string(),
|
|
162
|
+
params: v.array(v.object({
|
|
163
|
+
name: v.string(),
|
|
164
|
+
type: v.string(),
|
|
165
|
+
required: v.boolean(),
|
|
166
|
+
description: v.string(),
|
|
167
|
+
default: v.optional(v.any()),
|
|
168
|
+
in: v.string(),
|
|
169
|
+
})),
|
|
170
|
+
responseMapping: v.array(v.object({
|
|
171
|
+
name: v.string(),
|
|
172
|
+
path: v.string(),
|
|
173
|
+
})),
|
|
174
|
+
enabled: v.boolean(),
|
|
175
|
+
},
|
|
176
|
+
handler: async (ctx, args) => {
|
|
177
|
+
const now = Date.now();
|
|
178
|
+
|
|
179
|
+
if (args.id) {
|
|
180
|
+
// Update existing
|
|
181
|
+
await ctx.db.patch(args.id, {
|
|
182
|
+
name: args.name,
|
|
183
|
+
displayName: args.displayName,
|
|
184
|
+
description: args.description,
|
|
185
|
+
method: args.method,
|
|
186
|
+
path: args.path,
|
|
187
|
+
params: args.params,
|
|
188
|
+
responseMapping: args.responseMapping,
|
|
189
|
+
enabled: args.enabled,
|
|
190
|
+
updatedAt: now,
|
|
191
|
+
});
|
|
192
|
+
return args.id;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Create new
|
|
196
|
+
return await ctx.db.insert("providerActions", {
|
|
197
|
+
directCallId: args.directCallId,
|
|
198
|
+
name: args.name,
|
|
199
|
+
displayName: args.displayName,
|
|
200
|
+
description: args.description,
|
|
201
|
+
method: args.method,
|
|
202
|
+
path: args.path,
|
|
203
|
+
params: args.params,
|
|
204
|
+
responseMapping: args.responseMapping,
|
|
205
|
+
enabled: args.enabled,
|
|
206
|
+
createdAt: now,
|
|
207
|
+
updatedAt: now,
|
|
208
|
+
});
|
|
209
|
+
},
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Delete an action
|
|
214
|
+
*/
|
|
215
|
+
export const deleteAction = mutation({
|
|
216
|
+
args: {
|
|
217
|
+
id: v.id("providerActions"),
|
|
218
|
+
},
|
|
219
|
+
handler: async (ctx, args) => {
|
|
220
|
+
await ctx.db.delete(args.id);
|
|
221
|
+
return { success: true };
|
|
222
|
+
},
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Publish Direct Call - set status to live
|
|
227
|
+
*/
|
|
228
|
+
export const publishDirectCall = mutation({
|
|
229
|
+
args: {
|
|
230
|
+
id: v.id("providerDirectCall"),
|
|
231
|
+
},
|
|
232
|
+
handler: async (ctx, args) => {
|
|
233
|
+
const now = Date.now();
|
|
234
|
+
await ctx.db.patch(args.id, {
|
|
235
|
+
status: "live",
|
|
236
|
+
publishedAt: now,
|
|
237
|
+
updatedAt: now,
|
|
238
|
+
});
|
|
239
|
+
return { success: true, publishedAt: now };
|
|
240
|
+
},
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Set Direct Call status (draft, testing, live)
|
|
245
|
+
*/
|
|
246
|
+
export const setStatus = mutation({
|
|
247
|
+
args: {
|
|
248
|
+
id: v.id("providerDirectCall"),
|
|
249
|
+
status: v.string(),
|
|
250
|
+
},
|
|
251
|
+
handler: async (ctx, args) => {
|
|
252
|
+
const now = Date.now();
|
|
253
|
+
const update: { status: string; updatedAt: number; publishedAt?: number } = {
|
|
254
|
+
status: args.status,
|
|
255
|
+
updatedAt: now,
|
|
256
|
+
};
|
|
257
|
+
if (args.status === "live") {
|
|
258
|
+
update.publishedAt = now;
|
|
259
|
+
}
|
|
260
|
+
await ctx.db.patch(args.id, update);
|
|
261
|
+
return { success: true };
|
|
262
|
+
},
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
// ============================================
|
|
266
|
+
// QUERIES
|
|
267
|
+
// ============================================
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Get Direct Call config by providerId
|
|
271
|
+
*/
|
|
272
|
+
export const getDirectCallConfig = query({
|
|
273
|
+
args: {
|
|
274
|
+
providerId: v.id("providers"),
|
|
275
|
+
},
|
|
276
|
+
handler: async (ctx, args) => {
|
|
277
|
+
return await ctx.db
|
|
278
|
+
.query("providerDirectCall")
|
|
279
|
+
.withIndex("by_providerId", (q) => q.eq("providerId", args.providerId))
|
|
280
|
+
.first();
|
|
281
|
+
},
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Get Direct Call config by ID
|
|
286
|
+
*/
|
|
287
|
+
export const getDirectCallConfigById = query({
|
|
288
|
+
args: {
|
|
289
|
+
id: v.id("providerDirectCall"),
|
|
290
|
+
},
|
|
291
|
+
handler: async (ctx, args) => {
|
|
292
|
+
return await ctx.db.get(args.id);
|
|
293
|
+
},
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Get Direct Call config by API ID
|
|
298
|
+
*/
|
|
299
|
+
export const getDirectCallConfigByApiId = query({
|
|
300
|
+
args: {
|
|
301
|
+
apiId: v.string(), // Accept string since that's what frontend passes
|
|
302
|
+
},
|
|
303
|
+
handler: async (ctx, args) => {
|
|
304
|
+
return await ctx.db
|
|
305
|
+
.query("providerDirectCall")
|
|
306
|
+
.filter((q) => q.eq(q.field("apiId"), args.apiId as any))
|
|
307
|
+
.first();
|
|
308
|
+
},
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Get all actions for a Direct Call config
|
|
313
|
+
*/
|
|
314
|
+
export const getActions = query({
|
|
315
|
+
args: {
|
|
316
|
+
directCallId: v.id("providerDirectCall"),
|
|
317
|
+
},
|
|
318
|
+
handler: async (ctx, args) => {
|
|
319
|
+
return await ctx.db
|
|
320
|
+
.query("providerActions")
|
|
321
|
+
.withIndex("by_directCallId", (q) => q.eq("directCallId", args.directCallId))
|
|
322
|
+
.collect();
|
|
323
|
+
},
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Get single action by directCallId + name
|
|
328
|
+
*/
|
|
329
|
+
export const getActionByName = query({
|
|
330
|
+
args: {
|
|
331
|
+
directCallId: v.id("providerDirectCall"),
|
|
332
|
+
name: v.string(),
|
|
333
|
+
},
|
|
334
|
+
handler: async (ctx, args) => {
|
|
335
|
+
return await ctx.db
|
|
336
|
+
.query("providerActions")
|
|
337
|
+
.withIndex("by_directCallId_name", (q) =>
|
|
338
|
+
q.eq("directCallId", args.directCallId).eq("name", args.name)
|
|
339
|
+
)
|
|
340
|
+
.first();
|
|
341
|
+
},
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Get action by ID
|
|
346
|
+
*/
|
|
347
|
+
export const getActionById = query({
|
|
348
|
+
args: {
|
|
349
|
+
id: v.id("providerActions"),
|
|
350
|
+
},
|
|
351
|
+
handler: async (ctx, args) => {
|
|
352
|
+
return await ctx.db.get(args.id);
|
|
353
|
+
},
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* DEBUG: Get all Direct Call configs
|
|
358
|
+
*/
|
|
359
|
+
export const getAllConfigs = query({
|
|
360
|
+
args: {},
|
|
361
|
+
handler: async (ctx) => {
|
|
362
|
+
return await ctx.db.query("providerDirectCall").collect();
|
|
363
|
+
},
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Get all live Direct Call configs (for public API discovery)
|
|
368
|
+
*/
|
|
369
|
+
export const getLiveConfigs = query({
|
|
370
|
+
args: {},
|
|
371
|
+
handler: async (ctx) => {
|
|
372
|
+
return await ctx.db
|
|
373
|
+
.query("providerDirectCall")
|
|
374
|
+
.withIndex("by_status", (q) => q.eq("status", "live"))
|
|
375
|
+
.collect();
|
|
376
|
+
},
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Get Direct Call config by API ID (for test console)
|
|
381
|
+
*/
|
|
382
|
+
export const getConfig = query({
|
|
383
|
+
args: {
|
|
384
|
+
apiId: v.string(),
|
|
385
|
+
},
|
|
386
|
+
handler: async (ctx, args) => {
|
|
387
|
+
// Try as providerAPIs ID first
|
|
388
|
+
const config = await ctx.db
|
|
389
|
+
.query("providerDirectCall")
|
|
390
|
+
.withIndex("by_apiId")
|
|
391
|
+
.filter((q) => q.eq(q.field("apiId"), args.apiId as any))
|
|
392
|
+
.first();
|
|
393
|
+
|
|
394
|
+
return config;
|
|
395
|
+
},
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Get Direct Call config by API slug (for MCP/agent execution)
|
|
400
|
+
* Looks up API by name, then gets the Direct Call config
|
|
401
|
+
*/
|
|
402
|
+
export const getByApiSlug = query({
|
|
403
|
+
args: {
|
|
404
|
+
slug: v.string(),
|
|
405
|
+
},
|
|
406
|
+
handler: async (ctx, args) => {
|
|
407
|
+
// Normalize slug (lowercase, replace spaces/dashes)
|
|
408
|
+
const normalizedSlug = args.slug.toLowerCase().replace(/[\s-]/g, '_');
|
|
409
|
+
|
|
410
|
+
// Find API by name (case-insensitive match)
|
|
411
|
+
const apis = await ctx.db.query("providerAPIs").collect();
|
|
412
|
+
const api = apis.find(a =>
|
|
413
|
+
a.name.toLowerCase().replace(/[\s-]/g, '_') === normalizedSlug ||
|
|
414
|
+
a.name.toLowerCase() === args.slug.toLowerCase()
|
|
415
|
+
);
|
|
416
|
+
|
|
417
|
+
if (!api) {
|
|
418
|
+
return null;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// Get Direct Call config for this API
|
|
422
|
+
const config = await ctx.db
|
|
423
|
+
.query("providerDirectCall")
|
|
424
|
+
.withIndex("by_apiId")
|
|
425
|
+
.filter((q) => q.eq(q.field("apiId"), api._id))
|
|
426
|
+
.first();
|
|
427
|
+
|
|
428
|
+
if (!config || config.status !== 'live') {
|
|
429
|
+
return null;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
return {
|
|
433
|
+
...config,
|
|
434
|
+
apiName: api.name,
|
|
435
|
+
apiSlug: normalizedSlug,
|
|
436
|
+
};
|
|
437
|
+
},
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
// ============================================
|
|
441
|
+
// TEST ACTION
|
|
442
|
+
// ============================================
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* Test an action by calling the actual provider API
|
|
446
|
+
* For V1: Provider passes their own test key
|
|
447
|
+
*/
|
|
448
|
+
export const testAction = mutation({
|
|
449
|
+
args: {
|
|
450
|
+
token: v.string(),
|
|
451
|
+
directCallId: v.id("providerDirectCall"),
|
|
452
|
+
actionId: v.id("providerActions"),
|
|
453
|
+
params: v.record(v.string(), v.any()),
|
|
454
|
+
testKey: v.optional(v.string()),
|
|
455
|
+
},
|
|
456
|
+
handler: async (ctx, args) => {
|
|
457
|
+
const startTime = Date.now();
|
|
458
|
+
|
|
459
|
+
// 1. Verify provider session
|
|
460
|
+
const session = await ctx.db
|
|
461
|
+
.query("sessions")
|
|
462
|
+
.withIndex("by_token", (q) => q.eq("token", args.token))
|
|
463
|
+
.first();
|
|
464
|
+
|
|
465
|
+
if (!session || session.expiresAt < Date.now()) {
|
|
466
|
+
return {
|
|
467
|
+
success: false,
|
|
468
|
+
error: "Unauthorized - invalid or expired session",
|
|
469
|
+
latencyMs: Date.now() - startTime,
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// 2. Get directCallConfig
|
|
474
|
+
const config = await ctx.db.get(args.directCallId);
|
|
475
|
+
if (!config) {
|
|
476
|
+
return {
|
|
477
|
+
success: false,
|
|
478
|
+
error: "Direct Call config not found",
|
|
479
|
+
latencyMs: Date.now() - startTime,
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// Verify ownership
|
|
484
|
+
if (config.providerId !== session.providerId) {
|
|
485
|
+
return {
|
|
486
|
+
success: false,
|
|
487
|
+
error: "Unauthorized - you don't own this config",
|
|
488
|
+
latencyMs: Date.now() - startTime,
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
// 3. Get action
|
|
493
|
+
const action = await ctx.db.get(args.actionId);
|
|
494
|
+
if (!action) {
|
|
495
|
+
return {
|
|
496
|
+
success: false,
|
|
497
|
+
error: "Action not found",
|
|
498
|
+
latencyMs: Date.now() - startTime,
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
// 4. Get API key (use testKey if provided, else use stored key)
|
|
503
|
+
// Note: For production, encryptedMasterKey would need server-side decryption
|
|
504
|
+
// For V1 test console, we use testKey directly
|
|
505
|
+
const apiKey = args.testKey || config.encryptedMasterKey;
|
|
506
|
+
if (!apiKey) {
|
|
507
|
+
return {
|
|
508
|
+
success: false,
|
|
509
|
+
error: "No API key provided. Add a test key or configure master key.",
|
|
510
|
+
latencyMs: Date.now() - startTime,
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
// 5. Build URL with path params
|
|
515
|
+
let path = action.path;
|
|
516
|
+
const queryParams: Record<string, string> = {};
|
|
517
|
+
const bodyParams: Record<string, unknown> = {};
|
|
518
|
+
|
|
519
|
+
for (const paramDef of action.params) {
|
|
520
|
+
const value = args.params[paramDef.name];
|
|
521
|
+
if (value === undefined || value === "") continue;
|
|
522
|
+
|
|
523
|
+
if (paramDef.in === "path") {
|
|
524
|
+
// Replace {paramName} in path
|
|
525
|
+
path = path.replace(`{${paramDef.name}}`, String(value));
|
|
526
|
+
} else if (paramDef.in === "query") {
|
|
527
|
+
queryParams[paramDef.name] = String(value);
|
|
528
|
+
} else if (paramDef.in === "body") {
|
|
529
|
+
bodyParams[paramDef.name] = value;
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
// Build full URL
|
|
534
|
+
let url = config.baseUrl.replace(/\/$/, "") + path;
|
|
535
|
+
const queryString = new URLSearchParams(queryParams).toString();
|
|
536
|
+
if (queryString) {
|
|
537
|
+
url += (url.includes("?") ? "&" : "?") + queryString;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
// 6. Build headers with auth
|
|
541
|
+
const headers: Record<string, string> = {
|
|
542
|
+
"Content-Type": "application/json",
|
|
543
|
+
"Accept": "application/json",
|
|
544
|
+
};
|
|
545
|
+
|
|
546
|
+
// Add auth header
|
|
547
|
+
if (config.authType !== "none" && apiKey) {
|
|
548
|
+
const authValue = config.authPrefix
|
|
549
|
+
? `${config.authPrefix}${apiKey}`
|
|
550
|
+
: apiKey;
|
|
551
|
+
headers[config.authHeader] = authValue;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
// 7. Build fetch options
|
|
555
|
+
const fetchOptions: RequestInit = {
|
|
556
|
+
method: action.method,
|
|
557
|
+
headers,
|
|
558
|
+
};
|
|
559
|
+
|
|
560
|
+
// Add body for non-GET requests
|
|
561
|
+
if (action.method !== "GET" && Object.keys(bodyParams).length > 0) {
|
|
562
|
+
fetchOptions.body = JSON.stringify(bodyParams);
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// 8. Execute request
|
|
566
|
+
try {
|
|
567
|
+
const response = await fetch(url, fetchOptions);
|
|
568
|
+
const latencyMs = Date.now() - startTime;
|
|
569
|
+
|
|
570
|
+
// Try to parse response as JSON, fallback to text
|
|
571
|
+
let data: unknown;
|
|
572
|
+
const contentType = response.headers.get("content-type") || "";
|
|
573
|
+
|
|
574
|
+
if (contentType.includes("application/json")) {
|
|
575
|
+
try {
|
|
576
|
+
data = await response.json();
|
|
577
|
+
} catch {
|
|
578
|
+
data = await response.text();
|
|
579
|
+
}
|
|
580
|
+
} else {
|
|
581
|
+
data = await response.text();
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
return {
|
|
585
|
+
success: response.ok,
|
|
586
|
+
status: response.status,
|
|
587
|
+
data,
|
|
588
|
+
latencyMs,
|
|
589
|
+
};
|
|
590
|
+
} catch (error) {
|
|
591
|
+
return {
|
|
592
|
+
success: false,
|
|
593
|
+
error: error instanceof Error ? error.message : "Request failed",
|
|
594
|
+
latencyMs: Date.now() - startTime,
|
|
595
|
+
};
|
|
596
|
+
}
|
|
597
|
+
},
|
|
598
|
+
});
|