Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots πŸ“–

feat(desktop): send form bodies through the main process fetch

Luis Ortiz6c6ffea8001b
diff --git a/apps/desktop/src/main/ai-fetch.ts b/apps/desktop/src/main/ai-fetch.ts
index e209b1a..958caf6 100644
--- a/apps/desktop/src/main/ai-fetch.ts
+++ b/apps/desktop/src/main/ai-fetch.ts
@@ -5,7 +5,7 @@ export interface AiRequest {
url: string;
method?: string;
headers?: Record<string, string>;
- body?: string;
+ body?: string | Uint8Array<ArrayBuffer>;
}
export interface AiResponse {
diff --git a/apps/desktop/src/renderer/__tests__/main-fetch.test.ts b/apps/desktop/src/renderer/__tests__/main-fetch.test.ts
index 4788ead..d606398 100644
--- a/apps/desktop/src/renderer/__tests__/main-fetch.test.ts
+++ b/apps/desktop/src/renderer/__tests__/main-fetch.test.ts
@@ -35,4 +35,18 @@ describe('mainFetch', () => {
expect([...new Uint8Array(await response.arrayBuffer())]).toEqual([1, 2, 255]);
});
+
+ it('sends a form as bytes with its multipart boundary', async () => {
+ stubBridge(async () => ({ status: 200, statusText: 'OK', headers: {}, body: '{}', encoding: 'text' }));
+ const form = new FormData();
+ form.append('model', 'whisper-1');
+
+ await mainFetch('https://api.openai.com/v1/audio/transcriptions', { method: 'POST', body: form });
+
+ const [sent] = (window.mimik.ai.fetch as ReturnType<typeof vi.fn>).mock.calls[0] as [
+ { body: Uint8Array; headers: Record<string, string> },
+ ];
+ expect(sent.headers['content-type']).toMatch(/^multipart\/form-data; boundary=/);
+ expect(new TextDecoder().decode(sent.body)).toContain('whisper-1');
+ });
});
diff --git a/apps/desktop/src/renderer/lib/main-fetch.ts b/apps/desktop/src/renderer/lib/main-fetch.ts
index d80694f..aafab03 100644
--- a/apps/desktop/src/renderer/lib/main-fetch.ts
+++ b/apps/desktop/src/renderer/lib/main-fetch.ts
@@ -8,10 +8,15 @@ interface MainReply {
export async function mainFetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response> {
const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url;
+ const raw = init?.body;
+ const encoded = raw == null || typeof raw === 'string' ? null : new Request(url, { method: 'POST', body: raw });
const headers: Record<string, string> = {};
new Headers(init?.headers).forEach((value, key) => {
headers[key] = value;
});
+ const type = encoded?.headers.get('content-type');
+ if (type && !headers['content-type']) headers['content-type'] = type;
+ const payload = encoded ? new Uint8Array(await encoded.arrayBuffer()) : typeof raw === 'string' ? raw : undefined;
const signal = init?.signal ?? undefined;
signal?.throwIfAborted();
@@ -32,7 +37,7 @@ export async function mainFetch(input: RequestInfo | URL, init?: RequestInit): P
url,
method: init?.method ?? 'GET',
headers,
- body: typeof init?.body === 'string' ? init.body : undefined,
+ body: payload,
}),
aborted,
])) as MainReply;