(stateManager: MockConnectorStateManager)
| 43 | } |
| 44 | |
| 45 | function createMockConnectorApp(stateManager: MockConnectorStateManager) { |
| 46 | const app = new H3() |
| 47 | |
| 48 | app.use((event: H3Event) => { |
| 49 | const corsResult = handleCors(event, corsOptions) |
| 50 | if (corsResult !== false) { |
| 51 | return corsResult |
| 52 | } |
| 53 | }) |
| 54 | |
| 55 | function requireAuth(event: H3Event): void { |
| 56 | const authHeader = event.req.headers.get('authorization') |
| 57 | if (!authHeader || !authHeader.startsWith('Bearer ')) { |
| 58 | throw new HTTPError({ statusCode: 401, message: 'Authorization required' }) |
| 59 | } |
| 60 | const token = authHeader.slice(7) |
| 61 | if (token !== stateManager.token) { |
| 62 | throw new HTTPError({ statusCode: 401, message: 'Invalid token' }) |
| 63 | } |
| 64 | if (!stateManager.isConnected()) { |
| 65 | throw new HTTPError({ statusCode: 401, message: 'Not connected' }) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // POST /connect |
| 70 | app.post('/connect', async (event: H3Event) => { |
| 71 | const body = (await event.req.json()) as { token?: string } |
| 72 | const token = body?.token |
| 73 | |
| 74 | if (!token || token !== stateManager.token) { |
| 75 | throw new HTTPError({ statusCode: 401, message: 'Invalid token' }) |
| 76 | } |
| 77 | |
| 78 | stateManager.connect(token) |
| 79 | |
| 80 | return { |
| 81 | success: true, |
| 82 | data: { |
| 83 | npmUser: stateManager.config.npmUser, |
| 84 | avatar: stateManager.config.avatar ?? null, |
| 85 | connectedAt: stateManager.state.connectedAt ?? Date.now(), |
| 86 | }, |
| 87 | } satisfies ApiResponse<ConnectorEndpoints['POST /connect']['data']> |
| 88 | }) |
| 89 | |
| 90 | // GET /state |
| 91 | app.get('/state', (event: H3Event) => { |
| 92 | requireAuth(event) |
| 93 | |
| 94 | return { |
| 95 | success: true, |
| 96 | data: { |
| 97 | npmUser: stateManager.config.npmUser, |
| 98 | avatar: stateManager.config.avatar ?? null, |
| 99 | operations: stateManager.getOperations(), |
| 100 | }, |
| 101 | } satisfies ApiResponse<ConnectorEndpoints['GET /state']['data']> |
| 102 | }) |
no test coverage detected