rebirth http
v6.0.2
Un cliente HTTP tipo Java JPA para Angular.
$ npm install @ng-zorro/rebirth-http --saveRebirthHttpModule import { RebirthHttpModule } from '@ng-zorro/rebirth-http' ;
@ NgModule ( {
imports : [ BrowserModule , RebirthHttpModule ] ,
declarations : [ AppComponent ] ,
bootstrap : [ AppComponent ] ,
} )
export class AppModule { } import {
Any ,
RebirthHttpClient ,
JSONP ,
GET ,
POST ,
PUT ,
DELETE ,
PATCH ,
BaseUrl ,
Query ,
Path ,
Body ,
} from 'rebirth-http' ;
import { Observable } from 'rxjs/Observable' ;
import { HttpClient } from '@angular/common/http' ;
@ Injectable ( )
export class ArticleService extends RebirthHttpClient {
constructor ( http : HttpClient ) {
super ( http ) ;
}
@ GET ( 'article' )
getArticles (
@ Query ( 'pageIndex' ) pageIndex = 1 ,
@ Query ( 'pageSize' ) pageSize = 10
) : Observable < SearchResult < Article > > {
return Any ; // return Any as a placeholder
}
@ GET ( 'article/:id' )
getArticleByUrl ( @ Path ( 'id' ) articleUrl : string ) : Observable < Article > {
return Any ;
}
@ POST ( 'article' )
createArticle ( @ Body article : Article ) : Observable {
return Any ;
}
@ PUT ( 'article/:id' )
updateArticle (
@ Path ( 'id' ) id : string ,
@ Body article : Article
) : Observable < Article > {
return Any ;
}
@ DELETE ( 'article/:id' )
deleteArticleById ( @ Path ( 'id' ) id : string ) : Observable < Article > {
return Any ;
}
@ JSONP ( 'article/:id' )
getArticleByJsonp (
@ Path ( 'id' ) id : string ,
@ Query ( 'name' ) name : string
) : Observable < Article > {
return Any ;
}
@ POST ( 'file/upload' )
upload ( @ Body formData : FormData ) : Observable < any > {
return Any ;
}
}ArticleService ¡Ahora puede llamar a métodos de ArticleService para activar solicitudes HTTP! :)
Puede agregar interceptores para cualquier RebirthHttpClient .
import { config } from '@/config' ;
import { RebirthHttpProvider } from '@ng-zorro/rebirth-http' ;
@ Component ( {
selector : 'app-root' ,
} )
export class AppComponent {
constructor ( rebirthHttpProvider : RebirthHttpProvider ) {
rebirthHttpProvider . baseUrl ( config . api . host ) . addInterceptor ( {
request : ( request ) =>
console . log ( 'Global interceptors(request)' , request ) ,
response : ( response ) =>
console . log ( 'Global interceptors(response)' , response ) ,
} ) ;
}
} RebirthHttpClient Cada servicio responsable de HTTP debe heredar RebirthHttpClient .
getBaseUrl(): string : devuelve la URL base del cliente RebirthHttpgetDefaultHeaders(): { [name: string]: string } : devuelve los encabezados predeterminados de RebirthHttp en un par clave-valor Estos decoradores deben usarse en clases que heredan RebirthHttpClient .
@BaseUrl(url: string) : cambia la URL base de un RebirthHttpClient@DefaultHeaders(headers: Object) : cambia el encabezado predeterminado de un RebirthHttpClient Estos decoradores crearían métodos de RebirthHttpClient capaces de enviar solicitudes HTTP.
@GET(url: string)@POST(url: string)@PUT(url: string)@DELETE(url: string)@JSONP(url: string)@PATCH(url: string)@HEAD(url: string)@OPTIONS(url: String)Puede utilizar los decoradores siguientes para modificar las opciones HTTP.
@Headers(headers: any)@RequestOptions(headers: any)@Extra(headers: any) Utilice los decoradores siguientes para decorar los parámetros de los métodos de RebirthHttpClient , de modo que el parámetro se convierta en las opciones HTTP correspondientes.
@Path(key: string)@Query(key: string)@Header(key: string)@BodyGracias Paldom/angular2-rest nos dio la inspiración.
MIT