# api-stream

> Easily create streaming, rate-limited APIs

Latest version **1.0.0** (published 2016-08-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install api-stream
pnpm add api-stream
yarn add api-stream
bun add api-stream
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2016-08-09 |
| First published | 2016-08-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=4 |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Keno Schwalb |
| Maintainers | caerostris |
| Keywords | api, stream, query, limit, rate, rate limit, query limit, streaming |

## Links

- npm: https://www.npmjs.com/package/api-stream
- Repository: https://github.com/ubilabs/node-api-stream
- Homepage: https://github.com/ubilabs/node-api-stream#readme
- Issues: https://github.com/ubilabs/node-api-stream/issues
- npm.io page: https://npm.io/package/api-stream

## Dependencies (2)

- [flat-file-db](https://npm.io/package/flat-file-db.md) ^1.0.0
- [throttled-transform-stream](https://npm.io/package/throttled-transform-stream.md) ^1.0.0

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2016-08-09

## README

# api-stream [![Build Status](https://travis-ci.org/ubilabs/node-api-stream.svg?branch=master)](https://travis-ci.org/ubilabs/node-api-stream)

Create streaming, rate-limited APIs with ease

```sh
npm install api-stream --save
```

## Usage

Creating a streaming, rate-limited, caching Wikipedia search API:

```js
var https = require('https'),
  apiStream = require('api-stream'),
  apiURL = 'https://en.wikipedia.org/w/api.php?action=opensearch&format=json&search=';

module.exports = apiStream.createApi(constructorOptions => (query, done) => {
  https.get(apiURL + encodeURIComponent(query), res => {
    var response = '';

    if(res.statusCode !== 200) {
      done({status: res.statusCode}, null);
      return;
    }

    res.on('data', d => response += d);
    res.on('end', () => done(null, JSON.parse(response)));
  });
});
```

Using the created API:

```js
var WikiSearchAPI = require('./wikistream.js');

// create an API stream which runs with a maximum of
// 10 queries per second, and caches results in wiki.db
var wikiStream = new WikiSearchAPI({
  queriesPerSecond: 10,
  cacheFile: 'wiki.db'
});
wikiStream.on('data', d => console.log(d));
wikiStream.on('end', () => console.log('Done.'));

wikiStream.write('Hamburg');
wikiStream.write('Ubilabs');
wikiStream.write('NodeJS');
wikiStream.end();
```

## API-Stream Documentation

**apiStream.createApi(initialiseEndpoint, endpointDefaultOptions = {})**

* initialiseEndpoint `<Function>` Endpoint initialisation function
* endpointDefaultOptions `<Object>` Default options for the constructor of the created class

The endpoint initialisation function must take an `options` object as parameter. The options can be user-defined upon initialisation of the generated API class. The endpoint initialisation function should return an *endpoint function*. The endpoint function is called for every item written to the generated API stream. It is passed two parameters:

* **query:** Type: `?`. The query written to the stream, after it has been passed through the `accessor` function.
* **done:** Type: `Function`. A callback function which must be called once, after the API query is completed. Pass an error as first parameter to indiciate failure, or `null` to indicate success. Pass the API response as second parameter.

## Generated Class Documentation

The constructor of created stream class accepts an options object.  
The options object is used to define some behaviour (e.g. queriesPerSecond, caching), and is then passed on as parameter to `initialiseEndpoint`.  
All default values may be overriden by re-defining the value in `endpointDefaultOptions`.  
By default, the created stream classes support the following options:

* **options.queriesPerSecond:** Type: `Number`, default: `35`. Defines the maximum number of requests per second. Must be at least `1`.
* **options.cacheFile:** Type: `String`, default: `null`. Defines the name of the cache file to be used. The cache is disable if no name has been defined.
* **options.accessor:** Type: `Function`, default: `data => data`. Items written to the stream will be passed through `options.accessor` before being passed as query to `queryEndpoint`.
* **options.stats:** Type: `Object`, default: `{current: 0}`. A statistics object which will be attached to each result. The `options.stats.current` property is increment on each query.

---
_Source: https://npm.io/package/api-stream · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
