@plasmicpkgs/plasmic-cms 0.0.7 → 0.0.10

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/api.d.ts CHANGED
@@ -12,6 +12,10 @@ export interface QueryParams {
12
12
  desc: boolean;
13
13
  limit: number;
14
14
  }
15
+ export declare class HttpError extends Error {
16
+ status: number;
17
+ constructor(status: number, message: string);
18
+ }
15
19
  declare class API {
16
20
  private config;
17
21
  constructor(config: DatabaseConfig);
@@ -3,6 +3,9 @@ import { CanvasComponentProps } from "@plasmicapp/host/registerComponent";
3
3
  import React from "react";
4
4
  import { DatabaseConfig, QueryParams } from "./api";
5
5
  import { ApiCmsTable } from "./schema";
6
+ interface FetcherComponentProps {
7
+ hideIfNotFound?: boolean;
8
+ }
6
9
  interface CmsDataProviderProps extends DatabaseConfig {
7
10
  children?: React.ReactNode;
8
11
  }
@@ -16,7 +19,7 @@ interface CmsQueryLoaderProps extends QueryParams, CanvasComponentProps<TablesCo
16
19
  table?: string;
17
20
  }
18
21
  export declare const cmsQueryLoaderMeta: ComponentMeta<CmsQueryLoaderProps>;
19
- export declare function CmsQueryLoader({ table, children, setControlContextData, ...params }: CmsQueryLoaderProps): JSX.Element;
22
+ export declare function CmsQueryLoader({ table, children, setControlContextData, ...params }: CmsQueryLoaderProps): React.ReactElement<any, string | ((props: any) => React.ReactElement<any, any> | null) | (new (props: any) => React.Component<any, any, any>)> | null;
20
23
  interface CmsRowRepeaterProps extends CanvasComponentProps<TablesContextData> {
21
24
  children?: React.ReactNode;
22
25
  table?: string;
@@ -42,12 +45,12 @@ interface CmsRowLinkProps extends CanvasComponentProps<TablesContextData & {
42
45
  }
43
46
  export declare const cmsRowLinkMeta: ComponentMeta<CmsRowLinkProps>;
44
47
  export declare function CmsRowLink({ table, field, hrefProp, children, setControlContextData, }: CmsRowLinkProps): JSX.Element;
45
- interface CmsRowLoaderProps extends CanvasComponentProps<TablesContextData> {
48
+ interface CmsRowLoaderProps extends CanvasComponentProps<TablesContextData>, FetcherComponentProps {
46
49
  table: string;
47
50
  row: string;
48
51
  children: React.ReactNode;
49
52
  useDraft: boolean;
50
53
  }
51
54
  export declare const cmsRowLoaderMeta: ComponentMeta<CmsRowLoaderProps>;
52
- export declare function CmsRowLoader({ table, row, children, useDraft, setControlContextData, }: CmsRowLoaderProps): JSX.Element;
55
+ export declare function CmsRowLoader({ table, row, children, useDraft, hideIfNotFound, setControlContextData, }: CmsRowLoaderProps): React.ReactElement<any, string | ((props: any) => React.ReactElement<any, any> | null) | (new (props: any) => React.Component<any, any, any>)> | null;
53
56
  export {};
@@ -45,6 +45,115 @@ 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
+
66
+ function _inheritsLoose(subClass, superClass) {
67
+ subClass.prototype = Object.create(superClass.prototype);
68
+ subClass.prototype.constructor = subClass;
69
+
70
+ _setPrototypeOf(subClass, superClass);
71
+ }
72
+
73
+ function _getPrototypeOf(o) {
74
+ _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
75
+ return o.__proto__ || Object.getPrototypeOf(o);
76
+ };
77
+ return _getPrototypeOf(o);
78
+ }
79
+
80
+ function _setPrototypeOf(o, p) {
81
+ _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
82
+ o.__proto__ = p;
83
+ return o;
84
+ };
85
+
86
+ return _setPrototypeOf(o, p);
87
+ }
88
+
89
+ function _isNativeReflectConstruct() {
90
+ if (typeof Reflect === "undefined" || !Reflect.construct) return false;
91
+ if (Reflect.construct.sham) return false;
92
+ if (typeof Proxy === "function") return true;
93
+
94
+ try {
95
+ Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
96
+ return true;
97
+ } catch (e) {
98
+ return false;
99
+ }
100
+ }
101
+
102
+ function _construct(Parent, args, Class) {
103
+ if (_isNativeReflectConstruct()) {
104
+ _construct = Reflect.construct;
105
+ } else {
106
+ _construct = function _construct(Parent, args, Class) {
107
+ var a = [null];
108
+ a.push.apply(a, args);
109
+ var Constructor = Function.bind.apply(Parent, a);
110
+ var instance = new Constructor();
111
+ if (Class) _setPrototypeOf(instance, Class.prototype);
112
+ return instance;
113
+ };
114
+ }
115
+
116
+ return _construct.apply(null, arguments);
117
+ }
118
+
119
+ function _isNativeFunction(fn) {
120
+ return Function.toString.call(fn).indexOf("[native code]") !== -1;
121
+ }
122
+
123
+ function _wrapNativeSuper(Class) {
124
+ var _cache = typeof Map === "function" ? new Map() : undefined;
125
+
126
+ _wrapNativeSuper = function _wrapNativeSuper(Class) {
127
+ if (Class === null || !_isNativeFunction(Class)) return Class;
128
+
129
+ if (typeof Class !== "function") {
130
+ throw new TypeError("Super expression must either be null or a function");
131
+ }
132
+
133
+ if (typeof _cache !== "undefined") {
134
+ if (_cache.has(Class)) return _cache.get(Class);
135
+
136
+ _cache.set(Class, Wrapper);
137
+ }
138
+
139
+ function Wrapper() {
140
+ return _construct(Class, arguments, _getPrototypeOf(this).constructor);
141
+ }
142
+
143
+ Wrapper.prototype = Object.create(Class.prototype, {
144
+ constructor: {
145
+ value: Wrapper,
146
+ enumerable: false,
147
+ writable: true,
148
+ configurable: true
149
+ }
150
+ });
151
+ return _setPrototypeOf(Wrapper, Class);
152
+ };
153
+
154
+ return _wrapNativeSuper(Class);
155
+ }
156
+
48
157
  function _objectWithoutPropertiesLoose(source, excluded) {
49
158
  if (source == null) return {};
50
159
  var target = {};
@@ -869,6 +978,20 @@ function queryParamsToApi(params) {
869
978
  };
870
979
  }
