Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

ng-restclient

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ng-restclient

針對Angular5+提供使用修飾器定義產生的REST Client

latest
Source
npmnpm
Version
1.0.8
Version published
Weekly downloads
3
-50%
Maintainers
1
Weekly downloads
 
Created
Source

ng-restclient npm version Build Status Downloads

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

快速上手

  • 安裝
npm install ng-restclient
  • 定義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;
  }
}
  • 引入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 { }
  • 透過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);
  }
}

Keywords

REST

FAQs

Package last updated on 03 Dec 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts