-
Notifications
You must be signed in to change notification settings - Fork 1
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.
-
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 inDatastoreService.modelTypesbased onmodelType(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
SimpleHalModelorHalModel, the value ofuseClassproperty 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
transformResponseValuefunction 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
transformResponseValuefunction and returned value is used while creating a payload for the request
- when provided, the model property value is passed to the
-
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
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.