@faasjs/react 2.9.0 → 3.0.0-canary.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
@@ -28,7 +28,7 @@ npm install @faasjs/react
28
28
  import { FaasReactClient } from '@faasjs/react'
29
29
 
30
30
  const client = FaasReactClient({
31
- domain: 'localhost:8080/api'
31
+ baseUrl: 'localhost:8080/api/'
32
32
  })
33
33
  ```
34
34
 
@@ -54,6 +54,7 @@ const client = FaasReactClient({
54
54
  - [FaasParams](type-aliases/FaasParams.md)
55
55
  - [FaasReactClientInstance](type-aliases/FaasReactClientInstance.md)
56
56
  - [FaasReactClientOptions](type-aliases/FaasReactClientOptions.md)
57
+ - [OnError](type-aliases/OnError.md)
57
58
  - [OptionalWrapperProps](type-aliases/OptionalWrapperProps.md)
58
59
  - [Options](type-aliases/Options.md)
59
60
  - [ResponseHeaders](type-aliases/ResponseHeaders.md)
@@ -61,7 +62,6 @@ const client = FaasReactClient({
61
62
 
62
63
  ## Functions
63
64
 
64
- - [createSplitedContext](functions/createSplitedContext.md)
65
65
  - [createSplittingContext](functions/createSplittingContext.md)
66
66
  - [faas](functions/faas.md)
67
67
  - [FaasDataWrapper](functions/FaasDataWrapper.md)
package/dist/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { FaasAction, FaasParams, FaasData } from '@faasjs/types';
2
2
  export { FaasAction, FaasData, FaasParams } from '@faasjs/types';
3
- import { Options, ResponseError, Response as Response$1 } from '@faasjs/browser';
3
+ import { Options, FaasBrowserClient, ResponseError, BaseUrl, Response as Response$1 } from '@faasjs/browser';
4
4
  export { Options, Response, ResponseError, ResponseHeaders } from '@faasjs/browser';
5
5
  import * as react from 'react';
6
6
  import { ReactNode, ReactElement, Component, ComponentType, ComponentProps } from 'react';
@@ -67,16 +67,13 @@ declare function createSplittingContext<T extends Record<string, any>>(defaultVa
67
67
  }) => react.ReactNode;
68
68
  use: () => Readonly<T>;
69
69
  };
70
- /**
71
- * @deprecated Use `createSplittingContext` instead.
72
- */
73
- declare const createSplitedContext: typeof createSplittingContext;
74
70
 
75
71
  type FaasReactClientInstance = {
76
72
  id: string
77
73
  faas: <PathOrData extends FaasAction>(
78
74
  action: string | PathOrData,
79
- params: FaasParams<PathOrData>
75
+ params: FaasParams<PathOrData>,
76
+ options?: Options
80
77
  ) => Promise<Response<FaasData<PathOrData>>>
81
78
  useFaas: <PathOrData extends FaasAction>(
82
79
  action: string | PathOrData,
@@ -86,6 +83,8 @@ type FaasReactClientInstance = {
86
83
  FaasDataWrapper<PathOrData extends FaasAction>(
87
84
  props: FaasDataWrapperProps<PathOrData>
88
85
  ): JSX.Element
86
+ onError: OnError
87
+ browserClient: FaasBrowserClient
89
88
  }
90
89
 
91
90
  /**
@@ -112,14 +111,14 @@ type FaasDataWrapperProps<PathOrData extends FaasAction> = {
112
111
  ): JSX.Element | JSX.Element[]
113
112
  children?: React.ReactElement<Partial<FaasDataInjection>>
114
113
  fallback?: JSX.Element | false
115
- action: string
114
+ action: PathOrData | string
116
115
  params?: FaasParams<PathOrData>
117
116
  onDataChange?(args: FaasDataInjection<FaasData<PathOrData>>): void
118
117
  /** use custom data, should work with setData */
119
118
  data?: FaasData<PathOrData>
120
119
  /** use custom setData, should work with data */
121
120
  setData?: React.Dispatch<React.SetStateAction<FaasData<PathOrData>>>
122
- domain?: string
121
+ baseUrl?: string
123
122
  }
124
123
 
125
124
  type useFaasOptions<PathOrData extends FaasAction> = {
@@ -130,42 +129,46 @@ type useFaasOptions<PathOrData extends FaasAction> = {
130
129
  skip?: boolean | ((params: FaasParams<PathOrData>) => boolean)
131
130
  /** send the last request after milliseconds */
132
131
  debounce?: number
132
+ baseUrl?: string
133
133
  }
134
134
 
135
+ type OnError$1 = (action: string, params: Record<string, any>) => (res: ResponseError) => Promise<void>;
135
136
  type FaasReactClientOptions = {
136
- domain: string;
137
+ /** @default `/` */
138
+ baseUrl?: BaseUrl;
137
139
  options?: Options;
138
- onError?: (action: string, params: Record<string, any>) => (res: ResponseError) => Promise<void>;
140
+ onError?: OnError$1;
139
141
  };
140
142
  /**
141
143
  * Before use faas, you should initialize a FaasReactClient.
142
144
  *
143
- * @param props.domain {string} The domain of your faas server
145
+ * @param props.baseUrl {string} The baseUrl of your faas server
144
146
  * @param props.options {Options} The options of client
145
147
  * @returns {FaasReactClientInstance}
146
148
  *
147
149
  * @example
148
150
  * ```ts
149
151
  * const client = FaasReactClient({
150
- * domain: 'localhost:8080/api'
152
+ * baseUrl: 'localhost:8080/api/'
151
153
  * })
