@api-client/core 0.3.4 → 0.3.7

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 (109) hide show
  1. package/build/browser.d.ts +6 -0
  2. package/build/browser.js +15 -0
  3. package/build/browser.js.map +1 -1
  4. package/build/index.d.ts +14 -2
  5. package/build/index.js +26 -2
  6. package/build/index.js.map +1 -1
  7. package/build/src/lib/calculators/DataCalculator.d.ts +27 -0
  8. package/build/src/lib/calculators/DataCalculator.js +88 -0
  9. package/build/src/lib/calculators/DataCalculator.js.map +1 -0
  10. package/build/src/lib/fs/Fs.d.ts +52 -0
  11. package/build/src/lib/fs/Fs.js +245 -0
  12. package/build/src/lib/fs/Fs.js.map +1 -0
  13. package/build/src/lib/parsers/UrlEncoder.d.ts +51 -0
  14. package/build/src/lib/parsers/UrlEncoder.js +74 -0
  15. package/build/src/lib/parsers/UrlEncoder.js.map +1 -0
  16. package/build/src/lib/parsers/UrlParser.d.ts +104 -0
  17. package/build/src/lib/parsers/UrlParser.js +189 -0
  18. package/build/src/lib/parsers/UrlParser.js.map +1 -0
  19. package/build/src/lib/parsers/UrlValueParser.d.ts +92 -0
  20. package/build/src/lib/parsers/UrlValueParser.js +172 -0
  21. package/build/src/lib/parsers/UrlValueParser.js.map +1 -0
  22. package/build/src/lib/timers/Timers.d.ts +5 -0
  23. package/build/src/lib/timers/Timers.js +10 -0
  24. package/build/src/lib/timers/Timers.js.map +1 -0
  25. package/build/src/mocking/ProjectMock.d.ts +13 -0
  26. package/build/src/mocking/ProjectMock.js +16 -0
  27. package/build/src/mocking/ProjectMock.js.map +1 -0
  28. package/build/src/mocking/lib/Request.d.ts +32 -0
  29. package/build/src/mocking/lib/Request.js +63 -0
  30. package/build/src/mocking/lib/Request.js.map +1 -0
  31. package/build/src/mocking/lib/Response.d.ts +33 -0
  32. package/build/src/mocking/lib/Response.js +79 -0
  33. package/build/src/mocking/lib/Response.js.map +1 -0
  34. package/build/src/models/ErrorResponse.d.ts +5 -4
  35. package/build/src/models/ErrorResponse.js +18 -5
  36. package/build/src/models/ErrorResponse.js.map +1 -1
  37. package/build/src/models/SerializableError.d.ts +30 -0
  38. package/build/src/models/SerializableError.js +63 -0
  39. package/build/src/models/SerializableError.js.map +1 -0
  40. package/build/src/runtime/http-engine/ArcEngine.js +8 -4
  41. package/build/src/runtime/http-engine/ArcEngine.js.map +1 -1
  42. package/build/src/runtime/http-engine/HttpEngine.d.ts +3 -3
  43. package/build/src/runtime/http-engine/HttpEngine.js +3 -3
  44. package/build/src/runtime/http-engine/HttpEngine.js.map +1 -1
  45. package/build/src/runtime/http-engine/NodeEngine.js +9 -4
  46. package/build/src/runtime/http-engine/NodeEngine.js.map +1 -1
  47. package/build/src/runtime/http-engine/NodeEngineDirect.js +8 -2
  48. package/build/src/runtime/http-engine/NodeEngineDirect.js.map +1 -1
  49. package/build/src/runtime/node/BaseRunner.d.ts +21 -0
  50. package/build/src/runtime/node/BaseRunner.js +27 -0
  51. package/build/src/runtime/node/BaseRunner.js.map +1 -0
  52. package/build/src/runtime/node/ProjectParallelRunner.d.ts +81 -0
  53. package/build/src/runtime/node/ProjectParallelRunner.js +173 -0
  54. package/build/src/runtime/node/ProjectParallelRunner.js.map +1 -0
  55. package/build/src/runtime/node/ProjectRequestRunner.d.ts +125 -0
  56. package/build/src/runtime/node/ProjectRequestRunner.js +185 -0
  57. package/build/src/runtime/node/ProjectRequestRunner.js.map +1 -0
  58. package/build/src/runtime/node/ProjectRunner.d.ts +166 -64
  59. package/build/src/runtime/node/ProjectRunner.js +191 -139
  60. package/build/src/runtime/node/ProjectRunner.js.map +1 -1
  61. package/build/src/runtime/node/ProjectRunnerWorker.d.ts +1 -0
  62. package/build/src/runtime/node/ProjectRunnerWorker.js +58 -0
  63. package/build/src/runtime/node/ProjectRunnerWorker.js.map +1 -0
  64. package/build/src/runtime/node/ProjectSerialRunner.d.ts +11 -0
  65. package/build/src/runtime/node/ProjectSerialRunner.js +34 -0
  66. package/build/src/runtime/node/ProjectSerialRunner.js.map +1 -0
  67. package/build/src/runtime/reporters/ProjectRunCliReporter.d.ts +7 -0
  68. package/build/src/runtime/reporters/ProjectRunCliReporter.js +73 -0
  69. package/build/src/runtime/reporters/ProjectRunCliReporter.js.map +1 -0
  70. package/build/src/runtime/reporters/Reporter.d.ts +62 -0
  71. package/build/src/runtime/reporters/Reporter.js +98 -0
  72. package/build/src/runtime/reporters/Reporter.js.map +1 -0
  73. package/build/src/testing/TestCliHelper.d.ts +29 -0
  74. package/build/src/testing/TestCliHelper.js +80 -0
  75. package/build/src/testing/TestCliHelper.js.map +1 -0
  76. package/build/src/testing/getPort.d.ts +52 -0
  77. package/build/src/testing/getPort.js +169 -0
  78. package/build/src/testing/getPort.js.map +1 -0
  79. package/package.json +2 -1
  80. package/src/lib/calculators/DataCalculator.ts +91 -0
  81. package/src/lib/fs/Fs.ts +258 -0
  82. package/src/lib/parsers/UrlEncoder.ts +74 -0
  83. package/src/lib/parsers/UrlParser.ts +201 -0
  84. package/src/lib/parsers/UrlValueParser.ts +211 -0
  85. package/src/lib/timers/Timers.ts +9 -0
  86. package/src/mocking/LegacyInterfaces.ts +1 -1
  87. package/src/mocking/ProjectMock.ts +20 -0
  88. package/src/mocking/lib/Request.ts +85 -0
  89. package/src/mocking/lib/Response.ts +101 -0
  90. package/src/models/ErrorResponse.ts +20 -8
  91. package/src/models/SerializableError.ts +80 -0
  92. package/src/runtime/http-engine/ArcEngine.ts +8 -4
  93. package/src/runtime/http-engine/HttpEngine.ts +5 -5
  94. package/src/runtime/http-engine/NodeEngine.ts +10 -5
  95. package/src/runtime/http-engine/NodeEngineDirect.ts +9 -3
  96. package/src/runtime/node/BaseRunner.ts +29 -0
  97. package/src/runtime/node/ProjectParallelRunner.ts +234 -0
  98. package/src/runtime/node/ProjectRequestRunner.ts +281 -0
  99. package/src/runtime/node/ProjectRunner.ts +281 -182
  100. package/src/runtime/node/ProjectRunnerWorker.ts +62 -0
  101. package/src/runtime/node/ProjectSerialRunner.ts +36 -0
  102. package/src/runtime/reporters/ProjectRunCliReporter.ts +79 -0
  103. package/src/runtime/reporters/Reporter.ts +142 -0
  104. package/src/testing/TestCliHelper.ts +87 -0
  105. package/src/testing/getPort.ts +212 -0
  106. package/build/src/runtime/http-engine/Errors.d.ts +0 -10
  107. package/build/src/runtime/http-engine/Errors.js +0 -14
  108. package/build/src/runtime/http-engine/Errors.js.map +0 -1
  109. package/src/runtime/http-engine/Errors.ts +0 -13
