├── .gitignore ├── package.json ├── README.md └── cli-1881.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cli-1881", 3 | "version": "0.1.0", 4 | "description": "CLI search client for 1881", 5 | "main": "cli-1881.js", 6 | "scripts": { 7 | "start": "node cli-1881.js" 8 | }, 9 | "bin": { 10 | "cli1881": "./cli-1881.js" 11 | }, 12 | "author": "Kristian Byrkjeland ", 13 | "repository": "https://github.com/opp1881/cli-1881", 14 | "license": "ISC", 15 | "dependencies": { 16 | "node-fetch": "^2.6.0" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cli-1881 2 | 3 | A command line search tool built on the Opplysningen 1881 API. To use, you will need a valid subscription key. Please register for a subscription on [api1881.no](https://www.api1881.no) 4 | 5 | Install the subscription key to your environment variables: 6 | `$ export OPPLYSNINGEN_SUBSCRIPTION_KEY=[your_key]` 7 | 8 | Install the client package 9 | `$ npm i -g cli-1881` 10 | 11 | Run your query from the command line! 12 | `$ cli1881 pizza oslo` 13 | 14 | More search functionality will be added. 15 | -------------------------------------------------------------------------------- /cli-1881.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | const fetch = require('node-fetch') 3 | 4 | const subscriptionKey = process.env.OPPLYSNINGEN_SUBSCRIPTION_KEY; 5 | if (!subscriptionKey) { 6 | console.error('Environment variable for your subscription key is not set.') 7 | }; 8 | 9 | const headers = { 10 | 'Ocp-Apim-Subscription-Key': subscriptionKey 11 | } 12 | 13 | function search(query='', type='unit', page=1, limit=3) { 14 | const queryParams = new URLSearchParams({ 15 | query, 16 | page, 17 | limit 18 | }) 19 | const url = `https://services.api1881.no/search/${type}?${queryParams.toString()}` 20 | return fetch(url, {headers: headers}) 21 | .then(response => response.json()) 22 | } 23 | 24 | function lookupId(id) { 25 | const url = `https://services.api1881.no/lookup/id/${id}` 26 | return fetch(url, {headers: headers}) 27 | .then(response => response.json()) 28 | } 29 | 30 | async function search1881(query) { 31 | try { 32 | const res = await search(query) 33 | 34 | res.contacts.forEach(async (contact) => { 35 | lookupresult = await lookupId(contact.id) 36 | lookupresult.contacts.forEach((contact) => { 37 | const name = contact.type === 'Person' ? `${contact.firstName} ${contact.lastName}` : contact.name 38 | const phone = contact.contactPoints != null ? `${contact.contactPoints[0].label}: ${contact.contactPoints[0].value}` : '' 39 | const address = contact.geography != null ? contact.geography.address.addressString : '' 40 | 41 | console.log(`${phone}, ${name}, ${address} | ${contact.id} ${contact.type}`) 42 | }) 43 | }); 44 | 45 | } catch (error) { 46 | console.error('err: ' + error.message); 47 | } 48 | } 49 | console.log('Querying for: ' + process.argv.slice(2)) 50 | search1881(process.argv.slice(2)) 51 | --------------------------------------------------------------------------------