# pseudo-fetch

> Simple mock of global/window.fetch with a user-friendly api

Latest version **0.3.1** (published 2017-02-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install pseudo-fetch
pnpm add pseudo-fetch
yarn add pseudo-fetch
bun add pseudo-fetch
```

## 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.3.1 |
| Published | 2017-02-08 |
| First published | 2017-02-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Tormod Haugland |
| Maintainers | trmd |

## Links

- npm: https://www.npmjs.com/package/pseudo-fetch
- Repository: https://github.com/tOgg1/pseudo-fetch
- Homepage: https://github.com/tOgg1/pseudo-fetch#readme
- Issues: https://github.com/tOgg1/pseudo-fetch/issues
- npm.io page: https://npm.io/package/pseudo-fetch

## Dependencies (1)

- [url-parse](https://npm.io/package/url-parse.md) ^1.1.7

## Recent versions

- 0.3.1 (latest) — 2017-02-08
- 0.3.0 — 2017-02-07
- 0.2.2 — 2017-02-06
- 0.2.1 — 2017-02-06
- 0.2.0 — 2017-02-05
- 0.1.1 — 2017-02-05
- 0.1.0 — 2017-02-05
- 0.0.1 — 2017-02-04

## README

[![Build Status](https://travis-ci.org/tOgg1/pseudo-fetch.svg?branch=master)](https://travis-ci.org/tOgg1/pseudo-fetch)
[![Version](https://img.shields.io/badge/version-v0.3.0-blue.svg)](https://www.npmjs.com/package/pseudo-fetch)

# Pseudo Fetch

[Docs](https://togg1.github.io/pseudo-fetch/)

Pseudo-fetch is a lightweight mock of the Fetch API. It does not try to be compliant to any standard, but instead to have a light and easy API for you to use.

## Installation
```
npm install pseudo-fetch
```

## Quickstart

Setting up a simple mock-server which responds to requests sent with `fetch` is as easy as:

```javascript
import pseudoFetch from 'pseudo-fetch';

const server = pseudoFetch();  // Creates a server endpoint for you, and automatically overwrites window.fetch or global.fetch

server
  .get('/')
  .respond('Hello world');
  // Or .send

fetch('/')
  .then(response => response.text())
  .then(console.log);  // Will log 'Hello world'
```


Accepting other methods than GET:

```javascript
server
  .post('/myroute')
  .send('Hello');

// Alternatively
// server
//   .route('/myroute', 'POST')
//   .send('Hello');

fetch('/myroute')
  .then(response => response.status)
  .then(console.log);  // Will print 404

fetch('/myroute', {method: "POST"})
  .then(response => response.status)
  .then(console.log);  // Will print 200


```

Sending JSON-data:
```javascript
server
  .get('/my/json/endpoint')
  .send({message: 'Hello world', key: 'value'});

fetch('/my/json/endpoint')
  .then(response => response.json())
  .then(console.log); // Will print
                      // {
                      //   message: 'Hello world',
                      //   key: 'value'
                      // }
```

Setting return status:

```javascript
server
  .post('/human')
  .status(201)
  .send({name: 'New Human', id: '1'});

fetch('/human', {method: 'POST'})
  .then(response => response.status)
  .then(console.log)  // Will log 201
```

Settings headers:

```javascript
server
  .get('/')
  .setHeader('Content-Type', 'plain/text')
  .send('Hello world');
  // Or .respond

fetch('/')
  .then(response => response.headers.get('Content-Type'))
  .then(console.log);  // Will log '['plain/text']'
```

If you want to handle request/response data more thoroughly, you can supply a
function accepting two arguments to .send or .respond.

```javascript
server
  .post('/human')
  .send((request, response) => {
    let humanData = request.json();
    humanData.id = '1';

    response.body = humanData;  // Lets return it back
    response.headers.set('Content-Type', 'application/json');
  });

fetch('/human', {
    method: 'POST',
    body: JSON.stringify({name: ''}),
    headers: {
      'Content-Type': 'application/json'
    }
  })
  .then(response => response.json())
  .then(humanData => {
    console.log(humanData);  // Should print our humanData with the new id
    // Now we can do stuff with our newly created human
  });

```

[Next: Basic usage](https://togg1.github.io/pseudo-fetch/docs/basic-usage)

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