152
154
  * ```
153
155
  */
154
- declare function FaasReactClient({ domain, options, onError, }: FaasReactClientOptions): FaasReactClientInstance;
156
+ declare function FaasReactClient({ baseUrl, options, onError }?: FaasReactClientOptions): FaasReactClientInstance;
155
157
  /**
156
158
  * Get FaasReactClient instance
157
159
  *
158
- * @param domain {string} empty string for default domain
160
+ * @param host {string} empty string for default host
159
161
  * @returns {FaasReactClientInstance}
160
162
  *
161
163
  * @example
162
164
  * ```ts
163
165
  * getClient()
164
166
  * // or
165
- * getClient('another-domain')
167
+ * getClient('another-host')
166
168
  * ```
167
169
  */
168
- declare function getClient(domain?: string): FaasReactClientInstance;
170
+ declare function getClient(host?: string): FaasReactClientInstance;
171
+
169
172
  /**
170
173
  * Request faas server
171
174
  *
@@ -180,23 +183,7 @@ declare function getClient(domain?: string): FaasReactClientInstance;
180
183
  * })
181
184
  * ```
182
185
  */
183
- declare function faas<PathOrData extends FaasAction>(action: string | PathOrData, params: FaasParams<PathOrData>): Promise<Response$1<FaasData<PathOrData>>>;
184
- /**
185
- * Request faas server with React hook
186
- *
187
- * @param action {string} action name
188
- * @param defaultParams {object} initial action params
189
- * @returns {FaasDataInjection<any>}
190
- *
191
- * @example
192
- * ```tsx
193
- * function Post ({ id }) {
194
- * const { data } = useFaas<{ title: string }>('post/get', { id })
195
- * return <h1>{data.title}</h1>
196
- * }
197
- * ```
198
- */
199
- declare function useFaas<PathOrData extends FaasAction>(action: string | PathOrData, defaultParams: FaasParams<PathOrData>, options?: useFaasOptions<PathOrData>): FaasDataInjection<FaasData<PathOrData>>;
186
+ declare function faas<PathOrData extends FaasAction>(action: PathOrData | string, params: FaasParams<PathOrData>, options?: Options): Promise<Response$1<FaasData<PathOrData>>>;
200
187
 
201
188
  interface ErrorBoundaryProps {
202
189
  children?: ReactNode;
@@ -221,7 +208,7 @@ declare class ErrorBoundary extends Component<ErrorBoundaryProps, {
221
208
  render(): string | number | boolean | Iterable<ReactNode> | react_jsx_runtime.JSX.Element;
222
209
  }
223
210
 
224
- declare function FaasDataWrapper<PathOrData extends FaasAction>({ action, params, fallback, render, children, onDataChange, data, setData, domain, }: FaasDataWrapperProps<PathOrData>): JSX.Element;
211
+ declare function FaasDataWrapper<PathOrData extends FaasAction>({ action, params, fallback, render, children, onDataChange, data, setData, baseUrl, }: FaasDataWrapperProps<PathOrData>): JSX.Element;
225
212
  declare namespace FaasDataWrapper {
226
213
  var whyDidYouRender: boolean;
227
214
  }
@@ -265,4 +252,24 @@ declare const OptionalWrapper: React.FC<OptionalWrapperProps> & {
265
252
  whyDidYouRender: boolean;
266
253
  };
267
254
 
268
- export { ErrorBoundary, type ErrorBoundaryProps, type ErrorChildrenProps, type FaasDataInjection, FaasDataWrapper, type FaasDataWrapperProps, FaasReactClient, type FaasReactClientInstance, type FaasReactClientOptions, OptionalWrapper, type OptionalWrapperProps, createSplitedContext, createSplittingContext, faas, getClient, useConstant, useFaas, type useFaasOptions, withFaasData };
255
+ /**
256
+ * Request faas server with React hook
257
+ *
258
+ * @param action {string} action name
259
+ * @param defaultParams {object} initial action params
260
+ * @returns {FaasDataInjection<any>}
261
+ *
262
+ * @example
263
+ * ```tsx
264
+ * function Post ({ id }) {
265
+ * const { data } = useFaas<{ title: string }>('post/get', { id })
266
+ * return <h1>{data.title}</h1>
267
+ * }
268
+ * ```
269
+ */
270
+ declare function useFaas<PathOrData extends FaasAction>(action: PathOrData | string, defaultParams: FaasParams<PathOrData>, options?: useFaasOptions<PathOrData>): FaasDataInjection<FaasData<PathOrData>>;
271
+ declare namespace useFaas {
272
+ var whyDidYouRender: boolean;
273
+ }
274
+
275
+ export { ErrorBoundary, type ErrorBoundaryProps, type ErrorChildrenProps, type FaasDataInjection, FaasDataWrapper, type FaasDataWrapperProps, FaasReactClient, type FaasReactClientInstance, type FaasReactClientOptions, type OnError$1 as OnError, OptionalWrapper, type OptionalWrapperProps, createSplittingContext, faas, getClient, useConstant, useFaas, type useFaasOptions, withFaasData };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { FaasAction, FaasParams, FaasData } from '@faasjs/types';
2
2
  export { FaasAction, FaasData, FaasParams } from '@faasjs/types';
3
- import { Options, ResponseError, Response as Response$1 } from '@faasjs/browser';
3
+ import { Options, FaasBrowserClient, ResponseError, BaseUrl, Response as Response$1 } from '@faasjs/browser';
4
4
  export { Options, Response, ResponseError, ResponseHeaders } from '@faasjs/browser';
5
5
  import * as react from 'react';
6
6
  import { ReactNode, ReactElement, Component, ComponentType, ComponentProps } from 'react';
@@ -67,16 +67,13 @@ declare function createSplittingContext<T extends Record<string, any>>(defaultVa
67
67
  }) => react.ReactNode;
68
68
  use: () => Readonly<T>;
69
69
  };
70
- /**
71
- * @deprecated Use `createSplittingContext` instead.
72
- */
73
- declare const createSplitedContext: typeof createSplittingContext;
74
70
 
