@wise/dynamic-flow-client-internal 0.1.0

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.
Files changed (2) hide show
  1. package/README.md +214 -0
  2. package/package.json +99 -0
package/README.md ADDED
@@ -0,0 +1,214 @@
1
+ # Wise Dynamic Flow Web Client
2
+
3
+ This is the Wise Dynamic Flow web client. It provides a simple way to integrate a Dynamic Flow into your Wise web app.
4
+
5
+ ⚡ Access the latest deployed version of the [Dynamic Flow Playground](https://main--641de2ace0e1566a6a450fad.chromatic.com).
6
+
7
+ ## Integration
8
+
9
+ 1. Install `@wise/dynamic-flow-client-internal`.
10
+
11
+ ```
12
+ # yarn
13
+ yarn add @wise/dynamic-flow-client-internal
14
+
15
+ # npm
16
+ npm install @wise/dynamic-flow-client-internal
17
+
18
+ # pnpm
19
+ pnpm install @wise/dynamic-flow-client-internal
20
+ ```
21
+
22
+ 2. Install required peer dependencies (if not already installed). Please refer to setup instructions for `@transferwise/components` and `@transferwise/neptune-css` if installing up for the first time.
23
+
24
+ ```
25
+ # yarn
26
+ yarn add prop-types react react-dom react-intl
27
+ yarn add @transferwise/components @transferwise/formatting @transferwise/icons @transferwise/neptune-css @transferwise/neptune-validation
28
+
29
+ # npm
30
+ npm install prop-types react react-dom react-intl
31
+ npm install @transferwise/components @transferwise/formatting @transferwise/icons @transferwise/neptune-css @transferwise/neptune-validation
32
+
33
+ # pnpm
34
+ pnpm install prop-types react react-dom react-intl
35
+ pnpm install @transferwise/components @transferwise/formatting @transferwise/icons @transferwise/neptune-css @transferwise/neptune-validation
36
+ ```
37
+
38
+ **Note:** Keep in mind that some of these dependencies have their own peer dependencies. Don't forget to install those, for instance: `@transferwise/components` needs `@wise/art` and `@wise/components-theming`.
39
+
40
+ ```js
41
+ // Should be imported once in your application
42
+ import '@wise/dynamic-flow-client-internal/build/main.css';
43
+ ```
44
+
45
+ The `DynamicFlow` component must be wraped in a Neptune `Provider` to support localisation, a `ThemeProvider` to provide theming, and a `SnackbarProvider` to ensure snackbars display correctly. Translations should be imported from both components and dynamic flows, merged, and passed to the `Provider` component (as below).
46
+
47
+ ```js
48
+ import {
49
+ Provider,
50
+ SnackbarProvider,
51
+ translations as componentTranslations,
52
+ getLangFromLocale,
53
+ DEFAULT_LANG,
54
+ } from '@transferwise/components';
55
+ import {
56
+ DynamicFlow,
57
+ translations as dynamicFlowsTranslations,
58
+ } from '@wise/dynamic-flow-client-internal/';
59
+
60
+ const lang = getLangFromLocale(locale) || DEFAULT_LANG;
61
+ const i18n = {
62
+ locale,
63
+ messages: {
64
+ ...componentTranslations[lang],
65
+ ...dynamicFlowsTranslations[lang],
66
+ },
67
+ };
68
+
69
+ return (
70
+ <Provider i18n={i18n}>
71
+ <ThemeProvider theme={theme}>
72
+ <SnackbarProvider>
73
+ <DynamicFlow {...props} />
74
+ </SnackbarProvider>
75
+ </ThemeProvider>
76
+ </Provider>
77
+ );
78
+ ```
79
+
80
+ ## Configuring your Flow
81
+
82
+ DF can be initialised with `initialAction` (recommended) or with an `initialStep`.
83
+
84
+ ```tsx
85
+ <DynamicFlow
86
+ initialAction={{ method: 'GET', url: '/my-amazing-new-flow' }}
87
+ fetcher={(...args) => fetch(...args)}
88
+ onComplete={(result) => {
89
+ console.log('Flow exited with', result);
90
+ }}
91
+ onError={(error, statusCode) => {
92
+ console.error('Flow Error:', error, 'status code', statusCode);
93
+ }}
94
+ />
95
+ ```
96
+
97
+ ### When to use `initialStep` instead of `initialAction`
98
+
99
+ In some cases you may want to obtain the initial step yourself, and then pass it to DF component. In these cases you don't provide a `initialAction` since the next steps will result from interactions in the provided `initialStep`:
100
+
101
+ ```tsx
102
+ <DynamicFlow
103
+ initialStep={someInitialStep}
104
+ fetcher={...}
105
+ onComplete={...}
106
+ onError={...}
107
+ />
108
+ ```
109
+
110
+ ### The `fetcher` function prop
111
+
112
+ You must pass a fetcher function. This can be `window.fetch` itself or some wrapper function where you inject authorisation headers and anything else you many need.
113
+
114
+ You can take advantage of the `makeFetcher` 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
+
116
+ ```tsx
117
+ import { makeFetcher, DynamicFlow } from '@wise/dynamic-flow-client';
118
+
119
+ const myFetcher = makeFetcher('/my-base-url', { 'X-Access-Token': 'Tr4n5f3rw153' });
120
+
121
+ ...
122
+
123
+ <DynamicFlow
124
+ initialAction={{ method: 'GET', url: '/my-amazing-new-flow' }}
125
+ fetcher={myFetcher}
126
+ onComplete={...}
127
+ onError={...}
128
+ />
129
+ ```
130
+
131
+ #### Custom `fetcher` functions
132
+
133
+ If you want to write your own fetcher function (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
+
135
+ ```tsx
136
+ const mockFetcher = (input, init) => {
137
+ switch (input) {
138
+ case '/standard':
139
+ return Promise.resolve(new Response(init.body));
140
+ case '/exit':
141
+ return Promise.resolve(new Response(init.body, { headers: { 'x-df-exit': true } }));
142
+ case '/error':
143
+ default:
144
+ return Promise.resolve(new Response('An error has occurred.', { status: 500 }));
145
+ }
146
+ };
147
+ ```
148
+ 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
+
150
+ ```ts
151
+ const initialResponse = new Response(JSON.stringify(initialStep));
152
+ // ❌ wrong - the same instance is returned on each request
153
+ const mockFetcher = (input, init) => Promise.resolve(initialResponse);
154
+ ```
155
+
156
+ ```ts
157
+ // ✅ correct - a new instance is returned on each request
158
+ const mockFetcher = (input, init) => Promise.resolve(new Response(JSON.stringify(initialStep)));
159
+ ```
160
+
161
+ ### Telemetry
162
+
163
+ The `DynamicFlow` component accepts two optional props: `onEvent` and `onLog` which can be used to track and log.
164
+
165
+ In the example below we send tracking events to Mixpanel and logging events to Rollbar.
166
+
167
+ ```tsx
168
+ <DynamicFlow
169
+ onEvent={(event, props) => mixpanel.track(event, props)}
170
+ onLog={(level, message, extra) => Rollbar[level](message, extra)}
171
+ />
172
+ ```
173
+
174
+ Alternatively, you can log to the browser console:
175
+
176
+ ```ts
177
+ onEvent={(event, props) => console.log(event, props)}
178
+ onLog={(level, message, extra) => {
179
+ const levelToConsole = {
180
+ critical: console.error,
181
+ error: console.error,
182
+ warning: console.warn,
183
+ info: console.info,
184
+ debug: console.log,
185
+ } as const;
186
+ levelToConsole[level](message, extra);
187
+ }}
188
+ ```
189
+
190
+ ### Loader configuration
191
+
192
+ By default, DF will display a loading animation (The `Loader` component from Neptune) when the first step is loading. It will not display it during refresh-on-change or while submitting forms.
193
+
194
+ You can change the defaults by passing a `loaderConfig` prop:
195
+
196
+ ```ts
197
+ type LoaderConfig = {
198
+ size?: `xs | sm | md | lg | xl`;
199
+ initial?: boolean;
200
+ submission?: boolean;
201
+ };
202
+ ```
203
+
204
+ | property | type | notes | default |
205
+ | ------------ | ------- | ------------------------------------------------------------------------------ | ------- |
206
+ | `size` | string | The size of the Loader component. | `xl` |
207
+ | `initial` | boolean | Whether or not to display the Loader component while loading the initial step. | true |
208
+ | `submission` | boolean | Whether or not to display the Loader component during form submissions. | false |
209
+
210
+
211
+ ## Contributing
212
+
213
+ We love contributions! Check out `CONTRIBUTING.md` for more information.
214
+
package/package.json ADDED
@@ -0,0 +1,99 @@
1
+ {
2
+ "name": "@wise/dynamic-flow-client-internal",
3
+ "version": "0.1.0",
4
+ "description": "Dynamic Flow web client for Wise",
5
+ "main": "./build/index.js",
6
+ "types": "./build/index.d.ts",
7
+ "style": "./build/main.css",
8
+ "sideEffects": [
9
+ "*.css"
10
+ ],
11
+ "files": [
12
+ "build"
13
+ ],
14
+ "repository": {
15
+ "type": "git",
16
+ "fullname": "transferwise/dynamic-flow",
17
+ "url": "git+https://github.com/transferwise/dynamic-flow.git"
18
+ },
19
+ "scripts": {
20
+ "dev": "storybook dev -p 3005",
21
+ "build": "npm-run-all build:*",
22
+ "build:ts": "tsc --build ./tsconfig.build.json",
23
+ "build:css": "postcss src/main.css -o build/main.css",
24
+ "test": "pnpm test:once",
25
+ "test:once": "jest --config jest.config.js --env=jsdom",
26
+ "test:coverage": "jest --config jest.config.js --env=jsdom --coverage",
27
+ "test:watch": "pnpm test:once --watch",
28
+ "lint": "npm-run-all lint:ts lint:css",
29
+ "lint:ts": "eslint 'src/**/*.{js,jsx,ts,tsx}' --quiet",
30
+ "lint:css": "stylelint --allow-empty-input './src/**/*.css'",
31
+ "build-storybook": "storybook build -c .storybook"
32
+ },
33
+ "devDependencies": {
34
+ "@babel/core": "7.20.12",
35
+ "@babel/plugin-syntax-flow": "7.18.6",
36
+ "@babel/plugin-transform-react-jsx": "7.21.0",
37
+ "@babel/preset-env": "7.20.2",
38
+ "@babel/preset-react": "7.18.6",
39
+ "@babel/preset-typescript": "7.21.0",
40
+ "@formatjs/cli": "^4.8.3",
41
+ "@storybook/addon-a11y": "7.0.9",
42
+ "@storybook/addon-actions": "7.0.9",
43
+ "@storybook/addon-essentials": "7.0.9",
44
+ "@storybook/addon-interactions": "7.0.9",
45
+ "@storybook/addon-links": "7.0.9",
46
+ "@storybook/react": "7.0.9",
47
+ "@storybook/react-webpack5": "7.0.9",
48
+ "@storybook/testing-library": "0.1.0",
49
+ "@testing-library/dom": "9.2.0",
50
+ "@testing-library/jest-dom": "5.16.5",
51
+ "@testing-library/react": "14.0.0",
52
+ "@testing-library/react-hooks": "8.0.1",
53
+ "@testing-library/user-event": "13.2.1",
54
+ "@transferwise/components": "43.13.49",
55
+ "@transferwise/eslint-config": "8.0.1",
56
+ "@transferwise/formatting": "^2.10.0",
57
+ "@transferwise/icons": "3.2.3",
58
+ "@transferwise/neptune-css": "14.3.45",
59
+ "@transferwise/neptune-tokens": "8.4.0",
60
+ "@types/jest": "29.4.0",
61
+ "@types/react": "18",
62
+ "@types/react-dom": "18",
63
+ "@types/testing-library__jest-dom": "5.9.5",
64
+ "@wise/art": "2.0.1",
65
+ "@wise/components-theming": "^0.7.4",
66
+ "babel-jest": "29.5.0",
67
+ "currency-flags": "4.0.7",
68
+ "eslint": "8.36.0",
69
+ "eslint-plugin-storybook": "0.6.12",
70
+ "jest": "29.5.0",
71
+ "jest-environment-jsdom": "29.5.0",
72
+ "jest-fetch-mock": "^3.0.3",
73
+ "jest-watch-typeahead": "^2.2.2",
74
+ "npm-run-all": "4.1.5",
75
+ "postcss": "^8.4.21",
76
+ "postcss-cli": "^10.1.0",
77
+ "postcss-import": "^15.1.0",
78
+ "prettier": "2.8.4",
79
+ "react": "18.2.0",
80
+ "react-dom": "18.2.0",
81
+ "react-intl": "6.2.1",
82
+ "storybook": "7.0.9",
83
+ "stylelint": "14.16.1",
84
+ "stylelint-config-standard": "25.0.0",
85
+ "stylelint-no-unsupported-browser-features": "5.0.4",
86
+ "stylelint-value-no-unknown-custom-properties": "4.0.0",
87
+ "typescript": "4.9.5",
88
+ "webpack": "5.76.0"
89
+ },
90
+ "peerDependencies": {
91
+ "react": "^18",
92
+ "react-dom": "^18",
93
+ "react-intl": "^6"
94
+ },
95
+ "dependencies": {
96
+ "@wise/dynamic-flow-client": "^0.2.0"
97
+ },
98
+ "prettier": "@transferwise/eslint-config/.prettierrc.js"
99
+ }