@kivacore/addin-client 0.0.0-stage → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +438 -2
- package/dist/index.d.ts +100 -0
- package/dist/index.js +231 -0
- package/dist/index.js.map +1 -0
- package/package.json +32 -3
- package/src/core/docyrus-client.ts +271 -0
- package/src/core/types.ts +63 -0
- package/src/index.ts +8 -0
- package/tsconfig.json +9 -0
- package/tsup.config.ts +12 -0
- package/vitest.config.mts +13 -0
package/README.md
CHANGED
|
@@ -1,3 +1,439 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @kivacore/addin-client
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Client for Docyrus embedded add-ins to talk to the host app from an iframe using `postMessage`. It handles the handshake, RPC calls, method whitelisting, and event dispatching.
|
|
4
|
+
|
|
5
|
+
- ESM-only, TypeScript types included
|
|
6
|
+
- Works in browser iframes (not for Node/SSR)
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
- pnpm: `pnpm add @kivacore/addin-client`
|
|
11
|
+
- npm: `npm i @kivacore/addin-client`
|
|
12
|
+
- yarn: `yarn add @kivacore/addin-client`
|
|
13
|
+
|
|
14
|
+
Requires Node 18+ for development builds; runs in modern browsers.
|
|
15
|
+
|
|
16
|
+
## Quick Start
|
|
17
|
+
|
|
18
|
+
Create the client, await the handshake, then call host APIs. Subscribe to host-emitted events as needed.
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { createClient } from '@kivacore/addin-client';
|
|
22
|
+
|
|
23
|
+
// In production, set your host origin explicitly instead of '*'
|
|
24
|
+
const client = createClient('*');
|
|
25
|
+
|
|
26
|
+
async function main() {
|
|
27
|
+
await client.ready();
|
|
28
|
+
|
|
29
|
+
const user = await client.getUser();
|
|
30
|
+
const tenant = await client.getTenant();
|
|
31
|
+
|
|
32
|
+
// Example CRUD
|
|
33
|
+
const rec = await client.getRecord('crm', 'contacts', '123', { columns: ['id', 'email'] });
|
|
34
|
+
|
|
35
|
+
// Subscribe to host events
|
|
36
|
+
const off = client.on('record:updated', (payload) => console.log('Updated:', payload));
|
|
37
|
+
|
|
38
|
+
// Generic HTTP-like call
|
|
39
|
+
const items = await client.list<{ id: string; name: string }>(
|
|
40
|
+
'/apps/crm/datasources/contacts',
|
|
41
|
+
{ limit: 10 }
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
off();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
main().catch(console.error);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## API Overview
|
|
51
|
+
|
|
52
|
+
- `createClient(origin = '*'): DocyrusClient`
|
|
53
|
+
- Target origin for `postMessage`. Use an explicit origin in production for security.
|
|
54
|
+
- `client.ready(): Promise<void>`
|
|
55
|
+
- Resolves after handshake and method whitelist are confirmed. Rejects if required methods are missing.
|
|
56
|
+
- `client.on(eventName: string, handler: (payload: any) => void): () => void`
|
|
57
|
+
- Subscribes to host events; returns an unsubscribe function.
|
|
58
|
+
|
|
59
|
+
## Detailed API Reference
|
|
60
|
+
|
|
61
|
+
### Factory and Core
|
|
62
|
+
|
|
63
|
+
#### `createClient(origin = '*'): DocyrusClient`
|
|
64
|
+
|
|
65
|
+
Creates a new client and immediately starts the handshake with the host window (`window.parent`).
|
|
66
|
+
|
|
67
|
+
- Parameters:
|
|
68
|
+
- `origin` (string, optional): Target origin for `postMessage`. Use a specific origin in production (e.g. `https://app.example.com`). Default `'*'` for development.
|
|
69
|
+
- Returns: `DocyrusClient`
|
|
70
|
+
- Errors: none upon construction; subsequent calls will fail if the host doesn’t whitelist methods.
|
|
71
|
+
- Example:
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
import { createClient } from '@kivacore/addin-client';
|
|
75
|
+
const client = createClient('https://app.example.com');
|
|
76
|
+
await client.ready();
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
#### `client.ready(): Promise<void>`
|
|
80
|
+
|
|
81
|
+
Resolves when the host responds with `handshake_ack` and whitelists allowed methods. Rejects if required methods are missing.
|
|
82
|
+
|
|
83
|
+
- Parameters: none
|
|
84
|
+
- Returns: `Promise<void>`
|
|
85
|
+
- Errors:
|
|
86
|
+
- Missing methods in host whitelist cause rejection with a descriptive error.
|
|
87
|
+
- If the host never responds, your subsequent calls will time out (15s per call).
|
|
88
|
+
- Example:
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
await client.ready();
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
#### `client.on(eventName: string, handler: (payload: any) => void): () => void`
|
|
95
|
+
|
|
96
|
+
Subscribes to events pushed by the host.
|
|
97
|
+
|
|
98
|
+
- Parameters:
|
|
99
|
+
- `eventName` (string): Host-defined event name (e.g. `record:updated`).
|
|
100
|
+
- `handler` ((payload: any) => void): Callback invoked with event payload.
|
|
101
|
+
- Returns: `() => void` unsubscribe function.
|
|
102
|
+
- Errors: none (no-op if event is never emitted).
|
|
103
|
+
- Example:
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
const off = client.on('record:updated', (p) => console.log(p));
|
|
107
|
+
// later
|
|
108
|
+
off();
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Host Methods
|
|
112
|
+
|
|
113
|
+
Methods proxied to the host application. All require `await client.ready()` first and are subject to method whitelisting and a 15s RPC timeout.
|
|
114
|
+
|
|
115
|
+
#### `client.getUser(): Promise<User>`
|
|
116
|
+
|
|
117
|
+
Fetches the current user from the host.
|
|
118
|
+
|
|
119
|
+
- Parameters: none
|
|
120
|
+
- Returns: `Promise<User>`
|
|
121
|
+
- Errors:
|
|
122
|
+
- Not whitelisted by host
|
|
123
|
+
- RPC timeout (15s)
|
|
124
|
+
- Host-reported error
|
|
125
|
+
- Example:
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
const user = await client.getUser();
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
#### `client.getTenant(): Promise<Tenant>`
|
|
132
|
+
|
|
133
|
+
Fetches the current tenant.
|
|
134
|
+
|
|
135
|
+
- Parameters: none
|
|
136
|
+
- Returns: `Promise<Tenant>`
|
|
137
|
+
- Errors: same as above
|
|
138
|
+
- Example:
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
const tenant = await client.getTenant();
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
#### `client.getRecord(appSlug: string, dataSourceSlug: string, recordId: string, options: { columns?: string[] }): Promise<Record<string, any>>`
|
|
145
|
+
|
|
146
|
+
Fetches a single record.
|
|
147
|
+
|
|
148
|
+
- Parameters:
|
|
149
|
+
- `appSlug` (string): Application slug.
|
|
150
|
+
- `dataSourceSlug` (string): Data source slug within the app.
|
|
151
|
+
- `recordId` (string): Record identifier.
|
|
152
|
+
- `options` (object, optional):
|
|
153
|
+
- `columns?` (string[]): Subset of columns to fetch.
|
|
154
|
+
- Returns: `Promise<Record<string, any>>`
|
|
155
|
+
- Errors: same as above
|
|
156
|
+
- Example:
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
const rec = await client.getRecord('crm', 'contacts', '123', { columns: ['id', 'email'] });
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
#### `client.getRecords(appSlug: string, dataSourceSlug: string, options: { columns?: string[]; limit?: number; offset?: number; orderBy?: string; orderByDirection?: 'asc' | 'desc'; filters?: string[] }): Promise<Record<string, any>[]>`
|
|
163
|
+
|
|
164
|
+
Lists records with pagination, sorting, and filtering.
|
|
165
|
+
|
|
166
|
+
- Parameters:
|
|
167
|
+
- `appSlug` (string)
|
|
168
|
+
- `dataSourceSlug` (string)
|
|
169
|
+
- `options` (object):
|
|
170
|
+
- `columns?` (string[])
|
|
171
|
+
- `limit?` (number)
|
|
172
|
+
- `offset?` (number)
|
|
173
|
+
- `orderBy?` (string)
|
|
174
|
+
- `orderByDirection?` ('asc' | 'desc')
|
|
175
|
+
- `filters?` (string[]): Host-defined filter expressions.
|
|
176
|
+
- Returns: `Promise<Record<string, any>[]>`
|
|
177
|
+
- Errors: same as above
|
|
178
|
+
- Example:
|
|
179
|
+
|
|
180
|
+
```ts
|
|
181
|
+
const list = await client.getRecords('crm', 'contacts', { limit: 20, orderBy: 'createdAt', orderByDirection: 'desc' });
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
#### `client.insertRecord(appSlug: string, dataSourceSlug: string, data: Record<string, any>): Promise<Record<string, any>>`
|
|
185
|
+
|
|
186
|
+
Creates a new record.
|
|
187
|
+
|
|
188
|
+
- Parameters:
|
|
189
|
+
- `appSlug` (string)
|
|
190
|
+
- `dataSourceSlug` (string)
|
|
191
|
+
- `data` (object): Plain JSON-serializable values.
|
|
192
|
+
- Returns: `Promise<Record<string, any>>`
|
|
193
|
+
- Errors: same as above
|
|
194
|
+
- Example:
|
|
195
|
+
|
|
196
|
+
```ts
|
|
197
|
+
const created = await client.insertRecord('crm', 'contacts', { email: 'a@b.co', name: 'Ada' });
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
#### `client.updateRecord(appSlug: string, dataSourceSlug: string, recordId: string, data: Record<string, any>): Promise<Record<string, any>>`
|
|
201
|
+
|
|
202
|
+
Updates an existing record.
|
|
203
|
+
|
|
204
|
+
- Parameters:
|
|
205
|
+
- `appSlug` (string)
|
|
206
|
+
- `dataSourceSlug` (string)
|
|
207
|
+
- `recordId` (string)
|
|
208
|
+
- `data` (object)
|
|
209
|
+
- Returns: `Promise<Record<string, any>>`
|
|
210
|
+
- Errors: same as above
|
|
211
|
+
- Example:
|
|
212
|
+
|
|
213
|
+
```ts
|
|
214
|
+
const updated = await client.updateRecord('crm', 'contacts', '123', { name: 'Ada Lovelace' });
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
#### `client.deleteRecord(appSlug: string, dataSourceSlug: string, recordId: string): Promise<void>`
|
|
218
|
+
|
|
219
|
+
Deletes a record by id.
|
|
220
|
+
|
|
221
|
+
- Parameters:
|
|
222
|
+
- `appSlug` (string)
|
|
223
|
+
- `dataSourceSlug` (string)
|
|
224
|
+
- `recordId` (string)
|
|
225
|
+
- Returns: `Promise<void>`
|
|
226
|
+
- Errors: same as above
|
|
227
|
+
- Example:
|
|
228
|
+
|
|
229
|
+
```ts
|
|
230
|
+
await client.deleteRecord('crm', 'contacts', '123');
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
#### `client.showRecordDetailForm(appSlug: string, dataSourceSlug: string, recordId: string, options?: Record<string, any>): Promise<void>`
|
|
234
|
+
|
|
235
|
+
Opens the host UI record detail form in view mode.
|
|
236
|
+
|
|
237
|
+
- Parameters:
|
|
238
|
+
- `appSlug` (string)
|
|
239
|
+
- `dataSourceSlug` (string)
|
|
240
|
+
- `recordId` (string)
|
|
241
|
+
- `options` (object, optional): Additional host-defined options.
|
|
242
|
+
- Returns: `Promise<void>`
|
|
243
|
+
- Errors: same as above
|
|
244
|
+
- Example:
|
|
245
|
+
|
|
246
|
+
```ts
|
|
247
|
+
await client.showRecordDetailForm(‘crm’, ‘contacts’, ‘123’);
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
#### `client.showCreateRecordForm(appSlug: string, dataSourceSlug: string, options?: Record<string, any>): Promise<void>`
|
|
251
|
+
|
|
252
|
+
Opens the host UI record form in create mode.
|
|
253
|
+
|
|
254
|
+
- Parameters:
|
|
255
|
+
- `appSlug` (string)
|
|
256
|
+
- `dataSourceSlug` (string)
|
|
257
|
+
- `options` (object, optional): Additional host-defined options.
|
|
258
|
+
- Returns: `Promise<void>`
|
|
259
|
+
- Errors: same as above
|
|
260
|
+
- Example:
|
|
261
|
+
|
|
262
|
+
```ts
|
|
263
|
+
await client.showCreateRecordForm(‘crm’, ‘contacts’);
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
#### `client.showEditRecordForm(appSlug: string, dataSourceSlug: string, recordId: string, options?: Record<string, any>): Promise<void>`
|
|
267
|
+
|
|
268
|
+
Opens the host UI record form in edit mode.
|
|
269
|
+
|
|
270
|
+
- Parameters:
|
|
271
|
+
- `appSlug` (string)
|
|
272
|
+
- `dataSourceSlug` (string)
|
|
273
|
+
- `recordId` (string)
|
|
274
|
+
- `options` (object, optional): Additional host-defined options.
|
|
275
|
+
- Returns: `Promise<void>`
|
|
276
|
+
- Errors: same as above
|
|
277
|
+
- Example:
|
|
278
|
+
|
|
279
|
+
```ts
|
|
280
|
+
await client.showEditRecordForm(‘crm’, ‘contacts’, ‘123’);
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
#### `client.runAggregateQuery(appSlug: string, dataSourceSlug: string, rows: any[], calculations: any[]): Promise<Record<string, any>[]>`
|
|
284
|
+
|
|
285
|
+
Runs an aggregate query; the `rows` and `calculations` structures are host-defined.
|
|
286
|
+
|
|
287
|
+
- Parameters:
|
|
288
|
+
- `appSlug` (string)
|
|
289
|
+
- `dataSourceSlug` (string)
|
|
290
|
+
- `rows` (any[]): Grouping spec
|
|
291
|
+
- `calculations` (any[]): Aggregations to compute
|
|
292
|
+
- Returns: `Promise<Record<string, any>[]>`
|
|
293
|
+
- Errors: same as above
|
|
294
|
+
- Example:
|
|
295
|
+
|
|
296
|
+
```ts
|
|
297
|
+
const result = await client.runAggregateQuery('crm', 'contacts', [{ by: 'owner' }], [{ op: 'count' }]);
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
#### `client.runCustomQuery(customQueryId: string, filters?: string[], runBalanceQuery?: boolean): Promise<Record<string, any>[]>`
|
|
301
|
+
|
|
302
|
+
Runs a predefined SQL query by its ID and returns the results.
|
|
303
|
+
|
|
304
|
+
- Parameters:
|
|
305
|
+
- `customQueryId` (string): The ID of the custom query to run.
|
|
306
|
+
- `filters` (string[], optional): Array of filter expressions to apply.
|
|
307
|
+
- `runBalanceQuery` (boolean, optional): Whether to run as a balance calculation query (if main query has an additional balance query)
|
|
308
|
+
- Returns: `Promise<Record<string, any>[]>`
|
|
309
|
+
- Errors: same as above
|
|
310
|
+
- Example:
|
|
311
|
+
|
|
312
|
+
```ts
|
|
313
|
+
const results = await client.runCustomQuery('daily-sales-report', ['date > "2023-01-01"']);
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
### HTTP-like Methods
|
|
317
|
+
|
|
318
|
+
Generic helpers for host-backed API calls. All payloads must be JSON-serializable. Use generics to type results.
|
|
319
|
+
|
|
320
|
+
#### `client.list<T = any>(path: string, params?: Record<string, any>): Promise<T[]>`
|
|
321
|
+
|
|
322
|
+
- Parameters:
|
|
323
|
+
- `path` (string): Host-resolved API path (e.g. `/apps/crm/datasources/contacts`).
|
|
324
|
+
- `params` (object, optional): Query params.
|
|
325
|
+
- Returns: `Promise<T[]>`
|
|
326
|
+
- Errors: not whitelisted, RPC timeout, host error
|
|
327
|
+
- Example:
|
|
328
|
+
|
|
329
|
+
```ts
|
|
330
|
+
const items = await client.list<{ id: string }>("/apps/crm/datasources/contacts", { limit: 10 });
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
#### `client.get<T = any>(path: string, params?: Record<string, any>): Promise<T>`
|
|
334
|
+
|
|
335
|
+
- Parameters: same as `list`
|
|
336
|
+
- Returns: `Promise<T>`
|
|
337
|
+
- Errors: same as above
|
|
338
|
+
- Example:
|
|
339
|
+
|
|
340
|
+
```ts
|
|
341
|
+
const contact = await client.get<{ id: string; email: string }>("/apps/crm/datasources/contacts/123");
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
#### `client.post<T = any>(path: string, data?: Record<string, any>): Promise<T>`
|
|
345
|
+
|
|
346
|
+
- Parameters:
|
|
347
|
+
- `path` (string)
|
|
348
|
+
- `data` (object, optional): Body payload
|
|
349
|
+
- Returns: `Promise<T>`
|
|
350
|
+
- Errors: same as above
|
|
351
|
+
- Example:
|
|
352
|
+
|
|
353
|
+
```ts
|
|
354
|
+
const created = await client.post<{ id: string }>("/apps/crm/datasources/contacts", { name: 'Ada' });
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
#### `client.patch<T = any>(path: string, data?: Record<string, any>): Promise<T>`
|
|
358
|
+
|
|
359
|
+
- Parameters: same as `post`
|
|
360
|
+
- Returns: `Promise<T>`
|
|
361
|
+
- Errors: same as above
|
|
362
|
+
- Example:
|
|
363
|
+
|
|
364
|
+
```ts
|
|
365
|
+
const updated = await client.patch("/apps/crm/datasources/contacts/123", { email: 'ada@example.com' });
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
#### `client.delete<T = any>(path: string, data?: Record<string, any>): Promise<T>`
|
|
369
|
+
|
|
370
|
+
Note: named `delete` to mirror HTTP semantics; returns a payload if host provides one.
|
|
371
|
+
|
|
372
|
+
- Parameters:
|
|
373
|
+
- `path` (string)
|
|
374
|
+
- `data` (object, optional): Some hosts support body with DELETE
|
|
375
|
+
- Returns: `Promise<T>`
|
|
376
|
+
- Errors: same as above
|
|
377
|
+
- Example:
|
|
378
|
+
|
|
379
|
+
```ts
|
|
380
|
+
await client.delete("/apps/crm/datasources/contacts/123");
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
## Events
|
|
384
|
+
|
|
385
|
+
The host can push events to the add-in. Use `client.on` to subscribe and keep the returned function to unsubscribe.
|
|
386
|
+
|
|
387
|
+
```ts
|
|
388
|
+
const off = client.on('record:updated', (payload) => {
|
|
389
|
+
// payload shape is host-defined
|
|
390
|
+
});
|
|
391
|
+
// later
|
|
392
|
+
off();
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
## Handshake, Security, and Errors
|
|
396
|
+
|
|
397
|
+
- Handshake: on creation, the client sends a `handshake` message and waits for `handshake_ack` that includes an `allowedMethods` whitelist.
|
|
398
|
+
- Whitelisting: if any required method is missing from the host’s whitelist, `client.ready()` rejects with a descriptive error.
|
|
399
|
+
- Timeouts: each RPC has a 15s timeout; the returned promise rejects if no response arrives in time.
|
|
400
|
+
- Target origin: default is `'*'` for convenience. In production, set an explicit origin in `createClient('https://app.yourdomain.com')` to prevent message spoofing.
|
|
401
|
+
- Environment: runs in the browser (iframe) and posts to `window.parent`. Not intended for SSR or Node.
|
|
402
|
+
|
|
403
|
+
## Types
|
|
404
|
+
|
|
405
|
+
```ts
|
|
406
|
+
export interface User {
|
|
407
|
+
id: string;
|
|
408
|
+
email: string;
|
|
409
|
+
firstname: string;
|
|
410
|
+
lastname: string;
|
|
411
|
+
photo: string;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
export interface Tenant {
|
|
415
|
+
id: string;
|
|
416
|
+
name: string;
|
|
417
|
+
logo: string;
|
|
418
|
+
}
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
The package exports `User`, `Tenant`, `DocyrusClient`, and `createClient`.
|
|
422
|
+
|
|
423
|
+
## Development
|
|
424
|
+
|
|
425
|
+
From the monorepo root:
|
|
426
|
+
|
|
427
|
+
- Build: `pnpm -C packages/addin-client build`
|
|
428
|
+
- Watch: `pnpm -C packages/addin-client dev`
|
|
429
|
+
- Typecheck: `pnpm -C packages/addin-client typecheck`
|
|
430
|
+
- Lint: `pnpm -C packages/addin-client lint`
|
|
431
|
+
|
|
432
|
+
Or change into the package directory:
|
|
433
|
+
|
|
434
|
+
```bash
|
|
435
|
+
cd packages/addin-client
|
|
436
|
+
pnpm dev # or: pnpm build
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
Outputs ESM to `dist/` with bundled types via `tsup`.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* User object returned by the host app
|
|
3
|
+
*/
|
|
4
|
+
interface User {
|
|
5
|
+
id: string;
|
|
6
|
+
email: string;
|
|
7
|
+
firstname: string;
|
|
8
|
+
lastname: string;
|
|
9
|
+
photo: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Tenant object returned by the host app
|
|
13
|
+
*/
|
|
14
|
+
interface Tenant {
|
|
15
|
+
id: string;
|
|
16
|
+
name: string;
|
|
17
|
+
logo: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* All host-exposed functions live here
|
|
21
|
+
*/
|
|
22
|
+
interface DocyrusHostAPI {
|
|
23
|
+
getUser(): Promise<User>;
|
|
24
|
+
getTenant(): Promise<Tenant>;
|
|
25
|
+
getRecord(appSlug: string, dataSourceSlug: string, recordId: string, options: {
|
|
26
|
+
columns?: string[];
|
|
27
|
+
}): Promise<Record<string, unknown>>;
|
|
28
|
+
getRecords(appSlug: string, dataSourceSlug: string, options: {
|
|
29
|
+
columns?: string[];
|
|
30
|
+
limit?: number;
|
|
31
|
+
offset?: number;
|
|
32
|
+
orderBy?: string;
|
|
33
|
+
orderByDirection?: 'asc' | 'desc';
|
|
34
|
+
filters?: string[];
|
|
35
|
+
}): Promise<Array<Record<string, unknown>>>;
|
|
36
|
+
insertRecord(appSlug: string, dataSourceSlug: string, data: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
37
|
+
updateRecord(appSlug: string, dataSourceSlug: string, recordId: string, data: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
38
|
+
deleteRecord(appSlug: string, dataSourceSlug: string, recordId: string): Promise<void>;
|
|
39
|
+
showRecordDetailForm(appSlug: string, dataSourceSlug: string, recordId: string, options?: Record<string, unknown>): Promise<void>;
|
|
40
|
+
showCreateRecordForm(appSlug: string, dataSourceSlug: string, options?: Record<string, unknown>): Promise<void>;
|
|
41
|
+
showEditRecordForm(appSlug: string, dataSourceSlug: string, recordId: string, options?: Record<string, unknown>): Promise<void>;
|
|
42
|
+
runAggregateQuery(appSlug: string, dataSourceSlug: string, rows: unknown[], calculations: unknown[]): Promise<Array<Record<string, unknown>>>;
|
|
43
|
+
runCustomQuery(customQueryId: string, filters?: string[], runBalanceQuery?: boolean): Promise<Array<Record<string, unknown>>>;
|
|
44
|
+
list(path: string, params?: Record<string, unknown>): Promise<Array<Record<string, unknown>>>;
|
|
45
|
+
get(path: string, params?: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
46
|
+
post(path: string, data?: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
47
|
+
put(path: string, data?: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
48
|
+
patch(path: string, data?: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
49
|
+
delete(path: string, data?: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Implementation of DocyrusHostAPI via postMessage RPC
|
|
54
|
+
*/
|
|
55
|
+
declare class DocyrusClient implements DocyrusHostAPI {
|
|
56
|
+
private pending;
|
|
57
|
+
private allowed;
|
|
58
|
+
private readyPromise;
|
|
59
|
+
private targetOrigin;
|
|
60
|
+
private events;
|
|
61
|
+
constructor(targetOrigin?: string);
|
|
62
|
+
ready(): Promise<void>;
|
|
63
|
+
getUser(): Promise<User>;
|
|
64
|
+
getTenant(): Promise<Tenant>;
|
|
65
|
+
getRecord(appSlug: string, dataSourceSlug: string, recordId: string, options: {
|
|
66
|
+
columns?: string[];
|
|
67
|
+
}): Promise<Record<string, unknown>>;
|
|
68
|
+
getRecords(appSlug: string, dataSourceSlug: string, options: {
|
|
69
|
+
columns?: string[];
|
|
70
|
+
limit?: number;
|
|
71
|
+
offset?: number;
|
|
72
|
+
orderBy?: string;
|
|
73
|
+
orderByDirection?: 'asc' | 'desc';
|
|
74
|
+
filters?: string[];
|
|
75
|
+
}): Promise<Record<string, unknown>[]>;
|
|
76
|
+
insertRecord(appSlug: string, dataSourceSlug: string, data: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
77
|
+
updateRecord(appSlug: string, dataSourceSlug: string, recordId: string, data: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
78
|
+
deleteRecord(appSlug: string, dataSourceSlug: string, recordId: string): Promise<void>;
|
|
79
|
+
showRecordDetailForm(appSlug: string, dataSourceSlug: string, recordId: string, options?: Record<string, unknown>): Promise<void>;
|
|
80
|
+
showCreateRecordForm(appSlug: string, dataSourceSlug: string, options?: Record<string, unknown>): Promise<void>;
|
|
81
|
+
showEditRecordForm(appSlug: string, dataSourceSlug: string, recordId: string, options?: Record<string, unknown>): Promise<void>;
|
|
82
|
+
runAggregateQuery(appSlug: string, dataSourceSlug: string, rows: unknown[], calculations: unknown[]): Promise<Record<string, unknown>[]>;
|
|
83
|
+
runCustomQuery(customQueryId: string, filters?: string[], runBalanceQuery?: boolean): Promise<Record<string, unknown>[]>;
|
|
84
|
+
list(path: string, params?: Record<string, unknown>): Promise<Array<Record<string, unknown>>>;
|
|
85
|
+
get(path: string, params?: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
86
|
+
post(path: string, data?: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
87
|
+
put(path: string, data?: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
88
|
+
patch(path: string, data?: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
89
|
+
delete(path: string, data?: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
90
|
+
on(eventName: string, handler: (payload: unknown) => void): () => void;
|
|
91
|
+
private call;
|
|
92
|
+
private onMessage;
|
|
93
|
+
private handshake;
|
|
94
|
+
private verifyStaticContract;
|
|
95
|
+
}
|
|
96
|
+
/** Convenience factory */
|
|
97
|
+
declare const createClient: (origin?: string) => DocyrusClient;
|
|
98
|
+
|
|
99
|
+
export { DocyrusClient, createClient, createClient as default };
|
|
100
|
+
export type { DocyrusHostAPI, Tenant, User };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
// src/core/docyrus-client.ts
|
|
2
|
+
var DocyrusClient = class _DocyrusClient {
|
|
3
|
+
pending = /* @__PURE__ */ new Map();
|
|
4
|
+
allowed = /* @__PURE__ */ new Set();
|
|
5
|
+
readyPromise;
|
|
6
|
+
targetOrigin;
|
|
7
|
+
events = /* @__PURE__ */ new Map();
|
|
8
|
+
constructor(targetOrigin = "*") {
|
|
9
|
+
this.targetOrigin = targetOrigin;
|
|
10
|
+
window.addEventListener("message", this.onMessage);
|
|
11
|
+
this.readyPromise = this.handshake();
|
|
12
|
+
}
|
|
13
|
+
ready() {
|
|
14
|
+
return this.readyPromise;
|
|
15
|
+
}
|
|
16
|
+
async getUser() {
|
|
17
|
+
await this.readyPromise;
|
|
18
|
+
return this.call("getUser");
|
|
19
|
+
}
|
|
20
|
+
async getTenant() {
|
|
21
|
+
await this.readyPromise;
|
|
22
|
+
return this.call("getTenant");
|
|
23
|
+
}
|
|
24
|
+
async getRecord(appSlug, dataSourceSlug, recordId, options) {
|
|
25
|
+
await this.readyPromise;
|
|
26
|
+
return this.call("getRecord", [
|
|
27
|
+
appSlug,
|
|
28
|
+
dataSourceSlug,
|
|
29
|
+
recordId,
|
|
30
|
+
options
|
|
31
|
+
]);
|
|
32
|
+
}
|
|
33
|
+
async getRecords(appSlug, dataSourceSlug, options) {
|
|
34
|
+
await this.readyPromise;
|
|
35
|
+
return this.call("getRecords", [
|
|
36
|
+
appSlug,
|
|
37
|
+
dataSourceSlug,
|
|
38
|
+
options
|
|
39
|
+
]);
|
|
40
|
+
}
|
|
41
|
+
async insertRecord(appSlug, dataSourceSlug, data) {
|
|
42
|
+
await this.readyPromise;
|
|
43
|
+
return this.call("insertRecord", [
|
|
44
|
+
appSlug,
|
|
45
|
+
dataSourceSlug,
|
|
46
|
+
data
|
|
47
|
+
]);
|
|
48
|
+
}
|
|
49
|
+
async updateRecord(appSlug, dataSourceSlug, recordId, data) {
|
|
50
|
+
await this.readyPromise;
|
|
51
|
+
return this.call("updateRecord", [
|
|
52
|
+
appSlug,
|
|
53
|
+
dataSourceSlug,
|
|
54
|
+
recordId,
|
|
55
|
+
data
|
|
56
|
+
]);
|
|
57
|
+
}
|
|
58
|
+
async deleteRecord(appSlug, dataSourceSlug, recordId) {
|
|
59
|
+
await this.readyPromise;
|
|
60
|
+
return this.call("deleteRecord", [
|
|
61
|
+
appSlug,
|
|
62
|
+
dataSourceSlug,
|
|
63
|
+
recordId
|
|
64
|
+
]);
|
|
65
|
+
}
|
|
66
|
+
async showRecordDetailForm(appSlug, dataSourceSlug, recordId, options = {}) {
|
|
67
|
+
await this.readyPromise;
|
|
68
|
+
return this.call("showRecordDetailForm", [
|
|
69
|
+
appSlug,
|
|
70
|
+
dataSourceSlug,
|
|
71
|
+
recordId,
|
|
72
|
+
options
|
|
73
|
+
]);
|
|
74
|
+
}
|
|
75
|
+
async showCreateRecordForm(appSlug, dataSourceSlug, options = {}) {
|
|
76
|
+
await this.readyPromise;
|
|
77
|
+
return this.call("showCreateRecordForm", [
|
|
78
|
+
appSlug,
|
|
79
|
+
dataSourceSlug,
|
|
80
|
+
options
|
|
81
|
+
]);
|
|
82
|
+
}
|
|
83
|
+
async showEditRecordForm(appSlug, dataSourceSlug, recordId, options = {}) {
|
|
84
|
+
await this.readyPromise;
|
|
85
|
+
return this.call("showEditRecordForm", [
|
|
86
|
+
appSlug,
|
|
87
|
+
dataSourceSlug,
|
|
88
|
+
recordId,
|
|
89
|
+
options
|
|
90
|
+
]);
|
|
91
|
+
}
|
|
92
|
+
async runAggregateQuery(appSlug, dataSourceSlug, rows, calculations) {
|
|
93
|
+
await this.readyPromise;
|
|
94
|
+
return this.call("runAggregateQuery", [
|
|
95
|
+
appSlug,
|
|
96
|
+
dataSourceSlug,
|
|
97
|
+
rows,
|
|
98
|
+
calculations
|
|
99
|
+
]);
|
|
100
|
+
}
|
|
101
|
+
async runCustomQuery(customQueryId, filters, runBalanceQuery) {
|
|
102
|
+
await this.readyPromise;
|
|
103
|
+
return this.call("runCustomQuery", [
|
|
104
|
+
customQueryId,
|
|
105
|
+
filters,
|
|
106
|
+
runBalanceQuery
|
|
107
|
+
]);
|
|
108
|
+
}
|
|
109
|
+
async list(path, params) {
|
|
110
|
+
await this.readyPromise;
|
|
111
|
+
return this.call("list", [path, params]);
|
|
112
|
+
}
|
|
113
|
+
async get(path, params) {
|
|
114
|
+
await this.readyPromise;
|
|
115
|
+
return this.call("get", [path, params]);
|
|
116
|
+
}
|
|
117
|
+
async post(path, data) {
|
|
118
|
+
await this.readyPromise;
|
|
119
|
+
return this.call("post", [path, data]);
|
|
120
|
+
}
|
|
121
|
+
async put(path, data) {
|
|
122
|
+
await this.readyPromise;
|
|
123
|
+
return this.call("put", [path, data]);
|
|
124
|
+
}
|
|
125
|
+
async patch(path, data) {
|
|
126
|
+
await this.readyPromise;
|
|
127
|
+
return this.call("patch", [path, data]);
|
|
128
|
+
}
|
|
129
|
+
async delete(path, data) {
|
|
130
|
+
await this.readyPromise;
|
|
131
|
+
return this.call("delete", [path, data]);
|
|
132
|
+
}
|
|
133
|
+
on(eventName, handler) {
|
|
134
|
+
if (!this.events.has(eventName)) {
|
|
135
|
+
this.events.set(eventName, /* @__PURE__ */ new Set());
|
|
136
|
+
}
|
|
137
|
+
this.events.get(eventName)?.add(handler);
|
|
138
|
+
return () => {
|
|
139
|
+
this.events.get(eventName)?.delete(handler);
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
call(method, params = []) {
|
|
143
|
+
if (!this.allowed.has(method)) {
|
|
144
|
+
return Promise.reject(new Error(`Host did not whitelist method "${method}". Check your EmbeddedAppManager config.`));
|
|
145
|
+
}
|
|
146
|
+
const id = crypto.randomUUID();
|
|
147
|
+
const safeParams = JSON.parse(JSON.stringify(params));
|
|
148
|
+
const msg = {
|
|
149
|
+
type: "rpc",
|
|
150
|
+
id,
|
|
151
|
+
method,
|
|
152
|
+
params: safeParams
|
|
153
|
+
};
|
|
154
|
+
try {
|
|
155
|
+
window.parent.postMessage(msg, this.targetOrigin);
|
|
156
|
+
} catch (error) {
|
|
157
|
+
return Promise.reject(new Error(`Failed to post message: ${error instanceof Error ? error.message : String(error)}`));
|
|
158
|
+
}
|
|
159
|
+
return new Promise((resolve, reject) => {
|
|
160
|
+
this.pending.set(id, { resolve, reject });
|
|
161
|
+
setTimeout(() => {
|
|
162
|
+
if (this.pending.has(id)) {
|
|
163
|
+
this.pending.delete(id);
|
|
164
|
+
reject(new Error(`Docyrus RPC timeout for method "${method}"`));
|
|
165
|
+
}
|
|
166
|
+
}, 15e3);
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
onMessage = (ev) => {
|
|
170
|
+
const data = ev.data;
|
|
171
|
+
switch (data.type) {
|
|
172
|
+
case "handshake_ack":
|
|
173
|
+
this.allowed = new Set(data.allowedMethods);
|
|
174
|
+
this.verifyStaticContract();
|
|
175
|
+
this.readyPromise.resolve?.();
|
|
176
|
+
break;
|
|
177
|
+
case "rpc_response": {
|
|
178
|
+
const pending = this.pending.get(data.id);
|
|
179
|
+
if (!pending) {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
if (data.error) {
|
|
183
|
+
pending.reject(new Error(data.error));
|
|
184
|
+
} else {
|
|
185
|
+
pending.resolve(data.result);
|
|
186
|
+
}
|
|
187
|
+
this.pending.delete(data.id);
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
case "event": {
|
|
191
|
+
const subs = this.events.get(data.eventName);
|
|
192
|
+
subs?.forEach((h) => h(data.payload));
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
handshake() {
|
|
198
|
+
let resolve, reject;
|
|
199
|
+
const p = new Promise((res, rej) => {
|
|
200
|
+
resolve = res;
|
|
201
|
+
reject = rej;
|
|
202
|
+
});
|
|
203
|
+
p.resolve = resolve;
|
|
204
|
+
p.reject = reject;
|
|
205
|
+
const id = crypto.randomUUID();
|
|
206
|
+
const msg = { type: "handshake", id };
|
|
207
|
+
window.parent.postMessage(msg, this.targetOrigin);
|
|
208
|
+
return p;
|
|
209
|
+
}
|
|
210
|
+
verifyStaticContract() {
|
|
211
|
+
const excludedMethods = [
|
|
212
|
+
"constructor",
|
|
213
|
+
"ready",
|
|
214
|
+
"on",
|
|
215
|
+
"call",
|
|
216
|
+
"handshake",
|
|
217
|
+
"verifyStaticContract"
|
|
218
|
+
];
|
|
219
|
+
const missing = Object.getOwnPropertyNames(_DocyrusClient.prototype).filter(
|
|
220
|
+
(m) => typeof this[m] === "function" && !excludedMethods.includes(m) && !this.allowed.has(m)
|
|
221
|
+
);
|
|
222
|
+
if (missing.length) {
|
|
223
|
+
console.warn(`[DocyrusClient] Host did not expose method(s): ${missing.join(", ")}. These methods will reject if called.`);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
var createClient = (origin = "*") => new DocyrusClient(origin);
|
|
228
|
+
|
|
229
|
+
export { DocyrusClient, createClient, createClient as default };
|
|
230
|
+
//# sourceMappingURL=index.js.map
|
|
231
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/core/docyrus-client.ts"],"names":[],"mappings":";AAOO,IAAM,aAAA,GAAN,MAAM,cAAA,CAAwC;AAAA,EAC3C,OAAA,uBAAc,GAAA,EAA6E;AAAA,EAC3F,OAAA,uBAAc,GAAA,EAAY;AAAA,EAC1B,YAAA;AAAA,EACA,YAAA;AAAA,EACA,MAAA,uBAAa,GAAA,EAAuC;AAAA,EAC5D,WAAA,CAAY,eAAe,GAAA,EAAK;AAC9B,IAAA,IAAA,CAAK,YAAA,GAAe,YAAA;AACpB,IAAA,MAAA,CAAO,gBAAA,CAAiB,SAAA,EAAW,IAAA,CAAK,SAAS,CAAA;AACjD,IAAA,IAAA,CAAK,YAAA,GAAe,KAAK,SAAA,EAAU;AAAA,EACrC;AAAA,EACA,KAAA,GAAQ;AACN,IAAA,OAAO,IAAA,CAAK,YAAA;AAAA,EACd;AAAA,EACA,MAAM,OAAA,GAAU;AACd,IAAA,MAAM,IAAA,CAAK,YAAA;AAEX,IAAA,OAAO,IAAA,CAAK,KAAW,SAAS,CAAA;AAAA,EAClC;AAAA,EACA,MAAM,SAAA,GAAY;AAChB,IAAA,MAAM,IAAA,CAAK,YAAA;AAEX,IAAA,OAAO,IAAA,CAAK,KAAa,WAAW,CAAA;AAAA,EACtC;AAAA,EACA,MAAM,SAAA,CAAU,OAAA,EAAiB,cAAA,EAAwB,UAAkB,OAAA,EAAiC;AAC1G,IAAA,MAAM,IAAA,CAAK,YAAA;AAE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CAAA,sCAAA,CAAwC,CAAA;AAAA,IAC3H;AAAA,EACF;AACF;AAGO,IAAM,eAAe,CAAC,MAAA,GAAS,GAAA,KAAQ,IAAI,cAAc,MAAM","file":"index.js","sourcesContent":["import {\n type RpcEnvelope, type User, type Tenant, type DocyrusHostAPI\n} from './types';\n\n/**\n * Implementation of DocyrusHostAPI via postMessage RPC\n */\nexport class DocyrusClient implements DocyrusHostAPI {\n private pending = new Map<string, { resolve: (v: unknown) => void; reject: (e: unknown) => void }>();\n private allowed = new Set<string>();\n private readyPromise!: Promise<void>;\n private targetOrigin: string;\n private events = new Map<string, Set<(p: unknown) => void>>();\n constructor(targetOrigin = '*') {\n this.targetOrigin = targetOrigin;\n window.addEventListener('message', this.onMessage);\n this.readyPromise = this.handshake();\n }\n ready() {\n return this.readyPromise;\n }\n async getUser() {\n await this.readyPromise;\n\n return this.call<User>('getUser');\n }\n async getTenant() {\n await this.readyPromise;\n\n return this.call<Tenant>('getTenant');\n }\n async getRecord(appSlug: string, dataSourceSlug: string, recordId: string, options: { columns?: string[] }) {\n await this.readyPromise;\n\n return this.call<Record<string, unknown>>('getRecord', [\n appSlug,\n dataSourceSlug,\n recordId,\n options\n ]);\n }\n async getRecords(appSlug: string, dataSourceSlug: string, options: {\n columns?: string[]; limit?: number; offset?: number; orderBy?: string; orderByDirection?: 'asc' | 'desc'; filters?: string[];\n }) {\n await this.readyPromise;\n\n return this.call<Array<Record<string, unknown>>>('getRecords', [\n appSlug,\n dataSourceSlug,\n options\n ]);\n }\n async insertRecord(appSlug: string, dataSourceSlug: string, data: Record<string, unknown>) {\n await this.readyPromise;\n\n return this.call<Record<string, unknown>>('insertRecord', [\n appSlug,\n dataSourceSlug,\n data\n ]);\n }\n async updateRecord(appSlug: string, dataSourceSlug: string, recordId: string, data: Record<string, unknown>) {\n await this.readyPromise;\n\n return this.call<Record<string, unknown>>('updateRecord', [\n appSlug,\n dataSourceSlug,\n recordId,\n data\n ]);\n }\n async deleteRecord(appSlug: string, dataSourceSlug: string, recordId: string) {\n await this.readyPromise;\n\n return this.call<void>('deleteRecord', [\n appSlug,\n dataSourceSlug,\n recordId\n ]);\n }\n async showRecordDetailForm(appSlug: string, dataSourceSlug: string, recordId: string, options: Record<string, unknown> = {}) {\n await this.readyPromise;\n\n return this.call<void>('showRecordDetailForm', [\n appSlug,\n dataSourceSlug,\n recordId,\n options\n ]);\n }\n async showCreateRecordForm(appSlug: string, dataSourceSlug: string, options: Record<string, unknown> = {}) {\n await this.readyPromise;\n\n return this.call<void>('showCreateRecordForm', [\n appSlug,\n dataSourceSlug,\n options\n ]);\n }\n async showEditRecordForm(appSlug: string, dataSourceSlug: string, recordId: string, options: Record<string, unknown> = {}) {\n await this.readyPromise;\n\n return this.call<void>('showEditRecordForm', [\n appSlug,\n dataSourceSlug,\n recordId,\n options\n ]);\n }\n async runAggregateQuery(appSlug: string, dataSourceSlug: string, rows: unknown[], calculations: unknown[]) {\n await this.readyPromise;\n\n return this.call<Array<Record<string, unknown>>>('runAggregateQuery', [\n appSlug,\n dataSourceSlug,\n rows,\n calculations\n ]);\n }\n async runCustomQuery(customQueryId: string, filters?: string[], runBalanceQuery?: boolean) {\n await this.readyPromise;\n\n return this.call<Array<Record<string, unknown>>>('runCustomQuery', [\n customQueryId,\n filters,\n runBalanceQuery\n ]);\n }\n async list(path: string, params?: Record<string, unknown>): Promise<Array<Record<string, unknown>>> {\n await this.readyPromise;\n\n return this.call<Array<Record<string, unknown>>>('list', [path, params]);\n }\n async get(path: string, params?: Record<string, unknown>): Promise<Record<string, unknown>> {\n await this.readyPromise;\n\n return this.call<Record<string, unknown>>('get', [path, params]);\n }\n async post(path: string, data?: Record<string, unknown>): Promise<Record<string, unknown>> {\n await this.readyPromise;\n\n return this.call<Record<string, unknown>>('post', [path, data]);\n }\n async put(path: string, data?: Record<string, unknown>): Promise<Record<string, unknown>> {\n await this.readyPromise;\n\n return this.call<Record<string, unknown>>('put', [path, data]);\n }\n async patch(path: string, data?: Record<string, unknown>): Promise<Record<string, unknown>> {\n await this.readyPromise;\n\n return this.call<Record<string, unknown>>('patch', [path, data]);\n }\n async delete(path: string, data?: Record<string, unknown>): Promise<Record<string, unknown>> {\n await this.readyPromise;\n\n return this.call<Record<string, unknown>>('delete', [path, data]);\n }\n on(eventName: string, handler: (payload: unknown) => void) {\n if (!this.events.has(eventName)) {\n this.events.set(eventName, new Set());\n }\n this.events.get(eventName)?.add(handler);\n\n return () => {\n this.events.get(eventName)?.delete(handler);\n };\n }\n private call<T>(method: string, params: unknown[] = []) {\n if (!this.allowed.has(method)) {\n return Promise.reject(new Error(`Host did not whitelist method \"${method}\". Check your EmbeddedAppManager config.`));\n }\n\n const id = crypto.randomUUID();\n const safeParams = JSON.parse(JSON.stringify(params));\n const msg: RpcEnvelope = {\n type: 'rpc', id, method, params: safeParams\n };\n\n try {\n window.parent.postMessage(msg, this.targetOrigin);\n } catch (error) {\n return Promise.reject(new Error(`Failed to post message: ${error instanceof Error ? error.message : String(error)}`));\n }\n\n return new Promise<T>((resolve, reject) => {\n this.pending.set(id, { resolve: resolve as (v: unknown) => void, reject });\n setTimeout(() => {\n if (this.pending.has(id)) {\n this.pending.delete(id);\n reject(new Error(`Docyrus RPC timeout for method \"${method}\"`));\n }\n }, 15_000);\n });\n }\n private onMessage = (ev: MessageEvent) => {\n const data = ev.data as RpcEnvelope;\n\n switch (data.type) {\n case 'handshake_ack':\n this.allowed = new Set(data.allowedMethods);\n this.verifyStaticContract();\n (this.readyPromise as unknown as { resolve?: () => void }).resolve?.();\n break;\n\n case 'rpc_response': {\n const pending = this.pending.get(data.id);\n\n if (!pending) {\n return;\n }\n if (data.error) {\n pending.reject(new Error(data.error));\n } else {\n pending.resolve(data.result);\n }\n this.pending.delete(data.id);\n break;\n }\n\n case 'event': {\n const subs = this.events.get(data.eventName);\n\n subs?.forEach(h => h(data.payload));\n break;\n }\n\n default:\n break;\n }\n };\n private handshake(): Promise<void> {\n let resolve: () => void, reject: (e: unknown) => void;\n const p = new Promise<void>((res, rej) => {\n resolve = res;\n reject = rej;\n });\n\n // @ts-expect-error attaching resolve/reject to promise for external resolution\n (p as unknown as { resolve: () => void }).resolve = resolve;\n // @ts-expect-error attaching resolve/reject to promise for external resolution\n (p as unknown as { reject: (e: unknown) => void }).reject = reject;\n\n const id = crypto.randomUUID();\n const msg: RpcEnvelope = { type: 'handshake', id };\n\n window.parent.postMessage(msg, this.targetOrigin);\n\n return p;\n }\n private verifyStaticContract() {\n const excludedMethods = [\n 'constructor',\n 'ready',\n 'on',\n 'call',\n 'handshake',\n 'verifyStaticContract'\n ];\n const missing = Object.getOwnPropertyNames(DocyrusClient.prototype).filter(\n m => typeof (this as unknown as Record<string, unknown>)[m] === 'function' && !excludedMethods.includes(m) && !this.allowed.has(m)\n );\n\n if (missing.length) {\n console.warn(`[DocyrusClient] Host did not expose method(s): ${missing.join(', ')}. These methods will reject if called.`);\n }\n }\n}\n\n/** Convenience factory */\nexport const createClient = (origin = '*') => new DocyrusClient(origin);\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kivacore/addin-client",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
|
|
3
|
+
"version": "0.2.4",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "https://github.com/Docyrus/docyrus-devkit.git",
|
|
7
|
+
"directory": "packages/addin-client"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"main": "./dist/index.js",
|
|
11
|
+
"module": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"import": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/node": "26.6.6",
|
|
21
|
+
"tsup": "9.0.0",
|
|
22
|
+
"typescript": "7.0.2",
|
|
23
|
+
"vitest": "5.1.0"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsup",
|
|
27
|
+
"dev": "tsup --watch",
|
|
28
|
+
"lint": "eslint src",
|
|
29
|
+
"format": "eslint src --fix",
|
|
30
|
+
"typecheck": "eslint src && tsc --noEmit",
|
|
31
|
+
"test": "vitest run --passWithNoTests",
|
|
32
|
+
"test:watch": "vitest",
|
|
33
|
+
"deploy": "pnpm run build && pnpm publish --access public"
|
|
34
|
+
}
|
|
6
35
|
}
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type RpcEnvelope, type User, type Tenant, type DocyrusHostAPI
|
|
3
|
+
} from './types';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Implementation of DocyrusHostAPI via postMessage RPC
|
|
7
|
+
*/
|
|
8
|
+
export class DocyrusClient implements DocyrusHostAPI {
|
|
9
|
+
private pending = new Map<string, { resolve: (v: unknown) => void; reject: (e: unknown) => void }>();
|
|
10
|
+
private allowed = new Set<string>();
|
|
11
|
+
private readyPromise!: Promise<void>;
|
|
12
|
+
private targetOrigin: string;
|
|
13
|
+
private events = new Map<string, Set<(p: unknown) => void>>();
|
|
14
|
+
constructor(targetOrigin = '*') {
|
|
15
|
+
this.targetOrigin = targetOrigin;
|
|
16
|
+
window.addEventListener('message', this.onMessage);
|
|
17
|
+
this.readyPromise = this.handshake();
|
|
18
|
+
}
|
|
19
|
+
ready() {
|
|
20
|
+
return this.readyPromise;
|
|
21
|
+
}
|
|
22
|
+
async getUser() {
|
|
23
|
+
await this.readyPromise;
|
|
24
|
+
|
|
25
|
+
return this.call<User>('getUser');
|
|
26
|
+
}
|
|
27
|
+
async getTenant() {
|
|
28
|
+
await this.readyPromise;
|
|
29
|
+
|
|
30
|
+
return this.call<Tenant>('getTenant');
|
|
31
|
+
}
|
|
32
|
+
async getRecord(appSlug: string, dataSourceSlug: string, recordId: string, options: { columns?: string[] }) {
|
|
33
|
+
await this.readyPromise;
|
|
34
|
+
|
|
35
|
+
return this.call<Record<string, unknown>>('getRecord', [
|
|
36
|
+
appSlug,
|
|
37
|
+
dataSourceSlug,
|
|
38
|
+
recordId,
|
|
39
|
+
options
|
|
40
|
+
]);
|
|
41
|
+
}
|
|
42
|
+
async getRecords(appSlug: string, dataSourceSlug: string, options: {
|
|
43
|
+
columns?: string[]; limit?: number; offset?: number; orderBy?: string; orderByDirection?: 'asc' | 'desc'; filters?: string[];
|
|
44
|
+
}) {
|
|
45
|
+
await this.readyPromise;
|
|
46
|
+
|
|
47
|
+
return this.call<Array<Record<string, unknown>>>('getRecords', [
|
|
48
|
+
appSlug,
|
|
49
|
+
dataSourceSlug,
|
|
50
|
+
options
|
|
51
|
+
]);
|
|
52
|
+
}
|
|
53
|
+
async insertRecord(appSlug: string, dataSourceSlug: string, data: Record<string, unknown>) {
|
|
54
|
+
await this.readyPromise;
|
|
55
|
+
|
|
56
|
+
return this.call<Record<string, unknown>>('insertRecord', [
|
|
57
|
+
appSlug,
|
|
58
|
+
dataSourceSlug,
|
|
59
|
+
data
|
|
60
|
+
]);
|
|
61
|
+
}
|
|
62
|
+
async updateRecord(appSlug: string, dataSourceSlug: string, recordId: string, data: Record<string, unknown>) {
|
|
63
|
+
await this.readyPromise;
|
|
64
|
+
|
|
65
|
+
return this.call<Record<string, unknown>>('updateRecord', [
|
|
66
|
+
appSlug,
|
|
67
|
+
dataSourceSlug,
|
|
68
|
+
recordId,
|
|
69
|
+
data
|
|
70
|
+
]);
|
|
71
|
+
}
|
|
72
|
+
async deleteRecord(appSlug: string, dataSourceSlug: string, recordId: string) {
|
|
73
|
+
await this.readyPromise;
|
|
74
|
+
|
|
75
|
+
return this.call<void>('deleteRecord', [
|
|
76
|
+
appSlug,
|
|
77
|
+
dataSourceSlug,
|
|
78
|
+
recordId
|
|
79
|
+
]);
|
|
80
|
+
}
|
|
81
|
+
async showRecordDetailForm(appSlug: string, dataSourceSlug: string, recordId: string, options: Record<string, unknown> = {}) {
|
|
82
|
+
await this.readyPromise;
|
|
83
|
+
|
|
84
|
+
return this.call<void>('showRecordDetailForm', [
|
|
85
|
+
appSlug,
|
|
86
|
+
dataSourceSlug,
|
|
87
|
+
recordId,
|
|
88
|
+
options
|
|
89
|
+
]);
|
|
90
|
+
}
|
|
91
|
+
async showCreateRecordForm(appSlug: string, dataSourceSlug: string, options: Record<string, unknown> = {}) {
|
|
92
|
+
await this.readyPromise;
|
|
93
|
+
|
|
94
|
+
return this.call<void>('showCreateRecordForm', [
|
|
95
|
+
appSlug,
|
|
96
|
+
dataSourceSlug,
|
|
97
|
+
options
|
|
98
|
+
]);
|
|
99
|
+
}
|
|
100
|
+
async showEditRecordForm(appSlug: string, dataSourceSlug: string, recordId: string, options: Record<string, unknown> = {}) {
|
|
101
|
+
await this.readyPromise;
|
|
102
|
+
|
|
103
|
+
return this.call<void>('showEditRecordForm', [
|
|
104
|
+
appSlug,
|
|
105
|
+
dataSourceSlug,
|
|
106
|
+
recordId,
|
|
107
|
+
options
|
|
108
|
+
]);
|
|
109
|
+
}
|
|
110
|
+
async runAggregateQuery(appSlug: string, dataSourceSlug: string, rows: unknown[], calculations: unknown[]) {
|
|
111
|
+
await this.readyPromise;
|
|
112
|
+
|
|
113
|
+
return this.call<Array<Record<string, unknown>>>('runAggregateQuery', [
|
|
114
|
+
appSlug,
|
|
115
|
+
dataSourceSlug,
|
|
116
|
+
rows,
|
|
117
|
+
calculations
|
|
118
|
+
]);
|
|
119
|
+
}
|
|
120
|
+
async runCustomQuery(customQueryId: string, filters?: string[], runBalanceQuery?: boolean) {
|
|
121
|
+
await this.readyPromise;
|
|
122
|
+
|
|
123
|
+
return this.call<Array<Record<string, unknown>>>('runCustomQuery', [
|
|
124
|
+
customQueryId,
|
|
125
|
+
filters,
|
|
126
|
+
runBalanceQuery
|
|
127
|
+
]);
|
|
128
|
+
}
|
|
129
|
+
async list(path: string, params?: Record<string, unknown>): Promise<Array<Record<string, unknown>>> {
|
|
130
|
+
await this.readyPromise;
|
|
131
|
+
|
|
132
|
+
return this.call<Array<Record<string, unknown>>>('list', [path, params]);
|
|
133
|
+
}
|
|
134
|
+
async get(path: string, params?: Record<string, unknown>): Promise<Record<string, unknown>> {
|
|
135
|
+
await this.readyPromise;
|
|
136
|
+
|
|
137
|
+
return this.call<Record<string, unknown>>('get', [path, params]);
|
|
138
|
+
}
|
|
139
|
+
async post(path: string, data?: Record<string, unknown>): Promise<Record<string, unknown>> {
|
|
140
|
+
await this.readyPromise;
|
|
141
|
+
|
|
142
|
+
return this.call<Record<string, unknown>>('post', [path, data]);
|
|
143
|
+
}
|
|
144
|
+
async put(path: string, data?: Record<string, unknown>): Promise<Record<string, unknown>> {
|
|
145
|
+
await this.readyPromise;
|
|
146
|
+
|
|
147
|
+
return this.call<Record<string, unknown>>('put', [path, data]);
|
|
148
|
+
}
|
|
149
|
+
async patch(path: string, data?: Record<string, unknown>): Promise<Record<string, unknown>> {
|
|
150
|
+
await this.readyPromise;
|
|
151
|
+
|
|
152
|
+
return this.call<Record<string, unknown>>('patch', [path, data]);
|
|
153
|
+
}
|
|
154
|
+
async delete(path: string, data?: Record<string, unknown>): Promise<Record<string, unknown>> {
|
|
155
|
+
await this.readyPromise;
|
|
156
|
+
|
|
157
|
+
return this.call<Record<string, unknown>>('delete', [path, data]);
|
|
158
|
+
}
|
|
159
|
+
on(eventName: string, handler: (payload: unknown) => void) {
|
|
160
|
+
if (!this.events.has(eventName)) {
|
|
161
|
+
this.events.set(eventName, new Set());
|
|
162
|
+
}
|
|
163
|
+
this.events.get(eventName)?.add(handler);
|
|
164
|
+
|
|
165
|
+
return () => {
|
|
166
|
+
this.events.get(eventName)?.delete(handler);
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
private call<T>(method: string, params: unknown[] = []) {
|
|
170
|
+
if (!this.allowed.has(method)) {
|
|
171
|
+
return Promise.reject(new Error(`Host did not whitelist method "${method}". Check your EmbeddedAppManager config.`));
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const id = crypto.randomUUID();
|
|
175
|
+
const safeParams = JSON.parse(JSON.stringify(params));
|
|
176
|
+
const msg: RpcEnvelope = {
|
|
177
|
+
type: 'rpc', id, method, params: safeParams
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
try {
|
|
181
|
+
window.parent.postMessage(msg, this.targetOrigin);
|
|
182
|
+
} catch (error) {
|
|
183
|
+
return Promise.reject(new Error(`Failed to post message: ${error instanceof Error ? error.message : String(error)}`));
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return new Promise<T>((resolve, reject) => {
|
|
187
|
+
this.pending.set(id, { resolve: resolve as (v: unknown) => void, reject });
|
|
188
|
+
setTimeout(() => {
|
|
189
|
+
if (this.pending.has(id)) {
|
|
190
|
+
this.pending.delete(id);
|
|
191
|
+
reject(new Error(`Docyrus RPC timeout for method "${method}"`));
|
|
192
|
+
}
|
|
193
|
+
}, 15_000);
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
private onMessage = (ev: MessageEvent) => {
|
|
197
|
+
const data = ev.data as RpcEnvelope;
|
|
198
|
+
|
|
199
|
+
switch (data.type) {
|
|
200
|
+
case 'handshake_ack':
|
|
201
|
+
this.allowed = new Set(data.allowedMethods);
|
|
202
|
+
this.verifyStaticContract();
|
|
203
|
+
(this.readyPromise as unknown as { resolve?: () => void }).resolve?.();
|
|
204
|
+
break;
|
|
205
|
+
|
|
206
|
+
case 'rpc_response': {
|
|
207
|
+
const pending = this.pending.get(data.id);
|
|
208
|
+
|
|
209
|
+
if (!pending) {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
if (data.error) {
|
|
213
|
+
pending.reject(new Error(data.error));
|
|
214
|
+
} else {
|
|
215
|
+
pending.resolve(data.result);
|
|
216
|
+
}
|
|
217
|
+
this.pending.delete(data.id);
|
|
218
|
+
break;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
case 'event': {
|
|
222
|
+
const subs = this.events.get(data.eventName);
|
|
223
|
+
|
|
224
|
+
subs?.forEach(h => h(data.payload));
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
default:
|
|
229
|
+
break;
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
private handshake(): Promise<void> {
|
|
233
|
+
let resolve: () => void, reject: (e: unknown) => void;
|
|
234
|
+
const p = new Promise<void>((res, rej) => {
|
|
235
|
+
resolve = res;
|
|
236
|
+
reject = rej;
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
// @ts-expect-error attaching resolve/reject to promise for external resolution
|
|
240
|
+
(p as unknown as { resolve: () => void }).resolve = resolve;
|
|
241
|
+
// @ts-expect-error attaching resolve/reject to promise for external resolution
|
|
242
|
+
(p as unknown as { reject: (e: unknown) => void }).reject = reject;
|
|
243
|
+
|
|
244
|
+
const id = crypto.randomUUID();
|
|
245
|
+
const msg: RpcEnvelope = { type: 'handshake', id };
|
|
246
|
+
|
|
247
|
+
window.parent.postMessage(msg, this.targetOrigin);
|
|
248
|
+
|
|
249
|
+
return p;
|
|
250
|
+
}
|
|
251
|
+
private verifyStaticContract() {
|
|
252
|
+
const excludedMethods = [
|
|
253
|
+
'constructor',
|
|
254
|
+
'ready',
|
|
255
|
+
'on',
|
|
256
|
+
'call',
|
|
257
|
+
'handshake',
|
|
258
|
+
'verifyStaticContract'
|
|
259
|
+
];
|
|
260
|
+
const missing = Object.getOwnPropertyNames(DocyrusClient.prototype).filter(
|
|
261
|
+
m => typeof (this as unknown as Record<string, unknown>)[m] === 'function' && !excludedMethods.includes(m) && !this.allowed.has(m)
|
|
262
|
+
);
|
|
263
|
+
|
|
264
|
+
if (missing.length) {
|
|
265
|
+
console.warn(`[DocyrusClient] Host did not expose method(s): ${missing.join(', ')}. These methods will reject if called.`);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** Convenience factory */
|
|
271
|
+
export const createClient = (origin = '*') => new DocyrusClient(origin);
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* ------------------------------------------------------------ *
|
|
3
|
+
* Docyrus embedded client - Type definitions
|
|
4
|
+
* ------------------------------------------------------------
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export type RpcEnvelope<T = unknown>
|
|
8
|
+
= | { type: 'handshake'; id: string }
|
|
9
|
+
| { type: 'handshake_ack'; id: string; allowedMethods: string[] }
|
|
10
|
+
| {
|
|
11
|
+
type: 'rpc'; id: string; method: string; params: unknown[];
|
|
12
|
+
}
|
|
13
|
+
| {
|
|
14
|
+
type: 'rpc_response'; id: string; result?: T; error?: string;
|
|
15
|
+
}
|
|
16
|
+
| { type: 'event'; eventName: string; payload: unknown };
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* User object returned by the host app
|
|
20
|
+
*/
|
|
21
|
+
export interface User {
|
|
22
|
+
id: string;
|
|
23
|
+
email: string;
|
|
24
|
+
firstname: string;
|
|
25
|
+
lastname: string;
|
|
26
|
+
photo: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Tenant object returned by the host app
|
|
31
|
+
*/
|
|
32
|
+
export interface Tenant {
|
|
33
|
+
id: string;
|
|
34
|
+
name: string;
|
|
35
|
+
logo: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* All host-exposed functions live here
|
|
40
|
+
*/
|
|
41
|
+
export interface DocyrusHostAPI {
|
|
42
|
+
getUser(): Promise<User>;
|
|
43
|
+
getTenant(): Promise<Tenant>;
|
|
44
|
+
getRecord(appSlug: string, dataSourceSlug: string, recordId: string, options: { columns?: string[] }): Promise<Record<string, unknown>>;
|
|
45
|
+
getRecords(appSlug: string, dataSourceSlug: string, options: {
|
|
46
|
+
columns?: string[]; limit?: number; offset?: number; orderBy?: string; orderByDirection?: 'asc' | 'desc'; filters?: string[];
|
|
47
|
+
}): Promise<Array<Record<string, unknown>>>;
|
|
48
|
+
insertRecord(appSlug: string, dataSourceSlug: string, data: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
49
|
+
updateRecord(appSlug: string, dataSourceSlug: string, recordId: string, data: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
50
|
+
deleteRecord(appSlug: string, dataSourceSlug: string, recordId: string): Promise<void>;
|
|
51
|
+
showRecordDetailForm(appSlug: string, dataSourceSlug: string, recordId: string, options?: Record<string, unknown>): Promise<void>;
|
|
52
|
+
showCreateRecordForm(appSlug: string, dataSourceSlug: string, options?: Record<string, unknown>): Promise<void>;
|
|
53
|
+
showEditRecordForm(appSlug: string, dataSourceSlug: string, recordId: string, options?: Record<string, unknown>): Promise<void>;
|
|
54
|
+
runAggregateQuery(appSlug: string, dataSourceSlug: string, rows: unknown[], calculations: unknown[]): Promise<Array<Record<string, unknown>>>;
|
|
55
|
+
runCustomQuery(customQueryId: string, filters?: string[], runBalanceQuery?: boolean): Promise<Array<Record<string, unknown>>>;
|
|
56
|
+
// HTTP-like methods for API operations
|
|
57
|
+
list(path: string, params?: Record<string, unknown>): Promise<Array<Record<string, unknown>>>;
|
|
58
|
+
get(path: string, params?: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
59
|
+
post(path: string, data?: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
60
|
+
put(path: string, data?: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
61
|
+
patch(path: string, data?: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
62
|
+
delete(path: string, data?: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
63
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Export types
|
|
2
|
+
export type { User, Tenant, DocyrusHostAPI } from './core/types';
|
|
3
|
+
|
|
4
|
+
// Export client
|
|
5
|
+
export { DocyrusClient, createClient } from './core/docyrus-client';
|
|
6
|
+
|
|
7
|
+
// Default export - convenience factory
|
|
8
|
+
export { createClient as default } from './core/docyrus-client';
|
package/tsconfig.json
ADDED
package/tsup.config.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { defineProject, mergeConfig } from 'vitest/config';
|
|
2
|
+
|
|
3
|
+
import sharedConfig from '../../vitest.shared.mts';
|
|
4
|
+
|
|
5
|
+
export default mergeConfig(
|
|
6
|
+
sharedConfig,
|
|
7
|
+
defineProject({
|
|
8
|
+
test: {
|
|
9
|
+
name: 'addin-client',
|
|
10
|
+
environment: 'node'
|
|
11
|
+
}
|
|
12
|
+
})
|
|
13
|
+
);
|