mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-11 04:47:29 -08:00
fix(OpenAI Node): Update node to account for URL in credentials (#12356)
Some checks are pending
Test Master / install-and-build (push) Waiting to run
Test Master / Unit tests (18.x) (push) Blocked by required conditions
Test Master / Unit tests (20.x) (push) Blocked by required conditions
Test Master / Unit tests (22.4) (push) Blocked by required conditions
Test Master / Lint (push) Blocked by required conditions
Test Master / Notify Slack on failure (push) Blocked by required conditions
Some checks are pending
Test Master / install-and-build (push) Waiting to run
Test Master / Unit tests (18.x) (push) Blocked by required conditions
Test Master / Unit tests (20.x) (push) Blocked by required conditions
Test Master / Unit tests (22.4) (push) Blocked by required conditions
Test Master / Lint (push) Blocked by required conditions
Test Master / Notify Slack on failure (push) Blocked by required conditions
This commit is contained in:
parent
11e8520b70
commit
f78ccebe51
|
@ -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,
|
||||
};
|
||||
|
||||
|
|
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,
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue