canada-api
v5.0.0
Published
Cross platform API to fetch data from canada.ca
Readme
canada-api
Cross platform API for fetching public data from canada.ca.
Browser
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>Node 18+
Install
npm install canada-apiUsage
import ca from 'canada-api'Testing
npm testTests use the built-in Node.js test runner (node:test) and require Node 18 or later.
Core API
ca.normalize(url)
url{string|URL} - Full URL or relative path (e.g.'/en/page'or'https://www.canada.ca/en/page')- Returns: {URL} Normalized URL object with cleaned pathname
Validates and normalizes a canada.ca URL. Strips the /content/canadasite prefix, file extensions, and trailing slashes.
Throws {TypeError} if url is not a string or URL object.
Throws {Error} if the URL is not on canada.ca or the path does not start with /en/ or /fr/.
ca.request(url)
url{string|URL} - Relative or absolute URL on canada.ca- Returns: {Promise} Fulfills with an axios response object
Raw HTTP client for canada.ca. No URL transformation is applied.
{
"data": {},
"status": 200,
"statusText": "OK",
"headers": {},
"config": {},
"request": {}
}Basic API
ca.children(url)
url{string|URL} - Absolute or relative URL- Returns: {Promise} Fulfills with an axios response whose
datais an array of sitemap entries
Fetches and parses the sitemap for the given page, returning its child pages. Entries without a <loc> element are skipped.
{
"data": [
{
"path": "/en/department-national-defence/maple-leaf",
"lastmod": "2022-09-20T00:00:00.000Z"
}
]
}ca.content(url)
url{string|URL} - Absolute or relative URL- Returns: {Promise} Fulfills with an axios response whose
datais the raw HTML string
Retrieves the HTML content of the page.
{
"data": "<!DOCTYPE html>\r\n...."
}ca.meta(url)
url{string|URL} - Absolute or relative URL- Returns: {Promise} Fulfills with an axios response whose
datais a formatted metadata object
Fetches JCR metadata for the given page. The following transformations are applied:
- String
"true"/"false"values are converted to booleans @TypeHintproperties are removed- Empty arrays are removed
- Date strings are converted to ISO 8601
- Keys are sorted alphabetically
- A normalized
peerfield is added whengcAltLanguagePeeris present
{
"data": {
"cq:lastModified": "2022-10-25T19:16:28.000Z",
"fluidWidth": false,
"peer": "/fr/ministere-defense-nationale/feuille-erable"
}
}