# @kkt/mocker-api

> Creates mocks for REST APIs.

Latest version **6.1.0** (published 2021-01-14) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @kkt/mocker-api
pnpm add @kkt/mocker-api
yarn add @kkt/mocker-api
bun add @kkt/mocker-api
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 6.1.0 |
| Published | 2021-01-14 |
| First published | 2020-12-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 10.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 51 |
| Author | Kenny Wong |
| Maintainers | wcjiang |
| Keywords | kkt, mocker-api, framework |

## Links

- npm: https://www.npmjs.com/package/@kkt/mocker-api
- Repository: https://github.com/kktjs/kkt
- Homepage: https://github.com/kktjs/kkt#readme
- Issues: https://github.com/kktjs/kkt/issues
- npm.io page: https://npm.io/package/@kkt/mocker-api

## Dependencies (1)

- [mocker-api](https://npm.io/package/mocker-api.md) 2.7.5

## Alternatives

- [@sveltejs/kit](https://npm.io/package/@sveltejs/kit.md) — 2.2M weekly downloads
- [@tangle-network/brand](https://npm.io/package/@tangle-network/brand.md) — 10.0K weekly downloads
- [seneca](https://npm.io/package/seneca.md) — 7.4K weekly downloads
- [@bsb/base](https://npm.io/package/@bsb/base.md) — 7.2K weekly downloads
- [@modern-js/plugin-swc](https://npm.io/package/@modern-js/plugin-swc.md) — 3.4K weekly downloads

## Recent versions

- 6.1.0 (latest) — 2021-01-14
- 6.0.12 — 2021-01-12
- 6.0.11 — 2020-12-25
- 6.0.10 — 2020-12-23
- 6.0.9 — 2020-12-23
- 6.0.8 — 2020-12-23
- 6.0.7 — 2020-12-22
- 6.0.6 — 2020-12-22
- 6.0.5 — 2020-12-21
- 6.0.4 — 2020-12-19
- 6.0.3 — 2020-12-19
- 6.0.2 — 2020-12-19
- 6.0.1 — 2020-12-19
- 6.0.0 — 2020-12-19

## README

<p align="center">
  <h1>@kkt/mocker-api</h1>
</p>

<p align="center">
  <a href="https://www.npmjs.com/package/@kkt/mocker-api">
    <img src="https://img.shields.io/npm/v/@kkt/mocker-api.svg" alt="npm version">
  </a>
</p>

[`mocker-api`](https://github.com/jaywcjlove/mocker-api) that creates mocks for REST APIs.

> ⚠️ This package has been deprecated.

It can be configured using `proxySetup`.

```diff
- import path from 'path';
- import apiMocker from '@kkt/mocker-api';
- 
- export const devServer = (configFunction) => {
-   return (proxy, allowedHost) => {
-     let config = configFunction(proxy, allowedHost);
-     config = apiMocker(config, path.resolve('./mocker/index.js'));
-     return config;
-   }
- }

+ export const proxySetup = () => {
+   return {
+     path: path.resolve('./mocker/index.js'),
+   }
+ }
```

### Installation

```bash
yarn add --dev @kkt/mocker-api
# or use npm if you don't have yarn yet
npm install --save-dev @kkt/mocker-api
```

### Usage

In the `.kktrc.js` or `.kktrc.ts` you created for `kkt` add this code:

```js
import path from 'path';
import apiMocker from '@kkt/mocker-api';

export const devServer = (configFunction) => {
  return (proxy, allowedHost) => {
    // Create the default config by calling configFunction with the proxy/allowedHost parameters
    let config = configFunction(proxy, allowedHost);
    
    config = apiMocker(config, path.resolve('./mocker/index.js'));

    // Return your customised Webpack Development Server config.
    return config;
  }
}
```

In `package.json`, add a separate npm script to build library

```js
{
  "scripts": {
    ...
    "start": "kkt start",
    ...
  }
}
```

And you can now use CRA to build your library

### Configurations

```js

import path from 'path';
import apiMocker from '@kkt/mocker-api';

export const devServer = (configFunction) => (proxy, allowedHost) => {
  // Create the default config by calling configFunction with the proxy/allowedHost parameters
  let config = configFunction(proxy, allowedHost);
  config = apiMocker(
    config,
    path.resolve('./mocker/index.js'),
    /**
     * mocker-api Options
     * https://github.com/jaywcjlove/mocker-api/tree/6503e44d0c8fe1d833d6f367ccbb7630415f555c#options
     */
    {
      proxy: {
        // Turn a path string such as `/user/:name` into a regular expression.
        // https://www.npmjs.com/package/path-to-regexp
        '/repos/(.*)': 'https://api.github.com/',
        '/:owner/:repo/raw/:ref/(.*)': 'http://127.0.0.1:2018',
        '/api/repos/(.*)': 'http://127.0.0.1:3721/'
      },
      // rewrite target's url path. Object-keys will be used as RegExp to match paths.
      // https://github.com/jaywcjlove/mocker-api/issues/62
      pathRewrite: {
        '^/api/repos/': '/repos/',
      },
      changeHost: true,
      // modify the http-proxy options
      httpProxy: {
        options: {
          ignorePath: true,
        },
        listeners: {
          proxyReq: function (proxyReq, req, res, options) {
            console.log('proxyReq');
          },
        },
      },
    }
  );

  // Return your customised Webpack Development Server config.
  return config;
}
```

### License

Licensed under the MIT License

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