-
-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathuseConnector.ts
More file actions
449 lines (399 loc) · 13 KB
/
useConnector.ts
File metadata and controls
449 lines (399 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import type { PendingOperation, OperationStatus, OperationType } from '../../cli/src/types'
export interface NewOperation {
type: OperationType
params: Record<string, string>
description: string
command: string
/** ID of operation this depends on (must complete successfully first) */
dependsOn?: string
}
interface ApiResponse<T = unknown> {
success: boolean
data?: T
error?: string
}
export interface ConnectorState {
/** Whether we're currently connected to the local connector */
connected: boolean
/** Whether we're attempting to connect */
connecting: boolean
/** The npm username if connected and authenticated */
npmUser: string | null
/** Base64 data URL of the user's avatar */
avatar: string | null
/** Pending operations queue */
operations: PendingOperation[]
/** Last connection error message */
error: string | null
/** Timestamp of last completed execution (for triggering data refreshes) */
lastExecutionTime: number | null
}
interface ConnectResponse {
success: boolean
data?: {
npmUser: string | null
avatar: string | null
connectedAt: number
}
error?: string
}
interface StateResponse {
success: boolean
data?: {
npmUser: string | null
avatar: string | null
operations: PendingOperation[]
}
error?: string
}
const STORAGE_KEY = 'npmx-connector'
const DEFAULT_PORT = 31415
export const useConnector = createSharedComposable(function useConnector() {
// Persisted connection config
const config = useState<{ token: string; port: number } | null>('connector-config', () => null)
// Connection state
const state = useState<ConnectorState>('connector-state', () => ({
connected: false,
connecting: false,
npmUser: null,
avatar: null,
operations: [],
error: null,
lastExecutionTime: null,
}))
const baseUrl = computed(() => `http://localhost:${config.value?.port ?? DEFAULT_PORT}`)
const route = useRoute()
const router = useRouter()
onMounted(() => {
const urlToken = route.query.token as string | undefined
const urlPort = route.query.port as string | undefined
if (urlToken) {
const { token: _, port: __, ...cleanQuery } = route.query
router.replace({ query: cleanQuery })
// Connect with URL params
const port = urlPort ? Number.parseInt(urlPort, 10) : DEFAULT_PORT
connect(urlToken, port)
return
}
const stored = localStorage.getItem(STORAGE_KEY)
if (stored) {
try {
config.value = JSON.parse(stored)
// Auto-reconnect if we have stored credentials
if (config.value) {
reconnect()
}
} catch {
localStorage.removeItem(STORAGE_KEY)
}
}
})
async function connect(token: string, port: number = DEFAULT_PORT): Promise<boolean> {
state.value.connecting = true
state.value.error = null
try {
const response = await $fetch<ConnectResponse>(`http://localhost:${port}/connect`, {
method: 'POST',
body: { token },
timeout: 5000,
})
if (response.success && response.data) {
config.value = { token, port }
localStorage.setItem(STORAGE_KEY, JSON.stringify(config.value))
state.value.connected = true
state.value.npmUser = response.data.npmUser
state.value.avatar = response.data.avatar
state.value.error = null
// Fetch full state after connecting
await refreshState()
return true
} else {
state.value.error = response.error ?? 'Connection failed'
return false
}
} catch (err) {
const message = err instanceof Error ? err.message : 'Connection failed'
if (
message.includes('fetch') ||
message.includes('network') ||
message.includes('ECONNREFUSED')
) {
state.value.error = 'Could not reach connector. Is it running?'
} else if (message.includes('401') || message.includes('Unauthorized')) {
state.value.error = 'Invalid token'
} else {
state.value.error = message
}
return false
} finally {
state.value.connecting = false
}
}
async function reconnect(): Promise<boolean> {
if (!config.value) return false
return connect(config.value.token, config.value.port)
}
function disconnect() {
config.value = null
localStorage.removeItem(STORAGE_KEY)
state.value = {
connected: false,
connecting: false,
npmUser: null,
avatar: null,
operations: [],
error: null,
lastExecutionTime: null,
}
}
async function refreshState(): Promise<void> {
if (!config.value) return
try {
const response = await $fetch<StateResponse>(`${baseUrl.value}/state`, {
headers: {
Authorization: `Bearer ${config.value.token}`,
},
timeout: 5000,
})
if (response.success && response.data) {
state.value.npmUser = response.data.npmUser
state.value.avatar = response.data.avatar
state.value.operations = response.data.operations
state.value.connected = true
}
} catch {
// Connection lost
state.value.connected = false
state.value.error = 'Connection lost'
}
}
async function connectorFetch<T>(
path: string,
options: { method?: 'GET' | 'POST' | 'DELETE'; body?: Record<string, unknown> } = {},
): Promise<T | null> {
if (!config.value) return null
try {
const response = await $fetch(`${baseUrl.value}${path}`, {
method: options.method ?? 'GET',
headers: {
Authorization: `Bearer ${config.value.token}`,
},
body: options.body,
timeout: 30000,
})
return response as T
} catch (err) {
state.value.error = err instanceof Error ? err.message : 'Request failed'
return null
}
}
// Operation management
async function addOperation(operation: NewOperation): Promise<PendingOperation | null> {
const response = await connectorFetch<ApiResponse<PendingOperation>>('/operations', {
method: 'POST',
body: operation as unknown as Record<string, unknown>,
})
if (response?.success && response.data) {
await refreshState()
return response.data
}
return null
}
async function addOperations(operations: NewOperation[]): Promise<PendingOperation[]> {
const response = await connectorFetch<ApiResponse<PendingOperation[]>>('/operations/batch', {
method: 'POST',
body: operations as unknown as Record<string, unknown>,
})
if (response?.success && response.data) {
await refreshState()
return response.data
}
return []
}
async function removeOperation(id: string): Promise<boolean> {
const response = await connectorFetch<ApiResponse>(`/operations?id=${id}`, {
method: 'DELETE',
})
if (response?.success) {
await refreshState()
return true
}
return false
}
async function clearOperations(): Promise<number> {
const response = await connectorFetch<ApiResponse<{ removed: number }>>('/operations/all', {
method: 'DELETE',
})
if (response?.success && response.data) {
await refreshState()
return response.data.removed
}
return 0
}
async function approveOperation(id: string): Promise<boolean> {
const response = await connectorFetch<ApiResponse<PendingOperation>>(`/approve?id=${id}`, {
method: 'POST',
})
if (response?.success) {
await refreshState()
return true
}
return false
}
async function retryOperation(id: string): Promise<boolean> {
const response = await connectorFetch<ApiResponse<PendingOperation>>(`/retry?id=${id}`, {
method: 'POST',
})
if (response?.success) {
await refreshState()
return true
}
return false
}
async function approveAll(): Promise<number> {
const response = await connectorFetch<ApiResponse<{ approved: number }>>('/approve-all', {
method: 'POST',
})
if (response?.success && response.data) {
await refreshState()
return response.data.approved
}
return 0
}
async function executeOperations(
otp?: string,
): Promise<{ success: boolean; otpRequired?: boolean }> {
const response = await connectorFetch<
ApiResponse<{ results: unknown[]; otpRequired?: boolean }>
>('/execute', {
method: 'POST',
body: otp ? { otp } : undefined,
})
if (response?.success) {
await refreshState()
// Update timestamp to trigger data refreshes in panels
state.value.lastExecutionTime = Date.now()
return {
success: true,
otpRequired: response.data?.otpRequired,
}
}
return { success: false }
}
// Data fetching functions
async function listOrgUsers(
org: string,
): Promise<Record<string, 'developer' | 'admin' | 'owner'> | null> {
const response = await connectorFetch<
ApiResponse<Record<string, 'developer' | 'admin' | 'owner'>>
>(`/org/${encodeURIComponent(org)}/users`)
return response?.success ? (response.data ?? null) : null
}
async function listOrgTeams(org: string): Promise<string[] | null> {
const response = await connectorFetch<ApiResponse<string[]>>(
`/org/${encodeURIComponent(org)}/teams`,
)
return response?.success ? (response.data ?? null) : null
}
async function listTeamUsers(scopeTeam: string): Promise<string[] | null> {
const response = await connectorFetch<ApiResponse<string[]>>(
`/team/${encodeURIComponent(scopeTeam)}/users`,
)
return response?.success ? (response.data ?? null) : null
}
async function listPackageCollaborators(
pkg: string,
): Promise<Record<string, 'read-only' | 'read-write'> | null> {
const response = await connectorFetch<ApiResponse<Record<string, 'read-only' | 'read-write'>>>(
`/package/${encodeURIComponent(pkg)}/collaborators`,
)
return response?.success ? (response.data ?? null) : null
}
async function listUserPackages(): Promise<Record<string, 'read-write' | 'read-only'> | null> {
const response =
await connectorFetch<ApiResponse<Record<string, 'read-write' | 'read-only'>>>(
'/user/packages',
)
return response?.success ? (response.data ?? null) : null
}
async function listUserOrgs(): Promise<string[] | null> {
const response = await connectorFetch<ApiResponse<string[]>>('/user/orgs')
return response?.success ? (response.data ?? null) : null
}
// Computed helpers for operations
const pendingOperations = computed(() =>
state.value.operations.filter(op => op.status === 'pending'),
)
const approvedOperations = computed(() =>
state.value.operations.filter(op => op.status === 'approved'),
)
/** Operations that are done (completed, or failed without needing OTP retry) */
const completedOperations = computed(() =>
state.value.operations.filter(
op => op.status === 'completed' || (op.status === 'failed' && !op.result?.requiresOtp),
),
)
/** Operations that are still active (pending, approved, running, or failed needing OTP retry) */
const activeOperations = computed(() =>
state.value.operations.filter(
op =>
op.status === 'pending' ||
op.status === 'approved' ||
op.status === 'running' ||
(op.status === 'failed' && op.result?.requiresOtp),
),
)
const hasOperations = computed(() => state.value.operations.length > 0)
const hasPendingOperations = computed(() => pendingOperations.value.length > 0)
const hasApprovedOperations = computed(() => approvedOperations.value.length > 0)
const hasActiveOperations = computed(() => activeOperations.value.length > 0)
const hasCompletedOperations = computed(() => completedOperations.value.length > 0)
return {
// State
state: readonly(state),
config: readonly(config),
// Computed - connection
isConnected: computed(() => state.value.connected),
isConnecting: computed(() => state.value.connecting),
npmUser: computed(() => state.value.npmUser),
avatar: computed(() => state.value.avatar),
error: computed(() => state.value.error),
/** Timestamp of last execution completion (watch this to refresh data) */
lastExecutionTime: computed(() => state.value.lastExecutionTime),
// Computed - operations
operations: computed(() => state.value.operations),
pendingOperations,
approvedOperations,
completedOperations,
activeOperations,
hasOperations,
hasPendingOperations,
hasApprovedOperations,
hasActiveOperations,
hasCompletedOperations,
// Actions - connection
connect,
reconnect,
disconnect,
refreshState,
connectorFetch,
// Actions - operations
addOperation,
addOperations,
removeOperation,
clearOperations,
approveOperation,
retryOperation,
approveAll,
executeOperations,
// Actions - data fetching
listOrgUsers,
listOrgTeams,
listTeamUsers,
listPackageCollaborators,
listUserPackages,
listUserOrgs,
}
})
// Re-export types for convenience
export type { PendingOperation, OperationStatus, OperationType }