File

projects/ngrx-data-lib/src/lib/http/request-provider.ts

Description

represent an http request.

Index

Properties
Methods
Accessors

Constructor

constructor(route: string[], params: NameValue[], requestType: HttpMethod, body: any, responseFormats: IFormatConverter[], requestFormat: IFormatConverter, dataMapper: IDataMapper)
Parameters :
Name Type Optional
route string[] No
params NameValue[] No
requestType HttpMethod No
body any No
responseFormats IFormatConverter[] No
requestFormat IFormatConverter No
dataMapper IDataMapper No

Properties

Private _body
Type : any
Default value : null
Private _dataMapper
Type : IDataMapper
Private _requestFormat
Type : IFormatConverter
Private _requestType
Type : HttpMethod
Private _responseFormats
Type : IFormatConverter[]
Private _url
Type : string
Default value : null
Private params
Type : NameValue[]
Private route
Type : string[]

Methods

at
at(param: string)

add a path param to the url.

Parameters :
Name Type Optional Description
param string No

the param to add to the url.

Returns : RequestProvider
withBody
withBody(body: any)

add body to the request (only valid for POST and PUT requests)

Parameters :
Name Type Optional Description
body any No

the body to add

Returns : RequestProvider
withDataMapper
withDataMapper(mapper: IDataMapper)

set the data mapper to be used in this request.

Parameters :
Name Type Optional Description
mapper IDataMapper No

data mapper to be used.

Returns : RequestProvider
withParam
withParam(name: string, value: string)

Add a query param to the url (only valid for GET and DELETE requests)

Parameters :
Name Type Optional Description
name string No

the name of the parameter.

value string No

the value of the parameter.

Returns : RequestProvider
withRequestFormat
withRequestFormat(format: IFormatConverter)
Parameters :
Name Type Optional
format IFormatConverter No
Returns : RequestProvider

Accessors

responseFormats
getresponseFormats()

get the response formats that this request can parse.

Returns : IFormatConverter[]
requestFormat
getrequestFormat()

get the format converter to parse the request body in the case of POST and PUT.

Returns : IFormatConverter
requestType
getrequestType()

get the request type asociated with this provider.

Returns : HttpMethod
body
getbody()

get the request body or null if is not available.

Returns : any
dataMapper
getdataMapper()

get the data mapper asociated with this request.

Returns : IDataMapper
url
geturl()

get the url generated by this provider

Returns : string
import { NameValue } from '../models/name-value';
import { HttpMethod } from './http-method';
import { IFormatConverter } from '../data-format/format-converter';
import { IDataMapper } from '../data-mapping/datamapper';

/**
 * represent an http request.
 */
export class RequestProvider {
    
    //the route of the url.
    private route: string[];
  
    //the params to include in the url.
    private params: NameValue[];
  
    //the request body.
    private _body: any = null;
  
    //the constructed url.
    private _url: string = null;
  
    //the request type.
    private _requestType: HttpMethod;
  
    //response formats.
    private _responseFormats: IFormatConverter[];

    //response formats.
    private _requestFormat: IFormatConverter;

    //data mapper to be used to map the response into db actions.
    private _dataMapper: IDataMapper;

    /**
     * get the response formats that this request can parse.
     */
    get responseFormats() : IFormatConverter[]
    {
       return this._responseFormats;
    }

    /**
     * get the format converter to parse the request body in the case of POST and PUT.
     */
    get requestFormat() : IFormatConverter
    {
       return this._requestFormat;
    }

    /**
     * get the request type asociated with this provider.
     */
    get requestType(): HttpMethod {
      return this._requestType;
    }
  
    /**
     * get the request body or null if is not available.
     */
    get body(): any {
      return this._body;
    }

    /**
     * get the data mapper asociated with this request.
     */
    get dataMapper() : IDataMapper
    {
      return this._dataMapper;
    }
  
    /**
    * get the url generated by this provider
    */
    get url(): string {
  
      if (!this._url) {
        //create the url
        let url = this.route.join('/');
        let parsedParams: string[] = [];
  
        this.params.forEach((v) => {
          parsedParams.push([v.name, v.value].join('='));
        });
  
        this._url = encodeURI(url + '?' + parsedParams.join('&'));
      }
      
      return this._url;
  
    }
  
    constructor(route: string[], params: NameValue[], requestType: HttpMethod, body: any, responseFormats: IFormatConverter[], requestFormat: IFormatConverter, dataMapper: IDataMapper) {
  
      //clone arrays
      this.route = route.slice(0);
      this.params = params.slice(0);
      
      this._requestType = requestType;
      this._body = body;
      this._responseFormats = responseFormats;
      this._requestFormat = requestFormat;
      this._dataMapper = dataMapper;
    }

    //Start of the fluent api
    //-------------------------------------------------------------------------------------------------------
  
    /**
     * add a path param to the url.
     * @param param the param to add to the url.
     */
    at(param: string): RequestProvider {
      return new RequestProvider(this.route.concat(param), this.params, this._requestType, this._body, this._responseFormats, this._requestFormat, this._dataMapper);
    }
  
    /**
     * Add a query param to the url (only valid for GET and DELETE requests)
     * @param name the name of the parameter.
     * @param value the value of the parameter.
     */
    withParam(name: string, value: string): RequestProvider {
      if (this._requestType != 'GET' && this._requestType != 'DELETE' )
        throw "query params are only valid for GET and DELETE requests.";
  
      return new RequestProvider(this.route, this.params.concat(new NameValue(name, value)), this._requestType, this._body, this._responseFormats, this._requestFormat, this._dataMapper);
    }
  
    /**
     * add body to the request (only valid for POST and PUT requests)
     * @param body the body to add
     */
    withBody(body: any): RequestProvider {
      if (this._requestType != 'POST' && this._requestType != 'PUT')
        throw "request body is only valid for POST and PUT requests.";
  
      if (this._body != null)
        throw "request body can only be assigned one time.";
  
      return new RequestProvider(this.route, this.params, this._requestType, body, this._responseFormats, this._requestFormat, this._dataMapper);
    }

    withRequestFormat(format: IFormatConverter) : RequestProvider
    {
      if (this._requestType != 'POST' && this._requestType != 'PUT')
        throw "request format is only valid for POST and PUT requests.";

      return new RequestProvider(this.route, this.params, this._requestType, this._body, this._responseFormats, format, this._dataMapper); 
    }

    /**
     * set the data mapper to be used in this request.
     * @param mapper data mapper to be used.
     */
    withDataMapper(mapper: IDataMapper)
    {
      return new RequestProvider(this.route, this.params, this._requestType, this._body, this._responseFormats, this.requestFormat, mapper); 
    } 
  
  }
  

result-matching ""

    No results matching ""