-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: better follow redirect, catch more errors, edit docs and examples
- Loading branch information
1 parent
7b2f46a
commit 9544b16
Showing
10 changed files
with
2,132 additions
and
811 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
*.pid.lock | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# nyc test coverage | ||
.nyc_output | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# Bower dependency directory (https://bower.io/) | ||
bower_components | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (https://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directories | ||
node_modules/ | ||
jspm_packages/ | ||
|
||
# TypeScript v1 declaration files | ||
typings/ | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional eslint cache | ||
.eslintcache | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# Output of 'npm pack' | ||
*.tgz | ||
|
||
# Yarn Integrity file | ||
.yarn-integrity | ||
|
||
# dotenv environment variables file | ||
.env | ||
|
||
# next.js build output | ||
.next |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const opendata = require('lod-opendata') | ||
|
||
// Example with async / Await | ||
|
||
const asyncAwait = async () => { | ||
//Get all fields. Use defaults | ||
try { | ||
const result = await opendata() | ||
console.log(result) | ||
} catch (error) { | ||
console.log(error.message) | ||
} | ||
} | ||
asyncAwait() | ||
|
||
// Example with then / catch | ||
|
||
const thenCatch = () => { | ||
//Get all fields. Use defaults | ||
opendata() | ||
.then((obj) => console.log(obj)) | ||
.catch((err) => console.log(err.message)) | ||
|
||
//Get all fields from the main `resources` field' | ||
opendata('resources') | ||
.then((obj) => console.log(obj)) | ||
.catch((err) => console.log(err.message)) | ||
|
||
//Get the `url` field from `resources` field' | ||
opendata('resources/{url}') | ||
.then(({ resources: [{ url }] }) => console.log(url)) | ||
.catch((err) => console.log(err.message)) | ||
|
||
//Get multiple main fields: `page`, `title`, `slug` | ||
opendata('page,title,slug') | ||
.then(({ page, title, slug }) => console.log(page, title, slug)) | ||
.catch((err) => console.log(err.message)) | ||
|
||
//Get multiple subfields: `id`, `published`, `latest` from main field `resources` | ||
opendata('resources/{id,published,latest}') | ||
.then(({ resources: [{ id, published, latest }] }) => console.log(id, published, latest)) | ||
.catch((err) => console.log(err.message)) | ||
|
||
//Get all fields pass a custom api `url`. Useful if the API URL changes. | ||
const url = 'http://data.public.lu/api/1/datasets/letzebuerger-online-dictionnaire-raw-data' | ||
opendata('', url) | ||
.then((obj) => console.log(obj)) | ||
.catch((err) => console.log(err.message)) | ||
} | ||
thenCatch() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const opendata = ( | ||
fields = '', | ||
url = 'https://data.public.lu/api/1/datasets/letzebuerger-online-dictionnaire/' | ||
) => | ||
new Promise((resolve, reject) => { | ||
const { get } = url.startsWith('https') ? require('https') : require('http') | ||
try { | ||
get(url, { headers: { 'X-Fields': fields } }, (res) => { | ||
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) | ||
resolve(opendata(fields, res.headers.location)) | ||
if (res.statusCode !== 200) | ||
reject(new Error(`${res.statusCode} : ${res.statusMessage} ${url} FIELDS : ${fields}`)) | ||
let rawdata = '' | ||
res.setEncoding('utf8') | ||
res | ||
.on('data', (data) => (rawdata += data)) | ||
.on('end', () => { | ||
try { | ||
const jsondata = JSON.parse(rawdata) | ||
resolve(jsondata) | ||
} catch (err) { | ||
reject(err) | ||
} | ||
}) | ||
}) | ||
.on('error', reject) | ||
.end() | ||
} catch (err) { | ||
reject(err) | ||
} | ||
}) | ||
|
||
module.exports = opendata |
Oops, something went wrong.