@faasjs/react 0.0.3-beta.85 → 0.0.3-beta.87

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.
@@ -0,0 +1,122 @@
1
+ import { Response, Options, ResponseError } from '@faasjs/browser';
2
+ export { FaasBrowserClient, Options, Response, ResponseError, ResponseHeaders } from '@faasjs/browser';
3
+ import { FaasAction, FaasData, FaasParams } from '@faasjs/types';
4
+ export { FaasAction, FaasData, FaasParams } from '@faasjs/types';
5
+
6
+ /**
7
+ * Injects FaasData props.
8
+ */
9
+ interface FaasDataInjection<Data = any> {
10
+ action: string | any;
11
+ params: Record<string, any>;
12
+ loading: boolean;
13
+ data: Data;
14
+ error: any;
15
+ promise: Promise<Response<Data>>;
16
+ reload(params?: Record<string, any>): Promise<Response<Data>>;
17
+ setData: React.Dispatch<React.SetStateAction<Data>>;
18
+ setLoading: React.Dispatch<React.SetStateAction<boolean>>;
19
+ setPromise: React.Dispatch<React.SetStateAction<Promise<Response<Data>>>>;
20
+ setError: React.Dispatch<React.SetStateAction<any>>;
21
+ }
22
+ interface FaasDataWrapperProps<PathOrData extends FaasAction> {
23
+ render?(args: FaasDataInjection<FaasData<PathOrData>>): JSX.Element | JSX.Element[];
24
+ children?: React.ReactElement<Partial<FaasDataInjection>>;
25
+ fallback?: JSX.Element | false;
26
+ action: string;
27
+ params?: FaasParams<PathOrData>;
28
+ onDataChange?(args: FaasDataInjection<FaasData<PathOrData>>): void;
29
+ /** use custom data, should work with setData */
30
+ data?: FaasData<PathOrData>;
31
+ /** use custom setData, should work with data */
32
+ setData?: React.Dispatch<React.SetStateAction<FaasData<PathOrData>>>;
33
+ }
34
+ interface FaasReactClientInstance {
35
+ faas: <PathOrData extends FaasAction>(action: string | PathOrData, params: FaasParams<PathOrData>) => Promise<Response<FaasData<PathOrData>>>;
36
+ useFaas: <PathOrData extends FaasAction>(action: string | PathOrData, defaultParams: FaasParams<PathOrData>, options?: {
37
+ data?: FaasData<PathOrData>;
38
+ setData?: React.Dispatch<React.SetStateAction<FaasData<PathOrData>>>;
39
+ skip?: boolean;
40
+ }) => FaasDataInjection<FaasData<PathOrData>>;
41
+ FaasDataWrapper<PathOrData extends FaasAction>(props: FaasDataWrapperProps<PathOrData>): JSX.Element;
42
+ }
43
+ /**
44
+ * Before use faas, you should initialize a FaasReactClient.
45
+ *
46
+ * @param props.domain {string} The domain of your faas server
47
+ * @param props.options {Options} The options of client
48
+ * @returns {FaasReactClientInstance}
49
+ *
50
+ * ```ts
51
+ * const client = FaasReactClient({
52
+ * domain: 'localhost:8080/api'
53
+ * })
54
+ * ```
55
+ */
56
+ declare function FaasReactClient({ domain, options, onError }: {
57
+ domain: string;
58
+ options?: Options;
59
+ onError?: (action: string, params: Record<string, any>) => (res: ResponseError) => Promise<void>;
60
+ }): FaasReactClientInstance;
61
+ /**
62
+ * Get FaasReactClient instance
63
+ * @param domain {string} empty string for default domain
64
+ * @returns {FaasReactClientInstance}
65
+ *
66
+ * ```ts
67
+ * getClient()
68
+ * // or
69
+ * getClient('another-domain')
70
+ * ```
71
+ */
72
+ declare function getClient(domain?: string): FaasReactClientInstance;
73
+ /**
74
+ * Request faas server
75
+ *
76
+ * @param action {string} action name
77
+ * @param params {object} action params
78
+ * @returns {Promise<Response<any>>}
79
+ *
80
+ * ```ts
81
+ * faas<{ title: string }>('post/get', { id: 1 }).then(res => {
82
+ * console.log(res.data.title)
83
+ * })
84
+ * ```
85
+ */
86
+ declare function faas<PathOrData extends FaasAction>(action: string | PathOrData, params: FaasParams<PathOrData>): Promise<Response<FaasData<PathOrData>>>;
87
+ /**
88
+ * Request faas server with React hook
89
+ * @param action {string} action name
90
+ * @param defaultParams {object} initial action params
91
+ * @returns {FaasDataInjection<any>}
92
+ *
93
+ * ```ts
94
+ * function Post ({ id }) {
95
+ * const { data } = useFaas<{ title: string }>('post/get', { id })
96
+ * return <h1>{data.title}</h1>
97
+ * }
98
+ * ```
99
+ */
100
+ declare function useFaas<PathOrData extends FaasAction>(action: string | PathOrData, defaultParams: FaasParams<PathOrData>, options?: {
101
+ data?: FaasData<PathOrData>;
102
+ setData?: React.Dispatch<React.SetStateAction<FaasData<PathOrData>>>;
103
+ skip?: boolean;
104
+ }): FaasDataInjection<FaasData<PathOrData>>;
105
+ /**
106
+ * A data wrapper for react components
107
+ * @returns {JSX.Element}
108
+ *
109
+ * ```ts
110
+ * <FaasDataWrapper<{
111
+ * id: string
112
+ * title: string
113
+ * }>
114
+ * action='post/get'
115
+ * params={ { id: 1 } }
116
+ * render={ ({ data }) => <h1>{ data.title }</h1> }
117
+ * />
118
+ * ```
119
+ */
120
+ declare function FaasDataWrapper<PathOrData extends FaasAction>(props: FaasDataWrapperProps<PathOrData>): JSX.Element;
121
+
122
+ export { FaasDataInjection, FaasDataWrapper, FaasDataWrapperProps, FaasReactClient, FaasReactClientInstance, faas, getClient, useFaas };
package/dist/index.d.ts CHANGED
@@ -1,12 +1,7 @@
1
1
  import { Response, Options, ResponseError } from '@faasjs/browser';
