@asteby/metacore-runtime-react 53.0.0 → 53.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/CHANGELOG.md +6 -0
- package/dist/host-api.d.ts.map +1 -1
- package/dist/host-api.js +34 -7
- package/package.json +5 -5
- package/src/__tests__/host-api.test.ts +66 -0
- package/src/host-api.ts +30 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @asteby/metacore-runtime-react
|
|
2
2
|
|
|
3
|
+
## 53.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- fa551df: HostApi: `responseType: "blob"` request option. Federated pages can fetch PDFs and other binary documents (print tickets, download exports) through `useHostApi()` and get a `Blob` back, without reading tokens from `localStorage`. Error bodies of failed blob requests are decoded so `HostApiError.code`/`message` still work. Default (`"json"`) behavior is unchanged.
|
|
8
|
+
|
|
3
9
|
## 53.0.0
|
|
4
10
|
|
|
5
11
|
### Minor Changes
|
package/dist/host-api.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"host-api.d.ts","sourceRoot":"","sources":["../src/host-api.ts"],"names":[],"mappings":"AAYA,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAA;AACnD,OAAO,KAAK,EACR,OAAO,EAGV,MAAM,sBAAsB,CAAA;AAC7B,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AAG9C,MAAM,WAAW,oBAAoB;IACjC;;;;OAIG;IACH,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB;;;;OAIG;IACH,SAAS,CAAC,EAAE,OAAO,CAAA;CACtB;AAkBD,4EAA4E;AAC5E,wBAAgB,cAAc,CAAC,CAAC,EAAE,OAAO,GAAG,YAAY,CAWvD;
|
|
1
|
+
{"version":3,"file":"host-api.d.ts","sourceRoot":"","sources":["../src/host-api.ts"],"names":[],"mappings":"AAYA,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAA;AACnD,OAAO,KAAK,EACR,OAAO,EAGV,MAAM,sBAAsB,CAAA;AAC7B,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AAG9C,MAAM,WAAW,oBAAoB;IACjC;;;;OAIG;IACH,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB;;;;OAIG;IACH,SAAS,CAAC,EAAE,OAAO,CAAA;CACtB;AAkBD,4EAA4E;AAC5E,wBAAgB,cAAc,CAAC,CAAC,EAAE,OAAO,GAAG,YAAY,CAWvD;AAqDD,yEAAyE;AACzE,wBAAgB,aAAa,CAAC,MAAM,EAAE,SAAS,EAAE,OAAO,GAAE,oBAAyB,GAAG,OAAO,CA+C5F"}
|
package/dist/host-api.js
CHANGED
|
@@ -57,15 +57,41 @@ function toConfig(opts) {
|
|
|
57
57
|
config.params = opts.params;
|
|
58
58
|
if (opts.signal)
|
|
59
59
|
config.signal = opts.signal;
|
|
60
|
+
if (opts.responseType === 'blob')
|
|
61
|
+
config.responseType = 'blob';
|
|
60
62
|
return config;
|
|
61
63
|
}
|
|
62
|
-
|
|
64
|
+
// With `responseType: 'blob'` axios also delivers the error body as a Blob.
|
|
65
|
+
// Decode it back to JSON so `code`/`message` survive; non-JSON bodies are dropped.
|
|
66
|
+
async function decodeBlobError(e) {
|
|
67
|
+
const resp = e?.response;
|
|
68
|
+
if (!resp || typeof Blob === 'undefined' || !(resp.data instanceof Blob))
|
|
69
|
+
return e;
|
|
70
|
+
let data;
|
|
71
|
+
try {
|
|
72
|
+
data = JSON.parse(await resp.data.text());
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
data = undefined;
|
|
76
|
+
}
|
|
77
|
+
return { ...e, response: { ...resp, data } };
|
|
78
|
+
}
|
|
79
|
+
async function bodyOf(p, opts) {
|
|
63
80
|
try {
|
|
64
81
|
const res = await p;
|
|
82
|
+
if (opts?.responseType === 'blob') {
|
|
83
|
+
const d = res.data;
|
|
84
|
+
if (typeof Blob !== 'undefined' && d instanceof Blob)
|
|
85
|
+
return d;
|
|
86
|
+
// Some transports hand back raw bytes; normalize so callers always get a Blob.
|
|
87
|
+
if (d instanceof ArrayBuffer || ArrayBuffer.isView(d))
|
|
88
|
+
return new Blob([d]);
|
|
89
|
+
throw new HostApiError('Expected a binary response', 0);
|
|
90
|
+
}
|
|
65
91
|
return res.data;
|
|
66
92
|
}
|
|
67
93
|
catch (e) {
|
|
68
|
-
throw toHostApiError(e);
|
|
94
|
+
throw toHostApiError(opts?.responseType === 'blob' ? await decodeBlobError(e) : e);
|
|
69
95
|
}
|
|
70
96
|
}
|
|
71
97
|
/** Build the remote-facing {@link HostApi} over the host's ApiClient. */
|
|
@@ -79,17 +105,18 @@ export function createHostApi(client, options = {}) {
|
|
|
79
105
|
return Object.freeze({
|
|
80
106
|
get: (path, opts) => {
|
|
81
107
|
const args = withConfig([path], opts);
|
|
82
|
-
|
|
108
|
+
const batch = options.batchGets && opts?.responseType !== 'blob';
|
|
109
|
+
return bodyOf(batch ? batchGet(client, ...args) : client.get(...args), opts);
|
|
83
110
|
},
|
|
84
|
-
post: (path, body, opts) => bodyOf(client.post(...withConfig([path, body], opts))),
|
|
85
|
-
put: (path, body, opts) => bodyOf(client.put(...withConfig([path, body], opts))),
|
|
111
|
+
post: (path, body, opts) => bodyOf(client.post(...withConfig([path, body], opts)), opts),
|
|
112
|
+
put: (path, body, opts) => bodyOf(client.put(...withConfig([path, body], opts)), opts),
|
|
86
113
|
patch: (path, body, opts) => {
|
|
87
114
|
if (typeof patch !== 'function') {
|
|
88
115
|
return Promise.reject(new HostApiError('PATCH is not supported by this host', 0));
|
|
89
116
|
}
|
|
90
|
-
return bodyOf(patch.call(client, ...withConfig([path, body], opts)));
|
|
117
|
+
return bodyOf(patch.call(client, ...withConfig([path, body], opts)), opts);
|
|
91
118
|
},
|
|
92
|
-
delete: (path, opts) => bodyOf(client.delete(...withConfig([path], opts))),
|
|
119
|
+
delete: (path, opts) => bodyOf(client.delete(...withConfig([path], opts)), opts),
|
|
93
120
|
q: async (path, opts) => {
|
|
94
121
|
try {
|
|
95
122
|
const url = path.startsWith('/') ? path : `/${path}`;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@asteby/metacore-runtime-react",
|
|
3
|
-
"version": "53.
|
|
3
|
+
"version": "53.1.0",
|
|
4
4
|
"description": "React runtime for metacore hosts — renders addon contributions dynamically",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -37,8 +37,8 @@
|
|
|
37
37
|
"zod": "^4.3.0"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|
|
40
|
-
"@asteby/metacore-sdk": "^3.
|
|
41
|
-
"@asteby/metacore-ui": "^2.
|
|
40
|
+
"@asteby/metacore-sdk": "^3.16.0",
|
|
41
|
+
"@asteby/metacore-ui": "^2.29.0",
|
|
42
42
|
"@tanstack/react-query": ">=5",
|
|
43
43
|
"@tanstack/react-router": ">=1.100",
|
|
44
44
|
"@tanstack/react-table": ">=8",
|
|
@@ -69,8 +69,8 @@
|
|
|
69
69
|
}
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
|
-
"@asteby/metacore-sdk": "3.
|
|
73
|
-
"@asteby/metacore-ui": "2.
|
|
72
|
+
"@asteby/metacore-sdk": "3.16.0",
|
|
73
|
+
"@asteby/metacore-ui": "2.29.0",
|
|
74
74
|
"@tanstack/react-query": "^5.95.0",
|
|
75
75
|
"@tanstack/react-router": "^1.168.0",
|
|
76
76
|
"@tanstack/react-table": "^8.20.0",
|
|
@@ -43,6 +43,72 @@ describe('createHostApi', () => {
|
|
|
43
43
|
expect(client.get).toHaveBeenLastCalledWith('/plain')
|
|
44
44
|
})
|
|
45
45
|
|
|
46
|
+
it('responseType blob: asks the transport for a Blob and resolves to it, nothing else', async () => {
|
|
47
|
+
const pdf = new Blob(['%PDF-1.4'], { type: 'application/pdf' })
|
|
48
|
+
const client = axiosLike()
|
|
49
|
+
;(client.get as ReturnType<typeof vi.fn>).mockResolvedValueOnce({
|
|
50
|
+
data: pdf,
|
|
51
|
+
headers: { 'content-type': 'application/pdf' },
|
|
52
|
+
config: { headers: { Authorization: TOKEN } },
|
|
53
|
+
})
|
|
54
|
+
const api = createHostApi(client, { batchGets: true })
|
|
55
|
+
const out = await api.get<Blob>('/data/WarehouseOrder/1/documents/ticket.pdf', { responseType: 'blob' })
|
|
56
|
+
expect(out).toBe(pdf)
|
|
57
|
+
expect(client.get).toHaveBeenCalledWith('/data/WarehouseOrder/1/documents/ticket.pdf', { responseType: 'blob' })
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('responseType blob works on post and wraps raw bytes in a Blob', async () => {
|
|
61
|
+
const client = axiosLike()
|
|
62
|
+
;(client.post as ReturnType<typeof vi.fn>).mockResolvedValueOnce({ data: new Uint8Array([1, 2, 3]) })
|
|
63
|
+
const out = await createHostApi(client).post<Blob>('/export', { a: 1 }, { responseType: 'blob' })
|
|
64
|
+
expect(out).toBeInstanceOf(Blob)
|
|
65
|
+
expect(out.size).toBe(3)
|
|
66
|
+
expect(client.post).toHaveBeenCalledWith('/export', { a: 1 }, { responseType: 'blob' })
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
it('responseType blob: a non-binary success body is rejected', async () => {
|
|
70
|
+
const client = axiosLike()
|
|
71
|
+
await expect(
|
|
72
|
+
createHostApi(client).get('/x', { responseType: 'blob' }),
|
|
73
|
+
).rejects.toBeInstanceOf(HostApiError)
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it('responseType blob: error bodies delivered as Blob are decoded to code/message and never leak the token', async () => {
|
|
77
|
+
const client = axiosLike()
|
|
78
|
+
;(client.get as ReturnType<typeof vi.fn>).mockRejectedValueOnce({
|
|
79
|
+
message: 'boom',
|
|
80
|
+
config: { headers: { Authorization: TOKEN } },
|
|
81
|
+
response: {
|
|
82
|
+
status: 409,
|
|
83
|
+
data: new Blob([JSON.stringify({ code: 'invalid_state', message: 'No imprimible' })]),
|
|
84
|
+
},
|
|
85
|
+
})
|
|
86
|
+
const err = await createHostApi(client)
|
|
87
|
+
.get('/doc.pdf', { responseType: 'blob' })
|
|
88
|
+
.catch((e) => e)
|
|
89
|
+
expect(err).toBeInstanceOf(HostApiError)
|
|
90
|
+
expect(err.status).toBe(409)
|
|
91
|
+
expect(err.code).toBe('invalid_state')
|
|
92
|
+
expect(err.message).toBe('No imprimible')
|
|
93
|
+
expect(JSON.stringify(err)).not.toContain('secret-jwt')
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
it('responseType blob: a non-JSON error blob falls back to the status message', async () => {
|
|
97
|
+
const client = axiosLike()
|
|
98
|
+
;(client.get as ReturnType<typeof vi.fn>).mockRejectedValueOnce({
|
|
99
|
+
response: { status: 500, data: new Blob(['<html>'])},
|
|
100
|
+
})
|
|
101
|
+
const err = await createHostApi(client).get('/doc.pdf', { responseType: 'blob' }).catch((e) => e)
|
|
102
|
+
expect(err.status).toBe(500)
|
|
103
|
+
expect(err.message).toBe('Request failed with status 500')
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
it('json (default) requests are unchanged: no responseType forwarded', async () => {
|
|
107
|
+
const client = axiosLike()
|
|
108
|
+
await createHostApi(client).get('/x', { responseType: 'json' })
|
|
109
|
+
expect(client.get).toHaveBeenCalledWith('/x', {})
|
|
110
|
+
})
|
|
111
|
+
|
|
46
112
|
it('posts capability queries to the host endpoint', async () => {
|
|
47
113
|
const client = axiosLike()
|
|
48
114
|
const api = createHostApi(client, { capabilityPath: '/caps' })
|
package/src/host-api.ts
CHANGED
|
@@ -81,15 +81,37 @@ function toConfig(opts?: HostApiRequestOptions): Record<string, unknown> | undef
|
|
|
81
81
|
const config: Record<string, unknown> = {}
|
|
82
82
|
if (opts.params) config.params = opts.params
|
|
83
83
|
if (opts.signal) config.signal = opts.signal
|
|
84
|
+
if (opts.responseType === 'blob') config.responseType = 'blob'
|
|
84
85
|
return config
|
|
85
86
|
}
|
|
86
87
|
|
|
87
|
-
|
|
88
|
+
// With `responseType: 'blob'` axios also delivers the error body as a Blob.
|
|
89
|
+
// Decode it back to JSON so `code`/`message` survive; non-JSON bodies are dropped.
|
|
90
|
+
async function decodeBlobError(e: unknown): Promise<unknown> {
|
|
91
|
+
const resp = (e as AxiosLikeError | null)?.response
|
|
92
|
+
if (!resp || typeof Blob === 'undefined' || !(resp.data instanceof Blob)) return e
|
|
93
|
+
let data: unknown
|
|
94
|
+
try {
|
|
95
|
+
data = JSON.parse(await resp.data.text())
|
|
96
|
+
} catch {
|
|
97
|
+
data = undefined
|
|
98
|
+
}
|
|
99
|
+
return { ...(e as object), response: { ...resp, data } }
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async function bodyOf<T>(p: Promise<{ data: unknown }>, opts?: HostApiRequestOptions): Promise<T> {
|
|
88
103
|
try {
|
|
89
104
|
const res = await p
|
|
105
|
+
if (opts?.responseType === 'blob') {
|
|
106
|
+
const d = res.data
|
|
107
|
+
if (typeof Blob !== 'undefined' && d instanceof Blob) return d as T
|
|
108
|
+
// Some transports hand back raw bytes; normalize so callers always get a Blob.
|
|
109
|
+
if (d instanceof ArrayBuffer || ArrayBuffer.isView(d)) return new Blob([d as BlobPart]) as T
|
|
110
|
+
throw new HostApiError('Expected a binary response', 0)
|
|
111
|
+
}
|
|
90
112
|
return res.data as T
|
|
91
113
|
} catch (e) {
|
|
92
|
-
throw toHostApiError(e)
|
|
114
|
+
throw toHostApiError(opts?.responseType === 'blob' ? await decodeBlobError(e) : e)
|
|
93
115
|
}
|
|
94
116
|
}
|
|
95
117
|
|
|
@@ -106,20 +128,21 @@ export function createHostApi(client: ApiClient, options: CreateHostApiOptions =
|
|
|
106
128
|
return Object.freeze({
|
|
107
129
|
get: <T,>(path: string, opts?: HostApiRequestOptions) => {
|
|
108
130
|
const args = withConfig([path], opts) as [string, { params?: Record<string, unknown> }?]
|
|
109
|
-
|
|
131
|
+
const batch = options.batchGets && opts?.responseType !== 'blob'
|
|
132
|
+
return bodyOf<T>(batch ? batchGet(client, ...args) : client.get(...args), opts)
|
|
110
133
|
},
|
|
111
134
|
post: <T,>(path: string, body?: unknown, opts?: HostApiRequestOptions) =>
|
|
112
|
-
bodyOf<T>(client.post(...(withConfig([path, body], opts) as [string, unknown, unknown?]))),
|
|
135
|
+
bodyOf<T>(client.post(...(withConfig([path, body], opts) as [string, unknown, unknown?])), opts),
|
|
113
136
|
put: <T,>(path: string, body?: unknown, opts?: HostApiRequestOptions) =>
|
|
114
|
-
bodyOf<T>(client.put(...(withConfig([path, body], opts) as [string, unknown, unknown?]))),
|
|
137
|
+
bodyOf<T>(client.put(...(withConfig([path, body], opts) as [string, unknown, unknown?])), opts),
|
|
115
138
|
patch: <T,>(path: string, body?: unknown, opts?: HostApiRequestOptions) => {
|
|
116
139
|
if (typeof patch !== 'function') {
|
|
117
140
|
return Promise.reject(new HostApiError('PATCH is not supported by this host', 0))
|
|
118
141
|
}
|
|
119
|
-
return bodyOf<T>(patch.call(client, ...(withConfig([path, body], opts) as [string, unknown, unknown?])))
|
|
142
|
+
return bodyOf<T>(patch.call(client, ...(withConfig([path, body], opts) as [string, unknown, unknown?])), opts)
|
|
120
143
|
},
|
|
121
144
|
delete: <T,>(path: string, opts?: HostApiRequestOptions) =>
|
|
122
|
-
bodyOf<T>(client.delete(...(withConfig([path], opts) as [string, unknown?]))),
|
|
145
|
+
bodyOf<T>(client.delete(...(withConfig([path], opts) as [string, unknown?])), opts),
|
|
123
146
|
q: async <T,>(path: string, opts?: Pick<HostApiRequestOptions, 'params'>): Promise<HostQueryPart<T>> => {
|
|
124
147
|
try {
|
|
125
148
|
const url = path.startsWith('/') ? path : `/${path}`
|