forked from HunterXuan/wx-tfjs-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
04011e5
commit 92003dd
Showing
5 changed files
with
2,855 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/// <reference path="./wx.d.ts"> | ||
function generateResponse(res:any) { | ||
let header = res.header || {}; | ||
return { | ||
ok: ((res.statusCode / 200) | 0) === 1, // 200-299 | ||
status: res.statusCode, | ||
statusText: res.errMsg, | ||
url: '', | ||
clone: () => { | ||
return generateResponse(res); | ||
}, | ||
text: () => Promise.resolve(String.fromCharCode.apply(null, new Uint8Array(res.data))), | ||
json: () => { | ||
var json = {}; | ||
try { | ||
json = JSON.parse(String.fromCharCode.apply(null, new Uint8Array(res.data))); | ||
} | ||
catch (err) { | ||
console.error(err); | ||
} | ||
return Promise.resolve(json); | ||
}, | ||
arrayBuffer: () => Promise.resolve(res.data), | ||
blob: () => Promise.resolve(res.data), | ||
headers: { | ||
keys: () => Object.keys(header), | ||
entries: () => { | ||
let all = []; | ||
for (let key in header) { | ||
if (header.hasOwnProperty(key)) { | ||
all.push([key, header[key]]); | ||
} | ||
} | ||
return all; | ||
}, | ||
get: (n:string) => header[n.toLowerCase()], | ||
has: (n:string) => n.toLowerCase() in header | ||
} | ||
}; | ||
} | ||
|
||
async function fetch(input:any, init:any): Promise<any> { | ||
return new Promise(function(resolve, reject) { | ||
wx.request({ | ||
url: input, | ||
header: init.headers || {}, | ||
data: init.body || {}, | ||
method: init.method || 'GET', | ||
dataType: '', | ||
responseType: 'arraybuffer', | ||
success: function(res:any) { | ||
resolve(generateResponse(res)) | ||
}, | ||
fail: function(res:any) { | ||
reject(generateResponse(res)) | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
export { | ||
fetch, | ||
}; |
Oops, something went wrong.