mirror of
https://github.com/paricafe/misskey.git
synced 2024-11-25 16:36:44 -06:00
wip
This commit is contained in:
parent
92c170d540
commit
eacb5fea9f
2 changed files with 51 additions and 29 deletions
14
src/acct.ts
Normal file
14
src/acct.ts
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
export type Acct = {
|
||||||
|
username: string;
|
||||||
|
host: string | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function parse(acct: string): Acct {
|
||||||
|
if (acct.startsWith('@')) acct = acct.substr(1);
|
||||||
|
const split = acct.split('@', 2);
|
||||||
|
return { username: split[0], host: split[1] || null };
|
||||||
|
}
|
||||||
|
|
||||||
|
export function render(acct: Acct): string {
|
||||||
|
return acct.host == null ? acct.username : `${acct.username}@${acct.host}`;
|
||||||
|
}
|
66
src/api.ts
66
src/api.ts
|
@ -1,42 +1,50 @@
|
||||||
import { Endpoints } from './endpoints';
|
import { Endpoints } from './endpoints';
|
||||||
|
|
||||||
|
export function request<E extends keyof Endpoints>(
|
||||||
|
origin: string,
|
||||||
|
endpoint: E,
|
||||||
|
data: Endpoints[E]['req'] = {},
|
||||||
|
credential: string | null | undefined,
|
||||||
|
): Promise<Endpoints[E]['res']> {
|
||||||
|
const promise = new Promise<Endpoints[E]['res']>((resolve, reject) => {
|
||||||
|
// Append a credential
|
||||||
|
if (credential !== undefined) (data as Record<string, any>).i = credential;
|
||||||
|
|
||||||
|
// Send request
|
||||||
|
fetch(`${origin}/api/${endpoint}`, {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify(data),
|
||||||
|
credentials: 'omit',
|
||||||
|
cache: 'no-cache'
|
||||||
|
}).then(async (res) => {
|
||||||
|
const body = res.status === 204 ? null : await res.json();
|
||||||
|
|
||||||
|
if (res.status === 200) {
|
||||||
|
resolve(body);
|
||||||
|
} else if (res.status === 204) {
|
||||||
|
resolve(null);
|
||||||
|
} else {
|
||||||
|
reject(body.error);
|
||||||
|
}
|
||||||
|
}).catch(reject);
|
||||||
|
});
|
||||||
|
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
export class APIClient {
|
export class APIClient {
|
||||||
public i: { token: string; } | null = null;
|
public i: { token: string; } | null = null;
|
||||||
private apiUrl: string;
|
private origin: string;
|
||||||
|
|
||||||
constructor(opts: {
|
constructor(opts: {
|
||||||
apiUrl: APIClient['apiUrl'];
|
origin: APIClient['origin'];
|
||||||
}) {
|
}) {
|
||||||
this.apiUrl = opts.apiUrl;
|
this.origin = opts.origin;
|
||||||
}
|
}
|
||||||
|
|
||||||
public request<E extends keyof Endpoints>(
|
public request<E extends keyof Endpoints>(
|
||||||
endpoint: E, data: Endpoints[E]['req'] = {}, token?: string | null | undefined
|
endpoint: E, data: Endpoints[E]['req'] = {}, credential?: string | null | undefined,
|
||||||
): Promise<Endpoints[E]['res']> {
|
): Promise<Endpoints[E]['res']> {
|
||||||
const promise = new Promise<Endpoints[E]['res']>((resolve, reject) => {
|
return request(this.origin, endpoint, data, credential === undefined ? this.i?.token : credential);
|
||||||
// Append a credential
|
|
||||||
if (this.i) (data as Record<string, any>).i = this.i.token;
|
|
||||||
if (token !== undefined) (data as Record<string, any>).i = token;
|
|
||||||
|
|
||||||
// Send request
|
|
||||||
fetch(endpoint.indexOf('://') > -1 ? endpoint : `${this.apiUrl}/${endpoint}`, {
|
|
||||||
method: 'POST',
|
|
||||||
body: JSON.stringify(data),
|
|
||||||
credentials: 'omit',
|
|
||||||
cache: 'no-cache'
|
|
||||||
}).then(async (res) => {
|
|
||||||
const body = res.status === 204 ? null : await res.json();
|
|
||||||
|
|
||||||
if (res.status === 200) {
|
|
||||||
resolve(body);
|
|
||||||
} else if (res.status === 204) {
|
|
||||||
resolve(null);
|
|
||||||
} else {
|
|
||||||
reject(body.error);
|
|
||||||
}
|
|
||||||
}).catch(reject);
|
|
||||||
});
|
|
||||||
|
|
||||||
return promise;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue