-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support conversation to/from snakecase on axios client
- Loading branch information
Showing
7 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
docs/decisions/0006-middleware-support-for-http-clients.rst
This file contains 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,44 @@ | ||
Middleware Support for HTTP clients | ||
=================================== | ||
|
||
Status | ||
------ | ||
|
||
Accepted | ||
|
||
Context | ||
------- | ||
|
||
We currently expose HTTP clients(axios instances) via ``getAuthenticatedHttpClient`` and ``getHttpClient`` used to make API requests | ||
in our MFEs. There are instances where it would be helpful if consumers could apply middleware to these clients. | ||
For example the `axios-case-converter <https://www.npmjs.com/package/axios-case-converter>`_ package provides | ||
a middleware that handles snake-cased <-> camelCase conversions via axios interceptors. This middleware would allow our MFEs to | ||
avoid having to do this conversion manually. | ||
|
||
Decision | ||
-------- | ||
|
||
The ``initialize`` function provided in the initialize module initializes the ``AxiosJwtAuthService`` provided by ``@edx/frontend-platform``. | ||
We will add an optional param ``authMiddleware``, an array of middleware functions that will be applied to all http clients in | ||
the ``AxiosJwtAuthService``. | ||
|
||
Consumers will install the middleware they want to use and provide it to ``initialize``:: | ||
|
||
initialize({ | ||
messages: [appMessages], | ||
requireAuthenticatedUser: true, | ||
hydrateAuthenticatedUser: true, | ||
authMiddleware: [axiosCaseConverter, (client) => axiosRetry(client, { retries: 3 })], | ||
}); | ||
|
||
If a consumer chooses not to use ``initialize`` and instead the ``configure`` function, the middleware can be passed in the options param:: | ||
|
||
configure({ | ||
loggingService: getLoggingService(), | ||
config: getConfig(), | ||
options: { | ||
middleware: [axiosCaseConverter, (client) => axiosRetry(client, { retries: 3 })] | ||
} | ||
}); | ||
|
||
We decided to let consumers install their own middleware packages, removing the need to install the dependency as part of ``@edx/frontend-platform``. |
This file contains 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,58 @@ | ||
##################################################################### | ||
How to: Convert SnakeCase to CamelCase automatically for API Requests | ||
##################################################################### | ||
|
||
Introduction | ||
************ | ||
|
||
When using the HTTP client from ``@edx/frontend-platform``, you are making an API request to an | ||
Open edX service which requires you to handle snake-cased <-> camelCase conversions manually. The manual conversion quickly gets | ||
tedious, and is error prone if you forget to do it. | ||
|
||
Here is how you can configure the HTTP client to automatically convert snake_case <-> camelCase for you. | ||
|
||
How do I use configure automatic case conversion? | ||
************************************************* | ||
|
||
You want to install `axios-case-converter <https://www.npmjs.com/package/axios-case-converter>`_, and add it | ||
as a middleware when calling ``initialize`` in the consumer:: | ||
|
||
import axiosCaseConverter from 'axios-case-converter'; | ||
|
||
initialize({ | ||
messages: [], | ||
requireAuthenticatedUser: true, | ||
hydrateAuthenticatedUser: true, | ||
authMiddleware: [axiosCaseConverter], | ||
}); | ||
|
||
Or, if you choose to use ``configure`` instead:: | ||
|
||
import axiosCaseConverter from 'axios-case-converter'; | ||
|
||
configure({ | ||
loggingService: getLoggingService(), | ||
config: getConfig(), | ||
options: { | ||
middleware: [axiosCaseConverter, (client) => axiosRetry(client, { retries: 3 })] | ||
} | ||
}); | ||
|
||
By default the middleware will convert camelCase -> snake_case for payloads, and snake_case -> camelCase for responses. | ||
If you want to customize middleware behavior, i.e. only have responses transformed, you can configure it like this:: | ||
initialize({ | ||
messages: [], | ||
requireAuthenticatedUser: true, | ||
hydrateAuthenticatedUser: true, | ||
authMiddleware: [(client) => axiosCaseConverter(client, { | ||
// options for the middleware | ||
ignoreHeaders: true, // don't convert headers | ||
caseMiddleware: { | ||
requestInterceptor: (config) => { | ||
return config; | ||
} | ||
} | ||
})], | ||
}); | ||
|
||
See `axios-case-converter <https://github.com/mpyw/axios-case-converter>`_ for more details on configurations supported by the package. |
This file contains 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 contains 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 contains 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 contains 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 contains 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