rest-core
Rest Core is an evolution of ngx-resource lib which provides freedom for the developers. Each developer can implement his own request handlers.
In fact, rest-core
is an abstract common library which uses RestHandler
to make an requests, so it's even possible to use the lib on node.js server side with typescript. Just need to implement RestHandler
for it.
All my examples will be based on angalar 4.4.4+
Known RestHandlers
rest-ngx
. Based onHttpClient
from@angular/common/http
. IncludesRestModule.forRoot
.rest-ngx-http
. Based onHttp
from@angular/http
. IncludesRestModule.forRoot
.rest-cordova-advanced-http
. Based on Cordova Plugin Advanced HTTP.rest-fetch
. Besed on Fetch API. Not yet created.
Creation of rest class
// Using created rest
Final url is generated by concatination of $getUrl
, $getPathPrefix
and $getPath
methods of Rest
base class.
IRestParams
Is used by RestParams
decorator for class decoration
List of params:
url?: string;
- url of the api server; default''
pathPrefix?: string;
- path prefix of the api; default''
path?: string;
- path of the api; default''
headers?: any;
- headers; default{}
body?: any;
- default body; defaultnull
params?: any;
- default url params; defaultnull
query?: any;
- defualt query params; defaultnull
rootNode?: string;
- key to assign all body; defaultnull
removeTrailingSlash?: boolean;
- defaulttrue
addTimestamp?: boolean | string;
- defaultfalse
withCredentials?: boolean;
- defaultfalse
lean?: boolean;
- do no add$
properties on result. Used only withtoPromise: false
defaultfalse
mutateBody?: boolean;
- if need to mutate provided body with response body. defaultfalse
asPromise?: boolean;
- if method should return promise or object, which will be fullfilled after receiving response. defaulttrue
requestBodyType?: RestRequestBodyType;
- request body type. default: will be detected automatically. Check for possible body types in the sources of RestRequestBodyType. Type detection algorithm check here.responseBodyType?: RestResponseBodyType;
- response body type. default:RestResponseBodyType.JSON
Possible body type can be checked here RestResponseBodyType.
IRestAction
Is used by RestAction
decorator for methods.
List of params (is all above) plus following:
method?: RestRequestMethod;
- method of request. DefaultRestRequestMethod.Get
. All possible methods listed in RestRequestMethodexpectJsonArray?: boolean;
- if expected to receive an array. The field is used only withtoPromise: false
. Defaultfalse
.resultFactory?: IRestResultFactory;
- custom method to create result object. Default:returns {}
map?: IRestResponseMap;
- custom data mapping method. Default:returns without any changes
filter?: IRestResponseFilter;
- custom data filtering method. Default:returns true
RestGlobalConfig
Mainly used to set defaults
Models
What is that. It's an object which has build in methods to save, update, delete an model.
Here is an example of User
model.
// example of using the staffasync someMethodToCreateGroupAndUser
QueryParams Conversion
You can define the way query params are converted Set the global config at the root of your app.
RestGlobalConfig.queryMappingMethod = RestQueryMappingMethod.<CONVERTION_STRATEGY>
{
a: [{ b:1, c: [2, 3] }]
}
With <CONVERTION_STRATEGY>
being an enumerable within
Plain (default)
No convertion at all.
Output: ?a=[Object object]
Bracket
All array elements will be indexed
Output: ?a[0][b]=10383&a[0][c][0]=2&a[0][c][1]=3
JQueryParamsBracket
Implements the standard $.params way of converting
Output: ?a[0][b]=10383&a[0][c][]=2&a[0][c][]=3
Developing Rest Handler
Use RestHandler
abstract class as parent to create your Handler. I think it's clear what should it do by checking sources of the class.