# @marblejs/proxy

> A generic proxy server for Marble.js

Latest version **2.3.0-dev.7** (published 2019-06-12) · MIT license · 0 weekly downloads

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

## Install

```sh
npm install @marblejs/proxy
pnpm add @marblejs/proxy
yarn add @marblejs/proxy
bun add @marblejs/proxy
```

## Health

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

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 2.3.0-dev.7 |
| Published | 2019-06-12 |
| First published | 2019-06-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 15.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2144 |
| Author | Krzysztof Miemiec |
| Maintainers | jflakus |
| Keywords | marble.js, proxy, server |

## Links

- npm: https://www.npmjs.com/package/@marblejs/proxy
- Repository: https://github.com/marblejs/marble
- Homepage: https://github.com/marblejs/marble/tree/master/packages/proxy#readme
- Issues: https://github.com/marblejs/marble/issues
- npm.io page: https://npm.io/package/@marblejs/proxy

## Recent versions

- 2.3.0-dev.7 (latest) — 2019-06-12
- 2.3.0-dev.23 (next) — 2019-07-03
- 2.3.0-dev.22 — 2019-07-03
- 2.3.0-dev.21 — 2019-07-03
- 2.3.0-dev.11 — 2019-06-19
- 2.3.0-dev.10 — 2019-06-18
- 2.3.0-dev.9 — 2019-06-14

## README

<p align="center">
  <a href="https://marblejs.com">
    <img src="https://github.com/marblejs/marble/blob/master/assets/img/logo.png?raw=true" width="200" alt="Marble.js logo"/>
  </a>
</p>

# @marblejs/proxy

A simple proxy server for [Marble.js](https://github.com/marblejs/marble). It allows to send and receive requests from any server defined as `(req: IncomingMessage, res: OutgoingMessage) => void`. It is a base for `@marblejs/testing` and `@marblejs/serverless` packages.

## Installation

```
$ npm i @marblejs/proxy
```

## Documentation

For the latest updates, documentation, change log, and release information visit [docs.marblejs.com](https://docs.marblejs.com) and follow [@marble_js](https://twitter.com/marble_js) on Twitter.

## Usage

```typescript
import { ServerProxy, ServerApp, ServerProxyRequest, ServerProxyResponse } from '@marblejs/proxy';

export interface MyRequest {
  headers: Record<string, string>;
  method: string;
  path: string;
  body?: string;
}

export interface MyResponse {
  headers: Record<string, string>;
  body: string;
  code: number;
}

export class MyProxy extends ServerProxy<MyRequest, MyResponse> {

  constructor(app: ServerApp) {
    super(app);
  }

  normalizeError(error: Error): MyResponse {
    return {
      code: 502,
      body: error.message,
      headers: {},
    };
  }

  normalizeRequest(proxyRequest: MyRequest): ServerProxyRequest {
    const {
      body,
      headers,
      method,
      path,
    } = proxyRequest;
    return {
      headers,
      // Convert body to buffer
      body: body !== undefined ? Buffer.from(body) : undefined,
      method,
      path
    };
  }

  normalizeResponse(serverProxyResponse: ServerProxyResponse): MyResponse {
    const {
      headers,
      body,
      statusCode,
    } = serverProxyResponse;

    return {
      // Convert multi-value headers from Record<string, string[]>
      headers: Object.keys(headers).reduce((result, key) => {
        result[key] = headers[key][0];
        return headers;
      }, {}),
      // Convert body from Buffer to string
      body: body.toString(),
      code: statusCode,
    };
  }
}

```

License: MIT

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