feat: add OpenAI integration with custom header logging and request aborting

This commit is contained in:
MythEclipse
2026-05-26 01:34:37 +07:00
parent c1c149855a
commit 2156c52c35
2 changed files with 48 additions and 0 deletions
+22
View File
@@ -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);
});
+26
View File
@@ -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}`);
}