@leyyo/http-mock 1.2.1 → 1.3.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.
@@ -1,25 +1,70 @@
1
- import type { CookieOptions, Response } from "express";
2
- import type { Arr, Dict } from "@leyyo/common";
3
- import type { HttpData, HttpHeaders, HttpStatus } from "../shared";
1
+ import { CookieOptions, Response } from "express";
2
+ import { Arr, Dict, HttpStatus } from "@leyyo/common";
3
+ import { HttpData, HttpHeaders } from "../shared";
4
+ /**
5
+ * Response local storage
6
+ * */
4
7
  export interface ResponseLocal {
5
8
  }
9
+ /**
10
+ * Response cookies
11
+ * */
6
12
  export type ResponseCookies = Dict<ResponseCookie>;
13
+ /**
14
+ * Response data
15
+ * */
7
16
  export type ResponseData = Arr | HttpData | string | number | boolean | unknown;
17
+ /**
18
+ * Response error callback
19
+ * */
8
20
  export type ResponseErrorCallback = (e?: Error) => void;
21
+ /**
22
+ * Service mock result for http bulk calls
23
+ * */
9
24
  export interface MockServicePreparedResponse<R = ResponseData> {
25
+ /**
26
+ * Response status
27
+ * */
10
28
  status: HttpStatus;
29
+ /**
30
+ * Response status message
31
+ * */
11
32
  statusMessage: string;
33
+ /**
34
+ * Response headers
35
+ * */
12
36
  headers: HttpHeaders;
37
+ /**
38
+ * Response cookies
39
+ * */
13
40
  cookies: ResponseCookies;
41
+ /**
42
+ * Response cleared cookies, Them should be cleared after all calls
43
+ * */
14
44
  clearedCookies: Dict<CookieOptions>;
45
+ /**
46
+ * Response data
47
+ * */
15
48
  data: R;
16
49
  }
50
+ /**
51
+ * Response resolver lambda
52
+ * */
17
53
  export type MockResponseResolve<R> = (dto: MockServicePreparedResponse<R>) => void;
54
+ /**
55
+ * Response cookie with options
56
+ * */
18
57
  export interface ResponseCookie {
19
58
  value: string | number;
20
59
  opt: CookieOptions;
21
60
  }
61
+ /**
62
+ * Http mock response, it extends express response
63
+ * */
22
64
  export interface MockResponseLike<R extends ResponseData, L extends ResponseLocal = ResponseLocal> extends Response<R, L> {
65
+ /**
66
+ * Indicates that it's fake/mock response
67
+ * */
23
68
  readonly isFake: boolean;
24
69
  [another: string]: unknown;
25
70
  }
