@aller/labrador-utils

0.0.23 • Public • Published

Labrador Utilities

Greenkeeper badge wercker status Coverage Status

Install

yarn add @aller/labrador-utils

or

npm install @aller/labrador-utils

Modules

Labrador API

Init

const { api } = require('labrador-utils')
const labApi = api({ base: 'http://api.secret.labrador.api.lol', site_id: 1 })

High-level methods

labApi.getArticleById({ id: 9999999, args... }) // {Article}
labApi.getArticlesByTags({ tags:['test', 'bil'], args... }) // [{Article}...]
labApi.getArticlesByTag({ tag: 'test', args... }) // [{Article}...]
labApi.getArticlesBySection({ section: 'mat', args... }) // [{Article}...]
labApi.getFrontPageById({ id: 9999999, args... }) // { FrontPage }
labApi.getFragment({ id: 9999999, args... }, ?clean) // Returns HTML string. If clean is provided, the resulting HTML is parsed throuhg parse5 to ensure valid markup.

Article-based high level methods also have query-specific options e.g:

labApi.getArticleById({
  isFeatured: true,
  visibility_status: 'P'
})

Base methods

labApi.getQuery('/front', { query: 'id:999999', args... }).get() // { result: [{ FrontPage }...] }
labApi.getFrontPage({ query: 'id:9999999', args... }).getResult() // [{ FrontPage }...]
labApi.getArticles({ query: 'tag:test', args... }).getFrist() //  { Article }

All base methods have 3 request methods

get() -> { result: [ { ENTITY }...] }
getResult() -> [ {ENTITY }... ]
getFirst() -> { ENTITY }

Query builder

The base methods use the queryBuilder for parsing of the query-parameter. The queryBuilder takes nested JS objects/arrays as input and generates a query string as output.

labApi.getArticles({
    query: [{ tag: ['nyheter', 'bil'] }, { 'review_score': '*' }], 
}).getResult()
// query: (tag:(nyheter AND bil) AND review_score:*)

// The underlying queryBuilder method can also be directly accessed:
labApi.queryBuilder([{ tags: ['hello', { wat: ['test1', ['hello', 'hello2'], 'OR'] }] }])
// returns the string: '(tags:(hello AND wat:(test1 OR (hello AND hello2))))'

Concepts:

['nyheter', 'bil'] // -> (nyheter AND bil)
['nyheter', 'bil', 'OR'] // -> (nyheter OR bil), the 'OR' keyword needs to be the last el. of the array.
{ tag: ['nyheter', 'bil', 'OR']} // -> (tag:(nyheter OR bil))
// And any nested combination of these three concepts

Additional arguments

All additional args. on all methods, are sent as query params, e.g:

labApi.getArticlesByTags({ 
  tags: ['test', 'bil'], 
  limit: 5, 
  start: 5, 
  order: 'desc', 
  orderBy: 'published'
})

Defaults are found here

Readme

Keywords

none

Package Sidebar

Install

npm i @aller/labrador-utils

Weekly Downloads

3

Version

0.0.23

License

ISC

Last publish

Collaborators

  • aslak2800
  • roninjc
  • idanko
  • mariusjn
  • lobunto
  • konrad-j
  • oyvigri
  • goodleby
  • jimoe
  • vnorvik
  • kevinmidboe