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

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/lib/index.js CHANGED
@@ -1,129 +1,31 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react')) :
3
- typeof define === 'function' && define.amd ? define(['exports', 'react'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.FaasReactClient = {}, global.React));
5
- })(this, (function (exports, React) { 'use strict';
6
-
7
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
-
9
- var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
10
-
11
- class Response {
12
- constructor({ status, headers, data }) {
13
- this.status = status;
14
- this.headers = headers;
15
- this.data = data;
16
- }
17
- }
18
- class ResponseError extends Error {
19
- constructor({ message, status, headers, body }) {
20
- super(message);
21
- this.status = status;
22
- this.headers = headers;
23
- this.body = body;
24
- }
25
- }
26
- class FaasBrowserClient {
27
- /**
28
- * 创建 FaasJS 浏览器客户端
29
- * @param baseUrl {string} 网关地址
30
- * @param options {object} 默认配置项
31
- */
32
- constructor(baseUrl, options) {
33
- this.host = baseUrl[baseUrl.length - 1] === '/' ? baseUrl : baseUrl + '/';
34
- this.defaultOptions = options || Object.create(null);
35
- console.debug('[faas] baseUrl: ' + this.host);
36
- }
37
- /**
38
- * 发起请求
39
- * @param action {string} 动作名称
40
- * @param params {any} 动作参数
41
- * @param options {object} 默认配置项
42
- */
43
- async action(action, params, options = {}) {
44
- const url = this.host + action.toLowerCase() + '?_=' + new Date().getTime().toString();
45
- options = Object.assign(Object.assign({}, this.defaultOptions), options);
46
- const xhr = new XMLHttpRequest();
47
- xhr.open('POST', url);
48
- xhr.withCredentials = true;
49
- xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
50
- if (options.beforeRequest)
51
- await options.beforeRequest({
52
- action,
53
- params,
54
- xhr
55
- });
56
- return new Promise(function (resolve, reject) {
57
- xhr.onload = function () {
58
- var _a;
59
- let res = xhr.response;
60
- const headersList = xhr.getAllResponseHeaders().trim().split(/[\r\n]+/);
61
- const headers = {};
62
- headersList.forEach(function (line) {
63
- const parts = line.split(': ');
64
- const key = parts.shift();
65
- const value = parts.join(': ');
66
- if (key)
67
- headers[key] = value;
68
- });
69
- if (xhr.response && ((_a = xhr.getResponseHeader('Content-Type')) === null || _a === void 0 ? void 0 : _a.includes('json')))
70
- try {
71
- res = JSON.parse(xhr.response);
72
- if (res.error && res.error.message)
73
- reject(new ResponseError({
74
- message: res.error.message,
75
- status: xhr.status,
76
- headers,
77
- body: res
78
- }));
79
- }
80
- catch (error) {
81
- console.error(error);
82
- }
83
- if (xhr.status >= 200 && xhr.status < 300)
84
- resolve(new Response({
85
- status: xhr.status,
86
- headers,
87
- data: res.data
88
- }));
89
- else {
90
- console.error(xhr, res);
91
- reject(new ResponseError({
92
- message: xhr.statusText || xhr.status.toString(),
93
- status: xhr.status,
94
- headers,
95
- body: res
96
- }));
97
- }
98
- };
99
- xhr.onerror = function () {
100
- reject(new ResponseError({
101
- message: 'Network Error',
102
- status: xhr.status,
103
- headers: {},
104
- body: null
105
- }));
106
- };
107
- xhr.send(JSON.stringify(params));
108
- });
109
- }
110
- }
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@faasjs/browser'), require('react')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', '@faasjs/browser', 'react'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.FaasReactClient = {}, global.browser, global.React));
5
+ })(this, (function (exports, browser, react) { 'use strict';
111
6
 
112
7
  function FaasReactClient({ domain, options, onError }) {
113
- const client = new FaasBrowserClient(domain, options);
114
- const useFaas = function (action, defaultParams) {
115
- const [loading, setLoading] = React__default["default"].useState(true);
116
- const [data, setData] = React__default["default"].useState();
117
- const [error, setError] = React__default["default"].useState();
118
- const [promise, setPromise] = React__default["default"].useState();
119
- const [params, setParams] = React__default["default"].useState(defaultParams);
120
- const [reloadTimes, setReloadTimes] = React__default["default"].useState(0);
121
- React__default["default"].useEffect(function () {
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
+ async function faas(action, params) {
11
+ if (onError)
12
+ return client.action(action, params)
13
+ .catch(onError(action, params));
14
+ return client.action(action, params);
15
+ }
16
+ function useFaas(action, defaultParams) {
17
+ const [loading, setLoading] = react.useState(true);
18
+ const [data, setData] = react.useState();
19
+ const [error, setError] = react.useState();
20
+ const [promise, setPromise] = react.useState();
21
+ const [params, setParams] = react.useState(defaultParams);
22
+ const [reloadTimes, setReloadTimes] = react.useState(0);
23
+ react.useEffect(function () {
122
24
  if (JSON.stringify(defaultParams) !== JSON.stringify(params)) {
123
25
  setParams(defaultParams);
124
26
  }
125
27
  }, [JSON.stringify(defaultParams)]);
126
- React__default["default"].useEffect(function () {
28
+ react.useEffect(function () {
127
29
  setLoading(true);
128
30
  const request = client.action(action, params);
129
31
  setPromise(request);
@@ -166,22 +68,18 @@
166
68
  setPromise,
167
69
  setError,
168
70
  };
169
- };
71
+ }
170
72
  return {
171
- async faas(action, params) {
172
- if (onError)
173
- return client.action(action, params).catch(onError(action, params));
174
- return client.action(action, params);
175
- },
73
+ faas,
176
74
  useFaas,
177
75
  FaasData({ action, params, fallback, element, onDataChange }) {
178
76
  const request = useFaas(action, params);
179
- const [loaded, setLoaded] = React__default["default"].useState(false);
180
- React.useEffect(function () {
77
+ const [loaded, setLoaded] = react.useState(false);
78
+ react.useEffect(function () {
181
79
  if (!loaded && !request.loading)
182
80
  setLoaded(true);
183
81
  }, [request.loading]);
184
- React.useEffect(() => {
82
+ react.useEffect(() => {
185
83
  if (onDataChange)
186
84
  onDataChange(request);
187
85
  }, [JSON.stringify(request.data)]);
@@ -1,16 +1,16 @@
1
1
  /// <reference types="react" />
2
- import { Options, Params, Response, ResponseError } from '../../browser/src';
3
- export type { FaasBrowserClient, Options, Params, Response, ResponseHeaders, ResponseError } from '../../browser/src';
2
+ import { Options, Params, Response, ResponseError } from '@faasjs/browser';
3
+ export type { FaasBrowserClient, Options, Params, Response, ResponseHeaders, ResponseError } from '@faasjs/browser';
4
4
  declare type FaasDataInjection<T = any> = {
5
5
  loading: boolean;
6
6
  data: T;
7
7
  error: any;
8
8
  promise: Promise<Response<T>>;
9
9
  reload(params?: Params): Promise<Response<T>>;
10
- setData(data: T): void;
11
- setLoading(loading: boolean): void;
12
- setPromise(promise: Promise<Response<T>>): void;
13
- setError(error: any): void;
10
+ setData: React.Dispatch<React.SetStateAction<T>>;
11
+ setLoading: React.Dispatch<React.SetStateAction<boolean>>;
12
+ setPromise: React.Dispatch<React.SetStateAction<Promise<Response<T>>>>;
13
+ setError: React.Dispatch<React.SetStateAction<any>>;
14
14
  };
15
15
  declare type FaasDataProps<T = any> = {
16
16
  element(args: FaasDataInjection<T>): JSX.Element;
@@ -24,7 +24,7 @@ export declare function FaasReactClient({ domain, options, onError }: {
24
24
  options?: Options;
25
25
  onError?: (action: string, params: Params) => (res: ResponseError) => Promise<any>;
26
26
  }): {
27
- faas<T = any>(action: string, params: Params): Promise<Response<T>>;
27
+ faas: <T = any>(action: string, params: Params) => Promise<Response<T>>;
28
28
  useFaas: <T_1 = any>(action: string, defaultParams: Params) => FaasDataInjection<T_1>;
29
29
  FaasData<T_2 = any>({ action, params, fallback, element, onDataChange }: FaasDataProps<T_2>): JSX.Element;
30
30
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/react",
3
- "version": "0.0.2-beta.288",
3
+ "version": "0.0.2-beta.296",
4
4
  "license": "MIT",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/react/src/index.d.ts",
@@ -17,7 +17,7 @@
17
17
  "lib"
18
18
  ],
19
19
  "peerDependencies": {
20
- "@faasjs/browser": "*",
20
+ "@faasjs/browser": "^0.0.2-beta.280",
21
21
  "react": "*"
22
22
  },
23
23
  "devDependencies": {
@@ -27,5 +27,5 @@
27
27
  "rollup-plugin-typescript2": "*",
28
28
  "typescript": "*"
29
29
  },
30
- "gitHead": "5b04eb90f5c828ea2f4da11ad0eb6c76720207e6"
30
+ "gitHead": "6df200b3b73c28613ada04fd15b24955f85ea7e3"
31
31
  }
@@ -1,52 +0,0 @@
1
- export declare type Params = {
2
- [key: string]: any;
3
- };
4
- export declare type Options = {
5
- beforeRequest?: ({ action, params, xhr }: {
6
- action: string;
7
- params: Params;
8
- xhr: XMLHttpRequest;
9
- }) => Promise<void> | void;
10
- };
11
- export declare type ResponseHeaders = {
12
- [key: string]: string;
13
- };
14
- export declare class Response<T = any> {
15
- readonly status: number;
16
- readonly headers: ResponseHeaders;
17
- readonly data: T;
18
- constructor({ status, headers, data }: {
19
- status: number;
20
- headers: ResponseHeaders;
21
- data: T;
22
- });
23
- }
24
- export declare class ResponseError extends Error {
25
- readonly status: number;
26
- readonly headers: ResponseHeaders;
27
- readonly body: any;
28
- constructor({ message, status, headers, body }: {
29
- message: string;
30
- status: number;
31
- headers: ResponseHeaders;
32
- body: any;
33
- });
34
- }
35
- export declare class FaasBrowserClient {
36
- host: string;
37
- defaultOptions: Options;
38
- /**
39
- * 创建 FaasJS 浏览器客户端
40
- * @param baseUrl {string} 网关地址
41
- * @param options {object} 默认配置项
42
- */
43
- constructor(baseUrl: string, options?: Options);
44
- /**
45
- * 发起请求
46
- * @param action {string} 动作名称
47
- * @param params {any} 动作参数
48
- * @param options {object} 默认配置项
49
- */
50
- action<T = any>(action: string, params: Params, options?: Options): Promise<Response<T>>;
51
- }
52
- export default FaasBrowserClient;