From 2156c52c354c393b8103f11caf2725bc7c967afa Mon Sep 17 00:00:00 2001 From: MythEclipse Date: Tue, 26 May 2026 01:34:37 +0700 Subject: [PATCH] feat: add OpenAI integration with custom header logging and request aborting --- test-headers.js | 22 ++++++++++++++++++++++ test-wrapper.js | 26 ++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 test-headers.js create mode 100644 test-wrapper.js diff --git a/test-headers.js b/test-headers.js new file mode 100644 index 0000000..784c6a8 --- /dev/null +++ b/test-headers.js @@ -0,0 +1,22 @@ +import OpenAI from "openai"; + +const openai = new OpenAI({ + apiKey: "sk-test", + baseURL: "https://9router.imrnes.team/v1", + fetch: async (url, init) => { + console.log("Headers passed by OpenAI:"); + const headers = new Headers(init.headers); + for (const [k, v] of headers.entries()) { + console.log(` ${k}: ${v}`); + } + // abort early to avoid making the request + throw new Error("aborted"); + } +}); + +openai.chat.completions.create({ + model: "test", + messages: [{role: "user", content: "test"}] +}).catch(e => { + if (e.message !== "aborted") console.error(e); +}); diff --git a/test-wrapper.js b/test-wrapper.js new file mode 100644 index 0000000..72f3b89 --- /dev/null +++ b/test-wrapper.js @@ -0,0 +1,26 @@ +const init = { + headers: { + Accept: 'application/json', + Authorization: 'Bearer sk-4ab633fd86509c5b-8r850e-8c58839a', + 'Content-Type': 'application/json', + 'User-Agent': 'OpenAI/JS 6.38.0', + 'x-stainless-arch': 'x64', + 'x-stainless-lang': 'js', + 'x-stainless-os': 'Linux', + 'x-stainless-package-version': '6.38.0', + 'x-stainless-runtime': 'node', + 'x-stainless-runtime-version': 'v26.2.0' + } +}; + +const headers = new Headers(init?.headers); +headers.set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"); +for (const key of Array.from(headers.keys())) { + if (key.toLowerCase().startsWith("x-stainless")) { + headers.delete(key); + } +} + +for (const [k, v] of headers.entries()) { + console.log(`${k}: ${v}`); +}