Skip to content

HeaderAttribute

Mihael Safaric edited this page Aug 10, 2022 · 4 revisions

HeaderAttribute is used for decorating direct model header properties. It takes one argument, HeaderAttributeOptions, the type and the default values can be checked here.

Options

  • useClass: boolean | class | () => class

    • optional
    • when omitted, the property value from the response will directly be used as a value of decorated model property
    • when true, an instance of property type class will be used as a value of decorated model property. While instantiating an instance of type class, the raw value of the property from the response will be passed as a first parameter to the constructor
    • when string, the class will be searched in DatastoreService.modelTypes based on modelType (see DefiningModelRelationships)
    • when function, the function will be called and it is expected of function to return a class which should be instantiated
    • when instance of SimpleHalModel or HalModel, the value of useClass property will be used as a class for instantiating the property value. While instantiating an instance of type class, the raw value of the property from the response will be passed as a first parameter to the constructor
  • transformResponseValue: (rawAttribute: any) => any

    • optional
    • when provided, the raw property value from the response is passed to transformResponseValue function and returned value is saved to the model (instead of raw value from the response).
  • transformBeforeSave: (rawAttribute: any) => any

    • when provided, the model property value is passed to the transformResponseValue function and returned value is used while creating a payload for the request
  • externalName: string

    • if your model property name and the property name fetched from the server are different, you can specify the external property name setting this property
    • this name will be used when parsing a response from server and also when generating a payload for sending it to the server

Example

Raw response fetched from the server

GET /User/1

Headers:
Content-Type: text/html;
language: en;

{
  "id": "1",
  "name": "John",
  "additionalInfo": {
    "height": "170",
    "weight": "75"
  }
},
...
class User extends HalModel {
  @Attribute()
  public name: string;

  @HeaderAttribute()
  public language: Language;
}

After the model creation, user.language will be equal to en and when saving the model, language header will be set.