@stackbit/cms-core 0.8.4-develop.1 → 0.8.4-develop.2

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.
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.runCustomAction = exports.resolveCustomActionsById = exports.convertFieldActionToAPICustomAction = exports.convertObjectModelActionToAPICustomObjectAction = exports.convertDocumentActionToAPICustomDocumentAction = exports.getGlobalAndBulkAPIActions = exports.stripModelActions = void 0;
6
+ exports.runCustomAction = exports.resolveCustomActionsById = exports.getFieldActions = exports.getObjectFieldActionsThunk = exports.getObjectModelActionsThunk = exports.getDocumentActionsThunk = exports.getGlobalAndBulkAPIActions = exports.stripModelActions = void 0;
7
7
  const lodash_1 = __importDefault(require("lodash"));
8
8
  const sdk_1 = require("@stackbit/sdk");
9
9
  const utils_1 = require("@stackbit/utils");
@@ -33,7 +33,7 @@ function stripModelActions({ modelMap }) {
33
33
  });
34
34
  }
35
35
  exports.stripModelActions = stripModelActions;
36
- async function getGlobalAndBulkAPIActions({ stackbitConfig, customActionMap, contentSourceDataById, userLogger, pageUrl, user, locale, currentPageDocument }) {
36
+ async function getGlobalAndBulkAPIActions({ stackbitConfig, customActionRunStateMap, contentSourceDataById, userLogger, pageUrl, user, locale, currentPageDocument }) {
37
37
  if (!stackbitConfig || !Array.isArray(stackbitConfig.actions)) {
38
38
  return [];
39
39
  }
@@ -43,24 +43,15 @@ async function getGlobalAndBulkAPIActions({ stackbitConfig, customActionMap, con
43
43
  });
44
44
  return (0, utils_1.mapPromise)(stackbitConfig.actions, async (action) => {
45
45
  var _a, _b;
46
- const actionId = `config.actions.${action.name}`;
47
- const prevStoreAction = customActionMap[actionId];
48
- const storeAction = {
49
- // if configuration is updated, the new action properties will override the stored action properties
50
- ...action,
51
- actionId,
52
- label: (_a = action.label) !== null && _a !== void 0 ? _a : lodash_1.default.startCase(action.name),
53
- runningHandler: prevStoreAction === null || prevStoreAction === void 0 ? void 0 : prevStoreAction.runningHandler,
54
- lastResultState: prevStoreAction === null || prevStoreAction === void 0 ? void 0 : prevStoreAction.lastResultState
55
- };
56
- customActionMap[actionId] = storeAction;
46
+ const actionId = globalActionId(action);
47
+ const actionRunState = customActionRunStateMap[actionId];
57
48
  let state;
58
- if (storeAction.runningHandler) {
49
+ if (actionRunState === null || actionRunState === void 0 ? void 0 : actionRunState.runningHandler) {
59
50
  state = 'running';
60
51
  }
61
- else if (storeAction.state) {
52
+ else if (action.state) {
62
53
  const pageDocument = getCSIDocumentWithSourceFromDocumentSpec(currentPageDocument, configDelegate);
63
- state = await storeAction.state({
54
+ state = await action.state({
64
55
  actionId: actionId,
65
56
  currentLocale: locale,
66
57
  currentUser: user ? { name: user.name, email: user.email } : undefined,
@@ -70,152 +61,158 @@ async function getGlobalAndBulkAPIActions({ stackbitConfig, customActionMap, con
70
61
  });
71
62
  }
72
63
  else {
73
- state = (_b = storeAction.lastResultState) !== null && _b !== void 0 ? _b : 'enabled';
64
+ state = (_a = actionRunState === null || actionRunState === void 0 ? void 0 : actionRunState.lastResultState) !== null && _a !== void 0 ? _a : 'enabled';
74
65
  }
75
66
  return (0, utils_1.omitByNil)({
67
+ type: action.type,
76
68
  actionId: actionId,
77
- type: storeAction.type,
78
- name: storeAction.name,
79
- label: storeAction.label,
80
- icon: storeAction.icon,
69
+ name: action.name,
70
+ label: (_b = action.label) !== null && _b !== void 0 ? _b : lodash_1.default.startCase(action.name),
71
+ icon: action.icon,
81
72
  state: state,
82
- inputFields: storeAction.inputFields
73
+ inputFields: action.inputFields
83
74
  });
84
75
  });
85
76
  }
86
77
  exports.getGlobalAndBulkAPIActions = getGlobalAndBulkAPIActions;
87
- function convertDocumentActionToAPICustomDocumentAction({ action, customActionMap, csiDocument }) {
88
- var _a, _b;
89
- const actionId = `${csiDocument.srcType}:${csiDocument.srcProjectId}:${csiDocument.id}:${action.name}`;
90
- const prevStoreAction = customActionMap[actionId];
91
- const storeAction = {
92
- // if configuration is updated, the new action properties will override the stored action properties
93
- ...action,
94
- type: 'document',
95
- actionId,
96
- label: (_a = action.label) !== null && _a !== void 0 ? _a : lodash_1.default.startCase(action.name),
97
- runningHandler: prevStoreAction === null || prevStoreAction === void 0 ? void 0 : prevStoreAction.runningHandler,
98
- lastResultState: prevStoreAction === null || prevStoreAction === void 0 ? void 0 : prevStoreAction.lastResultState,
99
- documentSpec: {
100
- srcType: csiDocument.srcType,
101
- srcProjectId: csiDocument.srcProjectId,
102
- srcDocumentId: csiDocument.id
103
- }
104
- };
105
- customActionMap[actionId] = storeAction;
106
- let state;
107
- let needsResolving = false;
108
- if (storeAction === null || storeAction === void 0 ? void 0 : storeAction.runningHandler) {
109
- state = 'running';
78
+ function getDocumentActionsThunk({ csiDocument, model, srcType, srcProjectId, customActionRunStateMap }) {
79
+ if (!('actions' in model) || !Array.isArray(model.actions)) {
80
+ return () => undefined;
110
81
  }
111
- else if (action.state) {
112
- state = 'unknown';
113
- needsResolving = true;
114
- }
115
- else {
116
- state = (_b = storeAction === null || storeAction === void 0 ? void 0 : storeAction.lastResultState) !== null && _b !== void 0 ? _b : 'enabled';
117
- }
118
- return (0, utils_1.omitByNil)({
119
- actionId: actionId,
120
- type: 'document',
121
- name: storeAction.name,
122
- label: storeAction.label,
123
- icon: storeAction.icon,
124
- state: state,
125
- inputFields: storeAction.inputFields,
126
- needsResolving: needsResolving
82
+ const extendedDocumentActions = model.actions.map((action) => {
83
+ var _a;
84
+ const documentSpec = {
85
+ srcType: srcType,
86
+ srcProjectId: srcProjectId,
87
+ srcDocumentId: csiDocument.id
88
+ };
89
+ return {
90
+ // if configuration is updated, the new action properties will override the stored action properties
91
+ ...action,
92
+ type: 'document',
93
+ actionId: documentActionId({ ...documentSpec, actionName: action.name }),
94
+ label: (_a = action.label) !== null && _a !== void 0 ? _a : lodash_1.default.startCase(action.name),
95
+ documentSpec: documentSpec
96
+ };
97
+ });
98
+ return () => extendedDocumentActions.map((extendedAction) => {
99
+ return (0, utils_1.omitByNil)({
100
+ type: 'document',
101
+ actionId: extendedAction.actionId,
102
+ name: extendedAction.name,
103
+ label: extendedAction.label,
104
+ icon: extendedAction.icon,
105
+ inputFields: extendedAction.inputFields,
106
+ ...getAPIActionState(extendedAction, customActionRunStateMap)
107
+ });
127
108
  });
128
109
  }
129
- exports.convertDocumentActionToAPICustomDocumentAction = convertDocumentActionToAPICustomDocumentAction;
130
- function convertObjectModelActionToAPICustomObjectAction({ action, customActionMap, csiParentDocument, fieldPath }) {
131
- var _a, _b;
132
- const actionId = `${csiParentDocument.srcType}:${csiParentDocument.srcProjectId}:${csiParentDocument.id}:${fieldPath.join('.')}:${action.name}`;
133
- const prevStoreAction = customActionMap[actionId];
134
- const storeAction = {
135
- // if configuration is updated, the new action properties will override the stored action properties
136
- ...action,
137
- type: 'objectModel',
138
- actionId,
139
- label: (_a = action.label) !== null && _a !== void 0 ? _a : lodash_1.default.startCase(action.name),
140
- runningHandler: prevStoreAction === null || prevStoreAction === void 0 ? void 0 : prevStoreAction.runningHandler,
141
- lastResultState: prevStoreAction === null || prevStoreAction === void 0 ? void 0 : prevStoreAction.lastResultState,
142
- documentSpec: {
143
- srcType: csiParentDocument.srcType,
144
- srcProjectId: csiParentDocument.srcProjectId,
145
- srcDocumentId: csiParentDocument.id
146
- },
147
- fieldPath
148
- };
149
- customActionMap[actionId] = storeAction;
150
- let state;
151
- let needsResolving = false;
152
- if (storeAction === null || storeAction === void 0 ? void 0 : storeAction.runningHandler) {
153
- state = 'running';
154
- }
155
- else if (action.state) {
156
- state = 'unknown';
157
- needsResolving = true;
110
+ exports.getDocumentActionsThunk = getDocumentActionsThunk;
111
+ function getObjectModelActionsThunk({ model, csiParentDocument, srcType, srcProjectId, customActionRunStateMap, fieldPath }) {
112
+ if (!('actions' in model) || !Array.isArray(model.actions)) {
113
+ return () => undefined;
158
114
  }
159
- else {
160
- state = (_b = storeAction === null || storeAction === void 0 ? void 0 : storeAction.lastResultState) !== null && _b !== void 0 ? _b : 'enabled';
161
- }
162
- return (0, utils_1.omitByNil)({
163
- actionId: actionId,
164
- type: 'object',
165
- name: storeAction.name,
166
- label: storeAction.label,
167
- icon: storeAction.icon,
168
- state: state,
169
- inputFields: storeAction.inputFields,
170
- needsResolving: needsResolving
115
+ const extendedObjectModelActions = model.actions.map((action) => {
116
+ var _a;
117
+ const documentSpec = {
118
+ srcType: srcType,
119
+ srcProjectId: srcProjectId,
120
+ srcDocumentId: csiParentDocument.id
121
+ };
122
+ return {
123
+ // if configuration is updated, the new action properties will override the stored action properties
124
+ ...action,
125
+ type: 'objectModel',
126
+ actionId: fieldPathActionId({ ...documentSpec, fieldPath, actionName: action.name }),
127
+ label: (_a = action.label) !== null && _a !== void 0 ? _a : lodash_1.default.startCase(action.name),
128
+ documentSpec: documentSpec,
129
+ fieldPath
130
+ };
131
+ });
132
+ return () => extendedObjectModelActions.map((extendedAction) => {
133
+ return (0, utils_1.omitByNil)({
134
+ type: 'object',
135
+ actionId: extendedAction.actionId,
136
+ name: extendedAction.name,
137
+ label: extendedAction.label,
138
+ icon: extendedAction.icon,
139
+ inputFields: extendedAction.inputFields,
140
+ ...getAPIActionState(extendedAction, customActionRunStateMap)
141
+ });
171
142
  });
172
143
  }
173
- exports.convertObjectModelActionToAPICustomObjectAction = convertObjectModelActionToAPICustomObjectAction;
174
- function convertFieldActionToAPICustomAction({ action, customActionMap, csiParentDocument, fieldPath }) {
175
- var _a, _b, _c;
176
- const actionId = `${csiParentDocument.srcType}:${csiParentDocument.srcProjectId}:${csiParentDocument.id}:${fieldPath.join('.')}:${action.name}`;
177
- const prevStoreAction = customActionMap[actionId];
178
- const storeAction = {
179
- // if configuration is updated, the new action properties will override the stored action properties
180
- ...action,
181
- type: action.type === 'object' ? 'objectField' : 'field',
182
- actionId,
183
- label: (_a = action.label) !== null && _a !== void 0 ? _a : lodash_1.default.startCase(action.name),
184
- runningHandler: prevStoreAction === null || prevStoreAction === void 0 ? void 0 : prevStoreAction.runningHandler,
185
- lastResultState: prevStoreAction === null || prevStoreAction === void 0 ? void 0 : prevStoreAction.lastResultState,
186
- documentSpec: {
187
- srcType: csiParentDocument.srcType,
188
- srcProjectId: csiParentDocument.srcProjectId,
189
- srcDocumentId: csiParentDocument.id
190
- },
191
- fieldPath
192
- };
193
- customActionMap[actionId] = storeAction;
194
- let state;
195
- let needsResolving = false;
196
- if (storeAction === null || storeAction === void 0 ? void 0 : storeAction.runningHandler) {
197
- state = 'running';
198
- }
199
- else if (action.state) {
200
- state = 'unknown';
201
- needsResolving = true;
144
+ exports.getObjectModelActionsThunk = getObjectModelActionsThunk;
145
+ function getObjectFieldActionsThunk({ modelField, csiParentDocument, srcType, srcProjectId, customActionRunStateMap, fieldPath }) {
146
+ if (!('actions' in modelField) || !Array.isArray(modelField.actions)) {
147
+ return () => undefined;
202
148
  }
203
- else {
204
- state = (_b = storeAction === null || storeAction === void 0 ? void 0 : storeAction.lastResultState) !== null && _b !== void 0 ? _b : 'enabled';
149
+ const objectFieldActions = modelField.actions.filter((action) => action.type === 'object');
150
+ const extendedObjectFieldActions = objectFieldActions.map((action) => {
151
+ var _a;
152
+ const documentSpec = {
153
+ srcType: srcType,
154
+ srcProjectId: srcProjectId,
155
+ srcDocumentId: csiParentDocument.id
156
+ };
157
+ return {
158
+ // if configuration is updated, the new action properties will override the stored action properties
159
+ ...action,
160
+ type: 'objectField',
161
+ actionId: fieldPathActionId({ ...documentSpec, fieldPath, actionName: action.name }),
162
+ label: (_a = action.label) !== null && _a !== void 0 ? _a : lodash_1.default.startCase(action.name),
163
+ documentSpec: documentSpec,
164
+ fieldPath
165
+ };
166
+ });
167
+ return () => extendedObjectFieldActions.map((extendedAction) => {
168
+ return (0, utils_1.omitByNil)({
169
+ type: 'object',
170
+ actionId: extendedAction.actionId,
171
+ name: extendedAction.name,
172
+ label: extendedAction.label,
173
+ icon: extendedAction.icon,
174
+ inputFields: extendedAction.inputFields,
175
+ ...getAPIActionState(extendedAction, customActionRunStateMap)
176
+ });
177
+ });
178
+ }
179
+ exports.getObjectFieldActionsThunk = getObjectFieldActionsThunk;
180
+ function getFieldActions({ modelField, csiParentDocument, srcType, srcProjectId, customActionRunStateMap, fieldPath }) {
181
+ if (!('actions' in modelField) || !Array.isArray(modelField.actions)) {
182
+ return undefined;
205
183
  }
206
- return (0, utils_1.omitByNil)({
207
- actionId: actionId,
208
- type: (_c = action.type) !== null && _c !== void 0 ? _c : 'field',
209
- name: storeAction.name,
210
- label: storeAction.label,
211
- icon: storeAction.icon,
212
- state: state,
213
- inputFields: storeAction.inputFields,
214
- needsResolving: needsResolving
184
+ const fieldActions = modelField.actions.filter((action) => action.type !== 'object');
185
+ const extendedFieldActions = fieldActions.map((action) => {
186
+ var _a;
187
+ const documentSpec = {
188
+ srcType: srcType,
189
+ srcProjectId: srcProjectId,
190
+ srcDocumentId: csiParentDocument.id
191
+ };
192
+ return {
193
+ // if configuration is updated, the new action properties will override the stored action properties
194
+ ...action,
195
+ type: 'field',
196
+ actionId: fieldPathActionId({ ...documentSpec, fieldPath, actionName: action.name }),
197
+ label: (_a = action.label) !== null && _a !== void 0 ? _a : lodash_1.default.startCase(action.name),
198
+ documentSpec: documentSpec,
199
+ fieldPath
200
+ };
201
+ });
202
+ return extendedFieldActions.map((extendedAction) => {
203
+ return (0, utils_1.omitByNil)({
204
+ type: 'field',
205
+ actionId: extendedAction.actionId,
206
+ name: extendedAction.name,
207
+ label: extendedAction.label,
208
+ icon: extendedAction.icon,
209
+ inputFields: extendedAction.inputFields,
210
+ ...getAPIActionState(extendedAction, customActionRunStateMap)
211
+ });
215
212
  });
216
213
  }
217
- exports.convertFieldActionToAPICustomAction = convertFieldActionToAPICustomAction;
218
- async function resolveCustomActionsById({ getActionRequest, customActionMap, contentSourceDataById, userLogger }) {
214
+ exports.getFieldActions = getFieldActions;
215
+ async function resolveCustomActionsById({ getActionRequest, customActionRunStateMap, contentSourceDataById, stackbitConfig, userLogger }) {
219
216
  var _a;
220
217
  const result = [];
221
218
  const { customActionIds, locale, user, pageUrl, currentPageDocument } = getActionRequest;
@@ -224,17 +221,22 @@ async function resolveCustomActionsById({ getActionRequest, customActionMap, con
224
221
  logger: userLogger
225
222
  });
226
223
  for (const actionId of customActionIds) {
227
- const storeAction = customActionMap[actionId];
228
- if (!storeAction) {
229
- userLogger.debug(`getCustomActionsById: custom action not found, id: '${actionId}'`);
224
+ const extendedAction = findCustomActionById({
225
+ actionId,
226
+ customActionRunStateMap,
227
+ contentSourceDataById,
228
+ stackbitConfig
229
+ });
230
+ if (!extendedAction) {
231
+ userLogger.debug(`custom action with id: '${actionId}' was not found`);
230
232
  continue;
231
233
  }
232
234
  try {
233
235
  let state;
234
- if (storeAction.runningHandler) {
236
+ if (extendedAction.runningHandler) {
235
237
  state = 'running';
236
238
  }
237
- else if (storeAction.state) {
239
+ else if (extendedAction.state) {
238
240
  const pageDocument = getCSIDocumentWithSourceFromDocumentSpec(currentPageDocument, configDelegate);
239
241
  const commonStateOptions = {
240
242
  actionId: actionId,
@@ -249,66 +251,66 @@ async function resolveCustomActionsById({ getActionRequest, customActionMap, con
249
251
  currentPageDocument: pageDocument,
250
252
  ...configDelegate
251
253
  };
252
- if (storeAction.type === 'global' || storeAction.type === 'bulk') {
253
- state = await storeAction.state(commonStateOptions);
254
+ if (extendedAction.type === 'global' || extendedAction.type === 'bulk') {
255
+ state = await extendedAction.state(commonStateOptions);
254
256
  }
255
- else if (storeAction.type === 'document') {
257
+ else if (extendedAction.type === 'document') {
256
258
  const { document, model } = getCSIDocumentAndModelWithSourceFromDocumentSpec({
257
- documentSpec: storeAction.documentSpec,
259
+ documentSpec: extendedAction.documentSpec,
258
260
  contentSourceDataById
259
261
  });
260
- state = await storeAction.state({
262
+ state = await extendedAction.state({
261
263
  ...commonStateOptions,
262
264
  document: document,
263
265
  model: model
264
266
  });
265
267
  }
266
- else if (storeAction.type === 'objectModel') {
268
+ else if (extendedAction.type === 'objectModel') {
267
269
  const stateObjectParams = getHandlerParamsForObjectModelAction({
268
- storeAction,
270
+ extendedAction,
269
271
  contentSourceDataById
270
272
  });
271
- state = await storeAction.state({
273
+ state = await extendedAction.state({
272
274
  ...commonStateOptions,
273
275
  ...stateObjectParams
274
276
  });
275
277
  }
276
- else if (storeAction.type === 'objectField') {
278
+ else if (extendedAction.type === 'objectField') {
277
279
  const stateObjectParams = getHandlerParamsForObjectFieldAction({
278
- storeAction,
280
+ extendedAction,
279
281
  contentSourceDataById
280
282
  });
281
- state = await storeAction.state({
283
+ state = await extendedAction.state({
282
284
  ...commonStateOptions,
283
285
  ...stateObjectParams
284
286
  });
285
287
  }
286
- else if (storeAction.type === 'field') {
288
+ else if (extendedAction.type === 'field') {
287
289
  const stateFieldParams = getHandlerParamsForFieldAction({
288
- storeAction,
290
+ extendedAction,
289
291
  contentSourceDataById
290
292
  });
291
- state = await storeAction.state({
293
+ state = await extendedAction.state({
292
294
  ...commonStateOptions,
293
295
  ...stateFieldParams
294
296
  });
295
297
  }
296
298
  else {
297
- const _exhaustiveCheck = storeAction;
299
+ const _exhaustiveCheck = extendedAction;
298
300
  continue;
299
301
  }
300
302
  }
301
303
  else {
302
- state = (_a = storeAction === null || storeAction === void 0 ? void 0 : storeAction.lastResultState) !== null && _a !== void 0 ? _a : 'enabled';
304
+ state = (_a = extendedAction.lastResultState) !== null && _a !== void 0 ? _a : 'enabled';
303
305
  }
304
306
  result.push((0, utils_1.omitByNil)({
305
307
  actionId: actionId,
306
- type: storeActionTypeToAPIActionType(storeAction.type),
307
- name: storeAction.name,
308
- label: storeAction.label,
309
- icon: storeAction.icon,
308
+ type: storeActionTypeToAPIActionType(extendedAction.type),
309
+ name: extendedAction.name,
310
+ label: extendedAction.label,
311
+ icon: extendedAction.icon,
310
312
  state: state,
311
- inputFields: storeAction.inputFields
313
+ inputFields: extendedAction.inputFields
312
314
  }));
313
315
  }
314
316
  catch (error) {
@@ -318,24 +320,31 @@ async function resolveCustomActionsById({ getActionRequest, customActionMap, con
318
320
  return result;
319
321
  }
320
322
  exports.resolveCustomActionsById = resolveCustomActionsById;
321
- function runCustomAction({ runActionRequest, customActionMap, contentSourceDataById, userLogger, stackbitConfig }) {
323
+ function runCustomAction({ runActionRequest, customActionRunStateMap, contentSourceDataById, stackbitConfig, userLogger }) {
322
324
  var _a, _b;
323
- const storeAction = customActionMap[runActionRequest.actionId];
324
- if (!storeAction) {
325
+ const extendedAction = findCustomActionById({
326
+ actionId: runActionRequest.actionId,
327
+ customActionRunStateMap,
328
+ contentSourceDataById,
329
+ stackbitConfig
330
+ });
331
+ if (!extendedAction) {
325
332
  throw new Error(`Error running action: action not found, action name: '${runActionRequest.actionName}' action ID: '${runActionRequest.actionId}'.`);
326
333
  }
327
- const prevResultState = storeAction.lastResultState;
328
- if (storeAction.lastResultState && storeAction.lastResultState !== 'enabled') {
334
+ const prevResultState = extendedAction.lastResultState;
335
+ if (extendedAction.lastResultState && extendedAction.lastResultState !== 'enabled') {
329
336
  throw new Error(`Error running action: action is not enabled, action name: '${runActionRequest.actionName}' action ID: '${runActionRequest.actionId}'.`);
330
337
  }
331
338
  try {
332
- const actionLogger = userLogger.createLogger({ label: `action:${storeAction.name}` });
339
+ const actionLogger = userLogger.createLogger({ label: `action:${extendedAction.name}` });
333
340
  const configDelegate = (0, config_delegate_1.createConfigDelegate)({ contentSourceDataById: contentSourceDataById, logger: actionLogger });
334
341
  const currentPageDocument = getCSIDocumentWithSourceFromDocumentSpec(runActionRequest.currentPageDocument, configDelegate);
335
- storeAction.runningHandler = true;
336
- storeAction.lastResultState = 'running';
342
+ customActionRunStateMap[runActionRequest.actionId] = {
343
+ runningHandler: true,
344
+ lastResultState: 'running'
345
+ };
337
346
  const commonRunOptions = {
338
- actionId: storeAction.actionId,
347
+ actionId: extendedAction.actionId,
339
348
  inputData: runActionRequest.inputData,
340
349
  currentLocale: runActionRequest.locale,
341
350
  currentUser: runActionRequest.user,
@@ -351,10 +360,10 @@ function runCustomAction({ runActionRequest, customActionMap, contentSourceDataB
351
360
  ...configDelegate
352
361
  };
353
362
  let promise;
354
- if (storeAction.type === 'global') {
355
- promise = storeAction.run(commonRunOptions);
363
+ if (extendedAction.type === 'global') {
364
+ promise = extendedAction.run(commonRunOptions);
356
365
  }
357
- else if (storeAction.type === 'bulk') {
366
+ else if (extendedAction.type === 'bulk') {
358
367
  const documents = runActionRequest.documents.map((documentSpec) => {
359
368
  const { document } = getCSIDocumentAndModelWithSourceFromDocumentSpec({
360
369
  documentSpec,
@@ -362,78 +371,80 @@ function runCustomAction({ runActionRequest, customActionMap, contentSourceDataB
362
371
  });
363
372
  return document;
364
373
  });
365
- promise = storeAction.run({
374
+ promise = extendedAction.run({
366
375
  ...commonRunOptions,
367
376
  documents
368
377
  });
369
378
  }
370
- else if (storeAction.type === 'document') {
379
+ else if (extendedAction.type === 'document') {
371
380
  const { document, model } = getCSIDocumentAndModelWithSourceFromDocumentSpec({
372
- documentSpec: storeAction.documentSpec,
381
+ documentSpec: extendedAction.documentSpec,
373
382
  contentSourceDataById
374
383
  });
375
- promise = storeAction.run({
384
+ promise = extendedAction.run({
376
385
  ...commonRunOptions,
377
386
  document,
378
387
  model,
379
388
  contentSourceActions: commonRunOptions.getContentSourceActionsForSource({
380
- srcType: storeAction.documentSpec.srcType,
381
- srcProjectId: storeAction.documentSpec.srcProjectId
389
+ srcType: extendedAction.documentSpec.srcType,
390
+ srcProjectId: extendedAction.documentSpec.srcProjectId
382
391
  })
383
392
  });
384
393
  }
385
- else if (storeAction.type === 'objectModel') {
394
+ else if (extendedAction.type === 'objectModel') {
386
395
  const handlerObjectParams = getHandlerParamsForObjectModelAction({
387
- storeAction,
396
+ extendedAction,
388
397
  contentSourceDataById
389
398
  });
390
- promise = storeAction.run({
399
+ promise = extendedAction.run({
391
400
  ...commonRunOptions,
392
401
  ...handlerObjectParams,
393
402
  contentSourceActions: commonRunOptions.getContentSourceActionsForSource({
394
- srcType: storeAction.documentSpec.srcType,
395
- srcProjectId: storeAction.documentSpec.srcProjectId
403
+ srcType: extendedAction.documentSpec.srcType,
404
+ srcProjectId: extendedAction.documentSpec.srcProjectId
396
405
  })
397
406
  });
398
407
  }
399
- else if (storeAction.type === 'objectField') {
408
+ else if (extendedAction.type === 'objectField') {
400
409
  const handlerObjectParams = getHandlerParamsForObjectFieldAction({
401
- storeAction,
410
+ extendedAction,
402
411
  contentSourceDataById
403
412
  });
404
- promise = storeAction.run({
413
+ promise = extendedAction.run({
405
414
  ...commonRunOptions,
406
415
  ...handlerObjectParams,
407
416
  contentSourceActions: commonRunOptions.getContentSourceActionsForSource({
408
- srcType: storeAction.documentSpec.srcType,
409
- srcProjectId: storeAction.documentSpec.srcProjectId
417
+ srcType: extendedAction.documentSpec.srcType,
418
+ srcProjectId: extendedAction.documentSpec.srcProjectId
410
419
  })
411
420
  });
412
421
  }
413
- else if (storeAction.type === 'field') {
414
- const handlerFieldParams = getHandlerParamsForFieldAction({ storeAction, contentSourceDataById });
415
- promise = storeAction.run({
422
+ else if (extendedAction.type === 'field') {
423
+ const handlerFieldParams = getHandlerParamsForFieldAction({ extendedAction, contentSourceDataById });
424
+ promise = extendedAction.run({
416
425
  ...commonRunOptions,
417
426
  ...handlerFieldParams,
418
427
  contentSourceActions: commonRunOptions.getContentSourceActionsForSource({
419
- srcType: storeAction.documentSpec.srcType,
420
- srcProjectId: storeAction.documentSpec.srcProjectId
428
+ srcType: extendedAction.documentSpec.srcType,
429
+ srcProjectId: extendedAction.documentSpec.srcProjectId
421
430
  })
422
431
  });
423
432
  }
424
433
  else {
425
- throw new Error(`action type ${storeAction.type} not supported`);
434
+ throw new Error(`action type ${extendedAction.type} not supported`);
426
435
  }
427
436
  return promise
428
437
  .then((actionResult) => {
429
438
  var _a;
430
- storeAction.runningHandler = false;
431
- storeAction.lastResultState = actionResult === null || actionResult === void 0 ? void 0 : actionResult.state;
432
- userLogger.debug(`Action completed: ${storeAction.actionId}`);
439
+ customActionRunStateMap[runActionRequest.actionId] = {
440
+ runningHandler: false,
441
+ lastResultState: actionResult === null || actionResult === void 0 ? void 0 : actionResult.state
442
+ };
443
+ userLogger.debug(`Action completed: ${extendedAction.actionId}`);
433
444
  return Promise.resolve((0, utils_1.omitByNil)({
434
- actionId: storeAction.actionId,
435
- actionName: storeAction.name,
436
- actionType: storeActionTypeToAPIActionType(storeAction.type),
445
+ actionId: extendedAction.actionId,
446
+ actionName: extendedAction.name,
447
+ actionType: storeActionTypeToAPIActionType(extendedAction.type),
437
448
  // TODO: resolve the state if state function is defined
438
449
  state: (_a = actionResult === null || actionResult === void 0 ? void 0 : actionResult.state) !== null && _a !== void 0 ? _a : 'enabled',
439
450
  success: actionResult === null || actionResult === void 0 ? void 0 : actionResult.success,
@@ -441,13 +452,15 @@ function runCustomAction({ runActionRequest, customActionMap, contentSourceDataB
441
452
  }));
442
453
  })
443
454
  .catch((error) => {
444
- storeAction.runningHandler = false;
445
- storeAction.lastResultState = prevResultState;
455
+ customActionRunStateMap[runActionRequest.actionId] = {
456
+ runningHandler: false,
457
+ lastResultState: prevResultState
458
+ };
446
459
  userLogger.debug(`Error running action: ${error.message}`);
447
460
  return Promise.resolve({
448
- actionId: storeAction.actionId,
449
- actionName: storeAction.name,
450
- actionType: storeActionTypeToAPIActionType(storeAction.type),
461
+ actionId: extendedAction.actionId,
462
+ actionName: extendedAction.name,
463
+ actionType: storeActionTypeToAPIActionType(extendedAction.type),
451
464
  // TODO: resolve the state if state function is defined
452
465
  state: prevResultState !== null && prevResultState !== void 0 ? prevResultState : 'enabled',
453
466
  error: `Error running action: ${error.message}`
@@ -455,15 +468,17 @@ function runCustomAction({ runActionRequest, customActionMap, contentSourceDataB
455
468
  });
456
469
  }
457
470
  catch (error) {
458
- if (storeAction) {
459
- storeAction.runningHandler = false;
460
- storeAction.lastResultState = prevResultState;
471
+ if (customActionRunStateMap[runActionRequest.actionId]) {
472
+ customActionRunStateMap[runActionRequest.actionId] = {
473
+ runningHandler: false,
474
+ lastResultState: prevResultState
475
+ };
461
476
  }
462
477
  userLogger.debug(`Error running action: ${error.message}`);
463
478
  return Promise.resolve({
464
479
  actionId: runActionRequest.actionId,
465
- actionName: (_a = storeAction === null || storeAction === void 0 ? void 0 : storeAction.name) !== null && _a !== void 0 ? _a : runActionRequest.actionName,
466
- actionType: (_b = storeActionTypeToAPIActionType(storeAction === null || storeAction === void 0 ? void 0 : storeAction.type)) !== null && _b !== void 0 ? _b : runActionRequest.actionType,
480
+ actionName: (_a = extendedAction === null || extendedAction === void 0 ? void 0 : extendedAction.name) !== null && _a !== void 0 ? _a : runActionRequest.actionName,
481
+ actionType: (_b = storeActionTypeToAPIActionType(extendedAction === null || extendedAction === void 0 ? void 0 : extendedAction.type)) !== null && _b !== void 0 ? _b : runActionRequest.actionType,
467
482
  // TODO: resolve the state if state function is defined
468
483
  state: prevResultState !== null && prevResultState !== void 0 ? prevResultState : 'enabled',
469
484
  error: `Error running action: ${error.message}`
@@ -480,16 +495,34 @@ function getCSIDocumentWithSourceFromDocumentSpec(documentSpec, configDelegate)
480
495
  })
481
496
  : undefined;
482
497
  }
483
- function getHandlerParamsForObjectModelAction({ storeAction, contentSourceDataById }) {
498
+ function getAPIActionState(extendedAction, customActionRunStateMap) {
499
+ var _a;
500
+ const actionId = extendedAction.actionId;
501
+ const actionRunState = customActionRunStateMap[actionId];
502
+ let state;
503
+ let needsResolving = false;
504
+ if (actionRunState === null || actionRunState === void 0 ? void 0 : actionRunState.runningHandler) {
505
+ state = 'running';
506
+ }
507
+ else if (extendedAction.state) {
508
+ state = 'unknown';
509
+ needsResolving = true;
510
+ }
511
+ else {
512
+ state = (_a = actionRunState === null || actionRunState === void 0 ? void 0 : actionRunState.lastResultState) !== null && _a !== void 0 ? _a : 'enabled';
513
+ }
514
+ return { state, needsResolving };
515
+ }
516
+ function getHandlerParamsForObjectModelAction({ extendedAction, contentSourceDataById }) {
484
517
  const fieldActionCommonParams = getHandlerParamsForFieldAction({
485
- storeAction,
518
+ extendedAction,
486
519
  contentSourceDataById
487
520
  });
488
521
  if (!fieldActionCommonParams.documentField) {
489
- throw new Error(`object document field not found at field path: ${storeAction.fieldPath.join('.')}`);
522
+ throw new Error(`object document field not found at field path: ${extendedAction.fieldPath.join('.')}`);
490
523
  }
491
524
  const documentField = fieldActionCommonParams.documentField;
492
- const documentSpec = storeAction.documentSpec;
525
+ const documentSpec = extendedAction.documentSpec;
493
526
  const contentSourceData = (0, content_store_utils_1.getContentSourceDataByTypeAndProjectIdOrThrow)(documentSpec.srcType, documentSpec.srcProjectId, contentSourceDataById);
494
527
  const objectModel = contentSourceData.modelMap[documentField.modelName];
495
528
  if (!objectModel || objectModel.type !== 'object') {
@@ -506,13 +539,13 @@ function getHandlerParamsForObjectModelAction({ storeAction, contentSourceDataBy
506
539
  }
507
540
  };
508
541
  }
509
- function getHandlerParamsForObjectFieldAction({ storeAction, contentSourceDataById }) {
542
+ function getHandlerParamsForObjectFieldAction({ extendedAction, contentSourceDataById }) {
510
543
  const fieldActionCommonParams = getHandlerParamsForFieldAction({
511
- storeAction,
544
+ extendedAction,
512
545
  contentSourceDataById
513
546
  });
514
547
  if (!fieldActionCommonParams.documentField) {
515
- throw new Error(`object document field not found at field path: ${storeAction.fieldPath.join('.')}`);
548
+ throw new Error(`object document field not found at field path: ${extendedAction.fieldPath.join('.')}`);
516
549
  }
517
550
  return {
518
551
  ...fieldActionCommonParams,
@@ -520,8 +553,8 @@ function getHandlerParamsForObjectFieldAction({ storeAction, contentSourceDataBy
520
553
  modelField: fieldActionCommonParams.modelField
521
554
  };
522
555
  }
523
- function getHandlerParamsForFieldAction({ storeAction, contentSourceDataById }) {
524
- const documentSpec = storeAction.documentSpec;
556
+ function getHandlerParamsForFieldAction({ extendedAction, contentSourceDataById }) {
557
+ const documentSpec = extendedAction.documentSpec;
525
558
  const contentSourceData = (0, content_store_utils_1.getContentSourceDataByTypeAndProjectIdOrThrow)(documentSpec.srcType, documentSpec.srcProjectId, contentSourceDataById);
526
559
  const document = contentSourceData.documentMap[documentSpec.srcDocumentId];
527
560
  const csiDocument = contentSourceData.csiDocumentMap[documentSpec.srcDocumentId];
@@ -537,7 +570,7 @@ function getHandlerParamsForFieldAction({ storeAction, contentSourceDataById })
537
570
  const { modelField, documentField } = (0, content_store_utils_1.getModelAndDocumentFieldForLocalizedFieldPath)({
538
571
  document,
539
572
  model,
540
- fieldPath: storeAction.fieldPath,
573
+ fieldPath: extendedAction.fieldPath,
541
574
  modelMap: contentSourceData.modelMap
542
575
  });
543
576
  const csiDocumentField = (0, store_to_csi_docs_converter_1.mapStoreFieldToCSIField)(documentField);
@@ -550,7 +583,7 @@ function getHandlerParamsForFieldAction({ storeAction, contentSourceDataById })
550
583
  },
551
584
  documentField: csiDocumentField,
552
585
  modelField: modelField,
553
- fieldPath: storeAction.fieldPath
586
+ fieldPath: extendedAction.fieldPath
554
587
  };
555
588
  }
556
589
  function getCSIDocumentAndModelWithSourceFromDocumentSpec({ documentSpec, contentSourceDataById }) {
@@ -583,4 +616,157 @@ function storeActionTypeToAPIActionType(storeActionType) {
583
616
  }
584
617
  return storeActionType;
585
618
  }
619
+ function findCustomActionById({ actionId, customActionRunStateMap, contentSourceDataById, stackbitConfig }) {
620
+ var _a, _b, _c, _d, _e;
621
+ const actionRunState = customActionRunStateMap[actionId];
622
+ if (isGlobalActionId(actionId)) {
623
+ if (!stackbitConfig || !Array.isArray(stackbitConfig.actions)) {
624
+ return undefined;
625
+ }
626
+ const actionName = getGlobalActionNameFromId(actionId);
627
+ const action = stackbitConfig.actions.find((action) => action.name === actionName);
628
+ if (!action) {
629
+ return undefined;
630
+ }
631
+ return {
632
+ ...action,
633
+ actionId,
634
+ label: (_a = action.label) !== null && _a !== void 0 ? _a : lodash_1.default.startCase(action.name),
635
+ runningHandler: actionRunState === null || actionRunState === void 0 ? void 0 : actionRunState.runningHandler,
636
+ lastResultState: actionRunState === null || actionRunState === void 0 ? void 0 : actionRunState.lastResultState
637
+ };
638
+ }
639
+ const { srcType, srcProjectId, srcDocumentId, actionName, fieldPath } = (_b = parseActionId(actionId)) !== null && _b !== void 0 ? _b : {};
640
+ if (!srcType || !srcProjectId || !srcDocumentId || !actionName) {
641
+ return undefined;
642
+ }
643
+ const documentSpec = { srcType, srcProjectId, srcDocumentId };
644
+ const contentSourceData = (0, content_store_utils_1.getContentSourceDataByTypeAndProjectIdOrThrow)(srcType, srcProjectId, contentSourceDataById);
645
+ const document = contentSourceData.documentMap[srcDocumentId];
646
+ if (!document) {
647
+ return undefined;
648
+ }
649
+ const modelName = document.srcModelName;
650
+ const model = contentSourceData.modelMap[modelName];
651
+ // The model of a document is always 'page' or 'data',
652
+ // this condition helps TS to infer the right type of model.actions
653
+ if (!model || (model.type !== 'page' && model.type !== 'data')) {
654
+ return undefined;
655
+ }
656
+ if (!('actions' in model) || !Array.isArray(model.actions)) {
657
+ return undefined;
658
+ }
659
+ if (typeof fieldPath === 'undefined') {
660
+ // fieldPath was not provided, therefore the model must be of type "page" or "data",
661
+ // and the action type must be 'document'
662
+ const action = model.actions.find((action) => action.name === actionName);
663
+ if (!action) {
664
+ return undefined;
665
+ }
666
+ return {
667
+ // if configuration is updated, the new action properties will override the stored action properties
668
+ ...action,
669
+ type: 'document',
670
+ actionId,
671
+ label: (_c = action.label) !== null && _c !== void 0 ? _c : lodash_1.default.startCase(action.name),
672
+ documentSpec,
673
+ runningHandler: actionRunState === null || actionRunState === void 0 ? void 0 : actionRunState.runningHandler,
674
+ lastResultState: actionRunState === null || actionRunState === void 0 ? void 0 : actionRunState.lastResultState
675
+ };
676
+ }
677
+ else {
678
+ const { modelField, documentField } = (0, content_store_utils_1.getModelAndDocumentFieldForLocalizedFieldPath)({
679
+ document,
680
+ model,
681
+ fieldPath,
682
+ modelMap: contentSourceData.modelMap
683
+ });
684
+ if ('actions' in modelField && Array.isArray(modelField.actions)) {
685
+ const action = modelField.actions.find((action) => action.name === actionName);
686
+ if (action) {
687
+ return {
688
+ // if configuration is updated, the new action properties will override the stored action properties
689
+ ...action,
690
+ type: action.type === 'object' ? 'objectField' : 'field',
691
+ actionId,
692
+ label: (_d = action.label) !== null && _d !== void 0 ? _d : lodash_1.default.startCase(action.name),
693
+ documentSpec,
694
+ fieldPath,
695
+ runningHandler: actionRunState === null || actionRunState === void 0 ? void 0 : actionRunState.runningHandler,
696
+ lastResultState: actionRunState === null || actionRunState === void 0 ? void 0 : actionRunState.lastResultState
697
+ };
698
+ }
699
+ }
700
+ if (modelField.type === 'model') {
701
+ if (documentField.type !== 'model' || documentField.localized || documentField.isUnset) {
702
+ return undefined;
703
+ }
704
+ const modelName = documentField.srcModelName;
705
+ const model = contentSourceData.modelMap[modelName];
706
+ if (!model || model.type !== 'object') {
707
+ return undefined;
708
+ }
709
+ if (!('actions' in model && Array.isArray(model.actions))) {
710
+ return undefined;
711
+ }
712
+ // This is a nested model of type "object", so the action must be CustomActionObjectModel
713
+ const action = model.actions.find((action) => action.name === actionName);
714
+ if (!action) {
715
+ return undefined;
716
+ }
717
+ return {
718
+ // if configuration is updated, the new action properties will override the stored action properties
719
+ ...action,
720
+ type: 'objectModel',
721
+ actionId,
722
+ label: (_e = action.label) !== null && _e !== void 0 ? _e : lodash_1.default.startCase(action.name),
723
+ documentSpec,
724
+ fieldPath,
725
+ runningHandler: actionRunState === null || actionRunState === void 0 ? void 0 : actionRunState.runningHandler,
726
+ lastResultState: actionRunState === null || actionRunState === void 0 ? void 0 : actionRunState.lastResultState
727
+ };
728
+ }
729
+ }
730
+ }
731
+ function globalActionId(action) {
732
+ return `config.actions.${action.name}`;
733
+ }
734
+ function isGlobalActionId(actionId) {
735
+ return actionId.startsWith('config.actions.');
736
+ }
737
+ function getGlobalActionNameFromId(actionId) {
738
+ return actionId.substring('config.actions.'.length);
739
+ }
740
+ function documentActionId({ srcType, srcProjectId, srcDocumentId, actionName }) {
741
+ return `${srcType}:${srcProjectId}:${srcDocumentId}:${actionName}`;
742
+ }
743
+ function fieldPathActionId({ srcType, srcProjectId, srcDocumentId, fieldPath, actionName }) {
744
+ return `${srcType}:${srcProjectId}:${srcDocumentId}:${fieldPath.join('.')}:${actionName}`;
745
+ }
746
+ function parseActionId(actionId) {
747
+ var _a;
748
+ const parts = actionId.split(':');
749
+ if (parts.length < 4 || parts.length > 5) {
750
+ return undefined;
751
+ }
752
+ const srcType = parts[0];
753
+ const srcProjectId = parts[1];
754
+ const srcDocumentId = parts[2];
755
+ let fieldPath;
756
+ let actionName;
757
+ if (parts.length === 5) {
758
+ fieldPath = (_a = parts[3]) === null || _a === void 0 ? void 0 : _a.split('.');
759
+ actionName = parts[4];
760
+ }
761
+ else {
762
+ actionName = parts[3];
763
+ }
764
+ return {
765
+ srcType,
766
+ srcProjectId,
767
+ srcDocumentId,
768
+ actionName,
769
+ fieldPath
770
+ };
771
+ }
586
772
  //# sourceMappingURL=custom-actions.js.map