@@ -1,113 +1,261 @@
1
- import type { Application, CookieOptions, Errback, Request, Response } from "express";
2
- import type { Socket } from "net";
3
- import type { MockResponseLike, MockResponseResolve, ResponseData, ResponseErrorCallback, ResponseLocal } from "./index-types";
4
- import type { Dict, KeyValue, OneOrMore } from "@leyyo/common";
5
- import { HttpEvent, type HttpStatus } from "../shared";
6
- import type { OutgoingHttpHeader, OutgoingHttpHeaders } from "node:http";
1
+ import { Application, CookieOptions, Errback, Request, Response } from "express";
2
+ import { Socket } from "net";
3
+ import { MockResponseLike, MockResponseResolve, ResponseData, ResponseErrorCallback, ResponseLocal } from "./index-types";
4
+ import { Dict, HttpStatus, KeyValue, OneOrMore } from "@leyyo/common";
5
+ import { HttpEvent } from "../shared";
6
+ import { OutgoingHttpHeader, OutgoingHttpHeaders } from "node:http";
7
7
  export declare class MockResponse<R extends ResponseData, L extends ResponseLocal = ResponseLocal> extends HttpEvent<Response> implements MockResponseLike<R, L> {
8
+ /**
9
+ * is header already sent?
10
+ * */
8
11
  private _headersSent;
12
+ /**
13
+ * Response resolver lambda
14
+ * */
9
15
  private readonly _resolver;
16
+ /**
17
+ * Response headers
18
+ * */
10
19
  private _headers;
20
+ /**
21
+ * Response cookies
22
+ * */
11
23
  private _cookies;
24
+ /**
25
+ * Response cleared cookies
26
+ * */
12
27
  private _clearedCookies;
28
+ /**
29
+ * Response data
30
+ * */
13
31
  private _data;
32
+ /**
33
+ * Response status
34
+ * */
14
35
  private _status;
36
+ /**
37
+ * Response local vale
38
+ * */
15
39
  readonly locals: L;
40
+ /** @inheritDoc */
16
41
  readonly isFake: boolean;
17
42
  [another: string]: unknown;
18
- static setFirstOrigin(origin: Response): void;
19
- static get firstOrigin(): Response;
43
+ /**
44
+ * Constructor
45
+ *
46
+ * @param {MockResponseResolve} resolver - response resolver
47
+ * @param {Response} origin - first origin/real response
48
+ * */
20
49
  constructor(resolver: MockResponseResolve<R>, origin?: Response);
50
+ /**
51
+ * It's used for in place of original sen data
52
+ *
53
+ * It collects data and trigger resolver
54
+ *
55
+ * @param {any} data
56
+ * */
21
57
  private _send;
58
+ /**
59
+ * Set header
60
+ *
61
+ * @param {string} key - header key
62
+ * @param {(string|string[])} value - header value
63
+ * */
22
64
  private _setHeader;
65
+ /**
66
+ * Set cookie
67
+ *
68
+ * @param {string} key - cookie key
69
+ * @param {string} value - cookie value
70
+ * @param {CookieOptions} opt
71
+ * */
23
72
  private _setCookie;
73
+ /**
74
+ * Remove a key from cleared cookies
75
+ *
76
+ * @param {string} key - cookie key
77
+ * @return {boolean} - is removed?
78
+ * */
24
79
  private _cancelCookieClear;
80
+ /**
81
+ * Clear response
82
+ * */
25
83
  private _clear;
84
+ /** @inheritDoc */
26
85
  destroyed: boolean;
86
+ /** @inheritDoc */
27
87
  closed: boolean;
88
+ /** @inheritDoc */
28
89
  errored: Error;
90
+ /** @inheritDoc */
29
91
  readonly writable: boolean;
92
+ /** @inheritDoc */
30
93
  readonly writableAborted: boolean;
94
+ /** @inheritDoc */
31
95
  readonly writableCorked: number;
96
+ /** @inheritDoc */
32
97
  readonly writableEnded: boolean;
98
+ /** @inheritDoc */
33
99
  readonly writableFinished: boolean;
100
+ /** @inheritDoc */
34
101
  readonly writableHighWaterMark: number;
102
+ /** @inheritDoc */
35
103
  readonly writableLength: number;
104
+ /** @inheritDoc */
36
105
  readonly writableObjectMode: boolean;
106
+ /** @inheritDoc */
37
107
  writableNeedDrain: boolean;
108
+ /** @inheritDoc */
38
109
  _construct(_c?: ResponseErrorCallback): void;
110
+ /** @inheritDoc */
39
111
  _destroy(_e: Error | null, _c: ResponseErrorCallback): void;
112
+ /** @inheritDoc */
40
113
  _final(_c: ResponseErrorCallback): void;
114
+ /** @inheritDoc */
41
115
  _write(_c: unknown, _e: BufferEncoding, _r: ResponseErrorCallback): void;
116
+ /** @inheritDoc */
42
117
  _writev(_c: Array<{
43
118
  chunk: unknown;
44
119
  encoding: BufferEncoding;
45
120
  }>, _b: ResponseErrorCallback): void;
121
+ /** @inheritDoc */
46
122
  cork(): void;
123
+ /** @inheritDoc */
47
124
  destroy(_e: Error | undefined): this;
125
+ /** @inheritDoc */
48
126
  pipe<T extends NodeJS.WritableStream>(_d: T, _o: {
49
127
  end?: boolean | undefined;
50
128
  } | undefined): T;
129
+ /** @inheritDoc */
51
130
  setDefaultEncoding(_e: BufferEncoding): this;
131
+ /** @inheritDoc */
52
132
  uncork(): void;
133
+ /** @inheritDoc */
53
134
  end(): this;
135
+ /** @inheritDoc */
54
136
  write(_b: Uint8Array | string, _c?: ResponseErrorCallback | BufferEncoding, _d?: ResponseErrorCallback): boolean;
137
+ /** @inheritDoc */
55
138
  compose<T>(_s: ComposeFnParam | Iterable<T> | AsyncIterable<T> | T, _o: {
56
139
  signal: AbortSignal;
57
140
  } | undefined): T;
141
+ /** @inheritDoc */
58
142
  sendDate: boolean;
143
+ /** @inheritDoc */
59
144
  statusMessage: string;
145
+ /** @inheritDoc */
60
146
  chunkedEncoding: boolean;
147
+ /** @inheritDoc */
61
148
  shouldKeepAlive: boolean;
149
+ /** @inheritDoc */
62
150
  finished: boolean;
151
+ /** @inheritDoc */
63
152
  get connection(): Socket;
153
+ /** @inheritDoc */
64
154
  useChunkedEncodingByDefault: boolean;
155
+ /** @inheritDoc */
65
156
  readonly socket: Socket | null;
157
+ /** @inheritDoc */
66
158
  statusCode: number;
159
+ /** @inheritDoc */
67
160
  writeHead: ((statusCode: number, statusMessage?: string, headers?: (OutgoingHttpHeaders | OutgoingHttpHeader[])) => this) & ((statusCode: number, headers?: (OutgoingHttpHeaders | OutgoingHttpHeader[])) => this);
161
+ /** @inheritDoc */
68
162
  strictContentLength: boolean;
163
+ /** @inheritDoc */
69
164
  addTrailers(_h: OutgoingHttpHeaders | Array<[string, string]>): void;
165
+ /** @inheritDoc */
70
166
  assignSocket(_s: Socket): void;
167
+ /** @inheritDoc */
71
168
  detachSocket(_s: Socket): void;
169
+ /** @inheritDoc */
72
170
  flushHeaders(): void;
171
+ /** @inheritDoc */
73
172
  getHeader(name: string): number | string | string[] | undefined;
173
+ /** @inheritDoc */
74
174
  getHeaderNames(): string[];
175
+ /** @inheritDoc */
75
176
  getHeaders(): OutgoingHttpHeaders;
177
+ /** @inheritDoc */
76
178
  hasHeader(name: string): boolean;
179
+ /** @inheritDoc */
77
180
  removeHeader(name: string): void;
181
+ /** @inheritDoc */
78
182
  setHeader(name: string, value: number | string | ReadonlyArray<string>): this;
183
+ /** @inheritDoc */
79
184
  setTimeout(_m: number, _c: (() => void) | undefined): this;
185
+ /** @inheritDoc */
80
186
  writeContinue(_c: (() => void) | undefined): void;
187
+ /** @inheritDoc */
81
188
  writeProcessing(): void;
189
+ /** @inheritDoc */
82
190
  appendHeader(name: string, value: string | readonly string[]): this;
191
+ /** @inheritDoc */
83
192
  setHeaders(headers: Headers | Map<string, number | string | readonly string[]>): this;
193
+ /** @inheritDoc */
84
194
  writeEarlyHints(_h: Record<string, string | string[]>, _c: (() => void) | undefined): void;
195
+ /** @inheritDoc */
85
196
  readonly app: Application;
197
+ /** @inheritDoc */
86
198
  charset: string;
199
+ /** @inheritDoc */
87
200
  readonly req: Request;
201
+ /** @inheritDoc */
88
202
  get headersSent(): boolean;
203
+ /** @inheritDoc */
89
204
  json(data: R): this;
205
+ /** @inheritDoc */
90
206
  jsonp(data: R): this;
207
+ /** @inheritDoc */
91
208
  send(body?: unknown): this;
209
+ /** @inheritDoc */
92
210
  attachment(filename?: string): this;
211
+ /** @inheritDoc */
93
212
  clearCookie(name: string, options?: CookieOptions): this;
213
+ /** @inheritDoc */
94
214
  append(key: string, value?: OneOrMore<string>): this;
215
+ /** @inheritDoc */
95
216
  contentType(type: string): this;
217
+ /** @inheritDoc */
96
218
  cookie(key: string, value: unknown, option?: CookieOptions): this;
219
+ /** @inheritDoc */
97
220
  download(path: string, _fn?: Errback | string, _err?: unknown, _errBack?: Errback): void;
221
+ /** @inheritDoc */
98
222
  format(obj: unknown): this;
223
+ /** @inheritDoc */
99
224
  get(field: string): string;
225
+ /** @inheritDoc */
100
226
  header(field: unknown, value?: OneOrMore<string>): this;
227
+ /** @inheritDoc */
101
228
  links(map: unknown): this;
229
+ /** @inheritDoc */
102
230
  location(url: string): this;
231
+ /** @inheritDoc */
103
232
  redirect(url: KeyValue, status?: KeyValue): void;
233
+ /** @inheritDoc */
104
234
  render(_v: string, _o?: Dict | ((err: Error, html: string) => void), _c?: (err: Error, html: string) => void): void;
235
+ /** @inheritDoc */
105
236
  sendFile(path: string, _fn?: unknown, _err?: Errback): void;
237
+ /** @inheritDoc */
106
238
  sendStatus(status: number): this;
239
+ /** @inheritDoc */
107
240
  set(field: unknown, value?: string | string[]): this;
241
+ /** @inheritDoc */
108
242
  status(status: HttpStatus): this;
243
+ /** @inheritDoc */
109
244
  type(type: string): this;
245
+ /** @inheritDoc */
110
246
  vary(field: string): this;
247
+ /**
248
+ * Set first real response
249
+ *
250
+ * @param {Response} origin
251
+ * */
252
+ static setFirstOrigin(origin: Response): void;
253
+ /**
254
+ * Get first real response
255
+ *
256
+ * @return {Response}
257
+ * */
258
+ static get firstOrigin(): Response;
111
259
  }
112
260
  type ComposeFnParam = (source: any) => void;
113
261
  export {};