@seliseblocks/client 0.1.5 → 0.1.6
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/AI_USAGE_GUIDE.md +165 -165
- package/LICENSE +21 -21
- package/README.md +222 -222
- package/dist/client.d.ts +8 -0
- package/dist/client.js +1 -0
- package/dist/data/data-client.js +33 -33
- package/dist/http/http-client.js +30 -18
- package/package.json +48 -48
package/AI_USAGE_GUIDE.md
CHANGED
|
@@ -1,165 +1,165 @@
|
|
|
1
|
-
# AI Usage Guide
|
|
2
|
-
|
|
3
|
-
Use this file only for `@seliseblocks/client` app-code work. For full method details, read the source docstrings.
|
|
4
|
-
|
|
5
|
-
Source refs:
|
|
6
|
-
|
|
7
|
-
- Client setup: `src/client.ts`
|
|
8
|
-
- Auth: `src/auth/auth-client.ts`
|
|
9
|
-
- IAM: `src/iam/iam-client.ts`
|
|
10
|
-
- Data: `src/data/data-client.ts`
|
|
11
|
-
- Localization: `src/localization/localization-client.ts`
|
|
12
|
-
- MFA: `src/mfa/mfa-client.ts`
|
|
13
|
-
- HTTP/errors: `src/http/http-client.ts`, `src/http/errors.ts`
|
|
14
|
-
|
|
15
|
-
## Non-Negotiable Rules
|
|
16
|
-
|
|
17
|
-
- Configure `xBlocksKey`; SDK sends it as `x-blocks-key`.
|
|
18
|
-
- Do not send `ProjectKey` or `projectKey`.
|
|
19
|
-
- Do not add `/api` after `/v4`.
|
|
20
|
-
- Valid route shapes: `/iam/v4/...`, `/data/v4/...`, `/localization/v4/...`.
|
|
21
|
-
- SDK does not store tokens. App owns access token, refresh token, storage, and logout cleanup.
|
|
22
|
-
- Frontend code must not contain client secrets.
|
|
23
|
-
- Do not add impersonation, release deploy, schema/rules mutation, or localization authoring/admin APIs.
|
|
24
|
-
|
|
25
|
-
## Create Client
|
|
26
|
-
|
|
27
|
-
```ts
|
|
28
|
-
import { createBlocksClient } from "@seliseblocks/client";
|
|
29
|
-
|
|
30
|
-
export const blocks = createBlocksClient({
|
|
31
|
-
apiUrl: "https://api.seliseblocks.com",
|
|
32
|
-
xBlocksKey: env.BLOCKS_KEY,
|
|
33
|
-
accessToken: () => session.accessToken,
|
|
34
|
-
oidc: {
|
|
35
|
-
url: "https://iam.seliseblocks.com",
|
|
36
|
-
clientId: env.OIDC_CLIENT_ID,
|
|
37
|
-
redirectUri: env.OIDC_REDIRECT_URI ?? `${window.location.origin}/login/callback`
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
Use `accessToken` only for the current token resolver. Do not add SDK token storage.
|
|
43
|
-
Use a configured `redirectUri` when available; the current-origin `/login/callback` URL is only a browser fallback.
|
|
44
|
-
|
|
45
|
-
## Auth Decision
|
|
46
|
-
|
|
47
|
-
There are two auth flows.
|
|
48
|
-
|
|
49
|
-
OIDC/hosted IAM flow:
|
|
50
|
-
|
|
51
|
-
- Login: `blocks.auth.idp.initiate()`, `blocks.auth.idp.redirectToProvider()`, `blocks.auth.idp.callback(...)`
|
|
52
|
-
- Refresh: `blocks.auth.oidc.refreshToken({ refreshToken })`
|
|
53
|
-
- Refresh route: `POST /iam/v4/oidc/token`
|
|
54
|
-
- Must send `client_id`; SDK uses `request.clientId` or configured `oidc.clientId`.
|
|
55
|
-
- Do not manually build `oidc/authorize`.
|
|
56
|
-
|
|
57
|
-
Embedded/app-owned flow:
|
|
58
|
-
|
|
59
|
-
- Login: `blocks.auth.login(...)`
|
|
60
|
-
- Social: `blocks.auth.socialInitiate(clientId, redirectUri)` (two positional strings), `blocks.auth.socialCallback(request)` (object)
|
|
61
|
-
- Refresh: `blocks.auth.refresh({ refreshToken })`
|
|
62
|
-
- Routes: `/iam/v4/auth/login`, `/iam/v4/auth/social/*`, `/iam/v4/auth/refresh`
|
|
63
|
-
|
|
64
|
-
Shared auth methods for both flows:
|
|
65
|
-
|
|
66
|
-
- `userInfo()`
|
|
67
|
-
- `switchOrganization(...)`
|
|
68
|
-
- `logout(...)`, `logoutAll(...)`
|
|
69
|
-
- `signup(...)`, `recover(...)`, `resetPassword(...)`
|
|
70
|
-
- `changePassword(...)`
|
|
71
|
-
- `activate(...)`, `resendActivation(...)`, `validateActivation(...)`
|
|
72
|
-
- `identityProviders.*`, `config.*`, `userCodes.*`, `clientCredentials.*` when app has permission
|
|
73
|
-
- `accessToken()` - resolves the caller-owned bearer token configured on `createBlocksClient`.
|
|
74
|
-
- `isAuthenticated()` - checks the session against IAM via `GET /iam/v4/auth/me`, returns a boolean from the raw response status. Prefer this over `userInfo()` for a true/false check, since a hosted-IdP session may live in a cookie with no local `accessToken` to inspect.
|
|
75
|
-
|
|
76
|
-
## Service Map
|
|
77
|
-
|
|
78
|
-
IAM:
|
|
79
|
-
|
|
80
|
-
- `blocks.iam.me()`
|
|
81
|
-
- `blocks.iam.users.*`
|
|
82
|
-
- `blocks.iam.roles.*`
|
|
83
|
-
- `blocks.iam.permissions.*`
|
|
84
|
-
- `blocks.iam.resources.*`
|
|
85
|
-
- `blocks.iam.organizations.*`
|
|
86
|
-
- `blocks.iam.signupSettings.get()` / `.save(...)`
|
|
87
|
-
|
|
88
|
-
MFA:
|
|
89
|
-
|
|
90
|
-
- `blocks.mfa.config()` / `.saveConfig(...)` - tenant MFA policy
|
|
91
|
-
- `blocks.mfa.totp.setup()` / `.verifySetup({ code })` - TOTP enrollment
|
|
92
|
-
- `blocks.mfa.generate(...)` / `.resend(...)` / `.verify(...)` - OTP challenge flow
|
|
93
|
-
- `blocks.mfa.setMethod({ mfaType })`, `.disable()`
|
|
94
|
-
- `blocks.mfa.backupCodes.list()` / `.generate()` / `.use({ code, userId })`
|
|
95
|
-
|
|
96
|
-
Data:
|
|
97
|
-
|
|
98
|
-
- `blocks.data.schemas.*` for schema reads only
|
|
99
|
-
- `blocks.data.validations.*` for validation reads
|
|
100
|
-
- `blocks.data.collection("Name").list/get/create/update/delete`
|
|
101
|
-
- `blocks.data.graphql(...)`
|
|
102
|
-
- `blocks.data.files.*` for file/storage helpers
|
|
103
|
-
- `blocks.data.directories.*` for directory create/get/update/delete/move
|
|
104
|
-
- `blocks.data.objects.*` for browse/search/trash/shared/restore/share/access/inheritance
|
|
105
|
-
- `blocks.data.utilities.*` for runtime utility helpers
|
|
106
|
-
|
|
107
|
-
Localization:
|
|
108
|
-
|
|
109
|
-
- `blocks.localization.languages()`
|
|
110
|
-
- `blocks.localization.modules()`
|
|
111
|
-
- `blocks.localization.languagesForCurrentTenant()`
|
|
112
|
-
- `blocks.localization.modulesForCurrentTenant()`
|
|
113
|
-
- `blocks.localization.translations(moduleName, language)`
|
|
114
|
-
- `blocks.localization.load(language, modules)`
|
|
115
|
-
- `blocks.localization.cloudTranslations(moduleName, language)`
|
|
116
|
-
- `blocks.localization.loadCloud(language, modules)`
|
|
117
|
-
- `blocks.localization.keysByNames(...)`
|
|
118
|
-
- `blocks.localization.t(key, fallback, options)`
|
|
119
|
-
|
|
120
|
-
HTTP fallback:
|
|
121
|
-
|
|
122
|
-
- Use `blocks.http.request("/service/v4/...")` only when no typed method exists.
|
|
123
|
-
- Use `blocks.http.external(url, ...)` only for external URLs such as pre-signed uploads.
|
|
124
|
-
|
|
125
|
-
Errors:
|
|
126
|
-
|
|
127
|
-
- Every non-2xx response from `http.request`/`http.external` throws `BlocksApiError` (`status`, `statusText`, `body`). Catch it with `instanceof BlocksApiError` rather than assuming a generic `Error` shape.
|
|
128
|
-
|
|
129
|
-
## Minimal Examples
|
|
130
|
-
|
|
131
|
-
```ts
|
|
132
|
-
const me = await blocks.iam.me();
|
|
133
|
-
const schemas = await blocks.data.schemas.list({ pageNo: 1, pageSize: 20 });
|
|
134
|
-
const rows = await blocks.data.collection("Students").list({ pageNo: 1, pageSize: 20 });
|
|
135
|
-
await blocks.localization.load("en-US", ["common"]);
|
|
136
|
-
const label = blocks.localization.t("save", "Save");
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
OIDC refresh with explicit client id:
|
|
140
|
-
|
|
141
|
-
```ts
|
|
142
|
-
await blocks.auth.oidc.refreshToken({
|
|
143
|
-
clientId: env.OIDC_CLIENT_ID,
|
|
144
|
-
refreshToken: session.refreshToken
|
|
145
|
-
});
|
|
146
|
-
```
|
|
147
|
-
|
|
148
|
-
Embedded refresh:
|
|
149
|
-
|
|
150
|
-
```ts
|
|
151
|
-
await blocks.auth.refresh({
|
|
152
|
-
refreshToken: session.refreshToken
|
|
153
|
-
});
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
## Check Before Finishing
|
|
157
|
-
|
|
158
|
-
```bash
|
|
159
|
-
npm test
|
|
160
|
-
npm run lint
|
|
161
|
-
npm pack --dry-run
|
|
162
|
-
rg -n "v4[/\\]api|iam[/\\]v4[/\\]api|data[/\\]v4[/\\]api|localization[/\\]v4[/\\]api" src README.md AI_USAGE_GUIDE.md test
|
|
163
|
-
```
|
|
164
|
-
|
|
165
|
-
Only negative route-guard tests should match.
|
|
1
|
+
# AI Usage Guide
|
|
2
|
+
|
|
3
|
+
Use this file only for `@seliseblocks/client` app-code work. For full method details, read the source docstrings.
|
|
4
|
+
|
|
5
|
+
Source refs:
|
|
6
|
+
|
|
7
|
+
- Client setup: `src/client.ts`
|
|
8
|
+
- Auth: `src/auth/auth-client.ts`
|
|
9
|
+
- IAM: `src/iam/iam-client.ts`
|
|
10
|
+
- Data: `src/data/data-client.ts`
|
|
11
|
+
- Localization: `src/localization/localization-client.ts`
|
|
12
|
+
- MFA: `src/mfa/mfa-client.ts`
|
|
13
|
+
- HTTP/errors: `src/http/http-client.ts`, `src/http/errors.ts`
|
|
14
|
+
|
|
15
|
+
## Non-Negotiable Rules
|
|
16
|
+
|
|
17
|
+
- Configure `xBlocksKey`; SDK sends it as `x-blocks-key`.
|
|
18
|
+
- Do not send `ProjectKey` or `projectKey`.
|
|
19
|
+
- Do not add `/api` after `/v4`.
|
|
20
|
+
- Valid route shapes: `/iam/v4/...`, `/data/v4/...`, `/localization/v4/...`.
|
|
21
|
+
- SDK does not store tokens. App owns access token, refresh token, storage, and logout cleanup.
|
|
22
|
+
- Frontend code must not contain client secrets.
|
|
23
|
+
- Do not add impersonation, release deploy, schema/rules mutation, or localization authoring/admin APIs.
|
|
24
|
+
|
|
25
|
+
## Create Client
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { createBlocksClient } from "@seliseblocks/client";
|
|
29
|
+
|
|
30
|
+
export const blocks = createBlocksClient({
|
|
31
|
+
apiUrl: "https://api.seliseblocks.com",
|
|
32
|
+
xBlocksKey: env.BLOCKS_KEY,
|
|
33
|
+
accessToken: () => session.accessToken,
|
|
34
|
+
oidc: {
|
|
35
|
+
url: "https://iam.seliseblocks.com",
|
|
36
|
+
clientId: env.OIDC_CLIENT_ID,
|
|
37
|
+
redirectUri: env.OIDC_REDIRECT_URI ?? `${window.location.origin}/login/callback`
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Use `accessToken` only for the current token resolver. Do not add SDK token storage.
|
|
43
|
+
Use a configured `redirectUri` when available; the current-origin `/login/callback` URL is only a browser fallback.
|
|
44
|
+
|
|
45
|
+
## Auth Decision
|
|
46
|
+
|
|
47
|
+
There are two auth flows.
|
|
48
|
+
|
|
49
|
+
OIDC/hosted IAM flow:
|
|
50
|
+
|
|
51
|
+
- Login: `blocks.auth.idp.initiate()`, `blocks.auth.idp.redirectToProvider()`, `blocks.auth.idp.callback(...)`
|
|
52
|
+
- Refresh: `blocks.auth.oidc.refreshToken({ refreshToken })`
|
|
53
|
+
- Refresh route: `POST /iam/v4/oidc/token`
|
|
54
|
+
- Must send `client_id`; SDK uses `request.clientId` or configured `oidc.clientId`.
|
|
55
|
+
- Do not manually build `oidc/authorize`.
|
|
56
|
+
|
|
57
|
+
Embedded/app-owned flow:
|
|
58
|
+
|
|
59
|
+
- Login: `blocks.auth.login(...)`
|
|
60
|
+
- Social: `blocks.auth.socialInitiate(clientId, redirectUri)` (two positional strings), `blocks.auth.socialCallback(request)` (object)
|
|
61
|
+
- Refresh: `blocks.auth.refresh({ refreshToken })`
|
|
62
|
+
- Routes: `/iam/v4/auth/login`, `/iam/v4/auth/social/*`, `/iam/v4/auth/refresh`
|
|
63
|
+
|
|
64
|
+
Shared auth methods for both flows:
|
|
65
|
+
|
|
66
|
+
- `userInfo()`
|
|
67
|
+
- `switchOrganization(...)`
|
|
68
|
+
- `logout(...)`, `logoutAll(...)`
|
|
69
|
+
- `signup(...)`, `recover(...)`, `resetPassword(...)`
|
|
70
|
+
- `changePassword(...)`
|
|
71
|
+
- `activate(...)`, `resendActivation(...)`, `validateActivation(...)`
|
|
72
|
+
- `identityProviders.*`, `config.*`, `userCodes.*`, `clientCredentials.*` when app has permission
|
|
73
|
+
- `accessToken()` - resolves the caller-owned bearer token configured on `createBlocksClient`.
|
|
74
|
+
- `isAuthenticated()` - checks the session against IAM via `GET /iam/v4/auth/me`, returns a boolean from the raw response status. Prefer this over `userInfo()` for a true/false check, since a hosted-IdP session may live in a cookie with no local `accessToken` to inspect.
|
|
75
|
+
|
|
76
|
+
## Service Map
|
|
77
|
+
|
|
78
|
+
IAM:
|
|
79
|
+
|
|
80
|
+
- `blocks.iam.me()`
|
|
81
|
+
- `blocks.iam.users.*`
|
|
82
|
+
- `blocks.iam.roles.*`
|
|
83
|
+
- `blocks.iam.permissions.*`
|
|
84
|
+
- `blocks.iam.resources.*`
|
|
85
|
+
- `blocks.iam.organizations.*`
|
|
86
|
+
- `blocks.iam.signupSettings.get()` / `.save(...)`
|
|
87
|
+
|
|
88
|
+
MFA:
|
|
89
|
+
|
|
90
|
+
- `blocks.mfa.config()` / `.saveConfig(...)` - tenant MFA policy
|
|
91
|
+
- `blocks.mfa.totp.setup()` / `.verifySetup({ code })` - TOTP enrollment
|
|
92
|
+
- `blocks.mfa.generate(...)` / `.resend(...)` / `.verify(...)` - OTP challenge flow
|
|
93
|
+
- `blocks.mfa.setMethod({ mfaType })`, `.disable()`
|
|
94
|
+
- `blocks.mfa.backupCodes.list()` / `.generate()` / `.use({ code, userId })`
|
|
95
|
+
|
|
96
|
+
Data:
|
|
97
|
+
|
|
98
|
+
- `blocks.data.schemas.*` for schema reads only
|
|
99
|
+
- `blocks.data.validations.*` for validation reads
|
|
100
|
+
- `blocks.data.collection("Name").list/get/create/update/delete`
|
|
101
|
+
- `blocks.data.graphql(...)`
|
|
102
|
+
- `blocks.data.files.*` for file/storage helpers
|
|
103
|
+
- `blocks.data.directories.*` for directory create/get/update/delete/move
|
|
104
|
+
- `blocks.data.objects.*` for browse/search/trash/shared/restore/share/access/inheritance
|
|
105
|
+
- `blocks.data.utilities.*` for runtime utility helpers
|
|
106
|
+
|
|
107
|
+
Localization:
|
|
108
|
+
|
|
109
|
+
- `blocks.localization.languages()`
|
|
110
|
+
- `blocks.localization.modules()`
|
|
111
|
+
- `blocks.localization.languagesForCurrentTenant()`
|
|
112
|
+
- `blocks.localization.modulesForCurrentTenant()`
|
|
113
|
+
- `blocks.localization.translations(moduleName, language)`
|
|
114
|
+
- `blocks.localization.load(language, modules)`
|
|
115
|
+
- `blocks.localization.cloudTranslations(moduleName, language)`
|
|
116
|
+
- `blocks.localization.loadCloud(language, modules)`
|
|
117
|
+
- `blocks.localization.keysByNames(...)`
|
|
118
|
+
- `blocks.localization.t(key, fallback, options)`
|
|
119
|
+
|
|
120
|
+
HTTP fallback:
|
|
121
|
+
|
|
122
|
+
- Use `blocks.http.request("/service/v4/...")` only when no typed method exists.
|
|
123
|
+
- Use `blocks.http.external(url, ...)` only for external URLs such as pre-signed uploads.
|
|
124
|
+
|
|
125
|
+
Errors:
|
|
126
|
+
|
|
127
|
+
- Every non-2xx response from `http.request`/`http.external` throws `BlocksApiError` (`status`, `statusText`, `body`). Catch it with `instanceof BlocksApiError` rather than assuming a generic `Error` shape.
|
|
128
|
+
|
|
129
|
+
## Minimal Examples
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
const me = await blocks.iam.me();
|
|
133
|
+
const schemas = await blocks.data.schemas.list({ pageNo: 1, pageSize: 20 });
|
|
134
|
+
const rows = await blocks.data.collection("Students").list({ pageNo: 1, pageSize: 20 });
|
|
135
|
+
await blocks.localization.load("en-US", ["common"]);
|
|
136
|
+
const label = blocks.localization.t("save", "Save");
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
OIDC refresh with explicit client id:
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
await blocks.auth.oidc.refreshToken({
|
|
143
|
+
clientId: env.OIDC_CLIENT_ID,
|
|
144
|
+
refreshToken: session.refreshToken
|
|
145
|
+
});
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Embedded refresh:
|
|
149
|
+
|
|
150
|
+
```ts
|
|
151
|
+
await blocks.auth.refresh({
|
|
152
|
+
refreshToken: session.refreshToken
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## Check Before Finishing
|
|
157
|
+
|
|
158
|
+
```bash
|
|
159
|
+
npm test
|
|
160
|
+
npm run lint
|
|
161
|
+
npm pack --dry-run
|
|
162
|
+
rg -n "v4[/\\]api|iam[/\\]v4[/\\]api|data[/\\]v4[/\\]api|localization[/\\]v4[/\\]api" src README.md AI_USAGE_GUIDE.md test
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
Only negative route-guard tests should match.
|
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) SELISE Blocks
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) SELISE Blocks
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,222 +1,222 @@
|
|
|
1
|
-
# Blocks Client
|
|
2
|
-
|
|
3
|
-
Framework-neutral TypeScript SDK for SELISE Blocks frontend and app-runtime code.
|
|
4
|
-
|
|
5
|
-
`@seliseblocks/client` gives consumer apps a small, typed wrapper around the Blocks APIs they normally call at runtime: Auth/IAM, Data, Localization, and shared HTTP configuration. Detailed `what / why / how` descriptions are kept on the SDK functions themselves so editors and generated typings show the guidance where developers use the API.
|
|
6
|
-
|
|
7
|
-
Admin/control-plane work belongs in `@seliseblocks/cli-os`: project setup, schema/rules deploy, release deploy, and AI/admin terminal workflows.
|
|
8
|
-
|
|
9
|
-
For AI agents and automation, see [AI_USAGE_GUIDE.md](AI_USAGE_GUIDE.md).
|
|
10
|
-
|
|
11
|
-
## Install
|
|
12
|
-
|
|
13
|
-
```bash
|
|
14
|
-
npm install @seliseblocks/client@latest
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
Requires Node.js 20+ or a browser/runtime with `fetch`.
|
|
18
|
-
|
|
19
|
-
## Create A Client
|
|
20
|
-
|
|
21
|
-
```ts
|
|
22
|
-
import { createBlocksClient } from "@seliseblocks/client";
|
|
23
|
-
|
|
24
|
-
const blocks = createBlocksClient({
|
|
25
|
-
apiUrl: "https://api.seliseblocks.com",
|
|
26
|
-
xBlocksKey: "<blocks-key>",
|
|
27
|
-
accessToken: () => currentUserSession?.accessToken,
|
|
28
|
-
oidc: {
|
|
29
|
-
url: "https://iam.seliseblocks.com",
|
|
30
|
-
clientId: "<public-browser-client-id>",
|
|
31
|
-
redirectUri: configuredRedirectUri ?? `${window.location.origin}/login/callback`
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
`xBlocksKey` is always sent as the `x-blocks-key` header. Do not send `ProjectKey` or `projectKey` in request query/body payloads.
|
|
37
|
-
|
|
38
|
-
The SDK does not store tokens. Your app owns login state, refresh scheduling, storage, and logout cleanup.
|
|
39
|
-
|
|
40
|
-
Use a configured OIDC redirect URI when your app has one. The current-origin `/login/callback` URL is only a browser fallback.
|
|
41
|
-
|
|
42
|
-
## Auth
|
|
43
|
-
|
|
44
|
-
Auth has two login flows.
|
|
45
|
-
|
|
46
|
-
OIDC/hosted IAM login uses `auth.idp`. The SDK relies on Blocks IAM `IdpController`, sends the configured public `clientId`, and does not manually build `oidc/authorize` URLs. Refresh OIDC sessions with `auth.oidc.refreshToken()`, which calls `/iam/v4/oidc/token` and sends `client_id`.
|
|
47
|
-
|
|
48
|
-
```ts
|
|
49
|
-
await blocks.auth.idp.redirectToProvider();
|
|
50
|
-
|
|
51
|
-
const result = await blocks.auth.idp.callback(window.location.href);
|
|
52
|
-
|
|
53
|
-
const refreshed = await blocks.auth.oidc.refreshToken({
|
|
54
|
-
refreshToken: result.refresh_token,
|
|
55
|
-
clientId: "<public-browser-client-id>"
|
|
56
|
-
});
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
Embedded/app-owned login UI uses AuthController methods directly on `auth`: `auth.login()`, `auth.socialInitiate(clientId, redirectUri)` (two positional strings, not an object), `auth.socialCallback(request)`, and `auth.refresh()`.
|
|
60
|
-
|
|
61
|
-
```ts
|
|
62
|
-
const login = await blocks.auth.login({
|
|
63
|
-
username: "user@example.com",
|
|
64
|
-
password: "password"
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
const refreshed = await blocks.auth.refresh({
|
|
68
|
-
refreshToken: login.refresh_token
|
|
69
|
-
});
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
Other auth APIs, such as user info, organization switch, logout, signup, recovery, activation, auth config, user codes, client credentials, and identity providers, can be used by either flow when the app has the required token/permission.
|
|
73
|
-
|
|
74
|
-
`auth.oidc.clientCredentials()` is for trusted non-browser runtimes only because it requires a client secret.
|
|
75
|
-
|
|
76
|
-
`auth.accessToken()` resolves the caller-owned bearer token configured on `createBlocksClient` (string or callback). `auth.isAuthenticated()` checks the current session against IAM through `GET /iam/v4/auth/me` and returns a boolean based on the raw response status - use it instead of `userInfo()` when you need a true/false signal, since IAM's hosted IdP flow may set the session as a cookie with no local `accessToken` to inspect.
|
|
77
|
-
|
|
78
|
-
```ts
|
|
79
|
-
const config = await blocks.mfa.config();
|
|
80
|
-
await blocks.mfa.totp.setup();
|
|
81
|
-
await blocks.mfa.totp.verifySetup({ code: "123456" });
|
|
82
|
-
await blocks.mfa.setMethod({ mfaType: 1 });
|
|
83
|
-
const backupCodes = await blocks.mfa.backupCodes.generate();
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
## Mail And Notifications
|
|
87
|
-
|
|
88
|
-
```ts
|
|
89
|
-
await blocks.mail.send({
|
|
90
|
-
to: ["user@example.com"],
|
|
91
|
-
purpose: "welcome",
|
|
92
|
-
language: "en",
|
|
93
|
-
subjectDataContext: { firstName: "Ada" }
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
await blocks.mail.sendToAny({
|
|
97
|
-
to: ["qa@example.com"],
|
|
98
|
-
purpose: "welcome",
|
|
99
|
-
language: "en",
|
|
100
|
-
isTestMail: true
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
await blocks.notifier.notify({
|
|
104
|
-
userIds: ["user-id"],
|
|
105
|
-
denormalizedPayload: JSON.stringify({ orderId: "123" }),
|
|
106
|
-
saveDenormalizedPayloadAsAnObject: true
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
const inbox = await blocks.notifier.getNotifications({ isUnreadOnly: true, page: 1, pageSize: 20 });
|
|
110
|
-
await blocks.notifier.markNotificationAsRead({ id: inbox.notifications[0].id });
|
|
111
|
-
await blocks.notifier.markAllNotificationAsRead();
|
|
112
|
-
```
|
|
113
|
-
|
|
114
|
-
`mail.send`/`mail.sendToAny` call `/logic/v4/Mail/Send`/`SendToAny`. `notifier.*` calls `/logic/v4/Notifier/*`
|
|
115
|
-
for sending/reading notifications; tenant notification *channel configuration* (`/os/v4/Notification/*`)
|
|
116
|
-
is an admin concern handled by `@seliseblocks/cli-os`'s `notification *` commands, not this SDK.
|
|
117
|
-
|
|
118
|
-
## Common Runtime Calls
|
|
119
|
-
|
|
120
|
-
```ts
|
|
121
|
-
const me = await blocks.iam.me();
|
|
122
|
-
const orgs = await blocks.iam.organizations.my();
|
|
123
|
-
const features = await blocks.iam.resources.features();
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
`iam.organizations.my()` returns the IAM envelope with `organizations`, not `data`.
|
|
127
|
-
|
|
128
|
-
```ts
|
|
129
|
-
const schemas = await blocks.data.schemas.list();
|
|
130
|
-
const schemaInfo = await blocks.data.schemas.infoByName("Student");
|
|
131
|
-
const schemaRules = await blocks.data.validations.bySchemaId("schema-id");
|
|
132
|
-
|
|
133
|
-
const students = blocks.data.collection("Students");
|
|
134
|
-
const rows = await students.list({ pageNo: 1, pageSize: 20 });
|
|
135
|
-
await students.create({ firstName: "Test" });
|
|
136
|
-
|
|
137
|
-
const graph = await blocks.data.graphql({
|
|
138
|
-
query: "{ students { items { itemId firstName } } }"
|
|
139
|
-
});
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
```ts
|
|
143
|
-
const upload = await blocks.data.files.presignedUploadUrl({
|
|
144
|
-
name: "avatar.png",
|
|
145
|
-
configurationName: "default",
|
|
146
|
-
parentDirectoryId: "root",
|
|
147
|
-
accessModifier: "Private"
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
await blocks.data.files.uploadToUrl({
|
|
151
|
-
url: upload.uploadUrl,
|
|
152
|
-
body: file,
|
|
153
|
-
contentType: file.type
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
const items = await blocks.data.objects.list({
|
|
157
|
-
parentDirectoryId: "root",
|
|
158
|
-
limit: 20
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
const directory = await blocks.data.directories.create({
|
|
162
|
-
name: "Contracts",
|
|
163
|
-
parentDirectoryId: "root",
|
|
164
|
-
allowedFileExtensions: ["pdf", "docx"]
|
|
165
|
-
});
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
```ts
|
|
169
|
-
await blocks.localization.load("en-US", ["common", "dashboard"]);
|
|
170
|
-
const label = blocks.localization.t("dashboard.title", "Dashboard");
|
|
171
|
-
|
|
172
|
-
const tenantLanguages = await blocks.localization.languagesForCurrentTenant();
|
|
173
|
-
const selectedKeys = await blocks.localization.keysByNames({
|
|
174
|
-
keyNames: ["dashboard.title"]
|
|
175
|
-
});
|
|
176
|
-
```
|
|
177
|
-
|
|
178
|
-
## API Surface
|
|
179
|
-
|
|
180
|
-
- `blocks.auth`: AuthController login, social login, refresh, org switch, logout, signup, recovery, activation, auth config, user codes, client credentials, identity providers.
|
|
181
|
-
- `blocks.auth.idp`: hosted IdP initiate, browser redirect, callback, UI config.
|
|
182
|
-
- `blocks.auth.oidc`: refresh-token and client-credentials token endpoint helpers.
|
|
183
|
-
- `blocks.iam`: current user, users, roles, permissions, resources, organizations, signup settings (`blocks.iam.signupSettings`).
|
|
184
|
-
- `blocks.data`: schema reads, validation reads, GraphQL gateway execution, file/directory/object-tree storage helpers, and runtime collection CRUD.
|
|
185
|
-
- `blocks.localization`: tenant language/module discovery, UILM dictionary loading, selected key lookup, simple `t()` lookup.
|
|
186
|
-
- `blocks.mfa`: tenant MFA policy read/save, TOTP enrollment, OTP generate/resend/verify, method switch, disable, and backup codes.
|
|
187
|
-
- `blocks.mail`: `send`/`sendToAny` transactional email through the tenant's configured mail provider.
|
|
188
|
-
- `blocks.notifier`: `notify`, `getNotifications`, `getUnreadNotificationsBySubscriptionFilter`, `markNotificationAsRead`, `markAllNotificationAsRead`.
|
|
189
|
-
|
|
190
|
-
Professional class names are exported for advanced typing and adapters: `BlocksAuthenticationClient`, `BlocksIAMClient`, `BlocksDataClient`, `BlocksLocalizationClient`, `BlocksMfaClient`, `BlocksMailClient`, and `BlocksNotifierClient`. `BlocksApiError` is exported for typed error handling - every non-2xx response from `http.request`/`http.external` throws it, exposing `status`, `statusText`, and the parsed `body`:
|
|
191
|
-
|
|
192
|
-
```ts
|
|
193
|
-
try {
|
|
194
|
-
await blocks.iam.me();
|
|
195
|
-
} catch (error) {
|
|
196
|
-
if (error instanceof BlocksApiError) {
|
|
197
|
-
console.error(error.status, error.body);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
```
|
|
201
|
-
|
|
202
|
-
## Boundaries
|
|
203
|
-
|
|
204
|
-
This package must not contain:
|
|
205
|
-
|
|
206
|
-
- CLI client secrets
|
|
207
|
-
- impersonation APIs
|
|
208
|
-
- project create/list/use
|
|
209
|
-
- schema/rules deploy
|
|
210
|
-
- release deploy
|
|
211
|
-
- token/session/PKCE storage
|
|
212
|
-
- Node filesystem or OS credential storage
|
|
213
|
-
- framework UI components
|
|
214
|
-
- Localization authoring/admin endpoints
|
|
215
|
-
- Data schema/rules mutation endpoints
|
|
216
|
-
|
|
217
|
-
## Package Checks
|
|
218
|
-
|
|
219
|
-
```bash
|
|
220
|
-
npm test
|
|
221
|
-
npm pack --dry-run
|
|
222
|
-
```
|
|
1
|
+
# Blocks Client
|
|
2
|
+
|
|
3
|
+
Framework-neutral TypeScript SDK for SELISE Blocks frontend and app-runtime code.
|
|
4
|
+
|
|
5
|
+
`@seliseblocks/client` gives consumer apps a small, typed wrapper around the Blocks APIs they normally call at runtime: Auth/IAM, Data, Localization, and shared HTTP configuration. Detailed `what / why / how` descriptions are kept on the SDK functions themselves so editors and generated typings show the guidance where developers use the API.
|
|
6
|
+
|
|
7
|
+
Admin/control-plane work belongs in `@seliseblocks/cli-os`: project setup, schema/rules deploy, release deploy, and AI/admin terminal workflows.
|
|
8
|
+
|
|
9
|
+
For AI agents and automation, see [AI_USAGE_GUIDE.md](AI_USAGE_GUIDE.md).
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @seliseblocks/client@latest
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Requires Node.js 20+ or a browser/runtime with `fetch`.
|
|
18
|
+
|
|
19
|
+
## Create A Client
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { createBlocksClient } from "@seliseblocks/client";
|
|
23
|
+
|
|
24
|
+
const blocks = createBlocksClient({
|
|
25
|
+
apiUrl: "https://api.seliseblocks.com",
|
|
26
|
+
xBlocksKey: "<blocks-key>",
|
|
27
|
+
accessToken: () => currentUserSession?.accessToken,
|
|
28
|
+
oidc: {
|
|
29
|
+
url: "https://iam.seliseblocks.com",
|
|
30
|
+
clientId: "<public-browser-client-id>",
|
|
31
|
+
redirectUri: configuredRedirectUri ?? `${window.location.origin}/login/callback`
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
`xBlocksKey` is always sent as the `x-blocks-key` header. Do not send `ProjectKey` or `projectKey` in request query/body payloads.
|
|
37
|
+
|
|
38
|
+
The SDK does not store tokens. Your app owns login state, refresh scheduling, storage, and logout cleanup.
|
|
39
|
+
|
|
40
|
+
Use a configured OIDC redirect URI when your app has one. The current-origin `/login/callback` URL is only a browser fallback.
|
|
41
|
+
|
|
42
|
+
## Auth
|
|
43
|
+
|
|
44
|
+
Auth has two login flows.
|
|
45
|
+
|
|
46
|
+
OIDC/hosted IAM login uses `auth.idp`. The SDK relies on Blocks IAM `IdpController`, sends the configured public `clientId`, and does not manually build `oidc/authorize` URLs. Refresh OIDC sessions with `auth.oidc.refreshToken()`, which calls `/iam/v4/oidc/token` and sends `client_id`.
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
await blocks.auth.idp.redirectToProvider();
|
|
50
|
+
|
|
51
|
+
const result = await blocks.auth.idp.callback(window.location.href);
|
|
52
|
+
|
|
53
|
+
const refreshed = await blocks.auth.oidc.refreshToken({
|
|
54
|
+
refreshToken: result.refresh_token,
|
|
55
|
+
clientId: "<public-browser-client-id>"
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Embedded/app-owned login UI uses AuthController methods directly on `auth`: `auth.login()`, `auth.socialInitiate(clientId, redirectUri)` (two positional strings, not an object), `auth.socialCallback(request)`, and `auth.refresh()`.
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
const login = await blocks.auth.login({
|
|
63
|
+
username: "user@example.com",
|
|
64
|
+
password: "password"
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const refreshed = await blocks.auth.refresh({
|
|
68
|
+
refreshToken: login.refresh_token
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Other auth APIs, such as user info, organization switch, logout, signup, recovery, activation, auth config, user codes, client credentials, and identity providers, can be used by either flow when the app has the required token/permission.
|
|
73
|
+
|
|
74
|
+
`auth.oidc.clientCredentials()` is for trusted non-browser runtimes only because it requires a client secret.
|
|
75
|
+
|
|
76
|
+
`auth.accessToken()` resolves the caller-owned bearer token configured on `createBlocksClient` (string or callback). `auth.isAuthenticated()` checks the current session against IAM through `GET /iam/v4/auth/me` and returns a boolean based on the raw response status - use it instead of `userInfo()` when you need a true/false signal, since IAM's hosted IdP flow may set the session as a cookie with no local `accessToken` to inspect.
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
const config = await blocks.mfa.config();
|
|
80
|
+
await blocks.mfa.totp.setup();
|
|
81
|
+
await blocks.mfa.totp.verifySetup({ code: "123456" });
|
|
82
|
+
await blocks.mfa.setMethod({ mfaType: 1 });
|
|
83
|
+
const backupCodes = await blocks.mfa.backupCodes.generate();
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Mail And Notifications
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
await blocks.mail.send({
|
|
90
|
+
to: ["user@example.com"],
|
|
91
|
+
purpose: "welcome",
|
|
92
|
+
language: "en",
|
|
93
|
+
subjectDataContext: { firstName: "Ada" }
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
await blocks.mail.sendToAny({
|
|
97
|
+
to: ["qa@example.com"],
|
|
98
|
+
purpose: "welcome",
|
|
99
|
+
language: "en",
|
|
100
|
+
isTestMail: true
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
await blocks.notifier.notify({
|
|
104
|
+
userIds: ["user-id"],
|
|
105
|
+
denormalizedPayload: JSON.stringify({ orderId: "123" }),
|
|
106
|
+
saveDenormalizedPayloadAsAnObject: true
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
const inbox = await blocks.notifier.getNotifications({ isUnreadOnly: true, page: 1, pageSize: 20 });
|
|
110
|
+
await blocks.notifier.markNotificationAsRead({ id: inbox.notifications[0].id });
|
|
111
|
+
await blocks.notifier.markAllNotificationAsRead();
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
`mail.send`/`mail.sendToAny` call `/logic/v4/Mail/Send`/`SendToAny`. `notifier.*` calls `/logic/v4/Notifier/*`
|
|
115
|
+
for sending/reading notifications; tenant notification *channel configuration* (`/os/v4/Notification/*`)
|
|
116
|
+
is an admin concern handled by `@seliseblocks/cli-os`'s `notification *` commands, not this SDK.
|
|
117
|
+
|
|
118
|
+
## Common Runtime Calls
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
const me = await blocks.iam.me();
|
|
122
|
+
const orgs = await blocks.iam.organizations.my();
|
|
123
|
+
const features = await blocks.iam.resources.features();
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
`iam.organizations.my()` returns the IAM envelope with `organizations`, not `data`.
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
const schemas = await blocks.data.schemas.list();
|
|
130
|
+
const schemaInfo = await blocks.data.schemas.infoByName("Student");
|
|
131
|
+
const schemaRules = await blocks.data.validations.bySchemaId("schema-id");
|
|
132
|
+
|
|
133
|
+
const students = blocks.data.collection("Students");
|
|
134
|
+
const rows = await students.list({ pageNo: 1, pageSize: 20 });
|
|
135
|
+
await students.create({ firstName: "Test" });
|
|
136
|
+
|
|
137
|
+
const graph = await blocks.data.graphql({
|
|
138
|
+
query: "{ students { items { itemId firstName } } }"
|
|
139
|
+
});
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
const upload = await blocks.data.files.presignedUploadUrl({
|
|
144
|
+
name: "avatar.png",
|
|
145
|
+
configurationName: "default",
|
|
146
|
+
parentDirectoryId: "root",
|
|
147
|
+
accessModifier: "Private"
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
await blocks.data.files.uploadToUrl({
|
|
151
|
+
url: upload.uploadUrl,
|
|
152
|
+
body: file,
|
|
153
|
+
contentType: file.type
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
const items = await blocks.data.objects.list({
|
|
157
|
+
parentDirectoryId: "root",
|
|
158
|
+
limit: 20
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
const directory = await blocks.data.directories.create({
|
|
162
|
+
name: "Contracts",
|
|
163
|
+
parentDirectoryId: "root",
|
|
164
|
+
allowedFileExtensions: ["pdf", "docx"]
|
|
165
|
+
});
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
```ts
|
|
169
|
+
await blocks.localization.load("en-US", ["common", "dashboard"]);
|
|
170
|
+
const label = blocks.localization.t("dashboard.title", "Dashboard");
|
|
171
|
+
|
|
172
|
+
const tenantLanguages = await blocks.localization.languagesForCurrentTenant();
|
|
173
|
+
const selectedKeys = await blocks.localization.keysByNames({
|
|
174
|
+
keyNames: ["dashboard.title"]
|
|
175
|
+
});
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## API Surface
|
|
179
|
+
|
|
180
|
+
- `blocks.auth`: AuthController login, social login, refresh, org switch, logout, signup, recovery, activation, auth config, user codes, client credentials, identity providers.
|
|
181
|
+
- `blocks.auth.idp`: hosted IdP initiate, browser redirect, callback, UI config.
|
|
182
|
+
- `blocks.auth.oidc`: refresh-token and client-credentials token endpoint helpers.
|
|
183
|
+
- `blocks.iam`: current user, users, roles, permissions, resources, organizations, signup settings (`blocks.iam.signupSettings`).
|
|
184
|
+
- `blocks.data`: schema reads, validation reads, GraphQL gateway execution, file/directory/object-tree storage helpers, and runtime collection CRUD.
|
|
185
|
+
- `blocks.localization`: tenant language/module discovery, UILM dictionary loading, selected key lookup, simple `t()` lookup.
|
|
186
|
+
- `blocks.mfa`: tenant MFA policy read/save, TOTP enrollment, OTP generate/resend/verify, method switch, disable, and backup codes.
|
|
187
|
+
- `blocks.mail`: `send`/`sendToAny` transactional email through the tenant's configured mail provider.
|
|
188
|
+
- `blocks.notifier`: `notify`, `getNotifications`, `getUnreadNotificationsBySubscriptionFilter`, `markNotificationAsRead`, `markAllNotificationAsRead`.
|
|
189
|
+
|
|
190
|
+
Professional class names are exported for advanced typing and adapters: `BlocksAuthenticationClient`, `BlocksIAMClient`, `BlocksDataClient`, `BlocksLocalizationClient`, `BlocksMfaClient`, `BlocksMailClient`, and `BlocksNotifierClient`. `BlocksApiError` is exported for typed error handling - every non-2xx response from `http.request`/`http.external` throws it, exposing `status`, `statusText`, and the parsed `body`:
|
|
191
|
+
|
|
192
|
+
```ts
|
|
193
|
+
try {
|
|
194
|
+
await blocks.iam.me();
|
|
195
|
+
} catch (error) {
|
|
196
|
+
if (error instanceof BlocksApiError) {
|
|
197
|
+
console.error(error.status, error.body);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
## Boundaries
|
|
203
|
+
|
|
204
|
+
This package must not contain:
|
|
205
|
+
|
|
206
|
+
- CLI client secrets
|
|
207
|
+
- impersonation APIs
|
|
208
|
+
- project create/list/use
|
|
209
|
+
- schema/rules deploy
|
|
210
|
+
- release deploy
|
|
211
|
+
- token/session/PKCE storage
|
|
212
|
+
- Node filesystem or OS credential storage
|
|
213
|
+
- framework UI components
|
|
214
|
+
- Localization authoring/admin endpoints
|
|
215
|
+
- Data schema/rules mutation endpoints
|
|
216
|
+
|
|
217
|
+
## Package Checks
|
|
218
|
+
|
|
219
|
+
```bash
|
|
220
|
+
npm test
|
|
221
|
+
npm pack --dry-run
|
|
222
|
+
```
|
package/dist/client.d.ts
CHANGED
|
@@ -42,6 +42,13 @@ export type BlocksClientConfig = {
|
|
|
42
42
|
* Optional fetch-compatible function for tests, SSR, or custom runtimes.
|
|
43
43
|
*/
|
|
44
44
|
fetch?: typeof fetch;
|
|
45
|
+
/**
|
|
46
|
+
* Caller-owned hook invoked at most once per request when a protected call comes back 401.
|
|
47
|
+
* Should resolve a fresh token (deduping concurrent callers itself, e.g. via a shared
|
|
48
|
+
* in-flight refresh promise) or `undefined` if the session cannot be recovered -- the SDK
|
|
49
|
+
* retries the request once with the returned token, or leaves the 401 to throw if none.
|
|
50
|
+
*/
|
|
51
|
+
onUnauthorized?: () => Promise<string | undefined> | string | undefined;
|
|
45
52
|
/**
|
|
46
53
|
* Optional hosted IdP/OIDC browser-flow configuration.
|
|
47
54
|
*/
|
|
@@ -66,6 +73,7 @@ export type RequiredConfig = {
|
|
|
66
73
|
accessToken?: string | (() => Promise<string | undefined> | string | undefined);
|
|
67
74
|
apiUrl: string;
|
|
68
75
|
appDomain?: string;
|
|
76
|
+
onUnauthorized?: () => Promise<string | undefined> | string | undefined;
|
|
69
77
|
oidc?: BlocksOidcConfig & {
|
|
70
78
|
redirectUri: string;
|
|
71
79
|
scope: string;
|
package/dist/client.js
CHANGED
|
@@ -36,6 +36,7 @@ function normalizeConfig(config) {
|
|
|
36
36
|
accessToken: config.accessToken,
|
|
37
37
|
apiUrl: trimTrailingSlash(config.apiUrl),
|
|
38
38
|
appDomain: config.appDomain,
|
|
39
|
+
onUnauthorized: config.onUnauthorized,
|
|
39
40
|
oidc: config.oidc ? {
|
|
40
41
|
...config.oidc,
|
|
41
42
|
redirectUri: config.oidc.redirectUri ?? browserRedirectUri(),
|
package/dist/data/data-client.js
CHANGED
|
@@ -289,13 +289,13 @@ export class BlocksDataClient {
|
|
|
289
289
|
*/
|
|
290
290
|
create: (payload) => this.graphql({
|
|
291
291
|
operationName: createField,
|
|
292
|
-
query: `mutation ${createField}($input: ${schemaName}InsertInput!) {
|
|
293
|
-
${createField}(input: $input) {
|
|
294
|
-
acknowledged
|
|
295
|
-
itemId
|
|
296
|
-
message
|
|
297
|
-
totalImpactedData
|
|
298
|
-
}
|
|
292
|
+
query: `mutation ${createField}($input: ${schemaName}InsertInput!) {
|
|
293
|
+
${createField}(input: $input) {
|
|
294
|
+
acknowledged
|
|
295
|
+
itemId
|
|
296
|
+
message
|
|
297
|
+
totalImpactedData
|
|
298
|
+
}
|
|
299
299
|
}`,
|
|
300
300
|
variables: { input: payload }
|
|
301
301
|
}),
|
|
@@ -306,13 +306,13 @@ export class BlocksDataClient {
|
|
|
306
306
|
*/
|
|
307
307
|
delete: (itemId, deleteOptions = {}) => this.graphql({
|
|
308
308
|
operationName: deleteField,
|
|
309
|
-
query: `mutation ${deleteField}($filter: String, $input: ${schemaName}DeleteInput!) {
|
|
310
|
-
${deleteField}(filter: $filter, input: $input) {
|
|
311
|
-
acknowledged
|
|
312
|
-
itemId
|
|
313
|
-
message
|
|
314
|
-
totalImpactedData
|
|
315
|
-
}
|
|
309
|
+
query: `mutation ${deleteField}($filter: String, $input: ${schemaName}DeleteInput!) {
|
|
310
|
+
${deleteField}(filter: $filter, input: $input) {
|
|
311
|
+
acknowledged
|
|
312
|
+
itemId
|
|
313
|
+
message
|
|
314
|
+
totalImpactedData
|
|
315
|
+
}
|
|
316
316
|
}`,
|
|
317
317
|
variables: {
|
|
318
318
|
filter: itemIdFilter(itemId),
|
|
@@ -354,13 +354,13 @@ export class BlocksDataClient {
|
|
|
354
354
|
*/
|
|
355
355
|
update: (itemId, payload) => this.graphql({
|
|
356
356
|
operationName: updateField,
|
|
357
|
-
query: `mutation ${updateField}($filter: String, $input: ${schemaName}UpdateInput!) {
|
|
358
|
-
${updateField}(filter: $filter, input: $input) {
|
|
359
|
-
acknowledged
|
|
360
|
-
itemId
|
|
361
|
-
message
|
|
362
|
-
totalImpactedData
|
|
363
|
-
}
|
|
357
|
+
query: `mutation ${updateField}($filter: String, $input: ${schemaName}UpdateInput!) {
|
|
358
|
+
${updateField}(filter: $filter, input: $input) {
|
|
359
|
+
acknowledged
|
|
360
|
+
itemId
|
|
361
|
+
message
|
|
362
|
+
totalImpactedData
|
|
363
|
+
}
|
|
364
364
|
}`,
|
|
365
365
|
variables: {
|
|
366
366
|
filter: itemIdFilter(itemId),
|
|
@@ -472,18 +472,18 @@ function dynamicQueryInput(options) {
|
|
|
472
472
|
};
|
|
473
473
|
}
|
|
474
474
|
function collectionListQuery(fieldName, selection) {
|
|
475
|
-
return `query ${fieldName}($input: DynamicQueryInput) {
|
|
476
|
-
${fieldName}(input: $input) {
|
|
477
|
-
items {
|
|
478
|
-
${indent(selection, 6)}
|
|
479
|
-
}
|
|
480
|
-
totalCount
|
|
481
|
-
pageNo
|
|
482
|
-
pageSize
|
|
483
|
-
totalPages
|
|
484
|
-
hasNextPage
|
|
485
|
-
hasPreviousPage
|
|
486
|
-
}
|
|
475
|
+
return `query ${fieldName}($input: DynamicQueryInput) {
|
|
476
|
+
${fieldName}(input: $input) {
|
|
477
|
+
items {
|
|
478
|
+
${indent(selection, 6)}
|
|
479
|
+
}
|
|
480
|
+
totalCount
|
|
481
|
+
pageNo
|
|
482
|
+
pageSize
|
|
483
|
+
totalPages
|
|
484
|
+
hasNextPage
|
|
485
|
+
hasPreviousPage
|
|
486
|
+
}
|
|
487
487
|
}`;
|
|
488
488
|
}
|
|
489
489
|
function selectionFor(fields) {
|
package/dist/http/http-client.js
CHANGED
|
@@ -12,26 +12,38 @@ export class BlocksHttpClient {
|
|
|
12
12
|
}
|
|
13
13
|
async request(path, options = {}) {
|
|
14
14
|
const url = buildUrl(this.config.apiUrl, path, options.query);
|
|
15
|
-
const headers = new Headers(options.headers);
|
|
16
15
|
const rawBody = isBodyInit(options.body) ? options.body : undefined;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
headers.set("
|
|
16
|
+
const send = async (accessTokenOverride) => {
|
|
17
|
+
const headers = new Headers(options.headers);
|
|
18
|
+
headers.set("Accept", "application/json");
|
|
19
|
+
headers.set("x-blocks-key", this.config.xBlocksKey);
|
|
20
|
+
if (options.body !== undefined && !rawBody && !headers.has("Content-Type")) {
|
|
21
|
+
headers.set("Content-Type", "application/json");
|
|
22
|
+
}
|
|
23
|
+
if (options.auth !== false) {
|
|
24
|
+
const token = accessTokenOverride ?? options.accessToken ?? await this.auth.accessToken();
|
|
25
|
+
if (token)
|
|
26
|
+
headers.set("Authorization", `Bearer ${token}`);
|
|
27
|
+
}
|
|
28
|
+
return this.fetchImpl(url, {
|
|
29
|
+
body: rawBody ?? (options.body === undefined ? undefined : JSON.stringify(options.body)),
|
|
30
|
+
// IAM's hosted IdP flow sets the session as a Secure, httpOnly cookie by default;
|
|
31
|
+
// without this the browser never sends it back on subsequent Blocks API calls.
|
|
32
|
+
credentials: "include",
|
|
33
|
+
headers,
|
|
34
|
+
method: options.method ?? (options.body === undefined ? "GET" : "POST")
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
let response = await send();
|
|
38
|
+
// A 401 on a token the caller supplied directly (options.accessToken) is the
|
|
39
|
+
// caller's problem to resolve, not ours. Otherwise, give the app one chance to
|
|
40
|
+
// recover through its own onUnauthorized hook (expected to dedupe concurrent
|
|
41
|
+
// callers behind a single refresh) and retry exactly once with what it returns.
|
|
42
|
+
if (response.status === 401 && options.auth !== false && options.accessToken === undefined && this.config.onUnauthorized) {
|
|
43
|
+
const freshToken = await this.config.onUnauthorized();
|
|
44
|
+
if (freshToken)
|
|
45
|
+
response = await send(freshToken);
|
|
21
46
|
}
|
|
22
|
-
if (options.auth !== false) {
|
|
23
|
-
const token = options.accessToken ?? await this.auth.accessToken();
|
|
24
|
-
if (token)
|
|
25
|
-
headers.set("Authorization", `Bearer ${token}`);
|
|
26
|
-
}
|
|
27
|
-
const response = await this.fetchImpl(url, {
|
|
28
|
-
body: rawBody ?? (options.body === undefined ? undefined : JSON.stringify(options.body)),
|
|
29
|
-
// IAM's hosted IdP flow sets the session as a Secure, httpOnly cookie by default;
|
|
30
|
-
// without this the browser never sends it back on subsequent Blocks API calls.
|
|
31
|
-
credentials: "include",
|
|
32
|
-
headers,
|
|
33
|
-
method: options.method ?? (options.body === undefined ? "GET" : "POST")
|
|
34
|
-
});
|
|
35
47
|
const body = await parseBody(response);
|
|
36
48
|
if (!response.ok)
|
|
37
49
|
throw new BlocksApiError(response.status, response.statusText, body);
|
package/package.json
CHANGED
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@seliseblocks/client",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "Framework-neutral TypeScript client SDK for SELISE Blocks frontend applications.",
|
|
5
|
-
"license": "MIT",
|
|
6
|
-
"type": "module",
|
|
7
|
-
"keywords": [
|
|
8
|
-
"selise",
|
|
9
|
-
"blocks",
|
|
10
|
-
"sdk",
|
|
11
|
-
"typescript",
|
|
12
|
-
"frontend"
|
|
13
|
-
],
|
|
14
|
-
"main": "./dist/index.js",
|
|
15
|
-
"types": "./dist/index.d.ts",
|
|
16
|
-
"exports": {
|
|
17
|
-
".": {
|
|
18
|
-
"types": "./dist/index.d.ts",
|
|
19
|
-
"import": "./dist/index.js"
|
|
20
|
-
},
|
|
21
|
-
"./package.json": "./package.json"
|
|
22
|
-
},
|
|
23
|
-
"publishConfig": {
|
|
24
|
-
"access": "public"
|
|
25
|
-
},
|
|
26
|
-
"sideEffects": false,
|
|
27
|
-
"files": [
|
|
28
|
-
"dist",
|
|
29
|
-
"README.md",
|
|
30
|
-
"AI_USAGE_GUIDE.md",
|
|
31
|
-
"LICENSE"
|
|
32
|
-
],
|
|
33
|
-
"scripts": {
|
|
34
|
-
"clean": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\"",
|
|
35
|
-
"build": "npm run clean && tsc -p tsconfig.json",
|
|
36
|
-
"lint": "tsc -p tsconfig.json --noEmit",
|
|
37
|
-
"test": "npm run build && node --test test/*.test.mjs",
|
|
38
|
-
"prepack": "npm run build",
|
|
39
|
-
"prepublishOnly": "npm test"
|
|
40
|
-
},
|
|
41
|
-
"engines": {
|
|
42
|
-
"node": ">=20"
|
|
43
|
-
},
|
|
44
|
-
"devDependencies": {
|
|
45
|
-
"@types/node": "^22.0.0",
|
|
46
|
-
"typescript": "^5.5.0"
|
|
47
|
-
}
|
|
48
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@seliseblocks/client",
|
|
3
|
+
"version": "0.1.6",
|
|
4
|
+
"description": "Framework-neutral TypeScript client SDK for SELISE Blocks frontend applications.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"selise",
|
|
9
|
+
"blocks",
|
|
10
|
+
"sdk",
|
|
11
|
+
"typescript",
|
|
12
|
+
"frontend"
|
|
13
|
+
],
|
|
14
|
+
"main": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./package.json": "./package.json"
|
|
22
|
+
},
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"sideEffects": false,
|
|
27
|
+
"files": [
|
|
28
|
+
"dist",
|
|
29
|
+
"README.md",
|
|
30
|
+
"AI_USAGE_GUIDE.md",
|
|
31
|
+
"LICENSE"
|
|
32
|
+
],
|
|
33
|
+
"scripts": {
|
|
34
|
+
"clean": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\"",
|
|
35
|
+
"build": "npm run clean && tsc -p tsconfig.json",
|
|
36
|
+
"lint": "tsc -p tsconfig.json --noEmit",
|
|
37
|
+
"test": "npm run build && node --test test/*.test.mjs",
|
|
38
|
+
"prepack": "npm run build",
|
|
39
|
+
"prepublishOnly": "npm test"
|
|
40
|
+
},
|
|
41
|
+
"engines": {
|
|
42
|
+
"node": ">=20"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@types/node": "^22.0.0",
|
|
46
|
+
"typescript": "^5.5.0"
|
|
47
|
+
}
|
|
48
|
+
}
|