2
2
  export { FaasBrowserClient, Options, Response, ResponseError, ResponseHeaders } from '@faasjs/browser';
3
-
4
- interface FaasActions {
5
- }
6
- type FaasActionPaths = keyof FaasActions;
7
- type FaasAction = FaasActionPaths | Record<string, any>;
8
- type FaasParams<T = any> = T extends FaasActionPaths ? FaasActions[T]['Params'] : any;
9
- type FaasData<T = any> = T extends FaasActionPaths ? FaasActions[T]['Data'] : T;
3
+ import { FaasAction, FaasData, FaasParams } from '@faasjs/types';
4
+ export { FaasAction, FaasData, FaasParams } from '@faasjs/types';
10
5
 
11
6
  /**
12
7
  * Injects FaasData props.
@@ -124,4 +119,4 @@ declare function useFaas<PathOrData extends FaasAction>(action: string | PathOrD
124
119
  */
125
120
  declare function FaasDataWrapper<PathOrData extends FaasAction>(props: FaasDataWrapperProps<PathOrData>): JSX.Element;
126
121
 
127
- export { FaasAction, FaasData, FaasDataInjection, FaasDataWrapper, FaasDataWrapperProps, FaasParams, FaasReactClient, FaasReactClientInstance, faas, getClient, useFaas };
122
+ export { FaasDataInjection, FaasDataWrapper, FaasDataWrapperProps, FaasReactClient, FaasReactClientInstance, faas, getClient, useFaas };
package/dist/index.js CHANGED
@@ -1,42 +1,16 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
1
+ 'use strict';
2
+
3
+ var browser = require('@faasjs/browser');
4
+ var react = require('react');
19
5
 
20
6
  // src/index.tsx
