@leyyo/http-mock 1.1.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.
Files changed (58) hide show
  1. package/README.md +415 -6
  2. package/dist/application/index.types.d.ts +114 -0
  3. package/dist/{request/index-types.js → application/index.types.js} +1 -1
  4. package/dist/application/index.types.js.map +1 -0
  5. package/dist/application/mock-application.d.ts +112 -56
  6. package/dist/application/mock-application.js +230 -96
  7. package/dist/application/mock-application.js.map +1 -1
  8. package/dist/http-mock.d.ts +7 -10
  9. package/dist/http-mock.js +30 -9
  10. package/dist/http-mock.js.map +1 -1
  11. package/dist/index.d.ts +5 -0
  12. package/dist/index.js +5 -0
  13. package/dist/index.js.map +1 -1
  14. package/dist/index.types.d.ts +32 -0
  15. package/dist/index.types.js +3 -0
  16. package/dist/index.types.js.map +1 -0
  17. package/dist/internal.d.ts +1 -0
  18. package/dist/internal.js +6 -0
  19. package/dist/internal.js.map +1 -0
  20. package/dist/request/index.d.ts +2 -2
  21. package/dist/request/index.js +2 -2
  22. package/dist/request/index.types.d.ts +61 -0
  23. package/dist/request/index.types.js +3 -0
  24. package/dist/request/index.types.js.map +1 -0
  25. package/dist/request/mock.request.d.ts +248 -0
  26. package/dist/request/mock.request.js +342 -0
  27. package/dist/request/mock.request.js.map +1 -0
  28. package/dist/response/index-types.d.ts +63 -14
  29. package/dist/response/mock-response.d.ts +218 -76
  30. package/dist/response/mock-response.js +273 -153
  31. package/dist/response/mock-response.js.map +1 -1
  32. package/dist/shared/http-event.d.ts +39 -0
  33. package/dist/shared/http-event.js +97 -0
  34. package/dist/shared/http-event.js.map +1 -0
  35. package/dist/shared/http-method.d.ts +11 -0
  36. package/dist/shared/http-method.js +21 -0
  37. package/dist/shared/http-method.js.map +1 -0
  38. package/dist/shared/http-protocol.d.ts +10 -0
  39. package/dist/shared/http-protocol.js +13 -0
  40. package/dist/shared/http-protocol.js.map +1 -0
  41. package/dist/shared/index.d.ts +4 -0
  42. package/dist/shared/index.js +21 -0
  43. package/dist/shared/index.js.map +1 -0
  44. package/dist/shared/index.types.d.ts +41 -0
  45. package/dist/shared/index.types.js +3 -0
  46. package/dist/shared/index.types.js.map +1 -0
  47. package/package.json +29 -33
  48. package/dist/internal-component.d.ts +0 -3
  49. package/dist/internal-component.js +0 -8
  50. package/dist/internal-component.js.map +0 -1
  51. package/dist/request/index-types.d.ts +0 -20
  52. package/dist/request/index-types.js.map +0 -1
  53. package/dist/request/mock-request.d.ts +0 -121
  54. package/dist/request/mock-request.js +0 -229
  55. package/dist/request/mock-request.js.map +0 -1
  56. package/dist/server.d.ts +0 -0
  57. package/dist/server.js +0 -2
  58. package/dist/server.js.map +0 -1
package/dist/index.d.ts CHANGED
@@ -1 +1,6 @@
1
+ export * from './shared';
2
+ export * from './application';
3
+ export * from './request';
4
+ export * from './response';
1
5
  export * from './http-mock';
6
+ export * from './index.types';
package/dist/index.js CHANGED
@@ -14,5 +14,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./shared"), exports);
18
+ __exportStar(require("./application"), exports);
19
+ __exportStar(require("./request"), exports);
20
+ __exportStar(require("./response"), exports);
17
21
  __exportStar(require("./http-mock"), exports);
