@servicetitan/docs-uikit 24.3.0 → 25.0.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.
- package/docs/ajax-handlers.mdx +146 -0
- package/package.json +2 -2
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: AJAX Handlers
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
import Admonition from '@theme/Admonition';
|
|
6
|
+
import { VersionHistory, Changes } from '@site/src/components/version-history';
|
|
7
|
+
|
|
8
|
+
<VersionHistory>
|
|
9
|
+
<Changes forVersion="v25.0.0">
|
|
10
|
+
<Admonition type="caution" title="Breaking changes">
|
|
11
|
+
Changed <code>withMicroservice</code> to accept a single options argument.
|
|
12
|
+
</Admonition>
|
|
13
|
+
</Changes>
|
|
14
|
+
</VersionHistory>
|
|
15
|
+
|
|
16
|
+
## withMicroservice
|
|
17
|
+
|
|
18
|
+
`withMicroservice` is a higher-order function that wraps a component and authenticates
|
|
19
|
+
its requests to protected resources.
|
|
20
|
+
|
|
21
|
+
### Usage
|
|
22
|
+
|
|
23
|
+
The best way to use `withMicroservice` is to wrap the component in the ES module body. E.g.,
|
|
24
|
+
|
|
25
|
+
```tsx
|
|
26
|
+
import { withMicroservice } from '@servicetitan/ajax-handlers';
|
|
27
|
+
|
|
28
|
+
const UnwrappedApp: FC = () => {
|
|
29
|
+
...
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const App = withMicroservice({
|
|
33
|
+
baseURL: '/api',
|
|
34
|
+
authURL: '/api/auth',
|
|
35
|
+
component: UnwrappedApp,
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
If `withMicroservice` is called within another React component, we recommend memoizing
|
|
40
|
+
the return value to avoid performance issues when re-rendering. E.g.,
|
|
41
|
+
|
|
42
|
+
```tsx
|
|
43
|
+
import { withMicroservice } from '@servicetitan/ajax-handlers';
|
|
44
|
+
|
|
45
|
+
const UnwrappedApp: FC = () => {
|
|
46
|
+
...
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const App = () => {
|
|
50
|
+
const AppWithAuthentication = useMemo(() => {
|
|
51
|
+
return withMicroservice({
|
|
52
|
+
baseURL: '/api',
|
|
53
|
+
authURL: '/api/auth',
|
|
54
|
+
component: UnwrappedApp,
|
|
55
|
+
});
|
|
56
|
+
}, []);
|
|
57
|
+
|
|
58
|
+
return (
|
|
59
|
+
<Provide singletons={[ ... ]}>
|
|
60
|
+
<AppWithAuthentication />
|
|
61
|
+
</Provide>
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Parameters
|
|
67
|
+
|
|
68
|
+
`withMicroservice` accepts an object with the following properties:
|
|
69
|
+
|
|
70
|
+
| Name | Type | Description |
|
|
71
|
+
| :---------------- | :---------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
72
|
+
| `authAdapter` | <code>string | Function</code> | (optional) authentication scheme ([see below](#auth-adapter)). Defaults to **Bearer** authentication. |
|
|
73
|
+
| `authURL` | `string` | (optional) authentication endpoint for protected resources. Value is only optional for **Token** authentication. It is required for **Bearer** authentication. |
|
|
74
|
+
| `baseURL` | `string` | base URL of protected resources |
|
|
75
|
+
| `component` | `React.ComponentType` | component to wrap |
|
|
76
|
+
| `extraHeaders` | `Record<string, any>` | (optional) extra headers to add to requests |
|
|
77
|
+
| `preAuthenticate` | `boolean` | (optional) whether to authenticate immediately or wait for the first request. Defaults to `false`. |
|
|
78
|
+
| `tokens` | `Symbol[]` | (optional) symbols to use to provide `baseURL` to autogenerated API services and child components |
|
|
79
|
+
|
|
80
|
+
#### authAdapter{#auth-adapter}
|
|
81
|
+
|
|
82
|
+
Use `authAdapter` to select the authentication scheme. One of,
|
|
83
|
+
|
|
84
|
+
| Value | Description |
|
|
85
|
+
| :----------- | :------------------------------------------------------------------------------------------------------------------ |
|
|
86
|
+
| "bearer" | **Bearer** authentication sends the bearer token returned by `authURL` in the **Authorization** header of requests. |
|
|
87
|
+
| "token" | **Token** authentication uses the Token Server's "silent login" protocol to add secure cookies to requests. |
|
|
88
|
+
| `<Function>` | Custom authentication uses the adapter returned by `Function` to decorate requests. |
|
|
89
|
+
|
|
90
|
+
#### authUrl
|
|
91
|
+
|
|
92
|
+
The authentication endpoint for protected resources.
|
|
93
|
+
|
|
94
|
+
- For **Bearer** authentication, this is the endpoint that returns the bearer token to send in **Authorization** headers.
|
|
95
|
+
|
|
96
|
+
- For **Token server** authentication, this the "silent login" endpoint. Defaults to `${baseURL}/bff/silent-login`.
|
|
97
|
+
|
|
98
|
+
#### preAuthenticate
|
|
99
|
+
|
|
100
|
+
Controls when authentication happens:
|
|
101
|
+
|
|
102
|
+
| Value | Description |
|
|
103
|
+
| :------ | :----------------------------------------------------------------------------- |
|
|
104
|
+
| `false` | Wait for the first request to a protected resource. This is the default value. |
|
|
105
|
+
| `true` | Authenticate immediately after the component is loaded |
|
|
106
|
+
|
|
107
|
+
#### tokens
|
|
108
|
+
|
|
109
|
+
The main purpose of `tokens` is to [provide](./react-ioc#provide) the `baseURL` to autogenerated API services.
|
|
110
|
+
For example,
|
|
111
|
+
|
|
112
|
+
```tsx
|
|
113
|
+
import { BASE_URL_TOKEN_ArcGisApi, BASE_URL_TOKEN_AudiencesApi } from '@servicetitan/marketing-audience-api';
|
|
114
|
+
|
|
115
|
+
const MarketingWithMicroservice = withMicroservice({
|
|
116
|
+
component: Marketing,
|
|
117
|
+
tokens: [BASE_URL_TOKEN_ArcGisApi, BASE_URL_TOKEN_AudiencesApi],
|
|
118
|
+
...
|
|
119
|
+
});
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
You can also use `tokens` to pass the `baseURL` value to any child component. E.g.,
|
|
123
|
+
|
|
124
|
+
```tsx
|
|
125
|
+
const MY_BASE_URL_TOKEN = symbolToken<string>('MY_BASE_URL_TOKEN');
|
|
126
|
+
|
|
127
|
+
withMicroservice({ tokens: [MY_BASE_URL_TOKEN], ... });
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
And in the child component,
|
|
131
|
+
|
|
132
|
+
```tsx
|
|
133
|
+
import { symbolToken, useDependencies } from '@servicetitan/react-ioc';
|
|
134
|
+
|
|
135
|
+
const MY_BASE_URL_TOKEN = symbolToken<string>('MY_BASE_URL_TOKEN');
|
|
136
|
+
|
|
137
|
+
const Component = () => {
|
|
138
|
+
const [baseURL] = useDependencies(MY_BASE_URL_TOKEN);
|
|
139
|
+
|
|
140
|
+
...
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Authorization
|
|
145
|
+
|
|
146
|
+
`withMicroservice` currently only assists with performing authentication for requests of protected resources. There are currently no plans to implement authorization until we see concrete Token Server usage examples by product teams. If you find that having any sort of implementation for authorization would be helpful, especially in regards to Token Server implementation, please bring any ideas or discussions to the Frontend Platform team's attention so that we can create a platform solution.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@servicetitan/docs-uikit",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "25.0.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -16,5 +16,5 @@
|
|
|
16
16
|
"cli": {
|
|
17
17
|
"webpack": false
|
|
18
18
|
},
|
|
19
|
-
"gitHead": "
|
|
19
|
+
"gitHead": "db04847b5e4d8f0e679ce6c6f561aab10ade927d"
|
|
20
20
|
}
|