# @janiscommerce/api-view

> A package for managing API Views from any origin

Latest version **1.3.0** (published 2019-07-12) · ISC license · 0 weekly downloads

## Install

```sh
npm install @janiscommerce/api-view
pnpm add @janiscommerce/api-view
yarn add @janiscommerce/api-view
bun add @janiscommerce/api-view
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.0 |
| Published | 2019-07-12 |
| First published | 2019-06-07 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 12.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Janis |
| Maintainers | janiscommerce |

## Links

- npm: https://www.npmjs.com/package/@janiscommerce/api-view
- Repository: https://github.com/janis-commerce/api-view
- Homepage: https://github.com/janis-commerce/api-view#readme
- Issues: https://github.com/janis-commerce/api-view/issues
- npm.io page: https://npm.io/package/@janiscommerce/api-view

## Dependencies (1)

- [@janiscommerce/logger](https://npm.io/package/@janiscommerce/logger.md) ^1.1.0

## Recent versions

- 1.3.0 (latest) — 2019-07-12
- 1.2.0 — 2019-06-21
- 1.1.0 — 2019-06-18
- 1.0.0 — 2019-06-07

## README

# API View

[![Build Status](https://travis-ci.org/janis-commerce/api-view.svg?branch=master)](https://travis-ci.org/janis-commerce/api-view)
[![Coverage Status](https://coveralls.io/repos/github/janis-commerce/api-view/badge.svg?branch=master)](https://coveralls.io/github/janis-commerce/api-view?branch=master)

A package for managing API View from any origin.

## Installation

```
npm install @janiscommerce/api-view
```

## Dispatcher
* **new Dispatcher( object )**
Construct a Dispatcher with the request information

* async **.dispatch()**
This method dispatch the api instance.
Returns an object with `code` and the `body`.

## API
You should extend your apis from this module.

* **pathParameters** (*getter*). Returns the path parameters of the request. The `entity` and `entityId` if present.

* **headers** (*getter*). Returns the the headers of the request.

* **cookies** (*getter*). Returns the the cookies of the request.

* **setCode(code)**. Set a response httpCode. `code` must be a integer.

* **setHeader(headerName, headerValue)**. Set an individual response header. `headerName` must be a string.

* **setHeaders(headers)**. Set response headers. `headers` must be an object with "key-value" headers.

* **setCookie(cookieName, cookieValue)**. Set an individual response cookie. `cookieName` must be a string.

* **setCookies(cookies)**. Set response cookies. `cookies` must be an object with "key-value" cookies.

* **setBody(body)**. Set the response body.

## Usage

* How to process a `browse` request

```js
const { Dispatcher } = require('@janiscommerce/api-view');

const dispatcher = new Dispatcher({
	entity: 'product',
	action: 'browse',
	method: 'data'
});

const result = await dispatcher.dispatch();

console.log(result);
/**
	expected output:

	{
		code: 200,
		body: [
			{
				id: 5928,
				name: 'Great product',
				price: 45.1,
				category: 'Beverages'
			}, {
				id: 5929,
				name: 'Another great product',
				price: 32.0,
				category: 'groceries'
			}
		]
	}
*/
```

* How to process an `edit` request

```js
const { Dispatcher } = require('@janiscommerce/api-view');

const dispatcher = new Dispatcher({
	entity: 'product',
	action: 'edit',
	method: 'data',
	entity: 5928
});

const result = await dispatcher.dispatch();

console.log(result);
/**
	expected output:

	{
		code: 200,
		body: {
			id: 5928,
			name: 'Great product',
			price: 45.1,
			category: 'Beverages'
		}
	}
*/
```

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