75
71
  type FaasReactClientInstance = {
76
72
  id: string
77
73
  faas: <PathOrData extends FaasAction>(
78
74
  action: string | PathOrData,
79
- params: FaasParams<PathOrData>
75
+ params: FaasParams<PathOrData>,
76
+ options?: Options
80
77
  ) => Promise<Response<FaasData<PathOrData>>>
81
78
  useFaas: <PathOrData extends FaasAction>(
82
79
  action: string | PathOrData,
@@ -86,6 +83,8 @@ type FaasReactClientInstance = {
86
83
  FaasDataWrapper<PathOrData extends FaasAction>(
87
84
  props: FaasDataWrapperProps<PathOrData>
88
85
  ): JSX.Element
86
+ onError: OnError
87
+ browserClient: FaasBrowserClient
89
88
  }
90
89
 
91
90
  /**
@@ -112,14 +111,14 @@ type FaasDataWrapperProps<PathOrData extends FaasAction> = {
112
111
  ): JSX.Element | JSX.Element[]
113
112
  children?: React.ReactElement<Partial<FaasDataInjection>>
114
113
  fallback?: JSX.Element | false
115
- action: string
114
+ action: PathOrData | string
116
115
  params?: FaasParams<PathOrData>
117
116
  onDataChange?(args: FaasDataInjection<FaasData<PathOrData>>): void
118
117
  /** use custom data, should work with setData */
119
118
  data?: FaasData<PathOrData>
120
119
  /** use custom setData, should work with data */
121
120
  setData?: React.Dispatch<React.SetStateAction<FaasData<PathOrData>>>
122
- domain?: string
121
+ baseUrl?: string
123
122
  }
124
123
 
125
124
  type useFaasOptions<PathOrData extends FaasAction> = {
@@ -130,42 +129,46 @@ type useFaasOptions<PathOrData extends FaasAction> = {
130
129
  skip?: boolean | ((params: FaasParams<PathOrData>) => boolean)
131
130
  /** send the last request after milliseconds */
132
131
  debounce?: number
132
+ baseUrl?: string
133
133
  }
134
134
 
135
+ type OnError$1 = (action: string, params: Record<string, any>) => (res: ResponseError) => Promise<void>;
135
136
  type FaasReactClientOptions = {
136
- domain: string;
137
+ /** @default `/` */
138
+ baseUrl?: BaseUrl;
137
139
  options?: Options;
138
- onError?: (action: string, params: Record<string, any>) => (res: ResponseError) => Promise<void>;
140
+ onError?: OnError$1;
139
141
  };
140
142
  /**
141
143
  * Before use faas, you should initialize a FaasReactClient.
142
144
  *
143
- * @param props.domain {string} The domain of your faas server
145
+ * @param props.baseUrl {string} The baseUrl of your faas server
144
146
  * @param props.options {Options} The options of client
145
147
  * @returns {FaasReactClientInstance}
146
148
  *
147
149
  * @example
148
150
  * ```ts
149
151
  * const client = FaasReactClient({
150
- * domain: 'localhost:8080/api'
152
+ * baseUrl: 'localhost:8080/api/'
151
153
  * })
152
154
  * ```
153
155
  */
154
- declare function FaasReactClient({ domain, options, onError, }: FaasReactClientOptions): FaasReactClientInstance;
156
+ declare function FaasReactClient({ baseUrl, options, onError }?: FaasReactClientOptions): FaasReactClientInstance;
155
157
  /**
156
158
  * Get FaasReactClient instance
157
159
  *
158
- * @param domain {string} empty string for default domain
160
+ * @param host {string} empty string for default host
159
161
  * @returns {FaasReactClientInstance}
160
162
  *
161
163
  * @example
162
164
  * ```ts
163
165
  * getClient()
164
166
  * // or
165
- * getClient('another-domain')
167
+ * getClient('another-host')
166
168
  * ```
167
169
  */
168
- declare function getClient(domain?: string): FaasReactClientInstance;
170
+ declare function getClient(host?: string): FaasReactClientInstance;
171
+
169
172
  /**
170
173
  * Request faas server
171
174
  *
@@ -180,23 +183,7 @@ declare function getClient(domain?: string): FaasReactClientInstance;
180
183
  * })
181
184
  * ```
182
185
  */
183
- declare function faas<PathOrData extends FaasAction>(action: string | PathOrData, params: FaasParams<PathOrData>): Promise<Response$1<FaasData<PathOrData>>>;
184
- /**
185
- * Request faas server with React hook
186
- *
187
- * @param action {string} action name
188
- * @param defaultParams {object} initial action params
189
- * @returns {FaasDataInjection<any>}
190
- *
191
- * @example
192
- * ```tsx
193
- * function Post ({ id }) {
194
- * const { data } = useFaas<{ title: string }>('post/get', { id })
195
- * return <h1>{data.title}</h1>
196
- * }
197
- * ```
198
- */
199
- declare function useFaas<PathOrData extends FaasAction>(action: string | PathOrData, defaultParams: FaasParams<PathOrData>, options?: useFaasOptions<PathOrData>): FaasDataInjection<FaasData<PathOrData>>;
186
+ declare function faas<PathOrData extends FaasAction>(action: PathOrData | string, params: FaasParams<PathOrData>, options?: Options): Promise<Response$1<FaasData<PathOrData>>>;
200
187
 
201
188
  interface ErrorBoundaryProps {
202
189
  children?: ReactNode;
@@ -221,7 +208,7 @@ declare class ErrorBoundary extends Component<ErrorBoundaryProps, {
221
208
  render(): string | number | boolean | Iterable<ReactNode> | react_jsx_runtime.JSX.Element;
222
209
  }
223
210
 
224
- declare function FaasDataWrapper<PathOrData extends FaasAction>({ action, params, fallback, render, children, onDataChange, data, setData, domain, }: FaasDataWrapperProps<PathOrData>): JSX.Element;
211
+ declare function FaasDataWrapper<PathOrData extends FaasAction>({ action, params, fallback, render, children, onDataChange, data, setData, baseUrl, }: FaasDataWrapperProps<PathOrData>): JSX.Element;
225
212
  declare namespace FaasDataWrapper {
226
213
  var whyDidYouRender: boolean;
227
214
  }
@@ -265,4 +252,24 @@ declare const OptionalWrapper: React.FC<OptionalWrapperProps> & {
265
252
  whyDidYouRender: boolean;
266
253
  };
267
254
 
268
- export { ErrorBoundary, type ErrorBoundaryProps, type ErrorChildrenProps, type FaasDataInjection, FaasDataWrapper, type FaasDataWrapperProps, FaasReactClient, type FaasReactClientInstance, type FaasReactClientOptions, OptionalWrapper, type OptionalWrapperProps, createSplitedContext, createSplittingContext, faas, getClient, useConstant, useFaas, type useFaasOptions, withFaasData };
255
+ /**
256
+ * Request faas server with React hook
257
+ *
258
+ * @param action {string} action name
259
+ * @param defaultParams {object} initial action params
260
+ * @returns {FaasDataInjection<any>}
261
+ *
262
+ * @example
263
+ * ```tsx
264
+ * function Post ({ id }) {
265
+ * const { data } = useFaas<{ title: string }>('post/get', { id })
266
+ * return <h1>{data.title}</h1>
267
+ * }
268
+ * ```
269
+ */
270
+ declare function useFaas<PathOrData extends FaasAction>(action: PathOrData | string, defaultParams: FaasParams<PathOrData>, options?: useFaasOptions<PathOrData>): FaasDataInjection<FaasData<PathOrData>>;
271
+ declare namespace useFaas {
272
+ var whyDidYouRender: boolean;
273
+ }
274
+
275
+ export { ErrorBoundary, type ErrorBoundaryProps, type ErrorChildrenProps, type FaasDataInjection, FaasDataWrapper, type FaasDataWrapperProps, FaasReactClient, type FaasReactClientInstance, type FaasReactClientOptions, type OnError$1 as OnError, OptionalWrapper, type OptionalWrapperProps, createSplittingContext, faas, getClient, useConstant, useFaas, type useFaasOptions, withFaasData };
package/dist/index.js CHANGED
@@ -18,11 +18,10 @@ function createSplittingContext(defaultValue) {
18
18
  const keys = Object.keys(defaultValue);
19
19
  for (const key of keys) contexts[key] = react.createContext(defaultValue[key]);
20
20
  function Provider(props) {
21
- var _a, _b;
22
21
  let children = props.children;
23
22
  for (const key of keys) {
24
23
  const Context = contexts[key];
25
- const value = (_b = (_a = props.value) == null ? void 0 : _a[key]) != null ? _b : defaultValue[key];
24
+ const value = props.value?.[key] ?? defaultValue[key];
26
25
  children = /* @__PURE__ */ jsxRuntime.jsx(Context.Provider, { value, children });
27
26
  }
28
27
  return children;
@@ -43,7 +42,6 @@ function createSplittingContext(defaultValue) {
43
42
  use
44
43
  };
45
44
  }
46
- var createSplitedContext = createSplittingContext;
47
45
  function FaasDataWrapper({
48
46
  action,
49
47
  params,
@@ -53,9 +51,9 @@ function FaasDataWrapper({
53
51
  onDataChange,
54
52
  data,
55
53
  setData,
56
- domain
54
+ baseUrl
57
55
  }) {
58
- const request = getClient(domain).useFaas(action, params, {
56
+ const request = getClient(baseUrl).useFaas(action, params, {
59
57
  data,
60
58
  setData
61
59
  });
@@ -86,145 +84,142 @@ FaasDataWrapper.whyDidYouRender = true;
86
84
  function withFaasData(Component2, faasProps) {
87
85
  return (props) => /* @__PURE__ */ jsxRuntime.jsx(FaasDataWrapper, { ...faasProps, children: /* @__PURE__ */ jsxRuntime.jsx(Component2, { ...props }) });
88
86
  }
89
- var clients = {};
90
- function FaasReactClient({
91
- domain,
92
- options,
93
- onError
94
- }) {
95
- const client = new browser.FaasBrowserClient(domain, options);
96
- async function faas2(action, params) {
97
- if (onError)
98
- return client.action(action, params).catch(async (res) => {
99
- await onError(action, params)(res);
100
- return Promise.reject(res);
101
- });
102
- return client.action(action, params);
103
- }
104
- function useFaas2(action, defaultParams, options2) {
105
- if (!options2) options2 = {};
106
- const [loading, setLoading] = react.useState(true);
107
- const [data, setData] = react.useState();
108
- const [error, setError] = react.useState();
109
- const [promise, setPromise] = react.useState();
110
- const [params, setParams] = react.useState(defaultParams);
111
- const [reloadTimes, setReloadTimes] = react.useState(0);
112
- const [fails, setFails] = react.useState(0);
113
- const [skip, setSkip] = react.useState(
114
- typeof options2.skip === "function" ? options2.skip(defaultParams) : options2.skip
87
+ function useFaas(action, defaultParams, options) {
88
+ if (!options) options = {};
89
+ const [loading, setLoading] = react.useState(true);
90
+ const [data, setData] = react.useState();
91
+ const [error, setError] = react.useState();
92
+ const [promise, setPromise] = react.useState();
93
+ const [params, setParams] = react.useState(defaultParams);
94
+ const [reloadTimes, setReloadTimes] = react.useState(0);
95
+ const [fails, setFails] = react.useState(0);
96
+ const [skip, setSkip] = react.useState(
97
+ typeof options.skip === "function" ? options.skip(defaultParams) : options.skip
98
+ );
99
+ const client = getClient(options.baseUrl);
100
+ react.useEffect(() => {
101
+ setSkip(
102
+ typeof options.skip === "function" ? options.skip(params) : options.skip
115
103
  );
116
- react.useEffect(() => {
117
- setSkip(
118
- typeof options2.skip === "function" ? options2.skip(params) : options2.skip
104
+ }, [
105
+ typeof options.skip === "function" ? JSON.stringify(params) : options.skip
106
+ ]);
107
+ react.useEffect(() => {
108
+ if (JSON.stringify(defaultParams) !== JSON.stringify(params)) {
109
+ setParams(defaultParams);
110
+ }
111
+ }, [JSON.stringify(defaultParams)]);
112
+ react.useEffect(() => {
113
+ if (!action || skip) {
114
+ setLoading(false);
115
+ return;
116
+ }
117
+ setLoading(true);
118
+ const controller = new AbortController();
119
+ function send() {
120
+ const request = client.faas(
121
+ action,
122
+ options.params || params,
123
+ { signal: controller.signal }
119
124
  );
120
- }, [
121
- typeof options2.skip === "function" ? JSON.stringify(params) : options2.skip
122
- ]);
123
- react.useEffect(() => {
124
- if (JSON.stringify(defaultParams) !== JSON.stringify(params)) {
125
- setParams(defaultParams);
126
- }
127
- }, [JSON.stringify(defaultParams)]);
128
- react.useEffect(() => {
129
- if (!action || skip) {
125
+ setPromise(request);
126
+ request.then((r) => {
127
+ options?.setData ? options.setData(r.data) : setData(r.data);
130
128
  setLoading(false);
131
- return;
132
- }
133
- setLoading(true);
134
- const controller = new AbortController();
135
- function send() {
136
- const request = client.action(
137
- action,
138
- options2.params || params,
139
- { signal: controller.signal }
140
- );
141
- setPromise(request);
142
- request.then((r) => {
143
- (options2 == null ? void 0 : options2.setData) ? options2.setData(r.data) : setData(r.data);
144
- setLoading(false);
145
- }).catch(async (e) => {
146
- if (typeof (e == null ? void 0 : e.message) === "string" && e.message.toLowerCase().indexOf("aborted") >= 0)
147
- return;
148
- if (!fails && typeof (e == null ? void 0 : e.message) === "string" && e.message.indexOf("Failed to fetch") >= 0) {
149
- console.warn(`FaasReactClient: ${e.message} retry...`);
150
- setFails(1);
151
- return send();
129
+ }).catch(async (e) => {
130
+ if (typeof e?.message === "string" && e.message.toLowerCase().indexOf("aborted") >= 0)
131
+ return;
132
+ if (!fails && typeof e?.message === "string" && e.message.indexOf("Failed to fetch") >= 0) {
133
+ console.warn(`FaasReactClient: ${e.message} retry...`);
134
+ setFails(1);
135
+ return send();
136
+ }
137
+ if (client.onError)
138
+ try {
139
+ await client.onError(action, params)(e);
140
+ } catch (error2) {
141
+ setError(error2);
152
142
  }
153
- if (onError)
154
- try {
155
- await onError(action, params)(e);
156
- } catch (error2) {
157
- setError(error2);
158
- }
159
- else setError(e);
160
- setLoading(false);
161
- return Promise.reject(e);
162
- });
163
- }
164
- if (options2 == null ? void 0 : options2.debounce) {
165
- const timeout = setTimeout(send, options2.debounce);
166
- return () => {
167
- clearTimeout(timeout);
168
- controller.abort();
169
- setLoading(false);
170
- };
171
- }
172
- send();
143
+ else setError(e);
144
+ setLoading(false);
145
+ return Promise.reject(e);
146
+ });
147
+ }
148
+ if (options?.debounce) {
149
+ const timeout = setTimeout(send, options.debounce);
173
150
  return () => {
151
+ clearTimeout(timeout);
174
152
  controller.abort();
175
153
  setLoading(false);
176
154
  };
177
- }, [action, JSON.stringify(options2.params || params), reloadTimes, skip]);
178
- const reload = react.useCallback(
179
- (params2) => {
180
- if (params2) setParams(params2);
181
- setReloadTimes((prev) => prev + 1);
182
- return promise;
183
- },
184
- [params]
185
- );
186
- return {
187
- action,
188
- params,
189
- loading,
190
- data: (options2 == null ? void 0 : options2.data) || data,
191
- reloadTimes,
192
- error,
193
- promise,
194
- reload,
195
- setData: (options2 == null ? void 0 : options2.setData) || setData,
196
- setLoading,
197
- setPromise,
198
- setError
155
+ }
156
+ send();
157
+ return () => {
158
+ controller.abort();
159
+ setLoading(false);
199
160
  };
200
- }
201
- useFaas2.whyDidYouRender = true;
161
+ }, [action, JSON.stringify(options.params || params), reloadTimes, skip]);
162
+ const reload = react.useCallback(
163
+ (params2) => {
164
+ if (params2) setParams(params2);
165
+ setReloadTimes((prev) => prev + 1);
166
+ return promise;
167
+ },
168
+ [params]
169
+ );
170
+ return {
171
+ action,
172
+ params,
173
+ loading,
174
+ data: options?.data || data,
175
+ reloadTimes,
176
+ error,
177
+ promise,
178
+ reload,
179
+ setData: options?.setData || setData,
180
+ setLoading,
181
+ setPromise,
182
+ setError
183
+ };
184
+ }
185
+ useFaas.whyDidYouRender = true;
186
+
187
+ // src/faas.ts
188
+ async function faas(action, params, options) {
189
+ const client = getClient(options?.baseUrl);
190
+ if (client.onError)
191
+ return client.faas(action, params, options).catch(async (res) => {
192
+ await client.onError(action, params)(res);
193
+ return Promise.reject(res);
194
+ });
195
+ return client.browserClient.action(action, params, options);
196
+ }
197
+ var clients = {};
198
+ function FaasReactClient({ baseUrl, options, onError } = {
199
+ baseUrl: "/"
200
+ }) {
201
+ const client = new browser.FaasBrowserClient(baseUrl, options);
202
202
  const reactClient = {
203
203
  id: client.id,
204
- faas: faas2,
205
- useFaas: useFaas2,
206
- FaasDataWrapper: (props) => /* @__PURE__ */ jsxRuntime.jsx(FaasDataWrapper, { domain, ...props })
204
+ faas: async (action, params, options2) => faas(action, params, options2),
205
+ useFaas: (action, defaultParams, options2) => useFaas(action, defaultParams, options2),
206
+ FaasDataWrapper: (props) => /* @__PURE__ */ jsxRuntime.jsx(FaasDataWrapper, { baseUrl, ...props }),
207
+ onError,
208
+ browserClient: client
207
209
  };
208
- clients[domain] = reactClient;
210
+ clients[baseUrl] = reactClient;
209
211
  return reactClient;
210
212
  }
211
- function getClient(domain) {
212
- const client = clients[domain || Object.keys(clients)[0]];
213
+ function getClient(host) {
214
+ const client = clients[host || Object.keys(clients)[0]];
213
215
  if (!client) {
214
216
  console.warn("FaasReactClient is not initialized manually, use default.");
215
- return FaasReactClient({
216
- domain: "/"
217
- });
217
+ return FaasReactClient();
218
218
  }
219
219
  return client;
220
220
  }
221
- async function faas(action, params) {
222
- return getClient().faas(action, params);
223
- }
224
- function useFaas(action, defaultParams, options) {
225
- return getClient().useFaas(action, defaultParams, options);
226
- }
227
221
  var ErrorBoundary = class extends react.Component {
222
+ static whyDidYouRender = true;
228
223
  constructor(props) {
229
224
  super(props);
230
225
  this.state = {
@@ -239,9 +234,8 @@ var ErrorBoundary = class extends react.Component {
239
234
  });
240
235
  }
241
236
  render() {
242
- var _a;
243
237
  const errorMessage = (this.state.error || "").toString();
244
- const errorDescription = ((_a = this.state.info) == null ? void 0 : _a.componentStack) ? this.state.info.componentStack : null;
238
+ const errorDescription = this.state.info?.componentStack ? this.state.info.componentStack : null;
245
239
  if (this.state.error) {
246
240
  if (this.props.onError)
247
241
  this.props.onError(this.state.error, this.state.info);
@@ -261,9 +255,7 @@ var ErrorBoundary = class extends react.Component {
261
255
  }
262
256
  };
263
257
  ErrorBoundary.whyDidYouRender = true;
264
- ErrorBoundary.whyDidYouRender = true;
265
258
  var OptionalWrapper = ({ condition, Wrapper, wrapperProps, children }) => {
266
- console.debug("[OptionalWrapper] %s %s", Wrapper.name, condition);
267
259
  return condition ? /* @__PURE__ */ jsxRuntime.jsx(Wrapper, { ...wrapperProps, children }) : /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children });
268
260
  };
269
261
  OptionalWrapper.whyDidYouRender = true;
@@ -272,7 +264,6 @@ exports.ErrorBoundary = ErrorBoundary;
272
264
  exports.FaasDataWrapper = FaasDataWrapper;
273
265
  exports.FaasReactClient = FaasReactClient;
274
266
  exports.OptionalWrapper = OptionalWrapper;
275
- exports.createSplitedContext = createSplitedContext;
276
267
  exports.createSplittingContext = createSplittingContext;
277
268
  exports.faas = faas;
278
269
  exports.getClient = getClient;
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { useRef, createContext, useState, useEffect, useMemo, cloneElement, Component, useContext, useCallback } from 'react';
1
+ import { useRef, createContext, useState, useEffect, useMemo, cloneElement, useCallback, Component, useContext } from 'react';
2
2
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
3
3
  import { FaasBrowserClient } from '@faasjs/browser';
4
4
 
@@ -16,11 +16,10 @@ function createSplittingContext(defaultValue) {
16
16
  const keys = Object.keys(defaultValue);
17
17
  for (const key of keys) contexts[key] = createContext(defaultValue[key]);
18
18
  function Provider(props) {
19
- var _a, _b;
20
19
  let children = props.children;
21
20
  for (const key of keys) {
22
21
  const Context = contexts[key];
23
- const value = (_b = (_a = props.value) == null ? void 0 : _a[key]) != null ? _b : defaultValue[key];
22
+ const value = props.value?.[key] ?? defaultValue[key];
24
23
  children = /* @__PURE__ */ jsx(Context.Provider, { value, children });
25
24
  }
26
25
  return children;
@@ -41,7 +40,6 @@ function createSplittingContext(defaultValue) {
41
40
  use
42
41
  };
43
42
  }
44
- var createSplitedContext = createSplittingContext;
45
43
  function FaasDataWrapper({
46
44
  action,
47
45
  params,
@@ -51,9 +49,9 @@ function FaasDataWrapper({
51
49
  onDataChange,
52
50
  data,
53
51
  setData,
54
- domain
52
+ baseUrl
55
53
  }) {
56
- const request = getClient(domain).useFaas(action, params, {
54
+ const request = getClient(baseUrl).useFaas(action, params, {
57
55
  data,
58
56
  setData
59
57
  });
@@ -84,145 +82,142 @@ FaasDataWrapper.whyDidYouRender = true;
84
82
  function withFaasData(Component2, faasProps) {
85
83
  return (props) => /* @__PURE__ */ jsx(FaasDataWrapper, { ...faasProps, children: /* @__PURE__ */ jsx(Component2, { ...props }) });
86
84
  }
87
- var clients = {};
88
- function FaasReactClient({
89
- domain,
90
- options,
91
- onError
92
- }) {
93
- const client = new FaasBrowserClient(domain, options);
94
- async function faas2(action, params) {
95
- if (onError)
96
- return client.action(action, params).catch(async (res) => {
97
- await onError(action, params)(res);
98
- return Promise.reject(res);
99
- });
100
- return client.action(action, params);
101
- }
102
- function useFaas2(action, defaultParams, options2) {
103
- if (!options2) options2 = {};
104
- const [loading, setLoading] = useState(true);
105
- const [data, setData] = useState();
106
- const [error, setError] = useState();
107
- const [promise, setPromise] = useState();
108
- const [params, setParams] = useState(defaultParams);
109
- const [reloadTimes, setReloadTimes] = useState(0);
110
- const [fails, setFails] = useState(0);
111
- const [skip, setSkip] = useState(
112
- typeof options2.skip === "function" ? options2.skip(defaultParams) : options2.skip
85
+ function useFaas(action, defaultParams, options) {
86
+ if (!options) options = {};
87
+ const [loading, setLoading] = useState(true);
88
+ const [data, setData] = useState();
89
+ const [error, setError] = useState();
90
+ const [promise, setPromise] = useState();
91
+ const [params, setParams] = useState(defaultParams);
92
+ const [reloadTimes, setReloadTimes] = useState(0);
93
+ const [fails, setFails] = useState(0);
94
+ const [skip, setSkip] = useState(
95
+ typeof options.skip === "function" ? options.skip(defaultParams) : options.skip
96
+ );
97
+ const client = getClient(options.baseUrl);
98
+ useEffect(() => {
99
+ setSkip(
100
+ typeof options.skip === "function" ? options.skip(params) : options.skip
113
101
  );
114
- useEffect(() => {
115
- setSkip(
116
- typeof options2.skip === "function" ? options2.skip(params) : options2.skip
102
+ }, [
103
+ typeof options.skip === "function" ? JSON.stringify(params) : options.skip
104
+ ]);
105
+ useEffect(() => {
106
+ if (JSON.stringify(defaultParams) !== JSON.stringify(params)) {
107
+ setParams(defaultParams);
108
+ }
109
+ }, [JSON.stringify(defaultParams)]);
110
+ useEffect(() => {
111
+ if (!action || skip) {
112
+ setLoading(false);
113
+ return;
114
+ }
115
+ setLoading(true);
116
+ const controller = new AbortController();
117
+ function send() {
118
+ const request = client.faas(
119
+ action,
120
+ options.params || params,
121
+ { signal: controller.signal }
117
122
  );
118
- }, [
119
- typeof options2.skip === "function" ? JSON.stringify(params) : options2.skip
120
- ]);
121
- useEffect(() => {
122
- if (JSON.stringify(defaultParams) !== JSON.stringify(params)) {
123
- setParams(defaultParams);
124
- }
125
- }, [JSON.stringify(defaultParams)]);
126
- useEffect(() => {
127
- if (!action || skip) {
123
+ setPromise(request);
124
+ request.then((r) => {
125
+ options?.setData ? options.setData(r.data) : setData(r.data);
128
126
  setLoading(false);
129
- return;
130
- }
131
- setLoading(true);
132
- const controller = new AbortController();
133
- function send() {
134
- const request = client.action(
135
- action,
136
- options2.params || params,
137
- { signal: controller.signal }
138
- );
139
- setPromise(request);
140
- request.then((r) => {
141
- (options2 == null ? void 0 : options2.setData) ? options2.setData(r.data) : setData(r.data);
142
- setLoading(false);
143
- }).catch(async (e) => {
144
- if (typeof (e == null ? void 0 : e.message) === "string" && e.message.toLowerCase().indexOf("aborted") >= 0)
145
- return;
146
- if (!fails && typeof (e == null ? void 0 : e.message) === "string" && e.message.indexOf("Failed to fetch") >= 0) {
147
- console.warn(`FaasReactClient: ${e.message} retry...`);
148
- setFails(1);
149
- return send();
127
+ }).catch(async (e) => {
128
+ if (typeof e?.message === "string" && e.message.toLowerCase().indexOf("aborted") >= 0)
129
+ return;
130
+ if (!fails && typeof e?.message === "string" && e.message.indexOf("Failed to fetch") >= 0) {
131
+ console.warn(`FaasReactClient: ${e.message} retry...`);
132
+ setFails(1);
133
+ return send();
134
+ }
135
+ if (client.onError)
136
+ try {
137
+ await client.onError(action, params)(e);
138
+ } catch (error2) {
139
+ setError(error2);
150
140
  }
151
- if (onError)
152
- try {
153
- await onError(action, params)(e);
154
- } catch (error2) {
155
- setError(error2);
156
- }
157
- else setError(e);
158
- setLoading(false);
159
- return Promise.reject(e);
160
- });
161
- }
162
- if (options2 == null ? void 0 : options2.debounce) {
163
- const timeout = setTimeout(send, options2.debounce);
164
- return () => {
165
- clearTimeout(timeout);
166
- controller.abort();
167
- setLoading(false);
168
- };
169
- }
170
- send();
141
+ else setError(e);
142
+ setLoading(false);
143
+ return Promise.reject(e);
144
+ });
145
+ }
146
+ if (options?.debounce) {
147
+ const timeout = setTimeout(send, options.debounce);
171
148
  return () => {
149
+ clearTimeout(timeout);
172
150
  controller.abort();
173
151
  setLoading(false);
174
152
  };
175
- }, [action, JSON.stringify(options2.params || params), reloadTimes, skip]);
176
- const reload = useCallback(
177
- (params2) => {
178
- if (params2) setParams(params2);
179
- setReloadTimes((prev) => prev + 1);
180
- return promise;
181
- },
182
- [params]
183
- );
184
- return {
185
- action,
186
- params,
187
- loading,
188
- data: (options2 == null ? void 0 : options2.data) || data,
189
- reloadTimes,
190
- error,
191
- promise,
192
- reload,
193
- setData: (options2 == null ? void 0 : options2.setData) || setData,
194
- setLoading,
195
- setPromise,
196
- setError
153
+ }
154
+ send();
155
+ return () => {
156
+ controller.abort();
157
+ setLoading(false);
197
158
  };
198
- }
199
- useFaas2.whyDidYouRender = true;
159
+ }, [action, JSON.stringify(options.params || params), reloadTimes, skip]);
160
+ const reload = useCallback(
161
+ (params2) => {
162
+ if (params2) setParams(params2);
163
+ setReloadTimes((prev) => prev + 1);
164
+ return promise;
165
+ },
166
+ [params]
167
+ );
168
+ return {
169
+ action,
170
+ params,
171
+ loading,
172
+ data: options?.data || data,
173
+ reloadTimes,
174
+ error,
175
+ promise,
176
+ reload,
177
+ setData: options?.setData || setData,
178
+ setLoading,
179
+ setPromise,
180
+ setError
181
+ };
182
+ }
183
+ useFaas.whyDidYouRender = true;
184
+
185
+ // src/faas.ts
186
+ async function faas(action, params, options) {
187
+ const client = getClient(options?.baseUrl);
188
+ if (client.onError)
189
+ return client.faas(action, params, options).catch(async (res) => {
190
+ await client.onError(action, params)(res);
191
+ return Promise.reject(res);
192
+ });
193
+ return client.browserClient.action(action, params, options);
194
+ }
195
+ var clients = {};
196
+ function FaasReactClient({ baseUrl, options, onError } = {
197
+ baseUrl: "/"
198
+ }) {
199
+ const client = new FaasBrowserClient(baseUrl, options);
200
200
  const reactClient = {
201
201
  id: client.id,
202
- faas: faas2,
203
- useFaas: useFaas2,
204
- FaasDataWrapper: (props) => /* @__PURE__ */ jsx(FaasDataWrapper, { domain, ...props })
202
+ faas: async (action, params, options2) => faas(action, params, options2),
203
+ useFaas: (action, defaultParams, options2) => useFaas(action, defaultParams, options2),
204
+ FaasDataWrapper: (props) => /* @__PURE__ */ jsx(FaasDataWrapper, { baseUrl, ...props }),
205
+ onError,
206
+ browserClient: client
205
207
  };
206
- clients[domain] = reactClient;
208
+ clients[baseUrl] = reactClient;
207
209
  return reactClient;
208
210
  }
209
- function getClient(domain) {
210
- const client = clients[domain || Object.keys(clients)[0]];
211
+ function getClient(host) {
212
+ const client = clients[host || Object.keys(clients)[0]];
211
213
  if (!client) {
212
214
  console.warn("FaasReactClient is not initialized manually, use default.");
213
- return FaasReactClient({
214
- domain: "/"
215
- });
215
+ return FaasReactClient();
216
216
  }
217
217
  return client;
218
218
  }
219
- async function faas(action, params) {
220
- return getClient().faas(action, params);
221
- }
222
- function useFaas(action, defaultParams, options) {
223
- return getClient().useFaas(action, defaultParams, options);
224
- }
225
219
  var ErrorBoundary = class extends Component {
220
+ static whyDidYouRender = true;
226
221
  constructor(props) {
227
222
  super(props);
228
223
  this.state = {
@@ -237,9 +232,8 @@ var ErrorBoundary = class extends Component {
237
232
  });
238
233
  }
239
234
  render() {
240
- var _a;
241
235
  const errorMessage = (this.state.error || "").toString();
242
- const errorDescription = ((_a = this.state.info) == null ? void 0 : _a.componentStack) ? this.state.info.componentStack : null;
236
+ const errorDescription = this.state.info?.componentStack ? this.state.info.componentStack : null;
243
237
  if (this.state.error) {
244
238
  if (this.props.onError)
245
239
  this.props.onError(this.state.error, this.state.info);
@@ -259,11 +253,9 @@ var ErrorBoundary = class extends Component {
259
253
  }
260
254
  };
261
255
  ErrorBoundary.whyDidYouRender = true;
262
- ErrorBoundary.whyDidYouRender = true;
263
256
  var OptionalWrapper = ({ condition, Wrapper, wrapperProps, children }) => {
264
- console.debug("[OptionalWrapper] %s %s", Wrapper.name, condition);
265
257
  return condition ? /* @__PURE__ */ jsx(Wrapper, { ...wrapperProps, children }) : /* @__PURE__ */ jsx(Fragment, { children });
266
258
  };
267
259
  OptionalWrapper.whyDidYouRender = true;
268
260
 
269
- export { ErrorBoundary, FaasDataWrapper, FaasReactClient, OptionalWrapper, createSplitedContext, createSplittingContext, faas, getClient, useConstant, useFaas, withFaasData };
261
+ export { ErrorBoundary, FaasDataWrapper, FaasReactClient, OptionalWrapper, createSplittingContext, faas, getClient, useConstant, useFaas, withFaasData };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/react",
3
- "version": "2.9.0",
3
+ "version": "3.0.0-canary.2",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -34,7 +34,7 @@
34
34
  "dist"
35
35
  ],
36
36
  "dependencies": {
37
- "@faasjs/browser": "2.9.0"
37
+ "@faasjs/browser": "3.0.0-canary.2"
38
38
  },
39
39
  "peerDependencies": {
40
40
  "react": "*"
@@ -44,7 +44,7 @@
44
44
  "react": "*"
45
45
  },
46
46
  "engines": {
47
- "npm": ">=9.0.0",
48
- "node": ">=18.0.0"
47
+ "node": ">=22.0.0",
48
+ "npm": ">=10.0.0"
49
49
  }
50
50
  }