@wix/sdk-react 0.1.3 → 0.1.4

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.
@@ -1,11 +1,12 @@
1
1
  // src/index.tsx
2
2
  import * as React from "react";
3
- import { createContext, useMemo } from "react";
3
+ import { createContext, useEffect, useMemo } from "react";
4
4
  import {
5
5
  createClient
6
- } from "@wix/api-client";
7
- export * from "@wix/api-client";
6
+ } from "@wix/sdk";
7
+ export * from "@wix/sdk";
8
8
  var WixContext = createContext(void 0);
9
+ var HostEnvironmentContext = createContext(void 0);
9
10
  function WixProvider(props) {
10
11
  const { children, auth, modules, host } = props;
11
12
  const client = useMemo(
@@ -16,7 +17,26 @@ function WixProvider(props) {
16
17
  }),
17
18
  [auth, modules, host]
18
19
  );
19
- return /* @__PURE__ */ React.createElement(WixContext.Provider, { value: client }, children);
20
+ return /* @__PURE__ */ React.createElement(WixContext.Provider, { value: client }, host ? /* @__PURE__ */ React.createElement(HostEnvironmentProvider, { host }, children) : children);
21
+ }
22
+ function HostEnvironmentProvider(props) {
23
+ const [environment, setEnvironment] = React.useState(null);
24
+ useEffect(() => {
25
+ if (props.host.environment) {
26
+ setEnvironment(props.host.environment);
27
+ } else {
28
+ const handle = props.host.channel.observeState(
29
+ (_, env) => setEnvironment(env)
30
+ );
31
+ return () => {
32
+ Promise.resolve(handle).then((h) => h.disconnect());
33
+ };
34
+ }
35
+ }, [props.host.environment, props.host.channel]);
36
+ if (!environment) {
37
+ return null;
38
+ }
39
+ return /* @__PURE__ */ React.createElement(HostEnvironmentContext.Provider, { value: environment }, props.children);
20
40
  }