21
- var src_exports = {};
22
- __export(src_exports, {
23
- FaasBrowserClient: () => import_browser.FaasBrowserClient,
24
- FaasDataWrapper: () => FaasDataWrapper,
25
- FaasReactClient: () => FaasReactClient,
26
- faas: () => faas,
27
- getClient: () => getClient,
28
- useFaas: () => useFaas
29
- });
30
- module.exports = __toCommonJS(src_exports);
31
- var import_browser = require("@faasjs/browser");
32
- var import_react = require("react");
33
7
  var clients = {};
34
8
  function FaasReactClient({
35
9
  domain,
36
10
  options,
37
11
  onError
38
12
  }) {
39
- const client = new import_browser.FaasBrowserClient(domain, options);
13
+ const client = new browser.FaasBrowserClient(domain, options);
40
14
  async function faas2(action, params) {
41
15
  if (onError)
42
16
  return client.action(action, params).catch(async (res) => {
@@ -48,19 +22,19 @@ function FaasReactClient({
48
22
  function useFaas2(action, defaultParams, options2) {
49
23
  if (!options2)
50
24
  options2 = {};
51
- const [loading, setLoading] = (0, import_react.useState)(true);
52
- const [data, setData] = (0, import_react.useState)();
53
- const [error, setError] = (0, import_react.useState)();
54
- const [promise, setPromise] = (0, import_react.useState)();
55
- const [params, setParams] = (0, import_react.useState)(defaultParams);
56
- const [reloadTimes, setReloadTimes] = (0, import_react.useState)(0);
57
- const [fails, setFails] = (0, import_react.useState)(0);
58
- (0, import_react.useEffect)(function() {
25
+ const [loading, setLoading] = react.useState(true);
26
+ const [data, setData] = react.useState();
27
+ const [error, setError] = react.useState();
28
+ const [promise, setPromise] = react.useState();
29
+ const [params, setParams] = react.useState(defaultParams);
30
+ const [reloadTimes, setReloadTimes] = react.useState(0);
31
+ const [fails, setFails] = react.useState(0);
32
+ react.useEffect(function() {
59
33
  if (JSON.stringify(defaultParams) !== JSON.stringify(params)) {
60
34
  setParams(defaultParams);
61
35
  }
62
36
  }, [JSON.stringify(defaultParams)]);
63
- (0, import_react.useEffect)(function() {
37
+ react.useEffect(function() {
64
38
  if (!action || (options2 == null ? void 0 : options2.skip)) {
65
39
  setLoading(false);
66
40
  return;
@@ -71,7 +45,7 @@ function FaasReactClient({
71
45
  const request = client.action(action, options2.params || params, { signal: controller.signal });
72
46
  setPromise(request);
73
47
  request.then((r) => (options2 == null ? void 0 : options2.setData) ? options2.setData(r.data) : setData(r.data)).catch(async (e) => {
74
- if ((e == null ? void 0 : e.message) === "The user aborted a request.")
48
+ if ((e == null ? void 0 : e.message) === "The user aborted a request." || (e == null ? void 0 : e.message) === "Aborted")
75
49
  return;
76
50
  if (!fails && typeof (e == null ? void 0 : e.message) === "string" && e.message.indexOf("Failed to fetch") >= 0) {
77
51
  console.warn(`FaasReactClient: ${e.message} retry...`);
@@ -136,18 +110,18 @@ function FaasReactClient({
136
110
  data,
137
111
  setData
138
112
  });
139
- const [loaded, setLoaded] = (0, import_react.useState)(false);
140
- (0, import_react.useEffect)(function() {
113
+ const [loaded, setLoaded] = react.useState(false);
114
+ react.useEffect(function() {
141
115
  if (!loaded && !request.loading)
142
116
  setLoaded(true);
143
117
  }, [request.loading]);
144
- (0, import_react.useEffect)(function() {
118
+ react.useEffect(function() {
145
119
  if (onDataChange)
146
120
  onDataChange(request);
147
121
  }, [JSON.stringify(request.data)]);
148
122
  if (loaded) {
149
123
  if (children)
150
- return (0, import_react.cloneElement)(children, request);
124
+ return react.cloneElement(children, request);
151
125
  if (render)
152
126
  return render(request);
153
127
  }
@@ -170,22 +144,23 @@ function useFaas(action, defaultParams, options) {
170
144
  return getClient().useFaas(action, defaultParams, options);
171
145
  }
172
146
  function FaasDataWrapper(props) {
173
- const [client, setClient] = (0, import_react.useState)();
174
- (0, import_react.useEffect)(() => {
147
+ const [client, setClient] = react.useState();
148
+ react.useEffect(() => {
175
149
  if (client)
176
150
  return;
177
151
  setClient(getClient());
178
152
  }, []);
179
153
  if (!client)
180
154
  return props.fallback || null;
181
- return (0, import_react.createElement)(client.FaasDataWrapper, props);
155
+ return react.createElement(client.FaasDataWrapper, props);
182
156
  }
183
- // Annotate the CommonJS export names for ESM import in node:
184
- 0 && (module.exports = {
185
- FaasBrowserClient,
186
- FaasDataWrapper,
187
- FaasReactClient,
188
- faas,
189
- getClient,
190
- useFaas
157
+
158
+ Object.defineProperty(exports, 'FaasBrowserClient', {
159
+ enumerable: true,
160
+ get: function () { return browser.FaasBrowserClient; }
191
161
  });
162
+ exports.FaasDataWrapper = FaasDataWrapper;
163
+ exports.FaasReactClient = FaasReactClient;
164
+ exports.faas = faas;
165
+ exports.getClient = getClient;
166
+ exports.useFaas = useFaas;
package/dist/index.mjs CHANGED
@@ -1,13 +1,8 @@
1
+ import { FaasBrowserClient } from '@faasjs/browser';
2
+ export { FaasBrowserClient } from '@faasjs/browser';
3
+ import { useState, useEffect, createElement, cloneElement } from 'react';
4
+
1
5
  // src/index.tsx
2
- import {
3
- FaasBrowserClient
4
- } from "@faasjs/browser";
5
- import {
6
- useState,
7
- useEffect,
8
- createElement,
9
- cloneElement
10
- } from "react";
11
6
  var clients = {};
12
7
  function FaasReactClient({
13
8
  domain,
@@ -49,7 +44,7 @@ function FaasReactClient({
49
44
  const request = client.action(action, options2.params || params, { signal: controller.signal });
50
45
  setPromise(request);
51
46
  request.then((r) => (options2 == null ? void 0 : options2.setData) ? options2.setData(r.data) : setData(r.data)).catch(async (e) => {
52
- if ((e == null ? void 0 : e.message) === "The user aborted a request.")
47
+ if ((e == null ? void 0 : e.message) === "The user aborted a request." || (e == null ? void 0 : e.message) === "Aborted")
53
48
  return;
54
49
  if (!fails && typeof (e == null ? void 0 : e.message) === "string" && e.message.indexOf("Failed to fetch") >= 0) {
55
50
  console.warn(`FaasReactClient: ${e.message} retry...`);
@@ -158,11 +153,5 @@ function FaasDataWrapper(props) {
158
153
  return props.fallback || null;
159
154
  return createElement(client.FaasDataWrapper, props);
160
155
  }
161
- export {
162
- FaasBrowserClient,
163
- FaasDataWrapper,
164
- FaasReactClient,
165
- faas,
166
- getClient,
167
- useFaas
168
- };
156
+
157
+ export { FaasDataWrapper, FaasReactClient, faas, getClient, useFaas };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/react",
3
- "version": "0.0.3-beta.85",
3
+ "version": "0.0.3-beta.87",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -16,13 +16,13 @@
16
16
  },
17
17
  "funding": "https://github.com/sponsors/faasjs",
18
18
  "scripts": {
19
- "build": "tsup src/index.tsx --format esm,cjs --dts --clean"
19
+ "build": "tsup src/index.tsx --config ../../tsup.config.json"
20
20
  },
21
21
  "files": [
22
22
  "dist"
23
23
  ],
24
24
  "dependencies": {
25
- "@faasjs/browser": "^0.0.3-beta.85"
25
+ "@faasjs/browser": "^0.0.3-beta.87"
26
26
  },
27
27
  "peerDependencies": {
28
28
  "react": "*"