@contentful/react-apps-toolkit 0.1.2 → 0.2.0
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/dist/SDKProvider.d.ts +14 -0
- package/dist/SDKProvider.js +39 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +16 -5
- package/dist/useSDK.d.ts +7 -0
- package/dist/useSDK.js +18 -0
- package/dist/{index.spec.d.ts → useSDK.spec.d.ts} +0 -0
- package/dist/useSDK.spec.js +35 -0
- package/package.json +5 -2
- package/dist/index.spec.js +0 -8
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import React, { FC, ReactElement } from 'react';
|
|
2
|
+
import { KnownSDK } from '@contentful/app-sdk';
|
|
3
|
+
export declare const SDKContext: React.Context<{
|
|
4
|
+
sdk: KnownSDK | null;
|
|
5
|
+
}>;
|
|
6
|
+
interface SDKProviderProps {
|
|
7
|
+
loading?: ReactElement;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* The Component providing the AppSdk, the useSDK hook can only be used within this Provider
|
|
11
|
+
* @param props.loading an optional loading element that gets rendered while initializing the app
|
|
12
|
+
*/
|
|
13
|
+
export declare const SDKProvider: FC<SDKProviderProps>;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,39 @@
|
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.SDKProvider = exports.SDKContext = void 0;
|
|
18
|
+
var jsx_runtime_1 = require("react/jsx-runtime");
|
|
19
|
+
var react_1 = __importDefault(require("react"));
|
|
20
|
+
var app_sdk_1 = require("@contentful/app-sdk");
|
|
21
|
+
exports.SDKContext = react_1.default.createContext({ sdk: null });
|
|
22
|
+
/**
|
|
23
|
+
* The Component providing the AppSdk, the useSDK hook can only be used within this Provider
|
|
24
|
+
* @param props.loading an optional loading element that gets rendered while initializing the app
|
|
25
|
+
*/
|
|
26
|
+
var SDKProvider = function (props) {
|
|
27
|
+
var _a = react_1.default.useState(), sdk = _a[0], setSDK = _a[1];
|
|
28
|
+
react_1.default.useEffect(function () {
|
|
29
|
+
(0, app_sdk_1.init)(setSDK);
|
|
30
|
+
}, []);
|
|
31
|
+
if (!sdk) {
|
|
32
|
+
if (props.loading) {
|
|
33
|
+
return props.loading;
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
return (0, jsx_runtime_1.jsx)(exports.SDKContext.Provider, __assign({ value: { sdk: sdk } }, { children: props.children }));
|
|
38
|
+
};
|
|
39
|
+
exports.SDKProvider = SDKProvider;
|
package/dist/index.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from './useSDK';
|
|
2
|
+
export * from './SDKProvider';
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.
|
|
3
|
-
|
|
4
|
-
var
|
|
5
|
-
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
6
15
|
};
|
|
7
|
-
exports
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./useSDK"), exports);
|
|
18
|
+
__exportStar(require("./SDKProvider"), exports);
|
package/dist/useSDK.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { KnownSDK } from '@contentful/app-sdk';
|
|
2
|
+
/**
|
|
3
|
+
* A react hook returning the App SDK.
|
|
4
|
+
* - The type of SDK varies depending on the location where it is used.
|
|
5
|
+
* - As it is depending on the context providing the SDK, the hook can only be used within the SDKProvider
|
|
6
|
+
*/
|
|
7
|
+
export declare function useSDK<SDK extends KnownSDK = KnownSDK>(): SDK;
|
package/dist/useSDK.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useSDK = void 0;
|
|
4
|
+
var react_1 = require("react");
|
|
5
|
+
var SDKProvider_1 = require("./SDKProvider");
|
|
6
|
+
/**
|
|
7
|
+
* A react hook returning the App SDK.
|
|
8
|
+
* - The type of SDK varies depending on the location where it is used.
|
|
9
|
+
* - As it is depending on the context providing the SDK, the hook can only be used within the SDKProvider
|
|
10
|
+
*/
|
|
11
|
+
function useSDK() {
|
|
12
|
+
var sdk = (0, react_1.useContext)(SDKProvider_1.SDKContext).sdk;
|
|
13
|
+
if (!sdk) {
|
|
14
|
+
throw new Error('SDKContext not found. Make sure the useSDK hook is used inside the SDKProvider');
|
|
15
|
+
}
|
|
16
|
+
return sdk;
|
|
17
|
+
}
|
|
18
|
+
exports.useSDK = useSDK;
|
|
File without changes
|
|
@@ -0,0 +1,35 @@
|
|
|
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 index_1 = require("./index");
|
|
16
|
+
var mockedSdk = 'mocked-sdk';
|
|
17
|
+
jest.mock('react', function () {
|
|
18
|
+
return __assign(__assign({}, jest.requireActual('react')), { useContext: function () { return ({ sdk: mockedSdk }); } });
|
|
19
|
+
});
|
|
20
|
+
jest.mock('./SDKProvider', function () { return ({
|
|
21
|
+
SDKContext: {},
|
|
22
|
+
}); });
|
|
23
|
+
describe('useSDK', function () {
|
|
24
|
+
test('should return the sdk from the context', function () {
|
|
25
|
+
var result = (0, react_hooks_1.renderHook)(function () { return (0, index_1.useSDK)(); }).result;
|
|
26
|
+
expect(result.current).toBe('mocked-sdk');
|
|
27
|
+
});
|
|
28
|
+
test('should throw when the sdk is not in context', function () {
|
|
29
|
+
mockedSdk = undefined;
|
|
30
|
+
var result = (0, react_hooks_1.renderHook)(function () { return (0, index_1.useSDK)(); }).result;
|
|
31
|
+
expect(function () {
|
|
32
|
+
expect(result.current).not.toBe(undefined);
|
|
33
|
+
}).toThrow();
|
|
34
|
+
});
|
|
35
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contentful/react-apps-toolkit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Toolkit for building a Contentful app in React",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"author": "Contentful GmbH",
|
|
@@ -45,5 +45,8 @@
|
|
|
45
45
|
"publishConfig": {
|
|
46
46
|
"access": "public"
|
|
47
47
|
},
|
|
48
|
-
"
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@contentful/app-sdk": "^4.0.0"
|
|
50
|
+
},
|
|
51
|
+
"gitHead": "2066aafb2513dcd00c4894259667dd7a68aa6fa6"
|
|
49
52
|
}
|
package/dist/index.spec.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
var react_hooks_1 = require("@testing-library/react-hooks");
|
|
4
|
-
var index_1 = require("./index");
|
|
5
|
-
test('should increment counter', function () {
|
|
6
|
-
var result = (0, react_hooks_1.renderHook)(function () { return (0, index_1.useHelloWorld)(); }).result;
|
|
7
|
-
expect(result.current).toBe('hello world');
|
|
8
|
-
});
|