# esx-rs

> ECMAScript decorators for REST endpoint declaration, inspired by JAX-RS.

Latest version **0.1.6** (published 2018-04-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install esx-rs
pnpm add esx-rs
yarn add esx-rs
bun add esx-rs
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.6 |
| Published | 2018-04-29 |
| First published | 2018-04-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 70.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Maintainers | rraziel |
| Keywords | typescript, jax-rs, rest, decorator, ecmascript |

## Links

- npm: https://www.npmjs.com/package/esx-rs
- Repository: https://github.com/rraziel/esx-rs
- Issues: https://github.com/rraziel/esx-rs/issues
- npm.io page: https://npm.io/package/esx-rs

## Dependencies (2)

- [reflect-metadata](https://npm.io/package/reflect-metadata.md) 0.1.12
- [es-decorator-utils](https://npm.io/package/es-decorator-utils.md) 0.1.1

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.1.6 (latest) — 2018-04-29
- 0.1.4 — 2018-04-28
- 0.1.3 — 2018-04-22
- 0.1.2 — 2018-04-22
- 0.1.1 — 2018-04-22
- 0.1.0 — 2018-04-21

## README

# ESX-RS

[![AppVeyor](https://img.shields.io/appveyor/ci/rraziel/esx-rs/master.svg?label=Win32&style=flat)](https://ci.appveyor.com/project/rraziel/esx-rs)
[![CircleCI](https://img.shields.io/circleci/project/github/rraziel/esx-rs/master.svg?label=MacOS&style=flat)](https://circleci.com/gh/rraziel/esx-rs)
[![Travis CI](https://img.shields.io/travis/rraziel/esx-rs/master.svg?label=Linux&style=flat)](https://travis-ci.org/rraziel/esx-rs)
[![AppVeyor tests](https://img.shields.io/appveyor/tests/rraziel/esx-rs/master.svg?label=Tests&style=flat)](https://ci.appveyor.com/project/rraziel/esx-rs/build/tests)
[![Codecov](https://img.shields.io/codecov/c/github/rraziel/esx-rs.svg?label=Coverage&style=flat)](https://codecov.io/gh/rraziel/esx-rs)
[![Code Climate](https://img.shields.io/codeclimate/maintainability/rraziel/esx-rs.svg?label=Maintainability&style=flat)](https://codeclimate.com/github/rraziel/esx-rs)
[![Code Climate](https://img.shields.io/codeclimate/issues/rraziel/esx-rs.svg?label=Code%20Issues&style=flat)](https://codeclimate.com/github/rraziel/esx-rs/issues)

[![Dependencies](https://img.shields.io/david/rraziel/esx-rs.svg?label=Dependencies&style=flat)](https://david-dm.org/rraziel/esx-rs)
[![Development dependencies](https://img.shields.io/david/dev/rraziel/esx-rs.svg?label=Dev%20Dependencies&style=flat)](https://david-dm.org/rraziel/esx-rs?type=dev)
[![Known Vulnerabilities](https://snyk.io/test/github/rraziel/esx-rs/badge.svg)](https://snyk.io/test/github/rraziel/esx-rs)
[![Greenkeeper](https://badges.greenkeeper.io/rraziel/esx-rs.svg)](https://greenkeeper.io/)

A library inspired by [JAX-RS](https://en.wikipedia.org/wiki/Java_API_for_RESTful_Web_Services), allowing the description of REST endpoints through simple [TypeScript](https://www.typescriptlang.org/) decorators.

It has currently been integrated with:

| Integration                                                               | Type       | Description                                                                                                |
|:--------------------------------------------------------------------------|:-----------|:-----------------------------------------------------------------------------------------------------------|
| [esx-rs-client](https://github.com/rraziel/esx-rs-client)                 | Client     | Proxy generator to access remote ESX-RS endpoints through a class instance.                                |
| [esx-rs-client-angular](https://github.com/rraziel/esx-rs-client-angular) | Client     | `esx-rs-client` network transport based on [Angular HttpClient](https://angular.io/guide/http).            |
| [esx-rs-client-fetch](https://github.com/rraziel/esx-rs-client-fetch)     | Client     | `esx-rs-client` network transport based on [Fetch](https://fetch.spec.whatwg.org/).                        |
| [esx-rs-client-http](https://github.com/rraziel/esx-rs-client-http)       | Client     | `esx-rs-client` network transport based on [Node http](https://nodejs.org/api/http.html).                  |
| [esx-rs-client-xhr](https://github.com/rraziel/esx-rs-client-xhr)         | Client     | `esx-rs-client` network transport based on [XMLHttpRequest](https://en.wikipedia.org/wiki/XMLHttpRequest). |
| [esx-rs-server](https://github.com/rraziel/esx-rs-server)                 | Server     | Dispatch incoming HTTP requests to ESX-RS decorated class methods.                                         |
| [esx-rs-router-express](https://github.com/rraziel/esx-rs-router-express) | Server     | `esx-rs-server`-based router middleware for [Express](https://expressjs.com/).                             |
| [esx-rs-router-koa](https://github.com/rraziel/esx-rs-router-koa)         | Server     | `esx-rs-server`-based router middleware for [Koa](http://koajs.com/).                                      |
| [esx-rs-validation](https://github.com/rraziel/esx-rs-validation)         | Validation | Validation layer based on [es-validation](https://github.com/rraziel/es-validation).                       |
| [esx-rs-schema-openapi](https://github.com/rraziel/esx-rs-schema-openapi) | Schema     | Schema generation for [OpenAPI 3.0](https://www.openapis.org/).                                            |

## Getting Started

The library can be installed using `npm`:

```
npm install esx-rs --save
```

Or using `yarn`:

```
yarn add esx-rs
```

Endpoints can then be described using decorators:

```typescript
@Path('/users')
@Produces('application/json')
@Consumes('application/json')
class UsersEndpoint {

    @POST
    async createUser(user: User): Promise<User> {
        // ...
    }

    @PUT @Path('/:userId')
    async updateUser(@PathParam('userId') userId: string, user: User): Promise<User> {
        // ...
    }

    @GET @Path('/:userId')
    async getUser(@PathParam('userId') userId: string): Promise<User> {
        // ...
    }

    @DELETE @Path('/:userId')
    async deleteUser(@PathParam('userId') userId: string): Promise<void> {
        // ...
    }

}
```

## Usage

Various decorators are available, each targetting a subset of the typical REST properties for a service.

### Method

The HTTP method(s) can be specified using:

- `@DELETE`
- `@GET`
- `@HEAD`
- `@OPTIONS`
- `@PATCH`
- `@POST`
- `@PUT`
- `@HttpMethod`

### Path

The resource path can be specified using:

- `@Path`

Note: the [path-to-regexp](https://github.com/pillarjs/path-to-regexp) format is used, e.g. `/path/to/:resourceId/subpath/:subresourceId`.

### Resource Type

The type of resource, either consumed by the operation (mapped to `content-type`) or produced by the operation (mapped to `accept`), can be specified using:

- `@Consumes`
- `@Produces`

Multiple media types may be specified.

### Parameters

Operation parameters and resource properties are mapped using a specific decorator for each parameter type:

- `@CookieParam`
- `@FormParam`
- `@HeaderParam`
- `@MatrixParam`
- `@QueryParam`
- `@PathParam`

### Context

It is also possible to map the following context information to a parameter using the `@ContextParam` decorator:

- `HttpContext`
- `HttpRequest`
- `HttpResponse`

### Endpoint vs. Operation

Many decorators can be applied to both a class and its methods.

In this scenario, the `OperationInfo` object returned for a method contains merged information that includes both the operation and the endpoint information.

The following decorators can be applied to both classes and methods:

- `@DELETE`, `@GET`, `@HEAD`, `@OPTIONS`, `@PATCH`, `@POST` and `@PUT`
- `@Consumes` and `@Produces`
- `@Path`

The `@Path` decorator is handled a bit differently: the operation path is appended to the endpoint path.

## Limitations

At the moment, only concrete classes can be decorated.

This is due to the way ECMAScript gets generated, as interfaces no longer exist in the generated code.

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