Revert "Feat: AiScriptでリモートサーバーのAPIを叩く関数を追加 (#11887)" (MisskeyIO#281)
* Revert "Feat: AiScriptでリモートサーバーのAPIを叩く関数を追加 (#11887)"
This reverts commit f269841a83
.
This commit is contained in:
parent
c31f27e327
commit
3c79605460
|
@ -5,8 +5,8 @@
|
||||||
|
|
||||||
// TODO: なんでもかんでもos.tsに突っ込むのやめたいのでよしなに分割する
|
// TODO: なんでもかんでもos.tsに突っ込むのやめたいのでよしなに分割する
|
||||||
|
|
||||||
import { pendingApiRequestsCount, api, apiExternal, apiGet } from '@/scripts/api.js';
|
import { pendingApiRequestsCount, api, apiGet } from '@/scripts/api.js';
|
||||||
export { pendingApiRequestsCount, api, apiExternal, apiGet };
|
export { pendingApiRequestsCount, api, apiGet };
|
||||||
import { Component, markRaw, Ref, ref, defineAsyncComponent } from 'vue';
|
import { Component, markRaw, Ref, ref, defineAsyncComponent } from 'vue';
|
||||||
import { EventEmitter } from 'eventemitter3';
|
import { EventEmitter } from 'eventemitter3';
|
||||||
import insertTextAtCursor from 'insert-text-at-cursor';
|
import insertTextAtCursor from 'insert-text-at-cursor';
|
||||||
|
|
|
@ -50,16 +50,6 @@ export function createAiScriptEnv(opts) {
|
||||||
return values.ERROR('request_failed', utils.jsToVal(err));
|
return values.ERROR('request_failed', utils.jsToVal(err));
|
||||||
});
|
});
|
||||||
}),
|
}),
|
||||||
'Mk:apiExternal': values.FN_NATIVE(async ([host, ep, param, token]) => {
|
|
||||||
utils.assertString(host);
|
|
||||||
utils.assertString(ep);
|
|
||||||
if (token) utils.assertString(token);
|
|
||||||
return os.apiExternal(host.value, ep.value, utils.valToJs(param), token?.value).then(res => {
|
|
||||||
return utils.jsToVal(res);
|
|
||||||
}, err => {
|
|
||||||
return values.ERROR('request_failed', utils.jsToVal(err));
|
|
||||||
});
|
|
||||||
}),
|
|
||||||
'Mk:save': values.FN_NATIVE(([key, value]) => {
|
'Mk:save': values.FN_NATIVE(([key, value]) => {
|
||||||
utils.assertString(key);
|
utils.assertString(key);
|
||||||
miLocalStorage.setItem(`aiscript:${opts.storageKey}:${key.value}`, JSON.stringify(utils.valToJs(value)));
|
miLocalStorage.setItem(`aiscript:${opts.storageKey}:${key.value}`, JSON.stringify(utils.valToJs(value)));
|
||||||
|
|
|
@ -51,49 +51,6 @@ export function api<E extends keyof Misskey.Endpoints, P extends Misskey.Endpoin
|
||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function apiExternal<E extends keyof Misskey.Endpoints, P extends Misskey.Endpoints[E]['req']>(hostUrl: string, endpoint: E, data: P = {} as any, token?: string | null | undefined, signal?: AbortSignal): Promise<Misskey.Endpoints[E]['res']> {
|
|
||||||
if (!/^https?:\/\//.test(hostUrl)) throw new Error('invalid host name');
|
|
||||||
if (endpoint.includes('://')) throw new Error('invalid endpoint');
|
|
||||||
pendingApiRequestsCount.value++;
|
|
||||||
|
|
||||||
const onFinally = () => {
|
|
||||||
pendingApiRequestsCount.value--;
|
|
||||||
};
|
|
||||||
|
|
||||||
const promise = new Promise<Misskey.Endpoints[E]['res'] | void>((resolve, reject) => {
|
|
||||||
// Append a credential
|
|
||||||
(data as any).i = token;
|
|
||||||
|
|
||||||
const fullUrl = (hostUrl.slice(-1) === '/' ? hostUrl.slice(0, -1) : hostUrl)
|
|
||||||
+ '/api/' + (endpoint.slice(0, 1) === '/' ? endpoint.slice(1) : endpoint);
|
|
||||||
// Send request
|
|
||||||
window.fetch(fullUrl, {
|
|
||||||
method: 'POST',
|
|
||||||
body: JSON.stringify(data),
|
|
||||||
credentials: 'omit',
|
|
||||||
cache: 'no-cache',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
signal,
|
|
||||||
}).then(async (res) => {
|
|
||||||
const body = res.status === 204 ? null : await res.json();
|
|
||||||
|
|
||||||
if (res.status === 200) {
|
|
||||||
resolve(body);
|
|
||||||
} else if (res.status === 204) {
|
|
||||||
resolve();
|
|
||||||
} else {
|
|
||||||
reject(body.error);
|
|
||||||
}
|
|
||||||
}).catch(reject);
|
|
||||||
});
|
|
||||||
|
|
||||||
promise.then(onFinally, onFinally);
|
|
||||||
|
|
||||||
return promise;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Implements Misskey.api.ApiClient.request
|
// Implements Misskey.api.ApiClient.request
|
||||||
export function apiGet <E extends keyof Misskey.Endpoints, P extends Misskey.Endpoints[E]['req']>(endpoint: E, data: P = {} as any): Promise<Misskey.Endpoints[E]['res']> {
|
export function apiGet <E extends keyof Misskey.Endpoints, P extends Misskey.Endpoints[E]['req']>(endpoint: E, data: P = {} as any): Promise<Misskey.Endpoints[E]['res']> {
|
||||||
pendingApiRequestsCount.value++;
|
pendingApiRequestsCount.value++;
|
||||||
|
|
Loading…
Reference in New Issue