百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
N

ngx-resource

> 前端框架
开源

适用于 Angular 2 的资源 (REST) 客户端

198 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

适用于 Angular 2 的资源 (REST) 客户端

Unfortunately I don't have time to maintain the library. So PRs are more than welcome.

@ngx-resource/core @ngx-resource/handler-ngx-http @ngx-resource/handler-cordova-advanced-http @ngx-resource/handler-ngx-http-legacy

@ngx-resource/core

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+

Known ResourceHandlers

  • @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.

Creation of Resource class

…

Final url is generated by concatination of $getUrl, $getPathPrefix and $getPath methods of Resource base class.

IResourceParams

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 null
  • params?: any; - default url params; default null
  • query?: any; - defualt query params; default null
  • rootNode?: string; - key to assign all body; default null
  • removeTrailingSlash?: boolean; - default true
  • addTimestamp?: boolean | string; - default false
  • withCredentials?: boolean; - default false
  • lean?: boolean; - do no add $ properties on result. Used only with toPromise: false default false
  • mutateBody?: boolean; - if need to mutate provided body with response body. default false
  • returnAs?: ResourceActionReturnType; - what type response should be returned by action/method . default ResourceActionReturnType.Observable
  • keepEmptyBody?: 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.

IResourceAction

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 ResourceRequestMethod
  • expectJsonArray?: 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 changes
  • filter?: IResourceResponseFilter; - custom data filtering method. Default: returns true

ResourceGlobalConfig

Mainly used to set defaults.

Models

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()

…

QueryParams Conversion

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:

Plain (default)

No conversion 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

@ngx-resource/handler-ngx-http

It's implementation of ResourceHandler which uses Angular HttpClient

If you are using Angular 5, please use @ngx-resource/handler-ngx-http 5.x

How to install and setup it

bash
& npm i --save @ngx-resource/core @ngx-resource/handler-ngx-http

In you app module

…

Docs about @ngx-resource/core

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptangular-2networkrequestsrest

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类前端框架
定价开源

> 相关工具

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架