Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify apiRequest call #11923

Merged
merged 3 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion services/ui-src/src/utils/api/apiLib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
const mockResponse = (method?: string) => ({
response: { body: { json: () => ({ test: method }) } },
});
const mockDelete = jest.fn().mockImplementation(() => mockResponse());
const mockDelete = jest.fn().mockImplementation(() => ({ response: {} }));
const mockGet = jest.fn().mockImplementation(() => mockResponse("get"));
const mockPost = jest.fn().mockImplementation(() => mockResponse("post"));
const mockPut = jest.fn().mockImplementation(() => mockResponse("put"));
Expand Down
12 changes: 6 additions & 6 deletions services/ui-src/src/utils/api/apiLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ export async function apiRequest<T>(
try {
await updateTimeout();

if (!hasResponseBody) {
await request({ apiName, path, options }).response;
const { body } = await request({ apiName, path, options }).response;

if (hasResponseBody === false) {
return undefined as unknown as T;
}

const { body } = await request({ apiName, path, options }).response;
return (await body.json()) as unknown as T;
} catch (e: any) {
const info = `Request Failed - ${path} - ${e.response?.body}`;
Expand All @@ -97,13 +97,13 @@ export async function del<T>(path: string, opts?: RequestOptions): Promise<T> {
}

export async function get<T>(path: string, opts?: RequestOptions): Promise<T> {
return apiRequest<T>(ampGet, path, opts, true);
return apiRequest<T>(ampGet, path, opts);
}

export async function post<T>(path: string, opts?: RequestOptions): Promise<T> {
return apiRequest<T>(ampPost, path, opts, true);
return apiRequest<T>(ampPost, path, opts);
}

export async function put<T>(path: string, opts?: RequestOptions): Promise<T> {
return apiRequest<T>(ampPut, path, opts, true);
return apiRequest<T>(ampPut, path, opts);
}