@@ -0,0 +1,201 @@
1
+ import { UrlValueParser, IUrlValueParserOptions } from './UrlValueParser.js';
2
+
3
+ /**
4
+ * A class to parse URL string.
5
+ */
6
+ export class UrlParser extends UrlValueParser {
7
+ /**
8
+ * @param value The URL value
9
+ */
10
+ constructor(value: string, opts?: IUrlValueParserOptions) {
11
+ super(opts);
12
+ this.value = value;
13
+ }
14
+
15
+ /**
16
+ * Returns protocol value in format `protocol` + ':'
17
+ *
18
+ * @return The value of the protocol or `undefined` if the value is not set
19
+ */
20
+ get protocol(): string | undefined {
21
+ return this.__data.protocol;
22
+ }
23
+
24
+ /**
25
+ * Sets value of the `protocol`
26
+ *
27
+ * @param value Protocol value.
28
+ */
29
+ set protocol(value: string | undefined) {
30
+ this.__data.protocol = value;
31
+ }
32
+
33
+ /**
34
+ * It reads the authority part of the URL value. It doesn't parses it
35
+ * to host, port and credentials parts.
36
+ *
37
+ * @return The value of the host or `undefined` if the value is not set
38
+ */
39
+ get host(): string | undefined {
40
+ return this.__data.host;
41
+ }
42
+
43
+ /**
44
+ * Sets value of the `host`
45
+ *
46
+ * @param value Host value.
47
+ */
48
+ set host(value: string | undefined) {
49
+ this.__data.host = value;
50
+ }
51
+
52
+ /**
53
+ * Returns path part of the URL.
54
+ *
55
+ * @returns The value of the path or `undefined` if the value not set
56
+ */
57
+ get path(): string | undefined {
58
+ return this.__data.path || '/';
59
+ }
60
+
61
+ /**
62
+ * Sets value of the `path`
63
+ *
64
+ * @param value Path value.
65
+ */
66
+ set path(value: string | undefined) {
67
+ this.__data.path = value;
68
+ }
69
+
70
+ /**
71
+ * Returns anchor part of the URL.
72
+ *
73
+ * @returns The value of the anchor or `undefined` if the value not set
74
+ */
75
+ get anchor(): string | undefined {
76
+ return this.__data.anchor;
77
+ }
78
+
79
+ /**
80
+ * Sets value of the `anchor`
81
+ *
82
+ * @param value The anchor value.
83
+ */
84
+ set anchor(value: string | undefined) {
85
+ this.__data.anchor = value;
86
+ }
87
+
88
+ /**
89
+ * Returns search part of the URL.
90
+ *
91
+ * @returns the value of the search or `undefined` if the value not set
92
+ */
93
+ get search(): string | undefined {
94
+ return this.__data.search;
95
+ }
96
+
97
+ /**
98
+ * Sets value of the `search`
99
+ *
100
+ * @param value Search value.
101
+ */
102
+ set search(value: string | undefined) {
103
+ this.__data.search = value;
104
+ }
105
+
106
+ /**
107
+ * The URL value. It is the same as calling `toString()`.
108
+ *
109
+ * @return The URL value for current configuration.
110
+ */
111
+ get value(): string {
112
+ return this.toString();
113
+ }
114
+
115
+ /**
116
+ * Sets value of the URL.
117
+ * It parses the url and sets properties.
118
+ *
119
+ * @param value The URL value.
120
+ */
121
+ set value(value: string) {
122
+ this.protocol = this._parseProtocol(value);
123
+ this.host = this._parseHost(value);
124
+ this.path = this._parsePath(value);
125
+ this.anchor = this._parseAnchor(value);
126
+ this.search = this._parseSearch(value);
127
+ }
128
+
129
+ /**
130
+ * Returns an array of search params.
131
+ *
132
+ * @returns The list of search params. Each item contains an
133
+ * array where the first item is the name of the parameter and the second item is the
134
+ * value.
135
+ */
136
+ get searchParams(): string[][] {
137
+ return this._parseSearchParams(this.search);
138
+ }
139
+
140
+ /**
141
+ * Sets the value of `search` and `searchParams`.
142
+ *
143
+ * @param value Search params list.
144
+ */
145
+ set searchParams(value: string[][]) {
146
+ if (!value || !value.length) {
147
+ this.search = undefined;
148
+ return;
149
+ }
150
+ this.search = value.map((item) => {
151
+ let itemValue = item[1];
152
+ if (!item[0] && !itemValue) {
153
+ return '';
154
+ }
155
+ if (itemValue === undefined) {
156
+ itemValue = '';
157
+ } else if (typeof itemValue !== 'string') {
158
+ itemValue = String(itemValue);
159
+ }
160
+ return `${item[0]}=${itemValue}`;
161
+ })
162
+ .join(this.opts.queryDelimiter);
163
+ }
164
+
165
+ /**
166
+ * Returns the URL for current settings.
167
+ *
168
+ * @return The URL value.
169
+ */
170
+ toString(): string {
171
+ let result = '';
172
+ if (this.protocol) {
173
+ result += this.protocol;
174
+ result += '//';
175
+ }
176
+ if (this.host) {
177
+ result += this.host;
178
+ }
179
+ if (this.path) {
180
+ if (this.path === '/' && !this.host && !this.search && !this.anchor) {
181
+ // ???
182
+ } else {
183
+ if (this.path[0] !== '/') {
184
+ result += '/';
185
+ }
186
+ result += this.path;
187
+ }
188
+ } else if (this.search || this.anchor) {
189
+ result += '/';
190
+ }
191
+ if (this.search) {
192
+ const p = this.searchParams;
193
+ this.searchParams = p;
194
+ result += `?${this.search}`;
195
+ }
196
+ if (this.anchor) {
197
+ result += `#${this.anchor}`;
198
+ }
199
+ return result;
200
+ }
201
+ }
@@ -0,0 +1,211 @@
1
+ export interface IUrlValueParserOptions {
2
+ /**
3
+ * A query string delimiter to use when processing query parameters.
4
+ */
5
+ queryDelimiter?: string;
6
+ }
7
+
8
+ interface DataValues {
9
+ /**
10
+ * A protocol value in format `protocol` + ':'
11
+ */
12
+ protocol?: string;
13
+ /**
14
+ * The authority part of the URL value
15
+ */
16
+ host?: string;
17
+ /**
18
+ * Path part of the URL.
19
+ */
20
+ path?: string;
21
+ /**
22
+ * Anchor part of the URL.
23
+ */
24
+ anchor?: string;
25
+ /**
26
+ * Search part of the URL.
27
+ */
28
+ search?: string;
29
+ opts?: IUrlValueParserOptions | undefined;
30
+ }
31
+
32
+ /**
33
+ * Implements logic for parsing URL string.
34
+ */
35
+ export class UrlValueParser {
36
+ protected __data: DataValues;
37
+
38
+ constructor(opts?: IUrlValueParserOptions) {
39
+ this.__data = {};
40
+ this.opts = opts;
41
+ }
42
+
43
+ /**
44
+ * @returns Class options.
45
+ */
46
+ get opts(): IUrlValueParserOptions {
47
+ return this.__data.opts || {
48
+ queryDelimiter: '&',
49
+ };
50
+ }
51
+
52
+ /**
53
+ * Sets parser options.
54
+ * Unknown options are ignored.
55
+ */
56
+ set opts(opts: IUrlValueParserOptions | undefined) {
57
+ const options = (opts || {}) as IUrlValueParserOptions;
58
+ this.__data.opts = {
59
+ queryDelimiter: options.queryDelimiter || '&'
60
+ };
61
+ }
62
+
63
+ /**
64
+ * Returns protocol value in format `protocol` + ':'
65
+ *
66
+ * @param value URL to parse.
67
+ * @return Value of the protocol or undefined if value not set
68
+ */
69
+ protected _parseProtocol(value: string): string | undefined {
70
+ if (!value) {
71
+ return undefined;
72
+ }
73
+ const delimiterIndex = value.indexOf('://');
74
+ if (delimiterIndex !== -1) {
75
+ return value.substring(0, delimiterIndex + 1);
76
+ }
77
+ return undefined;
78
+ }
79
+
80
+ /**
81
+ * Gets a host value from the url.
82
+ * It reads the whole authority value of given `value`. It doesn't parses it
83
+ * to host, port and
84
+ * credentials parts. For URL panel it's enough.
85
+ *
86
+ * @param value The URL to parse
87
+ * @return Value of the host or undefined.
88
+ */
89
+ protected _parseHost(value: string): string | undefined {
90
+ if (!value) {
91
+ return undefined;
92
+ }
93
+ let result = value;
94
+ const delimiterIndex = result.indexOf('://');
95
+ if (delimiterIndex !== -1) {
96
+ result = result.substring(delimiterIndex + 3);
97
+ }
98
+ if (!result) {
99
+ return undefined;
100
+ }
101
+ // We don't need specifics here (username, password, port)
102
+ const host = result.split('/')[0];
103
+ return host;
104
+ }
105
+
106
+ /**
107
+ * Parses the path part of the URL.
108
+ *
109
+ * @param value URL value
110
+ * @returns Path part of the URL
111
+ */
112
+ protected _parsePath(value: string): string | undefined {
113
+ if (!value) {
114
+ return undefined;
115
+ }
116
+ let result = value;
117
+ const isBasePath = result[0] === '/';
118
+ if (!isBasePath) {
119
+ const index = result.indexOf('://');
120
+ if (index !== -1) {
121
+ result = result.substring(index + 3);
122
+ }
123
+ }
124
+ let index = result.indexOf('?');
125
+ if (index !== -1) {
126
+ result = result.substring(0, index);
127
+ }
128
+ index = result.indexOf('#');
129
+ if (index !== -1) {
130
+ result = result.substring(0, index);
131
+ }
132
+ const lastIsSlash = result[result.length - 1] === '/';
133
+ const parts = result.split('/').filter((part) => !!part);
134
+ if (!isBasePath) {
135
+ parts.shift();
136
+ }
137
+ let path = `/${ parts.join('/')}`;
138
+ if (lastIsSlash && parts.length > 1) {
139
+ path += '/';
140
+ }
141
+ return path;
142
+ }
143
+
144
+ /**
145
+ * Returns query parameters string (without the '?' sign) as a whole.
146
+ *
147
+ * @param value The URL to parse
148
+ * @returns Value of the search string or undefined.
149
+ */
150
+ protected _parseSearch(value: string): string | undefined {
151
+ if (!value) {
152
+ return undefined;
153
+ }
154
+ let index = value.indexOf('?');
155
+ if (index === -1) {
156
+ return undefined;
157
+ }
158
+ const result = value.substring(index + 1);
159
+ index = result.indexOf('#');
160
+ if (index === -1) {
161
+ return result;
162
+ }
163
+ return result.substring(0, index);
164
+ }
165
+
166
+ /**
167
+ * Reads a value of the anchor (or hash) parameter without the `#` sign.
168
+ *
169
+ * @param value The URL to parse
170
+ * @returns Value of the anchor (hash) or undefined.
171
+ */
172
+ protected _parseAnchor(value: string): string | undefined {
173
+ if (!value) {
174
+ return undefined;
175
+ }
176
+ const index = value.indexOf('#');
177
+ if (index === -1) {
178
+ return undefined;
179
+ }
180
+ return value.substring(index + 1);
181
+ }
182
+
183
+ /**
184
+ * Returns an array of items where each item is an array where first
185
+ * item is param name and second is it's value. Both always strings.
186
+ *
187
+ * @param search Parsed search parameter
188
+ * @returns Always returns an array.
189
+ */
190
+ protected _parseSearchParams(search?: string): string[][] {
191
+ const result: string[][] = [];
192
+ if (!search) {
193
+ return result;
194
+ }
195
+ const parts = search.split(this.opts.queryDelimiter as string);
196
+ parts.forEach((item) => {
197
+ const _part = ['', ''];
198
+ const _params = item.split('=');
199
+ let _name = _params.shift();
200
+ if (!_name && _name !== '') {
201
+ return;
202
+ }
203
+ _name = _name.trim();
204
+ const _value = _params.join('=').trim();
205
+ _part[0] = _name;
206
+ _part[1] = _value;
207
+ result.push(_part);
208
+ });
209
+ return result;
210
+ }
211
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Awaits the set number of milliseconds before resolving the promise.
3
+ * @param timeout The number of milliseconds to wait.
4
+ */
5
+ export function sleep(timeout = 0): Promise<void> {
6
+ return new Promise((resolve) => {
7
+ setTimeout(() => resolve(), timeout);
8
+ });
9
+ }
@@ -1,4 +1,4 @@
1
- import { DataMockInit, HarTimingInit, HttpRequestInit, HttpResponseRedirectStatusInit } from '@pawel-up/data-mock/types'
1
+ import { DataMockInit, HarTimingInit, HttpResponseRedirectStatusInit } from '@pawel-up/data-mock/types'
2
2
 
3
3
  export interface ArcDataMockInit extends DataMockInit {
4
4
  }
@@ -0,0 +1,20 @@
1
+ import { DataMock, DataMockInit } from '@pawel-up/data-mock';
2
+ import { Request } from './lib/Request.js';
3
+ import { Response } from './lib/Response.js';
4
+
5
+ export { IRequestLogInit } from './lib/Request.js';
6
+ export { IArcResponseInit } from './lib/Response.js';
7
+
8
+ export class ProjectMock extends DataMock {
9
+ projectRequest: Request;
10
+ response: Response;
11
+
12
+ /**
13
+ * @param init The library init options.
14
+ */
15
+ constructor(init?: DataMockInit) {
16
+ super(init);
17
+ this.projectRequest = new Request(init);
18
+ this.response = new Response(init);
19
+ }
20
+ }
@@ -0,0 +1,85 @@
1
+ import { Http, Types, Lorem, Time, DataMockInit, HttpRequestInit } from '@pawel-up/data-mock';
2
+ // import { randomValue } from '@pawel-up/data-mock/src/lib/Http.js';
3
+ import { IHttpRequest, Kind as HttpRequestKind } from '../../models/HttpRequest.js';
4
+ import { IRequest, Kind as RequestKind } from '../../models/Request.js';
5
+ import { ISentRequest } from '../../models/SentRequest.js';
6
+ import { IRequestLog } from '../../models/RequestLog.js';
7
+ import { IArcResponseInit, Response } from './Response.js';
8
+
9
+ export interface IRequestLogInit {
10
+ request?: HttpRequestInit;
11
+ response?: IArcResponseInit;
12
+ /**
13
+ * When set it ignores size information
14
+ */
15
+ noSize?: boolean;
16
+ /**
17
+ * Adds redirects to the request
18
+ */
19
+ redirects?: boolean;
20
+ noResponse?: boolean;
21
+ noRequest?: boolean;
22
+ }
23
+
24
+ export class Request {
25
+ types: Types;
26
+ lorem: Lorem;
27
+ time: Time;
28
+ http: Http;
29
+ response: Response;
30
+
31
+ constructor(init: DataMockInit={}) {
32
+ this.types = new Types(init.seed);
33
+ this.lorem = new Lorem(init);
34
+ this.time = new Time(init);
35
+ this.http = new Http(init);
36
+ this.response = new Response(init);
37
+ }
38
+
39
+ request(init?: HttpRequestInit): IRequest {
40
+ return {
41
+ kind: RequestKind,
42
+ expects: this.httpRequest(init),
43
+ info: {
44
+ name: this.lorem.words(2),
45
+ description: this.lorem.paragraph(),
46
+ },
47
+ }
48
+ }
49
+
50
+ httpRequest(init?: HttpRequestInit): IHttpRequest {
51
+ const request = this.http.request(init);
52
+ return {
53
+ kind: HttpRequestKind,
54
+ ...request,
55
+ }
56
+ }
57
+
58
+ sentRequest(init?: HttpRequestInit): ISentRequest {
59
+ const start = this.time.timestamp();
60
+ return {
61
+ startTime: start,
62
+ endTime: this.time.timestamp({ min: start + 1 }),
63
+ ...this.httpRequest(init),
64
+ };
65
+ }
66
+
67
+ log(init: IRequestLogInit = {}): IRequestLog {
68
+ const result: IRequestLog = {
69
+ kind: 'ARC#ResponseLog',
70
+ };
71
+ if (!init.noRequest) {
72
+ result.request = this.sentRequest(init.request);
73
+ }
74
+ if (!init.noResponse) {
75
+ result.response = this.response.arcResponse(init.response);
76
+ }
77
+ if (init.redirects) {
78
+ result.redirects = this.response.redirects();
79
+ }
80
+ if (!init.noSize) {
81
+ result.size = this.response.size();
82
+ }
83
+ return result;
84
+ }
85
+ }
@@ -0,0 +1,101 @@
1
+ import { Http, Har, Types, Lorem, Time, DataMockInit, HttpResponseInit, HarTimingInit, Internet } from '@pawel-up/data-mock';
2
+ import { IHttpResponse, Kind as HttpResponseKind } from '../../models/HttpResponse.js';
3
+ import { IArcResponse, Kind as ArcResponseKind } from '../../models/ArcResponse.js';
4
+ import { IRequestsSize } from '../../models/RequestsSize.js';
5
+ import { IResponseRedirect, Kind as ResponseRedirectKind } from '../../models/ResponseRedirect.js';
6
+
7
+ export interface IArcResponseInit extends HttpResponseInit, HarTimingInit {
8
+ /**
9
+ * When set it does not generate a response payload.
10
+ */
11
+ noBody?: boolean;
12
+ /**
13
+ * The first number of the status group. Other 2 are auto generated
14
+ */
15
+ statusGroup?: number;
16
+ /**
17
+ * Whether to generate timings object
18
+ */
19
+ timings?: boolean;
20
+ }
21
+
22
+ export class Response {
23
+ types: Types;
24
+ lorem: Lorem;
25
+ time: Time;
26
+ http: Http;
27
+ har: Har;
28
+ internet: Internet;
29
+
30
+ constructor(init: DataMockInit={}) {
31
+ this.types = new Types(init.seed);
32
+ this.lorem = new Lorem(init);
33
+ this.time = new Time(init);
34
+ this.http = new Http(init);
35
+ this.har = new Har(init);
36
+ this.internet = new Internet(init);
37
+ }
38
+
39
+ response(init: IArcResponseInit = {}): IHttpResponse {
40
+ const ct = init.noBody ? undefined : this.http.headers.contentType();
41
+ const body = init.noBody ? undefined : this.http.payload.payload(ct);
42
+ const headers = this.http.headers.headers('response', { mime: ct });
43
+ const statusGroup = init.statusGroup ? init.statusGroup : this.types.number({ min: 2, max: 5 });
44
+ const sCode = this.types.number({ min: 0, max: 99 }).toString();
45
+ const code = Number(`${statusGroup}${sCode.padStart(2, '0')}`);
46
+ const status = this.lorem.word();
47
+ const result: IHttpResponse = {
48
+ kind: HttpResponseKind,
49
+ status: code,
50
+ statusText: status,
51
+ headers,
52
+ };
53
+ if (!init.noBody) {
54
+ result.payload = body;
55
+ }
56
+ return result;
57
+ }
58
+
59
+ arcResponse(init: IArcResponseInit={}): IArcResponse {
60
+ const base = this.response(init);
61
+ const length = this.types.number({ min: 10, max: 4000 });
62
+ const result: IArcResponse = {
63
+ ...base,
64
+ kind: ArcResponseKind,
65
+ loadingTime: length,
66
+ };
67
+ if (init.timings) {
68
+ result.timings = this.har.timing(init);
69
+ }
70
+ return result;
71
+ }
72
+
73
+ size(): IRequestsSize {
74
+ const result: IRequestsSize = {
75
+ request: this.types.number({ min: 10 }),
76
+ response: this.types.number({ min: 10 }),
77
+ };
78
+ return result;
79
+ }
80
+
81
+ redirect(init?: IArcResponseInit): IResponseRedirect {
82
+ const start = this.time.timestamp();
83
+ const end = this.time.timestamp({ min: start + 1 })
84
+ const info: IResponseRedirect = {
85
+ kind: ResponseRedirectKind,
86
+ startTime: start,
87
+ endTime: end,
88
+ url: this.internet.uri(),
89
+ response: this.response({ ...init, statusGroup: 3}),
90
+ };
91
+ return info;
92
+ }
93
+
94
+ redirects(size=1, init?: IArcResponseInit): IResponseRedirect[] {
95
+ const result: IResponseRedirect[] = [];
96
+ for (let i = 0; i < size; i++) {
97
+ result.push(this.redirect(init));
98
+ }
99
+ return result;
100
+ }
101
+ }
@@ -2,31 +2,38 @@ import { IHttpResponse, HttpResponse, Kind } from './HttpResponse.js';
2
2
  import { ErrorResponse as LegacyErrorResponse } from './legacy/request/ArcResponse.js';
3
3
  import { PayloadSerializer } from '../lib/transformers/PayloadSerializer.js';
4
4
  import { Normalizer } from './legacy/Normalizer.js';
5
+ import { SerializableError, ISerializedError } from './SerializableError.js';
5
6
 
6
7
  export interface IErrorResponse extends IHttpResponse {
7
8
  /**
8
9
  * An error associated with the response
9
10
  */
10
- error: string | Error;
11
+ error: string | ISerializedError;
11
12
  }
12
13
 
13
14
  export class ErrorResponse extends HttpResponse {
14
15
  /**
15
16
  * An error associated with the response
16
17
  */
17
- error = new Error('Unknown error');
18
+ error = new SerializableError('Unknown error');
18
19
 
19
20
  /**
20
21
  * @returns The same Error or new Error instance when passed string.
21
22
  */
22
- static ensureError(error: string | Error): Error {
23
- return typeof error === 'string' ? new Error(error) : error;
23
+ static ensureError(error: string | Error | SerializableError): SerializableError {
24
+ if (typeof error === 'string') {
25
+ return new SerializableError(error);
26
+ }
27
+ if (error.name === 'SerializableError') {
28
+ return error as SerializableError;
29
+ }
30
+ return new SerializableError(error);
24
31
  }
25
32
 
26
33
  /**
27
34
  * @param error The error message or Error object to use.
28
35
  */
29
- static fromError(error: Error | string): ErrorResponse {
36
+ static fromError(error: Error | SerializableError | string): ErrorResponse {
30
37
  const err = ErrorResponse.ensureError(error);
31
38
  return new ErrorResponse({
32
39
  kind: Kind,
@@ -39,7 +46,7 @@ export class ErrorResponse extends HttpResponse {
39
46
  const init: IErrorResponse = {
40
47
  kind: Kind,
41
48
  status: input.status || 0,
42
- error: input.error ? ErrorResponse.ensureError(input.error) : new Error('Unknown error'),
49
+ error: input.error ? ErrorResponse.ensureError(input.error) : new SerializableError('Unknown error'),
43
50
  };
44
51
  if (input.headers) {
45
52
  init.headers = input.headers;
@@ -70,7 +77,7 @@ export class ErrorResponse extends HttpResponse {
70
77
  init = {
71
78
  kind: Kind,
72
79
  status: 0,
73
- error: new Error('Unknown error'),
80
+ error: new SerializableError('Unknown error'),
74
81
  };
75
82
  }
76
83
  this.new(init);
@@ -84,7 +91,12 @@ export class ErrorResponse extends HttpResponse {
84
91
  new(init: IErrorResponse): void {
85
92
  super.new(init);
86
93
  if (init.error) {
87
- this.error = ErrorResponse.ensureError(init.error);
94
+ if (typeof init.error === 'string') {
95
+ this.error = new SerializableError(init.error);
96
+ } else {
97
+ this.error = new SerializableError();
98
+ this.error.new(init.error);
99
+ }
88
100
  }
89
101
  }
90
102