@syncmatters/connector-sdk 1.0.44 → 1.0.45

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.
@@ -350,8 +350,10 @@ An object may describe a form for creating a field on it in the connected system
350
350
  (`upsertFieldsMeta`). Users reach it from the mapping screen ("Create field" for one field,
351
351
  "Create fields from source fields" for several), and scripts call it. The form's presence on an
352
352
  object is the capability. An object may also allow its fields to be deleted (`canDeleteFields: true`); only
353
- scripts call `deleteFields`: the UI does not, and the connector test harness checks only that
354
- `canDeleteFields` matches the test spec.
353
+ scripts call `deleteFields`, the UI does not. The connector test harness checks the form and
354
+ `canDeleteFields` against the test spec. It creates, repeats and deletes a real field, reported as
355
+ the object's `upsertFields` and `deleteFields` features, only for an object whose test module opts
356
+ in with `upsertFields.test`, because that changes the test account's schema ([09-testing.md](./09-testing.md#creating-and-deleting-fields-upsertfieldstest)).
355
357
 
356
358
  Scripts call them per object, like the data operations:
357
359
 
@@ -479,14 +481,21 @@ A row is shaped like a data upsert row: its keys are input ids.
479
481
  Before your method runs the platform refuses a row whose operation the form does not list, that
480
482
  has an unknown input, a missing mandatory input or a malformed option list, and removes inputs
481
483
  `displayIf` hides. It also refuses a row that repeats an earlier row's key (or, without a user key
