angular2-odata
Advanced tools
Comparing version
import { RequestOptions, Response } from '@angular/http'; | ||
import { PagedResult } from "./query"; | ||
export declare class KeyConfigs { | ||
@@ -13,5 +14,6 @@ Filter: string; | ||
handleError(err: any, caught: any): void; | ||
readonly requestOptions: RequestOptions; | ||
readonly postRequestOptions: RequestOptions; | ||
extractQueryResultData<T>(res: Response): Array<T>; | ||
requestOptions: RequestOptions; | ||
postRequestOptions: RequestOptions; | ||
extractQueryResultData<T>(res: Response): T[]; | ||
extractQueryResultDataWidhCount<T>(res: Response): PagedResult<T>; | ||
} |
@@ -13,2 +13,3 @@ "use strict"; | ||
const http_1 = require('@angular/http'); | ||
const query_1 = require("./query"); | ||
class KeyConfigs { | ||
@@ -55,2 +56,20 @@ constructor() { | ||
} | ||
extractQueryResultDataWidhCount(res) { | ||
let r = new query_1.PagedResult(); | ||
if (res.status < 200 || res.status >= 300) { | ||
throw new Error('Bad response status: ' + res.status); | ||
} | ||
let body = res.json(); | ||
let entities = body.value; | ||
r.data = entities; | ||
try { | ||
let count = parseInt(body["@odata.count"]) || entities.length; | ||
r.count = count; | ||
} | ||
catch (error) { | ||
console.warn("Cannot determine OData entities count. Falling back to collection length..."); | ||
r.count = entities.length; | ||
} | ||
return r; | ||
} | ||
}; | ||
@@ -57,0 +76,0 @@ ODataConfiguration = __decorate([ |
export { ODataService } from "./odata"; | ||
export { ODataConfiguration } from "./config"; | ||
export { ODataQuery } from "./query"; | ||
export { ODataQuery, PagedResult } from "./query"; | ||
export { ODataServiceFactory } from "./odataservicefactory"; |
@@ -8,4 +8,5 @@ "use strict"; | ||
exports.ODataQuery = query_1.ODataQuery; | ||
exports.PagedResult = query_1.PagedResult; | ||
var odataservicefactory_1 = require("./odataservicefactory"); | ||
exports.ODataServiceFactory = odataservicefactory_1.ODataServiceFactory; | ||
//# sourceMappingURL=index.js.map |
@@ -11,3 +11,3 @@ import { Http, Response } from '@angular/http'; | ||
constructor(_typeName: string, http: Http, config: ODataConfiguration); | ||
readonly TypeName: string; | ||
TypeName: string; | ||
Get(key: string): GetOperation<T>; | ||
@@ -14,0 +14,0 @@ Post(entity: T): Observable<T>; |
@@ -5,2 +5,6 @@ import { Http } from '@angular/http'; | ||
import { ODataOperation } from "./operation"; | ||
export declare class PagedResult<T> { | ||
data: T[]; | ||
count: number; | ||
} | ||
export declare class ODataQuery<T> extends ODataOperation<T> { | ||
@@ -18,3 +22,5 @@ constructor(_typeName: string, config: ODataConfiguration, http: Http); | ||
Exec(): Observable<Array<T>>; | ||
ExecWithCount(): Observable<PagedResult<T>>; | ||
private extractArrayData(res, config); | ||
private extractArrayDataWithCount(res, config); | ||
} |
"use strict"; | ||
const rx_1 = require('rxjs/rx'); | ||
const operation_1 = require("./operation"); | ||
class PagedResult { | ||
} | ||
exports.PagedResult = PagedResult; | ||
class ODataQuery extends operation_1.ODataOperation { | ||
@@ -44,7 +47,21 @@ constructor(_typeName, config, http) { | ||
} | ||
ExecWithCount() { | ||
let params = this.getQueryParams(); | ||
params.set("$count", 'true'); // OData v4 only | ||
let config = this.config; | ||
return this.http.get(this.config.baseUrl + "/" + this._typeName + "/", { search: params }) | ||
.map(res => this.extractArrayDataWithCount(res, config)) | ||
.catch((err, caught) => { | ||
this.config.handleError && this.config.handleError(err, caught); | ||
return rx_1.Observable.throw(err); | ||
}); | ||
} | ||
extractArrayData(res, config) { | ||
return config.extractQueryResultData(res); | ||
} | ||
extractArrayDataWithCount(res, config) { | ||
return config.extractQueryResultDataWidhCount(res); | ||
} | ||
} | ||
exports.ODataQuery = ODataQuery; | ||
//# sourceMappingURL=query.js.map |
import { Injectable } from '@angular/core'; | ||
import { RequestOptions, Headers, Response } from '@angular/http'; | ||
import { PagedResult } from "./query"; | ||
export class KeyConfigs{ | ||
public Filter:string = "$filter"; | ||
public Top:string = "$top"; | ||
public Skip:string = "$skip"; | ||
public OrderBy:string = "$orderby"; | ||
export class KeyConfigs { | ||
public Filter: string = "$filter"; | ||
public Top: string = "$top"; | ||
public Skip: string = "$skip"; | ||
public OrderBy: string = "$orderby"; | ||
} | ||
@Injectable() | ||
export class ODataConfiguration{ | ||
baseUrl:string = window.location.origin + "/odata"; | ||
export class ODataConfiguration { | ||
baseUrl: string = window.location.origin + "/odata"; | ||
public getEntityUri(entityKey:string, _typeName:string){ | ||
public getEntityUri(entityKey: string, _typeName: string) { | ||
//ToDo: Fix string based keys | ||
if ( !parseInt(entityKey) ){ | ||
return this.baseUrl + "/"+_typeName+"('"+entityKey+"')"; | ||
if (!parseInt(entityKey)) { | ||
return this.baseUrl + "/" + _typeName + "('" + entityKey + "')"; | ||
} | ||
return this.baseUrl + "/"+_typeName+"("+entityKey+")"; | ||
return this.baseUrl + "/" + _typeName + "(" + entityKey + ")"; | ||
} | ||
public Keys:KeyConfigs = new KeyConfigs(); | ||
handleError(err:any, caught:any):void{ | ||
console.warn("OData error: ", err,caught); | ||
public Keys: KeyConfigs = new KeyConfigs(); | ||
handleError(err: any, caught: any): void { | ||
console.warn("OData error: ", err, caught); | ||
}; | ||
get requestOptions(): RequestOptions{ | ||
return new RequestOptions({body: ''}); | ||
get requestOptions(): RequestOptions { | ||
return new RequestOptions({ body: '' }); | ||
}; | ||
get postRequestOptions():RequestOptions{ | ||
get postRequestOptions(): RequestOptions { | ||
let headers = new Headers({ 'Content-Type': 'application/json; charset=utf-8' }); | ||
@@ -39,3 +40,3 @@ return new RequestOptions({ headers: headers }); | ||
public extractQueryResultData<T>(res: Response):Array<T> { | ||
public extractQueryResultData<T>(res: Response): T[] { | ||
if (res.status < 200 || res.status >= 300) { | ||
@@ -45,5 +46,27 @@ throw new Error('Bad response status: ' + res.status); | ||
let body = res.json(); | ||
let entities:Array<T> = body.value; | ||
let entities: T[] = body.value; | ||
return entities; | ||
} | ||
} | ||
public extractQueryResultDataWidhCount<T>(res: Response): PagedResult<T> { | ||
let r = new PagedResult<T>(); | ||
if (res.status < 200 || res.status >= 300) { | ||
throw new Error('Bad response status: ' + res.status); | ||
} | ||
let body = res.json(); | ||
let entities: T[] = body.value; | ||
r.data = entities; | ||
try { | ||
let count = parseInt(body["@odata.count"]) || entities.length; | ||
r.count = count; | ||
} catch (error) { | ||
console.warn("Cannot determine OData entities count. Falling back to collection length..."); | ||
r.count = entities.length; | ||
} | ||
return r; | ||
} | ||
} |
export {ODataService} from "./odata"; | ||
export {ODataConfiguration} from "./config"; | ||
export {ODataQuery} from "./query"; | ||
export {ODataQuery, PagedResult} from "./query"; | ||
export {ODataServiceFactory} from "./odataservicefactory"; |
{ | ||
"name": "angular2-odata", | ||
"version": "0.0.17", | ||
"version": "0.0.18", | ||
"description": "OData service for Angular2", | ||
@@ -5,0 +5,0 @@ "main": "./build/index.js", |
25
query.ts
import { Injectable } from '@angular/core'; | ||
import { URLSearchParams, Http, Response } from '@angular/http'; | ||
import { Observable, Operator } from 'rxjs/rx'; | ||
import { Observable, Operator, Subject } from 'rxjs/rx'; | ||
import { ODataConfiguration } from "./config"; | ||
import { ODataOperation } from "./operation"; | ||
export class PagedResult<T>{ | ||
public data: T[]; | ||
public count: number; | ||
} | ||
export class ODataQuery<T> extends ODataOperation<T>{ | ||
@@ -54,2 +59,16 @@ constructor(_typeName:string, config:ODataConfiguration, http:Http) { | ||
} | ||
public ExecWithCount():Observable<PagedResult<T>>{ | ||
let params = this.getQueryParams(); | ||
params.set("$count",'true'); // OData v4 only | ||
let config = this.config; | ||
return this.http.get(this.config.baseUrl + "/"+this._typeName+"/", {search: params}) | ||
.map(res=>this.extractArrayDataWithCount(res,config)) | ||
.catch((err:any,caught:Observable<PagedResult<T>>)=>{ | ||
this.config.handleError && this.config.handleError(err,caught); | ||
return Observable.throw(err); | ||
}); | ||
} | ||
@@ -59,2 +78,6 @@ private extractArrayData(res: Response, config:ODataConfiguration):Array<T> { | ||
} | ||
private extractArrayDataWithCount(res:Response, config: ODataConfiguration):PagedResult<T>{ | ||
return config.extractQueryResultDataWidhCount<T>(res); | ||
} | ||
} |
@@ -11,3 +11,3 @@ # angular2-odata | ||
``` | ||
import { ODataConfiguration, ODataServiceFactory } from "angular2-odata"; | ||
import { ODataConfiguration, ODataServiceFactory, ODataService } from "angular2-odata"; | ||
import { bootstrap } from "angular2/platform/browser"; | ||
@@ -14,0 +14,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
54091
9.61%912
9.75%