@agility/content-fetch 2.0.0-rc23 → 2.0.0-rc25

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,13 +2,15 @@
2
2
  [![Netlify Status](https://api.netlify.com/api/v1/badges/c45f5d6e-923b-4019-820e-826e6185017d/deploy-status)](https://app.netlify.com/sites/agilitydocs/deploys)
3
3
 
4
4
  # Agility Content Fetch JS SDK
5
- This is the official JavaScript library for accessing live and preview content from your [Agility CMS](https://agilitycms.com) instance.
5
+
6
+ This is the official JavaScript library for accessing live and preview content from your [Agility CMS](https://agilitycms.com) instance.
6
7
 
7
8
  You can use this in both node and browser based JS apps.
8
9
 
9
10
  Don't have an Agility CMS instance? Sign up for [Free Trial](https://agilitycms.com/free) today!
10
11
 
11
12
  ## Features
13
+
12
14
  - Queries the high-availability, CDN backed Agility Fetch REST API
13
15
  - Get a sitemap for a given channel
14
16
  - Get a page, including its content zones, modules, and their content
@@ -19,56 +21,62 @@ Don't have an Agility CMS instance? Sign up for [Free Trial](https://agilitycms.
19
21
  - Optional in-memory caching
20
22
 
21
23
  ## Getting Started
24
+
22
25
  In order to use this sdk, you'll need to install the script and you'll also need to authenticate your requests.
23
26
 
24
27
  ### Prerequisites
25
- You must have access to an Agility instance to retrieve the *guid* and generate your *apiKey*. Or, you must have these values provided to you.
28
+
29
+ You must have access to an Agility instance to retrieve the _guid_ and generate your _apiKey_. Or, you must have these values provided to you.
26
30
 
27
31
  ### Installation
28
- Install it using **npm** (recommended):
32
+
33
+ **npm**
34
+
29
35
  ```
30
36
  npm install @agility/content-fetch
31
37
  ```
32
38
 
33
- If necessary, you can also reference a standalone package that can be used in a browser using a traditional **script** tag. In this case, the sdk can be accessed using a global variable by name of `agility`:
34
- ```html
35
- <!-- Use a specific version (i.e. 0.4.2) -->
36
- <script type="text/javascript" src="https://unpkg.com/@agility/content-fetch@0.4.2/dist/agility-content-fetch.browser.js"></script>
39
+ **yarn**
37
40
 
38
- <!-- Or, Use the latest version -->
39
- <script type="text/javascript" src="https://unpkg.com/@agility/content-fetch@latest/dist/agility-content-fetch.browser.js"></script>
40
41
  ```
42
+ yarn add @agility/content-fetch
43
+ ```
44
+
41
45
  ### Making a Request
46
+
42
47
  ```javascript
43
- import agility from '@agility/content-fetch'
48
+ import agility from "@agility/content-fetch"
44
49
 
45
50
  //initialize the api client
46
51
  const api = agility.getApi({
47
- guid: 'ade6cf3c',
48
- apiKey: 'defaultlive.201ffdd0841cacad5bb647e76547e918b0c9ecdb8b5ddb3cf92e9a79b03623cb'
49
- });
52
+ guid: "ade6cf3c",
53
+ apiKey: "defaultlive.201ffdd0841cacad5bb647e76547e918b0c9ecdb8b5ddb3cf92e9a79b03623cb",
54
+ })
50
55
 
51
56
  //make the request: get a content item with the ID '22'
52
- api.getContentItem({
53
- contentID: 22,
54
- locale: 'en-us'
55
- })
56
- .then(function(contentItem) {
57
- //on success
58
- console.log(contentItem);
59
- })
60
- .catch(function(error) {
61
- //on error
62
- console.log(error);
63
- });
57
+ api
58
+ .getContentItem({
59
+ contentID: 22,
60
+ locale: "en-us",
61
+ })
62
+ .then(function (contentItem) {
63
+ //on success
64
+ console.log(contentItem)
65
+ })
66
+ .catch(function (error) {
67
+ //on error
68
+ console.log(error)
69
+ })
64
70
  ```
65
71
 
66
72
  ## Documentation
73
+
67
74
  Full documentation for this SDK can be found in our [Agility Fetch JS SDK Reference Doc](https://agilitydocs.netlify.com/agility-content-fetch-js-sdk/).
68
75
 
69
76
  For docs & help around Agility CMS, please see [Agility CMS Documentation](https://help.agilitycms.com/hc/en-us)
70
77
 
71
78
  ## Tutorials
79
+
72
80
  [About the Content Fetch API](https://help.agilitycms.com/hc/en-us/articles/360031985112-About-the-Content-Fetch-API)
73
81
 
74
82
  [Authenticating your Content Fetch API Calls](https://help.agilitycms.com/hc/en-us/articles/360032225191-Authenticating-your-Content-Fetch-API-Calls)
@@ -92,17 +100,13 @@ For docs & help around Agility CMS, please see [Agility CMS Documentation](https
92
100
  [Content Webhooks](https://help.agilitycms.com/hc/en-us/articles/360035934911)
93
101
 
94
102
  ## Contributing
103
+
95
104
  If you would like to contribute to this SDK, you can fork the repository and submit a pull request. We'd love to include your updates.
96
105
 
97
106
  ### Running the Tests
107
+
98
108
  An essential part of contributing to this SDK is adding and running unit tests to ensure the stability of the project.
109
+
99
110
  ```
100
111
  > npm run test
101
112
  ```
102
-
103
-
104
-
105
-
106
-
107
-
108
-
package/dist/browser.js CHANGED
@@ -6,11 +6,20 @@
6
6
  };
7
7
 
8
8
  // src/utils.ts
9
- function logError(consoleMessage) {
10
- console.error("\x1B[41m%s\x1B[0m", consoleMessage);
9
+ function logDebug({ config, message }) {
10
+ if (config.logLevel !== "debug" && !config.debug)
11
+ return;
12
+ console.log("\x1B[33m%s\x1B[0m", message);
11
13
  }
12
- function logDebug(consoleMessage) {
13
- console.log("\x1B[33m%s\x1B[0m", consoleMessage);
14
+ function logInfo({ config, message }) {
15
+ if (config.logLevel !== "debug" && config.logLevel !== "info")
16
+ return;
17
+ console.log("\x1B[33m%s\x1B[0m", message);
18
+ }
19
+ function logError({ config, message }) {
20
+ if (config.logLevel === "silent")
21
+ return;
22
+ console.error("\x1B[41m%s\x1B[0m", message);
14
23
  }
15
24
  function buildRequestUrlPath(config, locale) {
16
25
  let apiFetchOrPreview = "";
@@ -71,6 +80,18 @@
71
80
  return true;
72
81
  }
73
82
 
83
+ // src/types/errors/Errors.ts
84
+ var TypeError2 = class extends Error {
85
+ /**
86
+ * @param {string} message The error message
87
+ * @param {Function} [asserter] The assertion function that threw the error. Removes stack-trace noise if provided.
88
+ */
89
+ constructor(message, asserter) {
90
+ super(message);
91
+ Error.captureStackTrace?.(this, asserter || this.constructor);
92
+ }
93
+ };
94
+
74
95
  // src/methods/getSitemapFlat.ts
75
96
  function getSitemapFlat(requestParams) {
76
97
  validateRequestParams(requestParams);
@@ -85,9 +106,9 @@
85
106
  }
86
107
  function validateRequestParams(requestParams) {
87
108
  if (!requestParams.languageCode && !requestParams.locale) {
88
- throw new TypeError("You must include a locale in your request params.");
109
+ throw new TypeError2("You must include a locale in your request params.", validateRequestParams);
89
110
  } else if (!requestParams.channelName) {
90
- throw new TypeError("You must include a channelName in your request params.");
111
+ throw new TypeError2("You must include a channelName in your request params.", validateRequestParams);
91
112
  } else {
92
113
  return;
93
114
  }
@@ -108,9 +129,9 @@
108
129
  }
109
130
  function validateRequestParams2(requestParams) {
110
131
  if (!requestParams.languageCode && !requestParams.locale) {
111
- throw new TypeError("You must include a locale in your request params.");
132
+ throw new TypeError2("You must include a locale in your request params.", validateRequestParams2);
112
133
  } else if (!requestParams.channelName) {
113
- throw new TypeError("You must include a channelName in your request params.");
134
+ throw new TypeError2("You must include a channelName in your request params.", validateRequestParams2);
114
135
  }
115
136
  }
116
137
  var getSitemapNested_default = getSitemapNested;
@@ -134,13 +155,13 @@
134
155
  }
135
156
  function validateRequestParams3(requestParams) {
136
157
  if (!requestParams.languageCode && !requestParams.locale) {
137
- throw new TypeError("You must include a locale in your request params.");
158
+ throw new TypeError2("You must include a locale in your request params.", validateRequestParams3);
138
159
  } else if (!requestParams.contentID) {
139
- throw new TypeError("You must include a contentID number in your request params.");
160
+ throw new TypeError2("You must include a contentID number in your request params.");
140
161
  } else if (requestParams.contentLinkDepth && (isNaN(requestParams.contentLinkDepth) || requestParams.contentLinkDepth < 0)) {
141
- throw new TypeError("When specifying contentLinkDepth, it must be a number greater than 0.");
162
+ throw new TypeError2("When specifying contentLinkDepth, it must be a number greater than 0.");
142
163
  } else if (requestParams.expandAllContentLinks && typeof requestParams.expandAllContentLinks !== "boolean") {
143
- throw new TypeError("ExpandAllContentLinks parameter must be a value of true or false");
164
+ throw new TypeError2("ExpandAllContentLinks parameter must be a value of true or false");
144
165
  } else {
145
166
  return;
146
167
  }
@@ -166,39 +187,39 @@
166
187
  }
167
188
  function validateRequestParams4(requestParams) {
168
189
  if (!requestParams.languageCode && !requestParams.locale) {
169
- throw new TypeError("You must include a locale in your request params.");
190
+ throw new TypeError2("You must include a locale in your request params.", validateRequestParams4);
170
191
  } else if (!requestParams.referenceName) {
171
- throw new TypeError("You must include a content referenceName in your request params.");
192
+ throw new TypeError2("You must include a content referenceName in your request params.", validateRequestParams4);
172
193
  } else if (requestParams.take && isNaN(requestParams.take)) {
173
- throw new TypeError("Take parameter must be a number.");
194
+ throw new TypeError2("Take parameter must be a number.", validateRequestParams4);
174
195
  } else if ((requestParams.take || requestParams.take == 0) && !isNaN(requestParams.take) && requestParams.take < 1) {
175
- throw new TypeError("Take parameter must be greater than 0.");
196
+ throw new TypeError2("Take parameter must be greater than 0.", validateRequestParams4);
176
197
  } else if (requestParams.take && !isNaN(requestParams.take) && requestParams.take > 250) {
177
- throw new TypeError("Take parameter must be 250 or less.");
198
+ throw new TypeError2("Take parameter must be 250 or less.", validateRequestParams4);
178
199
  } else if (requestParams.skip && isNaN(requestParams.skip)) {
179
- throw new TypeError("Skip parameter must be a number.");
200
+ throw new TypeError2("Skip parameter must be a number.", validateRequestParams4);
180
201
  } else if (requestParams.skip && !isNaN(requestParams.skip) && requestParams.skip < 0) {
181
- throw new TypeError("Skip parameter must be 0 or greater");
202
+ throw new TypeError2("Skip parameter must be 0 or greater", validateRequestParams4);
182
203
  } else if (requestParams.direction && (requestParams.direction !== "desc" && requestParams.direction !== "asc")) {
183
- throw new TypeError('Direction parameter must have a value of "asc" or "desc"');
204
+ throw new TypeError2('Direction parameter must have a value of "asc" or "desc"', validateRequestParams4);
184
205
  } else if (requestParams.filters && requestParams.filters.length > 0) {
185
206
  for (let i = 0; i < requestParams.filters.length; i++) {
186
207
  let filter = requestParams.filters[i];
187
208
  if (!filter.hasOwnProperty("property")) {
188
- throw new TypeError(JSON.stringify(filter) + " does not contain 'property'.");
209
+ throw new TypeError2(JSON.stringify(filter) + " does not contain 'property'.", validateRequestParams4);
189
210
  } else if (!filter.hasOwnProperty("operator")) {
190
- throw new TypeError(JSON.stringify(filter) + " does not contain 'operator'.");
211
+ throw new TypeError2(JSON.stringify(filter) + " does not contain 'operator'.", validateRequestParams4);
191
212
  } else if (!filter.hasOwnProperty("value")) {
192
- throw new TypeError(JSON.stringify(filter) + " does not contain 'value'.");
213
+ throw new TypeError2(JSON.stringify(filter) + " does not contain 'value'.", validateRequestParams4);
193
214
  }
194
215
  if (["eq", "ne", "gt", "gte", "lt", "lte", "like"].indexOf(filter.operator.toLowerCase()) < 0) {
195
- throw new TypeError(JSON.stringify(filter) + "Operator must be one of ['eq','ne','gt','gte','lt','lte','like'].");
216
+ throw new TypeError2(JSON.stringify(filter) + "Operator must be one of ['eq','ne','gt','gte','lt','lte','like', 'in', 'contains'].", validateRequestParams4);
196
217
  }
197
218
  }
198
219
  } else if (requestParams.filtersLogicOperator && requestParams.filtersLogicOperator.toLowerCase() !== "and" && requestParams.filtersLogicOperator.toLowerCase() !== "or") {
199
- throw new TypeError('FiltersLogicOperator parameter must have a value of "AND" or "OR"');
220
+ throw new TypeError2('FiltersLogicOperator parameter must have a value of "AND" or "OR"', validateRequestParams4);
200
221
  } else if (requestParams.expandAllContentLinks && typeof requestParams.expandAllContentLinks !== "boolean") {
201
- throw new TypeError("ExpandAllContentLinks parameter must be a value of true or false");
222
+ throw new TypeError2("ExpandAllContentLinks parameter must be a value of true or false", validateRequestParams4);
202
223
  }
203
224
  return true;
204
225
  }
@@ -223,11 +244,11 @@
223
244
  }
224
245
  function validateRequestParams5(requestParams) {
225
246
  if (!requestParams.languageCode && !requestParams.locale) {
226
- throw new TypeError("You must include a locale in your request params.");
247
+ throw new TypeError2("You must include a locale in your request params.", validateRequestParams5);
227
248
  } else if (!requestParams.pageID) {
228
- throw new TypeError("You must include a pageID in your request params.");
249
+ throw new TypeError2("You must include a pageID in your request params.", validateRequestParams5);
229
250
  } else if (requestParams.expandAllContentLinks && typeof requestParams.expandAllContentLinks !== "boolean") {
230
- throw new TypeError("ExpandAllContentLinks parameter must be a value of true or false");
251
+ throw new TypeError2("ExpandAllContentLinks parameter must be a value of true or false", validateRequestParams5);
231
252
  } else {
232
253
  return;
233
254
  }
@@ -253,7 +274,7 @@
253
274
  }
254
275
  function validateRequestParams6(requestParams) {
255
276
  if (!requestParams.galleryID) {
256
- throw new TypeError("You must include a galleryID number in your request params.");
277
+ throw new TypeError2("You must include a galleryID number in your request params.", validateRequestParams6);
257
278
  } else {
258
279
  return;
259
280
  }
@@ -301,7 +322,7 @@
301
322
  if (!requestParams.lastAccessDate.toISOString) {
302
323
  let dt = new Date(requestParams.lastAccessDate);
303
324
  if (isNaN(dt.getTime())) {
304
- throw new TypeError("You must include a valid Datetime for the lastAccessDate.");
325
+ throw new TypeError2("You must include a valid Datetime for the lastAccessDate.", validateRequestParams7);
305
326
  }
306
327
  }
307
328
  } else {
@@ -324,9 +345,9 @@
324
345
  }
325
346
  function validateRequestParams8(requestParams) {
326
347
  if (!requestParams.languageCode && !requestParams.locale) {
327
- throw new TypeError("You must include a locale in your request params.");
348
+ throw new TypeError2("You must include a locale in your request params.", validateRequestParams8);
328
349
  } else if (requestParams.syncToken == void 0 || requestParams.syncToken == null) {
329
- throw new TypeError("You must include a syncToken value your request params. Use zero (0) to start a new sync.");
350
+ throw new TypeError2("You must include a syncToken value your request params. Use zero (0) to start a new sync.", validateRequestParams8);
330
351
  } else {
331
352
  return;
332
353
  }
@@ -360,7 +381,8 @@
360
381
  var types_exports = {};
361
382
  __export(types_exports, {
362
383
  FilterLogicOperators: () => FilterLogicOperators,
363
- FilterOperators: () => FilterOperator_default
384
+ FilterOperators: () => FilterOperator_default,
385
+ SortDirections: () => SortDirections
364
386
  });
365
387
 
366
388
  // src/types/FilterOperator.ts
@@ -383,6 +405,13 @@
383
405
  return FilterLogicOperators2;
384
406
  })(FilterLogicOperators || {});
385
407
 
408
+ // src/types/SortDirection.ts
409
+ var SortDirections = /* @__PURE__ */ ((SortDirections2) => {
410
+ SortDirections2["ASC"] = "asc";
411
+ SortDirections2["DESC"] = "desc";
412
+ return SortDirections2;
413
+ })(SortDirections || {});
414
+
386
415
  // src/api-client.ts
387
416
  var defaultConfig = {
388
417
  baseUrl: null,
@@ -392,6 +421,7 @@
392
421
  locale: null,
393
422
  headers: {},
394
423
  requiresGuidInHeaders: false,
424
+ logLevel: "warn",
395
425
  debug: false,
396
426
  caching: {
397
427
  maxAge: 0
@@ -416,13 +446,13 @@
416
446
  }
417
447
  function validateConfigParams(configParams) {
418
448
  if (!configParams.guid || configParams.guid.length == 0) {
419
- throw new TypeError("You must provide a guid.");
449
+ throw new TypeError2("You must provide a guid.");
420
450
  } else if (!configParams.apiKey || configParams.apiKey.length == 0) {
421
- throw new TypeError("You must provide an access token.");
451
+ throw new TypeError2("You must provide an access token.");
422
452
  } else if (configParams.caching && isNaN(configParams.caching.maxAge)) {
423
- throw new TypeError("When specifying a cache maxAge, you must set a number value in miliseconds, i.e. 180000 (3 mins).");
453
+ throw new TypeError2("When specifying a cache maxAge, you must set a number value in miliseconds, i.e. 180000 (3 mins).");
424
454
  } else if (configParams.baseUrl && !isHttps(configParams.baseUrl)) {
425
- throw new TypeError(`When specifying a baseUrl (${configParams.baseUrl}), it must be over HTTPs.`);
455
+ throw new TypeError2(`When specifying a baseUrl (${configParams.baseUrl}), it must be over HTTPs.`);
426
456
  } else {
427
457
  return;
428
458
  }
@@ -471,9 +501,7 @@
471
501
  //the function that actually makes ALL our requests
472
502
  async makeRequest(reqConfig) {
473
503
  const isPreview = !!this.config.isPreview;
474
- if (this.config.debug) {
475
- logDebug(`AgilityCMS Fetch API LOG: ${reqConfig.baseURL}${reqConfig.url}`);
476
- }
504
+ logDebug({ config: this.config, message: `AgilityCMS Fetch API LOG: ${reqConfig.baseURL}${reqConfig.url}` });
477
505
  try {
478
506
  const fullUrl = `${reqConfig.baseURL}${reqConfig.url}`;
479
507
  let init = {
@@ -491,7 +519,11 @@
491
519
  }
492
520
  const response = await fetch(fullUrl, init);
493
521
  if (!response.ok) {
494
- logError(`AgilityCMS Fetch API ERROR: Request returned a ${response.status} response for ${reqConfig.baseURL}${reqConfig.url}. ${response.statusText}`);
522
+ if (response.status === 404) {
523
+ logInfo({ config: this.config, message: `AgilityCMS Fetch API: Request returned a ${response.status} response for ${reqConfig.baseURL}${reqConfig.url}. ${response.statusText}` });
524
+ return;
525
+ }
526
+ logError({ config: this.config, message: `AgilityCMS Fetch API ERROR: Request returned a ${response.status} response for ${reqConfig.baseURL}${reqConfig.url}. ${response.statusText}` });
495
527
  return;
496
528
  }
497
529
  let data = await response.json();
@@ -500,7 +532,7 @@
500
532
  }
501
533
  return data;
502
534
  } catch (error) {
503
- logError(`AgilityCMS Fetch API ERROR: Request failed for ${reqConfig.baseURL}${reqConfig.url} ... ${error} ... Does the item exist?`);
535
+ logError({ config: this.config, message: `AgilityCMS Fetch API ERROR: Request failed for ${reqConfig.baseURL}${reqConfig.url} ... ${error}` });
504
536
  }
505
537
  }
506
538
  };
package/dist/node.js CHANGED
@@ -24,11 +24,20 @@ __export(src_exports, {
24
24
  module.exports = __toCommonJS(src_exports);
25
25
 
26
26
  // src/utils.ts
27
- function logError(consoleMessage) {
28
- console.error("\x1B[41m%s\x1B[0m", consoleMessage);
27
+ function logDebug({ config, message }) {
28
+ if (config.logLevel !== "debug" && !config.debug)
29
+ return;
30
+ console.log("\x1B[33m%s\x1B[0m", message);
29
31
  }
30
- function logDebug(consoleMessage) {
31
- console.log("\x1B[33m%s\x1B[0m", consoleMessage);
32
+ function logInfo({ config, message }) {
33
+ if (config.logLevel !== "debug" && config.logLevel !== "info")
34
+ return;
35
+ console.log("\x1B[33m%s\x1B[0m", message);
36
+ }
37
+ function logError({ config, message }) {
38
+ if (config.logLevel === "silent")
39
+ return;
40
+ console.error("\x1B[41m%s\x1B[0m", message);
32
41
  }
33
42
  function buildRequestUrlPath(config, locale) {
34
43
  let apiFetchOrPreview = "";
@@ -89,6 +98,18 @@ function isHttps(url) {
89
98
  return true;
90
99
  }
91
100
 
101
+ // src/types/errors/Errors.ts
102
+ var TypeError2 = class extends Error {
103
+ /**
104
+ * @param {string} message The error message
105
+ * @param {Function} [asserter] The assertion function that threw the error. Removes stack-trace noise if provided.
106
+ */
107
+ constructor(message, asserter) {
108
+ super(message);
109
+ Error.captureStackTrace?.(this, asserter || this.constructor);
110
+ }
111
+ };
112
+
92
113
  // src/methods/getSitemapFlat.ts
93
114
  function getSitemapFlat(requestParams) {
94
115
  validateRequestParams(requestParams);
@@ -103,9 +124,9 @@ function getSitemapFlat(requestParams) {
103
124
  }
104
125
  function validateRequestParams(requestParams) {
105
126
  if (!requestParams.languageCode && !requestParams.locale) {
106
- throw new TypeError("You must include a locale in your request params.");
127
+ throw new TypeError2("You must include a locale in your request params.", validateRequestParams);
107
128
  } else if (!requestParams.channelName) {
108
- throw new TypeError("You must include a channelName in your request params.");
129
+ throw new TypeError2("You must include a channelName in your request params.", validateRequestParams);
109
130
  } else {
110
131
  return;
111
132
  }
@@ -126,9 +147,9 @@ function getSitemapNested(requestParams) {
126
147
  }
127
148
  function validateRequestParams2(requestParams) {
128
149
  if (!requestParams.languageCode && !requestParams.locale) {
129
- throw new TypeError("You must include a locale in your request params.");
150
+ throw new TypeError2("You must include a locale in your request params.", validateRequestParams2);
130
151
  } else if (!requestParams.channelName) {
131
- throw new TypeError("You must include a channelName in your request params.");
152
+ throw new TypeError2("You must include a channelName in your request params.", validateRequestParams2);
132
153
  }
133
154
  }
134
155
  var getSitemapNested_default = getSitemapNested;
@@ -152,13 +173,13 @@ function getContentItem(requestParams) {
152
173
  }
153
174
  function validateRequestParams3(requestParams) {
154
175
  if (!requestParams.languageCode && !requestParams.locale) {
155
- throw new TypeError("You must include a locale in your request params.");
176
+ throw new TypeError2("You must include a locale in your request params.", validateRequestParams3);
156
177
  } else if (!requestParams.contentID) {
157
- throw new TypeError("You must include a contentID number in your request params.");
178
+ throw new TypeError2("You must include a contentID number in your request params.");
158
179
  } else if (requestParams.contentLinkDepth && (isNaN(requestParams.contentLinkDepth) || requestParams.contentLinkDepth < 0)) {
159
- throw new TypeError("When specifying contentLinkDepth, it must be a number greater than 0.");
180
+ throw new TypeError2("When specifying contentLinkDepth, it must be a number greater than 0.");
160
181
  } else if (requestParams.expandAllContentLinks && typeof requestParams.expandAllContentLinks !== "boolean") {
161
- throw new TypeError("ExpandAllContentLinks parameter must be a value of true or false");
182
+ throw new TypeError2("ExpandAllContentLinks parameter must be a value of true or false");
162
183
  } else {
163
184
  return;
164
185
  }
@@ -184,39 +205,39 @@ function sanitizeReferenceName(referenceName) {
184
205
  }
185
206
  function validateRequestParams4(requestParams) {
186
207
  if (!requestParams.languageCode && !requestParams.locale) {
187
- throw new TypeError("You must include a locale in your request params.");
208
+ throw new TypeError2("You must include a locale in your request params.", validateRequestParams4);
188
209
  } else if (!requestParams.referenceName) {
189
- throw new TypeError("You must include a content referenceName in your request params.");
210
+ throw new TypeError2("You must include a content referenceName in your request params.", validateRequestParams4);
190
211
  } else if (requestParams.take && isNaN(requestParams.take)) {
191
- throw new TypeError("Take parameter must be a number.");
212
+ throw new TypeError2("Take parameter must be a number.", validateRequestParams4);
192
213
  } else if ((requestParams.take || requestParams.take == 0) && !isNaN(requestParams.take) && requestParams.take < 1) {
193
- throw new TypeError("Take parameter must be greater than 0.");
214
+ throw new TypeError2("Take parameter must be greater than 0.", validateRequestParams4);
194
215
  } else if (requestParams.take && !isNaN(requestParams.take) && requestParams.take > 250) {
195
- throw new TypeError("Take parameter must be 250 or less.");
216
+ throw new TypeError2("Take parameter must be 250 or less.", validateRequestParams4);
196
217
  } else if (requestParams.skip && isNaN(requestParams.skip)) {
197
- throw new TypeError("Skip parameter must be a number.");
218
+ throw new TypeError2("Skip parameter must be a number.", validateRequestParams4);
198
219
  } else if (requestParams.skip && !isNaN(requestParams.skip) && requestParams.skip < 0) {
199
- throw new TypeError("Skip parameter must be 0 or greater");
220
+ throw new TypeError2("Skip parameter must be 0 or greater", validateRequestParams4);
200
221
  } else if (requestParams.direction && (requestParams.direction !== "desc" && requestParams.direction !== "asc")) {
201
- throw new TypeError('Direction parameter must have a value of "asc" or "desc"');
222
+ throw new TypeError2('Direction parameter must have a value of "asc" or "desc"', validateRequestParams4);
202
223
  } else if (requestParams.filters && requestParams.filters.length > 0) {
203
224
  for (let i = 0; i < requestParams.filters.length; i++) {
204
225
  let filter = requestParams.filters[i];
205
226
  if (!filter.hasOwnProperty("property")) {
206
- throw new TypeError(JSON.stringify(filter) + " does not contain 'property'.");
227
+ throw new TypeError2(JSON.stringify(filter) + " does not contain 'property'.", validateRequestParams4);
207
228
  } else if (!filter.hasOwnProperty("operator")) {
208
- throw new TypeError(JSON.stringify(filter) + " does not contain 'operator'.");
229
+ throw new TypeError2(JSON.stringify(filter) + " does not contain 'operator'.", validateRequestParams4);
209
230
  } else if (!filter.hasOwnProperty("value")) {
210
- throw new TypeError(JSON.stringify(filter) + " does not contain 'value'.");
231
+ throw new TypeError2(JSON.stringify(filter) + " does not contain 'value'.", validateRequestParams4);
211
232
  }
212
233
  if (["eq", "ne", "gt", "gte", "lt", "lte", "like"].indexOf(filter.operator.toLowerCase()) < 0) {
213
- throw new TypeError(JSON.stringify(filter) + "Operator must be one of ['eq','ne','gt','gte','lt','lte','like'].");
234
+ throw new TypeError2(JSON.stringify(filter) + "Operator must be one of ['eq','ne','gt','gte','lt','lte','like', 'in', 'contains'].", validateRequestParams4);
214
235
  }
215
236
  }
216
237
  } else if (requestParams.filtersLogicOperator && requestParams.filtersLogicOperator.toLowerCase() !== "and" && requestParams.filtersLogicOperator.toLowerCase() !== "or") {
217
- throw new TypeError('FiltersLogicOperator parameter must have a value of "AND" or "OR"');
238
+ throw new TypeError2('FiltersLogicOperator parameter must have a value of "AND" or "OR"', validateRequestParams4);
218
239
  } else if (requestParams.expandAllContentLinks && typeof requestParams.expandAllContentLinks !== "boolean") {
219
- throw new TypeError("ExpandAllContentLinks parameter must be a value of true or false");
240
+ throw new TypeError2("ExpandAllContentLinks parameter must be a value of true or false", validateRequestParams4);
220
241
  }
221
242
  return true;
222
243
  }
@@ -241,11 +262,11 @@ function getPage(requestParams) {
241
262
  }
242
263
  function validateRequestParams5(requestParams) {
243
264
  if (!requestParams.languageCode && !requestParams.locale) {
244
- throw new TypeError("You must include a locale in your request params.");
265
+ throw new TypeError2("You must include a locale in your request params.", validateRequestParams5);
245
266
  } else if (!requestParams.pageID) {
246
- throw new TypeError("You must include a pageID in your request params.");
267
+ throw new TypeError2("You must include a pageID in your request params.", validateRequestParams5);
247
268
  } else if (requestParams.expandAllContentLinks && typeof requestParams.expandAllContentLinks !== "boolean") {
248
- throw new TypeError("ExpandAllContentLinks parameter must be a value of true or false");
269
+ throw new TypeError2("ExpandAllContentLinks parameter must be a value of true or false", validateRequestParams5);
249
270
  } else {
250
271
  return;
251
272
  }
@@ -271,7 +292,7 @@ function getGallery(requestParams) {
271
292
  }
272
293
  function validateRequestParams6(requestParams) {
273
294
  if (!requestParams.galleryID) {
274
- throw new TypeError("You must include a galleryID number in your request params.");
295
+ throw new TypeError2("You must include a galleryID number in your request params.", validateRequestParams6);
275
296
  } else {
276
297
  return;
277
298
  }
@@ -319,7 +340,7 @@ function validateRequestParams7(requestParams) {
319
340
  if (!requestParams.lastAccessDate.toISOString) {
320
341
  let dt = new Date(requestParams.lastAccessDate);
321
342
  if (isNaN(dt.getTime())) {
322
- throw new TypeError("You must include a valid Datetime for the lastAccessDate.");
343
+ throw new TypeError2("You must include a valid Datetime for the lastAccessDate.", validateRequestParams7);
323
344
  }
324
345
  }
325
346
  } else {
@@ -342,9 +363,9 @@ function getSyncContent(requestParams) {
342
363
  }
343
364
  function validateRequestParams8(requestParams) {
344
365
  if (!requestParams.languageCode && !requestParams.locale) {
345
- throw new TypeError("You must include a locale in your request params.");
366
+ throw new TypeError2("You must include a locale in your request params.", validateRequestParams8);
346
367
  } else if (requestParams.syncToken == void 0 || requestParams.syncToken == null) {
347
- throw new TypeError("You must include a syncToken value your request params. Use zero (0) to start a new sync.");
368
+ throw new TypeError2("You must include a syncToken value your request params. Use zero (0) to start a new sync.", validateRequestParams8);
348
369
  } else {
349
370
  return;
350
371
  }
@@ -378,7 +399,8 @@ var getSyncPages_default = getSyncPages;
378
399
  var types_exports = {};
379
400
  __export(types_exports, {
380
401
  FilterLogicOperators: () => FilterLogicOperators,
381
- FilterOperators: () => FilterOperator_default
402
+ FilterOperators: () => FilterOperator_default,
403
+ SortDirections: () => SortDirections
382
404
  });
383
405
 
384
406
  // src/types/FilterOperator.ts
@@ -401,6 +423,13 @@ var FilterLogicOperators = /* @__PURE__ */ ((FilterLogicOperators2) => {
401
423
  return FilterLogicOperators2;
402
424
  })(FilterLogicOperators || {});
403
425
 
426
+ // src/types/SortDirection.ts
427
+ var SortDirections = /* @__PURE__ */ ((SortDirections2) => {
428
+ SortDirections2["ASC"] = "asc";
429
+ SortDirections2["DESC"] = "desc";
430
+ return SortDirections2;
431
+ })(SortDirections || {});
432
+
404
433
  // src/api-client.ts
405
434
  var defaultConfig = {
406
435
  baseUrl: null,
@@ -410,6 +439,7 @@ var defaultConfig = {
410
439
  locale: null,
411
440
  headers: {},
412
441
  requiresGuidInHeaders: false,
442
+ logLevel: "warn",
413
443
  debug: false,
414
444
  caching: {
415
445
  maxAge: 0
@@ -434,13 +464,13 @@ function buildBaseUrl(guid) {
434
464
  }
435
465
  function validateConfigParams(configParams) {
436
466
  if (!configParams.guid || configParams.guid.length == 0) {
437
- throw new TypeError("You must provide a guid.");
467
+ throw new TypeError2("You must provide a guid.");
438
468
  } else if (!configParams.apiKey || configParams.apiKey.length == 0) {
439
- throw new TypeError("You must provide an access token.");
469
+ throw new TypeError2("You must provide an access token.");
440
470
  } else if (configParams.caching && isNaN(configParams.caching.maxAge)) {
441
- throw new TypeError("When specifying a cache maxAge, you must set a number value in miliseconds, i.e. 180000 (3 mins).");
471
+ throw new TypeError2("When specifying a cache maxAge, you must set a number value in miliseconds, i.e. 180000 (3 mins).");
442
472
  } else if (configParams.baseUrl && !isHttps(configParams.baseUrl)) {
443
- throw new TypeError(`When specifying a baseUrl (${configParams.baseUrl}), it must be over HTTPs.`);
473
+ throw new TypeError2(`When specifying a baseUrl (${configParams.baseUrl}), it must be over HTTPs.`);
444
474
  } else {
445
475
  return;
446
476
  }
@@ -489,9 +519,7 @@ var ApiClient = class {
489
519
  //the function that actually makes ALL our requests
490
520
  async makeRequest(reqConfig) {
491
521
  const isPreview = !!this.config.isPreview;
492
- if (this.config.debug) {
493
- logDebug(`AgilityCMS Fetch API LOG: ${reqConfig.baseURL}${reqConfig.url}`);
494
- }
522
+ logDebug({ config: this.config, message: `AgilityCMS Fetch API LOG: ${reqConfig.baseURL}${reqConfig.url}` });
495
523
  try {
496
524
  const fullUrl = `${reqConfig.baseURL}${reqConfig.url}`;
497
525
  let init = {
@@ -509,7 +537,11 @@ var ApiClient = class {
509
537
  }
510
538
  const response = await fetch(fullUrl, init);
511
539
  if (!response.ok) {
512
- logError(`AgilityCMS Fetch API ERROR: Request returned a ${response.status} response for ${reqConfig.baseURL}${reqConfig.url}. ${response.statusText}`);
540
+ if (response.status === 404) {
541
+ logInfo({ config: this.config, message: `AgilityCMS Fetch API: Request returned a ${response.status} response for ${reqConfig.baseURL}${reqConfig.url}. ${response.statusText}` });
542
+ return;
543
+ }
544
+ logError({ config: this.config, message: `AgilityCMS Fetch API ERROR: Request returned a ${response.status} response for ${reqConfig.baseURL}${reqConfig.url}. ${response.statusText}` });
513
545
  return;
514
546
  }
515
547
  let data = await response.json();
@@ -518,7 +550,7 @@ var ApiClient = class {
518
550
  }
519
551
  return data;
520
552
  } catch (error) {
521
- logError(`AgilityCMS Fetch API ERROR: Request failed for ${reqConfig.baseURL}${reqConfig.url} ... ${error} ... Does the item exist?`);
553
+ logError({ config: this.config, message: `AgilityCMS Fetch API ERROR: Request failed for ${reqConfig.baseURL}${reqConfig.url} ... ${error}` });
522
554
  }
523
555
  }
524
556
  };
@@ -1,14 +1,42 @@
1
1
  export interface Config {
2
+ /**
3
+ * The optional baseUrl for the API. If not provided, the API will use the default baseUrl for the instance.
4
+ * This is mostly used for testing purposes.
5
+ */
2
6
  baseUrl?: string | null;
7
+ /**
8
+ * If true, the API will use the Preview API. If false, it will use the Fetch API. Default is false.
9
+ * Make sure the API Key provided matches this value.
10
+ */
3
11
  isPreview?: boolean;
12
+ /**
13
+ * The guid that represents your instance.
14
+ */
4
15
  guid?: string | null;
16
+ /**
17
+ * The Fetch or Preview API key.
18
+ */
5
19
  apiKey?: string | null;
6
20
  locale?: string | null;
21
+ /**
22
+ * Additional headers to include in the request.
23
+ */
7
24
  headers?: {
8
25
  [key: string]: string;
9
26
  };
10
27
  requiresGuidInHeaders?: boolean;
28
+ /**
29
+ * The logging level. Default is 'warn'.
30
+ */
31
+ logLevel?: 'debug' | 'info' | 'warn' | 'error' | 'silent';
32
+ /**
33
+ * Used for debugging purposes. Default is false.
34
+ */
11
35
  debug?: boolean;
36
+ /**
37
+ * Optional Caching options. Caching is disabled by default.
38
+ * This is mostly used for Next.js and other server-side rendering frameworks.
39
+ */
12
40
  caching?: {
13
41
  maxAge?: number;
14
42
  };
@@ -0,0 +1,22 @@
1
+ export declare class EvalError extends Error {
2
+ constructor(message?: string, asserter?: any);
3
+ }
4
+ export declare class RangeError extends Error {
5
+ constructor(message?: string, asserter?: any);
6
+ }
7
+ export declare class ReferenceError extends Error {
8
+ constructor(message?: string, asserter?: any);
9
+ }
10
+ export declare class SyntaxError extends Error {
11
+ constructor(message?: string, asserter?: any);
12
+ }
13
+ export declare class TypeError extends Error {
14
+ /**
15
+ * @param {string} message The error message
16
+ * @param {Function} [asserter] The assertion function that threw the error. Removes stack-trace noise if provided.
17
+ */
18
+ constructor(message: string, asserter?: any);
19
+ }
20
+ export declare class URIError extends Error {
21
+ constructor(message?: string, asserter?: any);
22
+ }
@@ -1,3 +1,4 @@
1
1
  import FilterOperators from "./FilterOperator";
2
2
  import { FilterLogicOperators } from "./FilterLogicOperator";
3
- export { FilterOperators, FilterLogicOperators };
3
+ import { SortDirections } from "./SortDirection";
4
+ export { FilterOperators, FilterLogicOperators, SortDirections };
package/dist/utils.d.ts CHANGED
@@ -1,7 +1,14 @@
1
- declare function logError(consoleMessage: any): void;
2
- declare function logDebug(consoleMessage: any): void;
1
+ import { Config } from "./types/Config";
2
+ interface LogProps {
3
+ config: Config;
4
+ message: string;
5
+ }
6
+ declare function logDebug({ config, message }: LogProps): void;
7
+ declare function logInfo({ config, message }: LogProps): void;
8
+ declare function logWarning({ config, message }: LogProps): void;
9
+ declare function logError({ config, message }: LogProps): void;
3
10
  declare function buildRequestUrlPath(config: any, locale: any): string;
4
11
  declare function buildPathUrl(contentType: any, referenceName: any, skip: any, take: any, sort: any, direction: any, filters: any, filtersLogicOperator: any, contentLinkDepth: any, expandAllContentLinks: any): string;
5
12
  declare function buildAuthHeader(config: any): any;
6
13
  declare function isHttps(url: any): boolean;
7
- export { buildPathUrl, buildAuthHeader, buildRequestUrlPath, isHttps, logError, logDebug };
14
+ export { buildPathUrl, buildAuthHeader, buildRequestUrlPath, isHttps, logError, logDebug, logInfo, logWarning };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agility/content-fetch",
3
- "version": "2.0.0-rc23",
3
+ "version": "2.0.0-rc25",
4
4
  "description": "JavaScript library for the Agility Fetch API (node and browser)",
5
5
  "repository": {
6
6
  "type": "git",