Skip to content

Commit

Permalink
feat: better follow redirect, catch more errors, edit docs and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
robertoentringer committed Jul 22, 2020
1 parent 7b2f46a commit 9544b16
Show file tree
Hide file tree
Showing 10 changed files with 2,132 additions and 811 deletions.
60 changes: 0 additions & 60 deletions .gitignore
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ jobs:
node_js: lts/*
script:
- npm run lint
- npm run test
deploy:
provider: script
skip_cleanup: true
script:
- npx semantic-release
89 changes: 44 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,57 +16,56 @@ $ npm install lod-opendata
# Usage

```js
const lodAPI = require("./lod-opendata.js")
const opendata = require('lod-opendata')

// Example : get all fields
const getAllFields = async () => {
const data = await lodAPI()
console.log('getAllFields: ', data)
}
getAllFields()
// Example with async / Await

// Example : get all fields from resources field
const getFieldResources = async () => {
const fields = "resources"
const data = await lodAPI(fields)
console.log('getFieldResources : ', data)
const asyncAwait = async () => {
//Get all fields. Use defaults
try {
const result = await opendata()
console.log(result)
} catch (error) {
console.log(error.message)
}
}
getFieldResources()
asyncAwait()

// Example : get the url field from resources field
const getFieldResourcesUrl = async () => {
const fields = "resources/{url}"
const data = await lodAPI(fields)
console.log('getFieldResourcesUrl:', data)
}
getFieldResourcesUrl()
// Example with then / catch

// Example : get multiple fields
const getMultipleFields = async () => {
const fields = "page,title,tags"
const data = await lodAPI(fields)
console.log('getMultipleFields: ', data)
}
getMultipleFields()

// Example : get multiple subfields
const getMultipleSubfields = async () => {
const fields = "resources/{format,title,url}"
const { resources: [{ format, title, url }] } = await lodAPI(fields)
console.log('format: ', format)
console.log('title: ', title)
console.log('url: ', url)
}
getMultipleSubfields()

// Example : get all fields from resources field from a custom endPoint
const getCustomEndPoint = async () => {
const customEndPoint = 'https://data.public.lu/api/1/datasets/letzebuerger-online-dictionnaire-raw-data/'
const fields = "resources"
const data = await lodAPI(fields, customEndPoint)
console.log('getCustomEndPoint : ', data)
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))
}
getCustomEndPoint()
thenCatch()
```

See all fields available :<br>
Expand Down
50 changes: 50 additions & 0 deletions example.js
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()
33 changes: 33 additions & 0 deletions index.js
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
Loading

0 comments on commit 9544b16

Please sign in to comment.