angular2-odata
Advanced tools
Comparing version
import { RequestOptions, Response } from '@angular/http'; | ||
import { PagedResult } from "./query"; | ||
import { PagedResult } from './query'; | ||
export declare class KeyConfigs { | ||
Filter: string; | ||
Top: string; | ||
Skip: string; | ||
OrderBy: string; | ||
filter: string; | ||
top: string; | ||
skip: string; | ||
orderBy: string; | ||
select: string; | ||
expand: string; | ||
} | ||
export declare class ODataConfiguration { | ||
keys: KeyConfigs; | ||
baseUrl: string; | ||
getEntityUri(entityKey: string, _typeName: string): string; | ||
Keys: KeyConfigs; | ||
handleError(err: any, caught: any): void; | ||
@@ -17,3 +19,3 @@ requestOptions: RequestOptions; | ||
extractQueryResultData<T>(res: Response): T[]; | ||
extractQueryResultDataWidhCount<T>(res: Response): PagedResult<T>; | ||
extractQueryResultDataWithCount<T>(res: Response): PagedResult<T>; | ||
} |
@@ -13,9 +13,12 @@ "use strict"; | ||
const http_1 = require('@angular/http'); | ||
const query_1 = require("./query"); | ||
const query_1 = require('./query'); | ||
// import { Location } from '@angular/common'; | ||
class KeyConfigs { | ||
constructor() { | ||
this.Filter = "$filter"; | ||
this.Top = "$top"; | ||
this.Skip = "$skip"; | ||
this.OrderBy = "$orderby"; | ||
this.filter = '$filter'; | ||
this.top = '$top'; | ||
this.skip = '$skip'; | ||
this.orderBy = '$orderby'; | ||
this.select = '$select'; | ||
this.expand = '$expand'; | ||
} | ||
@@ -26,14 +29,17 @@ } | ||
constructor() { | ||
this.baseUrl = window.location.origin + "/odata"; | ||
this.Keys = new KeyConfigs(); | ||
this.keys = new KeyConfigs(); | ||
this.baseUrl = 'http://localhost/odata'; | ||
} | ||
// constructor(location?: Location) { | ||
// this.baseUrl = location.path + '/odata'; | ||
// } | ||
getEntityUri(entityKey, _typeName) { | ||
//ToDo: Fix string based keys | ||
if (!parseInt(entityKey)) { | ||
return this.baseUrl + "/" + _typeName + "('" + entityKey + "')"; | ||
// ToDo: Fix string based keys | ||
if (!parseInt(entityKey, 10)) { | ||
return this.baseUrl + '/' + _typeName + "('" + entityKey + "')"; | ||
} | ||
return this.baseUrl + "/" + _typeName + "(" + entityKey + ")"; | ||
return this.baseUrl + '/' + _typeName + '(' + entityKey + ')'; | ||
} | ||
handleError(err, caught) { | ||
console.warn("OData error: ", err, caught); | ||
console.warn('OData error: ', err, caught); | ||
} | ||
@@ -57,4 +63,4 @@ ; | ||
} | ||
extractQueryResultDataWidhCount(res) { | ||
let r = new query_1.PagedResult(); | ||
extractQueryResultDataWithCount(res) { | ||
let pagedResult = new query_1.PagedResult(); | ||
if (res.status < 200 || res.status >= 300) { | ||
@@ -65,12 +71,12 @@ throw new Error('Bad response status: ' + res.status); | ||
let entities = body.value; | ||
r.data = entities; | ||
pagedResult.data = entities; | ||
try { | ||
let count = parseInt(body["@odata.count"]) || entities.length; | ||
r.count = count; | ||
let count = parseInt(body['@odata.count'], 10) || entities.length; | ||
pagedResult.count = count; | ||
} | ||
catch (error) { | ||
console.warn("Cannot determine OData entities count. Falling back to collection length..."); | ||
r.count = entities.length; | ||
console.warn('Cannot determine OData entities count. Falling back to collection length...'); | ||
pagedResult.count = entities.length; | ||
} | ||
return r; | ||
return pagedResult; | ||
} | ||
@@ -77,0 +83,0 @@ }; |
@@ -1,4 +0,4 @@ | ||
export { ODataService } from "./odata"; | ||
export { ODataConfiguration } from "./config"; | ||
export { ODataQuery, PagedResult } from "./query"; | ||
export { ODataServiceFactory } from "./odataservicefactory"; | ||
export { ODataService } from './odata'; | ||
export { ODataConfiguration } from './config'; | ||
export { ODataQuery, PagedResult } from './query'; | ||
export { ODataServiceFactory } from './odataservicefactory'; |
"use strict"; | ||
var odata_1 = require("./odata"); | ||
var odata_1 = require('./odata'); | ||
exports.ODataService = odata_1.ODataService; | ||
var config_1 = require("./config"); | ||
var config_1 = require('./config'); | ||
exports.ODataConfiguration = config_1.ODataConfiguration; | ||
var query_1 = require("./query"); | ||
var query_1 = require('./query'); | ||
exports.ODataQuery = query_1.ODataQuery; | ||
exports.PagedResult = query_1.PagedResult; | ||
var odataservicefactory_1 = require("./odataservicefactory"); | ||
var odataservicefactory_1 = require('./odataservicefactory'); | ||
exports.ODataServiceFactory = odataservicefactory_1.ODataServiceFactory; | ||
//# sourceMappingURL=index.js.map |
import { Http, Response } from '@angular/http'; | ||
import { Observable } from 'rxjs/rx'; | ||
import { ODataConfiguration } from "./config"; | ||
import { ODataQuery } from "./query"; | ||
import { GetOperation } from "./operation"; | ||
import { ODataConfiguration } from './config'; | ||
import { ODataQuery } from './query'; | ||
import { GetOperation } from './operation'; | ||
export declare class ODataService<T> { | ||
@@ -19,6 +19,6 @@ private _typeName; | ||
Query(): ODataQuery<T>; | ||
private extractData(res); | ||
protected getEntityUri(entityKey: string): string; | ||
protected handleResponse(entity: Observable<Response>): Observable<T>; | ||
private extractData(res); | ||
private escapeKey(); | ||
} |
"use strict"; | ||
const rx_1 = require('rxjs/rx'); | ||
const query_1 = require("./query"); | ||
const operation_1 = require("./operation"); | ||
const query_1 = require('./query'); | ||
const operation_1 = require('./operation'); | ||
class ODataService { | ||
@@ -19,7 +19,7 @@ constructor(_typeName, http, config) { | ||
let body = JSON.stringify(entity); | ||
return this.handleResponse(this.http.post(this.config.baseUrl + "/" + this.TypeName, body, this.config.postRequestOptions)); | ||
return this.handleResponse(this.http.post(this.config.baseUrl + '/' + this.TypeName, body, this.config.postRequestOptions)); | ||
} | ||
CustomAction(key, actionName, postdata) { | ||
let body = JSON.stringify(postdata); | ||
return this.handleResponse(this.http.post(this.getEntityUri(key) + "/" + actionName, body, this.config.requestOptions)); | ||
return this.handleResponse(this.http.post(this.getEntityUri(key) + '/' + actionName, body, this.config.requestOptions)); | ||
} | ||
@@ -40,10 +40,2 @@ Patch(entity, key) { | ||
} | ||
extractData(res) { | ||
if (res.status < 200 || res.status >= 300) { | ||
throw new Error('Bad response status: ' + res.status); | ||
} | ||
let body = res.json(); | ||
let entity = body; | ||
return entity || null; | ||
} | ||
getEntityUri(entityKey) { | ||
@@ -55,6 +47,15 @@ return this.config.getEntityUri(entityKey, this._typeName); | ||
.catch((err, caught) => { | ||
this.config.handleError && this.config.handleError(err, caught); | ||
if (this.config.handleError) | ||
this.config.handleError(err, caught); | ||
return rx_1.Observable.throw(err); | ||
}); | ||
} | ||
extractData(res) { | ||
if (res.status < 200 || res.status >= 300) { | ||
throw new Error('Bad response status: ' + res.status); | ||
} | ||
let body = res.json(); | ||
let entity = body; | ||
return entity || null; | ||
} | ||
escapeKey() { | ||
@@ -61,0 +62,0 @@ } |
import { Http } from '@angular/http'; | ||
import { ODataService } from "./odata"; | ||
import { ODataConfiguration } from "./config"; | ||
import { ODataService } from './odata'; | ||
import { ODataConfiguration } from './config'; | ||
export declare class ODataServiceFactory { | ||
@@ -5,0 +5,0 @@ private http; |
@@ -13,4 +13,4 @@ "use strict"; | ||
const http_1 = require('@angular/http'); | ||
const odata_1 = require("./odata"); | ||
const config_1 = require("./config"); | ||
const odata_1 = require('./odata'); | ||
const config_1 = require('./config'); | ||
let ODataServiceFactory = class ODataServiceFactory { | ||
@@ -17,0 +17,0 @@ constructor(http, config) { |
import { URLSearchParams, Http, Response, RequestOptions } from '@angular/http'; | ||
import { Observable } from 'rxjs/rx'; | ||
import { ODataConfiguration } from "./config"; | ||
import { ODataConfiguration } from './config'; | ||
export declare abstract class ODataOperation<T> { | ||
@@ -8,9 +8,8 @@ protected _typeName: string; | ||
protected http: Http; | ||
constructor(_typeName: string, config: ODataConfiguration, http: Http); | ||
private _expand; | ||
Expand(expand: string): this; | ||
private _select; | ||
Select(select: string): this; | ||
constructor(_typeName: string, config: ODataConfiguration, http: Http); | ||
Expand(expand: string | string[]): this; | ||
Select(select: string | string[]): this; | ||
protected getParams(): URLSearchParams; | ||
private extractData(res); | ||
protected handleResponse(entity: Observable<Response>): Observable<T>; | ||
@@ -20,2 +19,4 @@ protected getEntityUri(entityKey: string): string; | ||
abstract Exec(...args: any[]): Observable<any>; | ||
protected parseStringOrStringArray(input: string | string[]): string; | ||
private extractData(res); | ||
} | ||
@@ -22,0 +23,0 @@ export declare abstract class OperationWithKey<T> extends ODataOperation<T> { |
@@ -11,7 +11,7 @@ "use strict"; | ||
Expand(expand) { | ||
this._expand = expand; | ||
this._expand = this.parseStringOrStringArray(expand); | ||
return this; | ||
} | ||
Select(select) { | ||
this._select = select; | ||
this._select = this.parseStringOrStringArray(select); | ||
return this; | ||
@@ -21,18 +21,13 @@ } | ||
let params = new http_1.URLSearchParams(); | ||
this._select && params.set("$select", this._select); | ||
this._expand && params.set("$expand", this._expand); | ||
if (this._select && this._select.length > 0) | ||
params.set(this.config.keys.select, this._select); | ||
if (this._expand && this._expand.length > 0) | ||
params.set(this.config.keys.expand, this._expand); | ||
return params; | ||
} | ||
extractData(res) { | ||
if (res.status < 200 || res.status >= 300) { | ||
throw new Error('Bad response status: ' + res.status); | ||
} | ||
let body = res.json(); | ||
let entity = body; | ||
return entity || null; | ||
} | ||
handleResponse(entity) { | ||
return entity.map(this.extractData) | ||
.catch((err, caught) => { | ||
this.config.handleError && this.config.handleError(err, caught); | ||
if (this.config.handleError) | ||
this.config.handleError(err, caught); | ||
return rx_1.Observable.throw(err); | ||
@@ -49,2 +44,16 @@ }); | ||
} | ||
parseStringOrStringArray(input) { | ||
if (input instanceof Array) { | ||
return input.join(','); | ||
} | ||
return input; | ||
} | ||
extractData(res) { | ||
if (res.status < 200 || res.status >= 300) { | ||
throw new Error('Bad response status: ' + res.status); | ||
} | ||
let body = res.json(); | ||
let entity = body; | ||
return entity || null; | ||
} | ||
} | ||
@@ -51,0 +60,0 @@ exports.ODataOperation = ODataOperation; |
import { Http } from '@angular/http'; | ||
import { Observable } from 'rxjs/rx'; | ||
import { ODataConfiguration } from "./config"; | ||
import { ODataOperation } from "./operation"; | ||
import { ODataConfiguration } from './config'; | ||
import { ODataOperation } from './operation'; | ||
export declare class PagedResult<T> { | ||
@@ -10,16 +10,17 @@ data: T[]; | ||
export declare class ODataQuery<T> extends ODataOperation<T> { | ||
private _filter; | ||
private _top; | ||
private _skip; | ||
private _orderBy; | ||
constructor(_typeName: string, config: ODataConfiguration, http: Http); | ||
private _filter; | ||
Filter(filter: string): ODataQuery<T>; | ||
private _top; | ||
Top(top: number): ODataQuery<T>; | ||
private _skip; | ||
Skip(skip: number): ODataQuery<T>; | ||
_orderBy: string; | ||
OrderBy(orderBy: string): ODataQuery<T>; | ||
private getQueryParams(); | ||
Exec(): Observable<Array<T>>; | ||
ExecWithCount(): Observable<PagedResult<T>>; | ||
private buildResourceURL(); | ||
private getQueryParams(); | ||
private extractArrayData(res, config); | ||
private extractArrayDataWithCount(res, config); | ||
} |
"use strict"; | ||
const rx_1 = require('rxjs/rx'); | ||
const operation_1 = require("./operation"); | ||
const operation_1 = require('./operation'); | ||
class PagedResult { | ||
@@ -29,17 +29,10 @@ } | ||
} | ||
getQueryParams() { | ||
let params = super.getParams(); | ||
this._filter && params.set(this.config.Keys.Filter, this._filter); | ||
this._top && params.set(this.config.Keys.Top, this._top.toString()); | ||
this._skip && params.set(this.config.Keys.Skip, this._skip.toString()); | ||
this._orderBy && params.set(this.config.Keys.OrderBy, this._orderBy); | ||
return params; | ||
} | ||
Exec() { | ||
let params = this.getQueryParams(); | ||
let config = this.config; | ||
return this.http.get(this.config.baseUrl + "/" + this._typeName + "/", { search: params }) | ||
return this.http.get(this.buildResourceURL(), { search: params }) | ||
.map(res => this.extractArrayData(res, config)) | ||
.catch((err, caught) => { | ||
this.config.handleError && this.config.handleError(err, caught); | ||
if (this.config.handleError) | ||
this.config.handleError(err, caught); | ||
return rx_1.Observable.throw(err); | ||
@@ -50,11 +43,27 @@ }); | ||
let params = this.getQueryParams(); | ||
params.set("$count", 'true'); // OData v4 only | ||
params.set('$count', 'true'); // OData v4 only | ||
let config = this.config; | ||
return this.http.get(this.config.baseUrl + "/" + this._typeName + "/", { search: params }) | ||
return this.http.get(this.buildResourceURL(), { search: params }) | ||
.map(res => this.extractArrayDataWithCount(res, config)) | ||
.catch((err, caught) => { | ||
this.config.handleError && this.config.handleError(err, caught); | ||
if (this.config.handleError) | ||
this.config.handleError(err, caught); | ||
return rx_1.Observable.throw(err); | ||
}); | ||
} | ||
buildResourceURL() { | ||
return this.config.baseUrl + '/' + this._typeName + '/'; | ||
} | ||
getQueryParams() { | ||
let params = super.getParams(); | ||
if (this._filter) | ||
params.set(this.config.keys.filter, this._filter); | ||
if (this._top) | ||
params.set(this.config.keys.top, this._top.toString()); | ||
if (this._skip) | ||
params.set(this.config.keys.skip, this._skip.toString()); | ||
if (this._orderBy) | ||
params.set(this.config.keys.orderBy, this._orderBy); | ||
return params; | ||
} | ||
extractArrayData(res, config) { | ||
@@ -64,3 +73,3 @@ return config.extractQueryResultData(res); | ||
extractArrayDataWithCount(res, config) { | ||
return config.extractQueryResultDataWidhCount(res); | ||
return config.extractQueryResultDataWithCount(res); | ||
} | ||
@@ -67,0 +76,0 @@ } |
import { Injectable } from '@angular/core'; | ||
import { RequestOptions, Headers, Response } from '@angular/http'; | ||
import { PagedResult } from "./query"; | ||
import { PagedResult } from './query'; | ||
// import { Location } from '@angular/common'; | ||
export class KeyConfigs { | ||
public Filter: string = "$filter"; | ||
public Top: string = "$top"; | ||
public Skip: string = "$skip"; | ||
public OrderBy: string = "$orderby"; | ||
public filter: string = '$filter'; | ||
public top: string = '$top'; | ||
public skip: string = '$skip'; | ||
public orderBy: string = '$orderby'; | ||
public select: string = '$select'; | ||
public expand: string = '$expand'; | ||
} | ||
@@ -14,17 +17,20 @@ | ||
export class ODataConfiguration { | ||
baseUrl: string = window.location.origin + "/odata"; | ||
public keys: KeyConfigs = new KeyConfigs(); | ||
public baseUrl: string = 'http://localhost/odata'; | ||
// constructor(location?: Location) { | ||
// this.baseUrl = location.path + '/odata'; | ||
// } | ||
public getEntityUri(entityKey: string, _typeName: string) { | ||
//ToDo: Fix string based keys | ||
if (!parseInt(entityKey)) { | ||
return this.baseUrl + "/" + _typeName + "('" + entityKey + "')"; | ||
// ToDo: Fix string based keys | ||
if (!parseInt(entityKey, 10)) { | ||
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); | ||
console.warn('OData error: ', err, caught); | ||
}; | ||
@@ -50,4 +56,4 @@ | ||
public extractQueryResultDataWidhCount<T>(res: Response): PagedResult<T> { | ||
let r = new PagedResult<T>(); | ||
public extractQueryResultDataWithCount<T>(res: Response): PagedResult<T> { | ||
let pagedResult = new PagedResult<T>(); | ||
@@ -60,14 +66,14 @@ if (res.status < 200 || res.status >= 300) { | ||
r.data = entities; | ||
pagedResult.data = entities; | ||
try { | ||
let count = parseInt(body["@odata.count"]) || entities.length; | ||
r.count = count; | ||
let count = parseInt(body['@odata.count'], 10) || entities.length; | ||
pagedResult.count = count; | ||
} catch (error) { | ||
console.warn("Cannot determine OData entities count. Falling back to collection length..."); | ||
r.count = entities.length; | ||
console.warn('Cannot determine OData entities count. Falling back to collection length...'); | ||
pagedResult.count = entities.length; | ||
} | ||
return r; | ||
return pagedResult; | ||
} | ||
} | ||
} |
@@ -1,4 +0,4 @@ | ||
export {ODataService} from "./odata"; | ||
export {ODataConfiguration} from "./config"; | ||
export {ODataQuery, PagedResult} from "./query"; | ||
export {ODataServiceFactory} from "./odataservicefactory"; | ||
export {ODataService} from './odata'; | ||
export {ODataConfiguration} from './config'; | ||
export {ODataQuery, PagedResult} from './query'; | ||
export {ODataServiceFactory} from './odataservicefactory'; |
83
odata.ts
import { URLSearchParams, Http, Response, Headers, RequestOptions } from '@angular/http'; | ||
import { Observable, Operator } from 'rxjs/rx'; | ||
import { ODataConfiguration } from "./config"; | ||
import { ODataQuery } from "./query"; | ||
import { GetOperation } from "./operation"; | ||
import { ODataConfiguration } from './config'; | ||
import { ODataQuery } from './query'; | ||
import { GetOperation } from './operation'; | ||
export class ODataService<T> { | ||
constructor(private _typeName:string, private http:Http, private config:ODataConfiguration) { } | ||
constructor(private _typeName: string, private http: Http, private config: ODataConfiguration) { } | ||
public get TypeName(){ | ||
return this._typeName; | ||
} | ||
public Get(key:string):GetOperation<T>{ | ||
public Get(key: string): GetOperation<T> { | ||
return new GetOperation<T>(this._typeName, this.config, this.http, key); | ||
} | ||
public Post(entity:T):Observable<T>{ | ||
public Post(entity: T): Observable<T> { | ||
let body = JSON.stringify(entity); | ||
return this.handleResponse(this.http.post(this.config.baseUrl + "/"+this.TypeName,body,this.config.postRequestOptions)); | ||
return this.handleResponse(this.http.post(this.config.baseUrl + '/' + this.TypeName, body, this.config.postRequestOptions)); | ||
} | ||
public CustomAction(key:string, actionName:string, postdata:any){ | ||
public CustomAction(key: string, actionName: string, postdata: any) { | ||
let body = JSON.stringify(postdata); | ||
return this.handleResponse(this.http.post(this.getEntityUri(key)+"/"+actionName,body, this.config.requestOptions)); | ||
return this.handleResponse(this.http.post(this.getEntityUri(key) + '/' + actionName, body, this.config.requestOptions)); | ||
} | ||
public Patch(entity:any, key:string):Observable<Response>{ | ||
public Patch(entity: any, key: string): Observable<Response> { | ||
let body = JSON.stringify(entity); | ||
return this.http.patch(this.getEntityUri(key),body,this.config.postRequestOptions); | ||
return this.http.patch(this.getEntityUri(key), body, this.config.postRequestOptions); | ||
} | ||
public Put(entity:T, key:string):Observable<T>{ | ||
public Put(entity: T, key: string): Observable<T> { | ||
let body = JSON.stringify(entity); | ||
return this.handleResponse(this.http.put(this.getEntityUri(key),body,this.config.postRequestOptions)); | ||
return this.handleResponse(this.http.put(this.getEntityUri(key), body, this.config.postRequestOptions)); | ||
} | ||
public Delete(key:string):Observable<Response>{ | ||
return this.http.delete(this.getEntityUri(key),this.config.requestOptions); | ||
public Delete(key: string): Observable<Response> { | ||
return this.http.delete(this.getEntityUri(key), this.config.requestOptions); | ||
} | ||
public Query():ODataQuery<T>{ | ||
public Query(): ODataQuery<T> { | ||
return new ODataQuery<T>(this.TypeName, this.config, this.http); | ||
} | ||
private extractData(res: Response) : T{ | ||
protected getEntityUri(entityKey: string) { | ||
return this.config.getEntityUri(entityKey, this._typeName); | ||
} | ||
protected handleResponse(entity: Observable<Response>) { | ||
return entity.map(this.extractData) | ||
.catch((err: any, caught: Observable<T>) => { | ||
if (this.config.handleError) this.config.handleError(err, caught); | ||
return Observable.throw(err); | ||
}); | ||
} | ||
private extractData(res: Response): T { | ||
if (res.status < 200 || res.status >= 300) { | ||
@@ -52,22 +64,9 @@ throw new Error('Bad response status: ' + res.status); | ||
let body = res.json(); | ||
let entity:T = body; | ||
let entity: T = body; | ||
return entity || null; | ||
} | ||
protected getEntityUri(entityKey:string){ | ||
return this.config.getEntityUri(entityKey, this._typeName); | ||
private escapeKey() { | ||
} | ||
protected handleResponse(entity:Observable<Response>){ | ||
return entity.map(this.extractData) | ||
.catch((err:any,caught:Observable<T>)=>{ | ||
this.config.handleError && this.config.handleError(err,caught); | ||
return Observable.throw(err); | ||
}); | ||
} | ||
private escapeKey(){ | ||
} | ||
} |
@@ -1,19 +0,19 @@ | ||
import { Injectable, Inject } from '@angular/core'; | ||
import { Injectable } from '@angular/core'; | ||
import { Http } from '@angular/http'; | ||
import { ODataService } from "./odata"; | ||
import { ODataConfiguration } from "./config"; | ||
import { ODataService } from './odata'; | ||
import { ODataConfiguration } from './config'; | ||
@Injectable() | ||
export class ODataServiceFactory{ | ||
constructor(private http:Http, private config : ODataConfiguration) { | ||
export class ODataServiceFactory { | ||
constructor(private http: Http, private config: ODataConfiguration) { | ||
} | ||
public CreateService<T>(typeName:string,handleError?:(err:any)=>any):ODataService<T>{ | ||
public CreateService<T>(typeName: string, handleError?: (err: any) => any): ODataService<T> { | ||
return new ODataService<T>(typeName, this.http, this.config); | ||
} | ||
public CreateServiceWithOptions<T>(typeName:string, config:ODataConfiguration):ODataService<T>{ | ||
return new ODataService<T>(typeName,this.http, config); | ||
public CreateServiceWithOptions<T>(typeName: string, config: ODataConfiguration): ODataService<T> { | ||
return new ODataService<T>(typeName, this.http, config); | ||
} | ||
} | ||
} |
121
operation.ts
import { Injectable } from '@angular/core'; | ||
import { URLSearchParams, Http, Response, RequestOptions } from '@angular/http'; | ||
import { Observable, Operator } from 'rxjs/rx'; | ||
import { ODataConfiguration } from "./config"; | ||
import { ODataConfiguration } from './config'; | ||
export abstract class ODataOperation<T>{ | ||
constructor(protected _typeName:string, | ||
protected config:ODataConfiguration, | ||
protected http:Http) { } | ||
export abstract class ODataOperation<T> { | ||
private _expand: string; | ||
public Expand(expand:string){ | ||
this._expand = expand; | ||
private _select: string; | ||
constructor(protected _typeName: string, | ||
protected config: ODataConfiguration, | ||
protected http: Http) { } | ||
public Expand(expand: string | string[]) { | ||
this._expand = this.parseStringOrStringArray(expand); | ||
return this; | ||
} | ||
private _select: string; | ||
public Select(select:string){ | ||
this._select = select; | ||
public Select(select: string | string[]) { | ||
this._select = this.parseStringOrStringArray(select); | ||
return this; | ||
} | ||
protected getParams():URLSearchParams{ | ||
protected getParams(): URLSearchParams { | ||
let params = new URLSearchParams(); | ||
this._select && params.set("$select", this._select); | ||
this._expand && params.set("$expand", this._expand); | ||
if (this._select && this._select.length > 0) params.set(this.config.keys.select, this._select); | ||
if (this._expand && this._expand.length > 0) params.set(this.config.keys.expand, this._expand); | ||
return params; | ||
} | ||
private extractData(res: Response):T{ | ||
if (res.status < 200 || res.status >= 300) { | ||
throw new Error('Bad response status: ' + res.status); | ||
} | ||
let body = res.json(); | ||
let entity:T = body; | ||
return entity || null; | ||
} | ||
protected handleResponse(entity:Observable<Response>){ | ||
protected handleResponse(entity: Observable<Response>) { | ||
return entity.map(this.extractData) | ||
.catch((err:any,caught:Observable<T>)=>{ | ||
this.config.handleError && this.config.handleError(err,caught); | ||
.catch((err: any, caught: Observable<T>) => { | ||
if (this.config.handleError) this.config.handleError(err, caught); | ||
return Observable.throw(err); | ||
}); | ||
} | ||
protected getEntityUri(entityKey:string){ | ||
protected getEntityUri(entityKey: string) { | ||
return this.config.getEntityUri(entityKey, this._typeName); | ||
} | ||
protected getRequestOptions():RequestOptions{ | ||
protected getRequestOptions(): RequestOptions { | ||
let options = this.config.requestOptions; | ||
@@ -59,42 +48,58 @@ options.search = this.getParams(); | ||
} | ||
abstract Exec(...args):Observable<any>; | ||
abstract Exec(...args): Observable<any>; | ||
protected parseStringOrStringArray(input: string | string[]): string { | ||
if (input instanceof Array) { | ||
return input.join(','); | ||
} | ||
return input as string; | ||
} | ||
private extractData(res: Response): T { | ||
if (res.status < 200 || res.status >= 300) { | ||
throw new Error('Bad response status: ' + res.status); | ||
} | ||
let body = res.json(); | ||
let entity: T = body; | ||
return entity || null; | ||
} | ||
} | ||
export abstract class OperationWithKey<T> extends ODataOperation<T> { | ||
constructor(protected _typeName:string, | ||
protected config:ODataConfiguration, | ||
protected http:Http, | ||
protected key:string) { | ||
constructor(protected _typeName: string, | ||
protected config: ODataConfiguration, | ||
protected http: Http, | ||
protected key: string) { | ||
super(_typeName, config, http); | ||
} | ||
abstract Exec(...args):Observable<any>; | ||
abstract Exec(...args): Observable<any>; | ||
} | ||
export abstract class OperationWithEntity<T> extends ODataOperation<T> { | ||
constructor(protected _typeName:string, | ||
protected config:ODataConfiguration, | ||
protected http:Http, | ||
protected entity:T) { | ||
constructor(protected _typeName: string, | ||
protected config: ODataConfiguration, | ||
protected http: Http, | ||
protected entity: T) { | ||
super(_typeName, config, http); | ||
} | ||
abstract Exec(...args):Observable<any>; | ||
abstract Exec(...args): Observable<any>; | ||
} | ||
export abstract class OperationWithKeyAndEntity<T> extends ODataOperation<T> { | ||
constructor(protected _typeName:string, | ||
protected config:ODataConfiguration, | ||
protected http:Http, | ||
protected key:string, | ||
protected entity:T) { | ||
constructor(protected _typeName: string, | ||
protected config: ODataConfiguration, | ||
protected http: Http, | ||
protected key: string, | ||
protected entity: T) { | ||
super(_typeName, config, http); | ||
} | ||
abstract Exec(...args):Observable<any>; | ||
abstract Exec(...args): Observable<any>; | ||
} | ||
export class GetOperation<T> extends OperationWithKey<T>{ | ||
public Exec():Observable<T> { | ||
export class GetOperation<T> extends OperationWithKey<T> { | ||
public Exec(): Observable<T> { | ||
return super.handleResponse(this.http.get(this.getEntityUri(this.key), this.getRequestOptions())); | ||
@@ -101,0 +106,0 @@ } |
{ | ||
"name": "angular2-odata", | ||
"version": "0.0.18", | ||
"version": "0.0.19", | ||
"description": "OData service for Angular2", | ||
@@ -18,2 +18,3 @@ "main": "./build/index.js", | ||
"angular", | ||
"angular2", | ||
"rest", | ||
@@ -31,11 +32,14 @@ "api", | ||
"devDependencies": { | ||
"@angular/common": "2.0.0-rc.5", | ||
"@angular/compiler": "2.0.0-rc.5", | ||
"@angular/core": "2.0.0-rc.5", | ||
"@angular/http": "2.0.0-rc.5", | ||
"@angular/platform-browser": "2.0.0-rc.5", | ||
"@angular/common": "2.0.0-rc.6", | ||
"@angular/compiler": "2.0.0-rc.6", | ||
"@angular/core": "2.0.0-rc.6", | ||
"@angular/http": "2.0.0-rc.6", | ||
"@angular/platform-browser": "2.0.0-rc.6", | ||
"codelyzer": "~0.0.21", | ||
"rxjs": "^5.0.0-beta.11", | ||
"tslint": "^3.7.1", | ||
"tslint-loader": "^2.1.3", | ||
"typescript": "^1.8.10", | ||
"zone.js": "^0.6.15" | ||
} | ||
} | ||
} |
85
query.ts
import { Injectable } from '@angular/core'; | ||
import { URLSearchParams, Http, Response } from '@angular/http'; | ||
import { Observable, Operator, Subject } from 'rxjs/rx'; | ||
import { ODataConfiguration } from "./config"; | ||
import { ODataOperation } from "./operation"; | ||
import { ODataConfiguration } from './config'; | ||
import { ODataOperation } from './operation'; | ||
@@ -12,7 +12,12 @@ export class PagedResult<T>{ | ||
export class ODataQuery<T> extends ODataOperation<T>{ | ||
constructor(_typeName:string, config:ODataConfiguration, http:Http) { | ||
export class ODataQuery<T> extends ODataOperation<T> { | ||
private _filter: string; | ||
private _top: number; | ||
private _skip: number; | ||
private _orderBy: string; | ||
constructor(_typeName: string, config: ODataConfiguration, http: Http) { | ||
super(_typeName, config, http); | ||
} | ||
private _filter: string; | ||
public Filter(filter: string): ODataQuery<T> { | ||
@@ -22,37 +27,25 @@ this._filter = filter; | ||
}; | ||
private _top:number; | ||
public Top(top: number):ODataQuery<T>{ | ||
public Top(top: number): ODataQuery<T> { | ||
this._top = top; | ||
return this; | ||
}; | ||
private _skip:number; | ||
public Skip(skip:number):ODataQuery<T>{ | ||
public Skip(skip: number): ODataQuery<T> { | ||
this._skip = skip; | ||
return this; | ||
} | ||
_orderBy: string; | ||
public OrderBy(orderBy:string):ODataQuery<T>{ | ||
public OrderBy(orderBy: string): ODataQuery<T> { | ||
this._orderBy = orderBy; | ||
return this; | ||
} | ||
private getQueryParams():URLSearchParams{ | ||
let params = super.getParams(); | ||
this._filter && params.set(this.config.Keys.Filter, this._filter); | ||
this._top && params.set(this.config.Keys.Top, this._top.toString()); | ||
this._skip && params.set(this.config.Keys.Skip, this._skip.toString()); | ||
this._orderBy && params.set(this.config.Keys.OrderBy, this._orderBy); | ||
return params; | ||
} | ||
public Exec():Observable<Array<T>>{ | ||
public Exec(): Observable<Array<T>> { | ||
let params = this.getQueryParams(); | ||
let config = this.config; | ||
return this.http.get(this.config.baseUrl + "/"+this._typeName+"/", {search: params}) | ||
.map(res=>this.extractArrayData(res,config)) | ||
.catch((err:any,caught:Observable<Array<T>>)=>{ | ||
this.config.handleError && this.config.handleError(err,caught); | ||
return this.http.get(this.buildResourceURL(), {search: params}) | ||
.map(res => this.extractArrayData(res, config)) | ||
.catch((err: any, caught: Observable<Array<T>>) => { | ||
if (this.config.handleError) this.config.handleError(err, caught); | ||
return Observable.throw(err); | ||
@@ -62,23 +55,35 @@ }); | ||
public ExecWithCount():Observable<PagedResult<T>>{ | ||
public ExecWithCount(): Observable<PagedResult<T>> { | ||
let params = this.getQueryParams(); | ||
params.set("$count",'true'); // OData v4 only | ||
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 this.http.get(this.buildResourceURL(), {search: params}) | ||
.map(res => this.extractArrayDataWithCount(res, config)) | ||
.catch((err: any, caught: Observable<PagedResult<T>>) => { | ||
if (this.config.handleError) this.config.handleError(err, caught); | ||
return Observable.throw(err); | ||
}); | ||
} | ||
private buildResourceURL(): string { | ||
return this.config.baseUrl + '/' + this._typeName + '/'; | ||
} | ||
private extractArrayData(res: Response, config:ODataConfiguration):Array<T> { | ||
private getQueryParams(): URLSearchParams { | ||
let params = super.getParams(); | ||
if (this._filter) params.set(this.config.keys.filter, this._filter); | ||
if (this._top) params.set(this.config.keys.top, this._top.toString()); | ||
if (this._skip) params.set(this.config.keys.skip, this._skip.toString()); | ||
if (this._orderBy) params.set(this.config.keys.orderBy, this._orderBy); | ||
return params; | ||
} | ||
private extractArrayData(res: Response, config: ODataConfiguration): Array<T> { | ||
return config.extractQueryResultData<T>(res); | ||
} | ||
private extractArrayDataWithCount(res:Response, config: ODataConfiguration):PagedResult<T>{ | ||
return config.extractQueryResultDataWidhCount<T>(res); | ||
private extractArrayDataWithCount(res: Response, config: ODataConfiguration): PagedResult<T> { | ||
return config.extractQueryResultDataWithCount<T>(res); | ||
} | ||
} | ||
} |
@@ -14,4 +14,7 @@ { | ||
}, | ||
"files": [ | ||
"index.ts" | ||
"exclude": [ | ||
".vscode", | ||
".git", | ||
"build", | ||
"node_modules" | ||
], | ||
@@ -18,0 +21,0 @@ "compileOnSave": false, |
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
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
70175
29.74%47
34.29%1319
44.63%11
37.5%1
Infinity%