@agility/content-fetch 2.0.14 → 2.0.15-beta.0
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 +14 -0
- package/dist/browser.js +14 -0
- package/dist/node.js +14 -0
- package/dist/types/Config.d.ts +7 -0
- package/dist/utils.d.ts +4 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -59,6 +59,20 @@ const contentItems = await api.getContentItem({
|
|
|
59
59
|
})
|
|
60
60
|
```
|
|
61
61
|
|
|
62
|
+
### Naming the app that makes the requests
|
|
63
|
+
|
|
64
|
+
Set `source` to say which app is calling, so usage can be reported by where it comes from (your production site, a staging site, a mobile app, a build job):
|
|
65
|
+
|
|
66
|
+
```javascript
|
|
67
|
+
const api = agility.getApi({
|
|
68
|
+
guid: "[guid]",
|
|
69
|
+
apiKey: "[your-api-key]",
|
|
70
|
+
source: "prod-website",
|
|
71
|
+
})
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
It is sent on every request as the `Agility-Source` header. It must be at most 64 characters of letters, digits, spaces and `_ . : / -`; anything else throws a `TypeError` from `getApi`. When `source` is not set, no header is sent. It is a label, not a credential: it doesn't change what the API returns or how responses are cached.
|
|
75
|
+
|
|
62
76
|
## Documentation
|
|
63
77
|
|
|
64
78
|
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
|
@@ -98,6 +98,11 @@
|
|
|
98
98
|
}
|
|
99
99
|
return url;
|
|
100
100
|
}
|
|
101
|
+
var SOURCE_HEADER = "Agility-Source";
|
|
102
|
+
var SOURCE_PATTERN = /^[A-Za-z0-9 _.:/-]{0,64}$/;
|
|
103
|
+
function isValidSource(source) {
|
|
104
|
+
return typeof source === "string" && SOURCE_PATTERN.test(source);
|
|
105
|
+
}
|
|
101
106
|
function buildAuthHeader(config) {
|
|
102
107
|
let defaultAuthHeaders = {
|
|
103
108
|
APIKey: config.apiKey,
|
|
@@ -110,6 +115,13 @@
|
|
|
110
115
|
...defaultAuthHeaders,
|
|
111
116
|
...config.headers
|
|
112
117
|
};
|
|
118
|
+
if (typeof config.source === "string" && config.source.length > 0) {
|
|
119
|
+
for (const key of Object.keys(headers)) {
|
|
120
|
+
if (key.toLowerCase() === SOURCE_HEADER.toLowerCase())
|
|
121
|
+
delete headers[key];
|
|
122
|
+
}
|
|
123
|
+
headers[SOURCE_HEADER] = config.source;
|
|
124
|
+
}
|
|
113
125
|
return headers;
|
|
114
126
|
}
|
|
115
127
|
function isHttps(url) {
|
|
@@ -558,6 +570,8 @@
|
|
|
558
570
|
throw new TypeError2("When specifying a cache maxAge, you must set a number value in miliseconds, i.e. 180000 (3 mins).");
|
|
559
571
|
} else if (configParams.baseUrl && !isHttps(configParams.baseUrl)) {
|
|
560
572
|
throw new TypeError2(`When specifying a baseUrl (${configParams.baseUrl}), it must be over HTTPs.`);
|
|
573
|
+
} else if (configParams.source !== void 0 && configParams.source !== null && !isValidSource(configParams.source)) {
|
|
574
|
+
throw new TypeError2('When specifying a source, it must be at most 64 characters of letters, digits, spaces and _ . : / -, i.e. "prod-website".');
|
|
561
575
|
} else {
|
|
562
576
|
return;
|
|
563
577
|
}
|
package/dist/node.js
CHANGED
|
@@ -116,6 +116,11 @@ function buildPathUrl(contentType, referenceName, skip, take, sort, direction, f
|
|
|
116
116
|
}
|
|
117
117
|
return url;
|
|
118
118
|
}
|
|
119
|
+
var SOURCE_HEADER = "Agility-Source";
|
|
120
|
+
var SOURCE_PATTERN = /^[A-Za-z0-9 _.:/-]{0,64}$/;
|
|
121
|
+
function isValidSource(source) {
|
|
122
|
+
return typeof source === "string" && SOURCE_PATTERN.test(source);
|
|
123
|
+
}
|
|
119
124
|
function buildAuthHeader(config) {
|
|
120
125
|
let defaultAuthHeaders = {
|
|
121
126
|
APIKey: config.apiKey,
|
|
@@ -128,6 +133,13 @@ function buildAuthHeader(config) {
|
|
|
128
133
|
...defaultAuthHeaders,
|
|
129
134
|
...config.headers
|
|
130
135
|
};
|
|
136
|
+
if (typeof config.source === "string" && config.source.length > 0) {
|
|
137
|
+
for (const key of Object.keys(headers)) {
|
|
138
|
+
if (key.toLowerCase() === SOURCE_HEADER.toLowerCase())
|
|
139
|
+
delete headers[key];
|
|
140
|
+
}
|
|
141
|
+
headers[SOURCE_HEADER] = config.source;
|
|
142
|
+
}
|
|
131
143
|
return headers;
|
|
132
144
|
}
|
|
133
145
|
function isHttps(url) {
|
|
@@ -576,6 +588,8 @@ function validateConfigParams(configParams) {
|
|
|
576
588
|
throw new TypeError2("When specifying a cache maxAge, you must set a number value in miliseconds, i.e. 180000 (3 mins).");
|
|
577
589
|
} else if (configParams.baseUrl && !isHttps(configParams.baseUrl)) {
|
|
578
590
|
throw new TypeError2(`When specifying a baseUrl (${configParams.baseUrl}), it must be over HTTPs.`);
|
|
591
|
+
} else if (configParams.source !== void 0 && configParams.source !== null && !isValidSource(configParams.source)) {
|
|
592
|
+
throw new TypeError2('When specifying a source, it must be at most 64 characters of letters, digits, spaces and _ . : / -, i.e. "prod-website".');
|
|
579
593
|
} else {
|
|
580
594
|
return;
|
|
581
595
|
}
|
package/dist/types/Config.d.ts
CHANGED
|
@@ -18,6 +18,13 @@ export interface Config {
|
|
|
18
18
|
*/
|
|
19
19
|
apiKey?: string | null;
|
|
20
20
|
locale?: string | null;
|
|
21
|
+
/**
|
|
22
|
+
* Names the app making the requests, such as "prod-website", "staging-website" or "ios-app".
|
|
23
|
+
* Sent on every request as the `Agility-Source` header, so API usage can be reported by where it
|
|
24
|
+
* comes from. At most 64 characters of letters, digits, space and `_ . : / -`. Optional: when it
|
|
25
|
+
* is not set (or is ""), no header is sent. It is a label, not a credential.
|
|
26
|
+
*/
|
|
27
|
+
source?: string | null;
|
|
21
28
|
/**
|
|
22
29
|
* Additional headers to include in the request.
|
|
23
30
|
*/
|
package/dist/utils.d.ts
CHANGED
|
@@ -41,7 +41,10 @@ declare function logDebugDetails({ config, details }: {
|
|
|
41
41
|
}): void;
|
|
42
42
|
declare function buildRequestUrlPath(config: any, locale: any): string;
|
|
43
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;
|
|
44
|
+
/** The request header that carries the `source` setting. */
|
|
45
|
+
declare const SOURCE_HEADER = "Agility-Source";
|
|
46
|
+
declare function isValidSource(source: unknown): boolean;
|
|
44
47
|
declare function buildAuthHeader(config: any): any;
|
|
45
48
|
declare function isHttps(url: any): boolean;
|
|
46
|
-
export { buildPathUrl, buildAuthHeader, buildRequestUrlPath, isHttps, logError, logDebug, logInfo, logWarning, logDebugDetails, headersToObject };
|
|
49
|
+
export { buildPathUrl, buildAuthHeader, buildRequestUrlPath, isValidSource, SOURCE_HEADER, isHttps, logError, logDebug, logInfo, logWarning, logDebugDetails, headersToObject };
|
|
47
50
|
export type { DebugDetails };
|