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

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);
13
+ }
14
+ function logInfo({ config, message }) {
15
+ if (config.logLevel !== "debug" && config.logLevel !== "info")
16
+ return;
17
+ console.log("\x1B[33m%s\x1B[0m", message);
11
18
  }
12
- function logDebug(consoleMessage) {
13
- console.log("\x1B[33m%s\x1B[0m", consoleMessage);
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 = "";
@@ -392,6 +401,7 @@
392
401
  locale: null,
393
402
  headers: {},
394
403
  requiresGuidInHeaders: false,
404
+ logLevel: "warn",
395
405
  debug: false,
396
406
  caching: {
397
407
  maxAge: 0
@@ -471,9 +481,7 @@
471
481
  //the function that actually makes ALL our requests
472
482
  async makeRequest(reqConfig) {
473
483
  const isPreview = !!this.config.isPreview;
474
- if (this.config.debug) {
475
- logDebug(`AgilityCMS Fetch API LOG: ${reqConfig.baseURL}${reqConfig.url}`);
476
- }
484
+ logDebug({ config: this.config, message: `AgilityCMS Fetch API LOG: ${reqConfig.baseURL}${reqConfig.url}` });
477
485
  try {
478
486
  const fullUrl = `${reqConfig.baseURL}${reqConfig.url}`;
479
487
  let init = {
@@ -491,7 +499,11 @@
491
499
  }
492
500
  const response = await fetch(fullUrl, init);
493
501
  if (!response.ok) {
494
- logError(`AgilityCMS Fetch API ERROR: Request returned a ${response.status} response for ${reqConfig.baseURL}${reqConfig.url}. ${response.statusText}`);
502
+ if (response.status === 404) {
503
+ logInfo({ config: this.config, message: `AgilityCMS Fetch API: Request returned a ${response.status} response for ${reqConfig.baseURL}${reqConfig.url}. ${response.statusText}` });
504
+ return;
505
+ }
506
+ logError({ config: this.config, message: `AgilityCMS Fetch API ERROR: Request returned a ${response.status} response for ${reqConfig.baseURL}${reqConfig.url}. ${response.statusText}` });
495
507
  return;
496
508
  }
497
509
  let data = await response.json();
@@ -500,7 +512,7 @@
500
512
  }
501
513
  return data;
502
514
  } catch (error) {
503
- logError(`AgilityCMS Fetch API ERROR: Request failed for ${reqConfig.baseURL}${reqConfig.url} ... ${error} ... Does the item exist?`);
515
+ logError({ config: this.config, message: `AgilityCMS Fetch API ERROR: Request failed for ${reqConfig.baseURL}${reqConfig.url} ... ${error}` });
504
516
  }
505
517
  }
506
518
  };
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);
31
+ }
32
+ function logInfo({ config, message }) {
33
+ if (config.logLevel !== "debug" && config.logLevel !== "info")
34
+ return;
35
+ console.log("\x1B[33m%s\x1B[0m", message);
29
36
  }
30
- function logDebug(consoleMessage) {
31
- console.log("\x1B[33m%s\x1B[0m", consoleMessage);
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 = "";
@@ -410,6 +419,7 @@ var defaultConfig = {
410
419
  locale: null,
411
420
  headers: {},
412
421
  requiresGuidInHeaders: false,
422
+ logLevel: "warn",
413
423
  debug: false,
414
424
  caching: {
415
425
  maxAge: 0
@@ -489,9 +499,7 @@ var ApiClient = class {
489
499
  //the function that actually makes ALL our requests
490
500
  async makeRequest(reqConfig) {
491
501
  const isPreview = !!this.config.isPreview;
492
- if (this.config.debug) {
493
- logDebug(`AgilityCMS Fetch API LOG: ${reqConfig.baseURL}${reqConfig.url}`);
494
- }
502
+ logDebug({ config: this.config, message: `AgilityCMS Fetch API LOG: ${reqConfig.baseURL}${reqConfig.url}` });
495
503
  try {
496
504
  const fullUrl = `${reqConfig.baseURL}${reqConfig.url}`;
497
505
  let init = {
@@ -509,7 +517,11 @@ var ApiClient = class {
509
517
  }
510
518
  const response = await fetch(fullUrl, init);
511
519
  if (!response.ok) {
512
- logError(`AgilityCMS Fetch API ERROR: Request returned a ${response.status} response for ${reqConfig.baseURL}${reqConfig.url}. ${response.statusText}`);
520
+ if (response.status === 404) {
521
+ logInfo({ config: this.config, message: `AgilityCMS Fetch API: Request returned a ${response.status} response for ${reqConfig.baseURL}${reqConfig.url}. ${response.statusText}` });
522
+ return;
523
+ }
524
+ logError({ config: this.config, message: `AgilityCMS Fetch API ERROR: Request returned a ${response.status} response for ${reqConfig.baseURL}${reqConfig.url}. ${response.statusText}` });
513
525
  return;
514
526
  }
515
527
  let data = await response.json();
@@ -518,7 +530,7 @@ var ApiClient = class {
518
530
  }
519
531
  return data;
520
532
  } catch (error) {
521
- logError(`AgilityCMS Fetch API ERROR: Request failed for ${reqConfig.baseURL}${reqConfig.url} ... ${error} ... Does the item exist?`);
533
+ logError({ config: this.config, message: `AgilityCMS Fetch API ERROR: Request failed for ${reqConfig.baseURL}${reqConfig.url} ... ${error}` });
522
534
  }
523
535
  }
524
536
  };
@@ -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
  };
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-rc24",
4
4
  "description": "JavaScript library for the Agility Fetch API (node and browser)",
5
5
  "repository": {
6
6
  "type": "git",