_HttpClient

_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

PropertyDescriptionTypeDefault
nullValueHandlingNull processinginclude,ignoreinclude
dateValueHandlingTime processingtimestamp,ignoretimestamp

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

  • @BaseUrl(url: string)

  • @BaseHeaders(headers: HttpHeaders | { [header: string]: string | string[] })

Method decorators

HttpOptions

PropertyDescriptionTypeDefault
aclACL config, depends on @delon/aclany-
observeSpecify response contentbody,events,response-
responseTypeSpecify content formatarraybuffer,blob,json,text-
reportProgressWhether monitor progress eventsboolean-
withCredentialsSet withCredentialsboolean-

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

    <