From 85f4bdc2145aebb00a05bdb5570aff991dff6dac Mon Sep 17 00:00:00 2001 From: Simone Date: Thu, 14 Mar 2024 15:26:13 +0100 Subject: [PATCH] chore: helper-plugin remove prefixFileUrlWithBackendUrl (#19764) * chore: remove prefixFileUrlWithBackendUrl from the helper plugin * chore: move the prefixFileUrlWithBackend in an util inside the admin * chore: remove commented code --- .../FormInputs/BlocksInput/Blocks/Image.tsx | 3 ++- .../EditView/components/FormInputs/Wysiwyg/Field.tsx | 3 ++- .../pages/ListView/components/TableCells/Media.tsx | 3 ++- packages/core/admin/admin/src/services/admin.ts | 3 +-- .../admin/src/utils/urls.ts} | 0 packages/core/helper-plugin/MIGRATION_GUIDE.md | 4 ++++ packages/core/helper-plugin/src/index.ts | 1 - .../admin/src/components/AssetCard/AssetCard.jsx | 3 +-- .../Carousel/CarouselAssetActions.jsx | 2 +- .../admin/src/components/TableList/PreviewCell.jsx | 3 +-- .../core/upload/admin/src/utils/createAssetUrl.js | 2 +- packages/core/upload/admin/src/utils/index.js | 1 + .../admin/src/utils/prefixFileUrlWithBackendUrl.js | 12 ++++++++++++ .../utils/tests/prefixFileUrlWithBackendUrl.test.js} | 4 ++-- 14 files changed, 30 insertions(+), 14 deletions(-) rename packages/core/{helper-plugin/src/utils/prefixFileUrlWithBackendUrl.ts => admin/admin/src/utils/urls.ts} (100%) create mode 100644 packages/core/upload/admin/src/utils/prefixFileUrlWithBackendUrl.js rename packages/core/{helper-plugin/src/utils/tests/prefixFileUrlWithBackendUrl.test.ts => upload/admin/src/utils/tests/prefixFileUrlWithBackendUrl.test.js} (82%) diff --git a/packages/core/admin/admin/src/content-manager/pages/EditView/components/FormInputs/BlocksInput/Blocks/Image.tsx b/packages/core/admin/admin/src/content-manager/pages/EditView/components/FormInputs/BlocksInput/Blocks/Image.tsx index 918f4fcb4b..dcffc7f64a 100644 --- a/packages/core/admin/admin/src/content-manager/pages/EditView/components/FormInputs/BlocksInput/Blocks/Image.tsx +++ b/packages/core/admin/admin/src/content-manager/pages/EditView/components/FormInputs/BlocksInput/Blocks/Image.tsx @@ -1,13 +1,14 @@ import * as React from 'react'; import { Box, Flex } from '@strapi/design-system'; -import { prefixFileUrlWithBackendUrl, useLibrary } from '@strapi/helper-plugin'; +import { useLibrary } from '@strapi/helper-plugin'; import { Picture } from '@strapi/icons'; import { type Attribute } from '@strapi/types'; import { type Element, Transforms, Editor } from 'slate'; import { useFocused, type RenderElementProps, useSelected } from 'slate-react'; import styled, { css } from 'styled-components'; +import { prefixFileUrlWithBackendUrl } from '../../../../../../../utils/urls'; import { useBlocksEditorContext, type BlocksStore } from '../BlocksEditor'; import { type Block } from '../utils/types'; diff --git a/packages/core/admin/admin/src/content-manager/pages/EditView/components/FormInputs/Wysiwyg/Field.tsx b/packages/core/admin/admin/src/content-manager/pages/EditView/components/FormInputs/Wysiwyg/Field.tsx index 760987308c..5cfb6d8353 100644 --- a/packages/core/admin/admin/src/content-manager/pages/EditView/components/FormInputs/Wysiwyg/Field.tsx +++ b/packages/core/admin/admin/src/content-manager/pages/EditView/components/FormInputs/Wysiwyg/Field.tsx @@ -1,10 +1,11 @@ import * as React from 'react'; import { Field, FieldError, FieldHint, FieldLabel, Flex } from '@strapi/design-system'; -import { prefixFileUrlWithBackendUrl, useLibrary } from '@strapi/helper-plugin'; +import { useLibrary } from '@strapi/helper-plugin'; import { EditorFromTextArea } from 'codemirror5'; import { useField } from '../../../../../../components/Form'; +import { prefixFileUrlWithBackendUrl } from '../../../../../../utils/urls'; import { Editor, EditorApi } from './Editor'; import { EditorLayout } from './EditorLayout'; diff --git a/packages/core/admin/admin/src/content-manager/pages/ListView/components/TableCells/Media.tsx b/packages/core/admin/admin/src/content-manager/pages/ListView/components/TableCells/Media.tsx index fc3388a42b..cec9893124 100644 --- a/packages/core/admin/admin/src/content-manager/pages/ListView/components/TableCells/Media.tsx +++ b/packages/core/admin/admin/src/content-manager/pages/ListView/components/TableCells/Media.tsx @@ -1,9 +1,10 @@ import * as React from 'react'; import { Avatar, AvatarGroup, Flex, Tooltip, Typography } from '@strapi/design-system'; -import { prefixFileUrlWithBackendUrl } from '@strapi/helper-plugin'; import styled from 'styled-components'; +import { prefixFileUrlWithBackendUrl } from '../../../../../utils/urls'; + import type { Entity } from '@strapi/types'; interface MediaFile { diff --git a/packages/core/admin/admin/src/services/admin.ts b/packages/core/admin/admin/src/services/admin.ts index 23e70cc952..f77c7af239 100644 --- a/packages/core/admin/admin/src/services/admin.ts +++ b/packages/core/admin/admin/src/services/admin.ts @@ -1,5 +1,3 @@ -import { prefixFileUrlWithBackendUrl } from '@strapi/helper-plugin'; - import { type TelemetryProperties, type Init, @@ -9,6 +7,7 @@ import { type Plugins, type GetLicenseLimitInformation, } from '../../../shared/contracts/admin'; +import { prefixFileUrlWithBackendUrl } from '../utils/urls'; import { adminApi } from './api'; diff --git a/packages/core/helper-plugin/src/utils/prefixFileUrlWithBackendUrl.ts b/packages/core/admin/admin/src/utils/urls.ts similarity index 100% rename from packages/core/helper-plugin/src/utils/prefixFileUrlWithBackendUrl.ts rename to packages/core/admin/admin/src/utils/urls.ts diff --git a/packages/core/helper-plugin/MIGRATION_GUIDE.md b/packages/core/helper-plugin/MIGRATION_GUIDE.md index 0bc11b8d46..1ff901bb4c 100644 --- a/packages/core/helper-plugin/MIGRATION_GUIDE.md +++ b/packages/core/helper-plugin/MIGRATION_GUIDE.md @@ -482,6 +482,10 @@ This util has been removed and not replaced. If you feel like you need this util This util has been removed and not replaced. If you feel like you need this util, please open an issue on the Strapi repository to discuss your usecase. +### prefixFileUrlWithBackendUrl + +This util has been removed and not replaced. Use the strapi backendUrl to prefix the relative url if you need. If you feel like you need this util, please open an issue on the Strapi repository to discuss your usecase. + ### pxToRem This util has been removed and not replaced. You should use directly this code in place of the pxToRem: diff --git a/packages/core/helper-plugin/src/index.ts b/packages/core/helper-plugin/src/index.ts index 1d7f93f1b0..74a985f991 100644 --- a/packages/core/helper-plugin/src/index.ts +++ b/packages/core/helper-plugin/src/index.ts @@ -49,7 +49,6 @@ export * from './utils/getFetchClient'; export * from './utils/getYupInnerErrors'; export * from './utils/hasPermissions'; export * from './utils/normalizeAPIError'; -export * from './utils/prefixFileUrlWithBackendUrl'; export * from './utils/prefixPluginTranslations'; export * from './utils/stopPropagation'; export * from './utils/translatedErrors'; diff --git a/packages/core/upload/admin/src/components/AssetCard/AssetCard.jsx b/packages/core/upload/admin/src/components/AssetCard/AssetCard.jsx index 4a20ed5c55..431bd8311a 100644 --- a/packages/core/upload/admin/src/components/AssetCard/AssetCard.jsx +++ b/packages/core/upload/admin/src/components/AssetCard/AssetCard.jsx @@ -1,10 +1,9 @@ import React from 'react'; -import { prefixFileUrlWithBackendUrl } from '@strapi/helper-plugin'; import PropTypes from 'prop-types'; import { AssetDefinition, AssetType } from '../../constants'; -import { createAssetUrl, getFileExtension } from '../../utils'; +import { createAssetUrl, getFileExtension, prefixFileUrlWithBackendUrl } from '../../utils'; import { AudioAssetCard } from './AudioAssetCard'; import { DocAssetCard } from './DocAssetCard'; diff --git a/packages/core/upload/admin/src/components/MediaLibraryInput/Carousel/CarouselAssetActions.jsx b/packages/core/upload/admin/src/components/MediaLibraryInput/Carousel/CarouselAssetActions.jsx index 134c633981..2b408c2bf0 100644 --- a/packages/core/upload/admin/src/components/MediaLibraryInput/Carousel/CarouselAssetActions.jsx +++ b/packages/core/upload/admin/src/components/MediaLibraryInput/Carousel/CarouselAssetActions.jsx @@ -1,12 +1,12 @@ import React from 'react'; import { CarouselActions, IconButton } from '@strapi/design-system'; -import { prefixFileUrlWithBackendUrl } from '@strapi/helper-plugin'; import { Pencil, Plus, Trash } from '@strapi/icons'; import PropTypes from 'prop-types'; import { useIntl } from 'react-intl'; import { AssetDefinition } from '../../../constants'; +import { prefixFileUrlWithBackendUrl } from '../../../utils'; import getTrad from '../../../utils/getTrad'; import { CopyLinkButton } from '../../CopyLinkButton'; diff --git a/packages/core/upload/admin/src/components/TableList/PreviewCell.jsx b/packages/core/upload/admin/src/components/TableList/PreviewCell.jsx index 24f30f58ce..2f72ce31bf 100644 --- a/packages/core/upload/admin/src/components/TableList/PreviewCell.jsx +++ b/packages/core/upload/admin/src/components/TableList/PreviewCell.jsx @@ -1,13 +1,12 @@ import React from 'react'; import { Avatar, Box, Icon, Initials } from '@strapi/design-system'; -import { prefixFileUrlWithBackendUrl } from '@strapi/helper-plugin'; import { Folder } from '@strapi/icons'; import PropTypes from 'prop-types'; import styled from 'styled-components'; import { AssetType } from '../../constants'; -import { createAssetUrl, getFileExtension } from '../../utils'; +import { createAssetUrl, getFileExtension, prefixFileUrlWithBackendUrl } from '../../utils'; import { VideoPreview } from '../AssetCard/VideoPreview'; const VideoPreviewWrapper = styled(Box)` diff --git a/packages/core/upload/admin/src/utils/createAssetUrl.js b/packages/core/upload/admin/src/utils/createAssetUrl.js index f45f3857b2..a70ff33ee3 100644 --- a/packages/core/upload/admin/src/utils/createAssetUrl.js +++ b/packages/core/upload/admin/src/utils/createAssetUrl.js @@ -1,4 +1,4 @@ -import { prefixFileUrlWithBackendUrl } from '@strapi/helper-plugin'; +import prefixFileUrlWithBackendUrl from './prefixFileUrlWithBackendUrl'; /** * Create image URL for asset diff --git a/packages/core/upload/admin/src/utils/index.js b/packages/core/upload/admin/src/utils/index.js index 7d76e557f2..38538c3391 100644 --- a/packages/core/upload/admin/src/utils/index.js +++ b/packages/core/upload/admin/src/utils/index.js @@ -12,3 +12,4 @@ export { default as getFolderURL } from './getFolderURL'; export { default as getTrad } from './getTrad'; export { default as toSingularTypes } from './toSingularTypes'; export { default as getFileExtension } from './getFileExtension'; +export { default as prefixFileUrlWithBackendUrl } from './prefixFileUrlWithBackendUrl'; diff --git a/packages/core/upload/admin/src/utils/prefixFileUrlWithBackendUrl.js b/packages/core/upload/admin/src/utils/prefixFileUrlWithBackendUrl.js new file mode 100644 index 0000000000..9a1c48f941 --- /dev/null +++ b/packages/core/upload/admin/src/utils/prefixFileUrlWithBackendUrl.js @@ -0,0 +1,12 @@ +/** + * Create the file URL with the backend URL + * @param {Object} asset + * @param {String} fileURL - if true, return the file URL with the Backend url + * if there's no file url or it doesn't start with a slash return the original file url. + * @return {String} file Url + */ +const prefixFileUrlWithBackendUrl = (fileURL) => { + return !!fileURL && fileURL.startsWith('/') ? `${window.strapi.backendURL}${fileURL}` : fileURL; +}; + +export default prefixFileUrlWithBackendUrl; diff --git a/packages/core/helper-plugin/src/utils/tests/prefixFileUrlWithBackendUrl.test.ts b/packages/core/upload/admin/src/utils/tests/prefixFileUrlWithBackendUrl.test.js similarity index 82% rename from packages/core/helper-plugin/src/utils/tests/prefixFileUrlWithBackendUrl.test.ts rename to packages/core/upload/admin/src/utils/tests/prefixFileUrlWithBackendUrl.test.js index 85040628f6..65e688dc46 100644 --- a/packages/core/helper-plugin/src/utils/tests/prefixFileUrlWithBackendUrl.test.ts +++ b/packages/core/upload/admin/src/utils/tests/prefixFileUrlWithBackendUrl.test.js @@ -1,6 +1,6 @@ -import { prefixFileUrlWithBackendUrl } from '../prefixFileUrlWithBackendUrl'; +import prefixFileUrlWithBackendUrl from '../prefixFileUrlWithBackendUrl'; -describe('HELPER_PLUGIN | utils | prefixFileUrlWithBackendUrl', () => { +describe('prefixFileUrlWithBackendUrl', () => { it("should add the strapi back-end url if the file's url startsWith '/'", () => { const data = '/upload/test'; const expected = 'http://localhost:1337/upload/test';