Skip to content

Commit

Permalink
Auto Parse Json Response
Browse files Browse the repository at this point in the history
  • Loading branch information
ricklove committed Jan 30, 2017
1 parent 12c9ab1 commit ad15fda
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
8 changes: 7 additions & 1 deletion lib/src/browser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/src/browser.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions lib/src/platform.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ export declare type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD';
export declare type HttpHeaders = {
[key: string]: string;
};
export interface HttpClientResponse {
data: any;
export interface HttpClientResponse<T> {
dataRaw: any;
data: T;
headers: HttpHeaders;
}
export interface HttpClient {
request(url: string, method?: HttpMethod, data?: any, headers?: HttpHeaders, withCredentials?: boolean): Promise<HttpClientResponse>;
request<T>(url: string, method?: HttpMethod, data?: any, headers?: HttpHeaders, withCredentials?: boolean): Promise<HttpClientResponse<T>>;
resolveUrl(url: string): string;
}
15 changes: 12 additions & 3 deletions src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class BrowserPlatformProvider implements P.PlatformProvider {
}

class BrowserHttpClient implements P.HttpClient {
async request(url: string, method?: P.HttpMethod, data?: any, headers?: P.HttpHeaders, withCredentials = false): Promise<P.HttpClientResponse> {
return new Promise<P.HttpClientResponse>((resolve, reject) => {
async request<T>(url: string, method?: P.HttpMethod, data?: any, headers?: P.HttpHeaders, withCredentials = false): Promise<P.HttpClientResponse<T>> {
return new Promise<P.HttpClientResponse<T>>((resolve, reject) => {
method = method || 'GET';

if (typeof data === 'object' && data.constructor === Object) {
Expand All @@ -40,7 +40,16 @@ class BrowserHttpClient implements P.HttpClient {
let headersList = response.getAllResponseHeaders().split('\n').map(x => x.trim().split('='));
let headers: P.HttpHeaders = {};
headersList.forEach(x => headers[x[0]] = x[1]);
resolve({ data: data, headers: headers });

let dataObj = null;

try {
dataObj = JSON.parse(data) as T;
} catch (err) {

}

resolve({ dataRaw: data, data: dataObj, headers: headers });
},
error: err => reject(err)
});
Expand Down
7 changes: 4 additions & 3 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ export abstract class Platform {
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD';
export type HttpHeaders = { [key: string]: string };

export interface HttpClientResponse {
data: any;
export interface HttpClientResponse<T> {
dataRaw: any;
data: T;
headers: HttpHeaders;
}

export interface HttpClient {
request(url: string, method?: HttpMethod, data?: any, headers?: HttpHeaders, withCredentials?: boolean): Promise<HttpClientResponse>;
request<T>(url: string, method?: HttpMethod, data?: any, headers?: HttpHeaders, withCredentials?: boolean): Promise<HttpClientResponse<T>>;
resolveUrl(url: string): string;
}

0 comments on commit ad15fda

Please sign in to comment.