ng-restclient
TypeScript icon, indicating that this package has built-in type declarations

1.0.8 • Public • Published

ng-restclient npm version Build Status Downloads

針對Angular5+並基於修飾器的REST API Client建構器

快速上手

  1. 安裝
npm install ng-restclient
  1. 定義REST API Class(因為修飾器並不能作用在Interface上)
import {
  ApiBase,
  ApiMethod,
  ApiParameter,
  ApiParameterTypes
} from 'ng-restclient';
import { Observable } from 'rxjs/Observable';
import { RequestMethod } from '@angular/http';
 
@ApiBase('https://jsonplaceholder.typicode.com')
export class FakeAPI {
  @ApiMethod({ url: '/posts/{postId}' })
  public getPost(
    @ApiParameter({ type: ApiParameterTypes.Route })
    postId: number,
    @ApiParameter() value: number
  ): Observable<JSON> {
    return null;
  }
 
  @ApiMethod({ url: '/posts', method: RequestMethod.Post })
  public postPost(): Observable<JSON> {
    return null;
  }
 
  @ApiMethod({ url: '/posts/{postId}', method: RequestMethod.Put })
  public putPost(
    @ApiParameter({ type: ApiParameterTypes.Route })
    postId: number
  ): Observable<JSON> {
    return null;
  }
 
  @ApiMethod({
    url: '/posts/{postId}',
    method: RequestMethod.Delete
  })
  public deletePost(
    @ApiParameter({ type: ApiParameterTypes.Route })
    postId: number
  ): Observable<JSON> {
    return null;
  }
}
  1. 引入Module
import { NgModule } from '@angular/core';
import { NgRestClientModule } from 'ng-restclient';
// something import
 
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    NgRestClientModule
    // something import
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
  1. 透過DI取得Builder,並使用build方法產生實例
import { Component } from '@angular/core';
import { RestClientBuilder } from 'ng-restclient';
import { FakeAPI } from './fakeAPI';
 
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
 
  constructor(private builder: RestClientBuilder) {
    const client: FakeAPI = builder.build(FakeAPI);
    client.getPost(1, null).subscribe(console.log);
  }
}

Readme

Keywords

Package Sidebar

Install

npm i ng-restclient

Weekly Downloads

1

Version

1.0.8

License

MIT

Last publish

Collaborators

  • xupeiyao