@wix/sdk-react 0.1.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.
@@ -0,0 +1,70 @@
1
+ // src/index.tsx
2
+ import * as React from "react";
3
+ import { createContext, useEffect, useMemo } from "react";
4
+ import {
5
+ createClient
6
+ } from "@wix/api-client";
7
+ var WixContext = createContext(void 0);
8
+ var HostEnvironmentContext = createContext(void 0);
9
+ function WixProvider(props) {
10
+ const { children, auth, modules, host } = props;
11
+ const client = useMemo(
12
+ () => createClient({
13
+ auth,
14
+ modules,
15
+ host
16
+ }),
17
+ [auth, modules, host]
18
+ );
19
+ return /* @__PURE__ */ React.createElement(WixContext.Provider, { value: client }, host ? /* @__PURE__ */ React.createElement(HostEnvironmentProvider, { host }, children) : children);
20
+ }
21
+ function HostEnvironmentProvider(props) {
22
+ const [environment, setEnvironment] = React.useState(null);
23
+ useEffect(() => {
24
+ if (props.host.environment) {
25
+ setEnvironment(props.host.environment);
26
+ } else {
27
+ const handle = props.host.channel.observeEnvironment(setEnvironment);
28
+ return () => {
29
+ Promise.resolve(handle).then((h) => h.disconnect());
30
+ };
31
+ }
32
+ }, [props.host.environment, props.host.channel]);
33
+ if (!environment) {
34
+ return null;
35
+ }
36
+ return /* @__PURE__ */ React.createElement(HostEnvironmentContext.Provider, { value: environment }, props.children);
37
+ }
38
+ function useWix() {
39
+ const sdk = React.useContext(WixContext);
40
+ if (!sdk) {
41
+ throw new Error(
42
+ "SDK context not found. Make sure to render SDKProvider in the component tree"
43
+ );
44
+ }
45
+ return sdk;
46
+ }
47
+ function useWixAuth() {
48
+ const sdk = useWix();
49
+ return sdk.auth;
50
+ }
51
+ function useWixModules(modules) {
52
+ const sdk = useWix();
53
+ return useMemo(() => sdk.use(modules), [sdk, modules]);
54
+ }
55
+ function useEnvironment() {
56
+ const environment = React.useContext(HostEnvironmentContext);
57
+ if (!environment) {
58
+ throw new Error(
59
+ "Host environment context not found. Make sure to render SDKProvider in the component tree and provide a host parameter"
60
+ );
61
+ }
62
+ return environment;
63
+ }
64
+ export {
65
+ WixProvider,
66
+ useEnvironment,
67
+ useWix,
68
+ useWixAuth,
69
+ useWixModules
70
+ };
@@ -0,0 +1,14 @@
1
+ import * as React from 'react';
2
+ import { Descriptors, Host, AuthenticationStrategy, AssertHostMatches, WixClient, BuildDescriptors } from '@wix/api-client';
3
+
4
+ declare function WixProvider<T extends Descriptors<H>, H extends Host<any> = Host>(props: React.PropsWithChildren<{
5
+ auth?: AuthenticationStrategy<H>;
6
+ modules?: AssertHostMatches<T, H>;
7
+ host?: H;
8
+ }>): React.JSX.Element;
9
+ declare function useWix<T extends WixClient = WixClient>(): T;
10
+ declare function useWixAuth<T extends AuthenticationStrategy>(): T;
11
+ declare function useWixModules<T extends Descriptors<Host<any>>>(modules: T): BuildDescriptors<T>;
12
+ declare function useEnvironment<T>(): T;
13
+
14
+ export { WixProvider, useEnvironment, useWix, useWixAuth, useWixModules };
@@ -0,0 +1,14 @@
1
+ import * as React from 'react';
2
+ import { Descriptors, Host, AuthenticationStrategy, AssertHostMatches, WixClient, BuildDescriptors } from '@wix/api-client';
3
+
4
+ declare function WixProvider<T extends Descriptors<H>, H extends Host<any> = Host>(props: React.PropsWithChildren<{
5
+ auth?: AuthenticationStrategy<H>;
6
+ modules?: AssertHostMatches<T, H>;
7
+ host?: H;
8
+ }>): React.JSX.Element;
9
+ declare function useWix<T extends WixClient = WixClient>(): T;
10
+ declare function useWixAuth<T extends AuthenticationStrategy>(): T;
11
+ declare function useWixModules<T extends Descriptors<Host<any>>>(modules: T): BuildDescriptors<T>;
12
+ declare function useEnvironment<T>(): T;
13
+
14
+ export { WixProvider, useEnvironment, useWix, useWixAuth, useWixModules };
package/build/index.js ADDED
@@ -0,0 +1,106 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // src/index.tsx
30
+ var src_exports = {};
31
+ __export(src_exports, {
32
+ WixProvider: () => WixProvider,
33
+ useEnvironment: () => useEnvironment,
34
+ useWix: () => useWix,
35
+ useWixAuth: () => useWixAuth,
36
+ useWixModules: () => useWixModules
37
+ });
38
+ module.exports = __toCommonJS(src_exports);
39
+ var React = __toESM(require("react"));
40
+ var import_react = require("react");
41
+ var import_api_client = require("@wix/api-client");
42
+ var WixContext = (0, import_react.createContext)(void 0);
43
+ var HostEnvironmentContext = (0, import_react.createContext)(void 0);
44
+ function WixProvider(props) {
45
+ const { children, auth, modules, host } = props;
46
+ const client = (0, import_react.useMemo)(
47
+ () => (0, import_api_client.createClient)({
48
+ auth,
49
+ modules,
50
+ host
51
+ }),
52
+ [auth, modules, host]
53
+ );
54
+ return /* @__PURE__ */ React.createElement(WixContext.Provider, { value: client }, host ? /* @__PURE__ */ React.createElement(HostEnvironmentProvider, { host }, children) : children);
55
+ }
56
+ function HostEnvironmentProvider(props) {
57
+ const [environment, setEnvironment] = React.useState(null);
58
+ (0, import_react.useEffect)(() => {
59
+ if (props.host.environment) {
60
+ setEnvironment(props.host.environment);
61
+ } else {
62
+ const handle = props.host.channel.observeEnvironment(setEnvironment);
63
+ return () => {
64
+ Promise.resolve(handle).then((h) => h.disconnect());
65
+ };
66
+ }
67
+ }, [props.host.environment, props.host.channel]);
68
+ if (!environment) {
69
+ return null;
70
+ }
71
+ return /* @__PURE__ */ React.createElement(HostEnvironmentContext.Provider, { value: environment }, props.children);
72
+ }
73
+ function useWix() {
74
+ const sdk = React.useContext(WixContext);
75
+ if (!sdk) {
76
+ throw new Error(
77
+ "SDK context not found. Make sure to render SDKProvider in the component tree"
78
+ );
79
+ }
80
+ return sdk;
81
+ }
82
+ function useWixAuth() {
83
+ const sdk = useWix();
84
+ return sdk.auth;
85
+ }
86
+ function useWixModules(modules) {
87
+ const sdk = useWix();
88
+ return (0, import_react.useMemo)(() => sdk.use(modules), [sdk, modules]);
89
+ }
90
+ function useEnvironment() {
91
+ const environment = React.useContext(HostEnvironmentContext);
92
+ if (!environment) {
93
+ throw new Error(
94
+ "Host environment context not found. Make sure to render SDKProvider in the component tree and provide a host parameter"
95
+ );
96
+ }
97
+ return environment;
98
+ }
99
+ // Annotate the CommonJS export names for ESM import in node:
100
+ 0 && (module.exports = {
101
+ WixProvider,
102
+ useEnvironment,
103
+ useWix,
104
+ useWixAuth,
105
+ useWixModules
106
+ });
@@ -0,0 +1,70 @@
1
+ // src/index.tsx
2
+ import * as React from "react";
3
+ import { createContext, useEffect, useMemo } from "react";
4
+ import {
5
+ createClient
6
+ } from "@wix/api-client";
7
+ var WixContext = createContext(void 0);
8
+ var HostEnvironmentContext = createContext(void 0);
9
+ function WixProvider(props) {
10
+ const { children, auth, modules, host } = props;
11
+ const client = useMemo(
12
+ () => createClient({
13
+ auth,
14
+ modules,
15
+ host
16
+ }),
17
+ [auth, modules, host]
18
+ );
19
+ return /* @__PURE__ */ React.createElement(WixContext.Provider, { value: client }, host ? /* @__PURE__ */ React.createElement(HostEnvironmentProvider, { host }, children) : children);
20
+ }
21
+ function HostEnvironmentProvider(props) {
22
+ const [environment, setEnvironment] = React.useState(null);
23
+ useEffect(() => {
24
+ if (props.host.environment) {
25
+ setEnvironment(props.host.environment);
26
+ } else {
27
+ const handle = props.host.channel.observeEnvironment(setEnvironment);
28
+ return () => {
29
+ Promise.resolve(handle).then((h) => h.disconnect());
30
+ };
31
+ }
32
+ }, [props.host.environment, props.host.channel]);
33
+ if (!environment) {
34
+ return null;
35
+ }
36
+ return /* @__PURE__ */ React.createElement(HostEnvironmentContext.Provider, { value: environment }, props.children);
37
+ }
38
+ function useWix() {
39
+ const sdk = React.useContext(WixContext);
40
+ if (!sdk) {
41
+ throw new Error(
42
+ "SDK context not found. Make sure to render SDKProvider in the component tree"
43
+ );
44
+ }
45
+ return sdk;
46
+ }
47
+ function useWixAuth() {
48
+ const sdk = useWix();
49
+ return sdk.auth;
50
+ }
51
+ function useWixModules(modules) {
52
+ const sdk = useWix();
53
+ return useMemo(() => sdk.use(modules), [sdk, modules]);
54
+ }
55
+ function useEnvironment() {
56
+ const environment = React.useContext(HostEnvironmentContext);
57
+ if (!environment) {
58
+ throw new Error(
59
+ "Host environment context not found. Make sure to render SDKProvider in the component tree and provide a host parameter"
60
+ );
61
+ }
62
+ return environment;
63
+ }
64
+ export {
65
+ WixProvider,
66
+ useEnvironment,
67
+ useWix,
68
+ useWixAuth,
69
+ useWixModules
70
+ };
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "@wix/sdk-react",
3
+ "version": "0.1.0",
4
+ "license": "UNLICENSED",
5
+ "main": "build/index.js",
6
+ "module": "build/index.mjs",
7
+ "browser": "build/browser/index.mjs",
8
+ "sideEffects": false,
9
+ "types": "build/index.d.ts",
10
+ "files": [
11
+ "build"
12
+ ],
13
+ "publishConfig": {
14
+ "registry": "https://registry.npmjs.org/",
15
+ "access": "public"
16
+ },
17
+ "scripts": {
18
+ "build": "tsup",
19
+ "lint": "eslint --max-warnings=0 .",
20
+ "test": "jest",
21
+ "typecheck": "tsc --noEmit"
22
+ },
23
+ "dependencies": {
24
+ "@wix/api-client": "1.2.1"
25
+ },
26
+ "peerDependencies": {
27
+ "react": "^16.0.0 || ^17.0.0 || ^18.0.0"
28
+ },
29
+ "devDependencies": {
30
+ "@swc/core": "^1.3.67",
31
+ "@swc/jest": "^0.2.26",
32
+ "@testing-library/jest-dom": "^5.16.5",
33
+ "@testing-library/react": "^14.0.0",
34
+ "@types/react": "^18.2.14",
35
+ "@wix/ecom": "^1.0.248",
36
+ "eslint": "^7.32.0",
37
+ "eslint-config-sdk": "0.0.0",
38
+ "is-ci": "^3.0.1",
39
+ "jest": "^27.5.1",
40
+ "jest-teamcity": "^1.11.0",
41
+ "react": "^18.2.0",
42
+ "react-dom": "^18.2.0",
43
+ "tsup": "^7.1.0",
44
+ "typescript": "~4.9.5"
45
+ },
46
+ "wix": {
47
+ "artifact": {
48
+ "groupId": "com.wixpress",
49
+ "artifactId": "sdk-react"
50
+ },
51
+ "validations": {
52
+ "source": [
53
+ "lint"
54
+ ],
55
+ "postDependenciesBuild": [
56
+ "typecheck"
57
+ ]
58
+ }
59
+ },
60
+ "eslintConfig": {
61
+ "extends": "sdk"
62
+ },
63
+ "falconPackageHash": "6993243e7fc4f838fe31d9f49137d8fbfc062bed228a4bca7d39f66b"
64
+ }