@pushwoosh/http-client 1.1.2 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -10
- package/dist/http-client.errors.d.ts +68 -31
- package/dist/http-client.helpers.d.ts +2 -0
- package/dist/http-client.types.d.ts +44 -4
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +6 -8
package/README.md
CHANGED
|
@@ -115,12 +115,9 @@ try {
|
|
|
115
115
|
`withCredentials` - передавать идентификационные данные в кросс доменных запросах
|
|
116
116
|
|
|
117
117
|
`response` - ответ от сервера:
|
|
118
|
+
|
|
118
119
|
```typescript
|
|
119
|
-
type Response<R extends Result> =
|
|
120
|
-
readonly result: R;
|
|
121
|
-
readonly status: number;
|
|
122
|
-
readonly message: string;
|
|
123
|
-
};
|
|
120
|
+
type Response<R extends Result> = | ResponseFailure | ResponseSuccess<R>;
|
|
124
121
|
```
|
|
125
122
|
|
|
126
123
|
#### GET, POST, PUT, UPDATE, DELETE запросы
|
|
@@ -139,7 +136,7 @@ try {
|
|
|
139
136
|
withCredentials: false,
|
|
140
137
|
responseMiddleware: (response: Response): { some: string } => { // если стандартный middleware для разбора ответа от сервера не подходит
|
|
141
138
|
const jsonResponse = httpClient.baseResponseMiddleware(response);
|
|
142
|
-
const result = httpClient.baseResponseMiddleware(
|
|
139
|
+
const result = httpClient.baseResponseMiddleware(jsonResponse);
|
|
143
140
|
return result;
|
|
144
141
|
},
|
|
145
142
|
});
|
|
@@ -210,7 +207,3 @@ try {
|
|
|
210
207
|
`ServiceUnavailableError (extends HttpClientError)` - Класс описывающий ошибку "Сервис недоступен"
|
|
211
208
|
|
|
212
209
|
`GatewayTimeoutError (extends HttpClientError)` - Класс описывающий ошибку "Вышло время ожидания ответа"
|
|
213
|
-
|
|
214
|
-
#### Ошибки связанные с валидацией ответа:
|
|
215
|
-
|
|
216
|
-
`ValidationError (extends HttpClientError)` - Класс описывающий ошибку валидации
|
|
@@ -1,78 +1,85 @@
|
|
|
1
|
+
import { ResponseDetails } from './http-client.types';
|
|
1
2
|
/**
|
|
2
3
|
* Класс описывающий ошибку связанную с модулем HttpClient.
|
|
3
4
|
*/
|
|
4
5
|
export declare class HttpClientError extends Error {
|
|
5
|
-
readonly name: string;
|
|
6
6
|
/**
|
|
7
7
|
* Статус ошибки.
|
|
8
8
|
*/
|
|
9
9
|
readonly status: number;
|
|
10
|
-
|
|
11
|
-
* Детали ошибки.
|
|
12
|
-
*/
|
|
13
|
-
readonly details: string | null;
|
|
14
|
-
constructor(message: string, status: number, details: string | null);
|
|
10
|
+
constructor(message: string, status: number);
|
|
15
11
|
}
|
|
16
12
|
/**
|
|
17
13
|
* Класс описывающий внутреннюю ошибку связанную с модулем HttpClient.
|
|
18
14
|
*/
|
|
19
15
|
export declare class InternalError extends HttpClientError {
|
|
20
|
-
|
|
21
|
-
constructor(message: string, details: string | null);
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Класс описывающий неизвестную ошибку.
|
|
25
|
-
*/
|
|
26
|
-
export declare class UnknownError extends HttpClientError {
|
|
27
|
-
constructor(code: number, details: string);
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Класс описывающий ошибку валидации ответа от сервера.
|
|
31
|
-
*/
|
|
32
|
-
export declare class ValidationError extends HttpClientError {
|
|
33
|
-
constructor(details: string);
|
|
16
|
+
constructor(message: string);
|
|
34
17
|
}
|
|
35
18
|
/**
|
|
36
19
|
* Класс описывающий ошибку "Плохой запрос".
|
|
37
20
|
* Это означает, что сервер не понимает запрос из-за неверного синтаксиса.
|
|
38
21
|
*/
|
|
39
22
|
export declare class BadRequestError extends HttpClientError {
|
|
40
|
-
|
|
23
|
+
/**
|
|
24
|
+
* Детали запроса.
|
|
25
|
+
*/
|
|
26
|
+
readonly details?: ResponseDetails;
|
|
27
|
+
constructor(message: string, details?: ResponseDetails);
|
|
41
28
|
}
|
|
42
29
|
/**
|
|
43
30
|
* Класс описывающий ошибку "Не авторизовано".
|
|
44
31
|
* Это означает. что для получения запрашиваемого ответа нужна аутентификация.
|
|
45
32
|
*/
|
|
46
33
|
export declare class UnauthorizedError extends HttpClientError {
|
|
47
|
-
|
|
34
|
+
/**
|
|
35
|
+
* Детали запроса.
|
|
36
|
+
*/
|
|
37
|
+
readonly details?: ResponseDetails;
|
|
38
|
+
constructor(message: string, details?: ResponseDetails);
|
|
48
39
|
}
|
|
49
40
|
/**
|
|
50
41
|
* Класс описывающий ошибку "Запрещено".
|
|
51
42
|
* Это означает, что у клиента нет прав доступа к содержимому.
|
|
52
43
|
*/
|
|
53
44
|
export declare class ForbiddenError extends HttpClientError {
|
|
54
|
-
|
|
45
|
+
/**
|
|
46
|
+
* Детали запроса.
|
|
47
|
+
*/
|
|
48
|
+
readonly details?: ResponseDetails;
|
|
49
|
+
constructor(message: string, details?: ResponseDetails);
|
|
55
50
|
}
|
|
56
51
|
/**
|
|
57
52
|
* Класс описывающий ошибку "Не найден".
|
|
58
53
|
* Это означает, что сервер не может найти запрашиваемый ресурс.
|
|
59
54
|
*/
|
|
60
55
|
export declare class NotFoundError extends HttpClientError {
|
|
61
|
-
|
|
56
|
+
/**
|
|
57
|
+
* Детали запроса.
|
|
58
|
+
*/
|
|
59
|
+
readonly details?: ResponseDetails;
|
|
60
|
+
constructor(message: string, details?: ResponseDetails);
|
|
62
61
|
}
|
|
63
62
|
/**
|
|
64
63
|
* Класс описывающий ошибку "Метод не доступен".
|
|
65
64
|
* Это означает, что серверу известен запрос, но он был отключен и не может быть использован.
|
|
66
65
|
*/
|
|
67
66
|
export declare class MethodNotAllowedError extends HttpClientError {
|
|
68
|
-
|
|
67
|
+
/**
|
|
68
|
+
* Детали запроса.
|
|
69
|
+
*/
|
|
70
|
+
readonly details?: ResponseDetails;
|
|
71
|
+
constructor(message: string, details?: ResponseDetails);
|
|
69
72
|
}
|
|
70
73
|
/**
|
|
71
74
|
* Класс описывающий ошибку "Медиа формат не поддерживается сервером".
|
|
72
75
|
* Это означает, что сервер не поддерживает медиа формат запрашиваемых данных.
|
|
73
76
|
*/
|
|
74
77
|
export declare class UnsupportedMediaTypeError extends HttpClientError {
|
|
75
|
-
|
|
78
|
+
/**
|
|
79
|
+
* Детали запроса.
|
|
80
|
+
*/
|
|
81
|
+
readonly details?: ResponseDetails;
|
|
82
|
+
constructor(message: string, details?: ResponseDetails);
|
|
76
83
|
}
|
|
77
84
|
/**
|
|
78
85
|
* Класс описывающий ошибку "Сервер не может обработать запрос".
|
|
@@ -80,33 +87,63 @@ export declare class UnsupportedMediaTypeError extends HttpClientError {
|
|
|
80
87
|
* и синтаксис запроса является корректным, но серверу не удалось обработать инструкции содержимого.
|
|
81
88
|
*/
|
|
82
89
|
export declare class UnprocessableEntityError extends HttpClientError {
|
|
83
|
-
|
|
90
|
+
/**
|
|
91
|
+
* Детали запроса.
|
|
92
|
+
*/
|
|
93
|
+
readonly details?: ResponseDetails;
|
|
94
|
+
constructor(message: string, details?: ResponseDetails);
|
|
84
95
|
}
|
|
85
96
|
/**
|
|
86
97
|
* Класс описывающий ошибку "Внутренняя ошибка сервера".
|
|
87
98
|
* Это означает, что сервер столкнулся с ситуацией, которую он не знает как обработать.
|
|
88
99
|
*/
|
|
89
100
|
export declare class InternalServerError extends HttpClientError {
|
|
90
|
-
|
|
101
|
+
/**
|
|
102
|
+
* Детали запроса.
|
|
103
|
+
*/
|
|
104
|
+
readonly details?: ResponseDetails;
|
|
105
|
+
constructor(message: string, details?: ResponseDetails);
|
|
91
106
|
}
|
|
92
107
|
/**
|
|
93
108
|
* Класс описывающий ошибку "Плохой шлюз".
|
|
94
109
|
* Это означает, что сервер действует как шлюз и получил недопустимый ответ.
|
|
95
110
|
*/
|
|
96
111
|
export declare class BadGatewayError extends HttpClientError {
|
|
97
|
-
|
|
112
|
+
/**
|
|
113
|
+
* Детали запроса.
|
|
114
|
+
*/
|
|
115
|
+
readonly details?: ResponseDetails;
|
|
116
|
+
constructor(message: string, details?: ResponseDetails);
|
|
98
117
|
}
|
|
99
118
|
/**
|
|
100
119
|
* Класс описывающий ошибку "Сервис недоступен".
|
|
101
120
|
* Это означает, что сервер не готов обрабатывать запрос.
|
|
102
121
|
*/
|
|
103
122
|
export declare class ServiceUnavailableError extends HttpClientError {
|
|
104
|
-
|
|
123
|
+
/**
|
|
124
|
+
* Детали запроса.
|
|
125
|
+
*/
|
|
126
|
+
readonly details?: ResponseDetails;
|
|
127
|
+
constructor(message: string, details?: ResponseDetails);
|
|
105
128
|
}
|
|
106
129
|
/**
|
|
107
130
|
* Класс описывающий ошибку "Вышло время ожидания ответа".
|
|
108
131
|
* Это означает, что сервер действует как шлюз и не может получить ответ вовремя.
|
|
109
132
|
*/
|
|
110
133
|
export declare class GatewayTimeoutError extends HttpClientError {
|
|
111
|
-
|
|
134
|
+
/**
|
|
135
|
+
* Детали запроса.
|
|
136
|
+
*/
|
|
137
|
+
readonly details?: ResponseDetails;
|
|
138
|
+
constructor(message: string, details?: ResponseDetails);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Класс описывающий неизвестную ошибку пришедшую с сервера.
|
|
142
|
+
*/
|
|
143
|
+
export declare class UnknownServerError extends HttpClientError {
|
|
144
|
+
/**
|
|
145
|
+
* Детали запроса.
|
|
146
|
+
*/
|
|
147
|
+
readonly details?: ResponseDetails;
|
|
148
|
+
constructor(message: string, code: number, details?: ResponseDetails);
|
|
112
149
|
}
|
|
@@ -7,7 +7,7 @@ export declare type Config = {
|
|
|
7
7
|
readonly token: string;
|
|
8
8
|
readonly logout: string;
|
|
9
9
|
};
|
|
10
|
-
cookie: Omit<CookieAttributes, 'domain'> & {
|
|
10
|
+
readonly cookie: Omit<CookieAttributes, 'domain'> & {
|
|
11
11
|
readonly domain: string;
|
|
12
12
|
};
|
|
13
13
|
readonly errorMiddleware?: (error: unknown) => Promise<never> | never;
|
|
@@ -18,18 +18,58 @@ export declare type PutMethod = 'PUT';
|
|
|
18
18
|
export declare type PatchMethod = 'PATCH';
|
|
19
19
|
export declare type DeleteMethod = 'DELETE';
|
|
20
20
|
export declare type Method = GetMethod | PutMethod | PostMethod | PatchMethod | DeleteMethod;
|
|
21
|
+
export declare type Headers = Record<string, string>;
|
|
21
22
|
export declare type Params = Record<string, string | number> | never;
|
|
22
23
|
export declare type Query = Record<string, Primitives> | URLSearchParams | string | never;
|
|
23
24
|
export declare type Body = Record<string, unknown> | FormData | never;
|
|
24
25
|
export declare type Result = unknown | void;
|
|
25
|
-
export declare
|
|
26
|
+
export declare enum ResponseSuccessStatus {
|
|
27
|
+
OK = 200,
|
|
28
|
+
CREATED = 201,
|
|
29
|
+
ACCEPT = 202,
|
|
30
|
+
NON_CONTENT = 203
|
|
31
|
+
}
|
|
32
|
+
export declare type ResponseDetails = {
|
|
33
|
+
readonly url: string;
|
|
34
|
+
readonly method: Method;
|
|
35
|
+
readonly headers: Headers | undefined;
|
|
36
|
+
readonly params: any;
|
|
37
|
+
readonly query: any;
|
|
38
|
+
readonly body: any;
|
|
39
|
+
readonly withCredentials: boolean | undefined;
|
|
40
|
+
readonly withAuthorization: boolean | undefined;
|
|
41
|
+
};
|
|
42
|
+
export declare type ResponseSuccess<R extends Result> = {
|
|
26
43
|
readonly result: R;
|
|
27
|
-
readonly status:
|
|
44
|
+
readonly status: ResponseSuccessStatus;
|
|
28
45
|
readonly message: string;
|
|
46
|
+
readonly details?: ResponseDetails;
|
|
47
|
+
};
|
|
48
|
+
export declare enum ResponseFailureStatus {
|
|
49
|
+
BAD_REQUEST = 400,
|
|
50
|
+
UNAUTHORIZED = 401,
|
|
51
|
+
FORBIDDEN = 403,
|
|
52
|
+
NOT_FOUND = 404,
|
|
53
|
+
METHOD_NOT_ALLOWED = 405,
|
|
54
|
+
UNSUPPORTED_MEDIA_TYPE = 415,
|
|
55
|
+
UNPROCESSABLE_ENTITY = 422,
|
|
56
|
+
INTERNAL_SERVER_ERROR = 500,
|
|
57
|
+
BAD_GATEWAY = 502,
|
|
58
|
+
SERVICE_UNAVAILABLE = 503,
|
|
59
|
+
GATEWAY_TIMEOUT = 504
|
|
60
|
+
}
|
|
61
|
+
export declare type ResponseFailure = {
|
|
62
|
+
readonly result?: {
|
|
63
|
+
readonly message?: string;
|
|
64
|
+
};
|
|
65
|
+
readonly status: number;
|
|
66
|
+
readonly message?: string;
|
|
67
|
+
readonly details: ResponseDetails;
|
|
29
68
|
};
|
|
69
|
+
export declare type Response<R extends Result> = ResponseFailure | ResponseSuccess<R>;
|
|
30
70
|
declare type BaseRequestOptions<M extends Method> = {
|
|
31
71
|
readonly method: M;
|
|
32
|
-
readonly headers?:
|
|
72
|
+
readonly headers?: Headers;
|
|
33
73
|
readonly withAuthorization: boolean;
|
|
34
74
|
readonly withCredentials?: boolean;
|
|
35
75
|
};
|
package/dist/index.d.ts
CHANGED