@pushwoosh/http-client 1.0.3 → 1.1.0

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.
@@ -1,21 +1,23 @@
1
- import { Body, Config, CookieOptions, DeleteOptions, GetOptions, Introspect, Method, Options, Params, PatchOptions, PostOptions, PutOptions, Query, Response, Result } from './http-client.types';
2
- export declare class HttpClient<I extends Introspect = Introspect> {
1
+ import { History } from 'history';
2
+ import { Body, Config, DeleteOptions, GetOptions, Introspect, Method, PatchOptions, PostOptions, PutOptions, Query, RequestOptions, Response } from './http-client.types';
3
+ export declare class HttpClient {
3
4
  /**
4
5
  * Имя куки в которой будет сохранена случайная строка для проверки ответа авторизации.
6
+ * Для более подробной информации: RFC 6749 https://www.rfc-editor.org/rfc/rfc6749.html#section-4.1.1.
5
7
  */
6
- private static STORIED_AUTHORIZATION_STATE_COOKIE_NAME;
8
+ static STORIED_AUTHORIZATION_STATE_COOKIE_NAME: string;
7
9
  /**
8
- * Имя куки в которой будет сохранен текущий адрес страницы.
10
+ * Имя куки в которой будет сохранен полный текущий адрес страницы.
9
11
  */
10
- private static STORIED_URL_COOKIE_NAME;
12
+ static STORIED_URL_COOKIE_NAME: string;
11
13
  /**
12
- * Параметры конфигурации.
14
+ * Библиотека для управления навигацией.
13
15
  */
14
- private readonly config;
16
+ private readonly history;
15
17
  /**
16
- * Параметры куки.
18
+ * Параметры конфигурации.
17
19
  */
18
- private readonly cookieOptions;
20
+ private readonly config;
19
21
  /**
20
22
  * Токен доступа.
21
23
  */
@@ -32,13 +34,13 @@ export declare class HttpClient<I extends Introspect = Introspect> {
32
34
  * Был ли установлен токен доступа вручную.
33
35
  */
34
36
  private isAccessTokenSetManual;
35
- constructor(config: Config, cookieOptions?: CookieOptions);
37
+ constructor(history: History, config: Config);
36
38
  /**
37
39
  * Разобрать ответ от сервера, и вернуть результат выполнения запроса.
38
40
  *
39
- * @param { Response<*> } response - ответ от сервера в формате Axios.
41
+ * @param { Response<*> } response - ответ от сервера.
40
42
  */
41
- static parseResponse<R extends Result>(response: Response<R>): R;
43
+ basicResponseMiddleware: <R extends unknown>(response: Response<R>) => R;
42
44
  /**
43
45
  * Проверить наличие авторизации.
44
46
  */
@@ -71,23 +73,23 @@ export declare class HttpClient<I extends Introspect = Introspect> {
71
73
  * @param { string } authorizationCode - код авторизации, полученный от сервера авторизации.
72
74
  * @param { string } authorizationState - уникальная строка, переданная клиентом серверу при авторизации, полученная от сервера авторизации.
73
75
  */
74
- initialize(authorizationCode: string | null, authorizationState: string | null): Promise<I | null>;
76
+ initialize: (authorizationCode: string | null, authorizationState: string | null) => Promise<Introspect | null>;
75
77
  /**
76
78
  * Начать сессию.
77
79
  */
78
- login(): Promise<void>;
80
+ login: () => Promise<void>;
79
81
  /**
80
82
  * Завершить сессию.
81
83
  */
82
- logout(): Promise<void>;
84
+ logout: () => Promise<void>;
83
85
  /**
84
86
  * Временный костыль, чтобы установить токен доступа из сессии.
85
87
  */
86
- setAccessToken(accessToken: string): void;
88
+ setAccessToken: (accessToken: string) => void;
87
89
  /**
88
90
  * Получить токен доступа, если время жизни токена закончилось, будет осуществлена попытка обновить токен.
89
91
  */
90
- getAccessToken(): Promise<string>;
92
+ getAccessToken: () => Promise<string>;
91
93
  /**
92
94
  * Выполнить запрос на сервер.
93
95
  *
@@ -101,7 +103,7 @@ export declare class HttpClient<I extends Introspect = Introspect> {
101
103
  * @param { string } url - адрес запроса.
102
104
  * @param { Options<Method, Params, Query, Body> } options - параметры запроса.
103
105
  */
104
- request<M extends Method, P extends Params, Q extends Query, B extends Body, R extends Result>(url: string, options: Options<M, P, Q, B>): Promise<Response<R>>;
106
+ request: <M extends Method, P extends Record<string, string | number>, Q extends Query, B extends Body, R extends unknown>(url: string, options: RequestOptions<M, P, Q, B>) => Promise<Response<R>>;
105
107
  /**
106
108
  * Выполнить GET запрос на сервер.
107
109
  *
@@ -113,7 +115,7 @@ export declare class HttpClient<I extends Introspect = Introspect> {
113
115
  * @param { string } url - адрес запроса.
114
116
  * @param { GetOptions<Params, Query> } options - параметры GET запроса.
115
117
  */
116
- get<P extends Params, Q extends Query, R extends Result>(url: string, options: GetOptions<P, Q>): Promise<R>;
118
+ get: <P extends Record<string, string | number>, Q extends Query, R extends unknown>(url: string, options: GetOptions<P, Q, R>) => Promise<R>;
117
119
  /**
118
120
  * Выполнить POST запрос на сервер.
119
121
  *
@@ -126,7 +128,7 @@ export declare class HttpClient<I extends Introspect = Introspect> {
126
128
  * @param { string } url - адрес запроса.
127
129
  * @param { PostOptions<Params, Query, Body> } options - параметры POST запроса.
128
130
  */
129
- post<P extends Params, Q extends Query, B extends Body, R extends Result>(url: string, options: PostOptions<P, Q, B>): Promise<R>;
131
+ post: <P extends Record<string, string | number>, Q extends Query, B extends Body, R extends unknown>(url: string, options: PostOptions<P, Q, B, R>) => Promise<R>;
130
132
  /**
131
133
  * Выполнить PUT запрос на сервер.
132
134
  *
@@ -139,7 +141,7 @@ export declare class HttpClient<I extends Introspect = Introspect> {
139
141
  * @param { string } url - адрес запроса.
140
142
  * @param { PutOptions<Params, Query, Body> } options - параметры PUT запроса.
141
143
  */
142
- put<P extends Params, Q extends Query, B extends Body, R extends Result>(url: string, options: PutOptions<P, Q, B>): Promise<R>;
144
+ put: <P extends Record<string, string | number>, Q extends Query, B extends Body, R extends unknown>(url: string, options: PutOptions<P, Q, B, R>) => Promise<R>;
143
145
  /**
144
146
  * Выполнить PATCH запрос на сервер.
145
147
  *
@@ -152,7 +154,7 @@ export declare class HttpClient<I extends Introspect = Introspect> {
152
154
  * @param { string } url - адрес запроса.
153
155
  * @param { PatchOptions<Params, Query, Body> } options - параметры PATCH запроса.
154
156
  */
155
- patch<P extends Params, Q extends Query, B extends Body, R extends Result>(url: string, options: PatchOptions<P, Q, B>): Promise<R>;
157
+ patch: <P extends Record<string, string | number>, Q extends Query, B extends Body, R extends unknown>(url: string, options: PatchOptions<P, Q, B, R>) => Promise<R>;
156
158
  /**
157
159
  * Выполнить DELETE запрос на сервер.
158
160
  *
@@ -165,5 +167,5 @@ export declare class HttpClient<I extends Introspect = Introspect> {
165
167
  * @param { string } url - адрес запроса.
166
168
  * @param { DeleteOptions<Params, Query, Body> } options - параметры DELETE запроса.
167
169
  */
168
- delete<P extends Params, Q extends Query, B extends Body, R extends Result>(url: string, options: DeleteOptions<P, Q, B>): Promise<R>;
170
+ delete: <P extends Record<string, string | number>, Q extends Query, B extends Body, R extends unknown>(url: string, options: DeleteOptions<P, Q, B, R>) => Promise<R>;
169
171
  }
@@ -26,6 +26,12 @@ export declare class InternalError extends HttpClientError {
26
26
  export declare class UnknownError extends HttpClientError {
27
27
  constructor(code: number, details: string);
28
28
  }
29
+ /**
30
+ * Класс описывающий ошибку валидации ответа от сервера.
31
+ */
32
+ export declare class ValidationError extends HttpClientError {
33
+ constructor(details: string);
34
+ }
29
35
  /**
30
36
  * Класс описывающий ошибку "Плохой запрос".
31
37
  * Это означает, что сервер не понимает запрос из-за неверного синтаксиса.
@@ -7,8 +7,11 @@ export declare type Config = {
7
7
  readonly token: string;
8
8
  readonly logout: string;
9
9
  };
10
+ cookie: Omit<CookieAttributes, 'domain'> & {
11
+ readonly domain: string;
12
+ };
13
+ readonly errorMiddleware?: (error: unknown) => Promise<never> | never;
10
14
  };
11
- export declare type CookieOptions = CookieAttributes;
12
15
  export declare type GetMethod = 'GET';
13
16
  export declare type PostMethod = 'POST';
14
17
  export declare type PutMethod = 'PUT';
@@ -19,13 +22,21 @@ export declare type Params = Record<string, string | number> | never;
19
22
  export declare type Query = Record<string, Primitives> | URLSearchParams | string | never;
20
23
  export declare type Body = Record<string, unknown> | FormData | never;
21
24
  export declare type Result = unknown | void;
25
+ export declare type Response<R extends Result> = {
26
+ readonly result: R;
27
+ readonly status: number;
28
+ readonly message: string;
29
+ };
22
30
  declare type BaseRequestOptions<M extends Method> = {
23
31
  readonly method: M;
24
32
  readonly headers?: Record<string, string>;
25
33
  readonly withAuthorization: boolean;
26
34
  readonly withCredentials?: boolean;
27
35
  };
28
- export declare type Options<M extends Method, P extends Params, Q extends Query, B extends Body> = M extends GetMethod ? [P] extends [never] ? [Q] extends [never] ? BaseRequestOptions<GetMethod> : BaseRequestOptions<GetMethod> & {
36
+ declare type SimpleRequestOptions<R extends Result> = {
37
+ readonly responseMiddleware?: (response: Response<any>) => R;
38
+ };
39
+ export declare type RequestOptions<M extends Method, P extends Params, Q extends Query, B extends Body> = M extends GetMethod ? [P] extends [never] ? [Q] extends [never] ? BaseRequestOptions<GetMethod> : BaseRequestOptions<GetMethod> & {
29
40
  query: Q;
30
41
  } : [Q] extends [never] ? BaseRequestOptions<GetMethod> & {
31
42
  params: P;
@@ -52,19 +63,15 @@ export declare type Options<M extends Method, P extends Params, Q extends Query,
52
63
  query: Q;
53
64
  body: B;
54
65
  };
55
- export declare type GetOptions<P extends Params, Q extends Query> = Omit<Options<GetMethod, P, Q, never>, 'method'>;
56
- export declare type PostOptions<P extends Params, Q extends Query, B extends Body> = Omit<Options<PostMethod, P, Q, B>, 'method'>;
57
- export declare type PutOptions<P extends Params, Q extends Query, B extends Body> = Omit<Options<PutMethod, P, Q, B>, 'method'>;
58
- export declare type PatchOptions<P extends Params, Q extends Query, B extends Body> = Omit<Options<PatchMethod, P, Q, B>, 'method'>;
59
- export declare type DeleteOptions<P extends Params, Q extends Query, B extends Body> = Omit<Options<DeleteMethod, P, Q, B>, 'method'>;
60
- export declare type Response<R extends Result> = {
61
- readonly result: R;
62
- readonly status: number;
63
- readonly message: string;
64
- };
66
+ export declare type GetOptions<P extends Params, Q extends Query, R extends Result> = Omit<RequestOptions<GetMethod, P, Q, never>, 'method'> & SimpleRequestOptions<R>;
67
+ export declare type PostOptions<P extends Params, Q extends Query, B extends Body, R extends Result> = Omit<RequestOptions<PostMethod, P, Q, B>, 'method'> & SimpleRequestOptions<R>;
68
+ export declare type PutOptions<P extends Params, Q extends Query, B extends Body, R extends Result> = Omit<RequestOptions<PutMethod, P, Q, B>, 'method'> & SimpleRequestOptions<R>;
69
+ export declare type PatchOptions<P extends Params, Q extends Query, B extends Body, R extends Result> = Omit<RequestOptions<PatchMethod, P, Q, B>, 'method'> & SimpleRequestOptions<R>;
70
+ export declare type DeleteOptions<P extends Params, Q extends Query, B extends Body, R extends Result> = Omit<RequestOptions<DeleteMethod, P, Q, B>, 'method'> & SimpleRequestOptions<R>;
65
71
  export declare type Introspect = {
66
72
  readonly user_id: string | number;
67
73
  readonly username: string | number;
74
+ readonly is_su?: boolean;
68
75
  readonly account_id: string | number;
69
76
  readonly expired_at: number;
70
77
  };