Skip to content

Feature/api_client#370

Merged
cafca merged 77 commits intodevelopfrom
feature/centralize_api_logic
Aug 12, 2020
Merged

Feature/api_client#370
cafca merged 77 commits intodevelopfrom
feature/centralize_api_logic

Conversation

@s-pic
Copy link
Copy Markdown
Collaborator

@s-pic s-pic commented Mar 31, 2020

Type of change

  • New feature (non-breaking change which adds functionality)

change summary

Prepares to solve #https://github.com/FixMyBerlin/fixmy.platform/issues/271 → This just adds the api module to do http requests against our backend and makes it subject to further review. We can than gradually replace de-duped code in pages/Map/apiservice.js, pages/User/apiservice.js and pages/Reports/apiservice.js

motivation and context

We finally want to solve https://github.com/FixMyBerlin/fixmy.platform/issues/57 → Once we have a central api client and a central place to throw api-related errors, we can work on the error handling in the view layer.

updates to dependencies and project setup

  • mocks ky with ky-universal in unit tests so that required browser APIs get polyfilled using node-fetch 39fa7eb
  • updates fetch-mock. Only after that update, response bodies could be inspeced with fetch-mocks lastOptions()
  • load dotenv during jest setup
  • updates ky

How Has This Been Tested?

Run npm run test to run new unit tests.

image

Aspects marked for discussion

Errors classes:

  • What kind of errors we want to differentiate in the client. Do we want to handle 401s, 404s etc. specifically?
  • api docs for the backend to get a grasp on how responses are structured throughout the api. I just looked at some endpoints in the reports feature

token handling

  • I figured we can read the current token from the store before every request and attach it as header.
    This has been implemented using a beforeRequest hook offered by ky
  • If we handle the current token like so, we don't need to connect our components to the store just in order to pass the token down to some component so that this component can pass it to an apiService
  • A negative aspect is that the apiService knows about our store, a dependency injection would be the cleaner pattern, but then we would need either multiple instances of the apiService or a Singleton. I considered the dependency to me fine since we will propably share the service between different apps.
  • If we keep the token handling as proposed, we could consider not storing it in the redux store at all.

What are the proposed next steps?

Iteratively integrate this, this should be another issue.
Here already some ideas what should happen:

  • make all ajax calls against our backend run over the new module
  • remove token from component state

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works

Sascha added 30 commits February 24, 2020 20:51
…to feature/centralize_api_logic

# Conflicts:
#	src/services/api/httpRequest.ts
#	src/services/api/index.ts
@cafca cafca reopened this Jun 3, 2020
@cafca cafca marked this pull request as draft July 15, 2020 13:27
@cafca cafca marked this pull request as ready for review July 29, 2020 19:05
Comment thread src/services/api/makeFMCError.ts Outdated
@cafca
Copy link
Copy Markdown
Collaborator

cafca commented Jul 30, 2020

@s-pic I have made a number of changes in this PR. I will list only the major changes here:

  • export all the error classes from index.ts to enable this pattern:
try {
  await api.request(url)
} catch(err) {
  if (err instanceof api.ApiError) {
    // handle api error
  } else {
    // handle other error
  }
}
  • renamed some of the modules and symbols to clarify their responsibilties
  • flatten the syntax of the options argument to request by including kyOptions in the main options parameter. I know you mentioned that you think there are good reasons for doing this so maybe we should talk about it. I think the more concise syntax is worth any future namespace clashes if that was your concern, but maybe there's something else I'm not seeing.
  • add an onSlowResponse callback that is triggered if requests don't complete within 5 seconds
  • add docstrings
  • added debug logging in the fmc:api namespace. I think this will come in very handy for debugging in the future.

I have made a comment above about the NetworkError class, which I don't like as it is now. We should find a way to fix it.

@s-pic
Copy link
Copy Markdown
Collaborator Author

s-pic commented Jul 30, 2020

Thanks for your remarks, I will get back to it asap.

Copy link
Copy Markdown
Collaborator Author

@s-pic s-pic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • flatten the syntax of the options argument to request by including kyOptions in the main options parameter. I know you mentioned that you think there are good reasons for doing this so maybe we should talk about it. I think the more concise syntax is worth any future namespace clashes if that was your concern, but maybe there's something else I'm not seeing.

Jup, my concern was to to mix up our config props with those for ky.
I see your point and I can live with the change 😃.

I have made a comment above about the NetworkError class, which I don't like as it is now. We should find a way to fix it.

I will take another shot at it.

Comment thread src/services/api/request.ts
Comment thread src/services/api/tests/api.unit.test.js
Comment thread src/services/api/request.ts
Comment thread src/services/api/makeFMCError.ts Outdated
Comment thread src/services/api/errors.ts Outdated
Comment thread src/services/api/makeFMCError.ts Outdated
@cafca cafca marked this pull request as draft August 11, 2020 12:14
@cafca cafca marked this pull request as ready for review August 12, 2020 16:34
@cafca cafca merged commit fee9f7f into develop Aug 12, 2020
@cafca cafca deleted the feature/centralize_api_logic branch August 12, 2020 16:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants