@wise/dynamic-flow-client-internal 1.0.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -84,7 +84,7 @@ DF can be initialised with `initialAction` (recommended) or with an `initialStep
84
84
  ```tsx
85
85
  <DynamicFlow
86
86
  initialAction={{ method: 'GET', url: '/my-amazing-new-flow' }}
87
- httpClient={(...args) => fetch(...args)}
87
+ customFetch={(...args) => fetch(...args)}
88
88
  onCompletion={(result) => {
89
89
  console.log('Flow exited with', result);
90
90
  }}
@@ -101,39 +101,43 @@ In some cases you may want to obtain the initial step yourself, and then pass it
101
101
  ```tsx
102
102
  <DynamicFlow
103
103
  initialStep={someInitialStep}
104
- httpClient={...}
104
+ customFetch={...}
105
105
  onCompletion={...}
106
106
  onError={...}
107
107
  />
108
108
  ```
109
109
 
110
- ### The `httpClient`
110
+ ### The `customFetch`
111
111
 
112
- You must pass an http client. This can be `window.fetch` itself or some wrapper function where you inject authorisation headers and anything else you many need.
112
+ You probably want to pass a custom fetch function. This would allow you to add additional request headers to each network request and possibly prefix a base URL to relative paths.
113
113
 
114
- You can take advantage of the `makeHttpClient` utility function. This function takes a `baseUrl` and `additionalHeaders` arguments. The `baseUrl` will be prefixed to any relative request URLs. Absolute URLs will not be altered. The `additionalHeaders` parameter can be used to add any request headers you need in all requests, such as `{ 'X-Access-Token': 'Tr4n5f3rw153' }`:
114
+ You can take advantage of the `makeCustomFetch` utility function. This function takes a `baseUrl` and `additionalHeaders` arguments. The `baseUrl` will be prefixed to any relative request URLs. Absolute URLs will not be altered. The `additionalHeaders` parameter can be used to add any request headers you need in all requests, such as `{ 'X-Access-Token': 'Tr4n5f3rw153' }`:
115
115
 
116
116
  ```tsx
117
- import { makeHttpClient, DynamicFlow } from '@wise/dynamic-flow-client';
117
+ import { makeCustomFetch, DynamicFlow } from '@wise/dynamic-flow-client-internal';
118
118
 
119
- const myHttpClient = makeHttpClient('/my-base-url', { 'X-Access-Token': 'Tr4n5f3rw153' });
119
+ const customFetch = makeCustomFetch('/gateway', {
120
+ 'Accept-Language': currentLanguage,
121
+ 'X-Access-Token': 'Tr4n5f3rw153',
122
+ 'X-Visual-Context': 'personal::light'
123
+ });
120
124
 
121
125
  ...
122
126
 
123
127
  <DynamicFlow
124
- initialAction={{ method: 'GET', url: '/my-amazing-new-flow' }}
125
- httpClient={myHttpClient}
128
+ initialAction={{ method: 'GET', url: '/my-flow' }}
129
+ customFetch={customFetch}
126
130
  onCompletion={...}
127
131
  onError={...}
128
132
  />
129
133
  ```
130
134
 
131
- #### Using a custom `httpClient`
135
+ #### Writing your own `customFetch` function
132
136
 
133
- If you want to write your own `httpClient` (or if you're writing mocks), it's important that you return proper `Response` objects, and that you **do not throw**. Errors should result in a response with an error status code and potentially a body with an error message. For example:
137
+ If you want to write your own `customFetch` (or if you're writing mocks), it's important that you return proper `Response` objects, and that you **do not throw**. Errors should result in a response with an error status code and potentially a body with an error message. For example:
134
138
 
135
139
  ```tsx
136
- const mockHttpClient = (input, init) => {
140
+ const mockCustomFetch = (input, init) => {
137
141
  switch (input) {
138
142
  case '/standard':
139
143
  return Promise.resolve(new Response(init.body));
@@ -145,17 +149,18 @@ const mockHttpClient = (input, init) => {
145
149
  }
146
150
  };
147
151
  ```
152
+
148
153
  Also, please make sure your mocks return a new `Response` instace every time. This is because responses are mutated when we parse their body, and they cannot be parsed a second time.
149
154
 
150
155
  ```ts
151
156
  const initialResponse = new Response(JSON.stringify(initialStep));
152
157
  // ❌ wrong - the same instance is returned on each request
153
- const mockHttpClient = (input, init) => Promise.resolve(initialResponse);
158
+ const mockCustomFetch = (input, init) => Promise.resolve(initialResponse);
154
159
  ```
155
160
 
156
161
  ```ts
157
162
  // ✅ correct - a new instance is returned on each request
158
- const mockHttpClient = (input, init) => Promise.resolve(new Response(JSON.stringify(initialStep)));
163
+ const mockCustomFetch = (input, init) => Promise.resolve(new Response(JSON.stringify(initialStep)));
159
164
  ```
160
165
 
161
166
  ### Telemetry
@@ -207,8 +212,6 @@ type LoaderConfig = {
207
212
  | `initial` | boolean | Whether or not to display the Loader component while loading the initial step. | true |
208
213
  | `submission` | boolean | Whether or not to display the Loader component during form submissions. | false |
209
214
 
210
-
211
215
  ## Contributing
212
216
 
213
217
  We love contributions! Check out `CONTRIBUTING.md` for more information.
214
-