diff --git a/packages/live-preview-sdk/package.json b/packages/live-preview-sdk/package.json index 72140e3e..e50381b9 100644 --- a/packages/live-preview-sdk/package.json +++ b/packages/live-preview-sdk/package.json @@ -45,7 +45,6 @@ "homepage": "https://github.com/contentful/live-preview#readme", "dependencies": { "@contentful/rich-text-types": "^16.2.0", - "@contentful/visual-sdk": "1.0.0-alpha.39", "flatted": "^3.2.9", "graphql-tag": "^2.12.6", "json-pointer": "^0.6.2", diff --git a/packages/live-preview-sdk/src/messages.ts b/packages/live-preview-sdk/src/messages.ts index 58d93c80..6f4e655a 100644 --- a/packages/live-preview-sdk/src/messages.ts +++ b/packages/live-preview-sdk/src/messages.ts @@ -1,5 +1,3 @@ -import type { RequestEntitiesMessage, RequestedEntitiesMessage } from '@contentful/visual-sdk'; -import { PostMessageMethods as StorePostMessageMethods } from '@contentful/visual-sdk'; import type { Asset, Entry } from 'contentful'; import type { SysLink } from 'contentful-management'; @@ -42,18 +40,9 @@ enum LivePreviewPostMessageMethods { UNKNOWN_REFERENCE_LOADED = 'UNKNOWN_REFERENCE_LOADED', } -export { - InspectorModeEventMethods, - LivePreviewPostMessageMethods, - RequestEntitiesMessage, - RequestedEntitiesMessage, - StorePostMessageMethods, -}; +export { InspectorModeEventMethods, LivePreviewPostMessageMethods }; -export type PostMessageMethods = - | LivePreviewPostMessageMethods - | StorePostMessageMethods - | InspectorModeEventMethods; +export type PostMessageMethods = LivePreviewPostMessageMethods | InspectorModeEventMethods; export type ConnectedMessage = { /** @deprecated use method instead */ @@ -112,7 +101,6 @@ export type EditorMessage = | UnknownEntityMessage | UrlChangedMessage | SubscribedMessage - | RequestEntitiesMessage | ErrorMessage | InspectorModeMouseMoveMessage | InspectorModeScrollMessage @@ -162,7 +150,6 @@ export type MessageFromEditor = ( | UnknownReferenceLoaded | InspectorModeChangedMessage | DebugModeEnabledMessage - | RequestedEntitiesMessage ) & { data: Argument; method: PostMessageMethods; diff --git a/packages/visual-sdk/.eslintrc.cjs b/packages/visual-sdk/.eslintrc.cjs deleted file mode 100644 index 5824a6f5..00000000 --- a/packages/visual-sdk/.eslintrc.cjs +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = { - extends: require.resolve('../../.eslintrc.cjs') -}; diff --git a/packages/visual-sdk/.swcrc b/packages/visual-sdk/.swcrc deleted file mode 100644 index f9dfcd0d..00000000 --- a/packages/visual-sdk/.swcrc +++ /dev/null @@ -1,18 +0,0 @@ -{ - "jsc": { - "parser": { - "syntax": "typescript", - "tsx": true - }, - "target": "es2020", - "loose": false, - "minify": { - "compress": false, - "mangle": false - } - }, - "module": { - "type": "es6" - }, - "minify": false -} diff --git a/packages/visual-sdk/CHANGELOG.md b/packages/visual-sdk/CHANGELOG.md deleted file mode 100644 index 7eb90c76..00000000 --- a/packages/visual-sdk/CHANGELOG.md +++ /dev/null @@ -1,333 +0,0 @@ -# Change Log - -All notable changes to this project will be documented in this file. -See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. - -# [1.0.0-alpha.39](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.38...@contentful/visual-sdk@1.0.0-alpha.39) (2023-12-27) - - -### Bug Fixes - -* print link with ID instead of object Object when logging the link ([#484](https://github.com/contentful/live-preview/issues/484)) ([9fb3510](https://github.com/contentful/live-preview/commit/9fb35107bfb37f204c41c12d4a771a6e15244e2f)) - - - - - -# [1.0.0-alpha.38](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.37...@contentful/visual-sdk@1.0.0-alpha.38) (2023-12-21) - - -### Bug Fixes - -* accept and handle list of missing entity IDs ([#483](https://github.com/contentful/live-preview/issues/483)) ([e6d52f1](https://github.com/contentful/live-preview/commit/e6d52f104c09af7ca19e6e7a8869f10dfc9c5be2)) - - - - - -# [1.0.0-alpha.37](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.36...@contentful/visual-sdk@1.0.0-alpha.37) (2023-12-21) - - -### Bug Fixes - -* cleanup request cache after timeout [SPA-1711] ([#482](https://github.com/contentful/live-preview/issues/482)) ([87000b4](https://github.com/contentful/live-preview/commit/87000b4bf29cca6829ee2f3e4b0f33811ef1fde2)) - - - - - -# [1.0.0-alpha.36](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.35...@contentful/visual-sdk@1.0.0-alpha.36) (2023-12-12) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.35](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.34...@contentful/visual-sdk@1.0.0-alpha.35) (2023-11-28) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.34](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.33...@contentful/visual-sdk@1.0.0-alpha.34) (2023-11-27) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.33](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.32...@contentful/visual-sdk@1.0.0-alpha.33) (2023-11-27) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.32](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.31...@contentful/visual-sdk@1.0.0-alpha.32) (2023-11-20) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.31](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.30...@contentful/visual-sdk@1.0.0-alpha.31) (2023-11-17) - - -### Bug Fixes - -* **entity-store:** split into asset and entry map to prevent id collision [] ([#406](https://github.com/contentful/live-preview/issues/406)) ([8689a1e](https://github.com/contentful/live-preview/commit/8689a1e388dfa290d4656f0380cb1ecdc642331c)) - - - - - -# [1.0.0-alpha.30](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.29...@contentful/visual-sdk@1.0.0-alpha.30) (2023-11-13) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.29](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.28...@contentful/visual-sdk@1.0.0-alpha.29) (2023-11-13) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.28](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.27...@contentful/visual-sdk@1.0.0-alpha.28) (2023-11-06) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.27](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.26...@contentful/visual-sdk@1.0.0-alpha.27) (2023-10-30) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.26](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.25...@contentful/visual-sdk@1.0.0-alpha.26) (2023-10-23) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.25](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.24...@contentful/visual-sdk@1.0.0-alpha.25) (2023-10-23) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.24](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.23...@contentful/visual-sdk@1.0.0-alpha.24) (2023-10-16) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.23](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.22...@contentful/visual-sdk@1.0.0-alpha.23) (2023-10-09) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.22](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.21...@contentful/visual-sdk@1.0.0-alpha.22) (2023-10-02) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.21](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.20...@contentful/visual-sdk@1.0.0-alpha.21) (2023-09-25) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.20](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.19...@contentful/visual-sdk@1.0.0-alpha.20) (2023-09-18) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.19](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.18...@contentful/visual-sdk@1.0.0-alpha.19) (2023-09-18) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.18](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.17...@contentful/visual-sdk@1.0.0-alpha.18) (2023-09-18) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.17](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.16...@contentful/visual-sdk@1.0.0-alpha.17) (2023-09-11) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.16](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.15...@contentful/visual-sdk@1.0.0-alpha.16) (2023-09-04) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.15](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.14...@contentful/visual-sdk@1.0.0-alpha.15) (2023-08-28) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.14](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.13...@contentful/visual-sdk@1.0.0-alpha.14) (2023-08-28) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.13](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.12...@contentful/visual-sdk@1.0.0-alpha.13) (2023-08-28) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.12](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.11...@contentful/visual-sdk@1.0.0-alpha.12) (2023-08-21) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.11](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.10...@contentful/visual-sdk@1.0.0-alpha.11) (2023-08-14) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.10](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.9...@contentful/visual-sdk@1.0.0-alpha.10) (2023-08-14) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.9](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.8...@contentful/visual-sdk@1.0.0-alpha.9) (2023-08-07) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.8](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.7...@contentful/visual-sdk@1.0.0-alpha.8) (2023-08-07) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.7](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.6...@contentful/visual-sdk@1.0.0-alpha.7) (2023-07-31) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.6](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.5...@contentful/visual-sdk@1.0.0-alpha.6) (2023-07-27) - - -### Features - -* support fallback locales ([a9c871a](https://github.com/contentful/live-preview/commit/a9c871a8ad5f2a2fbd8d213cbc84a63774944865)) - - - - - -# [1.0.0-alpha.5](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.4...@contentful/visual-sdk@1.0.0-alpha.5) (2023-07-24) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.4](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.3...@contentful/visual-sdk@1.0.0-alpha.4) (2023-07-24) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# [1.0.0-alpha.3](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.2...@contentful/visual-sdk@1.0.0-alpha.3) (2023-07-21) - - -### Bug Fixes - -* **types:** optimize export of typings ([1b57863](https://github.com/contentful/live-preview/commit/1b578639ad91683e32871af1e48d619e47db0eb2)) - - - - - -# [1.0.0-alpha.2](https://github.com/contentful/live-preview/compare/@contentful/visual-sdk@1.0.0-alpha.1...@contentful/visual-sdk@1.0.0-alpha.2) (2023-07-17) - -**Note:** Version bump only for package @contentful/visual-sdk - - - - - -# 1.0.0-alpha.1 (2023-07-13) - - -### Features - -* **sdk:** extract entity store for re-usability ([a8fbd76](https://github.com/contentful/live-preview/commit/a8fbd76d982c0a247ca84f51677b3250bc4d3e3c)) diff --git a/packages/visual-sdk/README.md b/packages/visual-sdk/README.md deleted file mode 100644 index e8643100..00000000 --- a/packages/visual-sdk/README.md +++ /dev/null @@ -1,5 +0,0 @@ -# Visual SDK - -> The base for visual editing SDK's of contentful. - -To be continued.. diff --git a/packages/visual-sdk/jest.config.cjs b/packages/visual-sdk/jest.config.cjs deleted file mode 100644 index 795824c3..00000000 --- a/packages/visual-sdk/jest.config.cjs +++ /dev/null @@ -1,7 +0,0 @@ -module.exports = { - modulePathIgnorePatterns: ['/dist/'], - testMatch: ['**/?(*.)+(spec|test).[jt]s?(x)'], - transform: { - '^.+\\.(t|j)sx?$': '@swc/jest', - }, -}; diff --git a/packages/visual-sdk/package.json b/packages/visual-sdk/package.json deleted file mode 100644 index d248f634..00000000 --- a/packages/visual-sdk/package.json +++ /dev/null @@ -1,59 +0,0 @@ -{ - "name": "@contentful/visual-sdk", - "author": "Contentful GmbH", - "version": "1.0.0-alpha.39", - "description": "Base SDK for building a visual SDK with contentful", - "license": "MIT", - "main": "dist/index.js", - "type": "module", - "repository": { - "type": "git", - "url": "git+https://github.com/contentful/live-preview.git" - }, - "bugs": { - "url": "https://github.com/contentful/live-preview/issues" - }, - "homepage": "https://github.com/contentful/live-preview#readme", - "module": "dist/esm/index.js", - "types": "dist/types/index.d.ts", - "exports": { - ".": { - "types": "./dist/types/index.d.ts", - "require": "./dist/cjs/index.js", - "default": "./dist/esm/index.js" - }, - "./store": { - "types": "./dist/types/storesindex.d.ts", - "require": "./dist/cjs/store/index.js", - "default": "./dist/esm/store/index.js" - }, - "./package.json": "./package.json" - }, - "files": [ - "dist" - ], - "scripts": { - "start": "yarn concurrently \"yarn:start:*\"", - "start:cjs": "yarn build:cjs -w", - "start:esm": "yarn build:esm -w", - "build": "yarn concurrently \"yarn:build:*\"", - "build:esm": "swc src -d dist/esm", - "build:cjs": "swc src -d dist/cjs -C module.type=commonjs", - "build:types": "tsc --outDir dist/types", - "lint": "eslint ./src --ext .js,.jsx,.ts,.tsx", - "tsc": "tsc --noEmit", - "test": "jest --watch", - "test:ci": "jest", - "coverage": "jest --coverage", - "prepublish": "yarn build" - }, - "devDependencies": { - "@swc/cli": "0.1.63", - "@swc/core": "1.3.100", - "@swc/jest": "0.2.29", - "@types/jest": "29.5.11", - "contentful": "10.6.14", - "jest": "29.7.0", - "typescript": "5.1.6" - } -} diff --git a/packages/visual-sdk/src/__test__/fixtures/asset.json b/packages/visual-sdk/src/__test__/fixtures/asset.json deleted file mode 100644 index 226c593c..00000000 --- a/packages/visual-sdk/src/__test__/fixtures/asset.json +++ /dev/null @@ -1,67 +0,0 @@ -{ - "metadata": { - "tags": [] - }, - "sys": { - "space": { - "sys": { - "type": "Link", - "linkType": "Space", - "id": "unknown" - } - }, - "id": "3tfemZitZGCb42qEfO1ipW", - "type": "Asset", - "createdAt": "2023-01-25T16:22:01.053Z", - "updatedAt": "2023-06-07T13:32:40.991Z", - "environment": { - "sys": { - "id": "master", - "type": "Link", - "linkType": "Environment" - } - }, - "publishedVersion": 18, - "publishedAt": "2023-04-12T15:46:45.430Z", - "firstPublishedAt": "2023-01-25T16:22:11.114Z", - "createdBy": { - "sys": { - "type": "Link", - "linkType": "User", - "id": "unknown" - } - }, - "updatedBy": { - "sys": { - "type": "Link", - "linkType": "User", - "id": "unknown" - } - }, - "publishedCounter": 2, - "version": 22, - "publishedBy": { - "sys": { - "type": "Link", - "linkType": "User", - "id": "unknown" - } - }, - "revision": 1 - }, - "fields": { - "title": "GoCoin Classic Product shot", - "file": { - "url": "//images.ctfassets.net/c586i03gpxi9/3tfemZitZGCb42qEfO1ipW/06adc14d7c6371acbde3b17131824f4b/arizona_sonora_wueste_std-1.jpg", - "details": { - "size": 227840, - "image": { - "width": 1000, - "height": 750 - } - }, - "fileName": "arizona_sonora_wueste_std-1.jpg", - "contentType": "image/jpeg" - } - } -} diff --git a/packages/visual-sdk/src/__test__/fixtures/entry.json b/packages/visual-sdk/src/__test__/fixtures/entry.json deleted file mode 100644 index f7d5f39e..00000000 --- a/packages/visual-sdk/src/__test__/fixtures/entry.json +++ /dev/null @@ -1,172 +0,0 @@ -{ - "metadata": { - "tags": [] - }, - "sys": { - "space": { - "sys": { - "type": "Link", - "linkType": "Space", - "id": "unknown" - } - }, - "id": "7J3DNGSAIWlcxrOfC25KlX", - "type": "Entry", - "createdAt": "2023-01-25T16:22:13.627Z", - "updatedAt": "2023-06-28T07:05:22.644Z", - "environment": { - "sys": { - "id": "master", - "type": "Link", - "linkType": "Environment" - } - }, - "publishedVersion": 18, - "publishedAt": "2023-06-28T07:04:23.492Z", - "firstPublishedAt": "2023-01-25T16:22:23.271Z", - "createdBy": { - "sys": { - "type": "Link", - "linkType": "User", - "id": "unknown" - } - }, - "updatedBy": { - "sys": { - "type": "Link", - "linkType": "User", - "id": "unknown" - } - }, - "publishedCounter": 2, - "version": 20, - "publishedBy": { - "sys": { - "type": "Link", - "linkType": "User", - "id": "unknown" - } - }, - "automationTags": [], - "contentType": { - "sys": { - "type": "Link", - "linkType": "ContentType", - "id": "topicProductFeature" - } - }, - "revision": 1 - }, - "fields": { - "internalName": "Travel insurance - Premium feature -2", - "name": "Travel insurance", - "shortDescription": { - "nodeType": "document", - "data": {}, - "content": [ - { - "nodeType": "paragraph", - "data": {}, - "content": [ - { - "nodeType": "text", - "value": "Medical, flight, and luggage delay insurance coverage tst", - "marks": [], - "data": {} - } - ] - } - ] - }, - "longDescription": { - "data": {}, - "content": [ - { - "data": {}, - "content": [ - { - "data": {}, - "marks": [ - { - "type": "bold" - } - ], - "value": "Medical emergency coverage", - "nodeType": "text" - } - ], - "nodeType": "heading-5" - }, - { - "data": {}, - "content": [ - { - "data": {}, - "marks": [], - "value": "If you are injured or become sick during a trip requiring a medical service, you can be covered for eligible medical services and transportation home.", - "nodeType": "text" - } - ], - "nodeType": "paragraph" - }, - { - "data": {}, - "content": [ - { - "data": {}, - "marks": [ - { - "type": "bold" - } - ], - "value": "Lost luggage reimbursement", - "nodeType": "text" - } - ], - "nodeType": "heading-5" - }, - { - "data": {}, - "content": [ - { - "data": {}, - "marks": [], - "value": "Lost or damaged carry-on bags and checked-in luggage are covered up to €2,100 per passenger.", - "nodeType": "text" - } - ], - "nodeType": "paragraph" - }, - { - "data": {}, - "content": [ - { - "data": {}, - "marks": [ - { - "type": "bold" - } - ], - "value": "Baggage delay insurance", - "nodeType": "text" - } - ], - "nodeType": "heading-5" - }, - { - "data": {}, - "content": [ - { - "data": {}, - "marks": [], - "value": "When the baggage is delayed over 5 hours, your essential purchases like toiletries and clothing are reimbursed. The maximum reimbursement amount is up to €80 per passenger per day, for up to 3 days.", - "nodeType": "text" - } - ], - "nodeType": "paragraph" - } - ], - "nodeType": "document" - } - } -} diff --git a/packages/visual-sdk/src/index.ts b/packages/visual-sdk/src/index.ts deleted file mode 100644 index 4c0be3bf..00000000 --- a/packages/visual-sdk/src/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './store/index'; diff --git a/packages/visual-sdk/src/store/EditorEntityStore.spec.ts b/packages/visual-sdk/src/store/EditorEntityStore.spec.ts deleted file mode 100644 index 1203577c..00000000 --- a/packages/visual-sdk/src/store/EditorEntityStore.spec.ts +++ /dev/null @@ -1,191 +0,0 @@ -import type { Asset, Entry } from 'contentful'; - -import assetJSON from '../__test__/fixtures/asset.json'; -import entryJSON from '../__test__/fixtures/entry.json'; -import { EditorEntityStore, PostMessageMethods } from './EditorEntityStore'; - -describe('EditorEntityStore', () => { - const locale = 'en-US'; - - const asset = assetJSON as Asset; - const entry = entryJSON as Entry; - const entities = [entry, asset]; - - const editorAsset = { - ...asset, - sys: { - ...asset.sys, - id: 'editor-asset-1', - }, - }; - - const editorEntry = { - ...entry, - sys: { - ...entry.sys, - id: 'editor-entry-1', - }, - }; - - const sendMessage = jest.fn(); - const subscribe = jest.fn().mockReturnValue(() => { - /* unsubscribe */ - }); - - function resolveEntries( - id: string, - entityType: 'Asset' | 'Entry', - entities: Array - ) { - // First it calls sendMessage to request the entity - expect(sendMessage).toHaveBeenCalledTimes(1); - expect(sendMessage).toHaveBeenCalledWith(PostMessageMethods.REQUEST_ENTITIES, { - entityIds: [id], - entityType: entityType, - locale: locale, - }); - - // Then it registers the listener to wait for a respsone - expect(subscribe).toHaveBeenCalledTimes(1); - expect(subscribe).toHaveBeenCalledWith( - PostMessageMethods.REQUESTED_ENTITIES, - expect.any(Function) - ); - - // Let it resolve - subscribe.mock.lastCall[1]({ entities }); - } - - beforeEach(() => { - jest.useFakeTimers(); - }); - - afterEach(() => { - jest.runOnlyPendingTimers(); - jest.useRealTimers(); - jest.clearAllMocks(); - }); - - const createStore = () => new EditorEntityStore({ entities, locale, sendMessage, subscribe }); - - it('should create a new instance', () => { - const store = new EditorEntityStore({ entities: [], locale, sendMessage, subscribe }); - expect(store).toBeDefined(); - }); - - it('should create a new instance with initial state', () => { - expect(createStore().entities).toEqual(entities); - }); - - describe('fetchAsset', () => { - it('should return the asset from the store', async () => { - const result = await createStore().fetchAsset(asset.sys.id); - expect(result).toEqual(asset); - }); - - it('should request the asset from the editor and persist it to the store', async () => { - const store = createStore(); - const promise = store.fetchAsset(editorAsset.sys.id); - - resolveEntries(editorAsset.sys.id, 'Asset', [editorAsset]); - - const result = await promise; - - expect(result).toEqual(editorAsset); - expect(store.entities).toHaveLength(entities.length + 1); - - // fetch again to see if it's now used from the store - const resultFromStore = await store.fetchAsset(editorAsset.sys.id); - expect(resultFromStore).toEqual(result); - expect(sendMessage).toHaveBeenCalledTimes(1); - }); - - it('should return undefined if the requested asset does not exist', async () => { - const promise = createStore().fetchAsset('unknown'); - resolveEntries('unknown', 'Asset', []); - jest.advanceTimersByTime(3000); - const result = await promise; - - expect(result).toBeUndefined(); - }); - }); - - describe('fetchAssets', () => { - it('should return the assets from the store', async () => { - const result = await createStore().fetchAssets([asset.sys.id]); - expect(result).toEqual([asset]); - }); - - it('should return the assets from the store and request missing ones', async () => { - const promise = createStore().fetchAssets([asset.sys.id, editorAsset.sys.id]); - resolveEntries(editorAsset.sys.id, 'Asset', [editorAsset]); - const result = await promise; - expect(result).toEqual([asset, editorAsset]); - }); - - it('should throw an error if one of the requested assets does not exist', async () => { - expect.assertions(1); - - const promise = createStore().fetchAssets(['unknown', asset.sys.id]); - jest.advanceTimersByTime(3000); - - await expect(promise).rejects.toThrowError(); - }); - }); - - describe('fetchEntry', () => { - it('should return the entry from the store', async () => { - const result = await createStore().fetchEntry(entry.sys.id); - expect(result).toEqual(entry); - }); - - it('should request the entry from the editor and persist it to the store', async () => { - const store = createStore(); - const promise = store.fetchEntry(editorEntry.sys.id); - - resolveEntries(editorEntry.sys.id, 'Entry', [editorEntry]); - - const result = await promise; - - expect(result).toEqual(editorEntry); - expect(store.entities).toHaveLength(entities.length + 1); - - // fetch again to see if it's now used from the store - const resultFromStore = await store.fetchAsset(editorEntry.sys.id); - expect(resultFromStore).toEqual(result); - expect(sendMessage).toHaveBeenCalledTimes(1); - }); - - it('should return undefined if the requested entry does not exist', async () => { - const promise = createStore().fetchEntry('unknown'); - resolveEntries('unknown', 'Entry', []); - jest.advanceTimersByTime(3000); - const result = await promise; - - expect(result).toBeUndefined(); - }); - }); - - describe('fetchEntries', () => { - it('should return the entries from the store', async () => { - const result = await createStore().fetchEntries([entry.sys.id]); - expect(result).toEqual([entry]); - }); - - it('should return the assets from the store and request missing ones', async () => { - const promise = createStore().fetchEntries([entry.sys.id, editorEntry.sys.id]); - resolveEntries(editorEntry.sys.id, 'Entry', [editorEntry]); - const result = await promise; - expect(result).toEqual([entry, editorEntry]); - }); - - it('should throw an error if one of the requested entries does not exist', async () => { - expect.assertions(1); - - const promise = createStore().fetchAssets(['unknown', entry.sys.id]); - jest.advanceTimersByTime(3000); - - await expect(promise).rejects.toThrowError(); - }); - }); -}); diff --git a/packages/visual-sdk/src/store/EditorEntityStore.ts b/packages/visual-sdk/src/store/EditorEntityStore.ts deleted file mode 100644 index 97c651a9..00000000 --- a/packages/visual-sdk/src/store/EditorEntityStore.ts +++ /dev/null @@ -1,170 +0,0 @@ -import type { Asset, Entry } from 'contentful'; - -import { EntityStore } from './EntityStore'; - -export type RequestEntitiesMessage = { - entityIds: string[]; - entityType: 'Asset' | 'Entry'; - locale: string; -}; - -export type RequestedEntitiesMessage = { - entities: Array; - missingEntityIds?: string[] -}; - -export enum PostMessageMethods { - REQUEST_ENTITIES = 'REQUEST_ENTITIES', - REQUESTED_ENTITIES = 'REQUESTED_ENTITIES', -} - -type SendMessage = ( - method: PostMessageMethods.REQUEST_ENTITIES, - params: RequestEntitiesMessage -) => void; -type Subscribe = ( - method: PostMessageMethods.REQUESTED_ENTITIES, - cb: (message: RequestedEntitiesMessage) => void -) => VoidFunction; - -/** - * EntityStore which resolves entries and assets from the editor - * over the sendMessage and subscribe functions. - */ -export class EditorEntityStore extends EntityStore { - private requestCache = new Map>(); - private sendMessage: SendMessage; - private subscribe: Subscribe; - private timeoutDuration: number; - - constructor({ - entities, - locale, - sendMessage, - subscribe, - timeoutDuration = 3000, - }: { - entities: Array; - locale: string; - sendMessage: SendMessage; - subscribe: Subscribe; - timeoutDuration?: number; - }) { - super({ entities, locale }); - this.sendMessage = sendMessage; - this.subscribe = subscribe; - this.timeoutDuration = timeoutDuration; - } - - private cleanupPromise(referenceId: string) { - setTimeout(() => { - this.requestCache.delete(referenceId); - }, 300); - } - - private cacheIdSeperator = ','; - - private getCacheId(id: string[]): string { - return id.length === 1 ? id[0] : id.join(this.cacheIdSeperator); - } - - private async fetchEntity(type: 'Asset', ids: string[]): Promise>; - private async fetchEntity(type: 'Entry', ids: string[]): Promise>; - private async fetchEntity( - type: 'Asset' | 'Entry', - ids: string[] - ): Promise | Array> { - const { missing, resolved } = this.getEntitiesFromMap(type, ids); - - if (missing.length === 0) { - // everything is already in cache - return resolved as Array | Array; - } - - const cacheId = this.getCacheId(missing); - const openRequest = this.requestCache.get(cacheId); - - if (openRequest) { - return openRequest; - } - - const newPromise = new Promise((resolve, reject) => { - const unsubscribe = this.subscribe( - PostMessageMethods.REQUESTED_ENTITIES, - (message: RequestedEntitiesMessage) => { - const messageIds = [ - ...message.entities.map((entity) => entity.sys.id), - ...(message.missingEntityIds ?? []), - ]; - if (missing.every((id) => messageIds.find((entityId) => entityId === id))) { - clearTimeout(timeout); - resolve(message.entities); - - this.cleanupPromise(cacheId); - ids.forEach((id) => this.cleanupPromise(id)); - - unsubscribe(); - } else { - console.warn('Unexpected entities received in REQUESTED_ENTITIES. Ignoring this response.') - } - } - ); - - const timeout = setTimeout(() => { - reject(new Error(`Request for entities timed out ${this.timeoutDuration}ms} for ${cacheId}`)); - - this.cleanupPromise(cacheId); - ids.forEach((id) => this.cleanupPromise(id)); - - unsubscribe(); - }, this.timeoutDuration); - - this.sendMessage(PostMessageMethods.REQUEST_ENTITIES, { - entityIds: missing, - entityType: type, - locale: this.locale, - }); - }); - - this.requestCache.set(cacheId, newPromise); - ids.forEach((cid) => { - this.requestCache.set(cid, newPromise); - }); - - const result = (await newPromise) as Array | Array; - - result.forEach((value) => { - this.addEntity(value); - }); - - return this.getEntitiesFromMap(type, ids).resolved as Array | Array; - } - - public async fetchAsset(id: string): Promise { - try { - return (await this.fetchAssets([id]))[0]; - } catch (err) { - // TODO: move to debug utils once it is extracted - console.warn(`Failed to request asset ${id}`); - return undefined; - } - } - - public fetchAssets(ids: string[]): Promise { - return this.fetchEntity('Asset', ids); - } - - public async fetchEntry(id: string): Promise { - try { - return (await this.fetchEntries([id]))[0]; - } catch (err) { - // TODO: move to debug utils once it is extracted - console.warn(`Failed to request entry ${id}`, err); - return undefined; - } - } - - public fetchEntries(ids: string[]): Promise { - return this.fetchEntity('Entry', ids); - } -} diff --git a/packages/visual-sdk/src/store/EditorStore.spec.ts b/packages/visual-sdk/src/store/EditorStore.spec.ts deleted file mode 100644 index 27789b1d..00000000 --- a/packages/visual-sdk/src/store/EditorStore.spec.ts +++ /dev/null @@ -1,131 +0,0 @@ -import type { Asset, Entry } from 'contentful'; - -import assetJSON from '../__test__/fixtures/asset.json'; -import entryJSON from '../__test__/fixtures/entry.json'; -import { EntityStore } from './EntityStore'; - -describe('EntityStore', () => { - const locale = 'en-US'; - - const asset = assetJSON as Asset; - const entry = entryJSON as Entry; - const entities = [entry, asset]; - - const createStore = () => new EntityStore({ entities, locale }); - - it('should create a new instance', () => { - const store = new EntityStore({ entities: [], locale }); - expect(store).toBeDefined(); - }); - - it('should create a new instance with initial state', () => { - expect(createStore().entities).toEqual(entities); - }); - - it('prevents id collision for assets and entries', async () => { - const patchedAsset = { ...asset, sys: { ...asset.sys, id: '1' } }; - const patchedEntry = { ...entry, sys: { ...entry.sys, id: '1' } }; - - const store = new EntityStore({ entities: [patchedAsset, patchedEntry], locale }); - - const [resolvedAsset, resolvedEntry] = await Promise.all([ - store.fetchAsset('1'), - store.fetchEntry('1'), - ]); - - expect(resolvedAsset).toEqual(patchedAsset); - expect(resolvedEntry).toEqual(patchedEntry); - }); - - describe('getValue', () => { - it('should return the value based on entityId and path', () => { - const store = createStore(); - - expect( - store.getValue({ sys: { id: entry.sys.id, linkType: 'Entry', type: 'Link' } }, [ - 'fields', - 'title', - ]) - ).toEqual(entry.fields.title); - expect( - store.getValue({ sys: { id: asset.sys.id, linkType: 'Asset', type: 'Link' } }, [ - 'fields', - 'title', - ]) - ).toEqual(asset.fields.title); - }); - - it('should return undefined if entity id does not exist', () => { - expect( - createStore().getValue({ sys: { id: 'test', linkType: 'Entry', type: 'Link' } }, [ - 'fields', - 'title', - ]) - ).toBeUndefined(); - }); - - it("should return undefined if field doesn't exist", () => { - expect( - createStore().getValue({ sys: { id: entry.sys.id, linkType: 'Entry', type: 'Link' } }, [ - 'fields', - 'description', - ]) - ).toBeUndefined(); - }); - - it('should return undefined if entity type does not match', () => { - expect( - createStore().getValue({ sys: { id: entry.sys.id, linkType: 'Asset', type: 'Link' } }, [ - 'fields', - 'title', - ]) - ).toBeUndefined(); - }); - }); - - describe('fetchAsset', () => { - it('should return the asset from the store', async () => { - const result = await createStore().fetchAsset(asset.sys.id); - expect(result).toEqual(asset); - }); - - it('should return undefined if the requested asset is not in the store', async () => { - const result = await createStore().fetchAsset('unknown'); - expect(result).toBeUndefined(); - }); - }); - - describe('fetchAssets', () => { - it('should return the assets from the store', async () => { - const result = await createStore().fetchAssets([asset.sys.id]); - expect(result).toEqual([asset]); - }); - - it('should throw an error if one of the requested assets is not in the store', async () => { - await expect(createStore().fetchAssets(['unknown', asset.sys.id])).rejects.toThrowError(); - }); - }); - - describe('fetchEntry', () => { - it('should return the entry from the store', async () => { - const result = await createStore().fetchEntry(entry.sys.id); - expect(result).toEqual(entry); - }); - - it('should return undefined if the requested entry is not in the store', async () => { - const result = await createStore().fetchEntry('unknown'); - expect(result).toBeUndefined(); - }); - }); - - describe('fetchEntries', () => { - it('should return the entry from the store', async () => { - const result = await createStore().fetchEntries([entry.sys.id]); - expect(result).toEqual([entry]); - }); - - it('should throw an error if one of the requested entries is not in the store', async () => { - await expect(createStore().fetchAssets(['unknown', entry.sys.id])).rejects.toThrowError(); - }); - }); -}); diff --git a/packages/visual-sdk/src/store/EntityStore.ts b/packages/visual-sdk/src/store/EntityStore.ts deleted file mode 100644 index 04fa8d56..00000000 --- a/packages/visual-sdk/src/store/EntityStore.ts +++ /dev/null @@ -1,120 +0,0 @@ -import type { Asset, Entry, UnresolvedLink } from 'contentful'; - -import { get } from './utils'; - -/** - * Base Store for entities - * Can be extened for the different loading behaviours (editor, production, ..) - */ -export class EntityStore { - protected locale: string; - protected entryMap = new Map(); - protected assetMap = new Map(); - - constructor({ entities, locale }: { entities: Array; locale: string }) { - this.locale = locale; - - for (const entity of entities) { - this.addEntity(entity); - } - } - - public get entities() { - return [...this.entryMap.values(), ...this.assetMap.values()]; - } - - public updateEntity(entity: Entry | Asset) { - this.addEntity(entity); - } - - public getValue( - entityLink: UnresolvedLink<'Entry' | 'Asset'>, - path: string[] - ): string | undefined { - const entity = this.getEntity(entityLink.sys.linkType, entityLink.sys.id); - - if (!entity) { - // TODO: move to `debug` utils once it is extracted - console.warn(`Unresolved entity reference: ${entityLink.sys.linkType} with ID ${entityLink.sys.id}`); - return; - } - - return get(entity, path); - } - - protected getEntitiesFromMap(type: 'Entry' | 'Asset', ids: string[]) { - const resolved = []; - const missing = []; - - for (const id of ids) { - const entity = this.getEntity(type, id); - if (entity) { - resolved.push(entity); - } else { - missing.push(id); - } - } - - return { - resolved, - missing, - }; - } - - protected addEntity(entity: Entry | Asset): void { - if (this.isAsset(entity)) { - this.assetMap.set(entity.sys.id, entity); - } else { - this.entryMap.set(entity.sys.id, entity); - } - } - - public async fetchAsset(id: string): Promise { - const { resolved, missing } = this.getEntitiesFromMap('Asset', [id]); - if (missing.length) { - // TODO: move to `debug` utils once it is extracted - console.warn(`Asset "${id}" is not in the store`); - return undefined; - } - - return resolved[0] as Asset; - } - public async fetchAssets(ids: string[]): Promise { - const { resolved, missing } = this.getEntitiesFromMap('Asset', ids); - if (missing.length) { - throw new Error(`Missing assets in the store (${missing.join(',')})`); - } - return resolved as Asset[]; - } - - public async fetchEntry(id: string): Promise { - const { resolved, missing } = this.getEntitiesFromMap('Entry', [id]); - if (missing.length) { - // TODO: move to `debug` utils once it is extracted - console.warn(`Entry "${id}" is not in the store`); - return undefined; - } - - return resolved[0] as Entry; - } - - public async fetchEntries(ids: string[]): Promise { - const { resolved, missing } = this.getEntitiesFromMap('Entry', ids); - if (missing.length) { - throw new Error(`Missing assets in the store (${missing.join(',')})`); - } - return resolved as Entry[]; - } - - private isAsset(entity: Entry | Asset): entity is Asset { - return entity.sys.type === 'Asset'; - } - - private getEntity(type: 'Asset' | 'Entry', id: string) { - if (type === 'Asset') { - return this.assetMap.get(id); - } - - return this.entryMap.get(id); - } -} diff --git a/packages/visual-sdk/src/store/index.ts b/packages/visual-sdk/src/store/index.ts deleted file mode 100644 index bd356ac1..00000000 --- a/packages/visual-sdk/src/store/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export { EntityStore } from './EntityStore'; -export { EditorEntityStore, PostMessageMethods } from './EditorEntityStore'; -export type { RequestEntitiesMessage, RequestedEntitiesMessage } from './EditorEntityStore'; diff --git a/packages/visual-sdk/src/store/utils.ts b/packages/visual-sdk/src/store/utils.ts deleted file mode 100644 index e7d54f5c..00000000 --- a/packages/visual-sdk/src/store/utils.ts +++ /dev/null @@ -1,12 +0,0 @@ -export function get(obj: Record, path: string[]): T | undefined { - if (!path.length) { - return obj as T; - } - - try { - const [currentPath, ...nextPath] = path; - return get(obj[currentPath], nextPath); - } catch (err) { - return undefined; - } -} diff --git a/packages/visual-sdk/tsconfig.json b/packages/visual-sdk/tsconfig.json deleted file mode 100644 index 564a5990..00000000 --- a/packages/visual-sdk/tsconfig.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "extends": "../../tsconfig.base.json", - "include": ["src"] -}