Skip to content

Commit

Permalink
fix(OpenAI Node): Update node to account for URL in credentials (#12356)
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoE105 authored Dec 30, 2024
1 parent 11e8520 commit f78cceb
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -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,
},
);
});
});

0 comments on commit f78cceb

Please sign in to comment.