@plasmicpkgs/plasmic-query 0.0.80 → 0.0.83

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/dist/Fetcher.d.ts CHANGED
@@ -1,25 +1,48 @@
1
1
  import registerComponent, { ComponentMeta } from "@plasmicapp/host/registerComponent";
2
2
  import { ReactNode } from "react";
3
- export interface FetchProps {
4
- url: string;
5
- method?: string;
6
- body?: any;
7
- headers?: any;
8
- }
9
- export interface DataFetcherProps extends FetchProps {
3
+ export interface GenericFetcherProps {
10
4
  children?: ReactNode;
11
- spinner?: ReactNode;
5
+ loadingDisplay?: ReactNode;
12
6
  previewSpinner?: boolean;
13
7
  errorDisplay?: ReactNode;
14
8
  previewErrorDisplay?: boolean;
15
- queryKey?: string;
16
9
  dataName?: string;
10
+ noLayout?: boolean;
11
+ className?: string;
12
+ }
13
+ declare type PropMetas<P> = ComponentMeta<P>["props"];
14
+ export declare const genericFetcherPropsMeta: PropMetas<GenericFetcherProps>;
15
+ export declare function GenericFetcherShell<T>({ result, children, loadingDisplay, previewSpinner, errorDisplay, previewErrorDisplay, dataName, noLayout, className, }: GenericFetcherProps & {
16
+ result: {
17
+ data?: T;
18
+ error?: Error;
19
+ isLoading?: boolean;
20
+ };
21
+ }): JSX.Element;
22
+ export interface FetchProps {
23
+ url?: string;
24
+ method?: string;
25
+ body?: string | {};
26
+ headers?: Record<string, string>;
17
27
  }
18
- export declare function DataFetcher({ queryKey, children, spinner, previewSpinner, errorDisplay, previewErrorDisplay, dataName, ...fetchProps }: DataFetcherProps): JSX.Element;
19
- export interface PlasmicQueryProviderProps {
20
- children: ReactNode;
28
+ export interface DataFetcherProps extends FetchProps, GenericFetcherProps {
29
+ queryKey?: string;
21
30
  }
31
+ export declare function DataFetcher(props: DataFetcherProps): JSX.Element;
22
32
  export declare const dataFetcherMeta: ComponentMeta<DataFetcherProps>;
23
33
  export declare function registerDataFetcher(loader?: {
24
34
  registerComponent: typeof registerComponent;
25
35
  }, customDataFetcherMeta?: ComponentMeta<DataFetcherProps>): void;
36
+ export interface GraphqlFetcherProps extends GenericFetcherProps, Omit<FetchProps, "body"> {
37
+ query?: string;
38
+ queryKey?: string;
39
+ }
40
+ export declare function GraphqlFetcher(props: GraphqlFetcherProps): JSX.Element;
41
+ export declare const graphqlFetcherMeta: ComponentMeta<GraphqlFetcherProps>;
42
+ export declare function registerGraphqlFetcher(loader?: {
43
+ registerComponent: typeof registerComponent;
44
+ }, customDataFetcherMeta?: ComponentMeta<GraphqlFetcherProps>): void;
45
+ export declare function registerAll(loader?: {
46
+ registerComponent: typeof registerComponent;
47
+ }): void;
48
+ export {};
@@ -45,6 +45,24 @@ function _asyncToGenerator(fn) {
45
45
  };
46
46
  }
47
47
 
48
+ function _extends() {
49
+ _extends = Object.assign || function (target) {
50
+ for (var i = 1; i < arguments.length; i++) {
51
+ var source = arguments[i];
52
+
53
+ for (var key in source) {
54
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
55
+ target[key] = source[key];
56
+ }
57
+ }
58
+ }
59
+
60
+ return target;
61
+ };
62
+
63
+ return _extends.apply(this, arguments);
64
+ }
65
+
48
66
  function _objectWithoutPropertiesLoose(source, excluded) {
49
67
  if (source == null) return {};
50
68
  var target = {};
@@ -789,119 +807,194 @@ var runtime_1 = /*#__PURE__*/createCommonjsModule(function (module) {
789
807
  }
790
808
  });
791
809
 
