mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 00:40:14 +01:00
🗺️ feat: Add Parameter Location Mapping for OpenAPI actions (#6858)
* fix: action parameters are assigned to the correct location (query, parameter, header, body) * removed copy/paste error * added unit tests, only add contenttype if specified --------- Co-authored-by: Peter Rothlaender <peter.rothlaender@ginkgo.com>
This commit is contained in:
parent
16aa5ed466
commit
6edd93f99e
2 changed files with 311 additions and 19 deletions
|
|
@ -206,6 +206,244 @@ describe('ActionRequest', () => {
|
|||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('handles GET requests with header and query parameters', async () => {
|
||||
mockedAxios.get.mockResolvedValue({ data: { success: true } });
|
||||
|
||||
const data: Record<string, unknown> = {
|
||||
'api-version': '2025-01-01',
|
||||
'some-header': 'header-var',
|
||||
};
|
||||
|
||||
const loc: Record<string, 'query' | 'path' | 'header' | 'body'> = {
|
||||
'api-version': 'query',
|
||||
'some-header': 'header',
|
||||
};
|
||||
|
||||
const actionRequest = new ActionRequest(
|
||||
'https://example.com',
|
||||
'/get',
|
||||
'GET',
|
||||
'testGET',
|
||||
false,
|
||||
'',
|
||||
loc,
|
||||
);
|
||||
const executer = actionRequest.setParams(data);
|
||||
const response = await executer.execute();
|
||||
expect(mockedAxios.get).toHaveBeenCalled();
|
||||
|
||||
const [url, config] = mockedAxios.get.mock.calls[0];
|
||||
expect(url).toBe('https://example.com/get');
|
||||
expect(config?.headers).toEqual({
|
||||
'some-header': 'header-var',
|
||||
});
|
||||
expect(config?.params).toEqual({
|
||||
'api-version': '2025-01-01',
|
||||
});
|
||||
expect(response.data.success).toBe(true);
|
||||
});
|
||||
|
||||
it('handles GET requests with header and path parameters', async () => {
|
||||
mockedAxios.get.mockResolvedValue({ data: { success: true } });
|
||||
|
||||
const data: Record<string, unknown> = {
|
||||
'user-id': '1',
|
||||
'some-header': 'header-var',
|
||||
};
|
||||
|
||||
const loc: Record<string, 'query' | 'path' | 'header' | 'body'> = {
|
||||
'user-id': 'path',
|
||||
'some-header': 'header',
|
||||
};
|
||||
|
||||
const actionRequest = new ActionRequest(
|
||||
'https://example.com',
|
||||
'/getwithpath/{user-id}',
|
||||
'GET',
|
||||
'testGETwithpath',
|
||||
false,
|
||||
'',
|
||||
loc,
|
||||
);
|
||||
const executer = actionRequest.setParams(data);
|
||||
const response = await executer.execute();
|
||||
expect(mockedAxios.get).toHaveBeenCalled();
|
||||
|
||||
const [url, config] = mockedAxios.get.mock.calls[0];
|
||||
expect(url).toBe('https://example.com/getwithpath/1');
|
||||
expect(config?.headers).toEqual({
|
||||
'some-header': 'header-var',
|
||||
});
|
||||
expect(config?.params).toEqual({
|
||||
});
|
||||
expect(response.data.success).toBe(true);
|
||||
});
|
||||
|
||||
it('handles POST requests with body, header and query parameters', async () => {
|
||||
mockedAxios.post.mockResolvedValue({ data: { success: true } });
|
||||
|
||||
const data: Record<string, unknown> = {
|
||||
'api-version': '2025-01-01',
|
||||
'message': 'a body parameter',
|
||||
'some-header': 'header-var',
|
||||
};
|
||||
|
||||
const loc: Record<string, 'query' | 'path' | 'header' | 'body'> = {
|
||||
'api-version': 'query',
|
||||
'message': 'body',
|
||||
'some-header': 'header',
|
||||
};
|
||||
|
||||
const actionRequest = new ActionRequest(
|
||||
'https://example.com',
|
||||
'/post',
|
||||
'POST',
|
||||
'testPost',
|
||||
false,
|
||||
'application/json',
|
||||
loc,
|
||||
);
|
||||
const executer = actionRequest.setParams(data);
|
||||
const response = await executer.execute();
|
||||
expect(mockedAxios.post).toHaveBeenCalled();
|
||||
|
||||
const [url, body, config] = mockedAxios.post.mock.calls[0];
|
||||
expect(url).toBe('https://example.com/post');
|
||||
expect(body).toEqual({ message: 'a body parameter' });
|
||||
expect(config?.headers).toEqual({
|
||||
'some-header': 'header-var',
|
||||
'Content-Type': 'application/json',
|
||||
});
|
||||
expect(config?.params).toEqual({
|
||||
'api-version': '2025-01-01',
|
||||
});
|
||||
expect(response.data.success).toBe(true);
|
||||
});
|
||||
|
||||
it('handles PUT requests with body, header and query parameters', async () => {
|
||||
mockedAxios.put.mockResolvedValue({ data: { success: true } });
|
||||
|
||||
const data: Record<string, unknown> = {
|
||||
'api-version': '2025-01-01',
|
||||
'message': 'a body parameter',
|
||||
'some-header': 'header-var',
|
||||
};
|
||||
|
||||
const loc: Record<string, 'query' | 'path' | 'header' | 'body'> = {
|
||||
'api-version': 'query',
|
||||
'message': 'body',
|
||||
'some-header': 'header',
|
||||
};
|
||||
|
||||
const actionRequest = new ActionRequest(
|
||||
'https://example.com',
|
||||
'/put',
|
||||
'PUT',
|
||||
'testPut',
|
||||
false,
|
||||
'application/json',
|
||||
loc,
|
||||
);
|
||||
const executer = actionRequest.setParams(data);
|
||||
const response = await executer.execute();
|
||||
expect(mockedAxios.put).toHaveBeenCalled();
|
||||
|
||||
const [url, body, config] = mockedAxios.put.mock.calls[0];
|
||||
expect(url).toBe('https://example.com/put');
|
||||
expect(body).toEqual({ message: 'a body parameter' });
|
||||
expect(config?.headers).toEqual({
|
||||
'some-header': 'header-var',
|
||||
'Content-Type': 'application/json',
|
||||
});
|
||||
expect(config?.params).toEqual({
|
||||
'api-version': '2025-01-01',
|
||||
});
|
||||
expect(response.data.success).toBe(true);
|
||||
});
|
||||
|
||||
it('handles PATCH requests with body, header and query parameters', async () => {
|
||||
mockedAxios.patch.mockResolvedValue({ data: { success: true } });
|
||||
|
||||
const data: Record<string, unknown> = {
|
||||
'api-version': '2025-01-01',
|
||||
'message': 'a body parameter',
|
||||
'some-header': 'header-var',
|
||||
};
|
||||
|
||||
const loc: Record<string, 'query' | 'path' | 'header' | 'body'> = {
|
||||
'api-version': 'query',
|
||||
'message': 'body',
|
||||
'some-header': 'header',
|
||||
};
|
||||
|
||||
const actionRequest = new ActionRequest(
|
||||
'https://example.com',
|
||||
'/patch',
|
||||
'PATCH',
|
||||
'testPatch',
|
||||
false,
|
||||
'application/json',
|
||||
loc,
|
||||
);
|
||||
const executer = actionRequest.setParams(data);
|
||||
const response = await executer.execute();
|
||||
expect(mockedAxios.patch).toHaveBeenCalled();
|
||||
|
||||
const [url, body, config] = mockedAxios.patch.mock.calls[0];
|
||||
expect(url).toBe('https://example.com/patch');
|
||||
expect(body).toEqual({ message: 'a body parameter' });
|
||||
expect(config?.headers).toEqual({
|
||||
'some-header': 'header-var',
|
||||
'Content-Type': 'application/json',
|
||||
});
|
||||
expect(config?.params).toEqual({
|
||||
'api-version': '2025-01-01',
|
||||
});
|
||||
expect(response.data.success).toBe(true);
|
||||
});
|
||||
|
||||
it('handles DELETE requests with body, header and query parameters', async () => {
|
||||
mockedAxios.delete.mockResolvedValue({ data: { success: true } });
|
||||
|
||||
const data: Record<string, unknown> = {
|
||||
'api-version': '2025-01-01',
|
||||
'message-id': '1',
|
||||
'some-header': 'header-var',
|
||||
};
|
||||
|
||||
const loc: Record<string, 'query' | 'path' | 'header' | 'body'> = {
|
||||
'api-version': 'query',
|
||||
'message-id': 'body',
|
||||
'some-header': 'header',
|
||||
};
|
||||
|
||||
const actionRequest = new ActionRequest(
|
||||
'https://example.com',
|
||||
'/delete',
|
||||
'DELETE',
|
||||
'testDelete',
|
||||
false,
|
||||
'application/json',
|
||||
loc,
|
||||
);
|
||||
const executer = actionRequest.setParams(data);
|
||||
const response = await executer.execute();
|
||||
expect(mockedAxios.delete).toHaveBeenCalled();
|
||||
|
||||
const [url, config] = mockedAxios.delete.mock.calls[0];
|
||||
expect(url).toBe('https://example.com/delete');
|
||||
expect(config?.data).toEqual({ 'message-id': '1' });
|
||||
expect(config?.headers).toEqual({
|
||||
'some-header': 'header-var',
|
||||
'Content-Type': 'application/json',
|
||||
});
|
||||
expect(config?.params).toEqual({
|
||||
'api-version': '2025-01-01',
|
||||
});
|
||||
expect(response.data.success).toBe(true);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
it('throws an error for unsupported HTTP method', async () => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue