# ngx-rest-ex

> Another decorator-based (Retrofit-like) HTTP client for Angular and NodeJS to consuming RESTful API

Latest version **2.4.1** (published 2020-07-12) · MIT license · 0 weekly downloads

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

## Install

```sh
npm install ngx-rest-ex
pnpm add ngx-rest-ex
yarn add ngx-rest-ex
bun add ngx-rest-ex
```

## Health

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

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 2.4.1 |
| Published | 2020-07-12 |
| First published | 2018-04-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 5 |
| Unpacked size | 859.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Kay Seven |
| Maintainers | dkhang97 |
| Keywords | angular, angular5, angular6, angular7, angular8, angular9, ng, ng7, ng8, ng9, ngx, NodeJS, annotation, decorator, decorator-based, http, rest, RESTful, JSON, FormUrlEncoded, Promise, Observable, API Client, Retrofit |

## Links

- npm: https://www.npmjs.com/package/ngx-rest-ex
- Repository: https://bitbucket.org/dkhang97/rest-annotations/src/ngx-rest-ex
- npm.io page: https://npm.io/package/ngx-rest-ex

## Dependencies (5)

- [qs](https://npm.io/package/qs.md) ^6.9.4
- [tslib](https://npm.io/package/tslib.md) ^1.10.0
- [injection-js](https://npm.io/package/injection-js.md) ^2.2.1
- [xmlhttprequest](https://npm.io/package/xmlhttprequest.md) ^1.8.0
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.13

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 2.4.1 (latest) — 2020-07-12
- 2.4.0 — 2020-07-05
- 2.3.1 — 2020-01-16
- 2.3.0 — 2019-09-21
- 2.2.0 — 2019-09-20
- 2.1.0 — 2019-07-29
- 2.0.0 — 2019-06-23
- 1.4.0 — 2019-06-18
- 1.3.1 — 2019-04-05
- 1.2.7 — 2019-03-27
- 1.2.6 — 2019-03-25
- 1.2.5 — 2019-03-24
- 1.2.4 — 2019-03-24
- 1.1.4 — 2018-09-15
- 1.1.1 — 2018-09-14
- … 2 more at https://npm.io/package/ngx-rest-ex/versions

## README

# ngx-rest-ex

## Deprecated

> ***This package has been migrated to [rest-annotations](https://www.npmjs.com/package/rest-annotations)***

---

[![version](https://img.shields.io/npm/v/ngx-rest-ex.svg?style=flat)](https://www.npmjs.com/package/ngx-rest-ex) [![npm](https://img.shields.io/npm/l/ngx-rest-ex.svg)](https://opensource.org/licenses/MIT)

## Installation

```bash
npm i ngx-rest-ex --save
```

## Example

```ts
import { Inject, Injectable, Injector } from '@angular/core';
import {
    RESTClient, GenerateBody, BaseUrl,
    GET, POST, PUT, PATCH, DELETE,
    Headers, Paths, Queries, Fields,
    Header, Path, Query, Field, Body
} from 'ngx-rest-ex';
import { Observable } from 'rxjs';

import { Todo } from './models';

@Injectable()
@BaseUrl('http://localhost:4200/api/')
// @Headers({
//     'Content-Type': 'application/x-www-form-urlencoded',
// })
export class ApiClient extends RESTClient {

    constructor(injector: Injector) {
        super(injector);
    }

    protected getDefaultHeaders() {
        return {
            'Content-Type': 'application/x-www-form-urlencoded',
            'X-Auth-Token': 'abc12356asd'
        };
    }

    @GET("todo")
    @Headers({ 'X-Auth-Token': null })
    @Queries({ pageSize: 20 })
    getTodoList(
        @Query("sort") sort?: string
    ): Promise<Todo[]> { return; }

    @GET("todo/{id}")
    getTodo(
        @Path("id") id: string
    ): Promise<Todo> { return; }

    @POST("todo")
    @Fields({ active: 1 })
    createTodo(
        @Body todo: Todo
    ): Promise<Todo> { return; }

    @PUT("todo", { generateBody: GenerateBody.Json })
    editTodo(
        @Field("id") id: string,
        @Body todo: Todo
    ): Promise<Todo> { return; }

    @DELETE("todo/{id}", { responseStrategy: 'httpResponse' })
    deleteTodo(
        @Path("id") id: string
    ): Observable<Todo> { return; }

}
```

### Import to your module

``` ts
import { HttpClientModule } from '@angular/common/http';

@NgModule({
  imports: [
    // ...
    HttpClientModule,
  ],
  providers: [ApiClient],
})
export class AppModule { }
```

### Using it in your component

``` ts
@Component({
  selector: 'app-to-do',
  templateUrl: './to-do.component.html',
})
export class ToDoComponent {

  constructor(
    private api: ApiClient
  ) {  }
  
  // Use API
}
```

### For NodeJS

``` ts
import { BaseUrl, RESTClient } from 'ngx-rest-ex';
import { createNodeInjector } from 'ngx-rest-ex/node';

@BaseUrl('http://localhost:4200/api/')
export class ApiService extends RESTClient {
    constructor() {
        super(createNodeInjector());
    }

    // Define your API consumer
}

// =================================================

const api = new ApiService();
// Use API
```

---

## API Docs

### RESTClient

#### Methods

>- **`getBaseUrl(): string`**: returns the base url of RESTClient
>- **`getDefaultHeaders(metaHeaders?: Record<string, string | string[] | number>): Record<string, string | string[] | number>`**: returns the default headers of RESTClient in a key-value pair
>- **`getAuthenticationHeader(action?: string, args?: any[]): Record<string, string | string[] | number>`**: return authentication header for `@Authentication`
>- **`requestInterceptor(req: HttpRequest<any>): void`**: intercept api request
>- **`responseInterceptor(response: HttpResponse<any>, responseStrategy: string): any`**: intercept api response
>- **`_noop()`**: typing the consumer

#### Class decorators

>- **`@BaseUrl(url: string)`**: will replace `getBaseUrl()` of the service
>- ~~**`@DefaultHeaders(headers: Record<string, string | string[] | number>, overlap?: boolean)`**: set default value for  `getDefaultHeaders()`~~
>
>##### SERVICE DEFAULT PARAMS
>
>- **`@Headers(values: Record<string, string | string[] | number>, overlap?: boolean)`**
>- **`@Queries(values: Record<string, any>, overlap?: boolean)`**
>- **`@Paths(values: Record<string, string | number | { value: string | number, preserve?: boolean }>, overlap?: boolean)`**
>- **`@Fields(values: Record<string, any>, overlap?: boolean)`**
>
>##### SERVICE HEADER CUSTOMIZATION
>
>- **`@HFormUrlEncoded()`**: set **Content-Type** in default headers to **application/x-www-form-urlencoded**
>- **`@HJson()`**: set **Content-Type** in default headers to **application/json**
>- **`@Authentication(requireAuthentication?: boolean)`**

#### Method decorators

>##### HTTP METHOD
>
>- **`@GET(url: String, opts?: RestMethodOptions)`**
>- **`@POST(url: String, opts?: RestMethodOptions)`**
>- **`@PUT(url: String, opts?: RestMethodOptions)`**
>- **`@PATCH(url: String, opts?: RestMethodOptions)`**
>- **`@DELETE(url: String, opts?: RestMethodOptions)`**
>
>##### DEFAULT PARAMS
>
>- **`@Headers(values: Record<string, string | string[] | number>)`**
>- **`@Queries(values: Record<string, any>)`**
>- **`@Paths(values: Record<string, string | number | { value: string | number, preserve?: boolean }>)`**
>- **`@Fields(values: Record<string, any>)`**
>
>##### HEADER CUSTOMIZATION
>
>- **`@HFormUrlEncoded()`**
>- **`@HJson()`**
>- **`@Authentication(requireAuthentication?: boolean)`**
>
#### Parameter decorators
>
>- **`@Path(key: string)`**
>- **`@Query(key: string)`**
>- **`@Header(key: string)`**
>- **`@Field(key: string)`**
>- **`@QueryObject`**
>- **`@Body`**

### RestMethodOptions

>- **`generateBody`**: specify the function to generate the body of a request
>- **`responseStrategy`**: strategy of response type, specify one of 'promise', 'httpResponse', 'observable', 'raw'

---

## License

MIT

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