@iamnnort/request 1.12.3 → 2.0.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.
package/LICENSE.md CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2019 mingchuno
3
+ Copyright (c) 2026 Nikita Pavets
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
18
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,44 +1,127 @@
1
- ## Info
1
+ # @iamnnort/request
2
2
 
3
3
  Request handler for Node.js - Fast - Interactive - Simple
4
4
 
5
5
  ## Installation
6
6
 
7
7
  ```bash
8
+ npm install @iamnnort/request
9
+ # or
8
10
  yarn add @iamnnort/request
9
11
  ```
10
12
 
11
13
  ## Usage
12
14
 
13
- ```javascript
14
- import { RequestDataSource, HttpMethods } from '@iamnnort/request';
15
+ ```typescript
16
+ import { RequestDataSource } from '@iamnnort/request';
15
17
 
16
18
  const dataSource = new RequestDataSource({
17
- baseUrl: '...',
18
- url: '/users'
19
+ name: 'Todo Api',
20
+ baseUrl: 'https://dummyjson.com',
21
+ url: '/todos',
19
22
  });
20
23
 
21
- const users = await dataSource.search();
24
+ // Search
25
+ const todos = await dataSource.search({
26
+ params: {
27
+ page: 1,
28
+ },
29
+ });
30
+
31
+ // Get by id
32
+ const todo = await dataSource.get(1);
33
+
34
+ // Create
35
+ const newTodo = await dataSource.create({
36
+ data: {
37
+ todo: 'Test todo',
38
+ completed: false,
39
+ userId: 1,
40
+ },
41
+ });
42
+
43
+ // Update
44
+ const updatedTodo = await dataSource.update(1, {
45
+ data: {
46
+ completed: true,
47
+ },
48
+ });
49
+
50
+ // Delete
51
+ await dataSource.remove(1);
52
+ ```
53
+
54
+ ## Logging
55
+
56
+ Set the `logLevel` option to enable it.
57
+
58
+ ```typescript
59
+ const dataSource = new RequestDataSource({
60
+ name: 'Todo Api',
61
+ baseUrl: 'https://dummyjson.com',
62
+ url: '/todos',
63
+ logLevel: 'debug',
64
+ });
65
+ ```
66
+
67
+ Log levels: `trace`, `debug`, `info`, `warn`, `error`, `fatal`.
22
68
 
23
- const user = await dataSource.get();
69
+ Logs include the HTTP method, full URL with query parameters, request body, status code, and duration.
70
+
71
+ When the log level is `trace` or `debug`, response body data is also included in the output.
72
+
73
+ ```
74
+ DEBUG (Todo Api): GET https://dummyjson.com/todos?page=1
75
+ INFO (Todo Api): GET https://dummyjson.com/todos?page=1 200 OK (150ms)
24
76
  ```
25
77
 
26
- ## Parameters
27
-
28
- | Parameter | Description |
29
- | ------------------ | -------------------------------------------------------------------------- |
30
- | `baseUrl` | Main part of the server URL that will be used for the request |
31
- | `url` | Server URL that will be used for the request |
32
- | `urlParts` | Additional parts of URL that will be used for the request |
33
- | `method` | Request method to be used when making the request |
34
- | `params` | URL parameters to be sent with the request |
35
- | `data` | Data to be sent as the request body |
36
- | `headers` | Custom headers to be sent |
37
- | `serializer` | Config that allows you to customize serializing |
38
- | `serializer.array` | Array element separator (`"indices"`, `"brackets"`, `"repeat"`, `"comma"`) |
39
- | `logger` | Enable a logger |
40
- | `debug` | Enable a debug mode |
78
+ ## Configuration
79
+
80
+ ### Base Config
81
+
82
+ | Parameter | Type | Description |
83
+ | ------------------ | -------- | -------------------------------------------------------------------------- |
84
+ | `name` | `string` | Name used as the logger label |
85
+ | `baseUrl` | `string` | Main part of the server URL that will be used for the request |
86
+ | `url` | `string \| number` | Server URL that will be used for the request |
87
+ | `urlParts` | `(string \| number)[]` | Additional parts of URL that will be used for the request |
88
+ | `baseUrlName` | `string` | Key to look up the base URL from `baseUrlMap` |
89
+ | `baseUrlMap` | `Record<string, string>` | Map of named base URLs |
90
+ | `headers` | `object` | Custom headers to be sent |
91
+ | `auth` | `object` | HTTP Basic auth credentials |
92
+ | `bearerToken` | `string` | Bearer token for Authorization header |
93
+ | `apiKey` | `string` | API key sent via `x-api-key` header |
94
+ | `timeout` | `number` | Request timeout in milliseconds |
95
+ | `responseType` | `string` | Response type (e.g. `json`, `text`, `stream`) |
96
+ | `logLevel` | `string` | Log level (`trace`, `debug`, `info`, `warn`, `error`, `fatal`) |
97
+ | `serializer` | `object` | Config that allows you to customize serializing |
98
+ | `serializer.array` | `string` | Array element separator (`indices`, `brackets`, `repeat`, `comma`) |
99
+
100
+ ### Request Config
101
+
102
+ | Parameter | Type | Description |
103
+ | ------------- | --------- | ------------------------------------------------- |
104
+ | `params` | `object` | URL parameters to be sent with the request |
105
+ | `data` | `object` | Data to be sent as the request body |
106
+ | `urlencoded` | `boolean` | Send data as `application/x-www-form-urlencoded` |
107
+ | `multipart` | `boolean` | Send data as `multipart/form-data` |
108
+ | `xml` | `boolean` | Send data as `text/xml` |
109
+
110
+ ## Methods
111
+
112
+ | Method | HTTP Method | Description |
113
+ | ------------ | ----------- | ---------------------------------------- |
114
+ | `search` | `GET` | Search for entities |
115
+ | `searchOne` | `GET` | Search for a single entity |
116
+ | `bulkSearch` | `GET` | Paginated search returning async generator |
117
+ | `get` | `GET` | Get entity by id |
118
+ | `create` | `POST` | Create entity |
119
+ | `bulkCreate` | `POST` | Create multiple entities |
120
+ | `update` | `PUT` | Update entity by id |
121
+ | `bulkUpdate` | `PUT` | Update multiple entities |
122
+ | `remove` | `DELETE` | Remove entity by id |
123
+ | `common` | any | Execute a custom request |
41
124
 
42
125
  ## License
43
126
 
44
- This project is licensed under the MIT license. See the [LICENSE](LICENSE) file for more info.
127
+ MIT © [Nikita Pavets](https://github.com/iamnnort)
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { AxiosRequestConfig } from 'axios';
1
+ import { AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios';
2
2
 
3
3
  type RequestParams = Pick<AxiosRequestConfig, 'params' | 'data'>;
4
4
  type RequestConfigParams = Pick<AxiosRequestConfig, 'params' | 'data'>;
@@ -23,8 +23,7 @@ type BaseRequestConfig = Pick<AxiosRequestConfig, 'auth' | 'headers' | 'timeout'
23
23
  urlParts?: (number | string)[];
24
24
  bearerToken?: string;
25
25
  apiKey?: string;
26
- debug?: boolean;
27
- logger?: boolean;
26
+ logLevel?: 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace';
28
27
  serializer?: {
29
28
  array?: 'indices' | 'brackets' | 'repeat' | 'comma';
30
29
  };
@@ -120,27 +119,32 @@ type PaginationResponse<T = unknown> = {
120
119
  pagination: Pagination;
121
120
  };
122
121
 
123
- declare class RequestBuilder {
124
- baseConfig: BaseRequestConfig;
125
- requestConfig: RequestConfig;
126
- config: AxiosRequestConfig;
127
- constructor(params: {
128
- baseConfig: BaseRequestConfig;
129
- requestConfig: RequestConfig;
130
- });
131
- makeContentType(): this;
132
- makeAuth(): this;
133
- makeUrl(): this;
134
- makeMethod(): this;
135
- makeData(): this;
136
- makeParams(): this;
137
- makeSerializer(): this;
138
- build(): AxiosRequestConfig<any>;
122
+ declare class Logger {
123
+ private config;
124
+ private logger;
125
+ private level;
126
+ constructor(config: BaseRequestConfig);
127
+ logRequest(request: AxiosRequestConfig): void;
128
+ logResponse(response: AxiosResponse, duration: number): void;
129
+ logError(error: AxiosError, duration: number): void;
130
+ makeResponse<T>(response: AxiosResponse): {
131
+ success: boolean;
132
+ status: HttpStatuses;
133
+ method: HttpMethods;
134
+ data: T;
135
+ };
136
+ makeErrorResponse<T>(error: AxiosError): {
137
+ success: boolean;
138
+ status: HttpStatuses;
139
+ method: HttpMethods;
140
+ data: T;
141
+ };
139
142
  }
140
143
 
141
144
  declare class RequestDataSource<Entity extends Record<string, any> = any, SearchParams extends RequestConfigParams = any, SearchResponse extends Record<string, any> = any, CreateParams extends RequestConfigParams = any, UpdateParams extends RequestConfigParams = any> {
142
- baseRequestConfig: BaseRequestConfig;
143
- constructor(baseRequestConfig: BaseRequestConfig);
145
+ config: BaseRequestConfig;
146
+ logger: Logger;
147
+ constructor(config: BaseRequestConfig);
144
148
  common<T>(requestConfig: RequestConfig, responseConfig?: ResponseConfig): Promise<T>;
145
149
  common<T>(requestConfig: RequestConfig, responseConfig: ResponseConfig & {
146
150
  raw: true;
@@ -150,7 +154,7 @@ declare class RequestDataSource<Entity extends Record<string, any> = any, Search
150
154
  raw: true;
151
155
  }): AsyncGenerator<PaginationResponse<T>>;
152
156
  search(config?: SearchParams): Promise<SearchResponse>;
153
- bulkSearch(config?: SearchParams): AsyncGenerator<Entity[], any, unknown>;
157
+ bulkSearch(config?: SearchParams): AsyncGenerator<Entity[], any, any>;
154
158
  searchOne(config?: SearchParams): Promise<Entity>;
155
159
  get(id: number | string, config?: SearchParams): Promise<Entity>;
156
160
  create(config: CreateParams): Promise<Entity>;
@@ -168,4 +172,4 @@ declare class RequestHelper {
168
172
  static sleep(seconds: number): Promise<unknown>;
169
173
  }
170
174
 
171
- export { type BaseRequestConfig, HttpMethods, HttpStatuses, type Pagination, type PaginationDto, type PaginationResponse, type RawResponse, RequestBuilder, type RequestConfig, type RequestConfigParams, RequestDataSource, RequestHelper, type RequestParams, type Response, type ResponseConfig };
175
+ export { type BaseRequestConfig, HttpMethods, HttpStatuses, type Pagination, type PaginationDto, type PaginationResponse, type RawResponse, type RequestConfig, type RequestConfigParams, RequestDataSource, RequestHelper, type RequestParams, type Response, type ResponseConfig };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AxiosRequestConfig } from 'axios';
1
+ import { AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios';
2
2
 
3
3
  type RequestParams = Pick<AxiosRequestConfig, 'params' | 'data'>;
4
4
  type RequestConfigParams = Pick<AxiosRequestConfig, 'params' | 'data'>;
@@ -23,8 +23,7 @@ type BaseRequestConfig = Pick<AxiosRequestConfig, 'auth' | 'headers' | 'timeout'
23
23
  urlParts?: (number | string)[];
24
24
  bearerToken?: string;
25
25
  apiKey?: string;
26
- debug?: boolean;
27
- logger?: boolean;
26
+ logLevel?: 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace';
28
27
  serializer?: {
29
28
  array?: 'indices' | 'brackets' | 'repeat' | 'comma';
30
29
  };
@@ -120,27 +119,32 @@ type PaginationResponse<T = unknown> = {
120
119
  pagination: Pagination;
121
120
  };
122
121
 
123
- declare class RequestBuilder {
124
- baseConfig: BaseRequestConfig;
125
- requestConfig: RequestConfig;
126
- config: AxiosRequestConfig;
127
- constructor(params: {
128
- baseConfig: BaseRequestConfig;
129
- requestConfig: RequestConfig;
130
- });
131
- makeContentType(): this;
132
- makeAuth(): this;
133
- makeUrl(): this;
134
- makeMethod(): this;
135
- makeData(): this;
136
- makeParams(): this;
137
- makeSerializer(): this;
138
- build(): AxiosRequestConfig<any>;
122
+ declare class Logger {
123
+ private config;
124
+ private logger;
125
+ private level;
126
+ constructor(config: BaseRequestConfig);
127
+ logRequest(request: AxiosRequestConfig): void;
128
+ logResponse(response: AxiosResponse, duration: number): void;
129
+ logError(error: AxiosError, duration: number): void;
130
+ makeResponse<T>(response: AxiosResponse): {
131
+ success: boolean;
132
+ status: HttpStatuses;
133
+ method: HttpMethods;
134
+ data: T;
135
+ };
136
+ makeErrorResponse<T>(error: AxiosError): {
137
+ success: boolean;
138
+ status: HttpStatuses;
139
+ method: HttpMethods;
140
+ data: T;
141
+ };
139
142
  }
140
143
 
141
144
  declare class RequestDataSource<Entity extends Record<string, any> = any, SearchParams extends RequestConfigParams = any, SearchResponse extends Record<string, any> = any, CreateParams extends RequestConfigParams = any, UpdateParams extends RequestConfigParams = any> {
142
- baseRequestConfig: BaseRequestConfig;
143
- constructor(baseRequestConfig: BaseRequestConfig);
145
+ config: BaseRequestConfig;
146
+ logger: Logger;
147
+ constructor(config: BaseRequestConfig);
144
148
  common<T>(requestConfig: RequestConfig, responseConfig?: ResponseConfig): Promise<T>;
145
149
  common<T>(requestConfig: RequestConfig, responseConfig: ResponseConfig & {
146
150
  raw: true;
@@ -150,7 +154,7 @@ declare class RequestDataSource<Entity extends Record<string, any> = any, Search
150
154
  raw: true;
151
155
  }): AsyncGenerator<PaginationResponse<T>>;
152
156
  search(config?: SearchParams): Promise<SearchResponse>;
153
- bulkSearch(config?: SearchParams): AsyncGenerator<Entity[], any, unknown>;
157
+ bulkSearch(config?: SearchParams): AsyncGenerator<Entity[], any, any>;
154
158
  searchOne(config?: SearchParams): Promise<Entity>;
155
159
  get(id: number | string, config?: SearchParams): Promise<Entity>;
156
160
  create(config: CreateParams): Promise<Entity>;
@@ -168,4 +172,4 @@ declare class RequestHelper {
168
172
  static sleep(seconds: number): Promise<unknown>;
169
173
  }
170
174
 
171
- export { type BaseRequestConfig, HttpMethods, HttpStatuses, type Pagination, type PaginationDto, type PaginationResponse, type RawResponse, RequestBuilder, type RequestConfig, type RequestConfigParams, RequestDataSource, RequestHelper, type RequestParams, type Response, type ResponseConfig };
175
+ export { type BaseRequestConfig, HttpMethods, HttpStatuses, type Pagination, type PaginationDto, type PaginationResponse, type RawResponse, type RequestConfig, type RequestConfigParams, RequestDataSource, RequestHelper, type RequestParams, type Response, type ResponseConfig };