wok-server 0.7.0 → 0.7.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.
@@ -36,7 +36,8 @@ function doRequest(opts) {
36
36
  method: opts.method,
37
37
  headers: opts.headers,
38
38
  timeout: opts.timeout && opts.timeout > 0 ? opts.timeout : 5000,
39
- rejectUnauthorized: false
39
+ rejectUnauthorized: false,
40
+ agent: opts.agent
40
41
  }, res => {
41
42
  const chunks = [];
42
43
  res.on('error', reject);
@@ -89,7 +90,8 @@ async function postJson(opts) {
89
90
  timeout: opts.timeout,
90
91
  method: 'POST',
91
92
  body: JSON.stringify(opts.body),
92
- followRedirect: false
93
+ followRedirect: false,
94
+ agent: opts.agent
93
95
  });
94
96
  if (res.status !== 200) {
95
97
  if (res.body.byteLength < 1024) {
@@ -117,7 +119,8 @@ async function getJson(opts) {
117
119
  headers: opts.headers,
118
120
  timeout: opts.timeout,
119
121
  method: 'GET',
120
- followRedirect: true
122
+ followRedirect: true,
123
+ agent: opts.agent
121
124
  });
122
125
  if (res.status !== 200) {
123
126
  if (res.body.byteLength < 1024) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wok-server",
3
- "version": "0.7.0",
3
+ "version": "0.7.2",
4
4
  "packageManager": "pnpm@8.9.0",
5
5
  "description": "一个基于 NodeJs 和 TypeScript 的后端框架,轻量级、克制、简洁。A lightweight, restrained, and concise backend framework based on Node.js and TypeScript.",
6
6
  "scripts": {
@@ -1,5 +1,5 @@
1
- import { IncomingHttpHeaders, request as requestHttp } from 'http'
2
- import { request as requestHttps } from 'https'
1
+ import { Agent as HttpAgent, IncomingHttpHeaders, request as requestHttp } from 'http'
2
+ import { Agent as HttpsAgent, request as requestHttps } from 'https'
3
3
  import { URL } from 'url'
4
4
 
5
5
  /**
@@ -34,6 +34,10 @@ export interface HttpRequestOpts {
34
34
  * 是否跟随重定向
35
35
  */
36
36
  followRedirect?: boolean
37
+ /**
38
+ * HTTP/HTTPS Agent,用于连接复用、代理等高级配置
39
+ */
40
+ agent?: HttpAgent | HttpsAgent
37
41
  }
38
42
  /**
39
43
  * 响应信息.
@@ -86,7 +90,8 @@ export function doRequest(opts: HttpRequestOpts): Promise<HttpResponseInfo> {
86
90
  method: opts.method,
87
91
  headers: opts.headers,
88
92
  timeout: opts.timeout && opts.timeout > 0 ? opts.timeout : 5000,
89
- rejectUnauthorized: false
93
+ rejectUnauthorized: false,
94
+ agent: opts.agent
90
95
  },
91
96
  res => {
92
97
  const chunks: any[] = []
@@ -136,7 +141,7 @@ export function doRequest(opts: HttpRequestOpts): Promise<HttpResponseInfo> {
136
141
  * @param opts
137
142
  */
138
143
  export async function postJson<T>(
139
- opts: Pick<HttpRequestOpts, 'url' | 'query' | 'headers' | 'timeout'> & { body: any }
144
+ opts: Pick<HttpRequestOpts, 'url' | 'query' | 'headers' | 'timeout' | 'agent'> & { body: unknown }
140
145
  ): Promise<T> {
141
146
  const headers = Object.assign({}, opts.headers || {}, {
142
147
  'Content-Type': 'application/json; charset=utf-8'
@@ -148,7 +153,8 @@ export async function postJson<T>(
148
153
  timeout: opts.timeout,
149
154
  method: 'POST',
150
155
  body: JSON.stringify(opts.body),
151
- followRedirect: false
156
+ followRedirect: false,
157
+ agent: opts.agent
152
158
  })
153
159
  if (res.status !== 200) {
154
160
  if (res.body.byteLength < 1024) {
@@ -173,7 +179,7 @@ export async function postJson<T>(
173
179
  * @param opts
174
180
  */
175
181
  export async function getJson<T>(
176
- opts: Pick<HttpRequestOpts, 'url' | 'query' | 'headers' | 'timeout'>
182
+ opts: Pick<HttpRequestOpts, 'url' | 'query' | 'headers' | 'timeout' | 'agent'>
177
183
  ): Promise<T> {
178
184
  const res = await doRequest({
179
185
  url: opts.url,
@@ -181,7 +187,8 @@ export async function getJson<T>(
181
187
  headers: opts.headers,
182
188
  timeout: opts.timeout,
183
189
  method: 'GET',
184
- followRedirect: true
190
+ followRedirect: true,
191
+ agent: opts.agent
185
192
  })
186
193
  if (res.status !== 200) {
187
194
  if (res.body.byteLength < 1024) {
@@ -1,6 +1,8 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node" />
3
- import { IncomingHttpHeaders } from 'http';
3
+ /// <reference types="node" />
4
+ import { Agent as HttpAgent, IncomingHttpHeaders } from 'http';
5
+ import { Agent as HttpsAgent } from 'https';
4
6
  /**
5
7
  * 请求选项.
6
8
  */
@@ -33,6 +35,10 @@ export interface HttpRequestOpts {
33
35
  * 是否跟随重定向
34
36
  */
35
37
  followRedirect?: boolean;
38
+ /**
39
+ * HTTP/HTTPS Agent,用于连接复用、代理等高级配置
40
+ */
41
+ agent?: HttpAgent | HttpsAgent;
36
42
  }
37
43
  /**
38
44
  * 响应信息.
@@ -61,11 +67,11 @@ export declare function doRequest(opts: HttpRequestOpts): Promise<HttpResponseIn
61
67
  * 发送 json 格式的 post 请求,body 是未序列化的普通对象,表示要发送的请求数据.
62
68
  * @param opts
63
69
  */
64
- export declare function postJson<T>(opts: Pick<HttpRequestOpts, 'url' | 'query' | 'headers' | 'timeout'> & {
65
- body: any;
70
+ export declare function postJson<T>(opts: Pick<HttpRequestOpts, 'url' | 'query' | 'headers' | 'timeout' | 'agent'> & {
71
+ body: unknown;
66
72
  }): Promise<T>;
67
73
  /**
68
74
  * get 请求获取 json 格式数据.
69
75
  * @param opts
70
76
  */
71
- export declare function getJson<T>(opts: Pick<HttpRequestOpts, 'url' | 'query' | 'headers' | 'timeout'>): Promise<T>;
77
+ export declare function getJson<T>(opts: Pick<HttpRequestOpts, 'url' | 'query' | 'headers' | 'timeout' | 'agent'>): Promise<T>;