canada-api 5.1.5 → 5.1.7
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 +10 -10
- package/dist/ca.js +1 -1
- package/package.json +1 -1
- package/src/children.js +52 -48
- package/src/content.js +21 -20
- package/src/index.js +24 -24
- package/src/meta.js +105 -102
- package/src/normalize.js +43 -37
- package/src/request.js +10 -5
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@ Cross platform API for fetching public data from [canada.ca](https://www.canada.
|
|
|
9
9
|
## Browser
|
|
10
10
|
|
|
11
11
|
```html
|
|
12
|
-
<script src="https://cdn.jsdelivr.net/npm/canada-api@5.1.
|
|
12
|
+
<script src="https://cdn.jsdelivr.net/npm/canada-api@5.1.7"></script>
|
|
13
13
|
```
|
|
14
14
|
|
|
15
15
|
## Node 18+
|
|
@@ -72,9 +72,9 @@ Fetches and parses the sitemap for the given page, returning its child pages. En
|
|
|
72
72
|
### `ca.content(url)`
|
|
73
73
|
|
|
74
74
|
- `url` {string|URL} - Absolute or relative URL
|
|
75
|
-
- Returns: {Promise} Fulfills with a response whose `data` is the raw HTML string
|
|
75
|
+
- Returns: {Promise} Fulfills with a response whose `data` is the raw HTML string, or parsed JSON for DAM asset URLs
|
|
76
76
|
|
|
77
|
-
Retrieves the HTML content of the page.
|
|
77
|
+
Retrieves the HTML content of the page. DAM asset URLs under `/content/dam/` are passed through without forcing a `.html` suffix.
|
|
78
78
|
|
|
79
79
|
```json
|
|
80
80
|
{
|
|
@@ -90,9 +90,9 @@ Retrieves the HTML content of the page.
|
|
|
90
90
|
### `ca.meta(url)`
|
|
91
91
|
|
|
92
92
|
- `url` {string|URL} - Absolute or relative URL
|
|
93
|
-
- Returns: {Promise} Fulfills with a response whose `data` is a formatted metadata object
|
|
93
|
+
- Returns: {Promise} Fulfills with a response whose `data` is a formatted metadata object, or parsed JSON for DAM asset URLs
|
|
94
94
|
|
|
95
|
-
Fetches JCR metadata for the given page. The following transformations are applied:
|
|
95
|
+
Fetches JCR metadata for the given page. For DAM asset URLs under `/content/dam/`, the asset JSON response is returned. The following transformations are applied to page metadata:
|
|
96
96
|
|
|
97
97
|
- String `"true"` / `"false"` values are converted to booleans
|
|
98
98
|
- `@TypeHint` properties are removed
|
|
@@ -152,7 +152,7 @@ API multiplateforme pour récupérer des données publiques de [canada.ca](https
|
|
|
152
152
|
## Navigateur
|
|
153
153
|
|
|
154
154
|
```html
|
|
155
|
-
<script src="https://cdn.jsdelivr.net/npm/canada-api@5.1.
|
|
155
|
+
<script src="https://cdn.jsdelivr.net/npm/canada-api@5.1.7"></script>
|
|
156
156
|
```
|
|
157
157
|
|
|
158
158
|
## Node 18+
|
|
@@ -207,9 +207,9 @@ Récupère et analyse le plan de site de la page donnée, retournant ses pages e
|
|
|
207
207
|
### `ca.content(url)`
|
|
208
208
|
|
|
209
209
|
- `url` {string|URL} - URL absolue ou relative
|
|
210
|
-
- Retourne: {Promise} Résout avec une réponse dont `data` est la chaîne HTML brute
|
|
210
|
+
- Retourne: {Promise} Résout avec une réponse dont `data` est la chaîne HTML brute, ou le JSON analysé pour les URL d'actifs DAM
|
|
211
211
|
|
|
212
|
-
Récupère le contenu HTML de la page.
|
|
212
|
+
Récupère le contenu HTML de la page. Les URL d'actifs DAM sous `/content/dam/` sont transmises sans forcer le suffixe `.html`.
|
|
213
213
|
|
|
214
214
|
```json
|
|
215
215
|
{
|
|
@@ -225,9 +225,9 @@ Récupère le contenu HTML de la page.
|
|
|
225
225
|
### `ca.meta(url)`
|
|
226
226
|
|
|
227
227
|
- `url` {string|URL} - URL absolue ou relative
|
|
228
|
-
- Retourne: {Promise} Résout avec une réponse dont `data` est un objet de métadonnées
|
|
228
|
+
- Retourne: {Promise} Résout avec une réponse dont `data` est un objet de métadonnées formaté, ou le JSON analysé pour les URL d'actifs DAM
|
|
229
229
|
|
|
230
|
-
Récupère les métadonnées JCR de la page donnée. Les transformations suivantes sont appliquées :
|
|
230
|
+
Récupère les métadonnées JCR de la page donnée. Pour les URL d'actifs DAM sous `/content/dam/`, la réponse JSON de l'actif est retournée. Les transformations suivantes sont appliquées aux métadonnées de page :
|
|
231
231
|
|
|
232
232
|
- Les valeurs `"true"` / `"false"` sont converties en booléens
|
|
233
233
|
- Les propriétés `@TypeHint` sont supprimées
|
package/dist/ca.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("ca",[],e):"object"==typeof exports?exports.ca=e():t.ca=e()}(Object("undefined"!=typeof self?self:this),()=>(()=>{"use strict";var t={d:(e,a)=>{for(var r in a)t.o(a,r)&&!t.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:a[r]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},e={};t.d(e,{default:()=>c});const a="https://www.canada.ca",r=t=>{if("string"==typeof t)t=new URL(t,a);else{if(!(t instanceof URL))throw new TypeError("string or URL object expected");t=new URL(t.href)}if(t.origin!==a)throw new Error("URL must start with "+a);if(t.pathname=t.pathname.replace(/^\/content\/canadasite/,""),t.pathname=t.pathname.replace(
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("ca",[],e):"object"==typeof exports?exports.ca=e():t.ca=e()}(Object("undefined"!=typeof self?self:this),()=>(()=>{"use strict";var t={d:(e,a)=>{for(var r in a)t.o(a,r)&&!t.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:a[r]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},e={};t.d(e,{default:()=>c});const a="https://www.canada.ca",r=t=>{if("string"==typeof t)t=new URL(t,a);else{if(!(t instanceof URL))throw new TypeError("string or URL object expected");t=new URL(t.href)}if(t.origin!==a)throw new Error("URL must start with "+a);if(t.pathname.startsWith("/content/dam/"))return t;if(t.pathname=t.pathname.replace(/^\/content\/canadasite/,""),t.pathname=t.pathname.replace(/\/+$/,""),t.pathname=t.pathname.replace(/\.[^/]*$/,""),!t.pathname.startsWith("/en/")&&!t.pathname.startsWith("/fr/"))throw new Error(`Invalid path: "${t.pathname}" must start with /en/ or /fr/`);return t},n=async(t,e={})=>{(t=new URL(t,a)).searchParams.set("_",Date.now());const{headers:r={},...n}=e;let o;try{o=await fetch(t,{signal:AbortSignal.timeout(3e4),...n,headers:{"User-Agent":"canada-api/5.1.7",Accept:"*/*",...r}})}catch(e){throw e.url=t.toString(),e}if(!o.ok){const e=new Error(`Request to ${t} failed: ${o.status} ${o.statusText}`);throw e.url=t.toString(),e}let s=await o.text();const c=o.headers.get("content-type")?.includes("application/json");if(c)try{s=JSON.parse(s)}catch(e){const a=new Error(`Failed to parse JSON response from ${t}: ${e.message}`);throw a.url=t.toString(),a}return{data:s,status:o.status,statusText:o.statusText,headers:Object.fromEntries(o.headers)}},o={Jan:"01",Feb:"02",Mar:"03",Apr:"04",May:"05",Jun:"06",Jul:"07",Aug:"08",Sep:"09",Oct:"10",Nov:"11",Dec:"12"};function s(t){if(/^\d{4}-\d{2}-\d{2}$/.test(t))return new Date(t).toISOString();let e=/^\w{3} (\w{3}) (\d{2}) (\d{4}) ([\d:]{8}) GMT([\-+]\d{4})$/.exec(t);return e?new Date(`${e[3]}-${o[e[1]]}-${e[2]}T${e[4]}${e[5]}`).toISOString():t}const c={normalize:r,request:n,children:async t=>{const e=r(t);if(e.pathname.startsWith("/content/dam/"))throw new Error(`children not available for DAM assets: "${e.pathname}"`);e.pathname+=".sitemap.xml";const a=await n(e,{redirect:"error"});return a.data=[...a.data.matchAll(/<url>([\s\S]*?)<\/url>/g)].map(([,t])=>{const e=t.match(/<loc>([\s\S]*?)<\/loc>/)?.[1],a=t.match(/<lastmod>([\s\S]*?)<\/lastmod>/)?.[1];return{loc:e,lastmod:a}}).filter(t=>t.loc).map(t=>({path:r(t.loc).pathname,lastmod:t.lastmod?new Date(t.lastmod).toISOString():null})),a},content:async t=>{const e=r(t);return e.pathname.startsWith("/content/dam/")||(e.pathname+=".html"),n(e,{redirect:"error"})},meta:async t=>{const e=r(t);e.pathname.startsWith("/content/dam/")?e.pathname+="/.json":e.pathname+="/_jcr_content.json";const a=await n(e,{redirect:"error"});return a.data=(t=>{const e={};for(const[a,n]of Object.entries(t))a.endsWith("@TypeHint")||Array.isArray(n)&&0===n.length||("true"===n?e[a]=!0:"false"===n?e[a]=!1:"gcAltLanguagePeer"===a?(e[a]=n,e.peer=r(n).pathname):e[a]="string"==typeof n?s(n.trim()):n);return Object.keys(e).sort().reduce((t,a)=>(t[a]=e[a],t),{})})(a.data),a}};return e.default})());
|
package/package.json
CHANGED
package/src/children.js
CHANGED
|
@@ -1,49 +1,53 @@
|
|
|
1
|
-
import normalize from "./normalize.js";
|
|
2
|
-
import request from "./request.js";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Represents a single URL entry from a sitemap
|
|
6
|
-
* @typedef {object} SitemapEntry
|
|
7
|
-
* @property {string} path - The normalized URL path (e.g., '/en/page')
|
|
8
|
-
* @property {string|null} lastmod - ISO 8601 timestamp or null if not present
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Parse XML sitemap data into structured URL entries
|
|
13
|
-
* @param {string}
|
|
14
|
-
* @returns {SitemapEntry[]} Array of sitemap entries with path and lastmod. Entries missing a `<loc>` element are skipped.
|
|
15
|
-
*/
|
|
16
|
-
export const parseSitemap = (xml) => {
|
|
17
|
-
return [...xml.matchAll(/<url>([\s\S]*?)<\/url>/g)]
|
|
18
|
-
.map(([, inner]) => {
|
|
19
|
-
const loc = inner.match(/<loc>([\s\S]*?)<\/loc>/)?.[1];
|
|
20
|
-
const lastmod = inner.match(/<lastmod>([\s\S]*?)<\/lastmod>/)?.[1];
|
|
21
|
-
return { loc, lastmod };
|
|
22
|
-
})
|
|
23
|
-
.filter(item => item.loc)
|
|
24
|
-
.map(item => ({
|
|
25
|
-
path: normalize(item.loc).pathname,
|
|
26
|
-
lastmod: item.lastmod ? new Date(item.lastmod).toISOString() : null,
|
|
27
|
-
}));
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Fetch and parse sitemap children for a canada.ca page
|
|
32
|
-
* @param {string|URL} url - Absolute or relative URL
|
|
33
|
-
* @returns {Promise<{data: SitemapEntry[], status: number, statusText: string, headers: object}>}
|
|
34
|
-
* @throws {Error} If the
|
|
35
|
-
*/
|
|
36
|
-
const children = async (url) => {
|
|
37
|
-
const target = normalize(url);
|
|
38
|
-
|
|
39
|
-
target.
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
response
|
|
46
|
-
|
|
47
|
-
};
|
|
48
|
-
|
|
1
|
+
import normalize from "./normalize.js";
|
|
2
|
+
import request from "./request.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Represents a single URL entry from a sitemap
|
|
6
|
+
* @typedef {object} SitemapEntry
|
|
7
|
+
* @property {string} path - The normalized URL path (e.g., '/en/page')
|
|
8
|
+
* @property {string|null} lastmod - ISO 8601 timestamp or null if not present
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Parse XML sitemap data into structured URL entries
|
|
13
|
+
* @param {string} xml - Raw XML sitemap content
|
|
14
|
+
* @returns {SitemapEntry[]} Array of sitemap entries with path and lastmod. Entries missing a `<loc>` element are skipped.
|
|
15
|
+
*/
|
|
16
|
+
export const parseSitemap = (xml) => {
|
|
17
|
+
return [...xml.matchAll(/<url>([\s\S]*?)<\/url>/g)]
|
|
18
|
+
.map(([, inner]) => {
|
|
19
|
+
const loc = inner.match(/<loc>([\s\S]*?)<\/loc>/)?.[1];
|
|
20
|
+
const lastmod = inner.match(/<lastmod>([\s\S]*?)<\/lastmod>/)?.[1];
|
|
21
|
+
return { loc, lastmod };
|
|
22
|
+
})
|
|
23
|
+
.filter(item => item.loc)
|
|
24
|
+
.map(item => ({
|
|
25
|
+
path: normalize(item.loc).pathname,
|
|
26
|
+
lastmod: item.lastmod ? new Date(item.lastmod).toISOString() : null,
|
|
27
|
+
}));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Fetch and parse sitemap children for a canada.ca page
|
|
32
|
+
* @param {string|URL} url - Absolute or relative URL for a canada.ca page
|
|
33
|
+
* @returns {Promise<{data: SitemapEntry[], status: number, statusText: string, headers: object}>}
|
|
34
|
+
* @throws {Error} If the URL points to a DAM asset path or if the request fails/returns a non-2xx status
|
|
35
|
+
*/
|
|
36
|
+
const children = async (url) => {
|
|
37
|
+
const target = normalize(url);
|
|
38
|
+
|
|
39
|
+
if (target.pathname.startsWith('/content/dam/')) {
|
|
40
|
+
throw new Error(`children not available for DAM assets: "${target.pathname}"`);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
target.pathname += '.sitemap.xml';
|
|
44
|
+
|
|
45
|
+
const response = await request(target, {
|
|
46
|
+
redirect: 'error'
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
response.data = parseSitemap(response.data);
|
|
50
|
+
return response;
|
|
51
|
+
};
|
|
52
|
+
|
|
49
53
|
export default children;
|
package/src/content.js
CHANGED
|
@@ -1,21 +1,22 @@
|
|
|
1
|
-
import normalize from "./normalize.js";
|
|
2
|
-
import request from "./request.js";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Fetch
|
|
6
|
-
* @param {string|URL} url - Absolute or relative URL
|
|
7
|
-
* @returns {Promise<{data: string, status: number, statusText: string, headers: object}>}
|
|
8
|
-
* @throws {Error} If the request fails or returns a non-2xx status
|
|
9
|
-
*/
|
|
10
|
-
const content = async (url) => {
|
|
11
|
-
const target = normalize(url);
|
|
12
|
-
|
|
13
|
-
target.
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
};
|
|
20
|
-
|
|
1
|
+
import normalize from "./normalize.js";
|
|
2
|
+
import request from "./request.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Fetch content for a canada.ca page or DAM asset
|
|
6
|
+
* @param {string|URL} url - Absolute or relative URL
|
|
7
|
+
* @returns {Promise<{data: string|object, status: number, statusText: string, headers: object}>}
|
|
8
|
+
* @throws {Error} If the request fails or returns a non-2xx status
|
|
9
|
+
*/
|
|
10
|
+
const content = async (url) => {
|
|
11
|
+
const target = normalize(url);
|
|
12
|
+
|
|
13
|
+
if (!target.pathname.startsWith('/content/dam/')) {
|
|
14
|
+
target.pathname += '.html';
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return request(target, {
|
|
18
|
+
redirect: 'error'
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
21
22
|
export default content;
|
package/src/index.js
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import normalize from "./normalize.js";
|
|
2
|
-
import request from "./request.js";
|
|
3
|
-
import children from "./children.js";
|
|
4
|
-
import content from "./content.js";
|
|
5
|
-
import meta from "./meta.js";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* @typedef {object} CanadaAPI
|
|
9
|
-
* @property {function} normalize - Normalize and validate canada.ca URLs
|
|
10
|
-
* @property {function} request - Raw HTTP client for canada.ca requests
|
|
11
|
-
* @property {function} children - Fetch and parse sitemap hierarchies
|
|
12
|
-
* @property {function} content - Fetch HTML content pages
|
|
13
|
-
* @property {function} meta - Fetch and format JCR metadata
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
/** @type {CanadaAPI} */
|
|
17
|
-
const ca = {
|
|
18
|
-
normalize,
|
|
19
|
-
request,
|
|
20
|
-
children,
|
|
21
|
-
content,
|
|
22
|
-
meta
|
|
23
|
-
}
|
|
24
|
-
|
|
1
|
+
import normalize from "./normalize.js";
|
|
2
|
+
import request from "./request.js";
|
|
3
|
+
import children from "./children.js";
|
|
4
|
+
import content from "./content.js";
|
|
5
|
+
import meta from "./meta.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {object} CanadaAPI
|
|
9
|
+
* @property {function} normalize - Normalize and validate canada.ca URLs
|
|
10
|
+
* @property {function} request - Raw HTTP client for canada.ca requests
|
|
11
|
+
* @property {function} children - Fetch and parse sitemap hierarchies
|
|
12
|
+
* @property {function} content - Fetch HTML content pages
|
|
13
|
+
* @property {function} meta - Fetch and format JCR metadata
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** @type {CanadaAPI} */
|
|
17
|
+
const ca = {
|
|
18
|
+
normalize,
|
|
19
|
+
request,
|
|
20
|
+
children,
|
|
21
|
+
content,
|
|
22
|
+
meta
|
|
23
|
+
}
|
|
24
|
+
|
|
25
25
|
export default ca
|
package/src/meta.js
CHANGED
|
@@ -1,103 +1,106 @@
|
|
|
1
|
-
import normalize from "./normalize.js";
|
|
2
|
-
import request from "./request.js";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Month name to number mapping
|
|
6
|
-
* @const {Record<string, string>}
|
|
7
|
-
* @private
|
|
8
|
-
*/
|
|
9
|
-
const months = {
|
|
10
|
-
'Jan': '01',
|
|
11
|
-
'Feb': '02',
|
|
12
|
-
'Mar': '03',
|
|
13
|
-
'Apr': '04',
|
|
14
|
-
'May': '05',
|
|
15
|
-
'Jun': '06',
|
|
16
|
-
'Jul': '07',
|
|
17
|
-
'Aug': '08',
|
|
18
|
-
'Sep': '09',
|
|
19
|
-
'Oct': '10',
|
|
20
|
-
'Nov': '11',
|
|
21
|
-
'Dec': '12'
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Try to parse and format date strings from JCR into ISO 8601
|
|
26
|
-
* @param {string} text - Potential date string to format
|
|
27
|
-
* @returns {string} ISO 8601 timestamp or original text if not a recognized date
|
|
28
|
-
* @description Supports YYYY-MM-DD and JCR date format (e.g. "Wed Nov 20 2019 13:17:13 GMT-0500").
|
|
29
|
-
* Uses explicit parsing to ensure consistent output across Node.js and browsers.
|
|
30
|
-
* @private
|
|
31
|
-
*/
|
|
32
|
-
function formatDate(text) {
|
|
33
|
-
// Simple YYYY-MM-DD format
|
|
34
|
-
if (/^\d{4}-\d{2}-\d{2}$/.test(text)) {
|
|
35
|
-
return new Date(text).toISOString()
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// RFC1123 format
|
|
39
|
-
let m = /^\w{3} (\w{3}) (\d{2}) (\d{4}) ([\d:]{8}) GMT([\-+]\d{4})$/.exec(text)
|
|
40
|
-
if (m) {
|
|
41
|
-
return new Date(`${m[3]}-${months[m[1]]}-${m[2]}T${m[4]}${m[5]}`).toISOString()
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return text
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Format and normalize metadata object
|
|
49
|
-
* @param {Record<string, any>} data - Raw metadata object from JCR
|
|
50
|
-
* @returns {Record<string, any>} Formatted metadata with normalized types and sorted keys
|
|
51
|
-
* @description Converts string booleans to native booleans, formats dates to ISO 8601,
|
|
52
|
-
* removes @TypeHint properties and empty arrays, sorts keys alphabetically, and adds a
|
|
53
|
-
* normalized `peer` field when `gcAltLanguagePeer` is present.
|
|
54
|
-
*/
|
|
55
|
-
export const formatMeta = (data) => {
|
|
56
|
-
const result = {}
|
|
57
|
-
|
|
58
|
-
for (const [key, value] of Object.entries(data)) {
|
|
59
|
-
if (key.endsWith('@TypeHint')) continue
|
|
60
|
-
if (Array.isArray(value) && value.length === 0) continue
|
|
61
|
-
|
|
62
|
-
if (value === 'true') {
|
|
63
|
-
result[key] = true
|
|
64
|
-
} else if (value === 'false') {
|
|
65
|
-
result[key] = false
|
|
66
|
-
} else if (key === 'gcAltLanguagePeer') {
|
|
67
|
-
result[key] = value
|
|
68
|
-
result['peer'] = normalize(value).pathname
|
|
69
|
-
} else if (typeof value === 'string') {
|
|
70
|
-
result[key] = formatDate(value.trim())
|
|
71
|
-
} else {
|
|
72
|
-
result[key] = value
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// Sort object keys alphabetically for readability
|
|
77
|
-
return Object.keys(result).sort().reduce((obj, key) => {
|
|
78
|
-
obj[key] = result[key]
|
|
79
|
-
return obj
|
|
80
|
-
}, {})
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Fetch and format
|
|
85
|
-
* @param {string|URL} url - Absolute or relative URL
|
|
86
|
-
* @returns {Promise<{data: Record<string, any
|
|
87
|
-
* @throws {Error} If the request fails or returns a non-2xx status
|
|
88
|
-
*/
|
|
89
|
-
const meta = async (url) => {
|
|
90
|
-
const target = normalize(url);
|
|
91
|
-
|
|
92
|
-
target.
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
1
|
+
import normalize from "./normalize.js";
|
|
2
|
+
import request from "./request.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Month name to number mapping
|
|
6
|
+
* @const {Record<string, string>}
|
|
7
|
+
* @private
|
|
8
|
+
*/
|
|
9
|
+
const months = {
|
|
10
|
+
'Jan': '01',
|
|
11
|
+
'Feb': '02',
|
|
12
|
+
'Mar': '03',
|
|
13
|
+
'Apr': '04',
|
|
14
|
+
'May': '05',
|
|
15
|
+
'Jun': '06',
|
|
16
|
+
'Jul': '07',
|
|
17
|
+
'Aug': '08',
|
|
18
|
+
'Sep': '09',
|
|
19
|
+
'Oct': '10',
|
|
20
|
+
'Nov': '11',
|
|
21
|
+
'Dec': '12'
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Try to parse and format date strings from JCR into ISO 8601
|
|
26
|
+
* @param {string} text - Potential date string to format
|
|
27
|
+
* @returns {string} ISO 8601 timestamp or original text if not a recognized date
|
|
28
|
+
* @description Supports YYYY-MM-DD and JCR date format (e.g. "Wed Nov 20 2019 13:17:13 GMT-0500").
|
|
29
|
+
* Uses explicit parsing to ensure consistent output across Node.js and browsers.
|
|
30
|
+
* @private
|
|
31
|
+
*/
|
|
32
|
+
function formatDate(text) {
|
|
33
|
+
// Simple YYYY-MM-DD format
|
|
34
|
+
if (/^\d{4}-\d{2}-\d{2}$/.test(text)) {
|
|
35
|
+
return new Date(text).toISOString()
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// RFC1123 format
|
|
39
|
+
let m = /^\w{3} (\w{3}) (\d{2}) (\d{4}) ([\d:]{8}) GMT([\-+]\d{4})$/.exec(text)
|
|
40
|
+
if (m) {
|
|
41
|
+
return new Date(`${m[3]}-${months[m[1]]}-${m[2]}T${m[4]}${m[5]}`).toISOString()
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return text
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Format and normalize metadata object
|
|
49
|
+
* @param {Record<string, any>} data - Raw metadata object from JCR
|
|
50
|
+
* @returns {Record<string, any>} Formatted metadata with normalized types and sorted keys
|
|
51
|
+
* @description Converts string booleans to native booleans, formats dates to ISO 8601,
|
|
52
|
+
* removes @TypeHint properties and empty arrays, sorts keys alphabetically, and adds a
|
|
53
|
+
* normalized `peer` field when `gcAltLanguagePeer` is present.
|
|
54
|
+
*/
|
|
55
|
+
export const formatMeta = (data) => {
|
|
56
|
+
const result = {}
|
|
57
|
+
|
|
58
|
+
for (const [key, value] of Object.entries(data)) {
|
|
59
|
+
if (key.endsWith('@TypeHint')) continue
|
|
60
|
+
if (Array.isArray(value) && value.length === 0) continue
|
|
61
|
+
|
|
62
|
+
if (value === 'true') {
|
|
63
|
+
result[key] = true
|
|
64
|
+
} else if (value === 'false') {
|
|
65
|
+
result[key] = false
|
|
66
|
+
} else if (key === 'gcAltLanguagePeer') {
|
|
67
|
+
result[key] = value
|
|
68
|
+
result['peer'] = normalize(value).pathname
|
|
69
|
+
} else if (typeof value === 'string') {
|
|
70
|
+
result[key] = formatDate(value.trim())
|
|
71
|
+
} else {
|
|
72
|
+
result[key] = value
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Sort object keys alphabetically for readability
|
|
77
|
+
return Object.keys(result).sort().reduce((obj, key) => {
|
|
78
|
+
obj[key] = result[key]
|
|
79
|
+
return obj
|
|
80
|
+
}, {})
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Fetch and format metadata
|
|
85
|
+
* @param {string|URL} url - Absolute or relative URL
|
|
86
|
+
* @returns {Promise<{data: Record<string, any>|any, status: number, statusText: string, headers: object}>}
|
|
87
|
+
* @throws {Error} If the request fails or returns a non-2xx status
|
|
88
|
+
*/
|
|
89
|
+
const meta = async (url) => {
|
|
90
|
+
const target = normalize(url);
|
|
91
|
+
|
|
92
|
+
if (target.pathname.startsWith('/content/dam/')) {
|
|
93
|
+
target.pathname += '/.json'
|
|
94
|
+
} else {
|
|
95
|
+
target.pathname += '/_jcr_content.json';
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const response = await request(target, {
|
|
99
|
+
redirect: 'error'
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
response.data = formatMeta(response.data);
|
|
103
|
+
return response;
|
|
104
|
+
};
|
|
105
|
+
|
|
103
106
|
export default meta;
|
package/src/normalize.js
CHANGED
|
@@ -1,38 +1,44 @@
|
|
|
1
|
-
import { BASE_URL } from './config.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Normalize a canada.ca URL to a clean pathname
|
|
5
|
-
* @param {string|URL} url - A full URL or relative path (e.g., 'https://www.canada.ca/en/page'
|
|
6
|
-
* @returns {URL} Normalized URL object with cleaned pathname
|
|
7
|
-
* @throws {TypeError} If url is not a string or URL object
|
|
8
|
-
* @throws {Error} If URL is not from canada.ca or path doesn't start with /en/ or /
|
|
9
|
-
*/
|
|
10
|
-
const normalize = (url) => {
|
|
11
|
-
|
|
12
|
-
if (typeof url === 'string') {
|
|
13
|
-
url = new URL(url, BASE_URL)
|
|
14
|
-
} else if (url instanceof URL) {
|
|
15
|
-
url = new URL(url.href)
|
|
16
|
-
} else {
|
|
17
|
-
throw new TypeError('string or URL object expected')
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// Verify domain
|
|
21
|
-
if (url.origin !== BASE_URL) {
|
|
22
|
-
throw new Error('URL must start with ' + BASE_URL)
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
1
|
+
import { BASE_URL } from './config.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Normalize a canada.ca URL to a clean pathname
|
|
5
|
+
* @param {string|URL} url - A full URL or relative path (e.g., 'https://www.canada.ca/en/page', '/en/page', or '/content/dam/...')
|
|
6
|
+
* @returns {URL} Normalized URL object with a cleaned pathname
|
|
7
|
+
* @throws {TypeError} If url is not a string or URL object
|
|
8
|
+
* @throws {Error} If URL is not from canada.ca or the path doesn't start with /en/, /fr/, or /content/dam/
|
|
9
|
+
*/
|
|
10
|
+
const normalize = (url) => {
|
|
11
|
+
|
|
12
|
+
if (typeof url === 'string') {
|
|
13
|
+
url = new URL(url, BASE_URL)
|
|
14
|
+
} else if (url instanceof URL) {
|
|
15
|
+
url = new URL(url.href)
|
|
16
|
+
} else {
|
|
17
|
+
throw new TypeError('string or URL object expected')
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Verify domain
|
|
21
|
+
if (url.origin !== BASE_URL) {
|
|
22
|
+
throw new Error('URL must start with ' + BASE_URL)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// No normalization for DAM asset paths
|
|
26
|
+
if (url.pathname.startsWith('/content/dam/')) {
|
|
27
|
+
return url
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
url.pathname = url.pathname.replace(/^\/content\/canadasite/, '');
|
|
31
|
+
url.pathname = url.pathname.replace(/\/+$/, '');
|
|
32
|
+
|
|
33
|
+
// Remove file extensions (like .html, .xml)
|
|
34
|
+
url.pathname = url.pathname.replace(/\.[^/]*$/, '');
|
|
35
|
+
|
|
36
|
+
// Verify root language
|
|
37
|
+
if (!url.pathname.startsWith('/en/') && !url.pathname.startsWith('/fr/')) {
|
|
38
|
+
throw new Error(`Invalid path: "${url.pathname}" must start with /en/ or /fr/`)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return url
|
|
42
|
+
}
|
|
43
|
+
|
|
38
44
|
export default normalize;
|
package/src/request.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { BASE_URL } from "./config.js";
|
|
2
2
|
|
|
3
|
+
/** @type {number} Default request timeout in ms. Callers can override by passing a `signal` in options. */
|
|
4
|
+
const DEFAULT_TIMEOUT = 30000;
|
|
5
|
+
|
|
3
6
|
/**
|
|
4
7
|
* Raw HTTP client for canada.ca
|
|
5
8
|
* @param {string|URL} url - Relative or absolute URL on canada.ca
|
|
@@ -9,16 +12,17 @@ import { BASE_URL } from "./config.js";
|
|
|
9
12
|
*/
|
|
10
13
|
const request = async (url, options = {}) => {
|
|
11
14
|
url = new URL(url, BASE_URL);
|
|
15
|
+
url.searchParams.set('_', Date.now());
|
|
12
16
|
|
|
13
17
|
const { headers: customHeaders = {}, ...requestOptions } = options;
|
|
14
18
|
|
|
15
19
|
let response;
|
|
16
20
|
try {
|
|
17
21
|
response = await fetch(url, {
|
|
18
|
-
signal: AbortSignal.timeout(
|
|
22
|
+
signal: AbortSignal.timeout(DEFAULT_TIMEOUT),
|
|
19
23
|
...requestOptions,
|
|
20
24
|
headers: {
|
|
21
|
-
'User-Agent': 'canada-api/5.1.
|
|
25
|
+
'User-Agent': 'canada-api/5.1.7',
|
|
22
26
|
'Accept': '*/*',
|
|
23
27
|
...customHeaders
|
|
24
28
|
}
|
|
@@ -29,7 +33,7 @@ const request = async (url, options = {}) => {
|
|
|
29
33
|
}
|
|
30
34
|
|
|
31
35
|
if (!response.ok) {
|
|
32
|
-
const error = new Error(
|
|
36
|
+
const error = new Error(`Request to ${url} failed: ${response.status} ${response.statusText}`);
|
|
33
37
|
error.url = url.toString();
|
|
34
38
|
throw error;
|
|
35
39
|
}
|
|
@@ -41,8 +45,9 @@ const request = async (url, options = {}) => {
|
|
|
41
45
|
try {
|
|
42
46
|
data = JSON.parse(data);
|
|
43
47
|
} catch (e) {
|
|
44
|
-
|
|
45
|
-
|
|
48
|
+
const error = new Error(`Failed to parse JSON response from ${url}: ${e.message}`);
|
|
49
|
+
error.url = url.toString();
|
|
50
|
+
throw error;
|
|
46
51
|
}
|
|
47
52
|
}
|
|
48
53
|
|