1.0.11 • Published 2 years ago

typescript-nest-rest-client v1.0.11

Weekly downloads
66
License
MIT
Repository
github
Last release
2 years ago

typescript-nest-rest-client

Nest HTTP client with Typescript Declarative Annotations, Observables, Interceptors and Timeouts. This package is production ready.

This is an implementation for nest of typescript-rest-client : https://www.npmjs.com/package/typescript-rest-client

Installation

yarn add typescript-rest-client typescript-nest-rest-client

or

npm install typescript-rest-client typescript-nest-rest-client --save

Example

import { Observable } from 'rxjs';
import {
  Body,
  Delete,
  Get,
  Path,
  Post,
  Put,
  RestClient,
} from 'typescript-rest-client';
import { v4 as uuidV4 } from 'uuid';
import { environment } from './environments/environment';
import { IUser } from './model/user';
import { HttpService, Injectable } from '@nestjs/common';
import { Client } from 'typescript-nest-rest-client';

@Injectable()
@Client({
  serviceId: () => 'user',
  baseUrl: () => environment.config.apis.rootUrl,
  headers: () => {
    return { 'x-request-type': 'API' };
  },
})
export class UserService extends RestClient {
  private static readonly url = 'users';

  constructor(private http: HttpService) {
    super(http);
  }

  public getDefaultHeaders(): { [p: string]: string } {
    return { 'x-request-id': uuidV4() };
  }

  protected requestInterceptor<T>(req: T) {
    console.dir(req);
  }

  protected responseInterceptor<T>(res: Observable<T>): Observable<any> {
    console.dir(res);
    return res;
  }

  @Get(() => `/${UserService.url}`)
  public getUsers(): Observable<IUser[]> {
    return null;
  }

  @Get(() => `/${UserService.url}/{id}`)
  public getUser(@Path('id') id: number): Observable<IUser> {
    return null;
  }

  @Post(() => `/${UserService.url}`)
  public createUser(@Body user: IUser): Observable<IUser> {
    return null;
  }

  @Put(() => `/${UserService.url}/{id}`)
  public editUser(
    @Path('id') id: number,
    @Body user: IUser
  ): Observable<IUser> {
    return null;
  }

  @Delete(() => `/${UserService.url}/{id}`)
  public deleteUser(@Path('id') id: number): Observable<void> {
    return null;
  }
}
1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

4 years ago

1.0.6-SNAPSHOT

4 years ago

1.0.5-SNAPSHOT

4 years ago

1.0.4-SNAPSHOT

4 years ago

1.0.3-SNAPSHOT

4 years ago

1.0.2-SNAPSHOT

4 years ago

1.0.1-SNAPSHOT

4 years ago

1.0.0-SNAPSHOT

4 years ago