diff --git a/lib/api.ts b/lib/api.ts index 49ccca7572..e8cae1c9d5 100644 --- a/lib/api.ts +++ b/lib/api.ts @@ -18,6 +18,7 @@ import { } from './types' import { Channel } from './channel' import { createAdapter } from './cmaAdapter' +import { createCMAClient } from './cma' import { KeyValueMap } from 'contentful-management/types' const DEFAULT_API_PRODUCERS = [ @@ -64,9 +65,10 @@ function makeSharedAPI( const currentLocation = data.location || locations.LOCATION_ENTRY_FIELD return { + cma: createCMAClient(ids, channel), cmaAdapter: createAdapter(channel), location: { - is: (tested) => currentLocation === tested, + is: (tested: string) => currentLocation === tested, }, user, parameters, diff --git a/lib/cma.ts b/lib/cma.ts new file mode 100644 index 0000000000..37b8ec32d9 --- /dev/null +++ b/lib/cma.ts @@ -0,0 +1,19 @@ +import { createClient } from 'contentful-management' +import { createAdapter } from './cmaAdapter' +import { CMAClient } from './types/cmaClient.types' +import type { IdsAPI } from './types/api.types' +import type { Channel } from './channel' + +export function createCMAClient(ids: IdsAPI, channel: Channel): CMAClient { + return createClient( + { apiAdapter: createAdapter(channel) }, + { + type: 'plain', + defaults: { + environmentId: ids.environmentAlias ?? ids.environment, + spaceId: ids.space, + organizationId: ids.organization, + }, + } + ) +} diff --git a/lib/types/api.types.ts b/lib/types/api.types.ts index e8b066f168..674fe0edb5 100644 --- a/lib/types/api.types.ts +++ b/lib/types/api.types.ts @@ -20,6 +20,7 @@ import { AppConfigAPI } from './app.types' import { NavigatorAPI } from './navigator.types' import { EntryFieldInfo, FieldInfo } from './field.types' import { Adapter, KeyValueMap } from 'contentful-management/types' +import { CMAClient } from './cmaClient.types' /* User API */ @@ -244,6 +245,8 @@ export interface BaseAppSDK< ids: Omit /** Adapter to be injected in contentful-management client */ cmaAdapter: Adapter + /** A CMA Client initialized with default params */ + cma: CMAClient } export type EditorAppSDK< diff --git a/package-lock.json b/package-lock.json index f93e038e3d..dea398e6d4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,14 +1,19 @@ { "name": "contentful-ui-extensions-sdk", - "version": "4.10.0-alpha.5", + "version": "4.21.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "contentful-ui-extensions-sdk", - "version": "4.10.0-alpha.5", + "version": "4.21.1", "license": "MIT", + "dependencies": { + "contentful-management": ">=7.30.0" + }, "devDependencies": { + "@rollup/plugin-commonjs": "^25.0.2", + "@rollup/plugin-node-resolve": "^15.1.0", "@semantic-release/changelog": "6.0.3", "@semantic-release/exec": "6.0.3", "@semantic-release/git": "10.0.1", @@ -26,7 +31,6 @@ "babel-eslint": "10.1.0", "chai": "4.3.7", "chai-as-promised": "7.1.1", - "contentful-management": "10.38.2", "cross-spawn": "7.0.3", "eslint": "8.43.0", "eslint-config-prettier": "8.8.0", @@ -57,9 +61,6 @@ "ts-mocha": "10.0.0", "tslib": "2.5.3", "typescript": "5.1.3" - }, - "peerDependencies": { - "contentful-management": ">=7.30.0" } }, "node_modules/@babel/code-frame": { @@ -201,7 +202,6 @@ }, "node_modules/@contentful/rich-text-types": { "version": "16.0.3", - "dev": true, "license": "MIT", "engines": { "node": ">=6.0.0" @@ -630,6 +630,140 @@ "node": ">=12" } }, + "node_modules/@rollup/plugin-commonjs": { + "version": "25.0.2", + "resolved": "https://registry.npmjs.org/@rollup/plugin-commonjs/-/plugin-commonjs-25.0.2.tgz", + "integrity": "sha512-NGTwaJxIO0klMs+WSFFtBP7b9TdTJ3K76HZkewT8/+yHzMiUGVQgaPtLQxNVYIgT5F7lxkEyVID+yS3K7bhCow==", + "dev": true, + "dependencies": { + "@rollup/pluginutils": "^5.0.1", + "commondir": "^1.0.1", + "estree-walker": "^2.0.2", + "glob": "^8.0.3", + "is-reference": "1.2.1", + "magic-string": "^0.27.0" + }, + "engines": { + "node": ">=14.0.0" + }, + "peerDependencies": { + "rollup": "^2.68.0||^3.0.0" + }, + "peerDependenciesMeta": { + "rollup": { + "optional": true + } + } + }, + "node_modules/@rollup/plugin-commonjs/node_modules/@rollup/pluginutils": { + "version": "5.0.2", + "resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-5.0.2.tgz", + "integrity": "sha512-pTd9rIsP92h+B6wWwFbW8RkZv4hiR/xKsqre4SIuAOaOEQRxi0lqLke9k2/7WegC85GgUs9pjmOjCUi3In4vwA==", + "dev": true, + "dependencies": { + "@types/estree": "^1.0.0", + "estree-walker": "^2.0.2", + "picomatch": "^2.3.1" + }, + "engines": { + "node": ">=14.0.0" + }, + "peerDependencies": { + "rollup": "^1.20.0||^2.0.0||^3.0.0" + }, + "peerDependenciesMeta": { + "rollup": { + "optional": true + } + } + }, + "node_modules/@rollup/plugin-commonjs/node_modules/brace-expansion": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz", + "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==", + "dev": true, + "dependencies": { + "balanced-match": "^1.0.0" + } + }, + "node_modules/@rollup/plugin-commonjs/node_modules/glob": { + "version": "8.1.0", + "resolved": "https://registry.npmjs.org/glob/-/glob-8.1.0.tgz", + "integrity": "sha512-r8hpEjiQEYlF2QU0df3dS+nxxSIreXQS1qRhMJM0Q5NDdR386C7jb7Hwwod8Fgiuex+k0GFjgft18yvxm5XoCQ==", + "dev": true, + "dependencies": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^5.0.1", + "once": "^1.3.0" + }, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/@rollup/plugin-commonjs/node_modules/minimatch": { + "version": "5.1.6", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.1.6.tgz", + "integrity": "sha512-lKwV/1brpG6mBUFHtb7NUmtABCb2WZZmm2wNiOA5hAb8VdCS4B3dtMWyvcoViccwAW/COERjXLt0zP1zXUN26g==", + "dev": true, + "dependencies": { + "brace-expansion": "^2.0.1" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/@rollup/plugin-node-resolve": { + "version": "15.1.0", + "resolved": "https://registry.npmjs.org/@rollup/plugin-node-resolve/-/plugin-node-resolve-15.1.0.tgz", + "integrity": "sha512-xeZHCgsiZ9pzYVgAo9580eCGqwh/XCEUM9q6iQfGNocjgkufHAqC3exA+45URvhiYV8sBF9RlBai650eNs7AsA==", + "dev": true, + "dependencies": { + "@rollup/pluginutils": "^5.0.1", + "@types/resolve": "1.20.2", + "deepmerge": "^4.2.2", + "is-builtin-module": "^3.2.1", + "is-module": "^1.0.0", + "resolve": "^1.22.1" + }, + "engines": { + "node": ">=14.0.0" + }, + "peerDependencies": { + "rollup": "^2.78.0||^3.0.0" + }, + "peerDependenciesMeta": { + "rollup": { + "optional": true + } + } + }, + "node_modules/@rollup/plugin-node-resolve/node_modules/@rollup/pluginutils": { + "version": "5.0.2", + "resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-5.0.2.tgz", + "integrity": "sha512-pTd9rIsP92h+B6wWwFbW8RkZv4hiR/xKsqre4SIuAOaOEQRxi0lqLke9k2/7WegC85GgUs9pjmOjCUi3In4vwA==", + "dev": true, + "dependencies": { + "@types/estree": "^1.0.0", + "estree-walker": "^2.0.2", + "picomatch": "^2.3.1" + }, + "engines": { + "node": ">=14.0.0" + }, + "peerDependencies": { + "rollup": "^1.20.0||^2.0.0||^3.0.0" + }, + "peerDependenciesMeta": { + "rollup": { + "optional": true + } + } + }, "node_modules/@rollup/pluginutils": { "version": "4.1.2", "dev": true, @@ -1257,6 +1391,12 @@ "@types/node": "*" } }, + "node_modules/@types/estree": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.1.tgz", + "integrity": "sha512-LG4opVs2ANWZ1TJoKc937iMmNstM/d0ae1vNbnBvBhqCSezgVUOzcLCqbI5elV8Vy6WKwKjaqR+zO9VKirBBCA==", + "dev": true + }, "node_modules/@types/fs-extra": { "version": "11.0.1", "dev": true, @@ -1278,7 +1418,6 @@ }, "node_modules/@types/json-patch": { "version": "0.0.30", - "dev": true, "license": "MIT" }, "node_modules/@types/json-schema": { @@ -1330,6 +1469,12 @@ "integrity": "sha512-Gj7cI7z+98M282Tqmp2K5EIsoouUEzbBJhQQzDE3jSIRk6r9gsz0oUokqIUR4u1R3dMHo0pDHM7sNOHyhulypw==", "dev": true }, + "node_modules/@types/resolve": { + "version": "1.20.2", + "resolved": "https://registry.npmjs.org/@types/resolve/-/resolve-1.20.2.tgz", + "integrity": "sha512-60BCwRFOZCQhDncwQdxxeOEEkbc5dIMccYLwbxsS4TUNeVECQ/pBJ0j09mrHOl/JJvpRPGwO9SvE4nR2Nb/a4Q==", + "dev": true + }, "node_modules/@types/semver": { "version": "7.5.0", "resolved": "https://registry.npmjs.org/@types/semver/-/semver-7.5.0.tgz", @@ -1810,7 +1955,6 @@ }, "node_modules/asynckit": { "version": "0.4.0", - "dev": true, "license": "MIT" }, "node_modules/available-typed-arrays": { @@ -1826,7 +1970,6 @@ }, "node_modules/axios": { "version": "0.27.2", - "dev": true, "license": "MIT", "dependencies": { "follow-redirects": "^1.14.9", @@ -1915,6 +2058,18 @@ "dev": true, "license": "MIT" }, + "node_modules/builtin-modules": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/builtin-modules/-/builtin-modules-3.3.0.tgz", + "integrity": "sha512-zhaCDicdLuWN5UbN5IMnFqNMhNfo919sH85y2/ea+5Yg9TsTkeZxpL+JLbp6cgYFS4sRLp3YV4S6yDuqVWHYOw==", + "dev": true, + "engines": { + "node": ">=6" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/builtins": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/builtins/-/builtins-5.0.1.tgz", @@ -1927,7 +2082,6 @@ }, "node_modules/call-bind": { "version": "1.0.2", - "dev": true, "license": "MIT", "dependencies": { "function-bind": "^1.1.1", @@ -2210,7 +2364,6 @@ }, "node_modules/combined-stream": { "version": "1.0.8", - "dev": true, "license": "MIT", "dependencies": { "delayed-stream": "~1.0.0" @@ -2261,7 +2414,6 @@ "version": "10.38.2", "resolved": "https://registry.npmjs.org/contentful-management/-/contentful-management-10.38.2.tgz", "integrity": "sha512-DstacThPYcLdnUAzBHKehqWb/lKU9/skDtVpwtMaopx0Z8fGIEiV+Xh/Wcg+hglHROnuPi+mBDaf3Po18AeP/w==", - "dev": true, "dependencies": { "@contentful/rich-text-types": "^16.0.3", "@types/json-patch": "0.0.30", @@ -2277,12 +2429,10 @@ }, "node_modules/contentful-management/node_modules/fast-copy": { "version": "3.0.0", - "dev": true, "license": "MIT" }, "node_modules/contentful-management/node_modules/type-fest": { "version": "3.5.4", - "dev": true, "license": "(MIT OR CC0-1.0)", "engines": { "node": ">=14.16" @@ -2293,7 +2443,6 @@ }, "node_modules/contentful-sdk-core": { "version": "7.0.1", - "dev": true, "license": "MIT", "dependencies": { "fast-copy": "^2.1.0", @@ -2625,6 +2774,15 @@ "dev": true, "license": "MIT" }, + "node_modules/deepmerge": { + "version": "4.3.1", + "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.3.1.tgz", + "integrity": "sha512-3sUqbMEc77XqpdNO7FRyRog+eW3ph+GYCbj+rK+uYyRMuwsVy0rMiVtPn+QJlKFvWP/1PYpapqYn0Me2knFn+A==", + "dev": true, + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/define-properties": { "version": "1.2.0", "dev": true, @@ -2642,7 +2800,6 @@ }, "node_modules/delayed-stream": { "version": "1.0.0", - "dev": true, "license": "MIT", "engines": { "node": ">=0.4.0" @@ -3734,7 +3891,6 @@ }, "node_modules/fast-copy": { "version": "2.1.1", - "dev": true, "license": "MIT" }, "node_modules/fast-deep-equal": { @@ -3990,7 +4146,6 @@ }, "node_modules/follow-redirects": { "version": "1.14.9", - "dev": true, "funding": [ { "type": "individual", @@ -4017,7 +4172,6 @@ }, "node_modules/form-data": { "version": "4.0.0", - "dev": true, "license": "MIT", "dependencies": { "asynckit": "^0.4.0", @@ -4075,7 +4229,6 @@ }, "node_modules/function-bind": { "version": "1.1.1", - "dev": true, "license": "MIT" }, "node_modules/function.prototype.name": { @@ -4121,7 +4274,6 @@ }, "node_modules/get-intrinsic": { "version": "1.2.1", - "dev": true, "license": "MIT", "dependencies": { "function-bind": "^1.1.1", @@ -4307,7 +4459,6 @@ }, "node_modules/has": { "version": "1.0.3", - "dev": true, "license": "MIT", "dependencies": { "function-bind": "^1.1.1" @@ -4345,7 +4496,6 @@ }, "node_modules/has-proto": { "version": "1.0.1", - "dev": true, "license": "MIT", "engines": { "node": ">= 0.4" @@ -4356,7 +4506,6 @@ }, "node_modules/has-symbols": { "version": "1.0.3", - "dev": true, "license": "MIT", "engines": { "node": ">= 0.4" @@ -4665,6 +4814,21 @@ "dev": true, "license": "MIT" }, + "node_modules/is-builtin-module": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/is-builtin-module/-/is-builtin-module-3.2.1.tgz", + "integrity": "sha512-BSLE3HnV2syZ0FK0iMA/yUGplUeMmNz4AW5fnTunbCIqZi4vG3WjJT9FHMy5D69xmAYBHXQhJdALdpwVxV501A==", + "dev": true, + "dependencies": { + "builtin-modules": "^3.3.0" + }, + "engines": { + "node": ">=6" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/is-callable": { "version": "1.2.7", "dev": true, @@ -4738,6 +4902,12 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/is-module": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/is-module/-/is-module-1.0.0.tgz", + "integrity": "sha512-51ypPSPCoTEIN9dy5Oy+h4pShgJmPCygKfyRCISBI+JoWT/2oJvK8QPxmwv7b/p239jXrm9M1mlQbyKJ5A152g==", + "dev": true + }, "node_modules/is-negative-zero": { "version": "2.0.2", "dev": true, @@ -4810,6 +4980,15 @@ "dev": true, "license": "MIT" }, + "node_modules/is-reference": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/is-reference/-/is-reference-1.2.1.tgz", + "integrity": "sha512-U82MsXXiFIrjCK4otLT+o2NA2Cd2g5MLoOVXUZjIOhLurrRxpEXzI8O0KZHr3IjLvlAH1kTPYSuqer5T9ZVBKQ==", + "dev": true, + "dependencies": { + "@types/estree": "*" + } + }, "node_modules/is-regex": { "version": "1.1.4", "dev": true, @@ -5429,12 +5608,10 @@ }, "node_modules/lodash.isplainobject": { "version": "4.0.6", - "dev": true, "license": "MIT" }, "node_modules/lodash.isstring": { "version": "4.0.1", - "dev": true, "license": "MIT" }, "node_modules/lodash.merge": { @@ -5641,6 +5818,18 @@ "lz-string": "bin/bin.js" } }, + "node_modules/magic-string": { + "version": "0.27.0", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.27.0.tgz", + "integrity": "sha512-8UnnX2PeRAPZuN12svgR9j7M1uWMovg/CEnIwIG0LFkXSJJe4PdfUGiTGl8V9bsBHFUtfVINcSyYxd7q+kx9fA==", + "dev": true, + "dependencies": { + "@jridgewell/sourcemap-codec": "^1.4.13" + }, + "engines": { + "node": ">=12" + } + }, "node_modules/make-dir": { "version": "3.1.0", "dev": true, @@ -5999,7 +6188,6 @@ }, "node_modules/mime-db": { "version": "1.51.0", - "dev": true, "license": "MIT", "engines": { "node": ">= 0.6" @@ -6007,7 +6195,6 @@ }, "node_modules/mime-types": { "version": "2.1.34", - "dev": true, "license": "MIT", "dependencies": { "mime-db": "1.51.0" @@ -10043,7 +10230,6 @@ }, "node_modules/object-inspect": { "version": "1.12.3", - "dev": true, "license": "MIT", "funding": { "url": "https://github.com/sponsors/ljharb" @@ -10344,7 +10530,6 @@ }, "node_modules/p-throttle": { "version": "4.1.1", - "dev": true, "license": "MIT", "engines": { "node": ">=10" @@ -10585,7 +10770,6 @@ }, "node_modules/qs": { "version": "6.10.3", - "dev": true, "license": "BSD-3-Clause", "dependencies": { "side-channel": "^1.0.4" @@ -11472,7 +11656,6 @@ }, "node_modules/side-channel": { "version": "1.0.4", - "dev": true, "license": "MIT", "dependencies": { "call-bind": "^1.0.0", diff --git a/package.json b/package.json index 10a321a256..9d753215bf 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "contentful-ui-extensions-sdk", "description": "A JavaScript library to develop custom apps for Contentful", - "version": "4.10.0-alpha.5", + "version": "4.21.1", "author": "Contentful GmbH", "license": "MIT", "sideEffects": true, @@ -11,6 +11,8 @@ }, "homepage": "https://www.contentful.com/developers/docs/extensibility/app-framework/sdk/", "main": "dist/cf-extension-api.js", + "unpkg": "dist/cf-extension-api.bundled.js", + "jsdelivr": "dist/cf-extension-api.bundled.js", "types": "dist/index.d.ts", "files": [ "dist/cf-extension-api.js", @@ -33,7 +35,12 @@ "prepare": "husky install", "lint-staged": "lint-staged" }, + "dependencies": { + "contentful-management": ">=7.30.0" + }, "devDependencies": { + "@rollup/plugin-commonjs": "^25.0.2", + "@rollup/plugin-node-resolve": "^15.1.0", "@semantic-release/changelog": "6.0.3", "@semantic-release/exec": "6.0.3", "@semantic-release/git": "10.0.1", @@ -51,7 +58,6 @@ "babel-eslint": "10.1.0", "chai": "4.3.7", "chai-as-promised": "7.1.1", - "contentful-management": "10.38.2", "cross-spawn": "7.0.3", "eslint": "8.43.0", "eslint-config-prettier": "8.8.0", @@ -133,8 +139,5 @@ ], "@semantic-release/github" ] - }, - "peerDependencies": { - "contentful-management": ">=7.30.0" } } diff --git a/rollup.config.js b/rollup.config.js index 029657b079..7f9dfef947 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,9 +1,11 @@ const typescript = require('rollup-plugin-typescript2') const { terser } = require('rollup-plugin-terser') +const { nodeResolve } = require('@rollup/plugin-node-resolve') +const commonjs = require('@rollup/plugin-commonjs') const pkg = require('./package.json') -const makeConfigForOutput = output => ({ +const makeConfigForOutput = (output, plugins = []) => ({ input: './lib/index.ts', output, plugins: [ @@ -11,10 +13,11 @@ const makeConfigForOutput = output => ({ terser({ format: { comments: false, - ecma: 5 - } - }) - ] + ecma: 5, + }, + }), + ...plugins, + ], }) module.exports = [ @@ -22,6 +25,18 @@ module.exports = [ file: pkg.main, format: 'umd', name: 'contentfulExtension', - footer: 'globalThis.contentfulApp = globalThis.contentfulExtension;' - }) + footer: 'globalThis.contentfulApp = globalThis.contentfulExtension;', + globals: { + 'contentful-management': 'contentfulManagement', + }, + }), + makeConfigForOutput( + { + file: pkg.unpkg, + format: 'umd', + name: 'contentfulExtension', + footer: 'globalThis.contentfulApp = globalThis.contentfulExtension;', + }, + [nodeResolve({ browser: true }), commonjs()] + ), ] diff --git a/test/unit/api.spec.ts b/test/unit/api.spec.ts index 9cd2df8cb8..da82377cc5 100644 --- a/test/unit/api.spec.ts +++ b/test/unit/api.spec.ts @@ -16,6 +16,7 @@ const sharedExpected = [ 'ids', 'access', 'cmaAdapter', + 'cma', ] function test(expected: string[], location: string | undefined, expectedLocation = location) { diff --git a/test/unit/cma.spec.ts b/test/unit/cma.spec.ts new file mode 100644 index 0000000000..8b458a394f --- /dev/null +++ b/test/unit/cma.spec.ts @@ -0,0 +1,22 @@ +import { expect } from 'chai' +import { createCMAClient } from '../../lib/cma' +import { IdsAPI } from '../../lib/types' + +describe('createCMAClient()', function () { + it('Creates a CMA client', function () { + const ids: IdsAPI = { + space: 'space', + environment: 'environment', + environmentAlias: 'environmentAlias', + organization: 'organization', + extension: 'extension', + user: 'user', + field: 'field', + entry: 'entry', + contentType: 'contentType', + } + + const client = createCMAClient(ids, { addHandler: () => {} } as any) + expect(client).to.be.an('object') + }) +})