Skip to content

Commit 9544b16

Browse files
feat: better follow redirect, catch more errors, edit docs and examples
1 parent 7b2f46a commit 9544b16

10 files changed

+2132
-811
lines changed

.gitignore

-60
Original file line numberDiff line numberDiff line change
@@ -1,61 +1 @@
1-
# Logs
2-
logs
3-
*.log
4-
npm-debug.log*
5-
yarn-debug.log*
6-
yarn-error.log*
7-
8-
# Runtime data
9-
pids
10-
*.pid
11-
*.seed
12-
*.pid.lock
13-
14-
# Directory for instrumented libs generated by jscoverage/JSCover
15-
lib-cov
16-
17-
# Coverage directory used by tools like istanbul
18-
coverage
19-
20-
# nyc test coverage
21-
.nyc_output
22-
23-
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24-
.grunt
25-
26-
# Bower dependency directory (https://bower.io/)
27-
bower_components
28-
29-
# node-waf configuration
30-
.lock-wscript
31-
32-
# Compiled binary addons (https://nodejs.org/api/addons.html)
33-
build/Release
34-
35-
# Dependency directories
361
node_modules/
37-
jspm_packages/
38-
39-
# TypeScript v1 declaration files
40-
typings/
41-
42-
# Optional npm cache directory
43-
.npm
44-
45-
# Optional eslint cache
46-
.eslintcache
47-
48-
# Optional REPL history
49-
.node_repl_history
50-
51-
# Output of 'npm pack'
52-
*.tgz
53-
54-
# Yarn Integrity file
55-
.yarn-integrity
56-
57-
# dotenv environment variables file
58-
.env
59-
60-
# next.js build output
61-
.next

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ jobs:
88
node_js: lts/*
99
script:
1010
- npm run lint
11+
- npm run test
1112
deploy:
1213
provider: script
13-
skip_cleanup: true
1414
script:
1515
- npx semantic-release

README.md

+44-45
Original file line numberDiff line numberDiff line change
@@ -16,57 +16,56 @@ $ npm install lod-opendata
1616
# Usage
1717

1818
```js
19-
const lodAPI = require("./lod-opendata.js")
19+
const opendata = require('lod-opendata')
2020

21-
// Example : get all fields
22-
const getAllFields = async () => {
23-
const data = await lodAPI()
24-
console.log('getAllFields: ', data)
25-
}
26-
getAllFields()
21+
// Example with async / Await
2722

28-
// Example : get all fields from resources field
29-
const getFieldResources = async () => {
30-
const fields = "resources"
31-
const data = await lodAPI(fields)
32-
console.log('getFieldResources : ', data)
23+
const asyncAwait = async () => {
24+
//Get all fields. Use defaults
25+
try {
26+
const result = await opendata()
27+
console.log(result)
28+
} catch (error) {
29+
console.log(error.message)
30+
}
3331
}
34-
getFieldResources()
32+
asyncAwait()
3533

36-
// Example : get the url field from resources field
37-
const getFieldResourcesUrl = async () => {
38-
const fields = "resources/{url}"
39-
const data = await lodAPI(fields)
40-
console.log('getFieldResourcesUrl:', data)
41-
}
42-
getFieldResourcesUrl()
34+
// Example with then / catch
4335

44-
// Example : get multiple fields
45-
const getMultipleFields = async () => {
46-
const fields = "page,title,tags"
47-
const data = await lodAPI(fields)
48-
console.log('getMultipleFields: ', data)
49-
}
50-
getMultipleFields()
51-
52-
// Example : get multiple subfields
53-
const getMultipleSubfields = async () => {
54-
const fields = "resources/{format,title,url}"
55-
const { resources: [{ format, title, url }] } = await lodAPI(fields)
56-
console.log('format: ', format)
57-
console.log('title: ', title)
58-
console.log('url: ', url)
59-
}
60-
getMultipleSubfields()
61-
62-
// Example : get all fields from resources field from a custom endPoint
63-
const getCustomEndPoint = async () => {
64-
const customEndPoint = 'https://data.public.lu/api/1/datasets/letzebuerger-online-dictionnaire-raw-data/'
65-
const fields = "resources"
66-
const data = await lodAPI(fields, customEndPoint)
67-
console.log('getCustomEndPoint : ', data)
36+
const thenCatch = () => {
37+
//Get all fields. Use defaults
38+
opendata()
39+
.then((obj) => console.log(obj))
40+
.catch((err) => console.log(err.message))
41+
42+
//Get all fields from the main `resources` field'
43+
opendata('resources')
44+
.then((obj) => console.log(obj))
45+
.catch((err) => console.log(err.message))
46+
47+
//Get the `url` field from `resources` field'
48+
opendata('resources/{url}')
49+
.then(({ resources: [{ url }] }) => console.log(url))
50+
.catch((err) => console.log(err.message))
51+
52+
//Get multiple main fields: `page`, `title`, `slug`
53+
opendata('page,title,slug')
54+
.then(({ page, title, slug }) => console.log(page, title, slug))
55+
.catch((err) => console.log(err.message))
56+
57+
//Get multiple subfields: `id`, `published`, `latest` from main field `resources`
58+
opendata('resources/{id,published,latest}')
59+
.then(({ resources: [{ id, published, latest }] }) => console.log(id, published, latest))
60+
.catch((err) => console.log(err.message))
61+
62+
//Get all fields pass a custom api `url`. Useful if the API URL changes.
63+
const url = 'http://data.public.lu/api/1/datasets/letzebuerger-online-dictionnaire-raw-data'
64+
opendata('', url)
65+
.then((obj) => console.log(obj))
66+
.catch((err) => console.log(err.message))
6867
}
69-
getCustomEndPoint()
68+
thenCatch()
7069
```
7170

7271
See all fields available :<br>

example.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const opendata = require('lod-opendata')
2+
3+
// Example with async / Await
4+
5+
const asyncAwait = async () => {
6+
//Get all fields. Use defaults
7+
try {
8+
const result = await opendata()
9+
console.log(result)
10+
} catch (error) {
11+
console.log(error.message)
12+
}
13+
}
14+
asyncAwait()
15+
16+
// Example with then / catch
17+
18+
const thenCatch = () => {
19+
//Get all fields. Use defaults
20+
opendata()
21+
.then((obj) => console.log(obj))
22+
.catch((err) => console.log(err.message))
23+
24+
//Get all fields from the main `resources` field'
25+
opendata('resources')
26+
.then((obj) => console.log(obj))
27+
.catch((err) => console.log(err.message))
28+
29+
//Get the `url` field from `resources` field'
30+
opendata('resources/{url}')
31+
.then(({ resources: [{ url }] }) => console.log(url))
32+
.catch((err) => console.log(err.message))
33+
34+
//Get multiple main fields: `page`, `title`, `slug`
35+
opendata('page,title,slug')
36+
.then(({ page, title, slug }) => console.log(page, title, slug))
37+
.catch((err) => console.log(err.message))
38+
39+
//Get multiple subfields: `id`, `published`, `latest` from main field `resources`
40+
opendata('resources/{id,published,latest}')
41+
.then(({ resources: [{ id, published, latest }] }) => console.log(id, published, latest))
42+
.catch((err) => console.log(err.message))
43+
44+
//Get all fields pass a custom api `url`. Useful if the API URL changes.
45+
const url = 'http://data.public.lu/api/1/datasets/letzebuerger-online-dictionnaire-raw-data'
46+
opendata('', url)
47+
.then((obj) => console.log(obj))
48+
.catch((err) => console.log(err.message))
49+
}
50+
thenCatch()

index.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const opendata = (
2+
fields = '',
3+
url = 'https://data.public.lu/api/1/datasets/letzebuerger-online-dictionnaire/'
4+
) =>
5+
new Promise((resolve, reject) => {
6+
const { get } = url.startsWith('https') ? require('https') : require('http')
7+
try {
8+
get(url, { headers: { 'X-Fields': fields } }, (res) => {
9+
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location)
10+
resolve(opendata(fields, res.headers.location))
11+
if (res.statusCode !== 200)
12+
reject(new Error(`${res.statusCode} : ${res.statusMessage} ${url} FIELDS : ${fields}`))
13+
let rawdata = ''
14+
res.setEncoding('utf8')
15+
res
16+
.on('data', (data) => (rawdata += data))
17+
.on('end', () => {
18+
try {
19+
const jsondata = JSON.parse(rawdata)
20+
resolve(jsondata)
21+
} catch (err) {
22+
reject(err)
23+
}
24+
})
25+
})
26+
.on('error', reject)
27+
.end()
28+
} catch (err) {
29+
reject(err)
30+
}
31+
})
32+
33+
module.exports = opendata

0 commit comments

Comments
 (0)