-
Notifications
You must be signed in to change notification settings - Fork 4.5k
chore: Refactor API #36412
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
Merged
Merged
chore: Refactor API #36412
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4ef493d
refactor api and add tests for interceptors
531b6b0
fix imports
39478d7
remove apiutils test file
97af61d
use default serializer of axios
2a0c476
add missing logic
967906a
update tests
88dc351
add comment for why we pass 2nd second parameter in api functions
ed537d3
add comment for why we pass 2nd second parameter in api functions
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export const compose = | ||
| <T>(...fns: Array<(arg: T) => T>) => | ||
| (x: T) => | ||
| fns.reduce((acc, fn) => fn(acc), x); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { compose } from "./compose"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export * from "./object"; | ||
| export * from "./compose"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
225 changes: 225 additions & 0 deletions
225
app/client/src/api/__tests__/apiFailureResponseInterceptors.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,225 @@ | ||
| import axios from "axios"; | ||
| import type { AxiosError } from "axios"; | ||
|
|
||
| import { | ||
| apiFailureResponseInterceptor, | ||
| apiSuccessResponseInterceptor, | ||
| } from "api/interceptors"; | ||
| import type { ApiResponse } from "api/types"; | ||
| import { | ||
| createMessage, | ||
| ERROR_0, | ||
| ERROR_413, | ||
| ERROR_500, | ||
| SERVER_API_TIMEOUT_ERROR, | ||
| } from "ee/constants/messages"; | ||
| import { ERROR_CODES } from "ee/constants/ApiConstants"; | ||
| import { UserCancelledActionExecutionError } from "sagas/ActionExecution/errorUtils"; | ||
|
|
||
| describe("Api success response interceptors", () => { | ||
| beforeAll(() => { | ||
| axios.interceptors.response.use( | ||
| apiSuccessResponseInterceptor, | ||
| apiFailureResponseInterceptor, | ||
| ); | ||
| }); | ||
|
|
||
| it("checks 413 error", async () => { | ||
| axios.defaults.adapter = async () => { | ||
| return Promise.reject({ | ||
| response: { | ||
| status: 413, | ||
| }, | ||
| } as AxiosError); | ||
| }; | ||
|
|
||
| try { | ||
| await axios.get("https://example.com"); | ||
| } catch (error) { | ||
| expect((error as AxiosError<ApiResponse>).response?.status).toBe(413); | ||
| expect((error as AxiosError<ApiResponse>).message).toBe( | ||
| createMessage(ERROR_413, 100), | ||
| ); | ||
| expect( | ||
| (error as AxiosError<ApiResponse> & { statusCode?: string }).statusCode, | ||
| ).toBe("AE-APP-4013"); | ||
| } | ||
|
|
||
| axios.defaults.adapter = undefined; | ||
| }); | ||
|
|
||
| it("checks the response message when request is made when user is offline", async () => { | ||
| const onlineGetter: jest.SpyInstance = jest.spyOn( | ||
| window.navigator, | ||
| "onLine", | ||
| "get", | ||
| ); | ||
|
|
||
| onlineGetter.mockReturnValue(false); | ||
| axios.defaults.adapter = async () => { | ||
| return new Promise((resolve, reject) => { | ||
| reject({ | ||
| message: "Network Error", | ||
| } as AxiosError); | ||
| }); | ||
| }; | ||
|
|
||
| try { | ||
| await axios.get("https://example.com"); | ||
| } catch (error) { | ||
| expect((error as AxiosError<ApiResponse>).message).toBe( | ||
| createMessage(ERROR_0), | ||
| ); | ||
| } | ||
|
|
||
| onlineGetter.mockRestore(); | ||
| axios.defaults.adapter = undefined; | ||
| }); | ||
|
|
||
| it("checks if it throws UserCancelledActionExecutionError user cancels the request ", async () => { | ||
| const cancelToken = axios.CancelToken.source(); | ||
|
|
||
| axios.defaults.adapter = async () => { | ||
| return new Promise((resolve, reject) => { | ||
| cancelToken.cancel("User cancelled the request"); | ||
|
|
||
| reject({ | ||
| message: "User cancelled the request", | ||
| } as AxiosError); | ||
| }); | ||
| }; | ||
|
|
||
| try { | ||
| await axios.get("https://example.com", { | ||
| cancelToken: cancelToken.token, | ||
| }); | ||
| } catch (error) { | ||
| expect(error).toBeInstanceOf(UserCancelledActionExecutionError); | ||
| } | ||
|
|
||
| axios.defaults.adapter = undefined; | ||
| }); | ||
|
|
||
| it("checks the response message when request fails for exeuction action urls", async () => { | ||
| axios.defaults.adapter = async () => { | ||
| return Promise.reject({ | ||
| response: { | ||
| status: 500, | ||
| statusText: "Internal Server Error", | ||
| headers: { | ||
| "content-length": 1, | ||
| }, | ||
| config: { | ||
| headers: { | ||
| timer: "1000", | ||
| }, | ||
| }, | ||
| }, | ||
| config: { | ||
| url: "/v1/actions/execute", | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| const url = "/v1/actions/execute"; | ||
| const response = await axios.get(url); | ||
|
|
||
| expect(response).toHaveProperty("clientMeta"); | ||
|
|
||
| axios.defaults.adapter = undefined; | ||
| }); | ||
|
|
||
| it("checks the error response in case of timeout", async () => { | ||
| axios.defaults.adapter = async () => { | ||
| return Promise.reject({ | ||
| code: "ECONNABORTED", | ||
| message: "timeout of 1000ms exceeded", | ||
| }); | ||
| }; | ||
|
|
||
| try { | ||
| await axios.get("https://example.com"); | ||
| } catch (error) { | ||
| expect((error as AxiosError<ApiResponse>).message).toBe( | ||
| createMessage(SERVER_API_TIMEOUT_ERROR), | ||
| ); | ||
| expect((error as AxiosError<ApiResponse>).code).toBe( | ||
| ERROR_CODES.REQUEST_TIMEOUT, | ||
| ); | ||
| } | ||
|
|
||
| axios.defaults.adapter = undefined; | ||
| }); | ||
|
|
||
| it("checks the error response in case of server error", async () => { | ||
| axios.defaults.adapter = async () => { | ||
| return Promise.reject({ | ||
| response: { | ||
| status: 502, | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| try { | ||
| await axios.get("https://example.com"); | ||
| } catch (error) { | ||
| expect((error as AxiosError<ApiResponse>).message).toBe( | ||
| createMessage(ERROR_500), | ||
| ); | ||
| expect((error as AxiosError<ApiResponse>).code).toBe( | ||
| ERROR_CODES.SERVER_ERROR, | ||
| ); | ||
| } | ||
|
|
||
| axios.defaults.adapter = undefined; | ||
| }); | ||
|
|
||
| it("checks error response in case of unauthorized error", async () => { | ||
| axios.defaults.adapter = async () => { | ||
| return Promise.reject({ | ||
| response: { | ||
| status: 401, | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| try { | ||
| await axios.get("https://example.com"); | ||
| } catch (error) { | ||
| expect((error as AxiosError<ApiResponse>).message).toBe( | ||
| "Unauthorized. Redirecting to login page...", | ||
| ); | ||
| expect((error as AxiosError<ApiResponse>).code).toBe( | ||
| ERROR_CODES.REQUEST_NOT_AUTHORISED, | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| it("checks error response in case of not found error", async () => { | ||
| axios.defaults.adapter = async () => { | ||
| return Promise.reject({ | ||
| response: { | ||
| data: { | ||
| responseMeta: { | ||
| status: 404, | ||
| error: { | ||
| code: "AE-ACL-4004", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| try { | ||
| await axios.get("https://example.com"); | ||
| } catch (error) { | ||
| expect((error as AxiosError<ApiResponse>).message).toBe( | ||
| "Resource Not Found", | ||
| ); | ||
| expect((error as AxiosError<ApiResponse>).code).toBe( | ||
| ERROR_CODES.PAGE_NOT_FOUND, | ||
| ); | ||
| } | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reusing the refactored interceptors.