@agility/content-fetch 2.0.14 → 2.0.15
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 +26 -0
- package/dist/browser.js +28 -3
- package/dist/index.d.ts +2 -1
- package/dist/methods/getContentList.d.ts +6 -0
- package/dist/node.js +30 -3
- package/dist/types/Config.d.ts +14 -0
- package/dist/types/errors/Errors.d.ts +11 -0
- package/dist/utils.d.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -59,6 +59,32 @@ const contentItems = await api.getContentItem({
|
|
|
59
59
|
})
|
|
60
60
|
```
|
|
61
61
|
|
|
62
|
+
### Telling "not found" apart from "failed" (`throwOnError`)
|
|
63
|
+
|
|
64
|
+
By default a request that fails (a 5xx, a timeout, a network error, a body that isn't JSON) returns `undefined`, the same as a 404. With a data cache (Next.js ISR or `"use cache"`), that `undefined` can be cached, and one failed request becomes a lasting "not found".
|
|
65
|
+
|
|
66
|
+
Set `throwOnError: true` to tell them apart. A 404 still returns `undefined`; anything else throws a `FetchApiError` with `status` (undefined when no response came back) and `url`, so nothing is cached and the next request tries again. It is off by default, so existing code is unchanged.
|
|
67
|
+
|
|
68
|
+
```javascript
|
|
69
|
+
import agility, { FetchApiError } from "@agility/content-fetch"
|
|
70
|
+
|
|
71
|
+
const api = agility.getApi({ guid: "[guid]", apiKey: "[your-api-key]", throwOnError: true })
|
|
72
|
+
|
|
73
|
+
const page = await api.getPage({ pageID: 2, locale: "en-us" }) // undefined only for a 404
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Returning only some fields (`fields`)
|
|
77
|
+
|
|
78
|
+
`getContentList` takes `fields`, a comma separated list of the fields to return on each item. Names are case-sensitive and match the API's casing. Filters, sort, paging and `totalCount` are unaffected.
|
|
79
|
+
|
|
80
|
+
```javascript
|
|
81
|
+
const posts = await api.getContentList({
|
|
82
|
+
referenceName: "posts",
|
|
83
|
+
locale: "en-us",
|
|
84
|
+
fields: "fields.title,fields.uRL",
|
|
85
|
+
})
|
|
86
|
+
```
|
|
87
|
+
|
|
62
88
|
## Documentation
|
|
63
89
|
|
|
64
90
|
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).
|
package/dist/browser.js
CHANGED
|
@@ -68,7 +68,7 @@
|
|
|
68
68
|
let urlPath = `${config.baseUrl}/${apiFetchOrPreview}/${locale}`;
|
|
69
69
|
return urlPath;
|
|
70
70
|
}
|
|
71
|
-
function buildPathUrl(contentType, referenceName, skip, take, sort, direction, filters, filtersLogicOperator, filterString, contentLinkDepth, expandAllContentLinks) {
|
|
71
|
+
function buildPathUrl(contentType, referenceName, skip, take, sort, direction, filters, filtersLogicOperator, filterString, contentLinkDepth, expandAllContentLinks, fields) {
|
|
72
72
|
let url = `/${contentType}/${referenceName}?contentLinkDepth=${contentLinkDepth}&`;
|
|
73
73
|
filtersLogicOperator = filtersLogicOperator ? ` ${filtersLogicOperator} ` : " AND ";
|
|
74
74
|
if (sort) {
|
|
@@ -96,6 +96,9 @@
|
|
|
96
96
|
if (expandAllContentLinks) {
|
|
97
97
|
url += `expandAllContentLinks=${expandAllContentLinks}&`;
|
|
98
98
|
}
|
|
99
|
+
if (fields) {
|
|
100
|
+
url += `fields=${encodeURIComponent(fields)}&`;
|
|
101
|
+
}
|
|
99
102
|
return url;
|
|
100
103
|
}
|
|
101
104
|
function buildAuthHeader(config) {
|
|
@@ -130,6 +133,16 @@
|
|
|
130
133
|
Error.captureStackTrace?.(this, asserter || this.constructor);
|
|
131
134
|
}
|
|
132
135
|
};
|
|
136
|
+
var FetchApiError = class extends Error {
|
|
137
|
+
constructor(message, url, status, cause) {
|
|
138
|
+
super(message);
|
|
139
|
+
this.name = "FetchApiError";
|
|
140
|
+
this.url = url;
|
|
141
|
+
this.status = status;
|
|
142
|
+
if (cause !== void 0)
|
|
143
|
+
this.cause = cause;
|
|
144
|
+
}
|
|
145
|
+
};
|
|
133
146
|
|
|
134
147
|
// src/methods/getSitemapFlat.ts
|
|
135
148
|
function getSitemapFlat(requestParams) {
|
|
@@ -213,7 +226,7 @@
|
|
|
213
226
|
requestParams.referenceName = sanitizeReferenceName(requestParams.referenceName);
|
|
214
227
|
requestParams = { ...defaultParams2, ...requestParams };
|
|
215
228
|
const req = {
|
|
216
|
-
url: buildPathUrl("list", requestParams.referenceName, requestParams.skip, requestParams.take, requestParams.sort, requestParams.direction, requestParams.filters, requestParams.filtersLogicOperator, requestParams.filterString, requestParams.contentLinkDepth, requestParams.expandAllContentLinks),
|
|
229
|
+
url: buildPathUrl("list", requestParams.referenceName, requestParams.skip, requestParams.take, requestParams.sort, requestParams.direction, requestParams.filters, requestParams.filtersLogicOperator, requestParams.filterString, requestParams.contentLinkDepth, requestParams.expandAllContentLinks, requestParams.fields),
|
|
217
230
|
method: "get",
|
|
218
231
|
baseURL: buildRequestUrlPath(this.config, requestParams.locale ? requestParams.locale : requestParams.languageCode),
|
|
219
232
|
headers: buildAuthHeader(this.config),
|
|
@@ -271,6 +284,9 @@
|
|
|
271
284
|
} else if (requestParams.expandAllContentLinks && typeof requestParams.expandAllContentLinks !== "boolean") {
|
|
272
285
|
throw new TypeError2("ExpandAllContentLinks parameter must be a value of true or false", validateRequestParams4);
|
|
273
286
|
}
|
|
287
|
+
if (requestParams.fields !== void 0 && requestParams.fields !== null && typeof requestParams.fields !== "string") {
|
|
288
|
+
throw new TypeError2('Fields parameter must be a comma separated string, for example "fields.title,fields.uRL"', validateRequestParams4);
|
|
289
|
+
}
|
|
274
290
|
return true;
|
|
275
291
|
}
|
|
276
292
|
var defaultParams2 = {
|
|
@@ -672,6 +688,9 @@
|
|
|
672
688
|
return;
|
|
673
689
|
}
|
|
674
690
|
logError({ config: this.config, message: `AgilityCMS Fetch API ERROR: Request returned a ${response.status} (${response.statusText}) response for ${fullUrl}. Duration: ${duration}ms.${bodySnippet}` });
|
|
691
|
+
if (this.config.throwOnError) {
|
|
692
|
+
throw new FetchApiError(`AgilityCMS Fetch API: ${response.status} ${response.statusText} for ${fullUrl}`, fullUrl, response.status);
|
|
693
|
+
}
|
|
675
694
|
return;
|
|
676
695
|
}
|
|
677
696
|
let data = await response.json();
|
|
@@ -697,6 +716,8 @@
|
|
|
697
716
|
}
|
|
698
717
|
return data;
|
|
699
718
|
} catch (error) {
|
|
719
|
+
if (error instanceof FetchApiError)
|
|
720
|
+
throw error;
|
|
700
721
|
const duration = Date.now() - startTime;
|
|
701
722
|
logDebugDetails({
|
|
702
723
|
config: this.config,
|
|
@@ -714,6 +735,9 @@
|
|
|
714
735
|
}
|
|
715
736
|
});
|
|
716
737
|
logError({ config: this.config, message: `AgilityCMS Fetch API ERROR: Request failed for ${fullUrl} after ${duration}ms ... ${error}` });
|
|
738
|
+
if (this.config.throwOnError) {
|
|
739
|
+
throw new FetchApiError(`AgilityCMS Fetch API: request failed for ${fullUrl}: ${error}`, fullUrl, void 0, error);
|
|
740
|
+
}
|
|
717
741
|
}
|
|
718
742
|
}
|
|
719
743
|
};
|
|
@@ -726,6 +750,7 @@
|
|
|
726
750
|
|
|
727
751
|
// src/index.ts
|
|
728
752
|
module.exports = {
|
|
729
|
-
getApi
|
|
753
|
+
getApi,
|
|
754
|
+
FetchApiError
|
|
730
755
|
};
|
|
731
756
|
})();
|
package/dist/index.d.ts
CHANGED
|
@@ -10,4 +10,5 @@ import { SEOProperties, SEORobots } from "./types/SEO";
|
|
|
10
10
|
import { ApiClientInstance, getApi } from "./api-client";
|
|
11
11
|
import { ContentReference } from "./types/ContentReference";
|
|
12
12
|
export type { ApiClientInstance, Config, ContentItem, ContentList, ContentReference, Filter, Gallery, Page, PageScripts, GeoProperties, GeoAi, SchemaType, LlmsInclude, AiCrawl, StructuredData, SEOProperties, SEORobots };
|
|
13
|
-
|
|
13
|
+
import { FetchApiError } from "./types/errors/Errors";
|
|
14
|
+
export { getApi, FetchApiError };
|
|
@@ -109,6 +109,12 @@ export interface ContentListRequestParams {
|
|
|
109
109
|
*/
|
|
110
110
|
filters?: Array<Filter>;
|
|
111
111
|
filtersLogicOperator?: FilterLogicOperator;
|
|
112
|
+
/**
|
|
113
|
+
* A comma separated list of the fields to return, for example "fields.title,fields.uRL".
|
|
114
|
+
* Only those fields come back on each item. Names are case-sensitive and must match the
|
|
115
|
+
* API's casing. Filters, sort, paging and totalCount are unaffected.
|
|
116
|
+
*/
|
|
117
|
+
fields?: string;
|
|
112
118
|
}
|
|
113
119
|
declare function getContentList(this: ApiClientInstance, requestParams: ContentListRequestParams): Promise<ContentList>;
|
|
114
120
|
export default getContentList;
|
package/dist/node.js
CHANGED
|
@@ -19,6 +19,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
19
19
|
// src/index.ts
|
|
20
20
|
var src_exports = {};
|
|
21
21
|
__export(src_exports, {
|
|
22
|
+
FetchApiError: () => FetchApiError,
|
|
22
23
|
getApi: () => getApi
|
|
23
24
|
});
|
|
24
25
|
module.exports = __toCommonJS(src_exports);
|
|
@@ -86,7 +87,7 @@ function buildRequestUrlPath(config, locale) {
|
|
|
86
87
|
let urlPath = `${config.baseUrl}/${apiFetchOrPreview}/${locale}`;
|
|
87
88
|
return urlPath;
|
|
88
89
|
}
|
|
89
|
-
function buildPathUrl(contentType, referenceName, skip, take, sort, direction, filters, filtersLogicOperator, filterString, contentLinkDepth, expandAllContentLinks) {
|
|
90
|
+
function buildPathUrl(contentType, referenceName, skip, take, sort, direction, filters, filtersLogicOperator, filterString, contentLinkDepth, expandAllContentLinks, fields) {
|
|
90
91
|
let url = `/${contentType}/${referenceName}?contentLinkDepth=${contentLinkDepth}&`;
|
|
91
92
|
filtersLogicOperator = filtersLogicOperator ? ` ${filtersLogicOperator} ` : " AND ";
|
|
92
93
|
if (sort) {
|
|
@@ -114,6 +115,9 @@ function buildPathUrl(contentType, referenceName, skip, take, sort, direction, f
|
|
|
114
115
|
if (expandAllContentLinks) {
|
|
115
116
|
url += `expandAllContentLinks=${expandAllContentLinks}&`;
|
|
116
117
|
}
|
|
118
|
+
if (fields) {
|
|
119
|
+
url += `fields=${encodeURIComponent(fields)}&`;
|
|
120
|
+
}
|
|
117
121
|
return url;
|
|
118
122
|
}
|
|
119
123
|
function buildAuthHeader(config) {
|
|
@@ -148,6 +152,16 @@ var TypeError2 = class extends Error {
|
|
|
148
152
|
Error.captureStackTrace?.(this, asserter || this.constructor);
|
|
149
153
|
}
|
|
150
154
|
};
|
|
155
|
+
var FetchApiError = class extends Error {
|
|
156
|
+
constructor(message, url, status, cause) {
|
|
157
|
+
super(message);
|
|
158
|
+
this.name = "FetchApiError";
|
|
159
|
+
this.url = url;
|
|
160
|
+
this.status = status;
|
|
161
|
+
if (cause !== void 0)
|
|
162
|
+
this.cause = cause;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
151
165
|
|
|
152
166
|
// src/methods/getSitemapFlat.ts
|
|
153
167
|
function getSitemapFlat(requestParams) {
|
|
@@ -231,7 +245,7 @@ function getContentList(requestParams) {
|
|
|
231
245
|
requestParams.referenceName = sanitizeReferenceName(requestParams.referenceName);
|
|
232
246
|
requestParams = { ...defaultParams2, ...requestParams };
|
|
233
247
|
const req = {
|
|
234
|
-
url: buildPathUrl("list", requestParams.referenceName, requestParams.skip, requestParams.take, requestParams.sort, requestParams.direction, requestParams.filters, requestParams.filtersLogicOperator, requestParams.filterString, requestParams.contentLinkDepth, requestParams.expandAllContentLinks),
|
|
248
|
+
url: buildPathUrl("list", requestParams.referenceName, requestParams.skip, requestParams.take, requestParams.sort, requestParams.direction, requestParams.filters, requestParams.filtersLogicOperator, requestParams.filterString, requestParams.contentLinkDepth, requestParams.expandAllContentLinks, requestParams.fields),
|
|
235
249
|
method: "get",
|
|
236
250
|
baseURL: buildRequestUrlPath(this.config, requestParams.locale ? requestParams.locale : requestParams.languageCode),
|
|
237
251
|
headers: buildAuthHeader(this.config),
|
|
@@ -289,6 +303,9 @@ function validateRequestParams4(requestParams) {
|
|
|
289
303
|
} else if (requestParams.expandAllContentLinks && typeof requestParams.expandAllContentLinks !== "boolean") {
|
|
290
304
|
throw new TypeError2("ExpandAllContentLinks parameter must be a value of true or false", validateRequestParams4);
|
|
291
305
|
}
|
|
306
|
+
if (requestParams.fields !== void 0 && requestParams.fields !== null && typeof requestParams.fields !== "string") {
|
|
307
|
+
throw new TypeError2('Fields parameter must be a comma separated string, for example "fields.title,fields.uRL"', validateRequestParams4);
|
|
308
|
+
}
|
|
292
309
|
return true;
|
|
293
310
|
}
|
|
294
311
|
var defaultParams2 = {
|
|
@@ -690,6 +707,9 @@ var ApiClient = class {
|
|
|
690
707
|
return;
|
|
691
708
|
}
|
|
692
709
|
logError({ config: this.config, message: `AgilityCMS Fetch API ERROR: Request returned a ${response.status} (${response.statusText}) response for ${fullUrl}. Duration: ${duration}ms.${bodySnippet}` });
|
|
710
|
+
if (this.config.throwOnError) {
|
|
711
|
+
throw new FetchApiError(`AgilityCMS Fetch API: ${response.status} ${response.statusText} for ${fullUrl}`, fullUrl, response.status);
|
|
712
|
+
}
|
|
693
713
|
return;
|
|
694
714
|
}
|
|
695
715
|
let data = await response.json();
|
|
@@ -715,6 +735,8 @@ var ApiClient = class {
|
|
|
715
735
|
}
|
|
716
736
|
return data;
|
|
717
737
|
} catch (error) {
|
|
738
|
+
if (error instanceof FetchApiError)
|
|
739
|
+
throw error;
|
|
718
740
|
const duration = Date.now() - startTime;
|
|
719
741
|
logDebugDetails({
|
|
720
742
|
config: this.config,
|
|
@@ -732,6 +754,9 @@ var ApiClient = class {
|
|
|
732
754
|
}
|
|
733
755
|
});
|
|
734
756
|
logError({ config: this.config, message: `AgilityCMS Fetch API ERROR: Request failed for ${fullUrl} after ${duration}ms ... ${error}` });
|
|
757
|
+
if (this.config.throwOnError) {
|
|
758
|
+
throw new FetchApiError(`AgilityCMS Fetch API: request failed for ${fullUrl}: ${error}`, fullUrl, void 0, error);
|
|
759
|
+
}
|
|
735
760
|
}
|
|
736
761
|
}
|
|
737
762
|
};
|
|
@@ -744,9 +769,11 @@ function getApi(config) {
|
|
|
744
769
|
|
|
745
770
|
// src/index.ts
|
|
746
771
|
module.exports = {
|
|
747
|
-
getApi
|
|
772
|
+
getApi,
|
|
773
|
+
FetchApiError
|
|
748
774
|
};
|
|
749
775
|
// Annotate the CommonJS export names for ESM import in node:
|
|
750
776
|
0 && (module.exports = {
|
|
777
|
+
FetchApiError,
|
|
751
778
|
getApi
|
|
752
779
|
});
|
package/dist/types/Config.d.ts
CHANGED
|
@@ -48,5 +48,19 @@ export interface Config {
|
|
|
48
48
|
caching?: {
|
|
49
49
|
maxAge?: number;
|
|
50
50
|
};
|
|
51
|
+
/**
|
|
52
|
+
* Throw when a request fails, instead of returning `undefined`. Default is false.
|
|
53
|
+
*
|
|
54
|
+
* By default every failure (a 5xx, a timeout, a network error, a body that isn't JSON)
|
|
55
|
+
* returns `undefined`, the same as a 404. With a data cache (Next.js ISR or "use cache"),
|
|
56
|
+
* that `undefined` can be cached, so one failed request becomes a lasting "not found".
|
|
57
|
+
*
|
|
58
|
+
* When true:
|
|
59
|
+
* - a 404 still returns `undefined` (the item, page or sitemap doesn't exist);
|
|
60
|
+
* - anything else throws a `FetchApiError` with the `status` (undefined when no response
|
|
61
|
+
* came back or the body wasn't JSON) and the request `url`, so the render fails,
|
|
62
|
+
* nothing is cached, and the next request tries again.
|
|
63
|
+
*/
|
|
64
|
+
throwOnError?: boolean;
|
|
51
65
|
fetchConfig?: any;
|
|
52
66
|
}
|
|
@@ -17,6 +17,17 @@ export declare class TypeError extends Error {
|
|
|
17
17
|
*/
|
|
18
18
|
constructor(message: string, asserter?: any);
|
|
19
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* Thrown by a client created with `throwOnError: true` when a request fails for any
|
|
22
|
+
* reason other than a 404.
|
|
23
|
+
*/
|
|
24
|
+
export declare class FetchApiError extends Error {
|
|
25
|
+
/** The HTTP status, or undefined when no response came back (network error, timeout) or the body wasn't JSON. */
|
|
26
|
+
status?: number;
|
|
27
|
+
/** The request URL. */
|
|
28
|
+
url: string;
|
|
29
|
+
constructor(message: string, url: string, status?: number, cause?: unknown);
|
|
30
|
+
}
|
|
20
31
|
export declare class URIError extends Error {
|
|
21
32
|
constructor(message?: string, asserter?: any);
|
|
22
33
|
}
|
package/dist/utils.d.ts
CHANGED
|
@@ -40,7 +40,7 @@ declare function logDebugDetails({ config, details }: {
|
|
|
40
40
|
details: DebugDetails;
|
|
41
41
|
}): void;
|
|
42
42
|
declare function buildRequestUrlPath(config: any, locale: any): string;
|
|
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;
|
|
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, fields?: any): string;
|
|
44
44
|
declare function buildAuthHeader(config: any): any;
|
|
45
45
|
declare function isHttps(url: any): boolean;
|
|
46
46
|
export { buildPathUrl, buildAuthHeader, buildRequestUrlPath, isHttps, logError, logDebug, logInfo, logWarning, logDebugDetails, headersToObject };
|