1# libnpmsearch [![npm version](https://img.shields.io/npm/v/libnpmsearch.svg)](https://npm.im/libnpmsearch) [![license](https://img.shields.io/npm/l/libnpmsearch.svg)](https://npm.im/libnpmsearch) [![Travis](https://img.shields.io/travis/npm/libnpmsearch.svg)](https://travis-ci.org/npm/libnpmsearch) [![AppVeyor](https://ci.appveyor.com/api/projects/status/github/zkat/libnpmsearch?svg=true)](https://ci.appveyor.com/project/zkat/libnpmsearch) [![Coverage Status](https://coveralls.io/repos/github/npm/libnpmsearch/badge.svg?branch=latest)](https://coveralls.io/github/npm/libnpmsearch?branch=latest) 2 3[`libnpmsearch`](https://github.com/npm/libnpmsearch) is a Node.js library for 4programmatically accessing the npm search endpoint. It does **not** support 5legacy search through `/-/all`. 6 7## Example 8 9```js 10const search = require('libnpmsearch') 11 12console.log(await search('libnpm')) 13=> 14[ 15 { 16 name: 'libnpm', 17 description: 'programmatic npm API', 18 ...etc 19 }, 20 { 21 name: 'libnpmsearch', 22 description: 'Programmatic API for searching in npm and compatible registries', 23 ...etc 24 }, 25 ...more 26] 27``` 28 29## Install 30 31`$ npm install libnpmsearch` 32 33## Table of Contents 34 35* [Example](#example) 36* [Install](#install) 37* [API](#api) 38 * [search opts](#opts) 39 * [`search()`](#search) 40 * [`search.stream()`](#search-stream) 41 42### API 43 44#### <a name="opts"></a> `opts` for `libnpmsearch` commands 45 46The following opts are used directly by `libnpmsearch` itself: 47 48* `opts.limit` - Number of results to limit the query to. Default: 20 49* `opts.from` - Offset number for results. Used with `opts.limit` for pagination. Default: 0 50* `opts.detailed` - If true, returns an object with `package`, `score`, and `searchScore` fields, with `package` being what would usually be returned, and the other two containing details about how that package scored. Useful for UIs. Default: false 51* `opts.sortBy` - Used as a shorthand to set `opts.quality`, `opts.maintenance`, and `opts.popularity` with values that prioritize each one. Should be one of `'optimal'`, `'quality'`, `'maintenance'`, or `'popularity'`. Default: `'optimal'` 52* `opts.maintenance` - Decimal number between `0` and `1` that defines the weight of `maintenance` metrics when scoring and sorting packages. Default: `0.65` (same as `opts.sortBy: 'optimal'`) 53* `opts.popularity` - Decimal number between `0` and `1` that defines the weight of `popularity` metrics when scoring and sorting packages. Default: `0.98` (same as `opts.sortBy: 'optimal'`) 54* `opts.quality` - Decimal number between `0` and `1` that defines the weight of `quality` metrics when scoring and sorting packages. Default: `0.5` (same as `opts.sortBy: 'optimal'`) 55 56`libnpmsearch` uses [`npm-registry-fetch`](https://npm.im/npm-registry-fetch). 57Most options are passed through directly to that library, so please refer to 58[its own `opts` 59documentation](https://www.npmjs.com/package/npm-registry-fetch#fetch-options) 60for options that can be passed in. 61 62A couple of options of note for those in a hurry: 63 64* `opts.token` - can be passed in and will be used as the authentication token for the registry. For other ways to pass in auth details, see the n-r-f docs. 65* `opts.Promise` - If you pass this in, the Promises returned by `libnpmsearch` commands will use this Promise class instead. For example: `{Promise: require('bluebird')}` 66 67#### <a name="search"></a> `> search(query, [opts]) -> Promise` 68 69`query` must be either a String or an Array of search terms. 70 71If `opts.limit` is provided, it will be sent to the API to constrain the number 72of returned results. You may receive more, or fewer results, at the endpoint's 73discretion. 74 75The returned Promise resolved to an Array of search results with the following 76format: 77 78```js 79{ 80 name: String, 81 version: SemverString, 82 description: String || null, 83 maintainers: [ 84 { 85 username: String, 86 email: String 87 }, 88 ...etc 89 ] || null, 90 keywords: [String] || null, 91 date: Date || null 92} 93``` 94 95If `opts.limit` is provided, it will be sent to the API to constrain the number 96of returned results. You may receive more, or fewer results, at the endpoint's 97discretion. 98 99For streamed results, see [`search.stream`](#search-stream). 100 101##### Example 102 103```javascript 104await search('libnpm') 105=> 106[ 107 { 108 name: 'libnpm', 109 description: 'programmatic npm API', 110 ...etc 111 }, 112 { 113 name: 'libnpmsearch', 114 description: 'Programmatic API for searching in npm and compatible registries', 115 ...etc 116 }, 117 ...more 118] 119``` 120 121#### <a name="search-stream"></a> `> search.stream(query, [opts]) -> Stream` 122 123`query` must be either a String or an Array of search terms. 124 125If `opts.limit` is provided, it will be sent to the API to constrain the number 126of returned results. You may receive more, or fewer results, at the endpoint's 127discretion. 128 129The returned Stream emits one entry per search result, with each entry having 130the following format: 131 132```js 133{ 134 name: String, 135 version: SemverString, 136 description: String || null, 137 maintainers: [ 138 { 139 username: String, 140 email: String 141 }, 142 ...etc 143 ] || null, 144 keywords: [String] || null, 145 date: Date || null 146} 147``` 148 149For getting results in one chunk, see [`search`](#search-stream). 150 151##### Example 152 153```javascript 154search.stream('libnpm').on('data', console.log) 155=> 156// entry 1 157{ 158 name: 'libnpm', 159 description: 'programmatic npm API', 160 ...etc 161} 162// entry 2 163{ 164 name: 'libnpmsearch', 165 description: 'Programmatic API for searching in npm and compatible registries', 166 ...etc 167} 168// etc 169``` 170