feat: переработать кастомизацию HttpClient
- добавлен плоский ApiConfig с lifecycle hooks - добавлены ApiError, retry context, timeout и кастомные parser/serializer - обновлены примеры, документация и тесты под новый API
This commit is contained in:
24
example.ts
24
example.ts
@@ -8,25 +8,23 @@ import { getProfile } from './output/operations/get-profile';
|
||||
import { login } from './output/operations/login';
|
||||
import { register } from './output/operations/register';
|
||||
|
||||
type SecurityData = {
|
||||
token: string;
|
||||
};
|
||||
let accessToken: string | null = null;
|
||||
|
||||
const httpClient = new HttpClient<SecurityData>({
|
||||
const httpClient = new HttpClient({
|
||||
baseUrl: 'https://api.example.com',
|
||||
baseApiParams: {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
securityWorker: (securityData) => {
|
||||
if (!securityData?.token) {
|
||||
return undefined;
|
||||
onRequest: (params) => {
|
||||
if (!params.secure || !accessToken) {
|
||||
return params;
|
||||
}
|
||||
|
||||
return {
|
||||
...params,
|
||||
headers: {
|
||||
Authorization: `Bearer ${securityData.token}`,
|
||||
...params.headers,
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
},
|
||||
};
|
||||
},
|
||||
@@ -58,7 +56,7 @@ async function loginUser() {
|
||||
password: 'SecurePassword123',
|
||||
});
|
||||
|
||||
httpClient.setSecurityData({ token: result.access_token });
|
||||
accessToken = result.access_token;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user