@hostlink/light 3.3.2 → 3.4.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/README.md +32 -1
- package/dist/createClient.d.ts +7 -0
- package/dist/light.js +464 -405
- package/dist/light.umd.cjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -44,6 +44,38 @@ const users = await api.query({
|
|
|
44
44
|
await api.auth.logout();
|
|
45
45
|
```
|
|
46
46
|
|
|
47
|
+
### Audience tokens for multiple APIs
|
|
48
|
+
|
|
49
|
+
Create one client per API and connect resource clients to the Auth client:
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
const auth = createClient('https://auth.example.com/graphql');
|
|
53
|
+
const business = createClient('https://business.example.com/graphql');
|
|
54
|
+
const infra = createClient('https://infra.example.com/graphql');
|
|
55
|
+
|
|
56
|
+
business.useAudience(auth, 'business-api');
|
|
57
|
+
infra.useAudience(auth, 'infra-api');
|
|
58
|
+
|
|
59
|
+
await auth.auth.login('username', 'password');
|
|
60
|
+
|
|
61
|
+
// Each first request lazily obtains and attaches its audience token.
|
|
62
|
+
await business.query({ orders: { order_id: true } });
|
|
63
|
+
await infra.query({ servers: { server_id: true } });
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Audience tokens are cached independently and renewed shortly before expiry.
|
|
67
|
+
Concurrent requests share the same token request. Login and logout through
|
|
68
|
+
`auth.auth` clear cached audience tokens.
|
|
69
|
+
|
|
70
|
+
Lower-level token methods are also available:
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
business.setAccessToken(token);
|
|
74
|
+
business.getAccessToken();
|
|
75
|
+
business.clearAccessToken();
|
|
76
|
+
business.setAccessTokenProvider(async () => obtainToken());
|
|
77
|
+
```
|
|
78
|
+
|
|
47
79
|
## API Reference
|
|
48
80
|
|
|
49
81
|
### Creating a Client
|
|
@@ -446,4 +478,3 @@ MIT
|
|
|
446
478
|
|
|
447
479
|
|
|
448
480
|
|
|
449
|
-
|
package/dist/createClient.d.ts
CHANGED
|
@@ -10,10 +10,17 @@ import { createRoles } from './role';
|
|
|
10
10
|
import { default as createCollection } from './createCollection';
|
|
11
11
|
import { default as createList } from './createList';
|
|
12
12
|
import { createUsers } from './users';
|
|
13
|
+
export type AccessTokenProvider = () => Promise<string>;
|
|
13
14
|
type ClientType = {
|
|
14
15
|
post: typeof axios.post;
|
|
15
16
|
baseURL: string;
|
|
16
17
|
axios: ReturnType<typeof axios.create>;
|
|
18
|
+
setAccessToken: (token: string | null) => void;
|
|
19
|
+
getAccessToken: () => string | null;
|
|
20
|
+
clearAccessToken: () => void;
|
|
21
|
+
setAccessTokenProvider: (provider: AccessTokenProvider | null) => void;
|
|
22
|
+
onAuthChange: (listener: () => void) => () => void;
|
|
23
|
+
useAudience: (authClient: ClientType, audience: string) => void;
|
|
17
24
|
auth: ReturnType<typeof createAuth>;
|
|
18
25
|
mutation: ReturnType<typeof createMutation>;
|
|
19
26
|
query: ReturnType<typeof createQuery>;
|