From c3f49ca67ff842b538e55f2645fab0052f6aa4d6 Mon Sep 17 00:00:00 2001 From: csgulati09 Date: Mon, 18 Mar 2024 17:46:34 +0530 Subject: [PATCH] feat: common default header builder function --- src/apis/assistants.ts | 20 ++++++++++---------- src/apis/files.ts | 12 ++++++------ src/apis/images.ts | 8 ++++---- src/apis/models.ts | 17 ++++------------- src/apis/threads.ts | 40 ++++++++++++++++++++-------------------- src/utils.ts | 12 ++++++++++++ 6 files changed, 56 insertions(+), 53 deletions(-) diff --git a/src/apis/assistants.ts b/src/apis/assistants.ts index ccbf610..80b8aca 100644 --- a/src/apis/assistants.ts +++ b/src/apis/assistants.ts @@ -2,7 +2,7 @@ import { ApiClientInterface } from "../_types/generalTypes"; import { ApiResource } from "../apiResource"; import { RequestOptions } from "../baseClient"; import { OPEN_AI_API_KEY } from "../constants"; -import { finalResponse, overrideConfig } from "../utils"; +import { defaultHeadersBuilder, finalResponse, overrideConfig } from "../utils"; import { createHeaders } from "./createHeaders"; import OpenAI from "openai"; @@ -72,7 +72,7 @@ export class Assistants extends ApiResource { const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); const result = await OAIclient.beta.assistants.create(body, opts).withResponse(); @@ -97,7 +97,7 @@ export class Assistants extends ApiResource { const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); // @ts-ignore const result = await OAIclient.beta.assistants.list(query, opts).withResponse(); @@ -121,7 +121,7 @@ export class Assistants extends ApiResource { const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); const result = await OAIclient.beta.assistants.retrieve(assistantId, opts).withResponse(); @@ -147,7 +147,7 @@ export class Assistants extends ApiResource { const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); const result = await OAIclient.beta.assistants.update(assistantId, body, opts).withResponse(); @@ -171,7 +171,7 @@ export class Assistants extends ApiResource { const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); const result = await OAIclient.beta.assistants.del(assistantId, opts).withResponse(); @@ -202,7 +202,7 @@ export class Files extends ApiResource{ const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); const result = await OAIclient.beta.assistants.files.create(assistantId, body, opts).withResponse(); @@ -228,7 +228,7 @@ export class Files extends ApiResource{ const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); // @ts-ignore const result = await OAIclient.beta.assistants.files.list(assistantId, query, opts).withResponse(); @@ -253,7 +253,7 @@ export class Files extends ApiResource{ const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); const result = await OAIclient.beta.assistants.files.retrieve(assistantId, fileId, opts).withResponse(); @@ -278,7 +278,7 @@ export class Files extends ApiResource{ const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); const result = await OAIclient.beta.assistants.files.del(assistantId, fileId, opts).withResponse(); diff --git a/src/apis/files.ts b/src/apis/files.ts index 633a2d9..80ec7ad 100644 --- a/src/apis/files.ts +++ b/src/apis/files.ts @@ -2,7 +2,7 @@ import { ApiClientInterface } from "../_types/generalTypes"; import { ApiResource } from "../apiResource"; import { RequestOptions } from "../baseClient"; import { OPEN_AI_API_KEY } from "../constants"; -import { finalResponse, overrideConfig } from "../utils"; +import { defaultHeadersBuilder, finalResponse, overrideConfig } from "../utils"; import { createHeaders } from "./createHeaders"; import OpenAI from "openai"; @@ -25,7 +25,7 @@ export class MainFiles extends ApiResource { const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); // @ts-ignore @@ -51,7 +51,7 @@ export class MainFiles extends ApiResource { const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); const result = await OAIclient.files.list(query, opts).withResponse(); @@ -75,7 +75,7 @@ export class MainFiles extends ApiResource { const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); const result = await OAIclient.files.retrieve(fileId, opts).withResponse(); @@ -99,7 +99,7 @@ export class MainFiles extends ApiResource { const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); const result = await OAIclient.files.del(fileId, opts).withResponse(); @@ -123,7 +123,7 @@ export class MainFiles extends ApiResource { const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); const result = await OAIclient.files.content(fileId, opts).withResponse(); diff --git a/src/apis/images.ts b/src/apis/images.ts index 95255c0..adff6b3 100644 --- a/src/apis/images.ts +++ b/src/apis/images.ts @@ -2,7 +2,7 @@ import { ApiClientInterface } from "../_types/generalTypes"; import { ApiResource } from "../apiResource"; import { RequestOptions } from "../baseClient"; import { OPEN_AI_API_KEY } from "../constants"; -import { finalResponse, overrideConfig } from "../utils"; +import { defaultHeadersBuilder, finalResponse, overrideConfig } from "../utils"; import { createHeaders } from "./createHeaders"; import OpenAI from "openai"; @@ -66,7 +66,7 @@ export class Images extends ApiResource { const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); // @ts-ignore const result = await OAIclient.images.generate(body, opts).withResponse(); @@ -91,7 +91,7 @@ export class Images extends ApiResource { const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); // @ts-ignore @@ -117,7 +117,7 @@ export class Images extends ApiResource { const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); // @ts-ignore const result = await OAIclient.images.createVariation(body, opts).withResponse(); diff --git a/src/apis/models.ts b/src/apis/models.ts index 3c29f09..410bc4f 100644 --- a/src/apis/models.ts +++ b/src/apis/models.ts @@ -2,7 +2,7 @@ import { ApiClientInterface } from "../_types/generalTypes"; import { ApiResource } from "../apiResource"; import { RequestOptions } from "../baseClient"; import { OPEN_AI_API_KEY } from "../constants"; -import { finalResponse, overrideConfig } from "../utils"; +import { defaultHeadersBuilder, finalResponse, overrideConfig } from "../utils"; import { createHeaders } from "./createHeaders"; import OpenAI from "openai"; @@ -23,10 +23,7 @@ export class Models extends ApiResource { const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: { - ...this.client.customHeaders, - ...this.client.portkeyHeaders, - }, + defaultHeaders: defaultHeadersBuilder(this.client) }); const result = await OAIclient.models.list(opts).withResponse(); @@ -50,10 +47,7 @@ export class Models extends ApiResource { const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: { - ...this.client.customHeaders, - ...this.client.portkeyHeaders, - }, + defaultHeaders: defaultHeadersBuilder(this.client), }); const result = await OAIclient.models.retrieve(model, opts).withResponse(); @@ -77,10 +71,7 @@ export class Models extends ApiResource { const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: { - ...this.client.customHeaders, - ...this.client.portkeyHeaders, - }, + defaultHeaders: defaultHeadersBuilder(this.client), }); const result = await OAIclient.models.del(model, opts).withResponse(); diff --git a/src/apis/threads.ts b/src/apis/threads.ts index 49f0eb9..c333826 100644 --- a/src/apis/threads.ts +++ b/src/apis/threads.ts @@ -3,7 +3,7 @@ import { ApiClientInterface } from "../_types/generalTypes"; import { ApiResource } from "../apiResource"; import { RequestOptions } from "../baseClient"; import { OPEN_AI_API_KEY } from "../constants"; -import { finalResponse, overrideConfig } from "../utils"; +import { defaultHeadersBuilder, finalResponse, overrideConfig } from "../utils"; import { createHeaders } from "./createHeaders"; import OpenAI from "openai"; @@ -36,7 +36,7 @@ export class Threads extends ApiResource { const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); // @ts-ignore const result = await OAIclient.beta.threads.create(body, opts).withResponse(); @@ -60,7 +60,7 @@ export class Threads extends ApiResource { const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); const result = await OAIclient.beta.threads.retrieve(threadId, opts).withResponse(); @@ -86,7 +86,7 @@ export class Threads extends ApiResource { const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); const result = await OAIclient.beta.threads.update(threadId, body, opts).withResponse(); @@ -110,7 +110,7 @@ export class Threads extends ApiResource { const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); const result = await OAIclient.beta.threads.del(threadId, opts).withResponse(); @@ -135,7 +135,7 @@ export class Threads extends ApiResource { const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); const result = await OAIclient.beta.threads.createAndRun(body, opts).withResponse(); @@ -173,7 +173,7 @@ export class Messages extends ApiResource{ const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); // @ts-ignore const result = await OAIclient.beta.threads.messages.create(threadId, body, opts).withResponse(); @@ -199,7 +199,7 @@ export class Messages extends ApiResource{ const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); // @ts-ignore const result = await OAIclient.beta.threads.messages.list(threadId, query, opts).withResponse(); @@ -224,7 +224,7 @@ export class Messages extends ApiResource{ const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); const result = await OAIclient.beta.threads.messages.retrieve(threadId, messageId, opts).withResponse(); @@ -252,7 +252,7 @@ export class Messages extends ApiResource{ const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); const result = await OAIclient.beta.threads.messages.update(threadId, messageId, body, opts).withResponse(); @@ -284,7 +284,7 @@ export class Files extends ApiResource{ const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); // @ts-ignore const result = await OAIclient.beta.threads.messages.files.list(threadId, messageId, query, opts).withResponse(); @@ -310,7 +310,7 @@ export class Files extends ApiResource{ const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); const result = await OAIclient.beta.threads.messages.files.retrieve(threadId, messageId, fileId, opts).withResponse(); @@ -348,7 +348,7 @@ export class Runs extends ApiResource{ const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); const result = await OAIclient.beta.threads.runs.create(threadId, body, opts).withResponse(); @@ -374,7 +374,7 @@ export class Runs extends ApiResource{ const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); // @ts-ignore const result = await OAIclient.beta.threads.runs.list(threadId, query, opts).withResponse(); @@ -399,7 +399,7 @@ export class Runs extends ApiResource{ const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); const result = await OAIclient.beta.threads.runs.retrieve(threadId, runId, opts).withResponse(); @@ -426,7 +426,7 @@ export class Runs extends ApiResource{ const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); const result = await OAIclient.beta.threads.runs.update(threadId, runId, body, opts).withResponse(); @@ -453,7 +453,7 @@ export class Runs extends ApiResource{ const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); const result = await OAIclient.beta.threads.runs.submitToolOutputs(threadId, runId, body, opts).withResponse(); @@ -478,7 +478,7 @@ export class Runs extends ApiResource{ const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); const result = await OAIclient.beta.threads.runs.cancel(threadId, runId, opts).withResponse(); @@ -509,7 +509,7 @@ export class Steps extends ApiResource{ const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); // @ts-ignore const result = await OAIclient.beta.threads.runs.steps.list(threadId, runId, query, opts).withResponse(); @@ -535,7 +535,7 @@ export class Steps extends ApiResource{ const OAIclient = new OpenAI({ apiKey: OPEN_AI_API_KEY, baseURL: this.client.baseURL, - defaultHeaders: {...this.client.customHeaders, ...this.client.portkeyHeaders}, + defaultHeaders: defaultHeadersBuilder(this.client), }); const result = await OAIclient.beta.threads.runs.steps.retrieve(threadId, runId, stepId, opts).withResponse(); diff --git a/src/utils.ts b/src/utils.ts index 640f228..2d667a3 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -103,4 +103,16 @@ export function portkeyHeaders(headers:any) { .map(([key, value]) => [key.replace(prefix, ''), value]) return Object.fromEntries(filteredHeaders) +} + +export function defaultHeadersBuilder(client: any){ + + const customHeaders = client.customHeaders + const portkeyHeaders = client.portkeyHeaders + + customHeaders.hasOwnProperty("authorization") && + (client.customHeaders["authorization"] = + "Bearer " + client.customHeaders["authorization"]); + + return {...customHeaders, ...portkeyHeaders} } \ No newline at end of file