You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

news-wrapper

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

news-wrapper

A Javascript library to work with API news

2.0.0
latest
npmnpm
Version published
Weekly downloads
24
Maintainers
1
Weekly downloads
 
Created
Source

news-wrapper

Build Status

[WIP] A Javascript wrapper to work with [News API] (https://newsapi.org).

Installation

$ npm install --save news-wrapper

How to use

ES6

// to import a specific method
import NewsWrapper from 'news-wrapper';

const news = new NewsWrapper({
  token: 'YOUR_TOKEN_HERE'
});

// using  method
const promise = news.search.topheadlines({ sources: 'cnn'});
promise.then(({ data }) => {
  const markup = data.articles.map(headline => {
    return `<div>${headline.title}</div>`;
  });

  document.getElementById('news').innerHTML = markup;
});

CommonJS

const NewsWrapper = require('news-wrapper').default;

const news = new NewsWrapper({
  token: 'YOUR_TOKEN_HERE'
});

UMD in Browser

<!-- to import non-minified version -->
<script src="news-wrapper.umd.js"></script>

<!-- to import minified version -->
<script src="news-wrapper.umd.min.js"></script>

After that the library will be available to the Global as NewsWrapper. Follow an example:


const news = new NewsWrapper({
  token: 'YOUR_TOKEN_HERE'
});

const sources = news.search.sources({ category: 'technology'});

Methods

Follow the methods that the library provides.

search.topheadlines(query)

This method provides live top and breaking headlines for a single source, or multiple sources. Test in npm runkit.

Arguments

ArgumentTypeOptions
querystring'Any search query'
sourcesstring'Any source query'
categorystring'Any category query'
languagestring'Any language'
countrystring'Any country'

The argument query is optional. In case nothing is passed, the method will retrieve Techcrunch available headlines. In case more than one source is needed, you can add a string separated by commas, like 'cnn,techcrunch,bbc' for example.

Default: Techcrunch.

For more details about country and language supported, please check NewsAPI

Example

news.search.topheadlines({ sources: 'cnn'});
  .then(({ data }) => {
    // do what you want with the data
  })

If you want to specify more than one source:

news.search.topheadlines({ sources: 'cnn,techcrunch,bbc' });
  .then(({ data }) => {
    // do what you want with the data
  })

search.sources(query)

This method will get all sources available. It's possible to filter by either category and/or country. Test in npm runkit.

Arguments

ArgumentTypeOptions
categorystring'Any of the below list'
countrystring'Any of the below list'
languagestring'Any of the below list'
  • Available categories: business entertainment gaming general health-and-medical music politics science-and-nature sport technology Default: all categories.

  • Available countries: ar au br ca cn de es fr gb hk ie in is it nl no pk ru sa sv us za Default: all countries.

  • Available languages: ar en cn de es fr he it nl no pt ru sv ud Default: all languages.

Example

news.search.sources({ category: 'technology', country: 'us' })
  .then(({ data } ) => {
    // do what you want with the data
  })

search.everything(query)

This method retrieves all news related to the query passed in. It includes all kind of sources. Test in npm runkit.

Arguments

ArgumentTypeOptions
querystring'Any search query'
sourcesstring'Any source query'
fromstring'Starting date'
tostring'Ending date'
pageint'Pagination number'

The only required parameter is query, all other parameters are optional. Both from and to needs to be in ISO 8601 format (e.g. 2018-01-02 or 2018-01-02T12:16:22)

  • Default page: 1

Example

news.search.everything({ query: 'bitcoin' })
  .then(({ data }) => {
    // do what you want with the data
  })
news.search.everything({ query: 'bitcoin', sources: 'cnn', from: '2017-12-23', to: '2017-12-28', page: 2 })
  .then(({ data }) => {
    // do what you want with the data
  })

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

License

This project is licensed under the ISC License - see the LICENSE.md file for details

Keywords

TDD

FAQs

Package last updated on 02 Jan 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts