@faasjs/react 0.0.2-beta.360 → 0.0.2-beta.364

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