@shushed/helpers 0.0.263 → 0.0.264

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.
@@ -28,6 +28,42 @@ class AirtableHelper extends runtime_1.default {
28
28
  this.viewId = airtableOpts.viewId;
29
29
  }
30
30
  }
31
+ static normalizeWebhookCellValues(cellValuesByFieldId) {
32
+ const isSelectOption = (v) => v && typeof v === 'object' && !Array.isArray(v) && typeof v.id === 'string' && v.id.startsWith('sel');
33
+ const isLinkedRecord = (v) => v && typeof v === 'object' && !Array.isArray(v) && typeof v.id === 'string' && v.id.startsWith('rec');
34
+ const normalizeValue = (v) => {
35
+ if (isSelectOption(v))
36
+ return v.name;
37
+ if (isLinkedRecord(v))
38
+ return v.id;
39
+ return v;
40
+ };
41
+ const normalized = {};
42
+ for (const fieldId in cellValuesByFieldId) {
43
+ const val = cellValuesByFieldId[fieldId];
44
+ if (val && typeof val === 'object' && !Array.isArray(val) && Array.isArray(val.linkedRecordIds)) {
45
+ if (val.valuesByLinkedRecordId) {
46
+ normalized[fieldId] = val.linkedRecordIds.flatMap((id) => (val.valuesByLinkedRecordId[id] ?? []).map(normalizeValue));
47
+ }
48
+ else {
49
+ normalized[fieldId] = val.linkedRecordIds;
50
+ }
51
+ }
52
+ else if (isSelectOption(val)) {
53
+ normalized[fieldId] = val.name;
54
+ }
55
+ else if (Array.isArray(val) && val.length > 0 && isSelectOption(val[0])) {
56
+ normalized[fieldId] = val.map((opt) => opt.name);
57
+ }
58
+ else if (Array.isArray(val) && val.length > 0 && isLinkedRecord(val[0])) {
59
+ normalized[fieldId] = val.map((rec) => rec.id);
60
+ }
61
+ else {
62
+ normalized[fieldId] = val;
63
+ }
64
+ }
65
+ return normalized;
66
+ }
31
67
  static translateFields(dictionary, y) {
32
68
  const nextFields = {};
33
69
  for (const k in dictionary) {
@@ -421,7 +457,11 @@ class AirtableHelper extends runtime_1.default {
421
457
  let collectedRecords = [];
422
458
  let resp = null;
423
459
  try {
424
- resp = await fetch(`https://api.airtable.com/v0/bases/${this.baseId}/webhooks/${input.webhook.id}/payloads`, {
460
+ const payloadsUrl = new URL(`https://api.airtable.com/v0/bases/${this.baseId}/webhooks/${input.webhook.id}/payloads`);
461
+ if (startingCursor) {
462
+ payloadsUrl.searchParams.set('cursor', String(startingCursor));
463
+ }
464
+ resp = await fetch(payloadsUrl.toString(), {
425
465
  method: 'GET',
426
466
  headers: {
427
467
  'Authorization': 'Bearer ' + this.apiKey,
@@ -450,11 +490,11 @@ class AirtableHelper extends runtime_1.default {
450
490
  this.logging.log(`Received ${res.payloads.length} records from the ${scope}.`);
451
491
  for (const payload of res.payloads) {
452
492
  const changes = this.viewId
453
- ? payload.changedViewsById?.[this.viewId]
493
+ ? payload.changedTablesById?.[this.tableId]?.changedViewsById?.[this.viewId]
454
494
  : payload.changedTablesById?.[this.tableId];
455
495
  const airtableCreated = Object.entries(changes?.createdRecordsById || {}).map(([k, v]) => ({
456
496
  id: k,
457
- fields: AirtableHelper.translateFields(this.dictionary, { fields: v.cellValuesByFieldId || {} }).fields,
497
+ fields: AirtableHelper.translateFields(this.dictionary, { fields: AirtableHelper.normalizeWebhookCellValues(v.cellValuesByFieldId || {}) }).fields,
458
498
  created_at: v.createdTime || payload.timestamp,
459
499
  last_modified_at: v.createdTime || payload.timestamp,
460
500
  previous_fields: null
@@ -462,7 +502,7 @@ class AirtableHelper extends runtime_1.default {
462
502
  const airtableChanged = Object.entries(changes?.changedRecordsById || {}).map(([k, v]) => ({
463
503
  id: k,
464
504
  fields: AirtableHelper.translateFields(this.dictionary, {
465
- fields: Object.assign({}, v.unchanged?.cellValuesByFieldId, v.current?.cellValuesByFieldId)
505
+ fields: AirtableHelper.normalizeWebhookCellValues(Object.assign({}, v.unchanged?.cellValuesByFieldId, v.current?.cellValuesByFieldId))
466
506
  }).fields,
467
507
  last_modified_at: payload.timestamp
468
508
  }));
@@ -22,6 +22,7 @@ declare class AirtableHelper<T extends Record<string, string>, K extends keyof T
22
22
  primaryKeyWritable?: boolean;
23
23
  viewId?: string;
24
24
  });
25
+ static normalizeWebhookCellValues(cellValuesByFieldId: Record<string, any>): Record<string, any>;
25
26
  static translateFields<T extends Record<string, string>, S extends {
26
27
  fields: Record<string, any>;
27
28
  }>(dictionary: T, y: S): S & {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shushed/helpers",
3
- "version": "0.0.263",
3
+ "version": "0.0.264",
4
4
  "author": "",
5
5
  "license": "UNLICENSED",
6
6
  "description": "",