canada-api 5.1.5 → 5.1.6
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 +2 -2
- package/dist/ca.js +1 -1
- package/package.json +1 -1
- package/src/children.js +47 -48
- package/src/content.js +18 -20
- package/src/index.js +24 -24
- package/src/meta.js +100 -102
- package/src/normalize.js +37 -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.6"></script>
|
|
13
13
|
```
|
|
14
14
|
|
|
15
15
|
## Node 18+
|
|
@@ -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.6"></script>
|
|
156
156
|
```
|
|
157
157
|
|
|
158
158
|
## Node 18+
|
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,
|
|
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,r)=>{for(var a in r)t.o(r,a)&&!t.o(e,a)&&Object.defineProperty(e,a,{enumerable:!0,get:r[a]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},e={};t.d(e,{default:()=>c});const r="https://www.canada.ca",a=t=>{if("string"==typeof t)t=new URL(t,r);else{if(!(t instanceof URL))throw new TypeError("string or URL object expected");t=new URL(t.href)}if(t.origin!==r)throw new Error("URL must start with "+r);if(t.pathname=t.pathname.replace(/^\/content\/canadasite/,""),t.pathname=t.pathname.replace(/\.[^/]*$/,"").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,r)).searchParams.set("_",Date.now());const{headers:a={},...n}=e;let o;try{o=await fetch(t,{signal:AbortSignal.timeout(3e4),...n,headers:{"User-Agent":"canada-api/5.1.6",Accept:"*/*",...a}})}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 r=new Error(`Failed to parse JSON response from ${t}: ${e.message}`);throw r.url=t.toString(),r}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:a,request:n,children:async t=>{const e=a(t);e.pathname+=".sitemap.xml";const r=await n(e,{redirect:"error"});return r.data=[...r.data.matchAll(/<url>([\s\S]*?)<\/url>/g)].map(([,t])=>{const e=t.match(/<loc>([\s\S]*?)<\/loc>/)?.[1],r=t.match(/<lastmod>([\s\S]*?)<\/lastmod>/)?.[1];return{loc:e,lastmod:r}}).filter(t=>t.loc).map(t=>({path:a(t.loc).pathname,lastmod:t.lastmod?new Date(t.lastmod).toISOString():null})),r},content:async t=>{const e=a(t);return e.pathname+=".html",n(e,{redirect:"error"})},meta:async t=>{const e=a(t);e.pathname+="/_jcr_content.json";const r=await n(e,{redirect:"error"});return r.data=(t=>{const e={};for(const[r,n]of Object.entries(t))r.endsWith("@TypeHint")||Array.isArray(n)&&0===n.length||("true"===n?e[r]=!0:"false"===n?e[r]=!1:"gcAltLanguagePeer"===r?(e[r]=n,e.peer=a(n).pathname):e[r]="string"==typeof n?s(n.trim()):n);return Object.keys(e).sort().reduce((t,r)=>(t[r]=e[r],t),{})})(r.data),r}};return e.default})());
|
package/package.json
CHANGED
package/src/children.js
CHANGED
|
@@ -1,49 +1,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} data - 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
|
|
33
|
-
* @returns {Promise<{data: SitemapEntry[], status: number, statusText: string, headers: object}>}
|
|
34
|
-
* @throws {Error} If the request fails or returns a non-2xx status
|
|
35
|
-
*/
|
|
36
|
-
const children = async (url) => {
|
|
37
|
-
const target = normalize(url);
|
|
38
|
-
target.pathname += '.sitemap.xml';
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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} data - 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
|
|
33
|
+
* @returns {Promise<{data: SitemapEntry[], status: number, statusText: string, headers: object}>}
|
|
34
|
+
* @throws {Error} If the request fails or returns a non-2xx status
|
|
35
|
+
*/
|
|
36
|
+
const children = async (url) => {
|
|
37
|
+
const target = normalize(url);
|
|
38
|
+
target.pathname += '.sitemap.xml';
|
|
39
|
+
|
|
40
|
+
const response = await request(target, {
|
|
41
|
+
redirect: 'error'
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
response.data = parseSitemap(response.data);
|
|
45
|
+
return response;
|
|
46
|
+
};
|
|
47
|
+
|
|
49
48
|
export default children;
|
package/src/content.js
CHANGED
|
@@ -1,21 +1,19 @@
|
|
|
1
|
-
import normalize from "./normalize.js";
|
|
2
|
-
import request from "./request.js";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Fetch HTML content for a canada.ca page
|
|
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
|
-
target.pathname += '.html';
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
};
|
|
20
|
-
|
|
1
|
+
import normalize from "./normalize.js";
|
|
2
|
+
import request from "./request.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Fetch HTML content for a canada.ca page
|
|
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
|
+
target.pathname += '.html';
|
|
13
|
+
|
|
14
|
+
return request(target, {
|
|
15
|
+
redirect: 'error'
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
|
|
21
19
|
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,101 @@
|
|
|
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 JCR metadata for a canada.ca page
|
|
85
|
-
* @param {string|URL} url - Absolute or relative URL
|
|
86
|
-
* @returns {Promise<{data: Record<string, 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
|
-
target.pathname += '/_jcr_content.json';
|
|
92
|
-
|
|
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 JCR metadata for a canada.ca page
|
|
85
|
+
* @param {string|URL} url - Absolute or relative URL
|
|
86
|
+
* @returns {Promise<{data: Record<string, 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
|
+
target.pathname += '/_jcr_content.json';
|
|
92
|
+
|
|
93
|
+
const response = await request(target, {
|
|
94
|
+
redirect: 'error'
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
response.data = formatMeta(response.data);
|
|
98
|
+
return response;
|
|
99
|
+
};
|
|
100
|
+
|
|
103
101
|
export default meta;
|
package/src/normalize.js
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
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' or '/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 /fr/
|
|
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
|
-
url.pathname = url.pathname.replace(/^\/content\/canadasite/, '');
|
|
26
|
-
|
|
27
|
-
// Remove file extensions (like .html, .xml) and trailing slashes
|
|
28
|
-
url.pathname = url.pathname.replace(/\.[^/]*$/, '').replace(/\/+$/, '');
|
|
29
|
-
|
|
30
|
-
// Verify root language
|
|
31
|
-
if (!url.pathname.startsWith('/en/') && !url.pathname.startsWith('/fr/')) {
|
|
32
|
-
throw new Error(`Invalid path: "${url.pathname}" must start with /en/ or /fr/`)
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return url
|
|
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' or '/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 /fr/
|
|
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
|
+
url.pathname = url.pathname.replace(/^\/content\/canadasite/, '');
|
|
26
|
+
|
|
27
|
+
// Remove file extensions (like .html, .xml) and trailing slashes
|
|
28
|
+
url.pathname = url.pathname.replace(/\.[^/]*$/, '').replace(/\/+$/, '');
|
|
29
|
+
|
|
30
|
+
// Verify root language
|
|
31
|
+
if (!url.pathname.startsWith('/en/') && !url.pathname.startsWith('/fr/')) {
|
|
32
|
+
throw new Error(`Invalid path: "${url.pathname}" must start with /en/ or /fr/`)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return url
|
|
36
|
+
}
|
|
37
|
+
|
|
38
38
|
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.6',
|
|
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
|
|