datocms-plugin-sdk 0.6.15-alpha.1 → 0.6.17

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
@@ -33,7 +33,6 @@ import {
33
33
  SettingsAreaSidebarItemGroup,
34
34
  } from './types';
35
35
  import {
36
- isInitParent,
37
36
  isOnBootParent,
38
37
  isRenderAssetSourceParent,
39
38
  isRenderConfigScreenParent,
@@ -105,50 +104,48 @@ export type FullConnectParameters = {
105
104
  onBoot: (ctx: OnBootCtx) => void;
106
105
 
107
106
  /**
108
- * 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
109
108
  * action by returning `false`
110
109
  *
111
110
  * @tag beforeHooks
112
111
  */
113
- onBeforeItemDestroy: (item: Item, ctx: OnBootCtx) => MaybePromise<boolean>;
114
-
115
- /**
116
- * This function will be called before creating a new record. You can stop the
117
- * action by returning `false`
118
- *
119
- * @tag beforeHooks
120
- */
121
- onBeforeItemCreate: (
122
- payload: ItemCreateSchema,
112
+ onBeforeItemsDestroy: (
113
+ items: Item[],
123
114
  ctx: OnBootCtx,
124
- ) => MaybePromise<ItemCreateSchema | false>;
115
+ ) => MaybePromise<boolean>;
125
116
 
126
117
  /**
127
- * This function will be called before saving a new version of a record. You
128
- * can stop the action by returning `false`
118
+ * This function will be called before publishing records. You can stop the
119
+ * action by returning `false`
129
120
  *
130
121
  * @tag beforeHooks
131
122
  */
132
- onBeforeItemUpdate: (
133
- payload: ItemUpdateSchema,
123
+ onBeforeItemsPublish: (
124
+ items: Item[],
134
125
  ctx: OnBootCtx,
135
- ) => MaybePromise<ItemUpdateSchema | false>;
126
+ ) => MaybePromise<boolean>;
136
127
 
137
128
  /**
138
- * This function will be called before publishing a record. You can stop the
129
+ * This function will be called before unpublishing records. You can stop the
139
130
  * action by returning `false`
140
131
  *
141
132
  * @tag beforeHooks
142
133
  */
143
- onBeforeItemPublish: (item: Item, ctx: OnBootCtx) => MaybePromise<boolean>;
134
+ onBeforeItemsUnpublish: (
135
+ items: Item[],
136
+ ctx: OnBootCtx,
137
+ ) => MaybePromise<boolean>;
144
138
 
145
139
  /**
146
- * This function will be called before unpublishing a record. You can stop the
147
- * 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`
148
142
  *
149
143
  * @tag beforeHooks
150
144
  */
151
- onBeforeItemUnpublish: (item: Item, ctx: OnBootCtx) => MaybePromise<boolean>;
145
+ onBeforeItemUpsert: (
146
+ createOrUpdateItemPayload: ItemUpdateSchema | ItemCreateSchema,
147
+ ctx: OnBootCtx,
148
+ ) => MaybePromise<boolean>;
152
149
 
153
150
  /**
154
151
  * Use this function to declare new tabs you want to add in the top-bar of the
@@ -346,17 +343,13 @@ function toMultifield<Result>(
346
343
  };
347
344
  }
348
345
 
349
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
350
346
  type AsyncReturnType<T extends (...args: any) => any> = T extends (
351
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
352
347
  ...args: any
353
348
  ) => Promise<infer U>
354
349
  ? U
355
- : // eslint-disable-next-line @typescript-eslint/no-explicit-any
356
- T extends (...args: any) => infer U
350
+ : T extends (...args: any) => infer U
357
351
  ? U
358
- : // eslint-disable-next-line @typescript-eslint/no-explicit-any
359
- any;
352
+ : any;
360
353
 
361
354
  function getMaxScrollHeight() {
362
355
  const elements = document.querySelectorAll('body *');
@@ -442,15 +435,15 @@ export async function connect(
442
435
  manualFieldExtensions,
443
436
  itemFormSidebarPanels,
444
437
  itemFormOutlets,
445
- onBeforeItemDestroy,
446
- onBeforeItemPublish,
447
- onBeforeItemUnpublish,
448
- onBeforeItemUpdate,
449
- onBeforeItemCreate,
450
438
  } = configuration;
451
-
452
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
453
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;
454
447
 
455
448
  const penpalConnection = connectToParent({
456
449
  methods: {
@@ -472,11 +465,6 @@ export async function connect(
472
465
  manualFieldExtensions,
473
466
  itemFormSidebarPanels,
474
467
  itemFormOutlets,
475
- onBeforeItemDestroy,
476
- onBeforeItemPublish,
477
- onBeforeItemUnpublish,
478
- onBeforeItemUpdate,
479
- onBeforeItemCreate,
480
468
  overrideFieldExtensions: toMultifield(
481
469
  configuration.overrideFieldExtensions,
482
470
  ),
@@ -486,39 +474,61 @@ export async function connect(
486
474
  customBlockStylesForStructuredTextField: toMultifield(
487
475
  configuration.customBlockStylesForStructuredTextField,
488
476
  ),
489
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
490
477
  onChange(newSettings: any) {
491
478
  if (listener) {
492
479
  listener(newSettings);
493
480
  }
494
481
  },
495
- validateManualFieldExtensionParameters:
496
- 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
+ },
497
496
  },
498
497
  });
499
498
 
500
499
  const parent: Parent = await penpalConnection.promise;
501
500
  const initialSettings = await parent.getSettings();
502
501
 
503
- if (isInitParent(parent, initialSettings)) {
504
- // Nothing to do. Parent calls the method they need.
505
- }
506
-
507
502
  if (isOnBootParent(parent, initialSettings)) {
508
503
  type Settings = AsyncReturnType<OnBootMethods['getSettings']>;
504
+ let currentSettings = initialSettings as Settings;
509
505
 
510
- const render = (settings: Settings) => {
511
- if (!configuration.onBoot) {
512
- 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;
513
517
  }
514
518
 
515
- configuration.onBoot({
519
+ return (configuration as any)[methodName](...methodArgs, {
516
520
  ...parent,
517
- ...settings,
521
+ ...currentSettings,
522
+ ...extraCtx,
518
523
  });
519
524
  };
520
525
 
521
- render(initialSettings as Settings);
526
+ if (configuration.onBoot) {
527
+ configuration.onBoot({
528
+ ...parent,
529
+ ...currentSettings,
530
+ });
531
+ }
522
532
  }
523
533
 
524
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');