Skip to content

Commit

Permalink
add dynamic choose request client logic for weapp
Browse files Browse the repository at this point in the history
  • Loading branch information
8ggmaker committed May 22, 2017
1 parent c515f43 commit 5ddf87c
Show file tree
Hide file tree
Showing 3 changed files with 2,176 additions and 26 deletions.
51 changes: 28 additions & 23 deletions src/Http/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,38 @@ export class HttpClient implements IHttpClient{

private send(method:string,url:string,headers?:Map<string,string>,data?:string):Promise<string>{
return new Promise<string>((resolve,reject)=>{
let xhr = new XMLHttpRequest();

xhr.open(method,url,true);

if(headers){
headers.forEach((val,key)=>xhr.setRequestHeader(key,val));
}
xhr.send(data)

xhr.onload = ()=>{
if (xhr.status >= 200 && xhr.status < 300) {
resolve(xhr.response);
if(XMLHttpRequest){
let xhr = new XMLHttpRequest();
xhr.open(method,url,true);
if(headers){
headers.forEach((val,key)=>xhr.setRequestHeader(key,val));
}
else {
reject({
xhr.send(data)

xhr.onload = ()=>{
if (xhr.status >= 200 && xhr.status < 300) {
resolve(xhr.response);
}
else {
reject({
status: xhr.status,
statusText: xhr.statusText
});
}
};
xhr.onerror = ()=>{
reject({
status: xhr.status,
statusText: xhr.statusText
});
}
};

xhr.onerror = ()=>{
reject({
status: xhr.status,
statusText: xhr.statusText
});
};
};
}else{
wx.request({data:data,header:headers,method:method,url:url,fail:()=>{reject();},success:(res)=>{
if(res){
resolve(JSON.stringify(res.data));
}
}});
}
});
}

Expand Down
8 changes: 5 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"files":[
"src/*.ts"
"src/*.ts",
"src/Http/*.ts",
"typings/*.ts"
],
"compilerOptions": {
/* Basic Options */
Expand All @@ -23,7 +25,7 @@

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */
"noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
Expand All @@ -39,7 +41,7 @@
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
"typeRoots": ["./typings"] /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */

Expand Down
Loading

0 comments on commit 5ddf87c

Please sign in to comment.