wip
This commit is contained in:
parent
92c170d540
commit
eacb5fea9f
|
@ -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}`;
|
||||||
|
}
|
38
src/api.ts
38
src/api.ts
|
@ -1,25 +1,17 @@
|
||||||
import { Endpoints } from './endpoints';
|
import { Endpoints } from './endpoints';
|
||||||
|
|
||||||
export class APIClient {
|
export function request<E extends keyof Endpoints>(
|
||||||
public i: { token: string; } | null = null;
|
origin: string,
|
||||||
private apiUrl: string;
|
endpoint: E,
|
||||||
|
data: Endpoints[E]['req'] = {},
|
||||||
constructor(opts: {
|
credential: string | null | undefined,
|
||||||
apiUrl: APIClient['apiUrl'];
|
|
||||||
}) {
|
|
||||||
this.apiUrl = opts.apiUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
public request<E extends keyof Endpoints>(
|
|
||||||
endpoint: E, data: Endpoints[E]['req'] = {}, token?: string | null | undefined
|
|
||||||
): Promise<Endpoints[E]['res']> {
|
): Promise<Endpoints[E]['res']> {
|
||||||
const promise = new Promise<Endpoints[E]['res']>((resolve, reject) => {
|
const promise = new Promise<Endpoints[E]['res']>((resolve, reject) => {
|
||||||
// Append a credential
|
// Append a credential
|
||||||
if (this.i) (data as Record<string, any>).i = this.i.token;
|
if (credential !== undefined) (data as Record<string, any>).i = credential;
|
||||||
if (token !== undefined) (data as Record<string, any>).i = token;
|
|
||||||
|
|
||||||
// Send request
|
// Send request
|
||||||
fetch(endpoint.indexOf('://') > -1 ? endpoint : `${this.apiUrl}/${endpoint}`, {
|
fetch(`${origin}/api/${endpoint}`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify(data),
|
body: JSON.stringify(data),
|
||||||
credentials: 'omit',
|
credentials: 'omit',
|
||||||
|
@ -39,4 +31,20 @@ export class APIClient {
|
||||||
|
|
||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class APIClient {
|
||||||
|
public i: { token: string; } | null = null;
|
||||||
|
private origin: string;
|
||||||
|
|
||||||
|
constructor(opts: {
|
||||||
|
origin: APIClient['origin'];
|
||||||
|
}) {
|
||||||
|
this.origin = opts.origin;
|
||||||
|
}
|
||||||
|
|
||||||
|
public request<E extends keyof Endpoints>(
|
||||||
|
endpoint: E, data: Endpoints[E]['req'] = {}, credential?: string | null | undefined,
|
||||||
|
): Promise<Endpoints[E]['res']> {
|
||||||
|
return request(this.origin, endpoint, data, credential === undefined ? this.i?.token : credential);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue