-
Notifications
You must be signed in to change notification settings - Fork 1
DatastoreService
DatastoreService is the main service used by the library. This service should be extended by your own datastore service which will be configured to your needs.
constructor(public http: HttpClient) { }-
networkConfig:NetworkConfig- global network configuration
- default values here
- can be set via
DatastoreConfigdecorator or by settingnetworkConfigdirectly onDatastoreService
-
storage:HalStorage- local storage where models are saved
- see HalStorage
-
httpParamsOptions -
modelTypes:Array<typeof HalModel>- list of models that use string identificators when defining relationships, see more in the
Guide - available from
ngx-hal@3
- list of models that use string identificators when defining relationships, see more in the
buildUrl(model?: HalModel): string;- returns URL composed of
baseUrl,endpoint, andmodel endpoint(if model is passed)
createHalDocument<T extends HalModel>(rawResource: RawHalResource, modelClass: ModelConstructor<T> | ModelConstructorFn<T>, rawResponse?: HttpResponse<any>): HalDocument<T>;- returns an instance of
HalDocumentclass or an instance of a class provided as a substitute forHalDocumentclass -
HalDocumentclass resolution strategy:- use
halDocumentClassprovided on aModelServicelevel (viaModelServiceConfig) - if not provided on a
ModelServicelevel, usehalDocumentClassprovided on aDatastoreServicelevel (viaDatastoreConfig) - if not provided on a
DatastoreServicelevel, use a defaultHalDocumentclass
- use
findOne<T extends HalModel>(
modelClass: ModelConstructor<T, P>,
modelId: string,
includeRelationships: Array<string | RelationshipRequestDescriptor> = [],
requestOptions: RequestOptions = {},
customUrl?: string,
subsequentRequestsOptions: RequestOptions
): Observable<T> {-
modelClass- class of a model which will be fetched
-
includeRelationships:Array<string> -
requestOptions:RequestOptions- optional
- additional request options
- those options will be used only for the main request, custom options for relationship calls can be set via
subsequentRequestsOptions
-
customUrl:string- optional
- if provided,
customUrlwill be used when making an HTTP call
-
subsequentRequestsOptions:RequestOptions- optional
- when specified, the specified request options will be used for the relationship calls which do not have request options specified through
includeRelationshipsparameter - if the specific relationship has request options specified in
includeRelationships,subsequentRequestsOptionswill be ignored for that API call
find<T extends HalModel<P>>(modelClass: ModelConstructor<T, P>, params: object | { [param: string]: string | string[] } | HttpParams, includeMeta: boolean, includeRelationships: Array<string | RelationshipRequestDescriptor>, requestOptions: RequestOptions, customUrl?: string, subsequentRequestsOptions?: RequestOptions, storePartialModels?: boolean): Observable<HalDocument<T, P> | Array<T>>;-
modelClass- class of the models which are being fetched
-
params:object | { [param: string]: string | string[] } | HttpParams- optional
- parameters which will be send in the reqest
-
requestOptions.paramsis ignored in favor ofparams(TODO: change that in future versions of the lib)
-
includeMeta:boolean- optional
- if omitted or
falsy,Observable<Array<Model>>is returned - if
truthy,Observable<HalDocument<Model>>is returned (see HalDocument)
-
includeRelationships:Array<string> -
requestOptions:RequestOptions- optional
- additional request options
- those options will be used only for the main request, custom options for relationship calls can be set via
includeRelationshipsorsubsequentRequestsOptions -
requestOptions.paramsis ignored in favor ofparams(TODO: change that in future versions of the lib)
-
customUrl:string- optional
- if provided,
customUrlwill be used when making an HTTP call
-
subsequentRequestsOptions:RequestOptions- optional
- when specified, the specified request options will be used for the relationship calls which do not have request options specified through
includeRelationshipsparameter - if the specific relationship has request options specified in
includeRelationships,subsequentRequestsOptionswill be ignored for that API call
-
storePartialModels:boolean- optional
- default value:
false - forces storing fetched models to the local store
- see HalStorage
-
customUrl:string- optional
- if provided,
customUrlwill be used when making an HTTP call
- makes a custom HTTP request
- GET method
- it expects the response to be a valid HAL document
- it creates a proper HAL model extracted from the response and saves it in the local store
request<T extends HalModel<P>>(method: string, url: string, requestOptions: RequestOptions, modelClass: ModelConstructor<T, P>, singleResource: false): Observable<HalDocument<T>>;request<T extends HalModel<P>>(method: string, url: string, requestOptions: RequestOptions, modelClass: ModelConstructor<T, P>, singleResource: true): Observable<T>;request<T extends HalModel<P>>(method: string, url: string, requestOptions: RequestOptions, modelClass: ModelConstructor<T, P>, singleResource: boolean, includeNetworkConfig: boolean = true): Observable<HalDocument<T, P> | T>;-
method:string- HTTP method, supported values:
GET
- HTTP method, supported values:
-
url:string- request URL
- if
includeNetworkConfigis false, request will be made to{URL} - (default) if
includeNetworkConfigis true, request will be made tonetworkConfig.host/networkConfig.endpoint/{URL}
- if
- request URL
-
requestOptions:RequestOptions- additional request options
-
modelClass- class of a model which will be fetched
-
singleResource:boolean- set to true if single resource is expected in the response, false otherwise
- when true,
Twill be returned,HalDocument<T>otherwise
-
includeNetworkConfig:boolean- see
urlparameter
- see
save<T extends HalModel<P>>(model: T, modelClass: ModelConstructor<T, P>, requestOptions?: RequestOptions, options: CustomOptions = {}): Observable<T>;- makes
PUTrequest if model is already saved,POSTrequest otherwise -
model attributesandrequest options(ones passed as an argument and the global ones) are passed in the request - payload is generated by calling
generatePayloadon a passed model - request headers are generated by calling
generateHeaderson a passed model - if newly created resource is passed in the response of
POSTrequest then a new model is returned - if newly created resource is not passed in the response of
POSTrequest then the existing model is returned- if
Locationheader is set then the model URL is updated (it affects the following model properties:selfLink,uniqueModelIdentificator,id,isSaved)
- if
- options
-
specificFields: Array- optional
- if provided, only the listed fields are taken in consideration while generating a payload (if they are changed)
-
buildUrlFunction- function used for generating the final request URL
- optional
- default:
model.endpoint
-
transformPayloadBeforeSave- optional
- the function gets a generated payload as an argument and the value returned from the function will be the payload which will be sent in the request
-
update<T extends HalModel<P>>(model: T, requestOptions?: RequestOptions, options: CustomOptions = {}): Observable<T>;- make a
PATCHrequest which changed properties only - options
-
specificFields: Array- optional
- if provided, only the listed fields are taken in consideration while generating a payload (if they are changed)
-
buildUrlFunction- function used for generating the final request URL
- optional
- default:
model.endpoint
-
transformPayloadBeforeSave- optional
- the function gets a generated payload as an argument and the value returned from the function will be the payload which will be sent in the request
-
delete<T extends HalModel<P>>(model: T, requestOptions?: RequestOptions, options?: CustomOptions: Observable<void>;- makes
DELETErequest -
request options(ones passed as an argument and the global ones) are passed in the request -
options-
buildUrlFunction- function used for generating the final request URL
- optional
- default:
model.defaultUrlBuildFunction
-
- the model is removed from the local storage
head<T extends HalModel<P>>(url: string, requestOptions?: RequestOptions): Observable<any>;- makes
HEADrequest -
request options(ones passed as an argument and the global ones) are passed in the request
fetchModelRelationships<T extends HalModel<P>>(model: T, relationshipNames: RelationshipRequestDescriptor | Array<RelationshipRequestDescriptor>): Observable<T>;- fetches one or more relationships of passed
model - returns the original model which is passed through arguments
- the original model is fetched from the local store (HTTP call won't be made)
-
requestOptions- optional
- options which will be passed to all requests
createModel<T extends HalModel<P>>(modelClass: ModelConstructor<T, P>, recordData: object = {}): T;- locally creates an instance of a model class which is passed through the parameters,
modelClass(it does not tigger saving of newly created model) - model properties and relationships can be passed via
recordData
DatastoreService can be configured via DatastoreConfig decorator.