@biorate/axios 3.3.0 → 3.3.2
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 +147 -5
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -1,23 +1,165 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @biorate/axios
|
|
2
2
|
|
|
3
|
-
Axios OOP static interface
|
|
3
|
+
Axios OOP static interface — extends `axios` with lifecycle hooks, stubbing, mock caching, and a class-based HTTP client pattern.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Class-based** — extend `Axios` and override hooks for per-service configuration.
|
|
8
|
+
- **Lifecycle hooks** — `before()`, `after()`, `catch()`, `finally()` for request/response interception.
|
|
9
|
+
- **Static `fetch()`** — convenient static entry point (no instance required).
|
|
10
|
+
- **Stubbing** — `stub()` / `unstub()` for deterministic tests without network.
|
|
11
|
+
- **Mock mode** — `useMock()` caches GET responses and returns them on repeat requests.
|
|
12
|
+
- **Re-exports `axios`** — all `axios` types, classes, and helpers available from the same import.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pnpm add @biorate/axios
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Requires `@biorate/inversion`, `axios` (peer).
|
|
21
|
+
|
|
22
|
+
## Quick start
|
|
6
23
|
|
|
7
24
|
```ts
|
|
8
25
|
import { Axios } from '@biorate/axios';
|
|
9
26
|
|
|
10
27
|
class Yandex extends Axios {
|
|
11
28
|
public baseURL = 'https://yandex.ru';
|
|
29
|
+
public timeout = 5000;
|
|
12
30
|
}
|
|
13
31
|
|
|
14
32
|
(async () => {
|
|
15
33
|
const response = await Yandex.fetch<string>();
|
|
16
34
|
console.log(response.status); // 200
|
|
17
|
-
console.log(response.data);
|
|
35
|
+
console.log(response.data); // <!DOCTYPE html>...
|
|
18
36
|
})();
|
|
19
37
|
```
|
|
20
38
|
|
|
39
|
+
## Module reference
|
|
40
|
+
|
|
41
|
+
### `Axios` — Main class
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import { Axios } from '@biorate/axios';
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Extends `Singleton` from `@biorate/singleton`.
|
|
48
|
+
|
|
49
|
+
#### Static methods
|
|
50
|
+
|
|
51
|
+
| Method | Signature | Description |
|
|
52
|
+
|-------------------|------------------------------------------------------------|-----------------------------------------------|
|
|
53
|
+
| `fetch<T, D>` | `static async fetch<T, D>(options?): Promise<AxiosResponse<T, D>>` | Makes an HTTP request (uses `axios.request`). |
|
|
54
|
+
| `stub` | `static stub(params: IStubParam, persist?: boolean): void` | Replaces `fetch` with a fake response for testing. |
|
|
55
|
+
| `unstub` | `static unstub(): void` | Restores the original `fetch` implementation. |
|
|
56
|
+
| `useMock` | `static useMock(): void` | Enables mock mode — caches successful GET responses. |
|
|
57
|
+
| `defaults` | `static get defaults()` | Delegates to `axios.defaults`. |
|
|
58
|
+
|
|
59
|
+
#### Instance hooks
|
|
60
|
+
|
|
61
|
+
Override these in subclasses to add custom logic:
|
|
62
|
+
|
|
63
|
+
| Hook | Signature | When called |
|
|
64
|
+
|-------------------|---------------------------------------------------------------------|---------------------------------|
|
|
65
|
+
| `before` | `protected async before(params?: IAxiosFetchOptions): Promise<void>`| Before request. |
|
|
66
|
+
| `after` | `protected async after<T>(result, startTime, params?): Promise<void>`| After successful response. |
|
|
67
|
+
| `catch` | `protected async catch(e, startTime, params?): Promise<void>` | On error. |
|
|
68
|
+
| `finally` | `protected async finally(startTime, params?): Promise<void>` | Always after request completes. |
|
|
69
|
+
| `getStartTime` | `protected getStartTime(): [number, number]` | Returns `[Date.now(), 0]`. |
|
|
70
|
+
|
|
71
|
+
#### Instance properties
|
|
72
|
+
|
|
73
|
+
| Property | Type | Default | Description |
|
|
74
|
+
|------------------|----------|----------|------------------------|
|
|
75
|
+
| `timeout` | `number` | `15000` | Request timeout in ms. |
|
|
76
|
+
| `baseURL` | `string` | — | Base URL for requests. |
|
|
77
|
+
|
|
78
|
+
### Types
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
import { IAxiosFetchOptions, IStubParam } from '@biorate/axios';
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
| Export | Signature | Description |
|
|
85
|
+
|----------------------|----------------------------------------------------------------------|-------------------------------------------------|
|
|
86
|
+
| `IAxiosFetchOptions` | `type = AxiosRequestConfig & { path?: Record<string, string\|number>; retry?: boolean }` | Extended request config with path params. |
|
|
87
|
+
| `IStubParam` | `type = { data, status?, statusText?, headers?, config? }` | Parameters for `Axios.stub()` fake response. |
|
|
88
|
+
|
|
89
|
+
### Re-exports from `axios`
|
|
90
|
+
|
|
91
|
+
The package re-exports all `axios` exports for convenience:
|
|
92
|
+
|
|
93
|
+
**Classes:** `Axios`, `AxiosError`, `AxiosHeaders`, `CanceledError`, `CancelToken`.
|
|
94
|
+
|
|
95
|
+
**Interfaces:** `AxiosInstance`, `AxiosStatic`, `AxiosRequestConfig`, `InternalAxiosRequestConfig`, `AxiosResponse`, `AxiosDefaults`, `CreateAxiosDefaults`, `HeadersDefaults`, `AxiosInterceptorManager`, `AxiosInterceptorOptions`, `CancelStatic`, `Cancel`, `Canceler`, `CancelTokenStatic`, `CancelTokenSource`, `GenericAbortSignal`, `GenericFormData`, `GenericHTMLFormElement`.
|
|
96
|
+
|
|
97
|
+
**Types:** `RawAxiosRequestConfig`, `AxiosPromise`, `AxiosHeaderValue`, `RawAxiosHeaders`, `RawAxiosRequestHeaders`, `AxiosRequestHeaders`, `RawAxiosResponseHeaders`, `AxiosResponseHeaders`, `Method`, `ResponseType`, `responseEncoding`, `TransitionalOptions`, `AxiosAdapter`, `AxiosAdapterName`, `AxiosAdapterConfig`, `AxiosBasicCredentials`, `AxiosProxyConfig`, `AxiosProgressEvent`, `AxiosRequestTransformer`, `AxiosResponseTransformer`, `SerializerVisitor`, `SerializerOptions`, `FormSerializerOptions`, `ParamEncoder`, `CustomParamsSerializer`, `ParamsSerializerOptions`, `AddressFamily`, `LookupAddressEntry`, `LookupAddress`.
|
|
98
|
+
|
|
99
|
+
**Enums:** `HttpStatusCode` (all standard HTTP status codes).
|
|
100
|
+
|
|
101
|
+
**Functions:** `getAdapter`, `toFormData`, `formToJSON`, `isAxiosError`, `spread`, `isCancel`, `all`, `mergeConfig`, `create`.
|
|
102
|
+
|
|
103
|
+
## Usage patterns
|
|
104
|
+
|
|
105
|
+
### Subclass with hooks
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
import { Axios, IAxiosFetchOptions } from '@biorate/axios';
|
|
109
|
+
import { AxiosResponse } from 'axios';
|
|
110
|
+
|
|
111
|
+
class ApiClient extends Axios {
|
|
112
|
+
public baseURL = 'https://api.example.com';
|
|
113
|
+
|
|
114
|
+
protected async before(params?: IAxiosFetchOptions) {
|
|
115
|
+
console.log(`→ ${params?.method || 'GET'} ${params?.url}`);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
protected async after<T>(result: AxiosResponse<T>, startTime: [number, number]) {
|
|
119
|
+
console.log(`← ${result.status} (${Date.now() - startTime[0]}ms)`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const response = await ApiClient.fetch({ url: '/users' });
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Testing with stubs
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
Axios.stub({ data: { users: [] }, status: 200 });
|
|
130
|
+
const response = await ApiClient.fetch({ url: '/users' });
|
|
131
|
+
console.log(response.data); // { users: [] }
|
|
132
|
+
Axios.unstub();
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Mock mode
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
Axios.useMock();
|
|
139
|
+
// First call hits the network, subsequent identical GET calls return cached response.
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Architecture
|
|
143
|
+
|
|
144
|
+
```
|
|
145
|
+
Axios (extends Singleton)
|
|
146
|
+
│
|
|
147
|
+
├── static fetch(options)
|
|
148
|
+
│ ├── lookup mock cache (if useMock enabled)
|
|
149
|
+
│ └── → instance.before()
|
|
150
|
+
│ → instance.fetch() → axios.request(...)
|
|
151
|
+
│ → instance.after() or instance.catch()
|
|
152
|
+
│ → instance.finally()
|
|
153
|
+
│ └── set mock cache (if useMock enabled)
|
|
154
|
+
│
|
|
155
|
+
├── static stub(params) Replace implementation with FakeResponse
|
|
156
|
+
├── static unstub() Restore original implementation
|
|
157
|
+
├── static useMock() Enable GET response caching
|
|
158
|
+
│
|
|
159
|
+
└── Stubs (internal)
|
|
160
|
+
└── FakeResponse implements AxiosResponse
|
|
161
|
+
```
|
|
162
|
+
|
|
21
163
|
### Learn
|
|
22
164
|
|
|
23
165
|
- Documentation can be found here - [docs](https://biorate.github.io/core/modules/axios.html).
|
|
@@ -26,7 +168,7 @@ class Yandex extends Axios {
|
|
|
26
168
|
|
|
27
169
|
See the [CHANGELOG](https://github.com/biorate/core/blob/master/packages/%40biorate/axios/CHANGELOG.md)
|
|
28
170
|
|
|
29
|
-
|
|
171
|
+
## License
|
|
30
172
|
|
|
31
173
|
[MIT](https://github.com/biorate/core/blob/master/packages/%40biorate/axios/LICENSE)
|
|
32
174
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@biorate/axios",
|
|
3
|
-
"version": "3.3.
|
|
3
|
+
"version": "3.3.2",
|
|
4
4
|
"description": "Axios OOP static interface",
|
|
5
5
|
"main": "./dist/cjs/index.js",
|
|
6
6
|
"module": "./dist/esm/index.js",
|
|
@@ -47,9 +47,9 @@
|
|
|
47
47
|
],
|
|
48
48
|
"author": "llevkin",
|
|
49
49
|
"license": "MIT",
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "e17a03900999e261a0eb04c6afb09b9535cad952",
|
|
51
51
|
"peerDependencies": {
|
|
52
|
-
"lodash-es": ">=4.
|
|
52
|
+
"lodash-es": ">=4.18.1"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
55
|
"@biorate/singleton": "2.1.0",
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"path-to-url": "0.1.0"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
|
-
"@biorate/config": "3.2.
|
|
63
|
-
"@biorate/inversion": "3.1.
|
|
62
|
+
"@biorate/config": "3.2.2",
|
|
63
|
+
"@biorate/inversion": "3.1.2"
|
|
64
64
|
}
|
|
65
65
|
}
|