From ad269409e6b156eeb94c26e1b6e659aefc0dbb7f Mon Sep 17 00:00:00 2001 From: Luke Hagar Date: Wed, 13 Aug 2025 14:24:04 -0500 Subject: [PATCH] formatting pass --- src/server.ts | 107 +++++++++++++++++++----------------------- src/storage/index.ts | 2 - src/storage/sqlite.ts | 51 +++++++++++--------- src/storage/types.ts | 2 - 4 files changed, 78 insertions(+), 84 deletions(-) diff --git a/src/server.ts b/src/server.ts index 479037ee..c78050fb 100644 --- a/src/server.ts +++ b/src/server.ts @@ -155,18 +155,13 @@ export async function startServers({ const persisted = await storage().getAllEndpoints(); for (const ep of persisted) { try { - openApiStore.recordEndpoint( - ep.path, - ep.method.toLowerCase(), - ep.data.request, - { - status: ep.data.response?.status || 200, - headers: ep.data.response?.headers || {}, - contentType: ep.data.response?.contentType || 'application/json', - body: '[Raw data stored]', - rawData: Buffer.alloc(0), - } - ); + openApiStore.recordEndpoint(ep.path, ep.method.toLowerCase(), ep.data.request, { + status: ep.data.response?.status || 200, + headers: ep.data.response?.headers || {}, + contentType: ep.data.response?.contentType || 'application/json', + body: '[Raw data stored]', + rawData: Buffer.alloc(0), + }); } catch {} } } catch (e) { @@ -401,15 +396,17 @@ export async function startServers({ // Add the HAR entry to the store and persist if enabled harStore.addEntry(harEntry); if (dbPath) { - storage().saveHarEntry({ - startedDateTime: harEntry.startedDateTime, - time: harEntry.time, - request: harEntry.request, - response: { - ...harEntry.response, - // Do not persist raw buffer reference - }, - }).catch(() => {}); + storage() + .saveHarEntry({ + startedDateTime: harEntry.startedDateTime, + time: harEntry.time, + request: harEntry.request, + response: { + ...harEntry.response, + // Do not persist raw buffer reference + }, + }) + .catch(() => {}); } // Extract security schemes from headers - minimal work @@ -458,22 +455,24 @@ export async function startServers({ // Persist endpoint minimal info for reconstruction if (dbPath) { - storage().upsertEndpoint(path, method.toLowerCase(), { - path, - method: method.toLowerCase(), - request: { - query: queryParams, - headers: requestHeaders, - contentType: requestHeaders['content-type'] || 'application/json', - body: requestBody, - security: securitySchemes, - }, - response: { - status: proxyRes.statusCode || 500, - headers: responseHeaders, - contentType: responseHeaders['content-type'] || 'application/json', - }, - }).catch(() => {}); + storage() + .upsertEndpoint(path, method.toLowerCase(), { + path, + method: method.toLowerCase(), + request: { + query: queryParams, + headers: requestHeaders, + contentType: requestHeaders['content-type'] || 'application/json', + body: requestBody, + security: securitySchemes, + }, + response: { + status: proxyRes.statusCode || 500, + headers: responseHeaders, + contentType: responseHeaders['content-type'] || 'application/json', + }, + }) + .catch(() => {}); } if (verbose) { @@ -514,17 +513,12 @@ export async function startServers({ tempStore.setTargetUrl(target); for (const ep of persisted) { try { - tempStore.recordEndpoint( - ep.path, - ep.method.toLowerCase(), - ep.data.request, - { - status: ep.data.response?.status || 200, - headers: ep.data.response?.headers || {}, - contentType: ep.data.response?.contentType || 'application/json', - body: '[Raw data stored]' - } - ); + tempStore.recordEndpoint(ep.path, ep.method.toLowerCase(), ep.data.request, { + status: ep.data.response?.status || 200, + headers: ep.data.response?.headers || {}, + contentType: ep.data.response?.contentType || 'application/json', + body: '[Raw data stored]', + }); } catch {} } res.send(JSON.stringify(tempStore.getOpenAPISpec())); @@ -543,17 +537,12 @@ export async function startServers({ tempStore.setTargetUrl(target); for (const ep of persisted) { try { - tempStore.recordEndpoint( - ep.path, - ep.method.toLowerCase(), - ep.data.request, - { - status: ep.data.response?.status || 200, - headers: ep.data.response?.headers || {}, - contentType: ep.data.response?.contentType || 'application/json', - body: '[Raw data stored]' - } - ); + tempStore.recordEndpoint(ep.path, ep.method.toLowerCase(), ep.data.request, { + status: ep.data.response?.status || 200, + headers: ep.data.response?.headers || {}, + contentType: ep.data.response?.contentType || 'application/json', + body: '[Raw data stored]', + }); } catch {} } res.send(tempStore.getOpenAPISpecAsYAML()); diff --git a/src/storage/index.ts b/src/storage/index.ts index 79a354ff..25e76283 100644 --- a/src/storage/index.ts +++ b/src/storage/index.ts @@ -11,5 +11,3 @@ export async function initStorage(dbPath: string): Promise { export function storage(): StorageAdapter { return storageInstance; } - - diff --git a/src/storage/sqlite.ts b/src/storage/sqlite.ts index e40e9624..777916c6 100644 --- a/src/storage/sqlite.ts +++ b/src/storage/sqlite.ts @@ -75,28 +75,37 @@ export class SQLiteStorage implements StorageAdapter { } async getHarLog(): Promise { - const empty = { log: { version: '1.2', creator: { name: 'Arbiter', version: '1.0.0' }, entries: [] as any[] } }; + const empty = { + log: { version: '1.2', creator: { name: 'Arbiter', version: '1.0.0' }, entries: [] as any[] }, + }; if (!this.db) return empty; try { - const rows: Array<{ startedDateTime: string; time: number; request: string; response: string }> = this.db + const rows: Array<{ + startedDateTime: string; + time: number; + request: string; + response: string; + }> = this.db .prepare('SELECT startedDateTime, time, request, response FROM har_entries ORDER BY id ASC') .all(); - const entries = rows.map((r: { startedDateTime: string; time: number; request: string; response: string }) => { - let req: any = {}; - let res: any = {}; - try { - req = JSON.parse(r.request); - } catch {} - try { - res = JSON.parse(r.response); - } catch {} - return { - startedDateTime: r.startedDateTime, - time: r.time, - request: req, - response: res, - }; - }); + const entries = rows.map( + (r: { startedDateTime: string; time: number; request: string; response: string }) => { + let req: any = {}; + let res: any = {}; + try { + req = JSON.parse(r.request); + } catch {} + try { + res = JSON.parse(r.response); + } catch {} + return { + startedDateTime: r.startedDateTime, + time: r.time, + request: req, + response: res, + }; + } + ); return { log: { ...empty.log, entries } }; } catch { return empty; @@ -124,7 +133,9 @@ export class SQLiteStorage implements StorageAdapter { async getAllEndpoints(): Promise> { if (!this.db) return []; try { - const rows: Array<{ path: string; method: string; data: string }> = this.db.prepare('SELECT path, method, data FROM endpoints').all(); + const rows: Array<{ path: string; method: string; data: string }> = this.db + .prepare('SELECT path, method, data FROM endpoints') + .all(); return rows.map((r: { path: string; method: string; data: string }) => { let data: any = {}; try { @@ -139,5 +150,3 @@ export class SQLiteStorage implements StorageAdapter { } export const sqliteStorage = new SQLiteStorage(); - - diff --git a/src/storage/types.ts b/src/storage/types.ts index c95bc99d..c83cc5c0 100644 --- a/src/storage/types.ts +++ b/src/storage/types.ts @@ -12,5 +12,3 @@ export interface StorageAdapter { upsertEndpoint(path: string, method: string, data: any): Promise; getAllEndpoints(): Promise>; } - -