Skip to content

Commit f78cceb

Browse files
authored
fix(OpenAI Node): Update node to account for URL in credentials (#12356)
1 parent 11e8520 commit f78cceb

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

packages/@n8n/nodes-langchain/nodes/vendors/OpenAi/transport/index.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,22 @@ export async function apiRequest(
1818
endpoint: string,
1919
parameters?: RequestParameters,
2020
) {
21-
const { body, qs, uri, option, headers } = parameters ?? {};
21+
const { body, qs, option, headers } = parameters ?? {};
22+
23+
const credentials = await this.getCredentials('openAiApi');
24+
25+
let uri = `https://api.openai.com/v1${endpoint}`;
26+
27+
if (credentials.url) {
28+
uri = `${credentials?.url}${endpoint}`;
29+
}
2230

2331
const options = {
2432
headers,
2533
method,
2634
body,
2735
qs,
28-
uri: uri ?? `https://api.openai.com/v1${endpoint}`,
36+
uri,
2937
json: true,
3038
};
3139

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import type { IExecuteFunctions } from 'n8n-workflow';
2+
3+
import { apiRequest } from '../index';
4+
5+
const mockedExecutionContext = {
6+
getCredentials: jest.fn(),
7+
helpers: {
8+
requestWithAuthentication: jest.fn(),
9+
},
10+
};
11+
12+
describe('apiRequest', () => {
13+
beforeEach(() => {
14+
jest.resetAllMocks();
15+
});
16+
17+
it('should call requestWithAuthentication with credentials URL if one is provided', async () => {
18+
mockedExecutionContext.getCredentials.mockResolvedValue({
19+
url: 'http://www.test/url/v1',
20+
});
21+
22+
// Act
23+
await apiRequest.call(mockedExecutionContext as unknown as IExecuteFunctions, 'GET', '/test', {
24+
headers: { 'Content-Type': 'application/json' },
25+
});
26+
27+
// Assert
28+
29+
expect(mockedExecutionContext.getCredentials).toHaveBeenCalledWith('openAiApi');
30+
expect(mockedExecutionContext.helpers.requestWithAuthentication).toHaveBeenCalledWith(
31+
'openAiApi',
32+
{
33+
headers: { 'Content-Type': 'application/json' },
34+
method: 'GET',
35+
uri: 'http://www.test/url/v1/test',
36+
json: true,
37+
},
38+
);
39+
});
40+
41+
it('should call requestWithAuthentication with default URL if credentials URL is not provided', async () => {
42+
mockedExecutionContext.getCredentials.mockResolvedValue({});
43+
44+
// Act
45+
await apiRequest.call(mockedExecutionContext as unknown as IExecuteFunctions, 'GET', '/test', {
46+
headers: { 'Content-Type': 'application/json' },
47+
});
48+
49+
// Assert
50+
51+
expect(mockedExecutionContext.getCredentials).toHaveBeenCalledWith('openAiApi');
52+
expect(mockedExecutionContext.helpers.requestWithAuthentication).toHaveBeenCalledWith(
53+
'openAiApi',
54+
{
55+
headers: { 'Content-Type': 'application/json' },
56+
method: 'GET',
57+
uri: 'https://api.openai.com/v1/test',
58+
json: true,
59+
},
60+
);
61+
});
62+
});

0 commit comments

Comments
 (0)