mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
fix(OpenAI Node): Update node to account for URL in credentials (#12356)
This commit is contained in:
@@ -18,14 +18,22 @@ export async function apiRequest(
|
|||||||
endpoint: string,
|
endpoint: string,
|
||||||
parameters?: RequestParameters,
|
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 = {
|
const options = {
|
||||||
headers,
|
headers,
|
||||||
method,
|
method,
|
||||||
body,
|
body,
|
||||||
qs,
|
qs,
|
||||||
uri: uri ?? `https://api.openai.com/v1${endpoint}`,
|
uri,
|
||||||
json: true,
|
json: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
62
packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/transport/test/transport.test.ts
vendored
Normal file
62
packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/transport/test/transport.test.ts
vendored
Normal file
@@ -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,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user