792
- var _excluded = ["queryKey", "children", "spinner", "previewSpinner", "errorDisplay", "previewErrorDisplay", "dataName"];
810
+ var _excluded = ["url", "query", "method", "headers", "queryKey"];
811
+ var genericFetcherPropsMeta = {
812
+ children: "slot",
813
+ loadingDisplay: {
814
+ type: "slot",
815
+ defaultValue: "Loading..."
816
+ },
817
+ errorDisplay: {
818
+ type: "slot",
819
+ defaultValue: "Error fetching data"
820
+ },
821
+ dataName: {
822
+ type: "string",
823
+ defaultValue: "myVariable",
824
+ description: "Variable name to store the fetched data in"
825
+ },
826
+ previewSpinner: {
827
+ type: "boolean",
828
+ description: "Force preview the loading state",
829
+ displayName: "Preview loading"
830
+ },
831
+ previewErrorDisplay: {
832
+ type: "boolean",
833
+ description: "Force preview the error display",
834
+ displayName: "Preview error"
835
+ },
836
+ noLayout: {
837
+ type: "boolean",
838
+ displayName: "No layout",
839
+ description: "When set, CMS Data Loader will not layout its children; instead, the layout set on its parent element will be used. Useful if you want to set flex gap or control container tag type.",
840
+ defaultValue: false
841
+ }
842
+ };
843
+ function GenericFetcherShell(_ref) {
844
+ var result = _ref.result,
845
+ children = _ref.children,
846
+ loadingDisplay = _ref.loadingDisplay,
847
+ previewSpinner = _ref.previewSpinner,
848
+ errorDisplay = _ref.errorDisplay,
849
+ previewErrorDisplay = _ref.previewErrorDisplay,
850
+ dataName = _ref.dataName,
851
+ noLayout = _ref.noLayout,
852
+ className = _ref.className;
853
+ var inEditor = !!host.usePlasmicCanvasContext();
854
+
855
+ if (inEditor && previewSpinner || !("error" in result) && !("data" in result)) {
856
+ return React.createElement(React.Fragment, null, loadingDisplay != null ? loadingDisplay : null);
857
+ } else if (inEditor && previewErrorDisplay || "error" in result) {
858
+ return React.createElement(React.Fragment, null, errorDisplay != null ? errorDisplay : null);
859
+ } else {
860
+ var content = React.createElement(host.DataProvider, {
861
+ name: dataName,
862
+ data: result.data
863
+ }, children);
864
+ return noLayout ? content : React.createElement("div", {
865
+ className: className
866
+ }, content);
867
+ }
868
+ }
869
+ /**
870
+ * Tries to return the JSON response, or else returns an object with a text key containing the response body text.
871
+ */
793
872
 
794
873
  function performFetch(_x) {
795
874
  return _performFetch.apply(this, arguments);
796
875
  }
797
876
 
798
877
  function _performFetch() {
799
- _performFetch = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(_ref) {
878
+ _performFetch = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(_ref2) {
800
879
  var url, method, body, headers, response, text;
801
880
  return runtime_1.wrap(function _callee$(_context) {
802
881
  while (1) {
803
882
  switch (_context.prev = _context.next) {
804
883
  case 0:
805
- url = _ref.url, method = _ref.method, body = _ref.body, headers = _ref.headers;
806
- _context.next = 3;
884
+ url = _ref2.url, method = _ref2.method, body = _ref2.body, headers = _ref2.headers;
885
+
886
+ if (url) {
887
+ _context.next = 3;
888
+ break;
889
+ }
890
+
891
+ throw new Error("Please specify a URL to fetch");
892
+
893
+ case 3:
894
+ _context.next = 5;
807
895
  return fetch(url, {
808
896
  method: method,
809
897
  headers: headers,
810
- body: body
898
+ body: body === undefined ? body : typeof body === "string" ? body : JSON.stringify(body)
811
899
  });
812
900
 
813
- case 3:
901
+ case 5:
814
902
  response = _context.sent;
815
903
 
816
904
  if (response.ok) {
817
- _context.next = 6;
905
+ _context.next = 8;
818
906
  break;
819
907
  }
820
908
 
821
909
  throw new Error(response.statusText);
822
910
 
823
- case 6:
824
- _context.next = 8;
911
+ case 8:
912
+ _context.next = 10;
825
913
  return response.text();
826
914
 
827
- case 8:
915
+ case 10:
828
916
  text = _context.sent;
829
- _context.prev = 9;
917
+ _context.prev = 11;
830
918
  return _context.abrupt("return", JSON.parse(text));
831
919
 
832
- case 13:
833
- _context.prev = 13;
834
- _context.t0 = _context["catch"](9);
920
+ case 15:
921
+ _context.prev = 15;
922
+ _context.t0 = _context["catch"](11);
835
923
  return _context.abrupt("return", {
836
924
  text: text
837
925
  });
838
926
 
839
- case 16:
927
+ case 18:
840
928
  case "end":
841
929
  return _context.stop();
842
930
  }
843
931
  }
844
- }, _callee, null, [[9, 13]]);
932
+ }, _callee, null, [[11, 15]]);
845
933
  }));
846
934
  return _performFetch.apply(this, arguments);
847
935
  }
848
936
 
849
- function DataFetcher(_ref2) {
850
- var queryKey = _ref2.queryKey,
851
- children = _ref2.children,
852
- spinner = _ref2.spinner,
853
- errorDisplay = _ref2.errorDisplay,
854
- dataName = _ref2.dataName,
855
- fetchProps = _objectWithoutPropertiesLoose(_ref2, _excluded);
856
-
857
- var query$1 = query.usePlasmicQueryData(queryKey != null ? queryKey : JSON.stringify(fetchProps), function () {
937
+ function DataFetcher(props) {
938
+ var url = props.url,
939
+ method = props.method,
940
+ body = props.body,
941
+ headers = props.headers,
942
+ queryKey = props.queryKey;
943
+ var fetchProps = {
944
+ url: url,
945
+ method: method,
946
+ body: body,
947
+ headers: headers
948
+ };
949
+ var result = query.usePlasmicQueryData(queryKey != null ? queryKey : JSON.stringify(_extends({
950
+ type: "DataFetcher"
951
+ }, fetchProps)), function () {
858
952
  return performFetch(fetchProps);
859
953
  });
860
-
861
- if (!("error" in query$1) && !("data" in query$1)) {
862
- return React.createElement(React.Fragment, null, spinner != null ? spinner : null);
863
- } else if ("error" in query$1) {
864
- return React.createElement(React.Fragment, null, errorDisplay != null ? errorDisplay : null);
865
- } else {
866
- return React.createElement(host.DataProvider, {
867
- name: dataName != null ? dataName : queryKey,
868
- data: query$1.data
869
- }, children);
870
- }
954
+ return React.createElement(GenericFetcherShell, Object.assign({
955
+ result: result
956
+ }, props));
871
957
  }
872
- var dataFetcherMeta = {
873
- name: "hostless-plasmic-query-data-fetcher",
874
- displayName: "Data Fetcher",
875
- importName: "DataFetcher",
876
- importPath: "@plasmicpkgs/react-query",
877
- providesData: true,
878
- props: {
958
+
959
+ function mkFetchProps(defaultUrl, defaultMethod) {
960
+ return {
879
961
  url: {
880
962
  type: "string",
881
- defaultValue: "https://api.github.com/users/plasmicapp/repos",
963
+ defaultValue: defaultUrl,
882
964
  description: "Where to fetch the data from"
883
965
  },
884
966
  method: {
885
967
  type: "choice",
886
968
  options: ["GET", "DELETE", "CONNECT", "HEAD", "OPTIONS", "POST", "PUT", "TRACE"],
887
- defaultValue: "GET",
888
- description: "Method to be used when fetching"
969
+ defaultValue: defaultMethod,
970
+ description: "Method to use when fetching"
889
971
  },
890
972
  headers: {
891
973
  type: "object",
892
- description: "JSON object of the headers to be sent with the request"
974
+ description: "JSON object of the headers to be sent with the request",
975
+ defaultValue: {
976
+ "Content-type": "application/json"
977
+ }
893
978
  },
979
+ queryKey: {
980
+ type: "string",
981
+ description: "A globally unique ID for this query, used for invalidating queries"
982
+ }
983
+ };
984
+ }
985
+
986
+ var dataFetcherMeta = {
987
+ name: "hostless-plasmic-query-data-fetcher",
988
+ displayName: "Data Fetcher",
989
+ importName: "DataFetcher",
990
+ importPath: "@plasmicpkgs/plasmic-query",
991
+ providesData: true,
992
+ props: /*#__PURE__*/_extends({}, /*#__PURE__*/mkFetchProps("https://api.github.com/users/plasmicapp/repos", "GET"), {
894
993
  body: {
895
994
  type: "object",
896
995
  description: "JSON object of the body to be sent with the request"
897
- },
898
- dataName: {
899
- type: "string",
900
- defaultValue: "myVariable",
901
- description: "Variable name to store the fetched data in"
902
- },
903
- children: "slot"
904
- },
996
+ }
997
+ }, genericFetcherPropsMeta),
905
998
  defaultStyles: {
906
999
  maxWidth: "100%"
907
1000
  }
@@ -913,8 +1006,88 @@ function registerDataFetcher(loader, customDataFetcherMeta) {
913
1006
  registerComponent(DataFetcher, customDataFetcherMeta != null ? customDataFetcherMeta : dataFetcherMeta);
914
1007
  }
915
1008
  }
1009
+ function GraphqlFetcher(props) {
1010
+ var query$1 = props.query,
1011
+ url = props.url,
1012
+ method = props.method,
1013
+ headers = props.headers,
1014
+ queryKey = props.queryKey;
1015
+ var fetchProps = {
1016
+ body: query$1,
1017
+ url: url,
1018
+ method: method,
1019
+ headers: headers
1020
+ };
1021
+ var result = query.usePlasmicQueryData(queryKey != null ? queryKey : JSON.stringify(_extends({
1022
+ type: "GraphqlFetcher"
1023
+ }, fetchProps)), function () {
1024
+ return performFetch(fetchProps);
1025
+ });
1026
+ return React.createElement(GenericFetcherShell, Object.assign({
1027
+ result: result
1028
+ }, props));
1029
+ }
1030
+ var graphqlFetcherMeta = {
1031
+ name: "hostless-plasmic-query-graphql-fetcher",
1032
+ displayName: "GraphQL Fetcher",
1033
+ importName: "GraphqlFetcher",
1034
+ importPath: "@plasmicpkgs/plasmic-query",
1035
+ providesData: true,
1036
+ props: /*#__PURE__*/function () {
1037
+ var gqlMetas = {
1038
+ query: {
1039
+ type: "code",
1040
+ lang: "graphql",
1041
+ endpoint: function endpoint(props) {
1042
+ var _props$url;
1043
+
1044
+ return (_props$url = props.url) != null ? _props$url : "";
1045
+ },
1046
+ defaultValue: /*#__PURE__*/JSON.stringify({
1047
+ query: "{\n allPokemon {\n name\n sprites {\n front_default\n }\n }\n}"
1048
+ })
1049
+ }
1050
+ }; // Reorder the props
1051
+
1052
+ var _mkFetchProps$gqlMeta = /*#__PURE__*/_extends({}, /*#__PURE__*/mkFetchProps("https://dex-server.herokuapp.com/", "POST"), gqlMetas, genericFetcherPropsMeta),
1053
+ url = _mkFetchProps$gqlMeta.url,
1054
+ query = _mkFetchProps$gqlMeta.query,
1055
+ method = _mkFetchProps$gqlMeta.method,
1056
+ headers = _mkFetchProps$gqlMeta.headers,
1057
+ queryKey = _mkFetchProps$gqlMeta.queryKey,
1058
+ rest = /*#__PURE__*/_objectWithoutPropertiesLoose(_mkFetchProps$gqlMeta, _excluded);
1059
+
1060
+ return _extends({
1061
+ url: url,
1062
+ query: query,
1063
+ method: method,
1064
+ headers: headers,
1065
+ queryKey: queryKey
1066
+ }, rest);
1067
+ }(),
1068
+ defaultStyles: {
1069
+ maxWidth: "100%"
1070
+ }
1071
+ };
1072
+ function registerGraphqlFetcher(loader, customDataFetcherMeta) {
1073
+ if (loader) {
1074
+ loader.registerComponent(GraphqlFetcher, customDataFetcherMeta != null ? customDataFetcherMeta : graphqlFetcherMeta);
1075
+ } else {
1076
+ registerComponent(GraphqlFetcher, customDataFetcherMeta != null ? customDataFetcherMeta : graphqlFetcherMeta);
1077
+ }
1078
+ }
1079
+ function registerAll(loader) {
1080
+ registerDataFetcher(loader);
1081
+ registerGraphqlFetcher(loader);
1082
+ }
916
1083
 
917
1084
  exports.DataFetcher = DataFetcher;
1085
+ exports.GenericFetcherShell = GenericFetcherShell;
1086
+ exports.GraphqlFetcher = GraphqlFetcher;
918
1087
  exports.dataFetcherMeta = dataFetcherMeta;
1088
+ exports.genericFetcherPropsMeta = genericFetcherPropsMeta;
1089
+ exports.graphqlFetcherMeta = graphqlFetcherMeta;
1090
+ exports.registerAll = registerAll;
919
1091
  exports.registerDataFetcher = registerDataFetcher;
1092
+ exports.registerGraphqlFetcher = registerGraphqlFetcher;
920
1093
  //# sourceMappingURL=plasmic-query.cjs.development.js.map