diff --git a/packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/transport/index.ts b/packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/transport/index.ts index 36a5773245..619869bf9d 100644 --- a/packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/transport/index.ts +++ b/packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/transport/index.ts @@ -18,14 +18,22 @@ export async function apiRequest( endpoint: string, parameters?: RequestParameters, ) { - const { body, qs, uri, option, headers } = parameters ?? {}; + const { body, qs, option, headers } = parameters ?? {}; + + const credentials = await this.getCredentials('openAiApi'); + + let uri = `https://api.openai.com/v1${endpoint}`; + + if (credentials.url) { + uri = `${credentials?.url}${endpoint}`; + } const options = { headers, method, body, qs, - uri: uri ?? `https://api.openai.com/v1${endpoint}`, + uri, json: true, }; diff --git a/packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/transport/test/transport.test.ts b/packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/transport/test/transport.test.ts new file mode 100644 index 0000000000..399dad8562 --- /dev/null +++ b/packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/transport/test/transport.test.ts @@ -0,0 +1,62 @@ +import type { IExecuteFunctions } from 'n8n-workflow'; + +import { apiRequest } from '../index'; + +const mockedExecutionContext = { + getCredentials: jest.fn(), + helpers: { + requestWithAuthentication: jest.fn(), + }, +}; + +describe('apiRequest', () => { + beforeEach(() => { + jest.resetAllMocks(); + }); + + it('should call requestWithAuthentication with credentials URL if one is provided', async () => { + mockedExecutionContext.getCredentials.mockResolvedValue({ + url: 'http://www.test/url/v1', + }); + + // Act + await apiRequest.call(mockedExecutionContext as unknown as IExecuteFunctions, 'GET', '/test', { + headers: { 'Content-Type': 'application/json' }, + }); + + // Assert + + expect(mockedExecutionContext.getCredentials).toHaveBeenCalledWith('openAiApi'); + expect(mockedExecutionContext.helpers.requestWithAuthentication).toHaveBeenCalledWith( + 'openAiApi', + { + headers: { 'Content-Type': 'application/json' }, + method: 'GET', + uri: 'http://www.test/url/v1/test', + json: true, + }, + ); + }); + + it('should call requestWithAuthentication with default URL if credentials URL is not provided', async () => { + mockedExecutionContext.getCredentials.mockResolvedValue({}); + + // Act + await apiRequest.call(mockedExecutionContext as unknown as IExecuteFunctions, 'GET', '/test', { + headers: { 'Content-Type': 'application/json' }, + }); + + // Assert + + expect(mockedExecutionContext.getCredentials).toHaveBeenCalledWith('openAiApi'); + expect(mockedExecutionContext.helpers.requestWithAuthentication).toHaveBeenCalledWith( + 'openAiApi', + { + headers: { 'Content-Type': 'application/json' }, + method: 'GET', + uri: 'https://api.openai.com/v1/test', + json: true, + }, + ); + }); +});