datocms-plugin-sdk 0.6.15-alpha.0 → 0.6.15

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/src/connect.ts CHANGED
@@ -1,5 +1,11 @@
1
1
  import connectToParent from 'penpal/lib/connectToParent';
2
- import { Field, Item, ItemType } from './SiteApiSchema';
2
+ import {
3
+ Field,
4
+ Item,
5
+ ItemCreateSchema,
6
+ ItemType,
7
+ ItemUpdateSchema,
8
+ } from './SiteApiSchema';
3
9
  import {
4
10
  AssetSource,
5
11
  ContentAreaSidebarItem,
@@ -27,7 +33,6 @@ import {
27
33
  SettingsAreaSidebarItemGroup,
28
34
  } from './types';
29
35
  import {
30
- isInitParent,
31
36
  isOnBootParent,
32
37
  isRenderAssetSourceParent,
33
38
  isRenderConfigScreenParent,
@@ -86,6 +91,8 @@ export type RenderManualFieldExtensionConfigScreenCtx =
86
91
  export type RenderConfigScreenCtx = RenderConfigScreenPropertiesAndMethods &
87
92
  SizingUtilities;
88
93
 
94
+ type MaybePromise<T> = T | Promise<T>;
95
+
89
96
  /** The full options you can pass to the `connect` function */
90
97
  export type FullConnectParameters = {
91
98
  /**
@@ -97,44 +104,48 @@ export type FullConnectParameters = {
97
104
  onBoot: (ctx: OnBootCtx) => void;
98
105
 
99
106
  /**
100
- * This function will be called before destroying a record. You can stop the
107
+ * This function will be called before destroying records. You can stop the
101
108
  * action by returning `false`
102
109
  *
103
110
  * @tag beforeHooks
104
111
  */
105
- onBeforeItemDestroy: (item: Item, ctx: OnBootCtx) => Promise<boolean>;
106
-
107
- /**
108
- * This function will be called before saving a new version of a record. You
109
- * can stop the action by returning `false`
110
- *
111
- * @tag beforeHooks
112
- */
113
- onBeforeItemSave: (item: Item, ctx: OnBootCtx) => Promise<boolean>;
112
+ onBeforeItemsDestroy: (
113
+ items: Item[],
114
+ ctx: OnBootCtx,
115
+ ) => MaybePromise<boolean>;
114
116
 
115
117
  /**
116
- * This function will be called before publishing a record. You can stop the
118
+ * This function will be called before publishing records. You can stop the
117
119
  * action by returning `false`
118
120
  *
119
121
  * @tag beforeHooks
120
122
  */
121
- onBeforeItemPublish: (item: Item, ctx: OnBootCtx) => Promise<boolean>;
123
+ onBeforeItemsPublish: (
124
+ items: Item[],
125
+ ctx: OnBootCtx,
126
+ ) => MaybePromise<boolean>;
122
127
 
123
128
  /**
124
- * This function will be called before unpublishing a record. You can stop the
129
+ * This function will be called before unpublishing records. You can stop the
125
130
  * action by returning `false`
126
131
  *
127
132
  * @tag beforeHooks
128
133
  */
129
- onBeforeItemUnpublish: (item: Item, ctx: OnBootCtx) => Promise<boolean>;
134
+ onBeforeItemsUnpublish: (
135
+ items: Item[],
136
+ ctx: OnBootCtx,
137
+ ) => MaybePromise<boolean>;
130
138
 
131
139
  /**
132
- * This function will be called before duplicating a record. You can stop the
133
- * action by returning `false`
140
+ * This function will be called before saving a new version of a record. You
141
+ * can stop the action by returning `false`
134
142
  *
135
143
  * @tag beforeHooks
136
144
  */
137
- onBeforeItemDuplicate: (item: Item, ctx: OnBootCtx) => Promise<boolean>;
145
+ onBeforeItemUpsert: (
146
+ createOrUpdateItemPayload: ItemUpdateSchema | ItemCreateSchema,
147
+ ctx: OnBootCtx,
148
+ ) => MaybePromise<boolean>;
138
149
 
139
150
  /**
140
151
  * Use this function to declare new tabs you want to add in the top-bar of the
@@ -332,17 +343,13 @@ function toMultifield<Result>(
332
343
  };
333
344
  }
334
345
 
335
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
336
346
  type AsyncReturnType<T extends (...args: any) => any> = T extends (
337
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
338
347
  ...args: any
339
348
  ) => Promise<infer U>
340
349
  ? U
341
- : // eslint-disable-next-line @typescript-eslint/no-explicit-any
342
- T extends (...args: any) => infer U
350
+ : T extends (...args: any) => infer U
343
351
  ? U
344
- : // eslint-disable-next-line @typescript-eslint/no-explicit-any
345
- any;
352
+ : any;
346
353
 
347
354
  function getMaxScrollHeight() {
348
355
  const elements = document.querySelectorAll('body *');
@@ -428,15 +435,15 @@ export async function connect(
428
435
  manualFieldExtensions,
429
436
  itemFormSidebarPanels,
430
437
  itemFormOutlets,
431
- onBeforeItemDestroy,
432
- onBeforeItemPublish,
433
- onBeforeItemUnpublish,
434
- onBeforeItemSave,
435
- onBeforeItemDuplicate,
436
438
  } = configuration;
437
-
438
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
439
439
  let listener: ((newSettings: any) => void) | null = null;
440
+ let callMethodMergingBootCtxExecutor:
441
+ | ((
442
+ methodName: string,
443
+ methodArgs: any[],
444
+ extraCtx: Record<string, any>,
445
+ ) => void)
446
+ | null = null;
440
447
 
441
448
  const penpalConnection = connectToParent({
442
449
  methods: {
@@ -458,11 +465,6 @@ export async function connect(
458
465
  manualFieldExtensions,
459
466
  itemFormSidebarPanels,
460
467
  itemFormOutlets,
461
- onBeforeItemDestroy,
462
- onBeforeItemPublish,
463
- onBeforeItemUnpublish,
464
- onBeforeItemSave,
465
- onBeforeItemDuplicate,
466
468
  overrideFieldExtensions: toMultifield(
467
469
  configuration.overrideFieldExtensions,
468
470
  ),
@@ -472,39 +474,61 @@ export async function connect(
472
474
  customBlockStylesForStructuredTextField: toMultifield(
473
475
  configuration.customBlockStylesForStructuredTextField,
474
476
  ),
475
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
476
477
  onChange(newSettings: any) {
477
478
  if (listener) {
478
479
  listener(newSettings);
479
480
  }
480
481
  },
481
- validateManualFieldExtensionParameters:
482
- configuration.validateManualFieldExtensionParameters,
482
+ callMethodMergingBootCtx(
483
+ methodName: string,
484
+ methodArgs: any[],
485
+ extraCtx: Record<string, any>,
486
+ ) {
487
+ if (!callMethodMergingBootCtxExecutor) {
488
+ return null;
489
+ }
490
+ return callMethodMergingBootCtxExecutor(
491
+ methodName,
492
+ methodArgs,
493
+ extraCtx,
494
+ );
495
+ },
483
496
  },
484
497
  });
485
498
 
486
499
  const parent: Parent = await penpalConnection.promise;
487
500
  const initialSettings = await parent.getSettings();
488
501
 
489
- if (isInitParent(parent, initialSettings)) {
490
- // Nothing to do. Parent calls the method they need.
491
- }
492
-
493
502
  if (isOnBootParent(parent, initialSettings)) {
494
503
  type Settings = AsyncReturnType<OnBootMethods['getSettings']>;
504
+ let currentSettings = initialSettings as Settings;
495
505
 
496
- const render = (settings: Settings) => {
497
- if (!configuration.onBoot) {
498
- return;
506
+ listener = (newSettings: Settings) => {
507
+ currentSettings = newSettings;
508
+ };
509
+
510
+ callMethodMergingBootCtxExecutor = (
511
+ methodName: string,
512
+ methodArgs: any[],
513
+ extraCtx: Record<string, any>,
514
+ ) => {
515
+ if (!(methodName in configuration)) {
516
+ return undefined;
499
517
  }
500
518
 
501
- configuration.onBoot({
519
+ return (configuration as any)[methodName](...methodArgs, {
502
520
  ...parent,
503
- ...settings,
521
+ ...currentSettings,
522
+ ...extraCtx,
504
523
  });
505
524
  };
506
525
 
507
- render(initialSettings as Settings);
526
+ if (configuration.onBoot) {
527
+ configuration.onBoot({
528
+ ...parent,
529
+ ...currentSettings,
530
+ });
531
+ }
508
532
  }
509
533
 
510
534
  if (isRenderPageParent(parent, initialSettings)) {
package/src/guards.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { RenderItemFormOutletMethods } from '.';
2
2
  import {
3
- InitMethods,
4
3
  OnBootMethods,
5
4
  RenderPageMethods,
6
5
  RenderFieldExtensionMethods,
@@ -18,8 +17,6 @@ function buildGuard<P extends Parent>(mode: string) {
18
17
  settings.mode === mode;
19
18
  }
20
19
 
21
- export const isInitParent = buildGuard<InitMethods>('init');
22
-
23
20
  export const isOnBootParent = buildGuard<OnBootMethods>('onBoot');
24
21
 
25
22
  export const isRenderPageParent = buildGuard<RenderPageMethods>('renderPage');