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

This commit is contained in:
Ricardo Espinoza 2024-12-30 15:51:49 -05:00 committed by GitHub
parent 11e8520b70
commit f78ccebe51
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 72 additions and 2 deletions

View file

@ -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,
};

View 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,
},
);
});
});