parent
6cecc7bec7
commit
d2b3b4b9e7
13 changed files with 207 additions and 118 deletions
|
@ -1,16 +1,35 @@
|
|||
import axios from 'axios';
|
||||
import {printLog} from '../store';
|
||||
import {delay} from '../utils/delay';
|
||||
|
||||
export const ua = `Mozilla/5.0 MisskeyTools +https://github.com/Xeltica/MisskeyTools Node/${process.version}`;
|
||||
export const ua = `Mozilla/5.0 MisskeyTools +https://github.com/shrimpia/misskey-tools Node/${process.version}`;
|
||||
|
||||
const RETRY_COUNT = 5;
|
||||
|
||||
/**
|
||||
* Misskey APIを呼び出す
|
||||
*/
|
||||
export const api = <T = Record<string, unknown>>(host: string, endpoint: string, arg: Record<string, unknown>, i?: string): Promise<T> => {
|
||||
export const api = async <T = Record<string, unknown>>(host: string, endpoint: string, arg: Record<string, unknown>, token?: string): Promise<T> => {
|
||||
const a = { ...arg };
|
||||
if (i) {
|
||||
a.i = i;
|
||||
if (token) {
|
||||
a.i = token;
|
||||
}
|
||||
return axios.post<T>(`https://${host}/api/${endpoint}`, a).then(res => res.data);
|
||||
|
||||
for (let i = 0; i < RETRY_COUNT; i++) {
|
||||
let data: T;
|
||||
try {
|
||||
data = await axios.post<T>(`https://${host}/api/${endpoint}`, a).then(res => res.data);
|
||||
} catch (e) {
|
||||
printLog(`接続エラー: ${host}/api/${endpoint} リトライ中 (${i + 1} / ${RETRY_COUNT})\n${e}`, 'error');
|
||||
await delay(3000);
|
||||
continue;
|
||||
}
|
||||
if (!('error' in data)) {
|
||||
return data;
|
||||
}
|
||||
throw new MisskeyError((data as any).error);
|
||||
}
|
||||
throw new TimedOutError();
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -27,3 +46,18 @@ export const apiAvailable = async (host: string, i: string): Promise<boolean> =>
|
|||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
export class TimedOutError extends Error {}
|
||||
|
||||
export class MisskeyError extends Error {
|
||||
constructor(public error: MisskeyErrorObject) {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
||||
export interface MisskeyErrorObject {
|
||||
message: string;
|
||||
code: string;
|
||||
id: string;
|
||||
kind: string;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue