From 5678056dc53f1737f78f0ef89ff99051c4954ec0 Mon Sep 17 00:00:00 2001 From: Pavel Denisjuk Date: Thu, 22 Feb 2024 10:53:35 +0100 Subject: [PATCH] fix(api-headless-cms): export createModelField and isHeadlessCmsReady (#3891) --- packages/api-aco/src/createAcoContext.ts | 5 +++-- packages/api-aco/src/createAcoModels.ts | 12 ++++------- packages/api-aco/src/filter/filter.model.ts | 6 +++--- packages/api-aco/src/folder/folder.model.ts | 6 +++--- packages/api-aco/src/record/record.model.ts | 8 ++++---- .../src/utils/isInstallationPending.ts | 15 -------------- .../models/changeRequest.model.ts | 6 +++--- .../storageOperations/models/comment.model.ts | 6 +++--- .../models/contentReview.model.ts | 6 +++--- .../src/storageOperations/models/index.ts | 20 +++++++++---------- .../models/reviewer.model.ts | 6 +++--- .../models/workflow.model.ts | 8 +++----- .../src/FileManagerContextSetup.ts | 10 ++++------ .../createFileManagerPlugins.ts | 13 ------------ .../src/cmsFileStorage/file.model.ts | 7 +++---- .../cmsFileStorage/isInstallationPending.ts | 15 -------------- .../api-file-manager/src/graphql/index.ts | 6 +++--- .../src/modelModifier/CmsModelModifier.ts | 3 +-- .../__tests__/tasks/mocks/models.ts | 4 ++-- packages/api-headless-cms/src/index.ts | 2 ++ .../src/plugins/CmsModelPlugin.ts | 2 +- .../src/utils}/createModelField.ts | 2 +- .../src/utils/isHeadlessCmsReady.ts | 15 ++++++++++++++ .../api-mailer/src/crud/settings/model.ts | 4 ++-- packages/tasks/src/crud/model.ts | 6 +++--- 25 files changed, 79 insertions(+), 114 deletions(-) delete mode 100644 packages/api-aco/src/utils/isInstallationPending.ts delete mode 100644 packages/api-file-manager/src/cmsFileStorage/createFileManagerPlugins.ts delete mode 100644 packages/api-file-manager/src/cmsFileStorage/isInstallationPending.ts rename packages/{api-file-manager/src/cmsFileStorage => api-headless-cms/src/utils}/createModelField.ts (93%) create mode 100644 packages/api-headless-cms/src/utils/isHeadlessCmsReady.ts diff --git a/packages/api-aco/src/createAcoContext.ts b/packages/api-aco/src/createAcoContext.ts index 04e790bba3c..ac44856d7b6 100644 --- a/packages/api-aco/src/createAcoContext.ts +++ b/packages/api-aco/src/createAcoContext.ts @@ -2,9 +2,9 @@ import WebinyError from "@webiny/error"; import { ContextPlugin } from "@webiny/api"; import { I18NLocale } from "@webiny/api-i18n/types"; import { Tenant } from "@webiny/api-tenancy/types"; +import { isHeadlessCmsReady } from "@webiny/api-headless-cms"; import { createAcoHooks } from "~/createAcoHooks"; import { createAcoStorageOperations } from "~/createAcoStorageOperations"; -import { isInstallationPending } from "~/utils/isInstallationPending"; import { AcoContext, CreateAcoParams, Folder, IAcoAppRegisterParams } from "~/types"; import { createFolderCrudMethods } from "~/folder/folder.crud"; import { createSearchRecordCrudMethods } from "~/record/record.crud"; @@ -185,9 +185,10 @@ export const createAcoContext = (params: CreateAcoContextParams = {}) => { /** * We can skip the ACO initialization if the installation is pending. */ - if (isInstallationPending(context)) { + if (!(await isHeadlessCmsReady(context))) { return; } + await context.benchmark.measure("aco.context.setup", async () => { await setupAcoContext(context, params); }); diff --git a/packages/api-aco/src/createAcoModels.ts b/packages/api-aco/src/createAcoModels.ts index cd0ba306752..6162b04ecbb 100644 --- a/packages/api-aco/src/createAcoModels.ts +++ b/packages/api-aco/src/createAcoModels.ts @@ -1,18 +1,14 @@ import { CmsContext } from "@webiny/api-headless-cms/types"; -import { createFilterModelDefinition } from "~/filter/filter.model"; -import { createFolderModelDefinition } from "~/folder/folder.model"; -import { createSearchModelDefinition } from "~/record/record.model"; +import { createFilterModel } from "~/filter/filter.model"; +import { createFolderModel } from "~/folder/folder.model"; +import { createSearchModel } from "~/record/record.model"; import { modelFactory } from "~/utils/modelFactory"; export const createAcoModels = (context: CmsContext) => { /** * Create CmsModel plugins. */ - const modelDefinitions = [ - createFolderModelDefinition(), - createSearchModelDefinition(), - createFilterModelDefinition() - ]; + const modelDefinitions = [createFolderModel(), createSearchModel(), createFilterModel()]; const cmsModelPlugins = modelDefinitions.map(modelDefinition => { return modelFactory({ modelDefinition diff --git a/packages/api-aco/src/filter/filter.model.ts b/packages/api-aco/src/filter/filter.model.ts index a6e8da43890..6e290c21a8f 100644 --- a/packages/api-aco/src/filter/filter.model.ts +++ b/packages/api-aco/src/filter/filter.model.ts @@ -1,5 +1,5 @@ import { createModelField } from "~/utils/createModelField"; -import { createPrivateModelDefinition } from "@webiny/api-headless-cms"; +import { createPrivateModel } from "@webiny/api-headless-cms"; import { CmsModelField } from "@webiny/api-headless-cms/types"; const name = () => @@ -145,8 +145,8 @@ const value = () => export const FILTER_MODEL_ID = "acoFilter"; -export const createFilterModelDefinition = () => { - return createPrivateModelDefinition({ +export const createFilterModel = () => { + return createPrivateModel({ name: "ACO - Filter", modelId: FILTER_MODEL_ID, titleFieldId: "name", diff --git a/packages/api-aco/src/folder/folder.model.ts b/packages/api-aco/src/folder/folder.model.ts index e3d1ab7bd1a..80574bc5ce6 100644 --- a/packages/api-aco/src/folder/folder.model.ts +++ b/packages/api-aco/src/folder/folder.model.ts @@ -1,5 +1,5 @@ import { createModelField } from "~/utils/createModelField"; -import { createPrivateModelDefinition } from "@webiny/api-headless-cms"; +import { createPrivateModel } from "@webiny/api-headless-cms"; const titleField = () => createModelField({ @@ -115,8 +115,8 @@ const permissionsField = () => export const FOLDER_MODEL_ID = "acoFolder"; -export const createFolderModelDefinition = () => { - return createPrivateModelDefinition({ +export const createFolderModel = () => { + return createPrivateModel({ name: "ACO - Folder", modelId: FOLDER_MODEL_ID, titleFieldId: "title", diff --git a/packages/api-aco/src/record/record.model.ts b/packages/api-aco/src/record/record.model.ts index 3f2253d9582..4143c7a850f 100644 --- a/packages/api-aco/src/record/record.model.ts +++ b/packages/api-aco/src/record/record.model.ts @@ -1,6 +1,6 @@ import { CmsModelField } from "@webiny/api-headless-cms/types"; import { createModelField } from "~/utils/createModelField"; -import { createPrivateModelDefinition } from "@webiny/api-headless-cms"; +import { createPrivateModel } from "@webiny/api-headless-cms"; const typeField = () => createModelField({ @@ -92,14 +92,14 @@ const tagsField = () => export const SEARCH_RECORD_MODEL_ID = "acoSearchRecord"; -interface CreateSearchModelDefinitionParams { +interface CreateSearchModelParams { fields?: CmsModelField[]; } export const DEFAULT_FIELDS = ["type", "title", "content", "location", "data", "tags"]; -export const createSearchModelDefinition = (params?: CreateSearchModelDefinitionParams) => { - return createPrivateModelDefinition({ +export const createSearchModel = (params?: CreateSearchModelParams) => { + return createPrivateModel({ name: "ACO - Search Record", modelId: SEARCH_RECORD_MODEL_ID, titleFieldId: "title", diff --git a/packages/api-aco/src/utils/isInstallationPending.ts b/packages/api-aco/src/utils/isInstallationPending.ts deleted file mode 100644 index 9b14516b16b..00000000000 --- a/packages/api-aco/src/utils/isInstallationPending.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { AcoContext } from "~/types"; - -type CheckInstallationParams = Pick; -export const isInstallationPending = ({ tenancy, i18n }: CheckInstallationParams): boolean => { - /** - * In case of a fresh webiny project "tenant" and "locale" won't be there until - * installation is completed. So, we need to skip "ACO" creation till then. - */ - const tenant = tenancy.getCurrentTenant(); - if (!tenant) { - return true; - } - - return !i18n.getContentLocale(); -}; diff --git a/packages/api-apw/src/storageOperations/models/changeRequest.model.ts b/packages/api-apw/src/storageOperations/models/changeRequest.model.ts index cd12fd8003b..0700e50b2d3 100644 --- a/packages/api-apw/src/storageOperations/models/changeRequest.model.ts +++ b/packages/api-apw/src/storageOperations/models/changeRequest.model.ts @@ -1,4 +1,4 @@ -import { createPrivateModelDefinition } from "@webiny/api-headless-cms"; +import { createPrivateModel } from "@webiny/api-headless-cms"; import { createModelField } from "./utils"; const bodyField = () => @@ -44,8 +44,8 @@ const stepField = () => export const CHANGE_REQUEST_MODEL_ID = "apwChangeRequestModelDefinition"; -export const createChangeRequestModelDefinition = () => { - return createPrivateModelDefinition({ +export const createChangeRequestModel = () => { + return createPrivateModel({ name: "APW - Change Request", modelId: CHANGE_REQUEST_MODEL_ID, titleFieldId: "title", diff --git a/packages/api-apw/src/storageOperations/models/comment.model.ts b/packages/api-apw/src/storageOperations/models/comment.model.ts index 8a0e5c6e37a..37ed4e006f2 100644 --- a/packages/api-apw/src/storageOperations/models/comment.model.ts +++ b/packages/api-apw/src/storageOperations/models/comment.model.ts @@ -1,4 +1,4 @@ -import { createPrivateModelDefinition } from "@webiny/api-headless-cms"; +import { createPrivateModel } from "@webiny/api-headless-cms"; import { createModelField } from "./utils"; const commentBody = () => @@ -48,8 +48,8 @@ interface CreateCommentModelDefinitionParams { export const COMMENT_MODEL_ID = "apwCommentModelDefinition"; -export const createCommentModelDefinition = ({ modelId }: CreateCommentModelDefinitionParams) => { - return createPrivateModelDefinition({ +export const createCommentModel = ({ modelId }: CreateCommentModelDefinitionParams) => { + return createPrivateModel({ name: "APW - Comment", modelId: COMMENT_MODEL_ID, titleFieldId: "step", diff --git a/packages/api-apw/src/storageOperations/models/contentReview.model.ts b/packages/api-apw/src/storageOperations/models/contentReview.model.ts index 9061efc54b4..2dbb78ed915 100644 --- a/packages/api-apw/src/storageOperations/models/contentReview.model.ts +++ b/packages/api-apw/src/storageOperations/models/contentReview.model.ts @@ -1,4 +1,4 @@ -import { createPrivateModelDefinition } from "@webiny/api-headless-cms"; +import { createPrivateModel } from "@webiny/api-headless-cms"; import { CmsModelField } from "@webiny/api-headless-cms/types"; import { createModelField } from "./utils"; import { stepTitleField, stepTypeField, stepIdField, stepReviewersField } from "./workflow.model"; @@ -279,10 +279,10 @@ interface CreateContentReviewModelDefinitionParams { export const CONTENT_REVIEW_MODEL_ID = "apwContentReviewModelDefinition"; -export const createContentReviewModelDefinition = ({ +export const createContentReviewModel = ({ reviewerModelId }: CreateContentReviewModelDefinitionParams) => { - return createPrivateModelDefinition({ + return createPrivateModel({ name: "APW - Content Review", modelId: CONTENT_REVIEW_MODEL_ID, titleFieldId: "content", diff --git a/packages/api-apw/src/storageOperations/models/index.ts b/packages/api-apw/src/storageOperations/models/index.ts index 0083eaf7113..37e4c25a724 100644 --- a/packages/api-apw/src/storageOperations/models/index.ts +++ b/packages/api-apw/src/storageOperations/models/index.ts @@ -1,10 +1,10 @@ import WebinyError from "@webiny/error"; import { CmsModelPlugin, createCmsModel } from "@webiny/api-headless-cms"; -import { createWorkflowModelDefinition } from "./workflow.model"; -import { createContentReviewModelDefinition } from "./contentReview.model"; -import { createReviewerModelDefinition } from "./reviewer.model"; -import { createCommentModelDefinition } from "./comment.model"; -import { createChangeRequestModelDefinition } from "./changeRequest.model"; +import { createWorkflowModel } from "./workflow.model"; +import { createContentReviewModel } from "./contentReview.model"; +import { createReviewerModel } from "./reviewer.model"; +import { createCommentModel } from "./comment.model"; +import { createChangeRequestModel } from "./changeRequest.model"; import { CmsContext } from "@webiny/api-headless-cms/types"; import { isInstallationPending } from "~/plugins/utils"; @@ -33,15 +33,15 @@ export const createApwModels = (context: CmsContext) => { /** * Create CmsModel plugins. */ - const changeRequestModelDefinition = createChangeRequestModelDefinition(); - const reviewerModelDefinition = createReviewerModelDefinition(); - const workflowModelDefinition = createWorkflowModelDefinition({ + const changeRequestModelDefinition = createChangeRequestModel(); + const reviewerModelDefinition = createReviewerModel(); + const workflowModelDefinition = createWorkflowModel({ reviewerModelId: reviewerModelDefinition.modelId }); - const commentModelDefinition = createCommentModelDefinition({ + const commentModelDefinition = createCommentModel({ modelId: changeRequestModelDefinition.modelId }); - const contentReviewModelDefinition = createContentReviewModelDefinition({ + const contentReviewModelDefinition = createContentReviewModel({ reviewerModelId: reviewerModelDefinition.modelId }); diff --git a/packages/api-apw/src/storageOperations/models/reviewer.model.ts b/packages/api-apw/src/storageOperations/models/reviewer.model.ts index ef7c3a7ddc7..294b24f43d4 100644 --- a/packages/api-apw/src/storageOperations/models/reviewer.model.ts +++ b/packages/api-apw/src/storageOperations/models/reviewer.model.ts @@ -1,5 +1,5 @@ import { createModelField } from "./utils"; -import { createPrivateModelDefinition } from "@webiny/api-headless-cms"; +import { createPrivateModel } from "@webiny/api-headless-cms"; const idField = () => createModelField({ @@ -61,8 +61,8 @@ const emailField = () => export const REVIEWER_MODEL_ID = "apwReviewerModelDefinition"; -export const createReviewerModelDefinition = () => { - return createPrivateModelDefinition({ +export const createReviewerModel = () => { + return createPrivateModel({ name: "APW - Reviewer", titleFieldId: "displayName", modelId: REVIEWER_MODEL_ID, diff --git a/packages/api-apw/src/storageOperations/models/workflow.model.ts b/packages/api-apw/src/storageOperations/models/workflow.model.ts index 0048b05c80c..249fb0c4089 100644 --- a/packages/api-apw/src/storageOperations/models/workflow.model.ts +++ b/packages/api-apw/src/storageOperations/models/workflow.model.ts @@ -1,4 +1,4 @@ -import { createPrivateModelDefinition } from "@webiny/api-headless-cms"; +import { createPrivateModel } from "@webiny/api-headless-cms"; import { CmsModelField } from "@webiny/api-headless-cms/types"; import { createModelField } from "./utils"; import { WorkflowScopeTypes } from "~/types"; @@ -208,10 +208,8 @@ interface CreateWorkflowModelDefinitionParams { export const WORKFLOW_MODEL_ID = "apwWorkflowModelDefinition"; -export const createWorkflowModelDefinition = ({ - reviewerModelId -}: CreateWorkflowModelDefinitionParams) => { - return createPrivateModelDefinition({ +export const createWorkflowModel = ({ reviewerModelId }: CreateWorkflowModelDefinitionParams) => { + return createPrivateModel({ name: "APW - Workflow", /** * Id of the model cannot be appWorkflow because it clashes with the GraphQL types for APW. diff --git a/packages/api-file-manager/src/FileManagerContextSetup.ts b/packages/api-file-manager/src/FileManagerContextSetup.ts index f8ff48afd09..981408b56c7 100644 --- a/packages/api-file-manager/src/FileManagerContextSetup.ts +++ b/packages/api-file-manager/src/FileManagerContextSetup.ts @@ -3,12 +3,10 @@ import { createFileManager, FileManagerConfig } from "~/createFileManager"; import { FileStorage } from "~/storage/FileStorage"; import WebinyError from "@webiny/error"; import { SecurityPermission } from "@webiny/api-security/types"; -import { isInstallationPending } from "~/cmsFileStorage/isInstallationPending"; -import { createFileModel } from "~/cmsFileStorage/createFileManagerPlugins"; -import { FILE_MODEL_ID } from "~/cmsFileStorage/file.model"; +import { createFileModel, FILE_MODEL_ID } from "~/cmsFileStorage/file.model"; import { CmsFilesStorage } from "~/cmsFileStorage/CmsFilesStorage"; import { CmsModelModifierPlugin } from "~/modelModifier/CmsModelModifier"; -import { CmsModelPlugin } from "@webiny/api-headless-cms"; +import { CmsModelPlugin, isHeadlessCmsReady } from "@webiny/api-headless-cms"; import { FilesPermissions } from "~/createFileManager/permissions/FilesPermissions"; export class FileManagerContextSetup { @@ -78,7 +76,7 @@ export class FileManagerContextSetup { } private async setupCmsStorageOperations(aliases: FileManagerAliasesStorageOperations) { - if (isInstallationPending({ tenancy: this.context.tenancy, i18n: this.context.i18n })) { + if (!(await isHeadlessCmsReady(this.context))) { console.log("Installation pending!"); return; } @@ -86,7 +84,7 @@ export class FileManagerContextSetup { const withPrivateFiles = this.context.wcp.canUsePrivateFiles(); // This registers code plugins (model group, models) - const { fileModelDefinition } = createFileModel({ withPrivateFiles }); + const fileModelDefinition = createFileModel({ withPrivateFiles }); const modelModifiers = this.context.plugins.byType( CmsModelModifierPlugin.type diff --git a/packages/api-file-manager/src/cmsFileStorage/createFileManagerPlugins.ts b/packages/api-file-manager/src/cmsFileStorage/createFileManagerPlugins.ts deleted file mode 100644 index 100c44db73e..00000000000 --- a/packages/api-file-manager/src/cmsFileStorage/createFileManagerPlugins.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { createFileModelDefinition } from "~/cmsFileStorage/file.model"; - -interface CreateFileManagerPluginsParams { - withPrivateFiles: boolean; -} - -export const createFileModel = (params: CreateFileManagerPluginsParams) => { - return { - fileModelDefinition: createFileModelDefinition({ - withPrivateFiles: params.withPrivateFiles - }) - }; -}; diff --git a/packages/api-file-manager/src/cmsFileStorage/file.model.ts b/packages/api-file-manager/src/cmsFileStorage/file.model.ts index f1d28112052..28b46302c97 100644 --- a/packages/api-file-manager/src/cmsFileStorage/file.model.ts +++ b/packages/api-file-manager/src/cmsFileStorage/file.model.ts @@ -1,5 +1,4 @@ -import { createPrivateModelDefinition } from "@webiny/api-headless-cms"; -import { createModelField } from "./createModelField"; +import { createPrivateModel, createModelField } from "@webiny/api-headless-cms"; const required = () => { return { @@ -160,7 +159,7 @@ interface CreateFileModelDefinitionParams { withPrivateFiles: boolean; } -export const createFileModelDefinition = (params: CreateFileModelDefinitionParams) => { +export const createFileModel = (params: CreateFileModelDefinitionParams) => { const fields = [ locationField(), nameField(), @@ -176,7 +175,7 @@ export const createFileModelDefinition = (params: CreateFileModelDefinitionParam fields.push(accessControlField()); } - return createPrivateModelDefinition({ + return createPrivateModel({ name: "FmFile", modelId: FILE_MODEL_ID, titleFieldId: "name", diff --git a/packages/api-file-manager/src/cmsFileStorage/isInstallationPending.ts b/packages/api-file-manager/src/cmsFileStorage/isInstallationPending.ts deleted file mode 100644 index 0bad1f423b3..00000000000 --- a/packages/api-file-manager/src/cmsFileStorage/isInstallationPending.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { FileManagerContext } from "~/types"; - -type CheckInstallationParams = Pick; -export const isInstallationPending = ({ tenancy, i18n }: CheckInstallationParams): boolean => { - /** - * In case of a fresh webiny project "tenant" and "locale" won't be there until - * installation is completed. So, we need to skip "storage" creation till then. - */ - const tenant = tenancy.getCurrentTenant(); - if (!tenant) { - return true; - } - - return !i18n.getContentLocale(); -}; diff --git a/packages/api-file-manager/src/graphql/index.ts b/packages/api-file-manager/src/graphql/index.ts index e829ec5a5b2..ce146c0574d 100644 --- a/packages/api-file-manager/src/graphql/index.ts +++ b/packages/api-file-manager/src/graphql/index.ts @@ -1,11 +1,11 @@ -import { createBaseSchema } from "~/graphql/baseSchema"; import { ContextPlugin } from "@webiny/api"; +import { isHeadlessCmsReady } from "@webiny/api-headless-cms"; import { CmsModel } from "@webiny/api-headless-cms/types"; import { createFieldTypePluginRecords } from "@webiny/api-headless-cms/graphql/schema/createFieldTypePluginRecords"; import { createGraphQLSchemaPluginFromFieldPlugins } from "@webiny/api-headless-cms/utils/getSchemaFromFieldPlugins"; +import { createBaseSchema } from "~/graphql/baseSchema"; import { GraphQLSchemaPlugin } from "@webiny/handler-graphql"; import { createFilesSchema } from "~/graphql/filesSchema"; -import { isInstallationPending } from "~/cmsFileStorage/isInstallationPending"; import { getFileByUrl } from "~/graphql/getFileByUrl"; import { FileManagerContext } from "~/types"; @@ -15,7 +15,7 @@ export const createGraphQLSchemaPlugin = () => { // Files schema is generated dynamically, based on a CMS model, so we need to // register it from a ContextPlugin, to perform additional bootstrap. new ContextPlugin(async context => { - if (isInstallationPending(context)) { + if (!(await isHeadlessCmsReady(context))) { return; } diff --git a/packages/api-file-manager/src/modelModifier/CmsModelModifier.ts b/packages/api-file-manager/src/modelModifier/CmsModelModifier.ts index eb6e427ee85..e697248dc27 100644 --- a/packages/api-file-manager/src/modelModifier/CmsModelModifier.ts +++ b/packages/api-file-manager/src/modelModifier/CmsModelModifier.ts @@ -1,8 +1,7 @@ import { Plugin } from "@webiny/plugins"; import { CmsModelField as BaseModelField } from "@webiny/api-headless-cms/types"; +import { CmsPrivateModelFull, createModelField } from "@webiny/api-headless-cms"; import { FILE_MODEL_ID } from "~/cmsFileStorage/file.model"; -import { createModelField } from "~/cmsFileStorage/createModelField"; -import { CmsPrivateModelFull } from "@webiny/api-headless-cms"; type CmsModelField = Omit & { bulkEdit?: boolean }; diff --git a/packages/api-headless-cms-ddb-es/__tests__/tasks/mocks/models.ts b/packages/api-headless-cms-ddb-es/__tests__/tasks/mocks/models.ts index 652240a209f..4fba114ac42 100644 --- a/packages/api-headless-cms-ddb-es/__tests__/tasks/mocks/models.ts +++ b/packages/api-headless-cms-ddb-es/__tests__/tasks/mocks/models.ts @@ -2,7 +2,7 @@ import { CmsGroup, createCmsGroup, createCmsModel, - createPrivateModelDefinition + createPrivateModel } from "@webiny/api-headless-cms"; export const createMockModels = () => { @@ -64,7 +64,7 @@ export const createMockModels = () => { titleFieldId: "title" }), createCmsModel( - createPrivateModelDefinition({ + createPrivateModel({ modelId: "tag", name: "Tag", fields: [], diff --git a/packages/api-headless-cms/src/index.ts b/packages/api-headless-cms/src/index.ts index 47803660921..51b9566a5b7 100644 --- a/packages/api-headless-cms/src/index.ts +++ b/packages/api-headless-cms/src/index.ts @@ -18,6 +18,8 @@ import { createFieldConverters } from "~/fieldConverters"; import { createExportGraphQL } from "~/export"; import { createStorageTransform } from "~/storage"; import { createLexicalHTMLRenderer } from "./htmlRenderer/createLexicalHTMLRenderer"; +export * from "./utils/isHeadlessCmsReady"; +export * from "./utils/createModelField"; export type CreateHeadlessCmsGraphQLParams = CreateGraphQLParams; export const createHeadlessCmsGraphQL = (params: CreateHeadlessCmsGraphQLParams = {}) => { diff --git a/packages/api-headless-cms/src/plugins/CmsModelPlugin.ts b/packages/api-headless-cms/src/plugins/CmsModelPlugin.ts index 590da312e29..e678e77eb10 100644 --- a/packages/api-headless-cms/src/plugins/CmsModelPlugin.ts +++ b/packages/api-headless-cms/src/plugins/CmsModelPlugin.ts @@ -340,7 +340,7 @@ export const createCmsModel = ( return new CmsModelPlugin(model, options); }; -export const createPrivateModelDefinition = ( +export const createPrivateModel = ( input: Omit ): CmsPrivateModelFull => { return { diff --git a/packages/api-file-manager/src/cmsFileStorage/createModelField.ts b/packages/api-headless-cms/src/utils/createModelField.ts similarity index 93% rename from packages/api-file-manager/src/cmsFileStorage/createModelField.ts rename to packages/api-headless-cms/src/utils/createModelField.ts index ba46ea3dcc2..65447386308 100644 --- a/packages/api-file-manager/src/cmsFileStorage/createModelField.ts +++ b/packages/api-headless-cms/src/utils/createModelField.ts @@ -1,5 +1,5 @@ -import { CmsModelField } from "@webiny/api-headless-cms/types"; import camelCase from "lodash/camelCase"; +import { CmsModelField } from "~/types"; export interface CreateModelFieldParams extends Omit { diff --git a/packages/api-headless-cms/src/utils/isHeadlessCmsReady.ts b/packages/api-headless-cms/src/utils/isHeadlessCmsReady.ts new file mode 100644 index 00000000000..60fbf46cd84 --- /dev/null +++ b/packages/api-headless-cms/src/utils/isHeadlessCmsReady.ts @@ -0,0 +1,15 @@ +import { CmsContext } from "~/types"; + +export const isHeadlessCmsReady = async ({ tenancy, i18n }: CmsContext): Promise => { + /** + * In case of a fresh webiny project "tenant" and "locale" won't be there until the installation is completed. + */ + const tenant = tenancy.getCurrentTenant(); + if (!tenant) { + return false; + } + + const locale = i18n.getContentLocale(); + + return locale !== undefined; +}; diff --git a/packages/api-mailer/src/crud/settings/model.ts b/packages/api-mailer/src/crud/settings/model.ts index 33b15af2dd9..1bf252cd138 100644 --- a/packages/api-mailer/src/crud/settings/model.ts +++ b/packages/api-mailer/src/crud/settings/model.ts @@ -1,5 +1,5 @@ import lodashCamelCase from "lodash/camelCase"; -import { createCmsModel, createPrivateModelDefinition } from "@webiny/api-headless-cms"; +import { createCmsModel, createPrivateModel } from "@webiny/api-headless-cms"; import { CmsModelField, CmsModelFieldValidation } from "@webiny/api-headless-cms/types"; export const SETTINGS_MODEL_ID = "mailerSettings"; @@ -46,7 +46,7 @@ const createNumberField = (params: Omit) => { export const createSettingsModel = () => { return createCmsModel( - createPrivateModelDefinition({ + createPrivateModel({ name: "Mailer Settings", modelId: SETTINGS_MODEL_ID, fields: [ diff --git a/packages/tasks/src/crud/model.ts b/packages/tasks/src/crud/model.ts index ffdede229d5..d91b099c6af 100644 --- a/packages/tasks/src/crud/model.ts +++ b/packages/tasks/src/crud/model.ts @@ -1,11 +1,11 @@ -import { createCmsModel, createPrivateModelDefinition } from "@webiny/api-headless-cms"; +import { createCmsModel, createPrivateModel } from "@webiny/api-headless-cms"; import { ITaskLogItemType, TaskDataStatus } from "~/types"; export const WEBINY_TASK_MODEL_ID = "webinyTask"; export const WEBINY_TASK_LOG_MODEL_ID = "webinyTaskLog"; const taskLogModelPlugin = createCmsModel( - createPrivateModelDefinition({ + createPrivateModel({ name: "Webiny Task Log", modelId: WEBINY_TASK_LOG_MODEL_ID, fields: [ @@ -137,7 +137,7 @@ const taskLogModelPlugin = createCmsModel( ); const taskModelPlugin = createCmsModel( - createPrivateModelDefinition({ + createPrivateModel({ name: "Webiny Task", modelId: WEBINY_TASK_MODEL_ID, fields: [