@cervio/plugin-sdk 2.0.1 → 2.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/README.md +13 -0
- package/dist/index.d.ts +28 -1
- package/dist/index.js +59 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -75,6 +75,19 @@ const { capabilities } = await client.getCapabilities('clienti')
|
|
|
75
75
|
`getEntity`, `getCapabilities` and `listRecords` take the entity id **or** its
|
|
76
76
|
technical name. Never hard-code org-specific UUIDs.
|
|
77
77
|
|
|
78
|
+
Writes run with the user's permissions and the rules of the entity's form:
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
const { id } = await client.createRecord('ordini', { titolo: 'Sedie', importo: 1200 })
|
|
82
|
+
await client.updateRecord('ordini', id, { stato: 'ordinato' }) // only the fields you pass
|
|
83
|
+
await client.deleteRecord('ordini', id) // to the bin
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
A technical name is resolved to the entity id once per client. Pass
|
|
87
|
+
`expectedUpdatedAt` to `updateRecord` to fail with `etag_conflict` (409) instead of
|
|
88
|
+
overwriting a record someone saved since you read it. Declare the methods in
|
|
89
|
+
`cervio_apis` (`POST`, `PATCH`, `DELETE` on `/api/v1/entities/`).
|
|
90
|
+
|
|
78
91
|
`listRecords` (records of an entity without a view) works for organization
|
|
79
92
|
administrators only: for other users the API answers `view_required`. A plugin used
|
|
80
93
|
by everyone reads records with `listViewRecords` and counts them with
|
package/dist/index.d.ts
CHANGED
|
@@ -760,8 +760,27 @@ type ExternalProxyInput = {
|
|
|
760
760
|
headers?: Record<string, string>;
|
|
761
761
|
body?: unknown;
|
|
762
762
|
};
|
|
763
|
+
type RecordWriteOptions = {
|
|
764
|
+
/** Form whose rules (required fields, validations) apply; default: the entity's default form. */
|
|
765
|
+
formName?: string;
|
|
766
|
+
};
|
|
767
|
+
type UpdateRecordOptions = RecordWriteOptions & {
|
|
768
|
+
/**
|
|
769
|
+
* `updated_at` of the record as you read it. If someone saved it since, the update
|
|
770
|
+
* fails with `CervioApiError` code `etag_conflict` (409) instead of overwriting.
|
|
771
|
+
*/
|
|
772
|
+
expectedUpdatedAt?: string;
|
|
773
|
+
};
|
|
763
774
|
/** Bearer-authenticated client (PAT / OAuth2 access token). */
|
|
764
775
|
declare function createCervioClient(opts: CervioClientOptions): {
|
|
776
|
+
/** Creates a record with the user's permissions. `entity`: id or technical name. */
|
|
777
|
+
createRecord: (entity: string, data: Record<string, unknown>, opts?: RecordWriteOptions) => Promise<{
|
|
778
|
+
id: string;
|
|
779
|
+
}>;
|
|
780
|
+
/** Updates the given fields of a record (the others stay as they are). */
|
|
781
|
+
updateRecord: (entity: string, recordId: string, data: Record<string, unknown>, opts?: UpdateRecordOptions) => Promise<void>;
|
|
782
|
+
/** Moves a record to the bin (soft delete), with the user's permissions. */
|
|
783
|
+
deleteRecord: (entity: string, recordId: string) => Promise<void>;
|
|
765
784
|
me: () => Promise<Identity>;
|
|
766
785
|
listEntities: () => Promise<{
|
|
767
786
|
entities: Entity[];
|
|
@@ -808,6 +827,14 @@ declare function createCervioSessionClient(opts?: CervioSessionClientOptions): {
|
|
|
808
827
|
message?: string;
|
|
809
828
|
}>;
|
|
810
829
|
connectionProxy: <T = unknown>(connectionId: string, input: ExternalProxyInput) => Promise<T>;
|
|
830
|
+
/** Creates a record with the user's permissions. `entity`: id or technical name. */
|
|
831
|
+
createRecord: (entity: string, data: Record<string, unknown>, opts?: RecordWriteOptions) => Promise<{
|
|
832
|
+
id: string;
|
|
833
|
+
}>;
|
|
834
|
+
/** Updates the given fields of a record (the others stay as they are). */
|
|
835
|
+
updateRecord: (entity: string, recordId: string, data: Record<string, unknown>, opts?: UpdateRecordOptions) => Promise<void>;
|
|
836
|
+
/** Moves a record to the bin (soft delete), with the user's permissions. */
|
|
837
|
+
deleteRecord: (entity: string, recordId: string) => Promise<void>;
|
|
811
838
|
me: () => Promise<Identity>;
|
|
812
839
|
listEntities: () => Promise<{
|
|
813
840
|
entities: Entity[];
|
|
@@ -900,4 +927,4 @@ interface CervioI18n {
|
|
|
900
927
|
declare function createI18n(opts: CreateI18nOptions): CervioI18n;
|
|
901
928
|
|
|
902
929
|
export { CERVIO_UI_API_VERSION, CervioApiError, createCervioClient, createCervioSessionClient, createI18n, ensureExternalConnection, findEntityByName, getCervioUI, getCurrentTheme, getPluginContext, getTaskPluginContext, isCervioUIAvailable, isInsideCervioSandbox, markReady, onThemeChange, openExternalAuth, resolveApiBaseUrl, resolvePluginLocale, setTaskDraft, submitTaskOutcome };
|
|
903
|
-
export type { CervioClient, CervioClientOptions, CervioColorScheme, CervioHostThemeMessage, CervioI18n, CervioI18nCatalog, CervioI18nMessages, CervioPluginContext, CervioPluginRuntimeSettings, CervioSessionClient, CervioSessionClientOptions, CervioTaskDraftResult, CervioTaskOutcome, CervioTaskPluginBoot, CervioTaskPluginContextData, CervioTaskSubmitResult, CervioThemeChangeHandler, CervioThemeSnapshot, CervioUI, CervioUiActionSheetAction, CervioUiActionSheetOpenOptions, CervioUiAlertOptions, CervioUiBarcodeScanOptions, CervioUiBarcodeScanResult, CervioUiCameraOpenOptions, CervioUiConfirmOptions, CervioUiDateOpenOptions, CervioUiDialogVariant, CervioUiDomainPickOption, CervioUiEntityPickOptions, CervioUiExportDownloadOptions, CervioUiFileMeta, CervioUiFilesCaptureOptions, CervioUiFilesPickOptions, CervioUiFilesPickResult, CervioUiFilesUploadOptions, CervioUiFilesUploadResult, CervioUiFormatSnapshot, CervioUiFrameClosedEvent, CervioUiFrameMetricsEvent, CervioUiFrameOpenOptions, CervioUiFrameSize, CervioUiFrameSlot, CervioUiGroupPickOptions, CervioUiHostEvent, CervioUiLocationPickOptions, CervioUiLocationResult, CervioUiNavConfirmLeaveOptions, CervioUiPrintOpenOptions, CervioUiProgressShowOptions, CervioUiProgressUpdateOptions, CervioUiRecordPickOptions, CervioUiSelectOpenOptions, CervioUiSelectOption, CervioUiShareOpenOptions, CervioUiShareResult, CervioUiSheetAction, CervioUiSheetOpenOptions, CervioUiSurfaceClosedEvent, CervioUiSurfaceMessageEvent, CervioUiSurfaceOpenOptions, CervioUiSurfaceOpenedEvent, CervioUiTaskCompleteOptions, CervioUiTaskCompleteResult, CervioUiTaskListItem, CervioUiTaskOpenResult, CervioUiTaskOutcome, CervioUiTaskSummary, CervioUiThemeSnapshot, CervioUiToastOptions, CervioUiToastType, CervioUiUserPickOptions, CreateI18nOptions, Entity, EntityCapabilities, EntityRecord, ExternalConnectionStatus, ExternalProxyInput, Identity, ListViewRecordsOptions, ResolvePluginLocaleOptions, ViewRecordsPage };
|
|
930
|
+
export type { CervioClient, CervioClientOptions, CervioColorScheme, CervioHostThemeMessage, CervioI18n, CervioI18nCatalog, CervioI18nMessages, CervioPluginContext, CervioPluginRuntimeSettings, CervioSessionClient, CervioSessionClientOptions, CervioTaskDraftResult, CervioTaskOutcome, CervioTaskPluginBoot, CervioTaskPluginContextData, CervioTaskSubmitResult, CervioThemeChangeHandler, CervioThemeSnapshot, CervioUI, CervioUiActionSheetAction, CervioUiActionSheetOpenOptions, CervioUiAlertOptions, CervioUiBarcodeScanOptions, CervioUiBarcodeScanResult, CervioUiCameraOpenOptions, CervioUiConfirmOptions, CervioUiDateOpenOptions, CervioUiDialogVariant, CervioUiDomainPickOption, CervioUiEntityPickOptions, CervioUiExportDownloadOptions, CervioUiFileMeta, CervioUiFilesCaptureOptions, CervioUiFilesPickOptions, CervioUiFilesPickResult, CervioUiFilesUploadOptions, CervioUiFilesUploadResult, CervioUiFormatSnapshot, CervioUiFrameClosedEvent, CervioUiFrameMetricsEvent, CervioUiFrameOpenOptions, CervioUiFrameSize, CervioUiFrameSlot, CervioUiGroupPickOptions, CervioUiHostEvent, CervioUiLocationPickOptions, CervioUiLocationResult, CervioUiNavConfirmLeaveOptions, CervioUiPrintOpenOptions, CervioUiProgressShowOptions, CervioUiProgressUpdateOptions, CervioUiRecordPickOptions, CervioUiSelectOpenOptions, CervioUiSelectOption, CervioUiShareOpenOptions, CervioUiShareResult, CervioUiSheetAction, CervioUiSheetOpenOptions, CervioUiSurfaceClosedEvent, CervioUiSurfaceMessageEvent, CervioUiSurfaceOpenOptions, CervioUiSurfaceOpenedEvent, CervioUiTaskCompleteOptions, CervioUiTaskCompleteResult, CervioUiTaskListItem, CervioUiTaskOpenResult, CervioUiTaskOutcome, CervioUiTaskSummary, CervioUiThemeSnapshot, CervioUiToastOptions, CervioUiToastType, CervioUiUserPickOptions, CreateI18nOptions, Entity, EntityCapabilities, EntityRecord, ExternalConnectionStatus, ExternalProxyInput, Identity, ListViewRecordsOptions, RecordWriteOptions, ResolvePluginLocaleOptions, UpdateRecordOptions, ViewRecordsPage };
|
package/dist/index.js
CHANGED
|
@@ -134,6 +134,7 @@ function buildRecordsQuery(opts) {
|
|
|
134
134
|
const qs = params.toString();
|
|
135
135
|
return qs ? `?${qs}` : "";
|
|
136
136
|
}
|
|
137
|
+
var UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
137
138
|
function readEndpoints(request) {
|
|
138
139
|
const entityPath = (entity) => `/api/v1/entities/${encodeURIComponent(entity)}`;
|
|
139
140
|
const viewPath = (viewId) => `/api/v1/views/${encodeURIComponent(viewId)}`;
|
|
@@ -163,38 +164,84 @@ function readEndpoints(request) {
|
|
|
163
164
|
countViewRecords: (viewId) => request(`${viewPath(viewId)}/records/count`)
|
|
164
165
|
};
|
|
165
166
|
}
|
|
167
|
+
function writeEndpoints(request, getEntity) {
|
|
168
|
+
const ids = /* @__PURE__ */ new Map();
|
|
169
|
+
const entityId = (entity) => {
|
|
170
|
+
if (UUID_RE.test(entity)) return Promise.resolve(entity);
|
|
171
|
+
let id = ids.get(entity);
|
|
172
|
+
if (!id) {
|
|
173
|
+
id = getEntity(entity).then((r) => r.entity.id);
|
|
174
|
+
id.catch(() => ids.delete(entity));
|
|
175
|
+
ids.set(entity, id);
|
|
176
|
+
}
|
|
177
|
+
return id;
|
|
178
|
+
};
|
|
179
|
+
const recordsPath = async (entity) => `/api/v1/entities/${encodeURIComponent(await entityId(entity))}/records`;
|
|
180
|
+
return {
|
|
181
|
+
/** Creates a record with the user's permissions. `entity`: id or technical name. */
|
|
182
|
+
createRecord: async (entity, data, opts) => request(await recordsPath(entity), {
|
|
183
|
+
method: "POST",
|
|
184
|
+
body: { data, ...opts?.formName ? { form_name: opts.formName } : {} }
|
|
185
|
+
}),
|
|
186
|
+
/** Updates the given fields of a record (the others stay as they are). */
|
|
187
|
+
updateRecord: async (entity, recordId, data, opts) => {
|
|
188
|
+
await request(`${await recordsPath(entity)}/${encodeURIComponent(recordId)}`, {
|
|
189
|
+
method: "PATCH",
|
|
190
|
+
body: {
|
|
191
|
+
data,
|
|
192
|
+
...opts?.formName ? { form_name: opts.formName } : {},
|
|
193
|
+
...opts?.expectedUpdatedAt ? { expected_updated_at: opts.expectedUpdatedAt } : {}
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
},
|
|
197
|
+
/** Moves a record to the bin (soft delete), with the user's permissions. */
|
|
198
|
+
deleteRecord: async (entity, recordId) => {
|
|
199
|
+
await request(`${await recordsPath(entity)}/${encodeURIComponent(recordId)}`, { method: "DELETE" });
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
function jsonInit(init, headers) {
|
|
204
|
+
if (init?.body === void 0) return { method: init?.method ?? "GET", headers };
|
|
205
|
+
return {
|
|
206
|
+
method: init.method ?? "POST",
|
|
207
|
+
headers: { ...headers, "Content-Type": "application/json" },
|
|
208
|
+
body: JSON.stringify(init.body)
|
|
209
|
+
};
|
|
210
|
+
}
|
|
166
211
|
function createCervioClient(opts) {
|
|
167
212
|
const fetchImpl = opts.fetchImpl ?? fetch;
|
|
168
213
|
const baseUrl = resolveApiBaseUrl(opts.baseUrl);
|
|
169
|
-
async function request(path) {
|
|
170
|
-
const res = await fetchImpl(
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
}
|
|
175
|
-
});
|
|
214
|
+
async function request(path, init) {
|
|
215
|
+
const res = await fetchImpl(
|
|
216
|
+
`${baseUrl}${path}`,
|
|
217
|
+
jsonInit(init, { Authorization: `Bearer ${opts.token}`, Accept: "application/json" })
|
|
218
|
+
);
|
|
176
219
|
const body = await parseJsonSafe(res);
|
|
177
220
|
throwIfNotOk(res, body);
|
|
178
221
|
return body;
|
|
179
222
|
}
|
|
223
|
+
const read = readEndpoints(request);
|
|
180
224
|
return {
|
|
181
|
-
...
|
|
225
|
+
...read,
|
|
226
|
+
...writeEndpoints(request, read.getEntity)
|
|
182
227
|
};
|
|
183
228
|
}
|
|
184
229
|
function createCervioSessionClient(opts = {}) {
|
|
185
230
|
const fetchImpl = opts.fetchImpl ?? fetch;
|
|
186
231
|
const baseUrl = resolveApiBaseUrl(opts.baseUrl);
|
|
187
|
-
async function request(path) {
|
|
232
|
+
async function request(path, init) {
|
|
188
233
|
const res = await fetchImpl(`${baseUrl}${path}`, {
|
|
189
|
-
|
|
190
|
-
|
|
234
|
+
...jsonInit(init, { Accept: "application/json" }),
|
|
235
|
+
credentials: "include"
|
|
191
236
|
});
|
|
192
237
|
const body = await parseJsonSafe(res);
|
|
193
238
|
throwIfNotOk(res, body);
|
|
194
239
|
return body;
|
|
195
240
|
}
|
|
241
|
+
const read = readEndpoints(request);
|
|
196
242
|
return {
|
|
197
|
-
...
|
|
243
|
+
...read,
|
|
244
|
+
...writeEndpoints(request, read.getEntity),
|
|
198
245
|
/** External Connections - requires admin bind + `X-Cervio-Plugin-Key` (host injects on relay). */
|
|
199
246
|
connectionStatus: (connectionId) => request(
|
|
200
247
|
`/api/v1/plugins/connections/${encodeURIComponent(connectionId)}/status`
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cervio/plugin-sdk",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "SDK for Cervio plugins: CervioUI client, plugin context, /api/v1 client, theme sync, i18n, PDF viewer (./pdf) and write-gate helpers (./policy).",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cervio",
|