# toapi

> Make Every website provides APIs.

Latest version **0.0.2** (published 2017-12-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install toapi
pnpm add toapi
yarn add toapi
bun add toapi
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.2 |
| Published | 2017-12-17 |
| First published | 2017-12-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 15 |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| Author | Jogis |
| Maintainers | jogis |

## Links

- npm: https://www.npmjs.com/package/toapi
- npm.io page: https://npm.io/package/toapi

## Dependencies (15)

- [koa](https://npm.io/package/koa.md) ^2.4.1
- [md5](https://npm.io/package/md5.md) ^2.2.1
- [chalk](https://npm.io/package/chalk.md) ^2.3.0
- [debug](https://npm.io/package/debug.md) ^3.1.0
- [lowdb](https://npm.io/package/lowdb.md) ^1.0.0
- [temme](https://npm.io/package/temme.md) ^0.5.1
- [lodash](https://npm.io/package/lodash.md) ^4.17.4
- [pouchdb](https://npm.io/package/pouchdb.md) ^6.3.4
- [director](https://npm.io/package/director.md) ^1.2.8
- [fs-extra](https://npm.io/package/fs-extra.md) ^5.0.0
- [puppeteer](https://npm.io/package/puppeteer.md) ^0.13.0
- [url-parse](https://npm.io/package/url-parse.md) ^1.2.0
- [koa-router](https://npm.io/package/koa-router.md) ^7.3.0
- [route-parser](https://npm.io/package/route-parser.md) ^0.0.5
- [html-minifier](https://npm.io/package/html-minifier.md) ^3.5.7

## Recent versions

- 0.0.2 (latest) — 2017-12-17
- 0.0.1 — 2017-12-17

## README

## ToAPI

Make Every website provides APIs.

![](docs/imgs/api.png)

## Introduction

Topapi is a supper slight server framework, which provide one way to ask for every website to provide apis.

We don't need to climb the website day by day. Because they should provide the API we need!

All we need is a Temme snippet.

## Architecture

Toapi provide multi cache for every page u visit. So, don't worry about u make a huge trivial ddos like to the website.

![](docs/imgs/diagram.png)

## Demo

[![asciicast](https://asciinema.org/a/153102.png)](https://asciinema.org/a/153102)

## Get Started

### Install

```bash
git clone git@github.com:yesvods/toapi.git
cd toapi && npm install
```

### Start Example

* Node Require: 8.6+ to support async function

```bash
npm start
# then visit localhost:3000/job/3616345
```

That's all, can't be simple much more. Read more about [example](example).

## NPM

Install the NPM module:

```
npm install toapi
```

### Usage

Let's say we need to visit the job detail `API` of `lagou`.

Such as `https://www.lagou.com/jobs/3616345.html`

We just write a [temme](https://github.com/shinima/temme) selector like:

```
...
.job_company@company {
  .fl{find($name|fp, '拉勾认证企业')}
  .c_feature li:nth-child(1){find($domain|fp, '领域')}
  .c_feature li:nth-child(2){find($stage|fp, '发展阶段')}
  .c_feature li:nth-child(3){find($size|fp, '规模')}
  .c_feature li:nth-child(4) a[href=$link];
}
...
```

If u don't know what `temme` is. Maybe u can ref this: https://github.com/shinima/temme

And then `register` it!

```js
const ToAPI = require('toapi')

ToAPI.register({
  routeName: '/job/:id',
  matchUrl: 'https://www.lagou.com/jobs/:id.html',
  selector: selector,
})

ToAPI.start(3000)
```

All things are done, enjoy ur website swimming.

### API

#### ToAPI.start(port, options)

Start the `ToAPI` server, u can continue to `register` route after server started.

* port
  * the starting port
* options
  * watiUntil, defaults to `domcontentloaded`
    * Spec the browser event that represent page data is ready.
    * more options can be see [here](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagegotourl-options)
  * timeout, defaults to `30000`
    * Spec timeout value when requesting to the target page.

```js
// pageA will be timeout after 30000(default) ms
ToAPI.register(pageA)

ToAPI.start(3000, { timeout: 60000 })

// pageB will be timeout after 60000 ms
ToAPI.register(pageB)
```

#### ToAPI.register(options)

Register the page route to be served.

* routeName (optional)
  * The same as koa route
  * `ToAPI` will parse from `matchUrl` if not specified.
  * eample: `/page/:id`
* matchUrl
  * The target website match route
  * example: `http://example.com/page/:id`
* selector (optional)
  * The [Temme](https://github.com/shinima/temme) Selector to spec page
* waitUntil
  * DefaultsTo: `domcontentloaded`
* timeout
  * DefaultsTo: `30000`

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