feat(davinci-client): introduce request middleware#171
Conversation
🦋 Changeset detectedLatest commit: ce0d18f The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
View your CI Pipeline Execution ↗ for commit ce0d18f.
☁️ Nx Cloud last updated this comment at |
8838b69 to
19f4681
Compare
|
Deployed ac7598f to https://ForgeRock.github.io/ping-javascript-sdk/pr-171/ac7598f8c02c2d5a10d143677da201273542b7b6 branch gh-pages in ForgeRock/ping-javascript-sdk |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #171 +/- ##
==========================================
+ Coverage 44.22% 50.59% +6.36%
==========================================
Files 30 24 -6
Lines 1438 1354 -84
Branches 175 177 +2
==========================================
+ Hits 636 685 +49
+ Misses 802 669 -133
🚀 New features to boost your workflow:
|
| @@ -0,0 +1,9 @@ | |||
| export enum ActionTypes { | |||
There was a problem hiding this comment.
Why not just use a union? Don't really see the value in an enum here.
| export enum ActionTypes { | |
| export type ActionTypes = 'WELL_KNOWN'| 'DAVINCI_START' ... |
There was a problem hiding this comment.
Got it. This was just a relic from the old middleware design. I've updated it according to your suggestion.
|
|
||
| type NextFn = () => ModifiedFetchArgs; | ||
|
|
||
| const a = 'a' as ActionTypes; |
There was a problem hiding this comment.
If we take this code and the above comment about the enum
const values = {
a : 'a',
b: 'b',
c: 'c',
reassign: 'REASSIGN',
} as const
type ActionTypes = typeof values[keyof typeof values];
This makes it so the type is always up to date with the values, the type becomes self-updating as long as the object is updated.
we can obviously destructure all these values out if we need them individually also.
I believe this removes all of the as casting as well.
| import type { ModifiedFetchArgs, RequestMiddleware } from './request.effect.types.js'; | ||
|
|
||
| function getActionType(endpoint: string): ActionTypes { | ||
| switch (endpoint) { |
There was a problem hiding this comment.
I also think this would just become something like
return values[endpoint] || fallback if we move off the enum and just use the as const version.
There was a problem hiding this comment.
I'm not a huge fan of the effect naming convention within the folder. I imagine these files will be moved to a package effects but just feels overly verbose to me personally.
There was a problem hiding this comment.
This is just sticking with the convention that's already been established in the project. All files have a <name>.<file-type>.<file-extension> pattern. Yes, it's a bit verbose, but it can be helpful when files are open within displays that may not make the containing directory obvious. Plus, it really reinforces the fact that the file type strongly defines the patterns and conventions of the file contents.
|
|
||
| function iterator(): ModifiedFetchArgs { | ||
| const nextMiddlewareToBeCalled = mwareCopy.shift(); | ||
| if (nextMiddlewareToBeCalled) nextMiddlewareToBeCalled(request, actionCopy, iterator); |
There was a problem hiding this comment.
So when we ultimately call this nextMiddlewareToBeCalled, this function can modify the request?
There was a problem hiding this comment.
Yes, that is correct. This is almost an exact copy of what we have in the legacy SDK. This line is what calls the function that is provided by the developer.
|
|
||
| if (typeof api.extra === 'function') { | ||
| api.extra(); | ||
| console.log(api.endpoint); |
| const response = await queryApi.applyQuery(async (fetchArgs) => await mockQuery(fetchArgs)); | ||
|
|
||
| expect(resultFetchArgs.url.toString()).toBe('https://www.example.com/?searchParam=abc'); | ||
| expect(resultFetchArgs.headers).toStrictEqual({ 'x-new-header': '123' }); |
8cde118 to
77c47c8
Compare
77c47c8 to
dc469d5
Compare
|
should we add e2e tests for headers?
|
Good questions!
|
On PTO, reviewed, tested and approved by AJ
JIRA Ticket
SDKS-3624
Description
This introduces the feature of network or request middleware to the DaVinci Client. This allows the developer to intervene just before the request is made and make any last minutes changes to a request before it is executed by the browser.
Changeset? Yes.