@contentful/react-apps-toolkit 0.3.0 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -20,6 +20,14 @@ yarn add @contentful/react-apps-toolkit
20
20
 
21
21
  The following hooks and utilities are exported from the package: Nothing
22
22
 
23
+ #### SDKProvider
24
+
25
+ Wrapper component, which makes the Apps SDK available to children via React Context. To use any of the hooks contained in this package, an application must be wrapped in the SDK provider, as all hooks depend on the Apps SDK.
26
+
27
+ #### useCMA
28
+
29
+ Returns an initialized client for the Contentful Management API, which can immediately be used to communicate with the rest of your Contentful space. [Contentful Management API docs](https://www.contentful.com/developers/docs/references/content-management-api/).
30
+
23
31
  ### Resources
24
32
 
25
33
  - [Create Contentful App](https://www.contentful.com/developers/docs/extensibility/app-framework/create-contentful-app/)
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './useSDK';
2
2
  export * from './SDKProvider';
3
+ export * from './useCMA';
package/dist/index.js CHANGED
@@ -16,3 +16,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./useSDK"), exports);
18
18
  __exportStar(require("./SDKProvider"), exports);
19
+ __exportStar(require("./useCMA"), exports);
@@ -0,0 +1,6 @@
1
+ import { PlainClientAPI } from 'contentful-management';
2
+ /**
3
+ * React hook returning a CMA plain client instance.
4
+ * Must be used in the `SDKProvider` component. Will throw error, if called outside of `SDKProvider`.
5
+ */
6
+ export declare function useCMA(): PlainClientAPI | undefined;
package/dist/useCMA.js ADDED
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useCMA = void 0;
4
+ var contentful_management_1 = require("contentful-management");
5
+ var react_1 = require("react");
6
+ var useSDK_1 = require("./useSDK");
7
+ /**
8
+ * React hook returning a CMA plain client instance.
9
+ * Must be used in the `SDKProvider` component. Will throw error, if called outside of `SDKProvider`.
10
+ */
11
+ function useCMA() {
12
+ var sdk = (0, useSDK_1.useSDK)();
13
+ var cma = (0, react_1.useMemo)(function () {
14
+ return (0, contentful_management_1.createClient)({ apiAdapter: sdk.cmaAdapter }, {
15
+ type: 'plain',
16
+ defaults: {
17
+ environmentId: sdk.ids.environment,
18
+ spaceId: sdk.ids.space,
19
+ },
20
+ });
21
+ }, [sdk]);
22
+ return cma;
23
+ }
24
+ exports.useCMA = useCMA;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ var react_hooks_1 = require("@testing-library/react-hooks");
15
+ var useCMA_1 = require("./useCMA");
16
+ var mockedCma = 'mocked-cma';
17
+ var mockedSdk = {
18
+ cmaAdapter: 'placholder',
19
+ ids: {
20
+ environment: 'placeholder',
21
+ space: 'placeholder',
22
+ },
23
+ };
24
+ jest.mock('./useSDK', function () { return (__assign(__assign({}, jest.requireActual('./useSDK')), { useSDK: function () { return mockedSdk; } })); });
25
+ jest.mock('./SDKProvider', function () { return ({
26
+ SDKContext: {},
27
+ }); });
28
+ jest.mock('contentful-management', function () {
29
+ return __assign(__assign({}, jest.requireActual('contentful-management')), { createClient: function () { return mockedCma; } });
30
+ });
31
+ describe('useCMA', function () {
32
+ test('should return cma client', function () {
33
+ var result = (0, react_hooks_1.renderHook)(function () { return (0, useCMA_1.useCMA)(); }).result;
34
+ expect(result.current).toBe(mockedCma);
35
+ });
36
+ });
package/dist/useSDK.js CHANGED
@@ -11,7 +11,7 @@ var SDKProvider_1 = require("./SDKProvider");
11
11
  function useSDK() {
12
12
  var sdk = (0, react_1.useContext)(SDKProvider_1.SDKContext).sdk;
13
13
  if (!sdk) {
14
- throw new Error('SDKContext not found. Make sure the useSDK hook is used inside the SDKProvider');
14
+ throw new Error('SDKContext not found. Make sure this hook is used inside the SDKProvider');
15
15
  }
16
16
  return sdk;
17
17
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentful/react-apps-toolkit",
3
- "version": "0.3.0",
3
+ "version": "0.4.2",
4
4
  "description": "Toolkit for building a Contentful app in React",
5
5
  "keywords": [],
6
6
  "author": "Contentful GmbH",
@@ -39,6 +39,7 @@
39
39
  "@types/jest": "^27.4.0",
40
40
  "@types/react": "^17.0.39",
41
41
  "jest": "^27.5.0",
42
+ "react": "^17.0.2",
42
43
  "ts-jest": "^27.1.3",
43
44
  "typescript": "^4.5.5"
44
45
  },
@@ -46,7 +47,8 @@
46
47
  "access": "public"
47
48
  },
48
49
  "dependencies": {
49
- "@contentful/app-sdk": "^4.0.0"
50
+ "@contentful/app-sdk": "^4.0.0",
51
+ "contentful-management": ">=7.30.0"
50
52
  },
51
- "gitHead": "8de11c51bc4d238157b026236e1b5bf62e227058"
53
+ "gitHead": "07fa91924433749cd6946874600837e42b25e2d5"
52
54
  }