22
+ __exportStar(require("./index.types"), exports);
18
23
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,8CAA4B"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,gDAA8B;AAC9B,4CAA0B;AAC1B,6CAA2B;AAC3B,8CAA4B;AAC5B,gDAA8B"}
@@ -0,0 +1,32 @@
1
+ import { Application, Request, Response } from "express";
2
+ import { MockResponseResolve, ResponseData } from "./response";
3
+ import { MockServiceRequest } from "./request";
4
+ import { Dict } from "@leyyo/common";
5
+ export type HttpMockTuple = [Request, Response, Application];
6
+ /**
7
+ * Http mock interface
8
+ * */
9
+ export interface HttpMockLike {
10
+ /**
11
+ * Initialize http mock
12
+ *
13
+ * @param {Request} req
14
+ * */
15
+ init(req: Request): void;
16
+ /**
17
+ * Fork http mock as tuple
18
+ *
19
+ * @return {HttpMockTuple}
20
+ * */
21
+ fork(): HttpMockTuple;
22
+ /**
23
+ * Run it for http bulk
24
+ *
25
+ * @param {Request} req - real request
26
+ * @param {MockServiceRequest} service - service payloads
27
+ * @param {MockResponseResolve} resolver - response resolver to collect response
28
+ * @param {object} custom - custom parameters
29
+ * @return {HttpMockTuple}
30
+ * */
31
+ forBulk<R = ResponseData>(req: Request, service: MockServiceRequest<R>, resolver: MockResponseResolve<R>, custom?: Dict): HttpMockTuple;
32
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=index.types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.types.js","sourceRoot":"","sources":["../src/index.types.ts"],"names":[],"mappings":""}
@@ -0,0 +1 @@
1
+ export declare const FQN = "leyyo.http-mock";
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FQN = void 0;
4
+ // noinspection JSUnusedGlobalSymbols
5
+ exports.FQN = 'leyyo.http-mock';
6
+ //# sourceMappingURL=internal.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"internal.js","sourceRoot":"","sources":["../src/internal.ts"],"names":[],"mappings":";;;AAAA,qCAAqC;AACxB,QAAA,GAAG,GAAG,iBAAiB,CAAC"}
@@ -1,2 +1,2 @@
1
- export * from './index-types';
2
- export * from './mock-request';
1
+ export * from './index.types';
2
+ export * from './mock.request';
@@ -14,6 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./index-types"), exports);
18
- __exportStar(require("./mock-request"), exports);
17
+ __exportStar(require("./index.types"), exports);
18
+ __exportStar(require("./mock.request"), exports);
19
19
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,61 @@
1
+ import { Request } from "express";
2
+ import { ResponseData } from "../response";
3
+ import { Arr } from "@leyyo/common";
4
+ import { HttpCookies, HttpData, HttpHeaders, HttpMethod, HttpParams, HttpQuery } from "../shared";
5
+ export interface RequestLocal {
6
+ }
7
+ export type RequestBody = Arr | HttpData | string | unknown;
8
+ export type RequestErrorCallback = (error?: Error) => void;
9
+ /**
10
+ * It's used for bulk http calls
11
+ * */
12
+ export interface MockServiceRequest<B extends RequestBody = RequestBody> {
13
+ /**
14
+ * Http method
15
+ * */
16
+ method: HttpMethod;
17
+ /**
18
+ * Local path
19
+ * */
20
+ url: string;
21
+ /**
22
+ * Request payload
23
+ * */
24
+ body?: B;
25
+ /**
26
+ * Custom headers
27
+ * */
28
+ headers?: HttpHeaders;
29
+ /**
30
+ * Client cookies
31
+ * */
32
+ cookies?: HttpCookies;
33
+ /**
34
+ * Server cookies
35
+ * */
36
+ signedCookies?: HttpCookies;
37
+ }
38
+ /**
39
+ * Http mock request, it extends express request
40
+ * */
41
+ export interface MockRequestLike<B extends RequestBody = RequestBody, L extends RequestLocal = RequestLocal> extends Request<HttpParams, B, ResponseData, HttpQuery, L> {
42
+ /**
43
+ * Indicates that it's fake/mock request
44
+ * */
45
+ readonly isFake?: boolean;
46
+ /**
47
+ * Temporary local storage
48
+ * */
49
+ readonly locals?: L;
50
+ /**
51
+ * Get query parameter
52
+ * */
53
+ param(name: string, defaultValue?: any): string;
54
+ /**
55
+ * Custom properties
56
+ * */
57
+ [another: string]: unknown;
58
+ }
59
+ export interface PipeOption {
60
+ end?: boolean;
61
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=index.types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.types.js","sourceRoot":"","sources":["../../src/request/index.types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,248 @@
1
+ import { Application, MediaType, NextFunction, Request, Response } from "express";
2
+ import { Socket } from "net";
3
+ import { IncomingHttpHeaders } from "http";
4
+ import RangeParser from "range-parser";
5
+ import { MockRequestLike, MockServiceRequest, PipeOption, RequestBody, RequestErrorCallback, RequestLocal } from "./index.types";
6
+ import { _StreamAbort, _StreamBool, _StreamCompose, _StreamGeneric, _StreamIterator, _StreamReduce, _StreamVoid, HttpCookies, HttpEvent, HttpMethod, HttpParams, HttpProtocol, HttpQuery } from "../shared";
7
+ import { Dict, Fnc, HttpStatus, OneOrMore } from "@leyyo/common";
8
+ import { ArrayOptions, Readable } from "node:stream";
9
+ export declare class MockRequest<B extends RequestBody = RequestBody, L extends RequestLocal = RequestLocal> extends HttpEvent<Request> implements MockRequestLike<B, L> {
10
+ /** @inheritDoc */
11
+ readonly isFake: boolean;
12
+ /** @inheritDoc */
13
+ readonly locals: L;
14
+ /** @inheritDoc */
15
+ [another: string]: unknown;
16
+ /**
17
+ * Constructor
18
+ *
19
+ * @param {MockServiceRequest} service - for bulk request
20
+ * @param {Request} origin - first real request
21
+ * @param {object} custom - custom values
22
+ * */
23
+ constructor(service?: MockServiceRequest<B>, origin?: Request, custom?: Dict);
24
+ /** @inheritDoc*/
25
+ accepted: MediaType[];
26
+ /** @inheritDoc*/
27
+ readonly protocol: HttpProtocol;
28
+ /** @inheritDoc*/
29
+ readonly secure: boolean;
30
+ /** @inheritDoc*/
31
+ readonly ip: string;
32
+ /** @inheritDoc*/
33
+ readonly ips: string[];
34
+ /** @inheritDoc*/
35
+ readonly subdomains: string[];
36
+ /** @inheritDoc*/
37
+ readonly path: string;
38
+ /** @inheritDoc*/
39
+ readonly host: string;
40
+ /** @inheritDoc*/
41
+ readonly hostname: string;
42
+ /** @inheritDoc*/
43
+ readonly httpVersion: string;
44
+ /** @inheritDoc*/
45
+ readonly httpVersionMajor: number;
46
+ /** @inheritDoc*/
47
+ readonly httpVersionMinor: number;
48
+ /** @inheritDoc*/
49
+ readonly fresh: boolean;
50
+ /** @inheritDoc*/
51
+ readonly stale: boolean;
52
+ /** @inheritDoc*/
53
+ readonly xhr: boolean;
54
+ /** @inheritDoc*/
55
+ body: RequestBody;
56
+ /** @inheritDoc*/
57
+ cookies: any;
58
+ /** @inheritDoc*/
59
+ method: HttpMethod;
60
+ /** @inheritDoc*/
61
+ params: HttpParams;
62
+ /** @inheritDoc*/
63
+ query: HttpQuery;
64
+ /** @inheritDoc*/
65
+ route: any;
66
+ /** @inheritDoc*/
67
+ signedCookies: HttpCookies;
68
+ /** @inheritDoc*/
69
+ originalUrl: string;
70
+ /** @inheritDoc*/
71
+ url: string;
72
+ /** @inheritDoc*/
73
+ baseUrl: string;
74
+ /** @inheritDoc*/
75
+ app: Application;
76
+ /** @inheritDoc*/
77
+ res?: Response<unknown, L>;
78
+ /** @inheritDoc*/
79
+ next?: NextFunction;
80
+ /** @inheritDoc*/
81
+ headers: IncomingHttpHeaders;
82
+ /** @inheritDoc*/
83
+ is(t: OneOrMore<string>): string | false | null;
84
+ /** @inheritDoc*/
85
+ get(n: "set-cookie"): string[] | undefined;
86
+ /** @inheritDoc*/
87
+ get(n: string): string | undefined;
88
+ /** @inheritDoc*/
89
+ header(n: "set-cookie"): string[] | undefined;
90
+ /** @inheritDoc*/
91
+ header(n: string): string | undefined;
92
+ /** @inheritDoc*/
93
+ accepts(): string[];
94
+ /** @inheritDoc*/
95
+ accepts(t: string): string | false;
96
+ /** @inheritDoc*/
97
+ accepts(t: string[]): string | false;
98
+ /** @inheritDoc*/
99
+ accepts(...t: string[]): string | false;
100
+ /** @inheritDoc*/
101
+ acceptsCharsets(): string[];
102
+ /** @inheritDoc*/
103
+ acceptsCharsets(c: string): string | false;
104
+ /** @inheritDoc*/
105
+ acceptsCharsets(c: string[]): string | false;
106
+ /** @inheritDoc*/
107
+ acceptsCharsets(...c: string[]): string | false;
108
+ /** @inheritDoc*/
109
+ acceptsEncodings(): string[];
110
+ /** @inheritDoc*/
111
+ acceptsEncodings(e: string): string | false;
112
+ /** @inheritDoc*/
113
+ acceptsEncodings(e: string[]): string | false;
114
+ /** @inheritDoc*/
115
+ acceptsEncodings(...e: string[]): string | false;
116
+ /** @inheritDoc*/
117
+ acceptsLanguages(): string[];
118
+ /** @inheritDoc*/
119
+ acceptsLanguages(l: string): string | false;
120
+ /** @inheritDoc*/
121
+ acceptsLanguages(l: string[]): string | false;
122
+ /** @inheritDoc*/
123
+ acceptsLanguages(...l: string[]): string | false;
124
+ /** @inheritDoc*/
125
+ range(s: number, o?: RangeParser.Options): RangeParser.Ranges | RangeParser.Result | undefined;
126
+ /** @inheritDoc*/
127
+ aborted: boolean;
128
+ /** @inheritDoc*/
129
+ complete: boolean;
130
+ /** @inheritDoc*/
131
+ statusCode: HttpStatus | undefined;
132
+ /** @inheritDoc*/
133
+ statusMessage: string | undefined;
134
+ /** @inheritDoc*/
135
+ socket: Socket;
136
+ /** @inheritDoc*/
137
+ headersDistinct: NodeJS.Dict<string[]>;
138
+ /** @inheritDoc*/
139
+ rawHeaders: string[];
140
+ /** @inheritDoc*/
141
+ trailers: Record<string, string>;
142
+ /** @inheritDoc*/
143
+ rawTrailers: string[];
144
+ /** @inheritDoc*/
145
+ trailersDistinct: NodeJS.Dict<string[]>;
146
+ /** @inheritDoc*/
147
+ get connection(): Socket;
148
+ /** @inheritDoc*/
149
+ destroy(_e?: Error): this;
150
+ /** @inheritDoc*/
151
+ setTimeout(_m: number, _c?: Fnc): this;
152
+ /** @inheritDoc*/
153
+ [Symbol.asyncIterator](): AsyncIterableIterator<any>;
154
+ /** @inheritDoc*/
155
+ [Symbol.asyncDispose](): Promise<void>;
156
+ /** @inheritDoc*/
157
+ readonly closed: boolean;
158
+ /** @inheritDoc*/
159
+ readonly errored: Error;
160
+ /** @inheritDoc*/
161
+ destroyed: boolean;
162
+ /** @inheritDoc*/
163
+ readonly readableDidRead: boolean;
164
+ /** @inheritDoc*/
165
+ readonly readableEncoding: BufferEncoding | null;
166
+ /** @inheritDoc*/
167
+ readonly readableEnded: boolean;
168
+ /** @inheritDoc*/
169
+ readonly readableAborted: boolean;
170
+ /** @inheritDoc*/
171
+ readonly readableFlowing: boolean | null;
172
+ /** @inheritDoc*/
173
+ readonly readableHighWaterMark: number;
174
+ /** @inheritDoc*/
175
+ readonly readableLength: number;
176
+ /** @inheritDoc*/
177
+ readonly readableObjectMode: boolean;
178
+ /** @inheritDoc*/
179
+ readable: boolean;
180
+ /** @inheritDoc*/
181
+ _construct(_c: RequestErrorCallback): void;
182
+ /** @inheritDoc*/
183
+ _destroy(_e: Error | null, _c: RequestErrorCallback): void;
184
+ /** @inheritDoc*/
185
+ _read(_s: number): void;
186
+ /** @inheritDoc*/
187
+ read(_s: number | undefined): string | Buffer;
188
+ /** @inheritDoc*/
189
+ push(_c: any, _e?: BufferEncoding): boolean;
190
+ /** @inheritDoc*/
191
+ isPaused(): boolean;
192
+ /** @inheritDoc*/
193
+ resume(): this;
194
+ /** @inheritDoc*/
195
+ pause(): this;
196
+ /** @inheritDoc*/
197
+ pipe<T extends NodeJS.WritableStream>(_d: T, _o?: PipeOption): T;
198
+ /** @inheritDoc*/
199
+ setEncoding(_e: BufferEncoding): this;
200
+ /** @inheritDoc*/
201
+ unpipe(_d?: NodeJS.WritableStream): this;
202
+ /** @inheritDoc*/
203
+ unshift(_c: string | Uint8Array, _e?: BufferEncoding): void;
204
+ /** @inheritDoc*/
205
+ wrap(_s: NodeJS.ReadableStream): this;
206
+ /** @inheritDoc*/
207
+ asIndexedPairs(_o: _StreamAbort | undefined): Readable;
208
+ /** @inheritDoc*/
209
+ compose<T extends NodeJS.ReadableStream>(_s: _StreamCompose | Iterable<T> | AsyncIterable<T> | T, _o: _StreamAbort | undefined): T;
210
+ /** @inheritDoc*/
211
+ drop(_l: number, _o: _StreamAbort | undefined): Readable;
212
+ /** @inheritDoc*/
213
+ every(_f: _StreamBool, _o: ArrayOptions | undefined): Promise<boolean>;
214
+ /** @inheritDoc*/
215
+ filter(_f: _StreamBool, _o: ArrayOptions | undefined): Readable;
216
+ /** @inheritDoc*/
217
+ find<T>(_f: _StreamGeneric<T>, _o: ArrayOptions | undefined): Promise<T | undefined>;
218
+ /** @inheritDoc*/
219
+ flatMap(_f: _StreamGeneric<any>, _o: ArrayOptions | undefined): Readable;
220
+ /** @inheritDoc*/
221
+ forEach(_f: _StreamVoid, _o: ArrayOptions | undefined): Promise<void>;
222
+ /** @inheritDoc*/
223
+ iterator(_o: _StreamIterator | undefined): NodeJS.AsyncIterator<any>;
224
+ /** @inheritDoc*/
225
+ map(_f: _StreamGeneric<any>, _o: ArrayOptions | undefined): Readable;
226
+ /** @inheritDoc*/
227
+ reduce<T>(_f: _StreamReduce<T>, _i: undefined, _o: _StreamAbort | undefined): Promise<T>;
228
+ /** @inheritDoc*/
229
+ some(_f: _StreamBool, _o: ArrayOptions | undefined): Promise<boolean>;
230
+ /** @inheritDoc*/
231
+ take(_l: number, _o: _StreamAbort | undefined): Readable;
232
+ /** @inheritDoc*/
233
+ toArray(_o: _StreamAbort | undefined): Promise<any[]>;
234
+ /** @inheritDoc*/
235
+ param(name: string, defaultValue?: any): string;
236
+ /**
237
+ * Set first real request
238
+ *
239
+ * @param {Request} origin
240
+ * */
241
+ static setFirstOrigin(origin: Request): void;
242
+ /**
243
+ * Get first real request
244
+ *
245
+ * @return {Request}
246
+ * */
247
+ static get firstOrigin(): Request;
248
+ }