482
- input, its name), so you receive each field once. Where the system assigns keys and may accept two
483
- fields with the same name, the platform refreshes the object and refuses an add whose name is
484
- already there.
485
-
486
- **Do not check for an existing field yourself.** The mapping screen refuses a key that is already
487
- in cached metadata before it calls you. A script's call is not checked against the cache, so a
488
- duplicate key reaches your method and the system rejects it. Report an add the system rejects as a
489
- duplicate as a row error with code `"already_exists"`; the platform then refreshes the object.
484
+ input, its name), so you receive each field once. The platform does not look for an existing field
485
+ before calling you: an add of a field that is already there is yours to report, as a row error with
486
+ code `"already_exists"`. The platform then refreshes the object.
487
+
488
+ - **The user chooses the key** (`isUserKey`): do not check yourself. The mapping screen refuses a
489
+ key that is already in cached metadata, but a script's call is not checked against the cache, so
490
+ a duplicate key reaches your method and the system rejects it. Report that rejection as
491
+ `"already_exists"`.
492
+ - **The system assigns keys and accepts two fields with the same name**: check for the name
493
+ yourself, in the cheapest way your API allows (a lookup by name, or the field list you already
494
+ hold), and return `"already_exists"` for a name that is there rather than creating a second
495
+ field.
496
+
497
+ The connector test round trip (`upsertFields.test`, [09-testing.md](./09-testing.md#creating-and-deleting-fields-upsertfieldstest))
498
+ verifies this by sending the same row twice.
490
499
 
491
500
  ```js
492
501
  async upsertFields({ log, id, meta, objectMeta, rows }) {
@@ -302,6 +302,81 @@ This catches a connector that keys its rows on one column and tags another, whic
302
302
  only a sync finds. A spec without `query.key` (anything generated before it existed) skips
303
303
  these checks.
304
304
 
305
+ ## Creating and deleting fields: `upsertFields.test`
306
+
307
+ The generated `upsertFields` and `deleteFields` sections are metadata checks (the form's inputs,
308
+ its key and name inputs, `canDeleteFields`), run by the **fieldMetadata** feature. To exercise
309
+ `upsertFields()` and `deleteFields()` against the test account, opt the object in from
310
+ `mod Test.mjs`. The round trip creates a real field in the connected account, so the generator
311
+ never turns it on:
312
+
313
+ ```js
314
+ // HubSpot contacts: the user chooses the key (name); groupName is mandatory, so the row names it
315
+ // even though the form has a default
316
+ objectFeatures.Contacts.upsertFields = {
317
+ ...objectFeatures.Contacts.upsertFields,
318
+ test: { row: { label: "sm_test field", name: { operation: "add", value: "sm_test_field" }, fieldType: "text", groupName: "contactinformation" } },
319
+ };
320
+ // ConnectWise PSA companies: the system assigns the field id (the form has no key input), so the row needs
321
+ // the name input (caption, maxLen 25). podId is one of the form's podId options on the test account
322
+ objectFeatures.Companies.upsertFields.test = {
323
+ row: { caption: "sm_test field", podId: "12", fieldTypeIdentifier: "Text", entryTypeIdentifier: "EntryField" },
324
+ };
325
+ ```
326
+
327
+ `row` is an `upsertFields` row keyed by the form's input ids. A user key can be given as
328
+ `{ operation: "add", value }` or as a plain string. Mandatory inputs must be in the row even when
329
+ they have a `default`. The harness appends a per-run suffix to the key value and to the name, so
330
+ each run creates a new field. The suffix is lower-case letters and digits. It tries `_<suffix>`,
331
+ then `<suffix>`, then `-<suffix>` (a space instead of `_` for the name), shortens the value to fit
332
+ the input's byte `maxLen`, and uses the first value that passes the input's `regex`. If none does,
333
+ the create fails before anything is sent.
334
+
335
+ The round trip runs on a run that tests every feature, or that selects `upsertFields` or
336
+ `deleteFields`. Either value runs the whole round trip, because the delete needs the field the
337
+ create made. A run of `fieldMetadata` alone does not run it. It runs after the object's other
338
+ tests, and the harness:
339
+
340
+ 1. calls `connection.upsertFields.<object>` with the row and expects one result, no error and a
341
+ string `id` (a numeric id is dropped by the platform). Then it reads the object's cached
342
+ metadata and expects the field at `path` (or `id`) in `queryFields` or `upsertFields`.
343
+ 2. sends the same row again and expects a row error with code `"already_exists"`. Your connector
344
+ produces it: from the system's rejection of a duplicate user key, or from its own check of the
345
+ name where the system assigns keys
346
+ ([04](./04-meta-objects-fields.md#rows-and-upsertfields)).
347
+ 3. calls `connection.deleteFields.<object>` with every id the add calls created, expects no row
348
+ error, and checks the fields are gone from the cached metadata. The delete runs whatever
349
+ happened in steps 1 and 2.
350
+
351
+ The calls go through the connection, so the platform's row checks and metadata refresh (or your
352
+ `metaUpdate`) are part of what is tested.
353
+
354
+ The delete needs `deleteFields.canDeleteFields: true` in the spec and a `deleteFields` for the
355
+ object on the connection. The generator writes `canDeleteFields` from cached metadata, so a
356
+ features module generated before the object declared it needs regenerating first. Without both,
357
+ the round trip creates nothing and both features read `Not tested`, unless the spec sets
358
+ `upsertFields.test.leaveFieldBehind: true`. With that flag each run creates a field and leaves it
359
+ in the account, and `deleteFields` reads `Not tested` with a reason naming it.
360
+
361
+ The results are two object features, each with a status, a `NotTestedReason`, a `FatalError` and
362
+ details under its own key:
363
+
364
+ - **upsertFields** covers steps 1 and 2. The first step that fails decides the status: `Failed:
365
+ upsertFields not available on the connection`, `Failed: Test row invalid`, `Failed: Create
366
+ returned an error`, `Failed: No id returned for the created field`, `Failed: Field not in cached
367
+ metadata after create`, then for the repeated add `Failed: Repeated add threw an error`,
368
+ `Failed: Repeated add was not refused` or `Failed: Repeated add was not refused with
369
+ already_exists`. A thrown error or a failed metadata read is `Failed: Fatal error during
370
+ testing`. The message behind a failure is in `upsertFieldsFatalError`.
371
+ - **deleteFields** covers step 3: `Passed`, `Failed: Delete returned an error`, `Failed: Field
372
+ still in cached metadata after delete`, `Failed: Fatal error during testing`, or `Not tested`
373
+ when no field was created or it was left behind.
374
+
375
+ An object whose spec does not set `upsertFields.test`, or a run that does not select the round
376
+ trip, has neither feature on its result, and its summary is unchanged. For an object that opts
377
+ in, a failure of either feature fails the object and the run. A `Not tested` (such as an object
378
+ that cannot delete fields) makes an object that would pass Partial.
379
+
305
380
  ## Custom checks: `customChecks()`
306
381
 
307
382
  Some tests are not shaped like an object feature: a free statement with typed parameters,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@syncmatters/connector-sdk",
3
- "version": "1.0.44",
3
+ "version": "1.0.45",
4
4
  "description": "TypeScript type definitions for the SyncMatters connector SDK (types only - connectors execute on the SyncMatters platform)",
5
5
  "types": "./index.d.ts",
6
6
  "exports": {
@@ -12,9 +12,9 @@
12
12
  "license": "MIT",
13
13
  "author": "SyncMatters",
14
14
  "homepage": "https://syncmatters.com",
15
- "typesContentHash": "79b191c22a5a3bde8d16df135ddc5db08977a9d47afa877e5a058a019c7f81a1",
15
+ "typesContentHash": "b7824973f26c6a656f1ec55fb456f957e89bdfd93310e6792f07b74df65291ea",
16
16
  "dependencies": {
17
17
  "@types/node": "*",
18
- "@syncmatters/script-api": "^1.0.46"
18
+ "@syncmatters/script-api": "^1.0.47"
19
19
  }
20
20
  }