@contentful/react-apps-toolkit 0.2.1 → 0.4.1
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 +8 -0
- package/dist/SDKProvider.js +9 -1
- package/dist/SDKProvider.spec.d.ts +1 -0
- package/dist/SDKProvider.spec.js +34 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/useCMA.d.ts +6 -0
- package/dist/useCMA.js +24 -0
- package/dist/useCMA.spec.d.ts +1 -0
- package/dist/useCMA.spec.js +36 -0
- package/dist/useSDK.js +1 -1
- package/package.json +5 -3
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/SDKProvider.js
CHANGED
|
@@ -19,6 +19,7 @@ var jsx_runtime_1 = require("react/jsx-runtime");
|
|
|
19
19
|
var react_1 = __importDefault(require("react"));
|
|
20
20
|
var app_sdk_1 = require("@contentful/app-sdk");
|
|
21
21
|
exports.SDKContext = react_1.default.createContext({ sdk: null });
|
|
22
|
+
var DELAY_TIMEOUT = 4 * 1000;
|
|
22
23
|
/**
|
|
23
24
|
* The Component providing the AppSdk, the useSDK hook can only be used within this Provider
|
|
24
25
|
* @param props.loading an optional loading element that gets rendered while initializing the app
|
|
@@ -26,7 +27,14 @@ exports.SDKContext = react_1.default.createContext({ sdk: null });
|
|
|
26
27
|
var SDKProvider = function (props) {
|
|
27
28
|
var _a = react_1.default.useState(), sdk = _a[0], setSDK = _a[1];
|
|
28
29
|
react_1.default.useEffect(function () {
|
|
29
|
-
|
|
30
|
+
var timeout = window.setTimeout(function () {
|
|
31
|
+
console.warn("Your app is taking longer than expected to initialize. If you think this is an error with Contentful's App SDK, let us know: https://github.com/contentful/ui-extensions-sdk/issues");
|
|
32
|
+
}, DELAY_TIMEOUT);
|
|
33
|
+
(0, app_sdk_1.init)(function (sdk) {
|
|
34
|
+
setSDK(sdk);
|
|
35
|
+
window.clearTimeout(timeout);
|
|
36
|
+
});
|
|
37
|
+
return function () { return window.clearTimeout(timeout); };
|
|
30
38
|
}, []);
|
|
31
39
|
if (!sdk) {
|
|
32
40
|
if (props.loading) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var jsx_runtime_1 = require("react/jsx-runtime");
|
|
4
|
+
var react_1 = require("@testing-library/react");
|
|
5
|
+
var SDKProvider_1 = require("./SDKProvider");
|
|
6
|
+
var app_sdk_1 = require("@contentful/app-sdk");
|
|
7
|
+
jest.useFakeTimers();
|
|
8
|
+
jest.mock('@contentful/app-sdk', function () { return ({
|
|
9
|
+
init: jest.fn(),
|
|
10
|
+
}); });
|
|
11
|
+
describe('SDKProvider', function () {
|
|
12
|
+
var consoleWarnMock;
|
|
13
|
+
beforeEach(function () {
|
|
14
|
+
// @ts-ignore
|
|
15
|
+
app_sdk_1.init.mockImplementation(function (callback) {
|
|
16
|
+
callback({});
|
|
17
|
+
});
|
|
18
|
+
consoleWarnMock = jest.spyOn(console, 'warn').mockImplementation();
|
|
19
|
+
});
|
|
20
|
+
afterEach(function () {
|
|
21
|
+
consoleWarnMock === null || consoleWarnMock === void 0 ? void 0 : consoleWarnMock.mockRestore();
|
|
22
|
+
});
|
|
23
|
+
it('renders its children when sdk the init returns the sdk', function () {
|
|
24
|
+
var getByText = (0, react_1.render)((0, jsx_runtime_1.jsx)(SDKProvider_1.SDKProvider, { children: (0, jsx_runtime_1.jsx)("div", { children: "children" }) })).getByText;
|
|
25
|
+
expect(getByText('children')).toBeTruthy();
|
|
26
|
+
});
|
|
27
|
+
it('calls console warn after timeout if callback is not returning ', function () {
|
|
28
|
+
// @ts-ignore
|
|
29
|
+
app_sdk_1.init.mockImplementation(function () { });
|
|
30
|
+
(0, react_1.render)((0, jsx_runtime_1.jsx)(SDKProvider_1.SDKProvider, { children: (0, jsx_runtime_1.jsx)("div", { children: "children" }) }));
|
|
31
|
+
jest.runAllTimers();
|
|
32
|
+
expect(consoleWarnMock).toHaveBeenCalled();
|
|
33
|
+
});
|
|
34
|
+
});
|
package/dist/index.d.ts
CHANGED
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);
|
package/dist/useCMA.d.ts
ADDED
|
@@ -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
|
|
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
|
+
"version": "0.4.1",
|
|
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": "
|
|
53
|
+
"gitHead": "c46d32ec211a13f7e023d481b79e669f8e7b9001"
|
|
52
54
|
}
|