ohayolibs
Version:
Ohayo is a set of essential modules for ohayojp.
156 lines (122 loc) • 4.26 kB
Markdown
---
order: 1
title: _HttpClient
type: Service
---
[\_HttpClient](https://github.com/ohayojp/ohayo/blob/master/packages/theme/src/services/http/http.client.ts) service is based on Angular `HttpClient`.
## Features
- More friendly call methods
- Maintain `loading` attribute
- Handling null values
- Unified time format is timestamp
- Support decorator , etc
## DEMO
Network requests are generally used with Object as arguments, such as a `get` request:
```ts
HttpClient.get(url, { params: { pi: 1 } });
```
For `_HttpClient`:
```ts
_HttpClient.get(url, { pi: 1 });
```
## OhayoThemeConfig
Common configuration, such as unifying null and time processing for `_HttpClient`.
```ts
import { OhayoThemeConfig } from '@ohayo/theme';
export function fnOhayoThemeConfig(): OhayoThemeConfig {
return Object.assign(new OhayoThemeConfig(), <OhayoThemeConfig>{
http: {
nullValueHandling: 'ignore',
},
});
}
export class OhayoModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: OhayoModule,
providers: [
{ provide: OhayoThemeConfig, useFactory: fnOhayoThemeConfig },
],
};
}
}
```
### 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
```ts
class RestService extends BaseApi {
query( pi: number, ps: number): Observable<any> {
return;
}
get( id: number): Observable<any> {
return;
}
get( data: {}): Observable<any> {
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: number): Observable<any> {
return;
}
save( id: number, data: Object): Observable<any> {
return;
}
save( data: {}): Observable<any> {
return;
}
save( data: {}): Observable<any> {
return;
}
// If authorization is invalid, will be thrown directly `401` error and will not be sent.
ACL(): Observable<any> {
return;
}
}
```
### Class decorators
- ``
- ``
### Method decorators
- ``
- ``
- ``
- ``
- ``
- ``
- ``
- ``
#### HttpOptions
| Property | Description | Type | Default |
| ----------------- | ----------------------------------- | ---------------------------- | ------- |
| `acl` | ACL config, depends on `/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
- `` URL path parameters
- `` QueryString of URL
- `` Body of URL
- `` Headers of URL
- `` Request Payload
- Supported body (like`POST`, `PUT`) as a body data, equivalent to ``
- Not supported body (like `GET`, `DELETE` etc) as a `QueryString`