@faasjs/react 0.0.2-beta.296 → 0.0.2-beta.304

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
@@ -2,7 +2,7 @@
2
2
 
3
3
  React 插件
4
4
 
5
- [![License: MIT](https://img.shields.io/npm/l/@faasjs/react.svg)](https://github.com/faasjs/faasjs/blob/master/packages/faasjs/react/LICENSE)
5
+ [![License: MIT](https://img.shields.io/npm/l/@faasjs/react.svg)](https://github.com/faasjs/faasjs/blob/main/packages/faasjs/react/LICENSE)
6
6
  [![NPM Stable Version](https://img.shields.io/npm/v/@faasjs/react/stable.svg)](https://www.npmjs.com/package/@faasjs/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
 
package/lib/index.js CHANGED
@@ -6,11 +6,13 @@
6
6
 
7
7
  function FaasReactClient({ domain, options, onError }) {
8
8
  const client = new browser.FaasBrowserClient(domain, options);
9
- // async function faas<Path extends keyof FaasActions> (action: Path, params: FaasActions[Path]['request']): Promise<Response<FaasActions[Path]['response']>>
10
9
  async function faas(action, params) {
11
10
  if (onError)
12
11
  return client.action(action, params)
13
- .catch(onError(action, params));
12
+ .catch(async (res) => {
13
+ await onError(action, params)(res);
14
+ return Promise.reject(res);
15
+ });
14
16
  return client.action(action, params);
15
17
  }
16
18
  function useFaas(action, defaultParams) {
@@ -30,23 +32,21 @@
30
32
  const request = client.action(action, params);
31
33
  setPromise(request);
32
34
  request
33
- .then(r => {
34
- setData(r === null || r === void 0 ? void 0 : r.data);
35
- })
35
+ .then(r => setData(r.data))
36
36
  .catch(async (e) => {
37
37
  if (onError)
38
38
  try {
39
- setData(await onError(action, params)(e));
39
+ await onError(action, params)(e);
40
40
  }
41
41
  catch (error) {
42
42
  setError(error);
43
43
  }
44
- setError(e);
44
+ else
45
+ setError(e);
46
+ return Promise.reject(e);
45
47
  })
46
48
  .finally(() => setLoading(false));
47
- return () => {
48
- setLoading(false);
49
- };
49
+ return () => setLoading(false);
50
50
  }, [
51
51
  action,
52
52
  JSON.stringify(params),
@@ -79,7 +79,7 @@
79
79
  if (!loaded && !request.loading)
80
80
  setLoaded(true);
81
81
  }, [request.loading]);
82
- react.useEffect(() => {
82
+ react.useEffect(function () {
83
83
  if (onDataChange)
84
84
  onDataChange(request);
85
85
  }, [JSON.stringify(request.data)]);
@@ -1,30 +1,31 @@
1
1
  /// <reference types="react" />
2
- import { Options, Params, Response, ResponseError } from '@faasjs/browser';
3
- export type { FaasBrowserClient, Options, Params, Response, ResponseHeaders, ResponseError } from '@faasjs/browser';
4
- declare type FaasDataInjection<T = any> = {
2
+ import { Options, Response, ResponseError } from '@faasjs/browser';
3
+ import { FaasAction, FaasData, FaasParams } from '@faasjs/types';
4
+ export type { FaasBrowserClient, Options, Response, ResponseHeaders, ResponseError } from '@faasjs/browser';
5
+ declare type FaasDataInjection<Data = any> = {
5
6
  loading: boolean;
6
- data: T;
7
+ data: Data;
7
8
  error: any;
8
- promise: Promise<Response<T>>;
9
- reload(params?: Params): Promise<Response<T>>;
10
- setData: React.Dispatch<React.SetStateAction<T>>;
9
+ promise: Promise<Response<Data>>;
10
+ reload(params?: Record<string, any>): Promise<Response<Data>>;
11
+ setData: React.Dispatch<React.SetStateAction<Data>>;
11
12
  setLoading: React.Dispatch<React.SetStateAction<boolean>>;
12
- setPromise: React.Dispatch<React.SetStateAction<Promise<Response<T>>>>;
13
+ setPromise: React.Dispatch<React.SetStateAction<Promise<Response<Data>>>>;
13
14
  setError: React.Dispatch<React.SetStateAction<any>>;
14
15
  };
15
- declare type FaasDataProps<T = any> = {
16
- element(args: FaasDataInjection<T>): JSX.Element;
16
+ declare type FaasDataProps<PathOrData extends FaasAction> = {
17
+ element(args: FaasDataInjection<FaasData<PathOrData>>): JSX.Element;
17
18
  fallback?: JSX.Element | false;
18
19
  action: string;
19
- params?: Params;
20
- onDataChange?(args: FaasDataInjection<T>): void;
20
+ params?: FaasParams<PathOrData>;
21
+ onDataChange?(args: FaasDataInjection<FaasData<PathOrData>>): void;
21
22
  };
22
23
  export declare function FaasReactClient({ domain, options, onError }: {
23
24
  domain: string;
24
25
  options?: Options;
25
- onError?: (action: string, params: Params) => (res: ResponseError) => Promise<any>;
26
+ onError?: (action: string, params: Record<string, any>) => (res: ResponseError) => Promise<void>;
26
27
  }): {
27
- faas: <T = any>(action: string, params: Params) => Promise<Response<T>>;
28
- useFaas: <T_1 = any>(action: string, defaultParams: Params) => FaasDataInjection<T_1>;
29
- FaasData<T_2 = any>({ action, params, fallback, element, onDataChange }: FaasDataProps<T_2>): JSX.Element;
28
+ faas: <PathOrData extends Record<string, any>>(action: string | PathOrData, params: FaasParams<PathOrData>) => Promise<Response<FaasData<PathOrData>>>;
29
+ useFaas: <PathOrData_1 extends Record<string, any>>(action: string | PathOrData_1, defaultParams: FaasParams<PathOrData_1>) => FaasDataInjection<FaasData<PathOrData_1>>;
30
+ FaasData<PathOrData_2 extends Record<string, any>>({ action, params, fallback, element, onDataChange }: FaasDataProps<PathOrData_2>): JSX.Element;
30
31
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/react",
3
- "version": "0.0.2-beta.296",
3
+ "version": "0.0.2-beta.304",
4
4
  "license": "MIT",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/react/src/index.d.ts",
@@ -10,6 +10,10 @@
10
10
  "url": "git+https://github.com/faasjs/faasjs.git",
11
11
  "directory": "packages/react"
12
12
  },
13
+ "bugs": {
14
+ "url": "https://github.com/faasjs/faasjs/issues"
15
+ },
16
+ "funding": "https://github.com/sponsors/faasjs",
13
17
  "scripts": {
14
18
  "prepack": "rm -rf ./lib && rollup -c"
15
19
  },
@@ -27,5 +31,8 @@
27
31
  "rollup-plugin-typescript2": "*",
28
32
  "typescript": "*"
29
33
  },
30
- "gitHead": "6df200b3b73c28613ada04fd15b24955f85ea7e3"
34
+ "engines": {
35
+ "npm": ">=8.0.0"
36
+ },
37
+ "gitHead": "39d192f58071b851ec483dfca81fa4458307e3a2"
31
38
  }