@frontera-sdk/blueprint 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.
- package/LICENSE +202 -0
- package/README.md +48 -0
- package/package.json +68 -0
- package/src/blueprint-client.ts +81 -0
- package/src/hooks.ts +102 -0
- package/src/provider.tsx +31 -0
- package/src/types.ts +152 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 Sebati
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# @frontera-sdk/blueprint
|
|
2
|
+
|
|
3
|
+
React hooks for reading Blueprint — the organization-wide model of object
|
|
4
|
+
types, properties, links and metrics — from inside a Frontera app.
|
|
5
|
+
|
|
6
|
+
```bash
|
|
7
|
+
bun add @frontera-sdk/blueprint @frontera-sdk/core react @tanstack/react-query
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
```tsx
|
|
11
|
+
import { blueprintProvider } from '@frontera-sdk/blueprint/provider'
|
|
12
|
+
import { useObjects } from '@frontera-sdk/blueprint/hooks'
|
|
13
|
+
import { createFronteraApp } from '@frontera-sdk/core/create-frontera-app'
|
|
14
|
+
|
|
15
|
+
createFronteraApp(<App />, { providers: [blueprintProvider] })
|
|
16
|
+
|
|
17
|
+
function App() {
|
|
18
|
+
const { data, isLoading } = useObjects('Shipment', {
|
|
19
|
+
where: { property: 'deliveryStatus', op: 'eq', value: 'late' },
|
|
20
|
+
pageSize: 20,
|
|
21
|
+
})
|
|
22
|
+
return <p>{isLoading ? 'Loading…' : `${data?.rows.length} late`}</p>
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Filter on the server
|
|
27
|
+
|
|
28
|
+
`where` compiles into the object set, so the server filters and pages.
|
|
29
|
+
Filtering the returned array instead narrows one page and misreports every
|
|
30
|
+
total — a filter matching 8,961 records renders 5 of them under "Page 1 of 1".
|
|
31
|
+
For the same reason a total is its own query (`useAggregate` with a count over
|
|
32
|
+
the same object set), never `rows.length`.
|
|
33
|
+
|
|
34
|
+
## Entry points
|
|
35
|
+
|
|
36
|
+
| Import | What it is |
|
|
37
|
+
|---|---|
|
|
38
|
+
| `@frontera-sdk/blueprint/hooks` | `useObjects`, `useAggregate`, `useObjectInstance` |
|
|
39
|
+
| `@frontera-sdk/blueprint/provider` | the provider `createFronteraApp` takes |
|
|
40
|
+
| `@frontera-sdk/blueprint/blueprint-client` | the client underneath the hooks |
|
|
41
|
+
| `@frontera-sdk/blueprint/types` | request and response types, `WhereNode` |
|
|
42
|
+
|
|
43
|
+
Every read is scoped by the credential the host handed over at handshake, so an
|
|
44
|
+
object type the workspace was not granted is absent rather than forbidden.
|
|
45
|
+
|
|
46
|
+
## License
|
|
47
|
+
|
|
48
|
+
Apache-2.0. See [LICENSE](./LICENSE).
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@frontera-sdk/blueprint",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "React hooks for reading Blueprint data from inside a Frontera app.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"frontera",
|
|
7
|
+
"blueprint",
|
|
8
|
+
"react",
|
|
9
|
+
"hooks"
|
|
10
|
+
],
|
|
11
|
+
"license": "Apache-2.0",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"files": [
|
|
14
|
+
"src",
|
|
15
|
+
"!src/__tests__",
|
|
16
|
+
"!src/**/*.test.ts",
|
|
17
|
+
"README.md",
|
|
18
|
+
"LICENSE"
|
|
19
|
+
],
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"exports": {
|
|
24
|
+
"./types": {
|
|
25
|
+
"types": "./src/types.ts",
|
|
26
|
+
"import": "./src/types.ts"
|
|
27
|
+
},
|
|
28
|
+
"./blueprint-client": {
|
|
29
|
+
"types": "./src/blueprint-client.ts",
|
|
30
|
+
"import": "./src/blueprint-client.ts"
|
|
31
|
+
},
|
|
32
|
+
"./hooks": {
|
|
33
|
+
"types": "./src/hooks.ts",
|
|
34
|
+
"import": "./src/hooks.ts"
|
|
35
|
+
},
|
|
36
|
+
"./provider": {
|
|
37
|
+
"types": "./src/provider.tsx",
|
|
38
|
+
"import": "./src/provider.tsx"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"test": "bun test",
|
|
43
|
+
"typecheck": "bunx tsc --noEmit",
|
|
44
|
+
"smoke": "bun run scripts/smoke.ts"
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"@frontera-sdk/core": "0.1.0"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"@tanstack/react-query": "^5.90.21",
|
|
51
|
+
"react": "^19.0.0"
|
|
52
|
+
},
|
|
53
|
+
"peerDependenciesMeta": {
|
|
54
|
+
"@tanstack/react-query": {
|
|
55
|
+
"optional": true
|
|
56
|
+
},
|
|
57
|
+
"react": {
|
|
58
|
+
"optional": true
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
"devDependencies": {
|
|
62
|
+
"@tanstack/react-query": "^5.90.21",
|
|
63
|
+
"@types/bun": "latest",
|
|
64
|
+
"@types/react": "^19",
|
|
65
|
+
"react": "19.2.5",
|
|
66
|
+
"typescript": "^5.9.3"
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { FronteraClient } from '@frontera-sdk/core/client'
|
|
2
|
+
import type {
|
|
3
|
+
AggregateGroupBy,
|
|
4
|
+
AggregateRequest,
|
|
5
|
+
AggregateResponse,
|
|
6
|
+
MetricQueryRequest,
|
|
7
|
+
ObjectInstance,
|
|
8
|
+
QueryRequest,
|
|
9
|
+
QueryResponse,
|
|
10
|
+
} from './types'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Typed client for the Blueprint read API.
|
|
14
|
+
*
|
|
15
|
+
* Every method is workspace-scoped by the credential on the underlying
|
|
16
|
+
* `FronteraClient`. An object type the workspace has not been granted is absent
|
|
17
|
+
* from the server's catalog snapshot, so the query compiler raises
|
|
18
|
+
* `UNKNOWN_OBJECT_TYPE` — grant violations arrive as a 404, not a 403, and that
|
|
19
|
+
* is intentional: the caller cannot distinguish "does not exist" from "not
|
|
20
|
+
* granted", which is the point.
|
|
21
|
+
*/
|
|
22
|
+
export class BlueprintClient {
|
|
23
|
+
constructor(private readonly client: FronteraClient) {}
|
|
24
|
+
|
|
25
|
+
query<TRow = Record<string, unknown>>(request: QueryRequest): Promise<QueryResponse<TRow>> {
|
|
26
|
+
return this.client.request<QueryResponse<TRow>>('/v1/blueprint/query', {
|
|
27
|
+
method: 'POST',
|
|
28
|
+
body: request,
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
queryObjectSet<TRow = Record<string, unknown>>(
|
|
33
|
+
objectSetId: string,
|
|
34
|
+
request: Omit<QueryRequest, 'objectSet'> = {},
|
|
35
|
+
): Promise<QueryResponse<TRow>> {
|
|
36
|
+
return this.client.request<QueryResponse<TRow>>(
|
|
37
|
+
`/v1/blueprint/object-sets/${encodeURIComponent(objectSetId)}/query`,
|
|
38
|
+
{ method: 'POST', body: request },
|
|
39
|
+
)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Run an aggregation.
|
|
44
|
+
*
|
|
45
|
+
* `groupBy` is required by the service; it is defaulted here so a grand
|
|
46
|
+
* total reads as `aggregate({ objectSet, aggregations })` rather than
|
|
47
|
+
* forcing every caller to remember an empty array.
|
|
48
|
+
*/
|
|
49
|
+
aggregate(
|
|
50
|
+
request: Omit<AggregateRequest, 'groupBy'> & { groupBy?: AggregateGroupBy[] },
|
|
51
|
+
): Promise<AggregateResponse> {
|
|
52
|
+
return this.client.request<AggregateResponse>('/v1/blueprint/aggregate', {
|
|
53
|
+
method: 'POST',
|
|
54
|
+
// Default AFTER the spread: an explicit `undefined` in the request must
|
|
55
|
+
// not win over the fallback.
|
|
56
|
+
body: { ...request, groupBy: request.groupBy ?? [] },
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
instance<TProps = Record<string, unknown>>(
|
|
61
|
+
objectType: string,
|
|
62
|
+
pk: string,
|
|
63
|
+
): Promise<ObjectInstance<TProps>> {
|
|
64
|
+
return this.client.request<ObjectInstance<TProps>>(
|
|
65
|
+
`/v1/blueprint/object-types/${encodeURIComponent(objectType)}/instances/${encodeURIComponent(pk)}`,
|
|
66
|
+
)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
propertyValues(objectType: string, property: string): Promise<string[]> {
|
|
70
|
+
return this.client.request<string[]>(
|
|
71
|
+
`/v1/blueprint/object-types/${encodeURIComponent(objectType)}/properties/${encodeURIComponent(property)}/values`,
|
|
72
|
+
)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
metricQuery(apiName: string, request: MetricQueryRequest = {}): Promise<AggregateResponse> {
|
|
76
|
+
return this.client.request<AggregateResponse>(
|
|
77
|
+
`/v1/blueprint/metrics/${encodeURIComponent(apiName)}/query`,
|
|
78
|
+
{ method: 'POST', body: request },
|
|
79
|
+
)
|
|
80
|
+
}
|
|
81
|
+
}
|
package/src/hooks.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { createContext, useContext } from 'react'
|
|
2
|
+
import { useQuery, type UseQueryOptions, type UseQueryResult } from '@tanstack/react-query'
|
|
3
|
+
|
|
4
|
+
import type { BlueprintClient } from './blueprint-client'
|
|
5
|
+
import { objectsOf } from './types'
|
|
6
|
+
import type {
|
|
7
|
+
AggregateRequest,
|
|
8
|
+
AggregateResponse,
|
|
9
|
+
ObjectInstance,
|
|
10
|
+
QueryRequest,
|
|
11
|
+
QueryResponse,
|
|
12
|
+
WhereNode,
|
|
13
|
+
} from './types'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Query-key factory.
|
|
17
|
+
*
|
|
18
|
+
* Requests are serialised into the key so two structurally equal requests share
|
|
19
|
+
* a cache entry. Key order therefore matters: callers must build request
|
|
20
|
+
* objects consistently, which they do because these hooks construct them.
|
|
21
|
+
*/
|
|
22
|
+
export const blueprintKeys = {
|
|
23
|
+
all: ['blueprint'] as const,
|
|
24
|
+
query: (request: QueryRequest) => ['blueprint', 'query', JSON.stringify(request)] as const,
|
|
25
|
+
aggregate: (request: AggregateRequest) =>
|
|
26
|
+
['blueprint', 'aggregate', JSON.stringify(request)] as const,
|
|
27
|
+
instance: (objectType: string, pk: string) =>
|
|
28
|
+
['blueprint', 'instance', objectType, pk] as const,
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const FronteraBlueprintContext = createContext<BlueprintClient | null>(null)
|
|
32
|
+
|
|
33
|
+
export function useBlueprintClient(): BlueprintClient {
|
|
34
|
+
const client = useContext(FronteraBlueprintContext)
|
|
35
|
+
if (!client) {
|
|
36
|
+
throw new Error(
|
|
37
|
+
'No BlueprintClient in context. Wrap the tree in FronteraBlueprintContext.Provider — createFronteraApp does this for you in a Frontera app.',
|
|
38
|
+
)
|
|
39
|
+
}
|
|
40
|
+
return client
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
type ReadOptions<TData> = Omit<UseQueryOptions<TData, Error>, 'queryKey' | 'queryFn'>
|
|
44
|
+
|
|
45
|
+
/** Run an arbitrary object-set query. */
|
|
46
|
+
export function useObjectQuery<TRow = Record<string, unknown>>(
|
|
47
|
+
request: QueryRequest,
|
|
48
|
+
options: ReadOptions<QueryResponse<TRow>> = {},
|
|
49
|
+
): UseQueryResult<QueryResponse<TRow>, Error> {
|
|
50
|
+
const client = useBlueprintClient()
|
|
51
|
+
return useQuery({
|
|
52
|
+
queryKey: blueprintKeys.query(request),
|
|
53
|
+
queryFn: () => client.query<TRow>(request),
|
|
54
|
+
...options,
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* One object type, optionally filtered.
|
|
60
|
+
*
|
|
61
|
+
* `where` compiles into the object-set expression, so the SERVER filters and
|
|
62
|
+
* pages. Filtering the returned rows in the component instead is the classic
|
|
63
|
+
* mistake: it narrows only the page you happened to fetch, so a facet showing
|
|
64
|
+
* 8,961 matches renders 5 rows and claims "Page 1 of 1".
|
|
65
|
+
*/
|
|
66
|
+
export function useObjects<TRow = Record<string, unknown>>(
|
|
67
|
+
objectType: string,
|
|
68
|
+
options: ReadOptions<QueryResponse<TRow>> &
|
|
69
|
+
Omit<QueryRequest, 'objectSet'> & { where?: WhereNode } = {},
|
|
70
|
+
): UseQueryResult<QueryResponse<TRow>, Error> {
|
|
71
|
+
const { select, orderBy, page, pageSize, where, ...queryOptions } = options
|
|
72
|
+
return useObjectQuery<TRow>(
|
|
73
|
+
{ objectSet: objectsOf(objectType, where), select, orderBy, page, pageSize },
|
|
74
|
+
queryOptions,
|
|
75
|
+
)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function useAggregate(
|
|
79
|
+
request: AggregateRequest,
|
|
80
|
+
options: ReadOptions<AggregateResponse> = {},
|
|
81
|
+
): UseQueryResult<AggregateResponse, Error> {
|
|
82
|
+
const client = useBlueprintClient()
|
|
83
|
+
return useQuery({
|
|
84
|
+
queryKey: blueprintKeys.aggregate(request),
|
|
85
|
+
queryFn: () => client.aggregate(request),
|
|
86
|
+
...options,
|
|
87
|
+
})
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function useObjectInstance<TProps = Record<string, unknown>>(
|
|
91
|
+
objectType: string,
|
|
92
|
+
pk: string | null | undefined,
|
|
93
|
+
options: ReadOptions<ObjectInstance<TProps>> = {},
|
|
94
|
+
): UseQueryResult<ObjectInstance<TProps>, Error> {
|
|
95
|
+
const client = useBlueprintClient()
|
|
96
|
+
return useQuery({
|
|
97
|
+
queryKey: blueprintKeys.instance(objectType, pk ?? ''),
|
|
98
|
+
queryFn: () => client.instance<TProps>(objectType, pk as string),
|
|
99
|
+
enabled: Boolean(pk) && options.enabled !== false,
|
|
100
|
+
...options,
|
|
101
|
+
})
|
|
102
|
+
}
|
package/src/provider.tsx
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ReactNode } from 'react'
|
|
2
|
+
import type { FronteraClient } from '@frontera-sdk/core/client'
|
|
3
|
+
|
|
4
|
+
import { BlueprintClient } from './blueprint-client'
|
|
5
|
+
import { FronteraBlueprintContext } from './hooks'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Plug Blueprint reads into `createFronteraApp`.
|
|
9
|
+
*
|
|
10
|
+
* The dependency runs one way — `@frontera-sdk/blueprint` knows about
|
|
11
|
+
* `@frontera-sdk/core`, never the reverse — so the app entry point composes the
|
|
12
|
+
* two rather than sdk-core importing a domain package it should not know exists.
|
|
13
|
+
* That is why this is a callback the entry passes in:
|
|
14
|
+
*
|
|
15
|
+
* ```tsx
|
|
16
|
+
* createFronteraApp(<App />, { providers: [blueprintProvider] })
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* The client is rebuilt whenever the host rotates the credential, because the
|
|
20
|
+
* `FronteraClient` it wraps is replaced rather than mutated.
|
|
21
|
+
*/
|
|
22
|
+
export function blueprintProvider(
|
|
23
|
+
value: { client: FronteraClient },
|
|
24
|
+
children: ReactNode,
|
|
25
|
+
): ReactNode {
|
|
26
|
+
return (
|
|
27
|
+
<FronteraBlueprintContext.Provider value={new BlueprintClient(value.client)}>
|
|
28
|
+
{children}
|
|
29
|
+
</FronteraBlueprintContext.Provider>
|
|
30
|
+
)
|
|
31
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Request and response types for the Blueprint read API.
|
|
3
|
+
*
|
|
4
|
+
* `ObjectSetExpr` is deliberately loose. The service validates the expression
|
|
5
|
+
* tree in `query/validate.ts` with error messages far better than a structural
|
|
6
|
+
* type could produce, and mirroring that grammar here would mean shipping an
|
|
7
|
+
* SDK release every time the service gained a node kind. The common shapes are
|
|
8
|
+
* named so callers get completion for what they actually write.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Condition operators, mirroring the service's `ConditionOp`.
|
|
12
|
+
*
|
|
13
|
+
* Filtering belongs in the object-set expression, NOT in the app: a client-side
|
|
14
|
+
* `.filter()` over a fetched page silently reduces "8,961 cancelled shipments"
|
|
15
|
+
* to "the cancelled ones that happened to be in the last 200 rows", and the
|
|
16
|
+
* counts stop matching the table.
|
|
17
|
+
*/
|
|
18
|
+
export type ConditionOp =
|
|
19
|
+
| 'eq' | 'ne' | 'in' | 'notIn' | 'contains' | 'startsWith'
|
|
20
|
+
| 'isNull' | 'isNotNull'
|
|
21
|
+
| 'gt' | 'gte' | 'lt' | 'lte' | 'between'
|
|
22
|
+
| 'dateRange'
|
|
23
|
+
|
|
24
|
+
export type DatePreset =
|
|
25
|
+
| 'TODAY' | 'YESTERDAY' | 'LAST_7_DAYS' | 'LAST_30_DAYS' | 'LAST_90_DAYS'
|
|
26
|
+
| 'THIS_MONTH' | 'LAST_MONTH' | 'THIS_QUARTER' | 'THIS_YEAR'
|
|
27
|
+
|
|
28
|
+
export interface PropertyCondition {
|
|
29
|
+
property: string
|
|
30
|
+
op: ConditionOp
|
|
31
|
+
value?: unknown
|
|
32
|
+
values?: unknown[]
|
|
33
|
+
/** `dateRange` only — resolved to [start, end) server-side. */
|
|
34
|
+
preset?: DatePreset
|
|
35
|
+
timezone?: string
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export type WhereNode =
|
|
39
|
+
| PropertyCondition
|
|
40
|
+
| { and: WhereNode[] }
|
|
41
|
+
| { or: WhereNode[] }
|
|
42
|
+
| { not: WhereNode }
|
|
43
|
+
|
|
44
|
+
export type ObjectSetExpr =
|
|
45
|
+
| { type: 'base'; objectType: string }
|
|
46
|
+
| { type: 'filter'; objectSet: ObjectSetExpr; where: WhereNode }
|
|
47
|
+
| { type: 'searchAround'; objectSet: ObjectSetExpr; link: string }
|
|
48
|
+
| { type: 'union' | 'intersect' | 'subtract'; objectSets: ObjectSetExpr[] }
|
|
49
|
+
| { type: 'reference'; objectSetId: string }
|
|
50
|
+
| { type: 'static'; objectType: string; pks: Array<string | number> }
|
|
51
|
+
| { type: string; [key: string]: unknown }
|
|
52
|
+
|
|
53
|
+
export interface OrderBy {
|
|
54
|
+
property: string
|
|
55
|
+
dir: 'asc' | 'desc'
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface QueryRequest {
|
|
59
|
+
objectSet: ObjectSetExpr
|
|
60
|
+
select?: string[]
|
|
61
|
+
orderBy?: OrderBy[]
|
|
62
|
+
page?: number
|
|
63
|
+
pageSize?: number
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* `properties` is the FULL projection for the object type regardless of what
|
|
68
|
+
* `select` asked for — filter display columns client-side rather than assuming
|
|
69
|
+
* this list matches `select`.
|
|
70
|
+
*/
|
|
71
|
+
export interface QueryResponse<TRow = Record<string, unknown>> {
|
|
72
|
+
rows: TRow[]
|
|
73
|
+
properties: Array<{ apiName: string; displayName?: string; dataType?: string }>
|
|
74
|
+
totalCount?: number
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export const AGGREGATE_FUNCTIONS = [
|
|
78
|
+
'count',
|
|
79
|
+
'sum',
|
|
80
|
+
'avg',
|
|
81
|
+
'min',
|
|
82
|
+
'max',
|
|
83
|
+
'countDistinct',
|
|
84
|
+
] as const
|
|
85
|
+
|
|
86
|
+
export type AggregateFunction = (typeof AGGREGATE_FUNCTIONS)[number]
|
|
87
|
+
|
|
88
|
+
export type TimeGrain = 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year'
|
|
89
|
+
|
|
90
|
+
export interface AggregateSpec {
|
|
91
|
+
/**
|
|
92
|
+
* Result column name.
|
|
93
|
+
*
|
|
94
|
+
* `alias`, not `name` — this mirrors the service's `aggregateBody` schema
|
|
95
|
+
* exactly (blueprint-query-router.ts). Getting it wrong produces a 400 that
|
|
96
|
+
* says only "Expected required property", which is a miserable thing to
|
|
97
|
+
* debug from inside an app.
|
|
98
|
+
*/
|
|
99
|
+
alias: string
|
|
100
|
+
fn: AggregateFunction
|
|
101
|
+
property?: string
|
|
102
|
+
/** Per-aggregation filter expression, validated server-side. */
|
|
103
|
+
filters?: unknown
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export interface AggregateGroupBy {
|
|
107
|
+
property: string
|
|
108
|
+
/** Set for a temporal property to bucket by grain; the service aliases the
|
|
109
|
+
* result column as `${property}_${grain}`. */
|
|
110
|
+
bucket?: TimeGrain
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export interface AggregateRequest {
|
|
114
|
+
objectSet: ObjectSetExpr
|
|
115
|
+
aggregations: AggregateSpec[]
|
|
116
|
+
/** REQUIRED by the service — pass `[]` for a grand total. */
|
|
117
|
+
groupBy: AggregateGroupBy[]
|
|
118
|
+
having?: unknown
|
|
119
|
+
sort?: Array<{ alias: string; dir: 'asc' | 'desc' }>
|
|
120
|
+
limit?: number
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export interface AggregateResponse {
|
|
124
|
+
rows: Array<Record<string, unknown>>
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export interface MetricQueryRequest {
|
|
128
|
+
measures?: string[]
|
|
129
|
+
dimensions?: string[]
|
|
130
|
+
grain?: TimeGrain
|
|
131
|
+
limit?: number
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* One object, as the service returns it: the property bag itself.
|
|
136
|
+
*
|
|
137
|
+
* NOT wrapped in `{ objectType, primaryKey, properties }` — the instance route
|
|
138
|
+
* responds with the properties flat under the envelope's `data`, so a wrapper
|
|
139
|
+
* type here would make `.properties` permanently undefined and render an empty
|
|
140
|
+
* detail panel with no error to explain it.
|
|
141
|
+
*/
|
|
142
|
+
export type ObjectInstance<TProps = Record<string, unknown>> = TProps
|
|
143
|
+
|
|
144
|
+
/** Wrap an object set in a filter. `where` undefined returns the set unchanged. */
|
|
145
|
+
export function filtered(objectSet: ObjectSetExpr, where?: WhereNode): ObjectSetExpr {
|
|
146
|
+
return where ? { type: 'filter', objectSet, where } : objectSet
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** The common case: one object type, optionally filtered. */
|
|
150
|
+
export function objectsOf(objectType: string, where?: WhereNode): ObjectSetExpr {
|
|
151
|
+
return filtered({ type: 'base', objectType }, where)
|
|
152
|
+
}
|