适用于 Angular 2 的资源 (REST) 客户端
| @ngx-resource/core | @ngx-resource/handler-ngx-http | @ngx-resource/handler-cordova-advanced-http | @ngx-resource/handler-ngx-http-legacy |
|---|---|---|---|
Resource Core is an evolution of ngx-resource lib which provides flexibility for developers. Each developer can implement their own request handlers to easily customize the behavior.
In fact, @ngx-resource/core is an abstract common library which uses ResourceHandler to make requests, so it's even possible to use the lib on node.js server side with typescript. You just need to implement a ResourceHandler for it.
All my examples will be based on angular 4.4.4+
@ngx-resource/handler-ngx-http. Based on HttpClient from @angular/common/http. Includes ResourceModule.forRoot.@ngx-resource/handler-ngx-http-legacy. Based on Http from @angular/http. Includes ResourceModule.forRoot.@ngx-resource/handler-cordova-advanced-http. Based on Cordova Plugin Advanced HTTP.@ngx-resource/handler-fetch. Besed on Fetch API. Not yet created.…Final url is generated by concatination of $getUrl, $getPathPrefix and $getPath methods of Resource base class.
Is used by ResourceParams 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; default nullparams?: any; - default url params; default nullquery?: any; - defualt query params; default nullrootNode?: string; - key to assign all body; default nullremoveTrailingSlash?: boolean; - default trueaddTimestamp?: boolean | string; - default falsewithCredentials?: boolean; - default falselean?: boolean; - do no add $ properties on result. Used only with toPromise: false default falsemutateBody?: boolean; - if need to mutate provided body with response body. default falsereturnAs?: ResourceActionReturnType; - what type response should be returned by action/method . default ResourceActionReturnType.ObservablekeepEmptyBody?: boolean; - if need to keep empty body object {}requestBodyType?: ResourceRequestBodyType; - request body type. default: will be detected automatically.
Check for possible body types in the sources of ResourceRequestBodyType. Type detection algorithm check here.responseBodyType?: ResourceResponseBodyType; - response body type. default: ResourceResponseBodyType.JSON Possible body type can be checked here ResourceResponseBodyType.Is used by ResourceAction decorator for methods.
List of params (is all of the above) plus the following:
method?: ResourceRequestMethod; - method of request. Default ResourceRequestMethod.Get. All possible methods listed in ResourceRequestMethodexpectJsonArray?: boolean; - if expected to receive an array. The field is used only with toPromise: false. Default false.resultFactory?: IResourceResultFactory; - custom method to create result object. Default: returns {}map?: IResourceResponseMap; - custom data mapping method. Default: returns without any changesfilter?: IResourceResponseFilter; - custom data filtering method. Default: returns trueMainly used to set defaults.
An object with built-in in methods to save, update, and delete a model.
Here is an example of a User model.
Note: UserResource should be injected at the beginning in order to use static
model method like User.get(), User.query(), User.remove()
…You can define the way query params are converted. Set the global config at the root of your app.
ResourceGlobalConfig.queryMappingMethod = ResourceQueryMappingMethod.
{
a: [{ b:1, c: [2, 3] }]
}With `` being one of the following:
No conversion at all
Output: ?a=[Object object]
All array elements will be indexed
Output: ?a[0][b]=10383&a[0][c][0]=2&a[0][c][1]=3
Implements the standard $.params way of converting
Output: ?a[0][b]=10383&a[0][c][]=2&a[0][c][]=3
It's implementation of ResourceHandler which uses Angular HttpClient
& npm i --save @ngx-resource/core @ngx-resource/handler-ngx-httpIn you app module
…暂无开放 Issues,或尚未同步最近议题。