@agility/content-fetch 2.0.10 → 2.0.12
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/.github/workflows/npm-publish.yml +46 -0
- package/.github/workflows/pr-tests.yml +28 -0
- package/README.md +33 -17
- package/dist/browser.js +115 -5
- package/dist/node.js +115 -5
- package/dist/types/Config.d.ts +9 -1
- package/dist/utils.d.ts +34 -1
- package/package.json +3 -3
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
name: Publish to npm
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- "v*"
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
publish:
|
|
10
|
+
name: Build and publish to npm
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
permissions:
|
|
13
|
+
contents: read
|
|
14
|
+
id-token: write
|
|
15
|
+
|
|
16
|
+
steps:
|
|
17
|
+
- name: Checkout
|
|
18
|
+
uses: actions/checkout@v4
|
|
19
|
+
with:
|
|
20
|
+
fetch-depth: 0
|
|
21
|
+
|
|
22
|
+
- name: Verify tag matches package.json version
|
|
23
|
+
run: |
|
|
24
|
+
TAG_VERSION="${GITHUB_REF_NAME#v}"
|
|
25
|
+
PKG_VERSION=$(node -p "require('./package.json').version")
|
|
26
|
+
if [ "$TAG_VERSION" != "$PKG_VERSION" ]; then
|
|
27
|
+
echo "Tag ($TAG_VERSION) does not match package.json version ($PKG_VERSION)."
|
|
28
|
+
exit 1
|
|
29
|
+
fi
|
|
30
|
+
|
|
31
|
+
- name: Set up Node.js
|
|
32
|
+
uses: actions/setup-node@v4
|
|
33
|
+
with:
|
|
34
|
+
node-version: "24"
|
|
35
|
+
registry-url: "https://registry.npmjs.org"
|
|
36
|
+
|
|
37
|
+
- name: Install dependencies
|
|
38
|
+
run: npm ci
|
|
39
|
+
|
|
40
|
+
- name: Build
|
|
41
|
+
run: npm run build
|
|
42
|
+
|
|
43
|
+
# Always publish under the beta dist-tag. Promote to latest manually:
|
|
44
|
+
# npm dist-tag add @agility/content-fetch@<version> latest
|
|
45
|
+
- name: Publish
|
|
46
|
+
run: npm publish --access public --tag beta
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
name: Run tests on PR
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
pull_request:
|
|
5
|
+
branches:
|
|
6
|
+
- master
|
|
7
|
+
- dev # add whichever branches you want PRs against
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
test:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
|
|
13
|
+
steps:
|
|
14
|
+
- name: Checkout repository
|
|
15
|
+
uses: actions/checkout@v4
|
|
16
|
+
|
|
17
|
+
- name: Set up Node.js
|
|
18
|
+
uses: actions/setup-node@v4
|
|
19
|
+
with:
|
|
20
|
+
node-version: "24"
|
|
21
|
+
cache: "npm"
|
|
22
|
+
|
|
23
|
+
- name: Install dependencies
|
|
24
|
+
run: npm ci
|
|
25
|
+
|
|
26
|
+
- name: Run tests
|
|
27
|
+
run: npm test
|
|
28
|
+
|
package/README.md
CHANGED
|
@@ -5,13 +5,13 @@
|
|
|
5
5
|
|
|
6
6
|
This is the official JavaScript/TypeScript library for accessing live and preview content from your [Agility CMS](https://agilitycms.com) instance.
|
|
7
7
|
|
|
8
|
-
You can use this in both
|
|
8
|
+
You can use this in both Node.js and browser-based JavaScript apps.
|
|
9
9
|
|
|
10
10
|
Don't have an Agility CMS instance? Sign up for [Free Trial](https://agilitycms.com/free) today!
|
|
11
11
|
|
|
12
12
|
## Features
|
|
13
13
|
|
|
14
|
-
-
|
|
14
|
+
- Query the high-availability, CDN-backed Agility Fetch REST API
|
|
15
15
|
- Get a sitemap for a given channel
|
|
16
16
|
- Get a page, including its content zones, modules, and their content
|
|
17
17
|
- Get a content item
|
|
@@ -21,7 +21,7 @@ Don't have an Agility CMS instance? Sign up for [Free Trial](https://agilitycms.
|
|
|
21
21
|
|
|
22
22
|
## Getting Started
|
|
23
23
|
|
|
24
|
-
In order to use this
|
|
24
|
+
In order to use this SDK, you'll need to install the `@agility/content-fetch` package and you'll also need to authenticate your requests.
|
|
25
25
|
|
|
26
26
|
### Prerequisites
|
|
27
27
|
|
|
@@ -61,33 +61,31 @@ const contentItems = await api.getContentItem({
|
|
|
61
61
|
|
|
62
62
|
## Documentation
|
|
63
63
|
|
|
64
|
-
Full documentation for this SDK can be found in our [Agility Fetch JS SDK Reference Doc](https://
|
|
64
|
+
Full documentation for this SDK can be found in our [Agility Fetch JS SDK Reference Doc](https://agilitycms.com/docs/javascript/content-fetch-js-sdk).
|
|
65
65
|
|
|
66
|
-
For docs & help around Agility CMS, please see [Agility CMS Documentation](https://
|
|
66
|
+
For docs & help around Agility CMS, please see [Agility CMS Documentation](https://agilitycms.com/docs)
|
|
67
67
|
|
|
68
68
|
## Tutorials
|
|
69
69
|
|
|
70
|
-
[About the Content Fetch API](https://
|
|
70
|
+
[About using the Content Fetch API and authenticating API calls](https://agilitycms.com/docs/developers/content-fetch-api)
|
|
71
71
|
|
|
72
|
-
[
|
|
72
|
+
[Retrieving your API Key(s), Guid, and API URL](https://agilitycms.com/docs/training-guide/admin-api-keys)
|
|
73
73
|
|
|
74
|
-
[
|
|
74
|
+
[Making your First Call with the Content Fetch API](https://agilitycms.com/docs/training-guide/developer-api-basics)
|
|
75
75
|
|
|
76
|
-
[
|
|
76
|
+
[Calilng the Content Fetch API using the Javascript SDK](https://agilitycms.com/docs/javascript/content-fetch-js-sdk)
|
|
77
77
|
|
|
78
|
-
[
|
|
78
|
+
[Page Management in a Headless CMS](https://agilitycms.com/docs/developers/page-management-in-a-headless-cms)
|
|
79
79
|
|
|
80
|
-
[
|
|
80
|
+
[Using Agility CMS with Next.js](https://agilitycms.com/docs/nextjs)
|
|
81
81
|
|
|
82
|
-
[
|
|
82
|
+
[Creating a Next.js component in Agility CMS](https://agilitycms.com/docs/training-guide/developer-component-development)
|
|
83
83
|
|
|
84
|
-
[Creating a
|
|
84
|
+
[Creating a page model in Agility CMS](https://agilitycms.com/docs/developers/page-models)
|
|
85
85
|
|
|
86
|
-
[
|
|
86
|
+
[Deploying your Agility CMS Next.js app to Vercel](https://agilitycms.com/docs/nextjs/deploying-next-js-to-vercel)
|
|
87
87
|
|
|
88
|
-
[
|
|
89
|
-
|
|
90
|
-
[Content Webhooks](https://help.agilitycms.com/hc/en-us/articles/360035934911)
|
|
88
|
+
[Webhooks](https://agilitycms.com/docs/developers/webhooks)
|
|
91
89
|
|
|
92
90
|
## Contributing
|
|
93
91
|
|
|
@@ -100,3 +98,21 @@ An essential part of contributing to this SDK is adding and running unit tests t
|
|
|
100
98
|
```
|
|
101
99
|
> npm run test
|
|
102
100
|
```
|
|
101
|
+
|
|
102
|
+
### Publishing a New Release
|
|
103
|
+
|
|
104
|
+
Releases are published to npm automatically by [`.github/workflows/npm-publish.yml`](.github/workflows/npm-publish.yml) whenever a `v*` tag is pushed to the repo. To cut a release:
|
|
105
|
+
|
|
106
|
+
1. Bump the `version` field in `package.json` (following [semver](https://semver.org)) and merge that change into `master`.
|
|
107
|
+
2. Tag the merge commit and push the tag:
|
|
108
|
+
```
|
|
109
|
+
git tag v2.0.12
|
|
110
|
+
git push origin v2.0.12
|
|
111
|
+
```
|
|
112
|
+
3. The workflow verifies the tag matches `package.json`'s version, builds the package, and publishes it to npm under the `beta` dist-tag.
|
|
113
|
+
4. Once you've verified the `beta` release, promote it to `latest`:
|
|
114
|
+
```
|
|
115
|
+
npm dist-tag add @agility/content-fetch@2.0.12 latest
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Publishing uses npm's [Trusted Publishing](https://docs.npmjs.com/trusted-publishers) (OIDC) — no `NPM_TOKEN` secret is stored in this repo.
|
package/dist/browser.js
CHANGED
|
@@ -21,6 +21,43 @@
|
|
|
21
21
|
return;
|
|
22
22
|
console.error("\x1B[41m%s\x1B[0m", message);
|
|
23
23
|
}
|
|
24
|
+
var SENSITIVE_HEADERS = ["apikey", "authorization", "x-api-key"];
|
|
25
|
+
function headersToObject(headers) {
|
|
26
|
+
const obj = {};
|
|
27
|
+
if (headers instanceof Headers) {
|
|
28
|
+
headers.forEach((value, key) => {
|
|
29
|
+
obj[key] = value;
|
|
30
|
+
});
|
|
31
|
+
} else {
|
|
32
|
+
Object.keys(headers).forEach((key) => {
|
|
33
|
+
obj[key] = headers[key];
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
return obj;
|
|
37
|
+
}
|
|
38
|
+
function sanitizeHeaders(headers) {
|
|
39
|
+
const sanitized = headersToObject(headers);
|
|
40
|
+
Object.keys(sanitized).forEach((key) => {
|
|
41
|
+
if (SENSITIVE_HEADERS.includes(key.toLowerCase())) {
|
|
42
|
+
sanitized[key] = "***REDACTED***";
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
return sanitized;
|
|
46
|
+
}
|
|
47
|
+
function logDebugDetails({ config, details }) {
|
|
48
|
+
if (!config.debug && config.logLevel !== "debug")
|
|
49
|
+
return;
|
|
50
|
+
const sanitizedDetails = {
|
|
51
|
+
...details,
|
|
52
|
+
requestHeaders: details.requestHeaders ? sanitizeHeaders(details.requestHeaders) : void 0,
|
|
53
|
+
responseHeaders: details.responseHeaders ? sanitizeHeaders(details.responseHeaders) : void 0
|
|
54
|
+
};
|
|
55
|
+
const banner = `=== AgilityCMS Fetch API Debug [${details.type.toUpperCase()}] ===`;
|
|
56
|
+
const log = details.type === "error" ? console.error : console.log;
|
|
57
|
+
log("\x1B[36m%s\x1B[0m", banner);
|
|
58
|
+
log(JSON.stringify(sanitizedDetails, null, 2));
|
|
59
|
+
log("\x1B[36m%s\x1B[0m", "=".repeat(banner.length));
|
|
60
|
+
}
|
|
24
61
|
function buildRequestUrlPath(config, locale) {
|
|
25
62
|
let apiFetchOrPreview = "";
|
|
26
63
|
if (config.isPreview === true || config.isPreview === "true") {
|
|
@@ -572,9 +609,11 @@
|
|
|
572
609
|
//the function that actually makes ALL our requests
|
|
573
610
|
async makeRequest(reqConfig) {
|
|
574
611
|
const isPreview = !!this.config.isPreview;
|
|
575
|
-
|
|
612
|
+
const apiType = isPreview ? "preview" : "fetch";
|
|
613
|
+
const startTime = Date.now();
|
|
614
|
+
const fullUrl = `${reqConfig.baseURL}${reqConfig.url}`;
|
|
615
|
+
logDebug({ config: this.config, message: `AgilityCMS Fetch API LOG: ${fullUrl}` });
|
|
576
616
|
try {
|
|
577
|
-
const fullUrl = `${reqConfig.baseURL}${reqConfig.url}`;
|
|
578
617
|
let init = {
|
|
579
618
|
...this.config.fetchConfig,
|
|
580
619
|
method: "GET",
|
|
@@ -588,22 +627,93 @@
|
|
|
588
627
|
init.cache = "no-store";
|
|
589
628
|
delete init.next;
|
|
590
629
|
}
|
|
630
|
+
logDebugDetails({
|
|
631
|
+
config: this.config,
|
|
632
|
+
details: {
|
|
633
|
+
type: "request",
|
|
634
|
+
url: fullUrl,
|
|
635
|
+
method: init.method,
|
|
636
|
+
guid: this.config.guid,
|
|
637
|
+
apiType,
|
|
638
|
+
requestHeaders: init.headers,
|
|
639
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
640
|
+
}
|
|
641
|
+
});
|
|
591
642
|
const response = await fetch(fullUrl, init);
|
|
643
|
+
const duration = Date.now() - startTime;
|
|
592
644
|
if (!response.ok) {
|
|
645
|
+
let errorBody = "";
|
|
646
|
+
try {
|
|
647
|
+
errorBody = await response.text();
|
|
648
|
+
} catch (e) {
|
|
649
|
+
errorBody = "Unable to read error response body";
|
|
650
|
+
}
|
|
651
|
+
logDebugDetails({
|
|
652
|
+
config: this.config,
|
|
653
|
+
details: {
|
|
654
|
+
type: "error",
|
|
655
|
+
url: fullUrl,
|
|
656
|
+
method: init.method,
|
|
657
|
+
guid: this.config.guid,
|
|
658
|
+
apiType,
|
|
659
|
+
statusCode: response.status,
|
|
660
|
+
statusText: response.statusText,
|
|
661
|
+
requestHeaders: init.headers,
|
|
662
|
+
responseHeaders: headersToObject(response.headers),
|
|
663
|
+
responsePreview: errorBody.substring(0, 2e3),
|
|
664
|
+
responsePreviewTruncated: errorBody.length > 2e3,
|
|
665
|
+
duration,
|
|
666
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
667
|
+
}
|
|
668
|
+
});
|
|
669
|
+
const bodySnippet = errorBody ? ` Response body: ${errorBody.substring(0, 500)}` : "";
|
|
593
670
|
if (response.status === 404) {
|
|
594
|
-
logInfo({ config: this.config, message: `AgilityCMS Fetch API: Request returned a ${response.status} response
|
|
671
|
+
logInfo({ config: this.config, message: `AgilityCMS Fetch API: Request returned a ${response.status} response for ${fullUrl}. ${response.statusText}.${bodySnippet}` });
|
|
595
672
|
return;
|
|
596
673
|
}
|
|
597
|
-
logError({ config: this.config, message: `AgilityCMS Fetch API ERROR: Request returned a ${response.status}
|
|
674
|
+
logError({ config: this.config, message: `AgilityCMS Fetch API ERROR: Request returned a ${response.status} (${response.statusText}) response for ${fullUrl}. Duration: ${duration}ms.${bodySnippet}` });
|
|
598
675
|
return;
|
|
599
676
|
}
|
|
600
677
|
let data = await response.json();
|
|
678
|
+
logDebugDetails({
|
|
679
|
+
config: this.config,
|
|
680
|
+
details: {
|
|
681
|
+
type: "response",
|
|
682
|
+
url: fullUrl,
|
|
683
|
+
method: init.method,
|
|
684
|
+
guid: this.config.guid,
|
|
685
|
+
apiType,
|
|
686
|
+
statusCode: response.status,
|
|
687
|
+
statusText: response.statusText,
|
|
688
|
+
responseHeaders: headersToObject(response.headers),
|
|
689
|
+
contentType: response.headers.get("content-type") ?? void 0,
|
|
690
|
+
contentLength: response.headers.get("content-length") ?? void 0,
|
|
691
|
+
duration,
|
|
692
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
693
|
+
}
|
|
694
|
+
});
|
|
601
695
|
if (this.config.debug) {
|
|
602
696
|
data["agilityResponseHeaders"] = response.headers;
|
|
603
697
|
}
|
|
604
698
|
return data;
|
|
605
699
|
} catch (error) {
|
|
606
|
-
|
|
700
|
+
const duration = Date.now() - startTime;
|
|
701
|
+
logDebugDetails({
|
|
702
|
+
config: this.config,
|
|
703
|
+
details: {
|
|
704
|
+
type: "error",
|
|
705
|
+
url: fullUrl,
|
|
706
|
+
method: "GET",
|
|
707
|
+
guid: this.config.guid,
|
|
708
|
+
apiType,
|
|
709
|
+
errorName: error instanceof Error ? error.name : void 0,
|
|
710
|
+
errorMessage: error instanceof Error ? error.message : String(error),
|
|
711
|
+
errorStack: error instanceof Error ? error.stack : void 0,
|
|
712
|
+
duration,
|
|
713
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
714
|
+
}
|
|
715
|
+
});
|
|
716
|
+
logError({ config: this.config, message: `AgilityCMS Fetch API ERROR: Request failed for ${fullUrl} after ${duration}ms ... ${error}` });
|
|
607
717
|
}
|
|
608
718
|
}
|
|
609
719
|
};
|
package/dist/node.js
CHANGED
|
@@ -39,6 +39,43 @@ function logError({ config, message }) {
|
|
|
39
39
|
return;
|
|
40
40
|
console.error("\x1B[41m%s\x1B[0m", message);
|
|
41
41
|
}
|
|
42
|
+
var SENSITIVE_HEADERS = ["apikey", "authorization", "x-api-key"];
|
|
43
|
+
function headersToObject(headers) {
|
|
44
|
+
const obj = {};
|
|
45
|
+
if (headers instanceof Headers) {
|
|
46
|
+
headers.forEach((value, key) => {
|
|
47
|
+
obj[key] = value;
|
|
48
|
+
});
|
|
49
|
+
} else {
|
|
50
|
+
Object.keys(headers).forEach((key) => {
|
|
51
|
+
obj[key] = headers[key];
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
return obj;
|
|
55
|
+
}
|
|
56
|
+
function sanitizeHeaders(headers) {
|
|
57
|
+
const sanitized = headersToObject(headers);
|
|
58
|
+
Object.keys(sanitized).forEach((key) => {
|
|
59
|
+
if (SENSITIVE_HEADERS.includes(key.toLowerCase())) {
|
|
60
|
+
sanitized[key] = "***REDACTED***";
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
return sanitized;
|
|
64
|
+
}
|
|
65
|
+
function logDebugDetails({ config, details }) {
|
|
66
|
+
if (!config.debug && config.logLevel !== "debug")
|
|
67
|
+
return;
|
|
68
|
+
const sanitizedDetails = {
|
|
69
|
+
...details,
|
|
70
|
+
requestHeaders: details.requestHeaders ? sanitizeHeaders(details.requestHeaders) : void 0,
|
|
71
|
+
responseHeaders: details.responseHeaders ? sanitizeHeaders(details.responseHeaders) : void 0
|
|
72
|
+
};
|
|
73
|
+
const banner = `=== AgilityCMS Fetch API Debug [${details.type.toUpperCase()}] ===`;
|
|
74
|
+
const log = details.type === "error" ? console.error : console.log;
|
|
75
|
+
log("\x1B[36m%s\x1B[0m", banner);
|
|
76
|
+
log(JSON.stringify(sanitizedDetails, null, 2));
|
|
77
|
+
log("\x1B[36m%s\x1B[0m", "=".repeat(banner.length));
|
|
78
|
+
}
|
|
42
79
|
function buildRequestUrlPath(config, locale) {
|
|
43
80
|
let apiFetchOrPreview = "";
|
|
44
81
|
if (config.isPreview === true || config.isPreview === "true") {
|
|
@@ -590,9 +627,11 @@ var ApiClient = class {
|
|
|
590
627
|
//the function that actually makes ALL our requests
|
|
591
628
|
async makeRequest(reqConfig) {
|
|
592
629
|
const isPreview = !!this.config.isPreview;
|
|
593
|
-
|
|
630
|
+
const apiType = isPreview ? "preview" : "fetch";
|
|
631
|
+
const startTime = Date.now();
|
|
632
|
+
const fullUrl = `${reqConfig.baseURL}${reqConfig.url}`;
|
|
633
|
+
logDebug({ config: this.config, message: `AgilityCMS Fetch API LOG: ${fullUrl}` });
|
|
594
634
|
try {
|
|
595
|
-
const fullUrl = `${reqConfig.baseURL}${reqConfig.url}`;
|
|
596
635
|
let init = {
|
|
597
636
|
...this.config.fetchConfig,
|
|
598
637
|
method: "GET",
|
|
@@ -606,22 +645,93 @@ var ApiClient = class {
|
|
|
606
645
|
init.cache = "no-store";
|
|
607
646
|
delete init.next;
|
|
608
647
|
}
|
|
648
|
+
logDebugDetails({
|
|
649
|
+
config: this.config,
|
|
650
|
+
details: {
|
|
651
|
+
type: "request",
|
|
652
|
+
url: fullUrl,
|
|
653
|
+
method: init.method,
|
|
654
|
+
guid: this.config.guid,
|
|
655
|
+
apiType,
|
|
656
|
+
requestHeaders: init.headers,
|
|
657
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
658
|
+
}
|
|
659
|
+
});
|
|
609
660
|
const response = await fetch(fullUrl, init);
|
|
661
|
+
const duration = Date.now() - startTime;
|
|
610
662
|
if (!response.ok) {
|
|
663
|
+
let errorBody = "";
|
|
664
|
+
try {
|
|
665
|
+
errorBody = await response.text();
|
|
666
|
+
} catch (e) {
|
|
667
|
+
errorBody = "Unable to read error response body";
|
|
668
|
+
}
|
|
669
|
+
logDebugDetails({
|
|
670
|
+
config: this.config,
|
|
671
|
+
details: {
|
|
672
|
+
type: "error",
|
|
673
|
+
url: fullUrl,
|
|
674
|
+
method: init.method,
|
|
675
|
+
guid: this.config.guid,
|
|
676
|
+
apiType,
|
|
677
|
+
statusCode: response.status,
|
|
678
|
+
statusText: response.statusText,
|
|
679
|
+
requestHeaders: init.headers,
|
|
680
|
+
responseHeaders: headersToObject(response.headers),
|
|
681
|
+
responsePreview: errorBody.substring(0, 2e3),
|
|
682
|
+
responsePreviewTruncated: errorBody.length > 2e3,
|
|
683
|
+
duration,
|
|
684
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
685
|
+
}
|
|
686
|
+
});
|
|
687
|
+
const bodySnippet = errorBody ? ` Response body: ${errorBody.substring(0, 500)}` : "";
|
|
611
688
|
if (response.status === 404) {
|
|
612
|
-
logInfo({ config: this.config, message: `AgilityCMS Fetch API: Request returned a ${response.status} response
|
|
689
|
+
logInfo({ config: this.config, message: `AgilityCMS Fetch API: Request returned a ${response.status} response for ${fullUrl}. ${response.statusText}.${bodySnippet}` });
|
|
613
690
|
return;
|
|
614
691
|
}
|
|
615
|
-
logError({ config: this.config, message: `AgilityCMS Fetch API ERROR: Request returned a ${response.status}
|
|
692
|
+
logError({ config: this.config, message: `AgilityCMS Fetch API ERROR: Request returned a ${response.status} (${response.statusText}) response for ${fullUrl}. Duration: ${duration}ms.${bodySnippet}` });
|
|
616
693
|
return;
|
|
617
694
|
}
|
|
618
695
|
let data = await response.json();
|
|
696
|
+
logDebugDetails({
|
|
697
|
+
config: this.config,
|
|
698
|
+
details: {
|
|
699
|
+
type: "response",
|
|
700
|
+
url: fullUrl,
|
|
701
|
+
method: init.method,
|
|
702
|
+
guid: this.config.guid,
|
|
703
|
+
apiType,
|
|
704
|
+
statusCode: response.status,
|
|
705
|
+
statusText: response.statusText,
|
|
706
|
+
responseHeaders: headersToObject(response.headers),
|
|
707
|
+
contentType: response.headers.get("content-type") ?? void 0,
|
|
708
|
+
contentLength: response.headers.get("content-length") ?? void 0,
|
|
709
|
+
duration,
|
|
710
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
711
|
+
}
|
|
712
|
+
});
|
|
619
713
|
if (this.config.debug) {
|
|
620
714
|
data["agilityResponseHeaders"] = response.headers;
|
|
621
715
|
}
|
|
622
716
|
return data;
|
|
623
717
|
} catch (error) {
|
|
624
|
-
|
|
718
|
+
const duration = Date.now() - startTime;
|
|
719
|
+
logDebugDetails({
|
|
720
|
+
config: this.config,
|
|
721
|
+
details: {
|
|
722
|
+
type: "error",
|
|
723
|
+
url: fullUrl,
|
|
724
|
+
method: "GET",
|
|
725
|
+
guid: this.config.guid,
|
|
726
|
+
apiType,
|
|
727
|
+
errorName: error instanceof Error ? error.name : void 0,
|
|
728
|
+
errorMessage: error instanceof Error ? error.message : String(error),
|
|
729
|
+
errorStack: error instanceof Error ? error.stack : void 0,
|
|
730
|
+
duration,
|
|
731
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
732
|
+
}
|
|
733
|
+
});
|
|
734
|
+
logError({ config: this.config, message: `AgilityCMS Fetch API ERROR: Request failed for ${fullUrl} after ${duration}ms ... ${error}` });
|
|
625
735
|
}
|
|
626
736
|
}
|
|
627
737
|
};
|
package/dist/types/Config.d.ts
CHANGED
|
@@ -30,7 +30,15 @@ export interface Config {
|
|
|
30
30
|
*/
|
|
31
31
|
logLevel?: 'debug' | 'info' | 'warn' | 'error' | 'silent';
|
|
32
32
|
/**
|
|
33
|
-
*
|
|
33
|
+
* Enable debug mode for detailed logging. Default is false.
|
|
34
|
+
* Detailed logging is also enabled when logLevel is set to 'debug'.
|
|
35
|
+
* When enabled:
|
|
36
|
+
* - Adds comprehensive structured request/response/error logging with timing information,
|
|
37
|
+
* instance guid, api type (fetch/preview), and request/response headers
|
|
38
|
+
* - Logs error details including status codes, headers, response body previews,
|
|
39
|
+
* and exception name/message/stack
|
|
40
|
+
* - Sanitizes sensitive data (API keys, auth tokens) in debug output
|
|
41
|
+
* - When debug is true, also includes response headers in the returned data as 'agilityResponseHeaders'
|
|
34
42
|
*/
|
|
35
43
|
debug?: boolean;
|
|
36
44
|
/**
|
package/dist/utils.d.ts
CHANGED
|
@@ -7,8 +7,41 @@ declare function logDebug({ config, message }: LogProps): void;
|
|
|
7
7
|
declare function logInfo({ config, message }: LogProps): void;
|
|
8
8
|
declare function logWarning({ config, message }: LogProps): void;
|
|
9
9
|
declare function logError({ config, message }: LogProps): void;
|
|
10
|
+
interface DebugDetails {
|
|
11
|
+
type: 'request' | 'response' | 'error';
|
|
12
|
+
url?: string;
|
|
13
|
+
method?: string;
|
|
14
|
+
guid?: string | null;
|
|
15
|
+
apiType?: 'fetch' | 'preview';
|
|
16
|
+
requestHeaders?: Record<string, any>;
|
|
17
|
+
responseHeaders?: Record<string, any>;
|
|
18
|
+
statusCode?: number;
|
|
19
|
+
statusText?: string;
|
|
20
|
+
contentType?: string;
|
|
21
|
+
contentLength?: string;
|
|
22
|
+
duration?: number;
|
|
23
|
+
timestamp?: string;
|
|
24
|
+
errorName?: string;
|
|
25
|
+
errorMessage?: string;
|
|
26
|
+
errorStack?: string;
|
|
27
|
+
responsePreview?: string;
|
|
28
|
+
responsePreviewTruncated?: boolean;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Converts a fetch Headers instance (or plain object) to a plain object
|
|
32
|
+
*/
|
|
33
|
+
declare function headersToObject(headers: Record<string, any> | Headers): Record<string, any>;
|
|
34
|
+
/**
|
|
35
|
+
* Logs detailed debug information about requests, responses, and errors
|
|
36
|
+
* in a uniform structured format. Enabled by config.debug or logLevel 'debug'.
|
|
37
|
+
*/
|
|
38
|
+
declare function logDebugDetails({ config, details }: {
|
|
39
|
+
config: Config;
|
|
40
|
+
details: DebugDetails;
|
|
41
|
+
}): void;
|
|
10
42
|
declare function buildRequestUrlPath(config: any, locale: any): string;
|
|
11
43
|
declare function buildPathUrl(contentType: any, referenceName: any, skip: any, take: any, sort: any, direction: any, filters: any, filtersLogicOperator: any, filterString: any, contentLinkDepth: any, expandAllContentLinks: any): string;
|
|
12
44
|
declare function buildAuthHeader(config: any): any;
|
|
13
45
|
declare function isHttps(url: any): boolean;
|
|
14
|
-
export { buildPathUrl, buildAuthHeader, buildRequestUrlPath, isHttps, logError, logDebug, logInfo, logWarning };
|
|
46
|
+
export { buildPathUrl, buildAuthHeader, buildRequestUrlPath, isHttps, logError, logDebug, logInfo, logWarning, logDebugDetails, headersToObject };
|
|
47
|
+
export type { DebugDetails };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agility/content-fetch",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.12",
|
|
4
4
|
"description": "JS/TS library for the Agility Fetch API",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"scripts": {
|
|
21
21
|
"clean": "rimraf dist",
|
|
22
22
|
"build": "node build.js",
|
|
23
|
-
"prepare": "
|
|
23
|
+
"prepare": "npm run build",
|
|
24
24
|
"test": "jest",
|
|
25
25
|
"generate-docs": "node_modules/.bin/jsdoc --configure .jsdoc.json --verbose"
|
|
26
26
|
},
|
|
@@ -50,4 +50,4 @@
|
|
|
50
50
|
"ts-jest": "^29.1.1",
|
|
51
51
|
"typescript": "^5.1.6"
|
|
52
52
|
}
|
|
53
|
-
}
|
|
53
|
+
}
|