-
-
Save OMGZui/8b3143d3c58dacbefc8ac36b4fea0025 to your computer and use it in GitHub Desktop.
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
interface Iupload { | |
(file: Buffer): Promise<DataUploadType> | |
} | |
export let upload: Iupload | |
upload = (file: Buffer) => { | |
const header = randomHeader() | |
return new Promise((resolve, reject) => { | |
const req = Https.request(header, res => { | |
res.on('data', data => { | |
try { | |
const resp = JSON.parse(data.toString()) as DataUploadType | |
if (resp.error) { | |
reject(resp) | |
} else { | |
resolve(resp) | |
} | |
} catch (err) { | |
reject(err) | |
} | |
}) | |
}) | |
req.write(file) | |
req.on('error', err => reject(err)) | |
req.end() | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment