2.4.1 • Published 5 years ago
ngx-rest-ex v2.4.1
ngx-rest-ex
Deprecated
This package has been migrated to rest-annotations
Installation
npm i ngx-rest-ex --saveExample
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
import { HttpClientModule } from '@angular/common/http';
@NgModule({
imports: [
// ...
HttpClientModule,
],
providers: [ApiClient],
})
export class AppModule { }Using it in your component
@Component({
selector: 'app-to-do',
templateUrl: './to-do.component.html',
})
export class ToDoComponent {
constructor(
private api: ApiClient
) { }
// Use API
}For NodeJS
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 APIAPI Docs
RESTClient
Methods
getBaseUrl(): string: returns the base url of RESTClientgetDefaultHeaders(metaHeaders?: Record<string, string | string[] | number>): Record<string, string | string[] | number>: returns the default headers of RESTClient in a key-value pairgetAuthenticationHeader(action?: string, args?: any[]): Record<string, string | string[] | number>: return authentication header for@AuthenticationrequestInterceptor(req: HttpRequest<any>): void: intercept api requestresponseInterceptor(response: HttpResponse<any>, responseStrategy: string): any: intercept api response_noop(): typing the consumer
Class decorators
@BaseUrl(url: string): will replacegetBaseUrl()of the service@DefaultHeaders(headers: Record<string, string | string[] | number>, overlap?: boolean): set default value forgetDefaultHeaders()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 requestresponseStrategy: strategy of response type, specify one of 'promise', 'httpResponse', 'observable', 'raw'
License
MIT
2.4.1
5 years ago
2.4.0
5 years ago
2.3.1
6 years ago
2.3.0
6 years ago
2.2.1
6 years ago
2.2.0
6 years ago
2.1.0
6 years ago
2.0.0
6 years ago
1.4.0
6 years ago
1.3.1
7 years ago
1.3.0
7 years ago
1.2.7
7 years ago
1.2.6
7 years ago
1.2.5
7 years ago
1.2.4
7 years ago
1.2.3
7 years ago
1.2.2
7 years ago
1.2.1
7 years ago
1.2.0
7 years ago
1.1.4
7 years ago
1.1.3
7 years ago
1.1.2
7 years ago
1.1.1
7 years ago
1.1.0
7 years ago
1.0.1
7 years ago
1.0.0
8 years ago