mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-24 22:23:10 +02:00
update api api
This commit is contained in:
parent
3557741f13
commit
bdb592539e
3 changed files with 55 additions and 53 deletions
12
README.md
12
README.md
|
@ -18,21 +18,13 @@ coming soon
|
||||||
todo
|
todo
|
||||||
|
|
||||||
## API request
|
## API request
|
||||||
都度インスタンスやトークンを指定する場合
|
|
||||||
``` ts
|
|
||||||
import * as Misskey from 'misskey-js';
|
|
||||||
|
|
||||||
const meta = await Misskey.api.request('https://misskey.test', 'meta', { detail: true }, 'TOKEN');
|
|
||||||
```
|
|
||||||
|
|
||||||
最初にインスタンスやトークンを指定し、以後のリクエストでその情報を使いまわす場合
|
|
||||||
``` ts
|
``` ts
|
||||||
import * as Misskey from 'misskey-js';
|
import * as Misskey from 'misskey-js';
|
||||||
|
|
||||||
const cli = new Misskey.api.APIClient({
|
const cli = new Misskey.api.APIClient({
|
||||||
origin: 'https://misskey.test'
|
origin: 'https://misskey.test',
|
||||||
|
credential: 'TOKEN',
|
||||||
});
|
});
|
||||||
cli.i = { token: 'TOKEN' };
|
|
||||||
|
|
||||||
const meta = await cli.request('meta', { detail: true });
|
const meta = await cli.request('meta', { detail: true });
|
||||||
```
|
```
|
||||||
|
|
43
src/api.ts
43
src/api.ts
|
@ -14,18 +14,31 @@ export function isAPIError(reason: any): reason is APIError {
|
||||||
return reason[MK_API_ERROR] === true;
|
return reason[MK_API_ERROR] === true;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function request<E extends keyof Endpoints>(
|
export class APIClient {
|
||||||
origin: string,
|
public origin: string;
|
||||||
endpoint: E,
|
public credential: string | null | undefined;
|
||||||
data: Endpoints[E]['req'] = {},
|
public fetch: (typeof fetch);
|
||||||
credential: string | null | undefined,
|
|
||||||
|
constructor(opts: {
|
||||||
|
origin: APIClient['origin'];
|
||||||
|
credential?: APIClient['credential'];
|
||||||
|
fetch?: APIClient['fetch'] | null | undefined;
|
||||||
|
}) {
|
||||||
|
this.origin = opts.origin;
|
||||||
|
this.credential = opts.credential;
|
||||||
|
this.fetch = opts.fetch || fetch;
|
||||||
|
}
|
||||||
|
|
||||||
|
public request<E extends keyof Endpoints>(
|
||||||
|
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) => {
|
const promise = new Promise<Endpoints[E]['res']>((resolve, reject) => {
|
||||||
// Append a credential
|
// Append a credential
|
||||||
if (credential !== undefined) (data as Record<string, any>).i = credential;
|
if (this.credential) (data as Record<string, any>).i = this.credential;
|
||||||
|
if (credential) (data as Record<string, any>).i = credential;
|
||||||
|
|
||||||
// Send request
|
// Send request
|
||||||
fetch(`${origin}/api/${endpoint}`, {
|
this.fetch(`${this.origin}/api/${endpoint}`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify(data),
|
body: JSON.stringify(data),
|
||||||
credentials: 'omit',
|
credentials: 'omit',
|
||||||
|
@ -48,20 +61,4 @@ export function request<E extends keyof Endpoints>(
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
23
test/api.ts
23
test/api.ts
|
@ -1,4 +1,4 @@
|
||||||
import { request } from '../src/api';
|
import { APIClient } from '../src/api';
|
||||||
import { enableFetchMocks } from 'jest-fetch-mock';
|
import { enableFetchMocks } from 'jest-fetch-mock';
|
||||||
|
|
||||||
enableFetchMocks();
|
enableFetchMocks();
|
||||||
|
@ -21,16 +21,22 @@ describe('API', () => {
|
||||||
fetchMock.mockResponse(async (req) => {
|
fetchMock.mockResponse(async (req) => {
|
||||||
const body = await req.json();
|
const body = await req.json();
|
||||||
if (req.method == 'POST' && req.url == 'https://misskey.test/api/i') {
|
if (req.method == 'POST' && req.url == 'https://misskey.test/api/i') {
|
||||||
if (typeof body.i != 'string') {
|
if (body.i === 'TOKEN') {
|
||||||
|
return JSON.stringify({ id: 'foo' });
|
||||||
|
} else {
|
||||||
return { status: 400 };
|
return { status: 400 };
|
||||||
}
|
}
|
||||||
return JSON.stringify({ id: 'foo' });
|
|
||||||
} else {
|
} else {
|
||||||
return { status: 404 };
|
return { status: 404 };
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const res = await request('https://misskey.test', 'i', {}, 'TOKEN');
|
const cli = new APIClient({
|
||||||
|
origin: 'https://misskey.test',
|
||||||
|
credential: 'TOKEN',
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await cli.request('i');
|
||||||
|
|
||||||
// validate response
|
// validate response
|
||||||
expect(res).toEqual({
|
expect(res).toEqual({
|
||||||
|
@ -60,11 +66,18 @@ describe('API', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await request('https://misskey.test', 'i', {}, 'TOKEN');
|
const cli = new APIClient({
|
||||||
|
origin: 'https://misskey.test',
|
||||||
|
credential: 'TOKEN',
|
||||||
|
});
|
||||||
|
|
||||||
|
await cli.request('i');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
expect(e.id).toEqual('5d37dbcb-891e-41ca-a3d6-e690c97775ac');
|
expect(e.id).toEqual('5d37dbcb-891e-41ca-a3d6-e690c97775ac');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO: ネットワークエラーのテスト
|
// TODO: ネットワークエラーのテスト
|
||||||
|
|
||||||
|
// TODO: JSON以外が返ってきた場合のハンドリング
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue