@faasjs/react 0.0.2-beta.358 → 0.0.2-beta.362

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
@@ -7,3 +7,254 @@ React 插件
7
7
  [![NPM Beta Version](https://img.shields.io/npm/v/@faasjs/react/beta.svg)](https://www.npmjs.com/package/@faasjs/react)
8
8
 
9
9
  https://faasjs.com/guide/excel/react.html
10
+
11
+ ## Modules
12
+
13
+ ### Classes
14
+
15
+ - [FaasBrowserClient](classes/FaasBrowserClient.md)
16
+ - [Response](classes/Response.md)
17
+ - [ResponseError](classes/ResponseError.md)
18
+
19
+ ### Type aliases
20
+
21
+ - [FaasDataInjection](modules.md#faasdatainjection)
22
+ - [FaasDataWrapperProps](modules.md#faasdatawrapperprops)
23
+ - [FaasReactClientInstance](modules.md#faasreactclientinstance)
24
+ - [Options](modules.md#options)
25
+ - [ResponseHeaders](modules.md#responseheaders)
26
+
27
+ ### Functions
28
+
29
+ - [FaasDataWrapper](modules.md#faasdatawrapper)
30
+ - [FaasReactClient](modules.md#faasreactclient)
31
+ - [faas](modules.md#faas)
32
+ - [getClient](modules.md#getclient)
33
+ - [useFaas](modules.md#usefaas)
34
+
35
+ ## Type aliases
36
+
37
+ ### FaasDataInjection
38
+
39
+ Ƭ **FaasDataInjection**<`Data`\>: `Object`
40
+
41
+ #### Type parameters
42
+
43
+ | Name | Type |
44
+ | :------ | :------ |
45
+ | `Data` | `any` |
46
+
47
+ #### Type declaration
48
+
49
+ | Name | Type |
50
+ | :------ | :------ |
51
+ | `action` | `string` \| `any` |
52
+ | `data` | `Data` |
53
+ | `error` | `any` |
54
+ | `loading` | `boolean` |
55
+ | `params` | `Record`<`string`, `any`\> |
56
+ | `promise` | `Promise`<[`Response`](classes/Response.md)<`Data`\>\> |
57
+ | `setData` | `React.Dispatch`<`React.SetStateAction`<`Data`\>\> |
58
+ | `setError` | `React.Dispatch`<`React.SetStateAction`<`any`\>\> |
59
+ | `setLoading` | `React.Dispatch`<`React.SetStateAction`<`boolean`\>\> |
60
+ | `setPromise` | `React.Dispatch`<`React.SetStateAction`<`Promise`<[`Response`](classes/Response.md)<`Data`\>\>\>\> |
61
+ | `reload` | (`params?`: `Record`<`string`, `any`\>) => `Promise`<[`Response`](classes/Response.md)<`Data`\>\> |
62
+
63
+ ___
64
+
65
+ ### FaasDataWrapperProps
66
+
67
+ Ƭ **FaasDataWrapperProps**<`PathOrData`\>: `Object`
68
+
69
+ #### Type parameters
70
+
71
+ | Name | Type |
72
+ | :------ | :------ |
73
+ | `PathOrData` | extends `FaasAction` |
74
+
75
+ #### Type declaration
76
+
77
+ | Name | Type | Description |
78
+ | :------ | :------ | :------ |
79
+ | `action` | `string` | - |
80
+ | `data?` | `FaasData`<`PathOrData`\> | use custom data, should work with setData |
81
+ | `fallback?` | `JSX.Element` \| ``false`` | - |
82
+ | `params?` | `FaasParams`<`PathOrData`\> | - |
83
+ | `setData?` | `React.Dispatch`<`React.SetStateAction`<`FaasData`<`PathOrData`\>\>\> | use custom setData, should work with data |
84
+ | `onDataChange?` | (`args`: [`FaasDataInjection`](modules.md#faasdatainjection)<`FaasData`<`PathOrData`\>\>) => `void` | - |
85
+ | `render?` | (`args`: [`FaasDataInjection`](modules.md#faasdatainjection)<`FaasData`<`PathOrData`\>\>) => `Element` \| `Element`[] | - |
86
+
87
+ ___
88
+
89
+ ### FaasReactClientInstance
90
+
91
+ Ƭ **FaasReactClientInstance**: `Object`
92
+
93
+ #### Type declaration
94
+
95
+ | Name | Type |
96
+ | :------ | :------ |
97
+ | `FaasDataWrapper` | <PathOrData\>(`props`: [`FaasDataWrapperProps`](modules.md#faasdatawrapperprops)<`PathOrData`\>) => `Element` |
98
+ | `faas` | <PathOrData\>(`action`: `string` \| `PathOrData`, `params`: `FaasParams`<`PathOrData`\>) => `Promise`<[`Response`](classes/Response.md)<`FaasData`<`PathOrData`\>\>\> |
99
+ | `useFaas` | <PathOrData\>(`action`: `string` \| `PathOrData`, `defaultParams`: `FaasParams`<`PathOrData`\>) => [`FaasDataInjection`](modules.md#faasdatainjection)<`FaasData`<`PathOrData`\>\> |
100
+
101
+ ___
102
+
103
+ ### Options
104
+
105
+ Ƭ **Options**: `RequestInit` & { `headers?`: { [key: string]: `string`; } ; `beforeRequest?`: (`__namedParameters`: { `action`: `string` ; `options`: [`Options`](modules.md#options) ; `params`: `Record`<`string`, `any`\> }) => `void` \| `Promise`<`void`\> }
106
+
107
+ ___
108
+
109
+ ### ResponseHeaders
110
+
111
+ Ƭ **ResponseHeaders**: `Object`
112
+
113
+ #### Index signature
114
+
115
+ ▪ [key: `string`]: `string`
116
+
117
+ ## Functions
118
+
119
+ ### FaasDataWrapper
120
+
121
+ ▸ **FaasDataWrapper**<`PathOrData`\>(`props`): `JSX.Element`
122
+
123
+ A data wrapper for react components
124
+
125
+ **`example`**
126
+ <FaasDataWrapper<{
127
+ id: string
128
+ title: string
129
+ }>
130
+ action='post/get'
131
+ params={ { id: 1 } }
132
+ render={ ({ data }) => <h1>{ data.title }</h1> }
133
+ />
134
+
135
+ #### Type parameters
136
+
137
+ | Name | Type |
138
+ | :------ | :------ |
139
+ | `PathOrData` | extends `FaasAction` |
140
+
141
+ #### Parameters
142
+
143
+ | Name | Type |
144
+ | :------ | :------ |
145
+ | `props` | [`FaasDataWrapperProps`](modules.md#faasdatawrapperprops)<`PathOrData`\> |
146
+
147
+ #### Returns
148
+
149
+ `JSX.Element`
150
+
151
+ ___
152
+
153
+ ### FaasReactClient
154
+
155
+ ▸ **FaasReactClient**(`__namedParameters`): [`FaasReactClientInstance`](modules.md#faasreactclientinstance)
156
+
157
+ Before use faas, you should initialize a FaasReactClient.
158
+
159
+ **`example`**
160
+ const client = FaasReactClient({
161
+ domain: 'localhost:8080/api'
162
+ })
163
+
164
+ #### Parameters
165
+
166
+ | Name | Type |
167
+ | :------ | :------ |
168
+ | `__namedParameters` | `Object` |
169
+ | `__namedParameters.domain` | `string` |
170
+ | `__namedParameters.options?` | [`Options`](modules.md#options) |
171
+ | `__namedParameters.onError?` | (`action`: `string`, `params`: `Record`<`string`, `any`\>) => (`res`: [`ResponseError`](classes/ResponseError.md)) => `Promise`<`void`\> |
172
+
173
+ #### Returns
174
+
175
+ [`FaasReactClientInstance`](modules.md#faasreactclientinstance)
176
+
177
+ ___
178
+
179
+ ### faas
180
+
181
+ ▸ **faas**<`PathOrData`\>(`action`, `params`): `Promise`<[`Response`](classes/Response.md)<`FaasData`<`PathOrData`\>\>\>
182
+
183
+ Request faas server
184
+
185
+ **`example`**
186
+ faas<{ title: string }>('post/get', { id: 1 }).then(res => {
187
+ console.log(res.data.title)
188
+ })
189
+
190
+ #### Type parameters
191
+
192
+ | Name | Type |
193
+ | :------ | :------ |
194
+ | `PathOrData` | extends `FaasAction` |
195
+
196
+ #### Parameters
197
+
198
+ | Name | Type | Description |
199
+ | :------ | :------ | :------ |
200
+ | `action` | `string` \| `PathOrData` | action name |
201
+ | `params` | `FaasParams`<`PathOrData`\> | action params |
202
+
203
+ #### Returns
204
+
205
+ `Promise`<[`Response`](classes/Response.md)<`FaasData`<`PathOrData`\>\>\>
206
+
207
+ ___
208
+
209
+ ### getClient
210
+
211
+ ▸ **getClient**(`domain?`): [`FaasReactClientInstance`](modules.md#faasreactclientinstance)
212
+
213
+ Get FaasReactClient instance
214
+
215
+ **`example`**
216
+ getClient()
217
+ // or
218
+ getClient('another-domain')
219
+
220
+ #### Parameters
221
+
222
+ | Name | Type | Description |
223
+ | :------ | :------ | :------ |
224
+ | `domain?` | `string` | empty string for default domain |
225
+
226
+ #### Returns
227
+
228
+ [`FaasReactClientInstance`](modules.md#faasreactclientinstance)
229
+
230
+ ___
231
+
232
+ ### useFaas
233
+
234
+ ▸ **useFaas**<`PathOrData`\>(`action`, `defaultParams`): [`FaasDataInjection`](modules.md#faasdatainjection)<`FaasData`<`PathOrData`\>\>
235
+
236
+ Request faas server with React hook
237
+
238
+ **`example`**
239
+ function Post ({ id }) {
240
+ const { data } = useFaas<{ title: string }>('post/get', { id })
241
+
242
+ return <h1>{data.title}</h1>
243
+ }
244
+
245
+ #### Type parameters
246
+
247
+ | Name | Type |
248
+ | :------ | :------ |
249
+ | `PathOrData` | extends `FaasAction` |
250
+
251
+ #### Parameters
252
+
253
+ | Name | Type | Description |
254
+ | :------ | :------ | :------ |
255
+ | `action` | `string` \| `PathOrData` | action name |
256
+ | `defaultParams` | `FaasParams`<`PathOrData`\> | initial action params |
257
+
258
+ #### Returns
259
+
260
+ [`FaasDataInjection`](modules.md#faasdatainjection)<`FaasData`<`PathOrData`\>\>
package/dist/index.d.ts CHANGED
@@ -16,7 +16,7 @@ declare type FaasDataInjection<Data = any> = {
16
16
  setError: React.Dispatch<React.SetStateAction<any>>;
17
17
  };
18
18
  declare type FaasDataWrapperProps<PathOrData extends FaasAction> = {
19
- render?(args: FaasDataInjection<FaasData<PathOrData>>): JSX.Element;
19
+ render?(args: FaasDataInjection<FaasData<PathOrData>>): JSX.Element | JSX.Element[];
20
20
  fallback?: JSX.Element | false;
21
21
  action: string;
22
22
  params?: FaasParams<PathOrData>;
@@ -31,14 +31,70 @@ declare type FaasReactClientInstance = {
31
31
  useFaas: <PathOrData extends FaasAction>(action: string | PathOrData, defaultParams: FaasParams<PathOrData>) => FaasDataInjection<FaasData<PathOrData>>;
32
32
  FaasDataWrapper<PathOrData extends FaasAction>(props: FaasDataWrapperProps<PathOrData>): JSX.Element;
33
33
  };
34
+ /**
35
+ * Before use faas, you should initialize a FaasReactClient.
36
+ *
37
+ * @param props.domain {string} The domain of your faas server
38
+ * @param props.options {Options} The options of client
39
+ * @returns {FaasReactClientInstance}
40
+ * @example
41
+ * const client = FaasReactClient({
42
+ * domain: 'localhost:8080/api'
43
+ * })
44
+ */
34
45
  declare function FaasReactClient({ domain, options, onError }: {
35
46
  domain: string;
36
47
  options?: Options;
37
48
  onError?: (action: string, params: Record<string, any>) => (res: ResponseError) => Promise<void>;
38
49
  }): FaasReactClientInstance;
50
+ /**
51
+ * Get FaasReactClient instance
52
+ * @param domain {string} empty string for default domain
53
+ * @returns {FaasReactClientInstance}
54
+ * @example
55
+ * getClient()
56
+ * // or
57
+ * getClient('another-domain')
58
+ */
39
59
  declare function getClient(domain?: string): FaasReactClientInstance;
40
- declare function FaasDataWrapper<PathOrData extends FaasAction>(props: FaasDataWrapperProps<PathOrData> & {
41
- client?: FaasReactClientInstance;
42
- }): JSX.Element;
60
+ /**
61
+ * Request faas server
62
+ *
63
+ * @param action {string} action name
64
+ * @param params {object} action params
65
+ * @returns {Promise<Response<any>>}
66
+ * @example
67
+ * faas<{ title: string }>('post/get', { id: 1 }).then(res => {
68
+ * console.log(res.data.title)
69
+ * })
70
+ */
71
+ declare function faas<PathOrData extends FaasAction>(action: string | PathOrData, params: FaasParams<PathOrData>): Promise<Response<FaasData<PathOrData>>>;
72
+ /**
73
+ * Request faas server with React hook
74
+ * @param action {string} action name
75
+ * @param defaultParams {object} initial action params
76
+ * @returns {FaasDataInjection<any>}
77
+ * @example
78
+ * function Post ({ id }) {
79
+ * const { data } = useFaas<{ title: string }>('post/get', { id })
80
+ *
81
+ * return <h1>{data.title}</h1>
82
+ * }
83
+ */
84
+ declare function useFaas<PathOrData extends FaasAction>(action: string | PathOrData, defaultParams: FaasParams<PathOrData>): FaasDataInjection<FaasData<PathOrData>>;
85
+ /**
86
+ * A data wrapper for react components
87
+ * @returns {JSX.Element}
88
+ * @example
89
+ * <FaasDataWrapper<{
90
+ * id: string
91
+ * title: string
92
+ * }>
93
+ * action='post/get'
94
+ * params={ { id: 1 } }
95
+ * render={ ({ data }) => <h1>{ data.title }</h1> }
96
+ * />
97
+ */
98
+ declare function FaasDataWrapper<PathOrData extends FaasAction>(props: FaasDataWrapperProps<PathOrData>): JSX.Element;
43
99
 
44
- export { FaasDataInjection, FaasDataWrapper, FaasDataWrapperProps, FaasReactClient, FaasReactClientInstance, getClient };
100
+ export { FaasDataInjection, FaasDataWrapper, FaasDataWrapperProps, FaasReactClient, FaasReactClientInstance, faas, getClient, useFaas };
package/dist/index.js CHANGED
@@ -26,7 +26,9 @@ var src_exports = {};
26
26
  __export(src_exports, {
27
27
  FaasDataWrapper: () => FaasDataWrapper,
28
28
  FaasReactClient: () => FaasReactClient,
29
- getClient: () => getClient
29
+ faas: () => faas,
30
+ getClient: () => getClient,
31
+ useFaas: () => useFaas
30
32
  });
31
33
  var import_browser = require("@faasjs/browser");
32
34
  var import_react = require("react");
@@ -37,7 +39,7 @@ function FaasReactClient({
37
39
  onError
38
40
  }) {
39
41
  const client = new import_browser.FaasBrowserClient(domain, options);
40
- async function faas(action, params) {
42
+ async function faas2(action, params) {
41
43
  if (onError)
42
44
  return client.action(action, params).catch(async (res) => {
43
45
  await onError(action, params)(res);
@@ -45,7 +47,7 @@ function FaasReactClient({
45
47
  });
46
48
  return client.action(action, params);
47
49
  }
48
- function useFaas(action, defaultParams, options2) {
50
+ function useFaas2(action, defaultParams, options2) {
49
51
  if (!options2)
50
52
  options2 = {};
51
53
  const [loading, setLoading] = (0, import_react.useState)(true);
@@ -100,8 +102,8 @@ function FaasReactClient({
100
102
  };
101
103
  }
102
104
  const reactClient = {
103
- faas,
104
- useFaas,
105
+ faas: faas2,
106
+ useFaas: useFaas2,
105
107
  FaasDataWrapper({
106
108
  action,
107
109
  params,
@@ -111,7 +113,7 @@ function FaasReactClient({
111
113
  data,
112
114
  setData
113
115
  }) {
114
- const request = useFaas(action, params, {
116
+ const request = useFaas2(action, params, {
115
117
  data,
116
118
  setData
117
119
  });
@@ -138,8 +140,14 @@ function getClient(domain) {
138
140
  throw Error("FaasReactClient is not initialized");
139
141
  return client;
140
142
  }
143
+ async function faas(action, params) {
144
+ return getClient().faas(action, params);
145
+ }
146
+ function useFaas(action, defaultParams) {
147
+ return getClient().useFaas(action, defaultParams);
148
+ }
141
149
  function FaasDataWrapper(props) {
142
- const [client, setClient] = (0, import_react.useState)(props.client);
150
+ const [client, setClient] = (0, import_react.useState)();
143
151
  (0, import_react.useEffect)(() => {
144
152
  if (client)
145
153
  return;
@@ -154,5 +162,7 @@ module.exports = __toCommonJS(src_exports);
154
162
  0 && (module.exports = {
155
163
  FaasDataWrapper,
156
164
  FaasReactClient,
157
- getClient
165
+ faas,
166
+ getClient,
167
+ useFaas
158
168
  });
package/dist/index.mjs CHANGED
@@ -14,7 +14,7 @@ function FaasReactClient({
14
14
  onError
15
15
  }) {
16
16
  const client = new FaasBrowserClient(domain, options);
17
- async function faas(action, params) {
17
+ async function faas2(action, params) {
18
18
  if (onError)
19
19
  return client.action(action, params).catch(async (res) => {
20
20
  await onError(action, params)(res);
@@ -22,7 +22,7 @@ function FaasReactClient({
22
22
  });
23
23
  return client.action(action, params);
24
24
  }
25
- function useFaas(action, defaultParams, options2) {
25
+ function useFaas2(action, defaultParams, options2) {
26
26
  if (!options2)
27
27
  options2 = {};
28
28
  const [loading, setLoading] = useState(true);
@@ -77,8 +77,8 @@ function FaasReactClient({
77
77
  };
78
78
  }
79
79
  const reactClient = {
80
- faas,
81
- useFaas,
80
+ faas: faas2,
81
+ useFaas: useFaas2,
82
82
  FaasDataWrapper({
83
83
  action,
84
84
  params,
@@ -88,7 +88,7 @@ function FaasReactClient({
88
88
  data,
89
89
  setData
90
90
  }) {
91
- const request = useFaas(action, params, {
91
+ const request = useFaas2(action, params, {
92
92
  data,
93
93
  setData
94
94
  });
@@ -115,8 +115,14 @@ function getClient(domain) {
115
115
  throw Error("FaasReactClient is not initialized");
116
116
  return client;
117
117
  }
118
+ async function faas(action, params) {
119
+ return getClient().faas(action, params);
120
+ }
121
+ function useFaas(action, defaultParams) {
122
+ return getClient().useFaas(action, defaultParams);
123
+ }
118
124
  function FaasDataWrapper(props) {
119
- const [client, setClient] = useState(props.client);
125
+ const [client, setClient] = useState();
120
126
  useEffect(() => {
121
127
  if (client)
122
128
  return;
@@ -129,5 +135,7 @@ function FaasDataWrapper(props) {
129
135
  export {
130
136
  FaasDataWrapper,
131
137
  FaasReactClient,
132
- getClient
138
+ faas,
139
+ getClient,
140
+ useFaas
133
141
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/react",
3
- "version": "0.0.2-beta.358",
3
+ "version": "0.0.2-beta.362",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -23,8 +23,8 @@
23
23
  "dist"
24
24
  ],
25
25
  "peerDependencies": {
26
- "@faasjs/browser": "^0.0.2-beta.358",
27
- "@faasjs/types": "^0.0.2-beta.358",
26
+ "@faasjs/browser": "^0.0.2-beta.362",
27
+ "@faasjs/types": "^0.0.2-beta.362",
28
28
  "react": "*"
29
29
  },
30
30
  "devDependencies": {