canada-api 5.1.4 → 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 +277 -277
- package/dist/ca.js +1 -1
- package/package.json +44 -44
- package/src/children.js +0 -1
- package/src/content.js +1 -3
- package/src/meta.js +0 -2
- package/src/request.js +61 -39
package/README.md
CHANGED
|
@@ -1,277 +1,277 @@
|
|
|
1
|
-
([Français](#canada-api-1))
|
|
2
|
-
|
|
3
|
-
# canada-api
|
|
4
|
-
|
|
5
|
-
[](https://www.npmjs.com/package/canada-api) [](https://github.com/dnd-mdn/canada-api/blob/main/LICENSE.md)
|
|
6
|
-
|
|
7
|
-
Cross platform API for fetching public data from [canada.ca](https://www.canada.ca).
|
|
8
|
-
|
|
9
|
-
## Browser
|
|
10
|
-
|
|
11
|
-
```html
|
|
12
|
-
<script src="https://cdn.jsdelivr.net/npm/canada-api@5.1.
|
|
13
|
-
```
|
|
14
|
-
|
|
15
|
-
## Node 18+
|
|
16
|
-
|
|
17
|
-
### Install
|
|
18
|
-
|
|
19
|
-
```shell
|
|
20
|
-
npm install canada-api
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
### Usage
|
|
24
|
-
|
|
25
|
-
```js
|
|
26
|
-
import ca from 'canada-api'
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
## Testing
|
|
30
|
-
|
|
31
|
-
```shell
|
|
32
|
-
npm test
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
Tests use the built-in Node.js test runner (`node:test`) and require Node 18 or later.
|
|
36
|
-
|
|
37
|
-
## API
|
|
38
|
-
|
|
39
|
-
### `ca.normalize(url)`
|
|
40
|
-
|
|
41
|
-
- `url` {string|URL} - Full URL or relative path (e.g. `'/en/page'` or `'https://www.canada.ca/en/page'`)
|
|
42
|
-
- Returns: {URL} Normalized URL object with cleaned pathname
|
|
43
|
-
|
|
44
|
-
Validates and normalizes a canada.ca URL. Strips the `/content/canadasite` prefix, file extensions, and trailing slashes.
|
|
45
|
-
|
|
46
|
-
Throws {TypeError} if `url` is not a string or URL object.
|
|
47
|
-
Throws {Error} if the URL is not on canada.ca or the path does not start with `/en/` or `/fr/`.
|
|
48
|
-
|
|
49
|
-
### `ca.children(url)`
|
|
50
|
-
|
|
51
|
-
- `url` {string|URL} - Absolute or relative URL
|
|
52
|
-
- Returns: {Promise} Fulfills with a response whose `data` is an array of sitemap entries
|
|
53
|
-
|
|
54
|
-
Fetches and parses the sitemap for the given page, returning its child pages. Entries without a `<loc>` element are skipped.
|
|
55
|
-
|
|
56
|
-
```json
|
|
57
|
-
{
|
|
58
|
-
"data": [
|
|
59
|
-
{
|
|
60
|
-
"path": "/en/department-national-defence/maple-leaf",
|
|
61
|
-
"lastmod": "2022-09-20T00:00:00.000Z"
|
|
62
|
-
}
|
|
63
|
-
],
|
|
64
|
-
"status": 200,
|
|
65
|
-
"statusText": "OK",
|
|
66
|
-
"headers": {
|
|
67
|
-
"content-type": "text/xml"
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
### `ca.content(url)`
|
|
73
|
-
|
|
74
|
-
- `url` {string|URL} - Absolute or relative URL
|
|
75
|
-
- Returns: {Promise} Fulfills with a response whose `data` is the raw HTML string
|
|
76
|
-
|
|
77
|
-
Retrieves the HTML content of the page.
|
|
78
|
-
|
|
79
|
-
```json
|
|
80
|
-
{
|
|
81
|
-
"data": "<!DOCTYPE html>...",
|
|
82
|
-
"status": 200,
|
|
83
|
-
"statusText": "OK",
|
|
84
|
-
"headers": {
|
|
85
|
-
"content-type": "text/html"
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
### `ca.meta(url)`
|
|
91
|
-
|
|
92
|
-
- `url` {string|URL} - Absolute or relative URL
|
|
93
|
-
- Returns: {Promise} Fulfills with a response whose `data` is a formatted metadata object
|
|
94
|
-
|
|
95
|
-
Fetches JCR metadata for the given page. The following transformations are applied:
|
|
96
|
-
|
|
97
|
-
- String `"true"` / `"false"` values are converted to booleans
|
|
98
|
-
- `@TypeHint` properties are removed
|
|
99
|
-
- Empty arrays are removed
|
|
100
|
-
- Date strings are converted to ISO 8601
|
|
101
|
-
- Keys are sorted alphabetically
|
|
102
|
-
- A normalized `peer` field is added when `gcAltLanguagePeer` is present
|
|
103
|
-
|
|
104
|
-
```json
|
|
105
|
-
{
|
|
106
|
-
"data": {
|
|
107
|
-
"cq:lastModified": "2022-10-25T19:16:28.000Z",
|
|
108
|
-
"fluidWidth": false,
|
|
109
|
-
"peer": "/fr/ministere-defense-nationale/feuille-erable"
|
|
110
|
-
},
|
|
111
|
-
"status": 200,
|
|
112
|
-
"statusText": "OK",
|
|
113
|
-
"headers": {
|
|
114
|
-
"content-type": "application/json"
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
### `ca.request`
|
|
120
|
-
|
|
121
|
-
- `url` {string|URL} - Absolute or relative URL
|
|
122
|
-
- `options` {RequestInit} - Optional fetch options
|
|
123
|
-
- Returns: {Promise} Fulfills with a response object
|
|
124
|
-
|
|
125
|
-
Raw HTTP client with `https://www.canada.ca` as the base URL. Use this for any requests not covered by the methods above. No URL transformation is applied. Response bodies with a `application/json` content type are automatically parsed.
|
|
126
|
-
|
|
127
|
-
```js
|
|
128
|
-
const response = await ca.request('/en/department-national-defence.html');
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
All methods return the same response shape:
|
|
132
|
-
|
|
133
|
-
```json
|
|
134
|
-
{
|
|
135
|
-
"data": "...",
|
|
136
|
-
"status": 200,
|
|
137
|
-
"statusText": "OK",
|
|
138
|
-
"headers": {
|
|
139
|
-
"content-type": "text/html"
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
```
|
|
143
|
-
|
|
144
|
-
---
|
|
145
|
-
|
|
146
|
-
# canada-api
|
|
147
|
-
|
|
148
|
-
[](https://www.npmjs.com/package/canada-api) [](https://github.com/dnd-mdn/canada-api/blob/main/LICENSE.md)
|
|
149
|
-
|
|
150
|
-
API multiplateforme pour récupérer des données publiques de [canada.ca](https://www.canada.ca).
|
|
151
|
-
|
|
152
|
-
## Navigateur
|
|
153
|
-
|
|
154
|
-
```html
|
|
155
|
-
<script src="https://cdn.jsdelivr.net/npm/canada-api@5.1.
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
## Node 18+
|
|
159
|
-
|
|
160
|
-
### Installation
|
|
161
|
-
|
|
162
|
-
```shell
|
|
163
|
-
npm install canada-api
|
|
164
|
-
```
|
|
165
|
-
|
|
166
|
-
### Utilisation
|
|
167
|
-
|
|
168
|
-
```js
|
|
169
|
-
import ca from 'canada-api'
|
|
170
|
-
```
|
|
171
|
-
|
|
172
|
-
## API
|
|
173
|
-
|
|
174
|
-
### `ca.normalize(url)`
|
|
175
|
-
|
|
176
|
-
- `url` {string|URL} - URL complète ou chemin relatif (p. ex. `'/fr/page'` ou `'https://www.canada.ca/fr/page'`)
|
|
177
|
-
- Retourne: {URL} Objet URL normalisé avec un chemin nettoyé
|
|
178
|
-
|
|
179
|
-
Valide et normalise une URL de canada.ca. Supprime le préfixe `/content/canadasite`, les extensions de fichier et les barres obliques finales.
|
|
180
|
-
|
|
181
|
-
Lève {TypeError} si `url` n'est pas une chaîne ou un objet URL.
|
|
182
|
-
Lève {Error} si l'URL n'est pas sur canada.ca ou si le chemin ne commence pas par `/en/` ou `/fr/`.
|
|
183
|
-
|
|
184
|
-
### `ca.children(url)`
|
|
185
|
-
|
|
186
|
-
- `url` {string|URL} - URL absolue ou relative
|
|
187
|
-
- Retourne: {Promise} Résout avec une réponse dont `data` est un tableau d'entrées du plan de site
|
|
188
|
-
|
|
189
|
-
Récupère et analyse le plan de site de la page donnée, retournant ses pages enfants. Les entrées sans élément `<loc>` sont ignorées.
|
|
190
|
-
|
|
191
|
-
```json
|
|
192
|
-
{
|
|
193
|
-
"data": [
|
|
194
|
-
{
|
|
195
|
-
"path": "/fr/ministere-defense-nationale/feuille-erable",
|
|
196
|
-
"lastmod": "2022-09-20T00:00:00.000Z"
|
|
197
|
-
}
|
|
198
|
-
],
|
|
199
|
-
"status": 200,
|
|
200
|
-
"statusText": "OK",
|
|
201
|
-
"headers": {
|
|
202
|
-
"content-type": "text/xml"
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
```
|
|
206
|
-
|
|
207
|
-
### `ca.content(url)`
|
|
208
|
-
|
|
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
|
|
211
|
-
|
|
212
|
-
Récupère le contenu HTML de la page.
|
|
213
|
-
|
|
214
|
-
```json
|
|
215
|
-
{
|
|
216
|
-
"data": "<!DOCTYPE html>...",
|
|
217
|
-
"status": 200,
|
|
218
|
-
"statusText": "OK",
|
|
219
|
-
"headers": {
|
|
220
|
-
"content-type": "text/html"
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
```
|
|
224
|
-
|
|
225
|
-
### `ca.meta(url)`
|
|
226
|
-
|
|
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 formaté
|
|
229
|
-
|
|
230
|
-
Récupère les métadonnées JCR de la page donnée. Les transformations suivantes sont appliquées :
|
|
231
|
-
|
|
232
|
-
- Les valeurs `"true"` / `"false"` sont converties en booléens
|
|
233
|
-
- Les propriétés `@TypeHint` sont supprimées
|
|
234
|
-
- Les tableaux vides sont supprimés
|
|
235
|
-
- Les chaînes de date sont converties en ISO 8601
|
|
236
|
-
- Les clés sont triées alphabétiquement
|
|
237
|
-
- Un champ `peer` normalisé est ajouté lorsque `gcAltLanguagePeer` est présent
|
|
238
|
-
|
|
239
|
-
```json
|
|
240
|
-
{
|
|
241
|
-
"data": {
|
|
242
|
-
"cq:lastModified": "2022-10-25T19:16:28.000Z",
|
|
243
|
-
"fluidWidth": false,
|
|
244
|
-
"peer": "/en/department-national-defence/maple-leaf"
|
|
245
|
-
},
|
|
246
|
-
"status": 200,
|
|
247
|
-
"statusText": "OK",
|
|
248
|
-
"headers": {
|
|
249
|
-
"content-type": "application/json"
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
```
|
|
253
|
-
|
|
254
|
-
### `ca.request`
|
|
255
|
-
|
|
256
|
-
- `url` {string|URL} - URL absolue ou relative
|
|
257
|
-
- `options` {RequestInit} - Options fetch optionnelles
|
|
258
|
-
- Retourne: {Promise} Résout avec un objet réponse
|
|
259
|
-
|
|
260
|
-
Client HTTP brut avec `https://www.canada.ca` comme URL de base. Utilisez-le pour toute requête non couverte par les méthodes ci-dessus. Aucune transformation d'URL n'est appliquée. Les corps de réponse avec un type de contenu `application/json` sont automatiquement analysés.
|
|
261
|
-
|
|
262
|
-
```js
|
|
263
|
-
const response = await ca.request('/fr/ministere-defense-nationale.html');
|
|
264
|
-
```
|
|
265
|
-
|
|
266
|
-
Toutes les méthodes retournent la même structure de réponse :
|
|
267
|
-
|
|
268
|
-
```json
|
|
269
|
-
{
|
|
270
|
-
"data": "...",
|
|
271
|
-
"status": 200,
|
|
272
|
-
"statusText": "OK",
|
|
273
|
-
"headers": {
|
|
274
|
-
"content-type": "text/html"
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
```
|
|
1
|
+
([Français](#canada-api-1))
|
|
2
|
+
|
|
3
|
+
# canada-api
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/canada-api) [](https://github.com/dnd-mdn/canada-api/blob/main/LICENSE.md)
|
|
6
|
+
|
|
7
|
+
Cross platform API for fetching public data from [canada.ca](https://www.canada.ca).
|
|
8
|
+
|
|
9
|
+
## Browser
|
|
10
|
+
|
|
11
|
+
```html
|
|
12
|
+
<script src="https://cdn.jsdelivr.net/npm/canada-api@5.1.6"></script>
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Node 18+
|
|
16
|
+
|
|
17
|
+
### Install
|
|
18
|
+
|
|
19
|
+
```shell
|
|
20
|
+
npm install canada-api
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### Usage
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
import ca from 'canada-api'
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Testing
|
|
30
|
+
|
|
31
|
+
```shell
|
|
32
|
+
npm test
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Tests use the built-in Node.js test runner (`node:test`) and require Node 18 or later.
|
|
36
|
+
|
|
37
|
+
## API
|
|
38
|
+
|
|
39
|
+
### `ca.normalize(url)`
|
|
40
|
+
|
|
41
|
+
- `url` {string|URL} - Full URL or relative path (e.g. `'/en/page'` or `'https://www.canada.ca/en/page'`)
|
|
42
|
+
- Returns: {URL} Normalized URL object with cleaned pathname
|
|
43
|
+
|
|
44
|
+
Validates and normalizes a canada.ca URL. Strips the `/content/canadasite` prefix, file extensions, and trailing slashes.
|
|
45
|
+
|
|
46
|
+
Throws {TypeError} if `url` is not a string or URL object.
|
|
47
|
+
Throws {Error} if the URL is not on canada.ca or the path does not start with `/en/` or `/fr/`.
|
|
48
|
+
|
|
49
|
+
### `ca.children(url)`
|
|
50
|
+
|
|
51
|
+
- `url` {string|URL} - Absolute or relative URL
|
|
52
|
+
- Returns: {Promise} Fulfills with a response whose `data` is an array of sitemap entries
|
|
53
|
+
|
|
54
|
+
Fetches and parses the sitemap for the given page, returning its child pages. Entries without a `<loc>` element are skipped.
|
|
55
|
+
|
|
56
|
+
```json
|
|
57
|
+
{
|
|
58
|
+
"data": [
|
|
59
|
+
{
|
|
60
|
+
"path": "/en/department-national-defence/maple-leaf",
|
|
61
|
+
"lastmod": "2022-09-20T00:00:00.000Z"
|
|
62
|
+
}
|
|
63
|
+
],
|
|
64
|
+
"status": 200,
|
|
65
|
+
"statusText": "OK",
|
|
66
|
+
"headers": {
|
|
67
|
+
"content-type": "text/xml"
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### `ca.content(url)`
|
|
73
|
+
|
|
74
|
+
- `url` {string|URL} - Absolute or relative URL
|
|
75
|
+
- Returns: {Promise} Fulfills with a response whose `data` is the raw HTML string
|
|
76
|
+
|
|
77
|
+
Retrieves the HTML content of the page.
|
|
78
|
+
|
|
79
|
+
```json
|
|
80
|
+
{
|
|
81
|
+
"data": "<!DOCTYPE html>...",
|
|
82
|
+
"status": 200,
|
|
83
|
+
"statusText": "OK",
|
|
84
|
+
"headers": {
|
|
85
|
+
"content-type": "text/html"
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### `ca.meta(url)`
|
|
91
|
+
|
|
92
|
+
- `url` {string|URL} - Absolute or relative URL
|
|
93
|
+
- Returns: {Promise} Fulfills with a response whose `data` is a formatted metadata object
|
|
94
|
+
|
|
95
|
+
Fetches JCR metadata for the given page. The following transformations are applied:
|
|
96
|
+
|
|
97
|
+
- String `"true"` / `"false"` values are converted to booleans
|
|
98
|
+
- `@TypeHint` properties are removed
|
|
99
|
+
- Empty arrays are removed
|
|
100
|
+
- Date strings are converted to ISO 8601
|
|
101
|
+
- Keys are sorted alphabetically
|
|
102
|
+
- A normalized `peer` field is added when `gcAltLanguagePeer` is present
|
|
103
|
+
|
|
104
|
+
```json
|
|
105
|
+
{
|
|
106
|
+
"data": {
|
|
107
|
+
"cq:lastModified": "2022-10-25T19:16:28.000Z",
|
|
108
|
+
"fluidWidth": false,
|
|
109
|
+
"peer": "/fr/ministere-defense-nationale/feuille-erable"
|
|
110
|
+
},
|
|
111
|
+
"status": 200,
|
|
112
|
+
"statusText": "OK",
|
|
113
|
+
"headers": {
|
|
114
|
+
"content-type": "application/json"
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### `ca.request`
|
|
120
|
+
|
|
121
|
+
- `url` {string|URL} - Absolute or relative URL
|
|
122
|
+
- `options` {RequestInit} - Optional fetch options
|
|
123
|
+
- Returns: {Promise} Fulfills with a response object
|
|
124
|
+
|
|
125
|
+
Raw HTTP client with `https://www.canada.ca` as the base URL. Use this for any requests not covered by the methods above. No URL transformation is applied. Response bodies with a `application/json` content type are automatically parsed.
|
|
126
|
+
|
|
127
|
+
```js
|
|
128
|
+
const response = await ca.request('/en/department-national-defence.html');
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
All methods return the same response shape:
|
|
132
|
+
|
|
133
|
+
```json
|
|
134
|
+
{
|
|
135
|
+
"data": "...",
|
|
136
|
+
"status": 200,
|
|
137
|
+
"statusText": "OK",
|
|
138
|
+
"headers": {
|
|
139
|
+
"content-type": "text/html"
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
---
|
|
145
|
+
|
|
146
|
+
# canada-api
|
|
147
|
+
|
|
148
|
+
[](https://www.npmjs.com/package/canada-api) [](https://github.com/dnd-mdn/canada-api/blob/main/LICENSE.md)
|
|
149
|
+
|
|
150
|
+
API multiplateforme pour récupérer des données publiques de [canada.ca](https://www.canada.ca).
|
|
151
|
+
|
|
152
|
+
## Navigateur
|
|
153
|
+
|
|
154
|
+
```html
|
|
155
|
+
<script src="https://cdn.jsdelivr.net/npm/canada-api@5.1.6"></script>
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## Node 18+
|
|
159
|
+
|
|
160
|
+
### Installation
|
|
161
|
+
|
|
162
|
+
```shell
|
|
163
|
+
npm install canada-api
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### Utilisation
|
|
167
|
+
|
|
168
|
+
```js
|
|
169
|
+
import ca from 'canada-api'
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## API
|
|
173
|
+
|
|
174
|
+
### `ca.normalize(url)`
|
|
175
|
+
|
|
176
|
+
- `url` {string|URL} - URL complète ou chemin relatif (p. ex. `'/fr/page'` ou `'https://www.canada.ca/fr/page'`)
|
|
177
|
+
- Retourne: {URL} Objet URL normalisé avec un chemin nettoyé
|
|
178
|
+
|
|
179
|
+
Valide et normalise une URL de canada.ca. Supprime le préfixe `/content/canadasite`, les extensions de fichier et les barres obliques finales.
|
|
180
|
+
|
|
181
|
+
Lève {TypeError} si `url` n'est pas une chaîne ou un objet URL.
|
|
182
|
+
Lève {Error} si l'URL n'est pas sur canada.ca ou si le chemin ne commence pas par `/en/` ou `/fr/`.
|
|
183
|
+
|
|
184
|
+
### `ca.children(url)`
|
|
185
|
+
|
|
186
|
+
- `url` {string|URL} - URL absolue ou relative
|
|
187
|
+
- Retourne: {Promise} Résout avec une réponse dont `data` est un tableau d'entrées du plan de site
|
|
188
|
+
|
|
189
|
+
Récupère et analyse le plan de site de la page donnée, retournant ses pages enfants. Les entrées sans élément `<loc>` sont ignorées.
|
|
190
|
+
|
|
191
|
+
```json
|
|
192
|
+
{
|
|
193
|
+
"data": [
|
|
194
|
+
{
|
|
195
|
+
"path": "/fr/ministere-defense-nationale/feuille-erable",
|
|
196
|
+
"lastmod": "2022-09-20T00:00:00.000Z"
|
|
197
|
+
}
|
|
198
|
+
],
|
|
199
|
+
"status": 200,
|
|
200
|
+
"statusText": "OK",
|
|
201
|
+
"headers": {
|
|
202
|
+
"content-type": "text/xml"
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### `ca.content(url)`
|
|
208
|
+
|
|
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
|
|
211
|
+
|
|
212
|
+
Récupère le contenu HTML de la page.
|
|
213
|
+
|
|
214
|
+
```json
|
|
215
|
+
{
|
|
216
|
+
"data": "<!DOCTYPE html>...",
|
|
217
|
+
"status": 200,
|
|
218
|
+
"statusText": "OK",
|
|
219
|
+
"headers": {
|
|
220
|
+
"content-type": "text/html"
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
### `ca.meta(url)`
|
|
226
|
+
|
|
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 formaté
|
|
229
|
+
|
|
230
|
+
Récupère les métadonnées JCR de la page donnée. Les transformations suivantes sont appliquées :
|
|
231
|
+
|
|
232
|
+
- Les valeurs `"true"` / `"false"` sont converties en booléens
|
|
233
|
+
- Les propriétés `@TypeHint` sont supprimées
|
|
234
|
+
- Les tableaux vides sont supprimés
|
|
235
|
+
- Les chaînes de date sont converties en ISO 8601
|
|
236
|
+
- Les clés sont triées alphabétiquement
|
|
237
|
+
- Un champ `peer` normalisé est ajouté lorsque `gcAltLanguagePeer` est présent
|
|
238
|
+
|
|
239
|
+
```json
|
|
240
|
+
{
|
|
241
|
+
"data": {
|
|
242
|
+
"cq:lastModified": "2022-10-25T19:16:28.000Z",
|
|
243
|
+
"fluidWidth": false,
|
|
244
|
+
"peer": "/en/department-national-defence/maple-leaf"
|
|
245
|
+
},
|
|
246
|
+
"status": 200,
|
|
247
|
+
"statusText": "OK",
|
|
248
|
+
"headers": {
|
|
249
|
+
"content-type": "application/json"
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
### `ca.request`
|
|
255
|
+
|
|
256
|
+
- `url` {string|URL} - URL absolue ou relative
|
|
257
|
+
- `options` {RequestInit} - Options fetch optionnelles
|
|
258
|
+
- Retourne: {Promise} Résout avec un objet réponse
|
|
259
|
+
|
|
260
|
+
Client HTTP brut avec `https://www.canada.ca` comme URL de base. Utilisez-le pour toute requête non couverte par les méthodes ci-dessus. Aucune transformation d'URL n'est appliquée. Les corps de réponse avec un type de contenu `application/json` sont automatiquement analysés.
|
|
261
|
+
|
|
262
|
+
```js
|
|
263
|
+
const response = await ca.request('/fr/ministere-defense-nationale.html');
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
Toutes les méthodes retournent la même structure de réponse :
|
|
267
|
+
|
|
268
|
+
```json
|
|
269
|
+
{
|
|
270
|
+
"data": "...",
|
|
271
|
+
"status": 200,
|
|
272
|
+
"statusText": "OK",
|
|
273
|
+
"headers": {
|
|
274
|
+
"content-type": "text/html"
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
```
|
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
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "canada-api",
|
|
3
|
-
"version": "5.1.
|
|
4
|
-
"description": "Cross platform API to fetch data from canada.ca",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "src/index.js",
|
|
7
|
-
"browser": "dist/ca.js",
|
|
8
|
-
"exports": {
|
|
9
|
-
".": {
|
|
10
|
-
"browser": "./dist/ca.js",
|
|
11
|
-
"default": "./src/index.js"
|
|
12
|
-
}
|
|
13
|
-
},
|
|
14
|
-
"files": [
|
|
15
|
-
"src",
|
|
16
|
-
"dist"
|
|
17
|
-
],
|
|
18
|
-
"scripts": {
|
|
19
|
-
"test": "node --test tests/*.test.js",
|
|
20
|
-
"test:integration": "node --test tests/integration/*.js",
|
|
21
|
-
"build": "webpack",
|
|
22
|
-
"dev": "webpack --mode development"
|
|
23
|
-
},
|
|
24
|
-
"author": "National Defence",
|
|
25
|
-
"license": "MIT",
|
|
26
|
-
"engines": {
|
|
27
|
-
"node": ">=18"
|
|
28
|
-
},
|
|
29
|
-
"keywords": [
|
|
30
|
-
"canada",
|
|
31
|
-
"api",
|
|
32
|
-
"fetch"
|
|
33
|
-
],
|
|
34
|
-
"homepage": "https://github.com/dnd-mdn/canada-api#readme",
|
|
35
|
-
"bugs": "https://github.com/dnd-mdn/canada-api/issues",
|
|
36
|
-
"repository": {
|
|
37
|
-
"type": "git",
|
|
38
|
-
"url": "https://github.com/dnd-mdn/canada-api.git"
|
|
39
|
-
},
|
|
40
|
-
"devDependencies": {
|
|
41
|
-
"webpack": "^5.105.4",
|
|
42
|
-
"webpack-cli": "^7.0.2"
|
|
43
|
-
}
|
|
44
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "canada-api",
|
|
3
|
+
"version": "5.1.6",
|
|
4
|
+
"description": "Cross platform API to fetch data from canada.ca",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "src/index.js",
|
|
7
|
+
"browser": "dist/ca.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"browser": "./dist/ca.js",
|
|
11
|
+
"default": "./src/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"src",
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"test": "node --test tests/*.test.js",
|
|
20
|
+
"test:integration": "node --test tests/integration/*.js",
|
|
21
|
+
"build": "webpack",
|
|
22
|
+
"dev": "webpack --mode development"
|
|
23
|
+
},
|
|
24
|
+
"author": "National Defence",
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=18"
|
|
28
|
+
},
|
|
29
|
+
"keywords": [
|
|
30
|
+
"canada",
|
|
31
|
+
"api",
|
|
32
|
+
"fetch"
|
|
33
|
+
],
|
|
34
|
+
"homepage": "https://github.com/dnd-mdn/canada-api#readme",
|
|
35
|
+
"bugs": "https://github.com/dnd-mdn/canada-api/issues",
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "https://github.com/dnd-mdn/canada-api.git"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"webpack": "^5.105.4",
|
|
42
|
+
"webpack-cli": "^7.0.2"
|
|
43
|
+
}
|
|
44
|
+
}
|
package/src/children.js
CHANGED
package/src/content.js
CHANGED
|
@@ -10,10 +10,8 @@ import request from "./request.js";
|
|
|
10
10
|
const content = async (url) => {
|
|
11
11
|
const target = normalize(url);
|
|
12
12
|
target.pathname += '.html';
|
|
13
|
-
target.searchParams.set('_', Date.now());
|
|
14
13
|
|
|
15
|
-
return request(target, {
|
|
16
|
-
signal: AbortSignal.timeout(10000),
|
|
14
|
+
return request(target, {
|
|
17
15
|
redirect: 'error'
|
|
18
16
|
});
|
|
19
17
|
};
|
package/src/meta.js
CHANGED
|
@@ -89,10 +89,8 @@ export const formatMeta = (data) => {
|
|
|
89
89
|
const meta = async (url) => {
|
|
90
90
|
const target = normalize(url);
|
|
91
91
|
target.pathname += '/_jcr_content.json';
|
|
92
|
-
target.searchParams.set('_', Date.now());
|
|
93
92
|
|
|
94
93
|
const response = await request(target, {
|
|
95
|
-
signal: AbortSignal.timeout(10000),
|
|
96
94
|
redirect: 'error'
|
|
97
95
|
});
|
|
98
96
|
|
package/src/request.js
CHANGED
|
@@ -1,40 +1,62 @@
|
|
|
1
|
-
import { BASE_URL } from "./config.js";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
*
|
|
8
|
-
* @
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
1
|
+
import { BASE_URL } from "./config.js";
|
|
2
|
+
|
|
3
|
+
/** @type {number} Default request timeout in ms. Callers can override by passing a `signal` in options. */
|
|
4
|
+
const DEFAULT_TIMEOUT = 30000;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Raw HTTP client for canada.ca
|
|
8
|
+
* @param {string|URL} url - Relative or absolute URL on canada.ca
|
|
9
|
+
* @param {RequestInit} [options] - Fetch options
|
|
10
|
+
* @returns {Promise<{data: string|object, status: number, statusText: string, headers: object}>}
|
|
11
|
+
* @throws {Error} If the request fails or returns a non-2xx status
|
|
12
|
+
*/
|
|
13
|
+
const request = async (url, options = {}) => {
|
|
14
|
+
url = new URL(url, BASE_URL);
|
|
15
|
+
url.searchParams.set('_', Date.now());
|
|
16
|
+
|
|
17
|
+
const { headers: customHeaders = {}, ...requestOptions } = options;
|
|
18
|
+
|
|
19
|
+
let response;
|
|
20
|
+
try {
|
|
21
|
+
response = await fetch(url, {
|
|
22
|
+
signal: AbortSignal.timeout(DEFAULT_TIMEOUT),
|
|
23
|
+
...requestOptions,
|
|
24
|
+
headers: {
|
|
25
|
+
'User-Agent': 'canada-api/5.1.6',
|
|
26
|
+
'Accept': '*/*',
|
|
27
|
+
...customHeaders
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
} catch (e) {
|
|
31
|
+
e.url = url.toString();
|
|
32
|
+
throw e;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (!response.ok) {
|
|
36
|
+
const error = new Error(`Request to ${url} failed: ${response.status} ${response.statusText}`);
|
|
37
|
+
error.url = url.toString();
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
let data = await response.text();
|
|
42
|
+
const isJson = response.headers.get('content-type')?.includes('application/json');
|
|
43
|
+
|
|
44
|
+
if (isJson) {
|
|
45
|
+
try {
|
|
46
|
+
data = JSON.parse(data);
|
|
47
|
+
} catch (e) {
|
|
48
|
+
const error = new Error(`Failed to parse JSON response from ${url}: ${e.message}`);
|
|
49
|
+
error.url = url.toString();
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
data,
|
|
56
|
+
status: response.status,
|
|
57
|
+
statusText: response.statusText,
|
|
58
|
+
headers: Object.fromEntries(response.headers)
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
|
|
40
62
|
export default request;
|