_HttpClient service is based on Angular HttpClient
.
Features
More friendly call methods
Maintain loading
attribute
Handling null values
Unified time format is timestamp
Support decorator @GET, @POST etc
DEMO
Network requests are generally used with Object as arguments, such as a get
request:
HttpClient.get(url, { params: { pi: 1 } });
For _HttpClient
:
_HttpClient.get(url, { pi: 1 });
AlainThemeConfig
Common configuration, such as unifying null and time processing for _HttpClient
.
import { AlainThemeConfig } from '@delon/theme';
export function fnAlainThemeConfig(): AlainThemeConfig {
return Object.assign(new AlainThemeConfig(), {
http: {
nullValueHandling: 'ignore',
},
});
}
@NgModule({})
export class DelonModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: DelonModule,
providers: [
{ provide: AlainThemeConfig, useFactory: fnAlainThemeConfig },
],
};
}
}
API
Property | Description | Type | Default |
---|
nullValueHandling | Null processing | include,ignore | include |
dateValueHandling | Time processing | timestamp,ignore | timestamp |
Decorators
The target service must inherit BaseApi
abstract class.
Usage
@BaseUrl('/user')
@BaseHeaders({ bh: 'a' })
class RestService extends BaseApi {
@GET()
query(@Query('pi') pi: number, @Query('ps') ps: number): Observable {
return;
}
@GET(':id')
get(@Path('id') id: number): Observable {
return;
}
@GET()
get(@Payload data: {}): Observable {
return;
}
// Use `::id` to indicate escaping, and should be will be ignored when `id` value is `undefined`, like this:
// When `id` is `10` => 10:type
// When `id` is `undefined` => :id:type
@GET(':id::type')
get(@Path('id') id: number): Observable {
return;
}
@POST(':id')
save(@Path('id') id: number, @Body data: Object): Observable {
return;
}
@POST()
save(@Payload data: {}): Observable {
return;
}
@FORM()
save(@Payload data: {}): Observable {
return;
}
// If authorization is invalid, will be thrown directly `401` error and will not be sent.
@GET('', { acl: 'admin' })
ACL(): Observable {
return;
}
}
Class decorators
Method decorators
HttpOptions
Property | Description | Type | Default |
---|
acl | ACL config, depends on @delon/acl | any | - |
observe | Specify response content | body,events,response | - |
responseType | Specify content format | arraybuffer,blob,json,text | - |
reportProgress | Whether monitor progress events | boolean | - |
withCredentials | Set withCredentials | boolean | - |
Parameter decorators
@Path(key?: string)
URL path parameters
@Query(key?: string)
QueryString of URL
@Body
Body of URL
@Headers(key?: string)
Headers of URL
@Payload
Request Payload
Supported body (likePOST
, PUT
) as a body data, equivalent to @Body
Not supported body (like GET
, DELETE
etc) as a QueryString
<