# stubsy

> A mock server that ships with a UI to allow configuration during runtime.

Latest version **1.3.4** (published 2024-10-19) · ISC license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.3.4 |
| Published | 2024-10-19 |
| First published | 2021-05-30 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 544.9 KB |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Mustafa Mansour |
| Maintainers | mkman |
| Keywords | mock, server, node, express |

## Links

- npm: https://www.npmjs.com/package/stubsy
- Repository: https://github.com/MkMan/stubsy
- Homepage: https://github.com/MkMan/stubsy#readme
- Issues: https://github.com/MkMan/stubsy/issues
- npm.io page: https://npm.io/package/stubsy

## Dependencies (2)

- [express](https://npm.io/package/express.md) 4.21.1
- [body-parser](https://npm.io/package/body-parser.md) 1.20.3

## Alternatives

- [@expo/fingerprint](https://npm.io/package/@expo/fingerprint.md) — 6.2M weekly downloads
- [@azure/monitor-opentelemetry-exporter](https://npm.io/package/@azure/monitor-opentelemetry-exporter.md) — 850.0K weekly downloads
- [@azure/monitor-opentelemetry](https://npm.io/package/@azure/monitor-opentelemetry.md) — 624.0K weekly downloads
- [@posthog/ai](https://npm.io/package/@posthog/ai.md) — 423.3K weekly downloads
- [fakefilter](https://npm.io/package/fakefilter.md) — 63.9K weekly downloads

## Recent versions

- 1.3.4 (latest) — 2024-10-19
- 1.3.3 — 2024-03-27
- 1.3.2 — 2023-03-22
- 1.3.1 — 2022-11-07
- 1.3.0 — 2022-07-22
- 1.2.5 — 2022-06-25
- 1.2.4 — 2022-03-15
- 1.2.3 — 2022-01-11
- 1.2.2 — 2022-01-05
- 1.2.1 — 2021-06-29
- 1.2.0 — 2021-06-23
- 1.1.0 — 2021-06-05
- 1.0.0 — 2021-05-30

## README

# Stubsy

* [Installation](#installation)
* [Usage](#usage)
  * [Definitions](#definitions)
  * [API](#api)
  * [Example](#example)

Stubsy is a Node server built using Express. Its main goal is to allow
developers to easily set up the server but more importantly change its
behaviour after launch. This can be useful to test how the UI behaves
when an endpoint's response is `404` instead `200` for example.

Stubsy ships with a UI to show the set up of the server and allow
changing endpoint's behaviour.

![animated demo of Stubsy](./docs/stubsy.gif)

## Installation

```text
npm install --save-dev stubsy
```

## Usage

### Definitions

#### Endpoint

An `Endpoint` is REST endpoint, that is set up with a default
behaviour. An endpoint's behaviour is defined as follows.

```ts
type EndpointBehaviour = {
  path: string; // the route at which the endpoint is accessed
  responseBody: unknown;
  status: number;
  type: 'get' | 'post' | 'put' | 'delete' | 'patch';
  delay?: number;
};
```

**Note**: the `path` field can be any valid
[Express path](http://expressjs.com/en/guide/routing.html#route-paths).

#### Override

An `Override` is an overriding behaviour for an Endpoint to alter its
response. The override behaviour is defined as follows. The `path` and
`type` of the endpoint cannot be changed in an `Override`.

```ts
type OverrideBehaviour = {
  responseBody: unknown;
  status: number;
  delay?: number;
};
```

### API

#### `new Stubsy(portNumber)`

Creates an instance of `Stubsy`.

* `portNumber` \<Number\>: the port for the server to run on

#### `stubsy.app`

The underlying Express app. Use this instance variable to add more endpoints if needed
and to start the server.

#### `stubsy.registerEndpoint(endpointId, endpointBehaviour)`

Registers endpoints to be accessed on the server.

* `endpointId` \<String\> **required**: a unique identifier for the endpoint
* `endpointBehaviour` \<EndpointBehaviour\> **required**: the endpoint's behaviour

#### `stubsy.registerOverride(endpointId, overrideId, overrideBehaviour)`

Registers override behaviour for a previously defined endpoint.

* `endpointId` \<String\> **required**: the id of the endpoint to register
an override for
* `overrideId` \<String\> **required**: a unique identifier for the override
* `overrideBehaviour` \<OverrideBehaviour\> **required**: the override's behaviour

#### `stubsy.activateOverride(endpointId, overrideId)`

Activates the specified override on the endpoint.

* `endpointId` \<String\> **required**: the id of the endpoint to activate
the override on
* `overrideId` \<String\>: the id of the override to activate. If omitted
restores the override to the default behaviour.

#### ~~`stubsy.start()`~~ **Deprecated**

Starts the server on the port number specified.

### Example

```js
// server.js
import { Stubsy } from 'stubsy';

const stubsyPortNumber = 3000;
const stubsy = new Stubsy();

stubsy.registerEndpoint('films', {
  path: '/films',
  status: 200,
  type: 'get',
  responseBody: [{ title: 'Inception' }, { title: 'Tenet' }],
});

stubsy.registerOverride('films', 'error', { status: 404, responseBody: {} });
stubsy.registerOverride('films', 'outage', { status: 500, responseBody: {} });

stubsy.activateOverride('films', 'outage');

stubsy.app.listen(stubsyPortNumber);
```

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