feat: добавить ручной runtime-клиент
- добавлен корневой runtime export HttpClient и createApiClient - настроена сборка JS и d.ts для публичного API пакета - добавлен тест ручной сборки operations через createApiClient - добавлена документация по ручному клиенту без OpenAPI - версия пакета поднята до 5.1.0
This commit is contained in:
97
tests/unit/manual-client.test.ts
Normal file
97
tests/unit/manual-client.test.ts
Normal file
@@ -0,0 +1,97 @@
|
||||
import { describe, expect, test } from 'bun:test';
|
||||
import {
|
||||
ContentType,
|
||||
HttpClient,
|
||||
createApiClient,
|
||||
type ApiRequestClient,
|
||||
type RequestParams,
|
||||
} from '../../src/index.js';
|
||||
|
||||
describe('Manual runtime client', () => {
|
||||
test('должен собирать ручные операции через createApiClient', async () => {
|
||||
type User = {
|
||||
id: string;
|
||||
email: string;
|
||||
};
|
||||
|
||||
type CreateUserQuery = {
|
||||
invite?: boolean;
|
||||
};
|
||||
|
||||
type CreateUserPayload = {
|
||||
email: string;
|
||||
};
|
||||
|
||||
const createUser = (
|
||||
http: ApiRequestClient,
|
||||
query: CreateUserQuery,
|
||||
body: CreateUserPayload,
|
||||
requestParams: RequestParams = {},
|
||||
) =>
|
||||
http.request<User>({
|
||||
path: '/users',
|
||||
method: 'POST',
|
||||
query,
|
||||
body,
|
||||
type: ContentType.Json,
|
||||
format: 'json',
|
||||
secure: true,
|
||||
...requestParams,
|
||||
});
|
||||
|
||||
let requestUrl = '';
|
||||
let requestInit: RequestInit | undefined;
|
||||
|
||||
const http = new HttpClient({
|
||||
baseUrl: 'https://api.example.com',
|
||||
customFetch: async (url, init) => {
|
||||
requestUrl = String(url);
|
||||
requestInit = init;
|
||||
|
||||
return Response.json({ id: '1', email: 'user@example.com' });
|
||||
},
|
||||
onRequest: (params) => {
|
||||
if (!params.secure) {
|
||||
return params;
|
||||
}
|
||||
|
||||
const headers = new Headers(params.headers);
|
||||
|
||||
if (!headers.has('Authorization')) {
|
||||
headers.set('Authorization', 'Bearer manual-token');
|
||||
}
|
||||
|
||||
return {
|
||||
...params,
|
||||
headers,
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
const api = createApiClient(http, {
|
||||
users: {
|
||||
create: createUser,
|
||||
},
|
||||
});
|
||||
|
||||
const user = await api.users.create(
|
||||
{ invite: true },
|
||||
{ email: 'user@example.com' },
|
||||
{
|
||||
headers: {
|
||||
'X-Request-Id': 'request-1',
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
const headers = new Headers(requestInit?.headers);
|
||||
|
||||
expect(user).toEqual({ id: '1', email: 'user@example.com' });
|
||||
expect(requestUrl).toBe('https://api.example.com/users?invite=true');
|
||||
expect(requestInit?.method).toBe('POST');
|
||||
expect(requestInit?.body).toBe(JSON.stringify({ email: 'user@example.com' }));
|
||||
expect(headers.get('Authorization')).toBe('Bearer manual-token');
|
||||
expect(headers.get('Content-Type')).toBe(ContentType.Json);
|
||||
expect(headers.get('X-Request-Id')).toBe('request-1');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user