@tthr/vue 0.0.83 → 0.0.85
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/nuxt/module.js +7 -9
- package/nuxt/module.ts +7 -9
- package/nuxt/runtime/composables.ts +638 -0
- package/nuxt/runtime/plugin.client.ts +34 -0
- package/nuxt/runtime/server/mutation.post.ts +53 -0
- package/nuxt/runtime/server/plugins/cron.ts +377 -0
- package/nuxt/runtime/server/query.post.ts +51 -0
- package/nuxt/runtime/server/utils/handler.ts +375 -0
- package/nuxt/runtime/server/utils/{tether.js → tether.ts} +78 -29
- package/package.json +6 -9
- package/dist/nuxt.d.ts +0 -14
- package/dist/nuxt.d.ts.map +0 -1
- package/dist/nuxt.js +0 -48
- package/dist/nuxt.js.map +0 -1
- package/dist/runtime/composables.d.ts +0 -73
- package/dist/runtime/composables.d.ts.map +0 -1
- package/dist/runtime/composables.js +0 -112
- package/dist/runtime/composables.js.map +0 -1
- package/dist/runtime/plugin.d.ts +0 -11
- package/dist/runtime/plugin.d.ts.map +0 -1
- package/dist/runtime/plugin.js +0 -33
- package/dist/runtime/plugin.js.map +0 -1
- package/nuxt/runtime/composables.d.ts +0 -142
- package/nuxt/runtime/composables.d.ts.map +0 -1
- package/nuxt/runtime/composables.js +0 -414
- package/nuxt/runtime/composables.js.map +0 -1
- package/nuxt/runtime/plugin.client.d.ts +0 -17
- package/nuxt/runtime/plugin.client.d.ts.map +0 -1
- package/nuxt/runtime/plugin.client.js +0 -21
- package/nuxt/runtime/plugin.client.js.map +0 -1
- package/nuxt/runtime/server/mutation.post.js +0 -373
- package/nuxt/runtime/server/plugins/cron.d.ts +0 -38
- package/nuxt/runtime/server/plugins/cron.d.ts.map +0 -1
- package/nuxt/runtime/server/plugins/cron.js +0 -621
- package/nuxt/runtime/server/plugins/cron.js.map +0 -1
- package/nuxt/runtime/server/query.post.js +0 -372
|
@@ -1,373 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Server-side mutation handler
|
|
3
|
-
* Executes local custom functions or proxies generic CRUD to Tether API
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { defineEventHandler, readBody, createError, getHeader, getCookie, setResponseStatus } from 'h3';
|
|
7
|
-
import { useRuntimeConfig } from '#imports';
|
|
8
|
-
|
|
9
|
-
// Dynamic function registry - populated on first request
|
|
10
|
-
let functionRegistry = null;
|
|
11
|
-
let registryError = null;
|
|
12
|
-
|
|
13
|
-
// Verbose logging support
|
|
14
|
-
let verboseLogging = false;
|
|
15
|
-
const PREFIX = '[Tether]';
|
|
16
|
-
const log = {
|
|
17
|
-
debug: (...args) => { if (verboseLogging) console.log(PREFIX, ...args); },
|
|
18
|
-
info: (...args) => console.log(PREFIX, ...args),
|
|
19
|
-
warn: (...args) => console.warn(PREFIX, ...args),
|
|
20
|
-
error: (...args) => console.error(PREFIX, ...args),
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Try to load the user's custom functions from ~/tether/functions
|
|
25
|
-
*/
|
|
26
|
-
async function loadFunctionRegistry() {
|
|
27
|
-
if (functionRegistry !== null || registryError !== null) {
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
try {
|
|
32
|
-
// Try to import the user's functions index
|
|
33
|
-
// This path is relative to the Nuxt app's server runtime
|
|
34
|
-
log.debug('Loading custom functions from ~~/tether/functions/index.ts');
|
|
35
|
-
const functions = await import('~~/tether/functions/index.ts').catch((err) => {
|
|
36
|
-
log.debug('Failed to import functions:', err.message);
|
|
37
|
-
return null;
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
if (functions) {
|
|
41
|
-
log.debug('Loaded function modules:', Object.keys(functions));
|
|
42
|
-
functionRegistry = functions;
|
|
43
|
-
} else {
|
|
44
|
-
// No custom functions found - that's OK, we'll proxy everything
|
|
45
|
-
log.debug('No custom functions found, will proxy all requests');
|
|
46
|
-
functionRegistry = {};
|
|
47
|
-
}
|
|
48
|
-
} catch (error) {
|
|
49
|
-
// Failed to load - we'll proxy everything to Tether API
|
|
50
|
-
log.debug('Could not load custom functions:', error.message);
|
|
51
|
-
registryError = error;
|
|
52
|
-
functionRegistry = {};
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Look up a function by name (e.g., "channel.createChannel")
|
|
58
|
-
*/
|
|
59
|
-
function lookupFunction(name) {
|
|
60
|
-
if (!functionRegistry || !name) return null;
|
|
61
|
-
|
|
62
|
-
const parts = name.split('.');
|
|
63
|
-
if (parts.length !== 2) return null;
|
|
64
|
-
|
|
65
|
-
const [moduleName, fnName] = parts;
|
|
66
|
-
const module = functionRegistry[moduleName];
|
|
67
|
-
|
|
68
|
-
if (!module) return null;
|
|
69
|
-
|
|
70
|
-
const fn = module[fnName];
|
|
71
|
-
|
|
72
|
-
// Check if it's a valid Tether function (has a handler)
|
|
73
|
-
if (fn && typeof fn === 'object' && typeof fn.handler === 'function') {
|
|
74
|
-
return fn;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return null;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Create a database proxy that routes calls to Tether's CRUD endpoints
|
|
82
|
-
*/
|
|
83
|
-
function createDatabaseProxy(apiKey, url, projectId) {
|
|
84
|
-
return new Proxy({}, {
|
|
85
|
-
get(_target, tableName) {
|
|
86
|
-
const makeRequest = async (operation, args) => {
|
|
87
|
-
const response = await fetch(`${url}/api/v1/projects/${projectId}/query`, {
|
|
88
|
-
method: 'POST',
|
|
89
|
-
headers: {
|
|
90
|
-
'Content-Type': 'application/json',
|
|
91
|
-
'Authorization': `Bearer ${apiKey}`,
|
|
92
|
-
},
|
|
93
|
-
body: JSON.stringify({
|
|
94
|
-
function: `${tableName}.${operation}`,
|
|
95
|
-
args,
|
|
96
|
-
}),
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
if (!response.ok) {
|
|
100
|
-
const error = await response.json().catch(() => ({ error: 'Unknown error' }));
|
|
101
|
-
throw new Error(error.error || `Database operation failed: ${tableName}.${operation}`);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const result = await response.json();
|
|
105
|
-
return result.data;
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
const makeMutation = async (operation, args) => {
|
|
109
|
-
const response = await fetch(`${url}/api/v1/projects/${projectId}/mutation`, {
|
|
110
|
-
method: 'POST',
|
|
111
|
-
headers: {
|
|
112
|
-
'Content-Type': 'application/json',
|
|
113
|
-
'Authorization': `Bearer ${apiKey}`,
|
|
114
|
-
},
|
|
115
|
-
body: JSON.stringify({
|
|
116
|
-
function: `${tableName}.${operation}`,
|
|
117
|
-
args,
|
|
118
|
-
}),
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
if (!response.ok) {
|
|
122
|
-
const error = await response.json().catch(() => ({ error: 'Unknown error' }));
|
|
123
|
-
throw new Error(error.error || `Database operation failed: ${tableName}.${operation}`);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
const result = await response.json();
|
|
127
|
-
return result.data;
|
|
128
|
-
};
|
|
129
|
-
|
|
130
|
-
return {
|
|
131
|
-
findMany: async (options) => {
|
|
132
|
-
const args = {};
|
|
133
|
-
if (options?.where) args.where = options.where;
|
|
134
|
-
if (options?.limit) args.limit = options.limit;
|
|
135
|
-
if (options?.offset) args.offset = options.offset;
|
|
136
|
-
// Handle orderBy as either a string or an object { column: 'asc'|'desc' }
|
|
137
|
-
if (options?.orderBy) {
|
|
138
|
-
if (typeof options.orderBy === 'string') {
|
|
139
|
-
args.orderBy = options.orderBy;
|
|
140
|
-
} else if (typeof options.orderBy === 'object') {
|
|
141
|
-
const [column, dir] = Object.entries(options.orderBy)[0] || [];
|
|
142
|
-
if (column) {
|
|
143
|
-
args.orderBy = column;
|
|
144
|
-
args.orderDir = dir?.toUpperCase?.() || 'ASC';
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
if (options?.orderDir) args.orderDir = options.orderDir;
|
|
149
|
-
return makeRequest('list', args);
|
|
150
|
-
},
|
|
151
|
-
findFirst: async (options) => {
|
|
152
|
-
const args = { limit: 1 };
|
|
153
|
-
if (options?.where) args.where = options.where;
|
|
154
|
-
const results = await makeRequest('list', args);
|
|
155
|
-
return results?.[0] ?? null;
|
|
156
|
-
},
|
|
157
|
-
findUnique: async (options) => {
|
|
158
|
-
const args = { limit: 1 };
|
|
159
|
-
if (options?.where) args.where = options.where;
|
|
160
|
-
const results = await makeRequest('list', args);
|
|
161
|
-
return results?.[0] ?? null;
|
|
162
|
-
},
|
|
163
|
-
findById: async (id) => {
|
|
164
|
-
return makeRequest('get', { id });
|
|
165
|
-
},
|
|
166
|
-
count: async (options) => {
|
|
167
|
-
const args = {};
|
|
168
|
-
if (options?.where) args.where = options.where;
|
|
169
|
-
const result = await makeRequest('count', args);
|
|
170
|
-
return result?.count ?? 0;
|
|
171
|
-
},
|
|
172
|
-
insert: async (data) => {
|
|
173
|
-
return makeMutation('create', { data });
|
|
174
|
-
},
|
|
175
|
-
insertMany: async (items) => {
|
|
176
|
-
const results = [];
|
|
177
|
-
for (const data of items) {
|
|
178
|
-
const result = await makeMutation('create', { data });
|
|
179
|
-
results.push(result);
|
|
180
|
-
}
|
|
181
|
-
return results;
|
|
182
|
-
},
|
|
183
|
-
create: async (options) => {
|
|
184
|
-
return makeMutation('create', { data: options.data });
|
|
185
|
-
},
|
|
186
|
-
update: async (options) => {
|
|
187
|
-
const whereObj = options.where;
|
|
188
|
-
// Support both _id (standard) and id (legacy) in where clause
|
|
189
|
-
const id = whereObj?._id ?? whereObj?.id;
|
|
190
|
-
if (!id) {
|
|
191
|
-
throw new Error('Update requires an _id in the where clause');
|
|
192
|
-
}
|
|
193
|
-
const result = await makeMutation('update', { id, data: options.data });
|
|
194
|
-
return result?.rowsAffected ?? 0;
|
|
195
|
-
},
|
|
196
|
-
upsert: async (options) => {
|
|
197
|
-
const whereObj = options.where;
|
|
198
|
-
|
|
199
|
-
// Try to find existing record using the where clause
|
|
200
|
-
// This allows upserting on any field (e.g. clip_id, email, etc.)
|
|
201
|
-
// Use list with limit: 1 since findFirst doesn't exist on the server
|
|
202
|
-
const results = await makeRequest('list', { where: whereObj, limit: 1 }).catch(() => []);
|
|
203
|
-
const existing = results?.[0] ?? null;
|
|
204
|
-
|
|
205
|
-
if (existing) {
|
|
206
|
-
// Use _id from the found record for the update
|
|
207
|
-
const id = existing._id ?? existing.id;
|
|
208
|
-
if (!id) {
|
|
209
|
-
throw new Error('Found record has no _id or id field for update');
|
|
210
|
-
}
|
|
211
|
-
await makeMutation('update', { id, data: options.update });
|
|
212
|
-
return { ...existing, ...options.update };
|
|
213
|
-
} else {
|
|
214
|
-
return makeMutation('create', { data: options.create });
|
|
215
|
-
}
|
|
216
|
-
},
|
|
217
|
-
delete: async (options) => {
|
|
218
|
-
const whereObj = options.where;
|
|
219
|
-
// Support both _id (standard) and id (legacy) in where clause
|
|
220
|
-
const id = whereObj?._id ?? whereObj?.id;
|
|
221
|
-
if (id) {
|
|
222
|
-
const result = await makeMutation('delete', { id });
|
|
223
|
-
return result?.rowsAffected ?? 0;
|
|
224
|
-
}
|
|
225
|
-
// Compound where clause — use deleteMany
|
|
226
|
-
const result = await makeMutation('deleteMany', { where: whereObj });
|
|
227
|
-
return result?.rowsAffected ?? 0;
|
|
228
|
-
},
|
|
229
|
-
deleteById: async (id) => {
|
|
230
|
-
const result = await makeMutation('delete', { id });
|
|
231
|
-
return (result?.rowsAffected ?? 0) > 0;
|
|
232
|
-
},
|
|
233
|
-
};
|
|
234
|
-
},
|
|
235
|
-
});
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
export default defineEventHandler(async (event) => {
|
|
239
|
-
const config = useRuntimeConfig();
|
|
240
|
-
verboseLogging = config.tether?.verbose || process.env.TETHER_VERBOSE === 'true';
|
|
241
|
-
|
|
242
|
-
// Get API key from runtime config (populated from TETHER_API_KEY env var)
|
|
243
|
-
const apiKey = config.tether?.apiKey || process.env.TETHER_API_KEY;
|
|
244
|
-
const url = config.tether?.url || process.env.TETHER_URL || 'https://tether-api.strands.gg';
|
|
245
|
-
const projectId = config.tether?.projectId || process.env.TETHER_PROJECT_ID;
|
|
246
|
-
|
|
247
|
-
if (!apiKey) {
|
|
248
|
-
throw createError({
|
|
249
|
-
statusCode: 500,
|
|
250
|
-
message: 'Tether API key not configured. Set TETHER_API_KEY environment variable.',
|
|
251
|
-
});
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
if (!projectId) {
|
|
255
|
-
throw createError({
|
|
256
|
-
statusCode: 500,
|
|
257
|
-
message: 'Tether project ID not configured.',
|
|
258
|
-
});
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
const body = await readBody(event);
|
|
262
|
-
|
|
263
|
-
if (!body?.function) {
|
|
264
|
-
throw createError({
|
|
265
|
-
statusCode: 400,
|
|
266
|
-
message: 'Missing "function" in request body',
|
|
267
|
-
});
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
// Load function registry (once)
|
|
271
|
-
await loadFunctionRegistry();
|
|
272
|
-
|
|
273
|
-
// Try to find a custom function
|
|
274
|
-
const customFn = lookupFunction(body.function);
|
|
275
|
-
log.debug(`Mutation: ${body.function}, custom function found: ${!!customFn}`);
|
|
276
|
-
|
|
277
|
-
if (customFn) {
|
|
278
|
-
// Execute locally with database proxy
|
|
279
|
-
try {
|
|
280
|
-
log.debug(`Executing custom mutation: ${body.function}`);
|
|
281
|
-
const db = createDatabaseProxy(apiKey, url, projectId);
|
|
282
|
-
|
|
283
|
-
// Create auth context - try to get user identity from request
|
|
284
|
-
let userIdentity = null;
|
|
285
|
-
|
|
286
|
-
// Check for Strands auth token in cookie or header
|
|
287
|
-
const authHeader = getHeader(event, 'authorization');
|
|
288
|
-
const strandsToken = getHeader(event, 'x-strands-token');
|
|
289
|
-
const cookieToken = getCookie(event, 'strands_oauth_token');
|
|
290
|
-
|
|
291
|
-
// If using Strands auth, validate the token via Tether server
|
|
292
|
-
if (strandsToken || cookieToken || authHeader?.startsWith('Bearer ')) {
|
|
293
|
-
const token = strandsToken || cookieToken || authHeader?.replace('Bearer ', '');
|
|
294
|
-
if (token) {
|
|
295
|
-
try {
|
|
296
|
-
// Validate token via Tether server (which checks if Strands Auth is enabled)
|
|
297
|
-
const authResponse = await fetch(`${url}/api/v1/projects/${projectId}/auth/validate`, {
|
|
298
|
-
method: 'POST',
|
|
299
|
-
headers: { 'Content-Type': 'application/json' },
|
|
300
|
-
body: JSON.stringify({ accessToken: token }),
|
|
301
|
-
});
|
|
302
|
-
if (authResponse.ok) {
|
|
303
|
-
const authData = await authResponse.json();
|
|
304
|
-
if (authData.valid) {
|
|
305
|
-
userIdentity = {
|
|
306
|
-
subject: authData.userId,
|
|
307
|
-
email: authData.email,
|
|
308
|
-
name: authData.name,
|
|
309
|
-
};
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
} catch (authError) {
|
|
313
|
-
log.warn('Auth validation failed:', authError.message);
|
|
314
|
-
// Auth validation failed - continue without identity
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
const auth = {
|
|
320
|
-
getUserIdentity: async () => userIdentity,
|
|
321
|
-
};
|
|
322
|
-
|
|
323
|
-
const ctx = {
|
|
324
|
-
auth: {
|
|
325
|
-
userId: userIdentity?.subject ?? null,
|
|
326
|
-
claims: userIdentity ?? {},
|
|
327
|
-
},
|
|
328
|
-
userId: userIdentity?.subject ?? null,
|
|
329
|
-
};
|
|
330
|
-
|
|
331
|
-
const result = await customFn.handler({
|
|
332
|
-
db,
|
|
333
|
-
ctx,
|
|
334
|
-
auth,
|
|
335
|
-
args: body.args ?? {},
|
|
336
|
-
});
|
|
337
|
-
|
|
338
|
-
return { data: result };
|
|
339
|
-
} catch (error) {
|
|
340
|
-
log.error(`Mutation ${body.function} failed:`, error);
|
|
341
|
-
// Return JSON error response instead of throwing (avoids proxy HTML error pages)
|
|
342
|
-
setResponseStatus(event, 400);
|
|
343
|
-
return {
|
|
344
|
-
error: true,
|
|
345
|
-
message: error.message || 'Mutation execution failed',
|
|
346
|
-
function: body.function,
|
|
347
|
-
};
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
// No custom function found - proxy to Tether API (generic CRUD)
|
|
352
|
-
const response = await fetch(`${url}/api/v1/projects/${projectId}/mutation`, {
|
|
353
|
-
method: 'POST',
|
|
354
|
-
headers: {
|
|
355
|
-
'Content-Type': 'application/json',
|
|
356
|
-
'Authorization': `Bearer ${apiKey}`,
|
|
357
|
-
},
|
|
358
|
-
body: JSON.stringify({
|
|
359
|
-
function: body.function,
|
|
360
|
-
args: body.args,
|
|
361
|
-
}),
|
|
362
|
-
});
|
|
363
|
-
|
|
364
|
-
if (!response.ok) {
|
|
365
|
-
const error = await response.json().catch(() => ({ error: 'Unknown error' }));
|
|
366
|
-
throw createError({
|
|
367
|
-
statusCode: response.status,
|
|
368
|
-
message: error.error || 'Mutation failed',
|
|
369
|
-
});
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
return response.json();
|
|
373
|
-
});
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Nuxt server plugin for Tether cron execution
|
|
3
|
-
*
|
|
4
|
-
* This plugin automatically connects to Tether via WebSocket as a server connection
|
|
5
|
-
* to receive cron triggers. When a cron is due, Tether sends a trigger message,
|
|
6
|
-
* this plugin executes the function, and reports the result back.
|
|
7
|
-
*
|
|
8
|
-
* The connection is secured with the API key and only server connections
|
|
9
|
-
* (identified by ?type=server) receive cron triggers.
|
|
10
|
-
*/
|
|
11
|
-
import { defineNitroPlugin } from 'nitropack/runtime';
|
|
12
|
-
export { defineNitroPlugin };
|
|
13
|
-
/**
|
|
14
|
-
* Register a cron handler for a specific function
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```ts
|
|
18
|
-
* // In your Nuxt server setup
|
|
19
|
-
* import { registerCronHandler } from '#imports';
|
|
20
|
-
*
|
|
21
|
-
* registerCronHandler('reports.generate', async (args) => {
|
|
22
|
-
* // Your function logic here
|
|
23
|
-
* return { generated: true };
|
|
24
|
-
* });
|
|
25
|
-
* ```
|
|
26
|
-
*/
|
|
27
|
-
export declare function registerCronHandler(functionName: string, handler: (args: unknown) => Promise<unknown>): void;
|
|
28
|
-
/**
|
|
29
|
-
* Unregister a cron handler
|
|
30
|
-
*/
|
|
31
|
-
export declare function unregisterCronHandler(functionName: string): void;
|
|
32
|
-
/**
|
|
33
|
-
* Get all registered cron handlers
|
|
34
|
-
*/
|
|
35
|
-
export declare function getCronHandlers(): string[];
|
|
36
|
-
declare const _default: any;
|
|
37
|
-
export default _default;
|
|
38
|
-
//# sourceMappingURL=cron.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cron.d.ts","sourceRoot":"","sources":["cron.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAGtD,OAAO,EAAE,iBAAiB,EAAE,CAAC;AAqC7B;;;;;;;;;;;;;GAaG;AACH,wBAAgB,mBAAmB,CACjC,YAAY,EAAE,MAAM,EACpB,OAAO,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,OAAO,CAAC,OAAO,CAAC,GAC3C,IAAI,CAGN;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI,CAEhE;AAED;;GAEG;AACH,wBAAgB,eAAe,IAAI,MAAM,EAAE,CAE1C;;AAslBD,wBAmCG"}
|