21
41
  function useWix() {
22
42
  const sdk = React.useContext(WixContext);
@@ -35,8 +55,18 @@ function useWixModules(modules) {
35
55
  const sdk = useWix();
36
56
  return useMemo(() => sdk.use(modules), [sdk, modules]);
37
57
  }
58
+ function useEnvironment() {
59
+ const environment = React.useContext(HostEnvironmentContext);
60
+ if (!environment) {
61
+ throw new Error(
62
+ "Host environment context not found. Make sure to render SDKProvider in the component tree and provide a host parameter"
63
+ );
64
+ }
65
+ return environment;
66
+ }
38
67
  export {
39
68
  WixProvider,
69
+ useEnvironment,
40
70
  useWix,
41
71
  useWixAuth,
42
72
  useWixModules
package/build/index.d.mts CHANGED
@@ -1,15 +1,15 @@
1
1
  import * as React from 'react';
2
- import { Descriptors, AuthenticationStrategy, AssertHostMatches, WixClient, BuildDescriptors } from '@wix/api-client';
3
- export * from '@wix/api-client';
2
+ import { Descriptors, Host, AuthenticationStrategy, AssertHostMatches, WixClient, BuildDescriptors } from '@wix/sdk';
3
+ export * from '@wix/sdk';
4
4
 
5
- declare function WixProvider<T extends Descriptors<Host>, Host = unknown>(props: {
6
- children: React.ReactNode;
7
- auth?: AuthenticationStrategy<Host>;
8
- modules?: AssertHostMatches<T, Host>;
9
- host?: Host;
10
- }): React.JSX.Element;
5
+ declare function WixProvider<T extends Descriptors<H>, H extends Host<any> = Host>(props: React.PropsWithChildren<{
6
+ auth?: AuthenticationStrategy<H>;
7
+ modules?: AssertHostMatches<T, H>;
8
+ host?: H;
9
+ }>): React.JSX.Element;
11
10
  declare function useWix<T extends WixClient = WixClient>(): T;
12
11
  declare function useWixAuth<T extends AuthenticationStrategy>(): T;
13
- declare function useWixModules<T extends Descriptors<unknown>>(modules: T): BuildDescriptors<T>;
12
+ declare function useWixModules<T extends Descriptors<Host<any>>>(modules: T): BuildDescriptors<T>;
13
+ declare function useEnvironment<T>(): T;
14
14
 
15
- export { WixProvider, useWix, useWixAuth, useWixModules };
15
+ export { WixProvider, useEnvironment, useWix, useWixAuth, useWixModules };
package/build/index.d.ts CHANGED
@@ -1,15 +1,15 @@
1
1
  import * as React from 'react';
2
- import { Descriptors, AuthenticationStrategy, AssertHostMatches, WixClient, BuildDescriptors } from '@wix/api-client';
3
- export * from '@wix/api-client';
2
+ import { Descriptors, Host, AuthenticationStrategy, AssertHostMatches, WixClient, BuildDescriptors } from '@wix/sdk';
3
+ export * from '@wix/sdk';
4
4
 
5
- declare function WixProvider<T extends Descriptors<Host>, Host = unknown>(props: {
6
- children: React.ReactNode;
7
- auth?: AuthenticationStrategy<Host>;
8
- modules?: AssertHostMatches<T, Host>;
9
- host?: Host;
10
- }): React.JSX.Element;
5
+ declare function WixProvider<T extends Descriptors<H>, H extends Host<any> = Host>(props: React.PropsWithChildren<{
6
+ auth?: AuthenticationStrategy<H>;
7
+ modules?: AssertHostMatches<T, H>;
8
+ host?: H;
9
+ }>): React.JSX.Element;
11
10
  declare function useWix<T extends WixClient = WixClient>(): T;
12
11
  declare function useWixAuth<T extends AuthenticationStrategy>(): T;
13
- declare function useWixModules<T extends Descriptors<unknown>>(modules: T): BuildDescriptors<T>;
12
+ declare function useWixModules<T extends Descriptors<Host<any>>>(modules: T): BuildDescriptors<T>;
13
+ declare function useEnvironment<T>(): T;
14
14
 
15
- export { WixProvider, useWix, useWixAuth, useWixModules };
15
+ export { WixProvider, useEnvironment, useWix, useWixAuth, useWixModules };
package/build/index.js CHANGED
@@ -31,6 +31,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  var src_exports = {};
32
32
  __export(src_exports, {
33
33
  WixProvider: () => WixProvider,
34
+ useEnvironment: () => useEnvironment,
34
35
  useWix: () => useWix,
35
36
  useWixAuth: () => useWixAuth,
36
37
  useWixModules: () => useWixModules
@@ -38,20 +39,40 @@ __export(src_exports, {
38
39
  module.exports = __toCommonJS(src_exports);
39
40
  var React = __toESM(require("react"));
40
41
  var import_react = require("react");
41
- var import_api_client = require("@wix/api-client");
42
- __reExport(src_exports, require("@wix/api-client"), module.exports);
42
+ var import_sdk = require("@wix/sdk");
43
+ __reExport(src_exports, require("@wix/sdk"), module.exports);
43
44
  var WixContext = (0, import_react.createContext)(void 0);
45
+ var HostEnvironmentContext = (0, import_react.createContext)(void 0);
44
46
  function WixProvider(props) {
45
47
  const { children, auth, modules, host } = props;
46
48
  const client = (0, import_react.useMemo)(
47
- () => (0, import_api_client.createClient)({
49
+ () => (0, import_sdk.createClient)({
48
50
  auth,
49
51
  modules,
50
52
  host
51
53
  }),
52
54
  [auth, modules, host]
53
55
  );
54
- return /* @__PURE__ */ React.createElement(WixContext.Provider, { value: client }, children);
56
+ return /* @__PURE__ */ React.createElement(WixContext.Provider, { value: client }, host ? /* @__PURE__ */ React.createElement(HostEnvironmentProvider, { host }, children) : children);
57
+ }
58
+ function HostEnvironmentProvider(props) {
59
+ const [environment, setEnvironment] = React.useState(null);
60
+ (0, import_react.useEffect)(() => {
61
+ if (props.host.environment) {
62
+ setEnvironment(props.host.environment);
63
+ } else {
64
+ const handle = props.host.channel.observeState(
65
+ (_, env) => setEnvironment(env)
66
+ );
67
+ return () => {
68
+ Promise.resolve(handle).then((h) => h.disconnect());
69
+ };
70
+ }
71
+ }, [props.host.environment, props.host.channel]);
72
+ if (!environment) {
73
+ return null;
74
+ }
75
+ return /* @__PURE__ */ React.createElement(HostEnvironmentContext.Provider, { value: environment }, props.children);
55
76
  }
56
77
  function useWix() {
57
78
  const sdk = React.useContext(WixContext);
@@ -70,11 +91,21 @@ function useWixModules(modules) {
70
91
  const sdk = useWix();
71
92
  return (0, import_react.useMemo)(() => sdk.use(modules), [sdk, modules]);
72
93
  }
94
+ function useEnvironment() {
95
+ const environment = React.useContext(HostEnvironmentContext);
96
+ if (!environment) {
97
+ throw new Error(
98
+ "Host environment context not found. Make sure to render SDKProvider in the component tree and provide a host parameter"
99
+ );
100
+ }
101
+ return environment;
102
+ }
73
103
  // Annotate the CommonJS export names for ESM import in node:
74
104
  0 && (module.exports = {
75
105
  WixProvider,
106
+ useEnvironment,
76
107
  useWix,
77
108
  useWixAuth,
78
109
  useWixModules,
79
- ...require("@wix/api-client")
110
+ ...require("@wix/sdk")
80
111
  });
package/build/index.mjs CHANGED
@@ -1,11 +1,12 @@
1
1
  // src/index.tsx
2
2
  import * as React from "react";
3
- import { createContext, useMemo } from "react";
3
+ import { createContext, useEffect, useMemo } from "react";
4
4
  import {
5
5
  createClient
6
- } from "@wix/api-client";
7
- export * from "@wix/api-client";
6
+ } from "@wix/sdk";
7
+ export * from "@wix/sdk";
8
8
  var WixContext = createContext(void 0);
9
+ var HostEnvironmentContext = createContext(void 0);
9
10
  function WixProvider(props) {
10
11
  const { children, auth, modules, host } = props;
11
12
  const client = useMemo(
@@ -16,7 +17,26 @@ function WixProvider(props) {
16
17
  }),
17
18
  [auth, modules, host]
18
19
  );
19
- return /* @__PURE__ */ React.createElement(WixContext.Provider, { value: client }, children);
20
+ return /* @__PURE__ */ React.createElement(WixContext.Provider, { value: client }, host ? /* @__PURE__ */ React.createElement(HostEnvironmentProvider, { host }, children) : children);
21
+ }
22
+ function HostEnvironmentProvider(props) {
23
+ const [environment, setEnvironment] = React.useState(null);
24
+ useEffect(() => {
25
+ if (props.host.environment) {
26
+ setEnvironment(props.host.environment);
27
+ } else {
28
+ const handle = props.host.channel.observeState(
29
+ (_, env) => setEnvironment(env)
30
+ );
31
+ return () => {
32
+ Promise.resolve(handle).then((h) => h.disconnect());
33
+ };
34
+ }
35
+ }, [props.host.environment, props.host.channel]);
36
+ if (!environment) {
37
+ return null;
38
+ }
39
+ return /* @__PURE__ */ React.createElement(HostEnvironmentContext.Provider, { value: environment }, props.children);
20
40
  }
21
41
  function useWix() {
22
42
  const sdk = React.useContext(WixContext);
@@ -35,8 +55,18 @@ function useWixModules(modules) {
35
55
  const sdk = useWix();
36
56
  return useMemo(() => sdk.use(modules), [sdk, modules]);
37
57
  }
58
+ function useEnvironment() {
59
+ const environment = React.useContext(HostEnvironmentContext);
60
+ if (!environment) {
61
+ throw new Error(
62
+ "Host environment context not found. Make sure to render SDKProvider in the component tree and provide a host parameter"
63
+ );
64
+ }
65
+ return environment;
66
+ }
38
67
  export {
39
68
  WixProvider,
69
+ useEnvironment,
40
70
  useWix,
41
71
  useWixAuth,
42
72
  useWixModules
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wix/sdk-react",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "license": "UNLICENSED",
5
5
  "main": "build/index.js",
6
6
  "module": "build/index.mjs",
@@ -21,18 +21,18 @@
21
21
  "typecheck": "tsc --noEmit"
22
22
  },
23
23
  "dependencies": {
24
- "@wix/api-client": "1.2.4"
24
+ "@wix/sdk": "1.2.5"
25
25
  },
26
26
  "peerDependencies": {
27
27
  "react": "^16.0.0 || ^17.0.0 || ^18.0.0"
28
28
  },
29
29
  "devDependencies": {
30
- "@swc/core": "^1.3.69",
31
- "@swc/jest": "^0.2.26",
32
- "@testing-library/jest-dom": "^5.16.5",
30
+ "@swc/core": "^1.3.70",
31
+ "@swc/jest": "^0.2.27",
32
+ "@testing-library/jest-dom": "^5.17.0",
33
33
  "@testing-library/react": "^14.0.0",
34
34
  "@types/react": "^18.2.15",
35
- "@wix/ecom": "^1.0.263",
35
+ "@wix/ecom": "^1.0.274",
36
36
  "eslint": "^7.32.0",
37
37
  "eslint-config-sdk": "0.0.0",
38
38
  "is-ci": "^3.0.1",
@@ -60,5 +60,5 @@
60
60
  "eslintConfig": {
61
61
  "extends": "sdk"
62
62
  },
63
- "falconPackageHash": "463ec42a92032850845143b343ca581c45415bbaa869383f0706e14f"
63
+ "falconPackageHash": "3687c57855777c7c9d98d85b5ac23659f9e5313f154b233ab089e777"
64
64
  }