Use node-fetch instead of request (#6228)

* requestをnode-fetchになど

* format

* fix error

* t

* Fix test
This commit is contained in:
MeiMei 2020-04-09 23:42:23 +09:00 committed by GitHub
parent bb7edfee04
commit d3c0f3c251
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 140 additions and 218 deletions

View file

@ -1,7 +1,7 @@
import * as fs from 'fs';
import * as WebSocket from 'ws';
const fetch = require('node-fetch');
import * as req from 'request';
import fetch from 'node-fetch';
const FormData = require('form-data');
import * as childProcess from 'child_process';
export const async = (fn: Function) => (done: Function) => {
@ -20,6 +20,9 @@ export const request = async (endpoint: string, params: any, me?: any): Promise<
try {
const res = await fetch('http://localhost:8080/api' + endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(Object.assign(auth, params))
});
@ -64,18 +67,23 @@ export const react = async (user: any, note: any, reaction: string): Promise<any
}, user);
};
export const uploadFile = (user: any, path?: string): Promise<any> => new Promise((ok, rej) => {
req.post({
url: 'http://localhost:8080/api/drive/files/create',
formData: {
i: user.token,
file: fs.createReadStream(path || __dirname + '/resources/Lenna.png')
},
json: true
}, (err, httpResponse, body) => {
ok(body);
});
});
export const uploadFile = (user: any, path?: string): Promise<any> => {
const formData = new FormData();
formData.append('i', user.token);
formData.append('file', fs.createReadStream(path || __dirname + '/resources/Lenna.png'));
return fetch('http://localhost:8080/api/drive/files/create', {
method: 'post',
body: formData,
timeout: 30 * 1000,
}).then(res => {
if (!res.ok) {
throw `${res.status} ${res.statusText}`;
} else {
return res.json();
}
});
};
export function connectStream(user: any, channel: string, listener: (message: Record<string, any>) => any, params?: any): Promise<WebSocket> {
return new Promise((res, rej) => {