871
980
 
981
+ var HttpError = /*#__PURE__*/function (_Error) {
982
+ _inheritsLoose(HttpError, _Error);
983
+
984
+ function HttpError(status, message) {
985
+ var _this;
986
+
987
+ _this = _Error.call(this, message) || this;
988
+ _this.status = status;
989
+ return _this;
990
+ }
991
+
992
+ return HttpError;
993
+ }( /*#__PURE__*/_wrapNativeSuper(Error));
994
+
872
995
  var API = /*#__PURE__*/function () {
873
996
  function API(config) {
874
997
  this.config = config;
@@ -878,7 +1001,8 @@ var API = /*#__PURE__*/function () {
878
1001
 
879
1002
  _proto.get = /*#__PURE__*/function () {
880
1003
  var _get = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(endpoint, params) {
881
- var url, response;
1004
+ var url, response, message, _yield$response$json, _yield$response$json$;
1005
+
882
1006
  return runtime_1.wrap(function _callee$(_context) {
883
1007
  while (1) {
884
1008
  switch (_context.prev = _context.next) {
@@ -901,18 +1025,61 @@ var API = /*#__PURE__*/function () {
901
1025
 
902
1026
  case 5:
903
1027
  response = _context.sent;
904
- _context.next = 8;
1028
+
1029
+ if (!(response.status !== 200)) {
1030
+ _context.next = 25;
1031
+ break;
1032
+ }
1033
+
1034
+ _context.prev = 7;
1035
+ _context.next = 10;
905
1036
  return response.json();
906
1037
 
907
- case 8:
1038
+ case 10:
1039
+ _context.t0 = _yield$response$json = _context.sent;
1040
+
1041
+ if (!(_context.t0 == null)) {
1042
+ _context.next = 15;
1043
+ break;
1044
+ }
1045
+
1046
+ _context.t1 = void 0;
1047
+ _context.next = 16;
1048
+ break;
1049
+
1050
+ case 15:
1051
+ _context.t1 = (_yield$response$json$ = _yield$response$json.error) == null ? void 0 : _yield$response$json$.message;
1052
+
1053
+ case 16:
1054
+ message = _context.t1;
1055
+ _context.next = 24;
1056
+ break;
1057
+
1058
+ case 19:
1059
+ _context.prev = 19;
1060
+ _context.t2 = _context["catch"](7);
1061
+ _context.next = 23;
1062
+ return response.text();
1063
+
1064
+ case 23:
1065
+ message = _context.sent;
1066
+
1067
+ case 24:
1068
+ throw new HttpError(response.status, message);
1069
+
1070
+ case 25:
1071
+ _context.next = 27;
1072
+ return response.json();
1073
+
1074
+ case 27:
908
1075
  return _context.abrupt("return", _context.sent);
909
1076
 
910
- case 9:
1077
+ case 28:
911
1078
  case "end":
912
1079
  return _context.stop();
913
1080
  }
914
1081
  }
915
- }, _callee, this);
1082
+ }, _callee, this, [[7, 19]]);
916
1083
  }));
917
1084
 
918
1085
  function get(_x, _x2) {
@@ -941,7 +1108,7 @@ var API = /*#__PURE__*/function () {
941
1108
  _context2.prev = 7;
942
1109
  _context2.t0 = _context2["catch"](0);
943
1110
  console.error(_context2.t0);
944
- throw new Error("Cannot fetch CMS models.");
1111
+ throw _context2.t0;
945
1112
 
946
1113
  case 11:
947
1114
  case "end":
@@ -980,7 +1147,7 @@ var API = /*#__PURE__*/function () {
980
1147
  _context3.prev = 7;
981
1148
  _context3.t0 = _context3["catch"](0);
982
1149
  console.error(_context3.t0);
983
- throw new Error("Query returned invalid response.");
1150
+ throw _context3.t0;
984
1151
 
985
1152
  case 11:
986
1153
  case "end":
@@ -1005,7 +1172,7 @@ var API = /*#__PURE__*/function () {
1005
1172
  switch (_context4.prev = _context4.next) {
1006
1173
  case 0:
1007
1174
  _context4.prev = 0;
1008
- maybeUseDraft = useDraft ? "?useDraft=1" : "";
1175
+ maybeUseDraft = useDraft ? "?draft=1" : "";
1009
1176
  _context4.next = 4;
1010
1177
  return this.get("/tables/" + table + "/rows/" + row + maybeUseDraft);
1011
1178
 
@@ -1017,7 +1184,7 @@ var API = /*#__PURE__*/function () {
1017
1184
  _context4.prev = 8;
1018
1185
  _context4.t0 = _context4["catch"](0);
1019
1186
  console.error(_context4.t0);
1020
- throw new Error("Query returned invalid response.");
1187
+ throw _context4.t0;
1021
1188
 
1022
1189
  case 12:
1023
1190
  case "end":
@@ -1239,12 +1406,27 @@ var _excluded = ["children"],
1239
1406
  _excluded2 = ["table", "children", "setControlContextData"];
1240
1407
  var modulePath = "@plasmicpkgs/plasmic-cms";
1241
1408
  var componentPrefix = "hostless-plasmic-cms";
1409
+ var fetcherComponentPropMetas = {
1410
+ hideIfNotFound: {
1411
+ type: "boolean",
1412
+ defaultValue: false,
1413
+ description: "Whether to show an error if no result is found"
1414
+ }
1415
+ };
1242
1416
 
1243
- function renderMaybeData(maybeData, renderFn) {
1417
+ function renderMaybeData(maybeData, renderFn, loaderProps) {
1244
1418
  if ("error" in maybeData) {
1245
- var _maybeData$error;
1419
+ var error = maybeData.error;
1246
1420
 
1247
- return React.createElement("div", null, "Error: ", (_maybeData$error = maybeData.error) == null ? void 0 : _maybeData$error.message);
1421
+ if (error && error instanceof HttpError && error.status === 404) {
1422
+ if (loaderProps.hideIfNotFound) {
1423
+ return null;
1424
+ } else {
1425
+ return React.createElement("div", null, "Error: Data not found");
1426
+ }
1427
+ } else {
1428
+ return React.createElement("div", null, "Error: ", error == null ? void 0 : error.message);
1429
+ }
1248
1430
  }
1249
1431
 
1250
1432
  if (!("data" in maybeData)) {
@@ -1329,6 +1511,8 @@ function TablesFetcher(_ref2) {
1329
1511
  return React.createElement(TablesProvider, {
1330
1512
  tables: tables
1331
1513
  }, children);
1514
+ }, {
1515
+ hideIfNotFound: false
1332
1516
  });
1333
1517
  }
1334
1518
 
@@ -1446,6 +1630,8 @@ function CmsQueryLoader(_ref5) {
1446
1630
  table: table,
1447
1631
  rows: rows
1448
1632
  }, children);
1633
+ }, {
1634
+ hideIfNotFound: false
1449
1635
  });
1450
1636
  }
1451
1637
  var cmsRowRepeaterMeta = {
@@ -1688,7 +1874,7 @@ var cmsRowLoaderMeta = {
1688
1874
  displayName: "CMS Row Loader",
1689
1875
  importName: "CmsRowLoader",
1690
1876
  importPath: modulePath,
1691
- props: {
1877
+ props: /*#__PURE__*/_extends({
1692
1878
  children: {
1693
1879
  type: "slot",
1694
1880
  defaultValue: {
@@ -1715,13 +1901,14 @@ var cmsRowLoaderMeta = {
1715
1901
  description: "If set, also query unpublished content.",
1716
1902
  defaultValue: false
1717
1903
  }
1718
- }
1904
+ }, fetcherComponentPropMetas)
1719
1905
  };
1720
1906
  function CmsRowLoader(_ref12) {
1721
1907
  var table = _ref12.table,
1722
1908
  row = _ref12.row,
1723
1909
  children = _ref12.children,
1724
1910
  useDraft = _ref12.useDraft,
1911
+ hideIfNotFound = _ref12.hideIfNotFound,
1725
1912
  setControlContextData = _ref12.setControlContextData;
1726
1913
  var databaseConfig = useDatabase();
1727
1914
  var tables = useTables();
@@ -1744,9 +1931,29 @@ function CmsRowLoader(_ref12) {
1744
1931
  while (1) {
1745
1932
  switch (_context3.prev = _context3.next) {
1746
1933
  case 0:
1747
- return _context3.abrupt("return", mkApi(databaseConfig).fetchRow(table, row, useDraft));
1934
+ if (table) {
1935
+ _context3.next = 2;
1936
+ break;
1937
+ }
1748
1938
 
1749
- case 1:
1939
+ throw new Error("You must specify a model to fetch from.");
1940
+
1941
+ case 2:
1942
+ if (row) {
1943
+ _context3.next = 4;
1944
+ break;
1945
+ }
1946
+
1947
+ throw new Error("You must specify an entry name to fetch.");
1948
+
1949
+ case 4:
1950
+ _context3.next = 6;
1951
+ return mkApi(databaseConfig).fetchRow(table, row, useDraft);
1952
+
1953
+ case 6:
1954
+ return _context3.abrupt("return", _context3.sent);
1955
+
1956
+ case 7:
1750
1957
  case "end":
1751
1958
  return _context3.stop();
1752
1959
  }
@@ -1758,6 +1965,8 @@ function CmsRowLoader(_ref12) {
1758
1965
  table: table,
1759
1966
  row: row
1760
1967
  }, children);
1968
+ }, {
1969
+ hideIfNotFound: hideIfNotFound
1761
1970
  });
1762
1971
  }
1763
1972