@goast/typescript 0.3.7 → 0.3.8-beta1
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/README.md +43 -43
- package/assets/client/angular/angular-service.utils.ts +82 -82
- package/assets/client/angular/request-builder.ts +374 -374
- package/assets/client/fetch/fetch-client.utils.ts +52 -52
- package/assets/client/k6/request-builder.js +411 -411
- package/assets/stubs/easy-network-stub/easy-network-stub.utils.ts +260 -260
- package/cjs/lib/ast/references/typescript.js +2 -1
- package/cjs/lib/generators/models/model-generator.js +8 -2
- package/cjs/lib/generators/services/angular-services/angular-service-generator.js +9 -9
- package/cjs/lib/generators/services/easy-network-stub/easy-network-stub-generator.js +9 -9
- package/cjs/lib/generators/services/fetch-clients/fetch-client-generator.js +2 -2
- package/cjs/lib/generators/services/k6-clients/k6-client-generator.js +6 -6
- package/esm/lib/ast/references/typescript.js +1 -0
- package/esm/lib/generators/models/model-generator.js +8 -2
- package/esm/lib/generators/services/angular-services/angular-service-generator.js +9 -9
- package/esm/lib/generators/services/easy-network-stub/easy-network-stub-generator.js +9 -9
- package/esm/lib/generators/services/fetch-clients/fetch-client-generator.js +2 -2
- package/esm/lib/generators/services/k6-clients/k6-client-generator.js +6 -6
- package/package.json +2 -3
- package/types/lib/ast/references/typescript.d.ts +6 -0
- package/types/lib/generators/models/model-generator.d.ts +2 -1
|
@@ -1,260 +1,260 @@
|
|
|
1
|
-
import { EasyNetworkStub } from 'easy-network-stub';
|
|
2
|
-
|
|
3
|
-
import type { HttpMethod, RouteResponseCallback, ErrorResponse } from 'easy-network-stub';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Options for the `EasyNetworkStubWrapper`.
|
|
7
|
-
*/
|
|
8
|
-
export type EasyNetworkStubWrapperOptions = {
|
|
9
|
-
/**
|
|
10
|
-
* Delay in milliseconds before responding.
|
|
11
|
-
* @default 0
|
|
12
|
-
*/
|
|
13
|
-
delay: number;
|
|
14
|
-
/**
|
|
15
|
-
* Whether to log requests to the console.
|
|
16
|
-
* @default false
|
|
17
|
-
*/
|
|
18
|
-
logRequests: boolean;
|
|
19
|
-
/**
|
|
20
|
-
* Whether to log responses to the console.
|
|
21
|
-
* @default false
|
|
22
|
-
*/
|
|
23
|
-
logResponses: boolean;
|
|
24
|
-
/**
|
|
25
|
-
* Whether to remember requests. Remebered requests can be accessed via the `requests` property.
|
|
26
|
-
* @default false
|
|
27
|
-
*/
|
|
28
|
-
rememberRequests: boolean;
|
|
29
|
-
/**
|
|
30
|
-
* Function to call when a response is sent.
|
|
31
|
-
* @param response The response that was sent.
|
|
32
|
-
* @default undefined
|
|
33
|
-
*/
|
|
34
|
-
responseCallback?: (response: unknown) => void;
|
|
35
|
-
/**
|
|
36
|
-
* A prefix to add to the URL in log messages.
|
|
37
|
-
* @default /api/
|
|
38
|
-
*/
|
|
39
|
-
logUrlPrefix: string;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Represents a request intercepted by the `EasyNetworkStubWrapper`.
|
|
44
|
-
*/
|
|
45
|
-
export type StubRequestItem = {
|
|
46
|
-
/**
|
|
47
|
-
* The HTTP method of the request.
|
|
48
|
-
*/
|
|
49
|
-
method: HttpMethod;
|
|
50
|
-
/**
|
|
51
|
-
* The route of the request.
|
|
52
|
-
*/
|
|
53
|
-
route: string;
|
|
54
|
-
/**
|
|
55
|
-
* The request information provided by `easy-network-stub`.
|
|
56
|
-
*/
|
|
57
|
-
request: StubRequestInfo;
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Represents the request information provided by `easy-network-stub`.
|
|
62
|
-
*/
|
|
63
|
-
export type StubRequestInfo<TRoute extends string = string, TBody = unknown> = Parameters<
|
|
64
|
-
RouteResponseCallback<TRoute, TBody>
|
|
65
|
-
>[0];
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* A strict version of `RouteResponseCallback` that allows for a responder to be passed as the first argument.
|
|
69
|
-
*/
|
|
70
|
-
export type StrictRouteResponseCallback<T, P extends string, R> = (
|
|
71
|
-
respondWith: R,
|
|
72
|
-
...args: Parameters<RouteResponseCallback<P, T>>
|
|
73
|
-
) => ErrorResponse<any> | Promise<ErrorResponse<any>>;
|
|
74
|
-
|
|
75
|
-
type _UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
|
|
76
|
-
type __StubResponder<T extends Record<number, unknown>, S> = S extends number
|
|
77
|
-
? [T[S]] extends [never]
|
|
78
|
-
? (statusCode: S) => ErrorResponse<T[S]>
|
|
79
|
-
: (statusCode: S, content: T[S]) => ErrorResponse<T[S]>
|
|
80
|
-
: never;
|
|
81
|
-
type _StubResponder<T extends Record<number, unknown>> = _UnionToIntersection<__StubResponder<T, keyof T>>;
|
|
82
|
-
const untypedResponder = (statusCode: number, content?: unknown) => ({ statusCode, content });
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Gets the responder function typed to a specific set of status codes.
|
|
86
|
-
* @returns The responder function.
|
|
87
|
-
*/
|
|
88
|
-
export function getStubResponder<T extends Record<number, unknown>>(): _StubResponder<T> {
|
|
89
|
-
return untypedResponder as _StubResponder<T>;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Wrapper for `EasyNetworkStub` that adds additional functionality.
|
|
94
|
-
*/
|
|
95
|
-
export class EasyNetworkStubWrapper {
|
|
96
|
-
private _requests: StubRequestItem[] = [];
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* The options for the wrapper.
|
|
100
|
-
*/
|
|
101
|
-
public readonly options: Readonly<EasyNetworkStubWrapperOptions>;
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* The requests that have been intercepted by the wrapper.
|
|
105
|
-
*/
|
|
106
|
-
public get requests(): ReadonlyArray<StubRequestItem> {
|
|
107
|
-
return this._requests;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
constructor(
|
|
111
|
-
public readonly wrappedStub: EasyNetworkStub,
|
|
112
|
-
options?: Partial<EasyNetworkStubWrapperOptions>,
|
|
113
|
-
) {
|
|
114
|
-
this.options = {
|
|
115
|
-
delay: options?.delay ?? 0,
|
|
116
|
-
logRequests: options?.logRequests ?? false,
|
|
117
|
-
logResponses: options?.logResponses ?? false,
|
|
118
|
-
rememberRequests: options?.rememberRequests ?? false,
|
|
119
|
-
responseCallback: options?.responseCallback,
|
|
120
|
-
logUrlPrefix: options?.logUrlPrefix ?? '/api/',
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Add a single api stub to your intercepted routes.
|
|
126
|
-
* @param method The http method that should be stubbed
|
|
127
|
-
* @param route The route that should be stubbed. Supports parameters in the form of {name:type}.
|
|
128
|
-
* @param response The callback in which you can process the request and reply with the stub. When a Promise is returned, the stub response will be delayed until it is resolved.
|
|
129
|
-
*/
|
|
130
|
-
public stub<Route extends string>(
|
|
131
|
-
method: HttpMethod,
|
|
132
|
-
route: Route,
|
|
133
|
-
response: RouteResponseCallback<Route, unknown>,
|
|
134
|
-
): void {
|
|
135
|
-
this.wrappedStub.stub<Route>(method, route, async (request) => {
|
|
136
|
-
if (this.options.delay > 0) await sleep(this.options.delay);
|
|
137
|
-
return await this.runRequest(method, route, response, request);
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* The stub2 method provides a way to set the type of the body that is sent.
|
|
143
|
-
* Due to restrictions in TypeScript (https://github.com/microsoft/TypeScript/issues/10571, https://github.com/Microsoft/TypeScript/pull/26349),
|
|
144
|
-
* We have to hacke a bit here. Calling stub2<T>() will return the stub method with a body of type T.
|
|
145
|
-
*
|
|
146
|
-
* Usage:
|
|
147
|
-
* ```
|
|
148
|
-
* .stub2<MyRequest>()('GET', '/api/test', ({ body }) => {
|
|
149
|
-
* // body is of type MyRequest
|
|
150
|
-
* });
|
|
151
|
-
* ```
|
|
152
|
-
* @returns The stub method with a body of type T.
|
|
153
|
-
*/
|
|
154
|
-
public stub2<T>(): <Route extends string>(
|
|
155
|
-
method: HttpMethod,
|
|
156
|
-
route: Route,
|
|
157
|
-
response: RouteResponseCallback<Route, T>,
|
|
158
|
-
) => void {
|
|
159
|
-
return <Route extends string>(method: HttpMethod, route: Route, response: RouteResponseCallback<Route, T>) => {
|
|
160
|
-
this.wrappedStub.stub2<T>()<Route>(method, route, async (request) => {
|
|
161
|
-
if (this.options.delay > 0) await sleep(this.options.delay);
|
|
162
|
-
return await this.runRequest(method, route, response, request);
|
|
163
|
-
});
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Reset the stubs and requests.
|
|
169
|
-
*/
|
|
170
|
-
public reset(): void {
|
|
171
|
-
this._requests = [];
|
|
172
|
-
this.wrappedStub['_config'].stubs = [];
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
private async runRequest<Route extends string, T>(
|
|
176
|
-
method: HttpMethod,
|
|
177
|
-
route: Route,
|
|
178
|
-
response: RouteResponseCallback<Route, T>,
|
|
179
|
-
request: StubRequestInfo<Route, T>,
|
|
180
|
-
): Promise<unknown> {
|
|
181
|
-
if (this.options.rememberRequests) {
|
|
182
|
-
this._requests.push({ method, route, request });
|
|
183
|
-
}
|
|
184
|
-
this.log('request', method, route, request, '#7FFFFF');
|
|
185
|
-
try {
|
|
186
|
-
const r = await response(request);
|
|
187
|
-
this.options.responseCallback?.(r);
|
|
188
|
-
this.log('response', method, route, r, '#7FFF8E');
|
|
189
|
-
return r;
|
|
190
|
-
} catch (error: unknown) {
|
|
191
|
-
this.options.responseCallback?.(error);
|
|
192
|
-
if (isHttpResponse(error) && error.statusCode >= 200 && error.statusCode < 300) {
|
|
193
|
-
this.log('response', method, route, error.content, '#7FFF8E');
|
|
194
|
-
throw error;
|
|
195
|
-
} else {
|
|
196
|
-
const isError = isHttpResponse(error) && error.statusCode >= 400 && error.statusCode < 600;
|
|
197
|
-
this.log('response', method, route, error, isError ? '#FF7F7F' : '7F7F7F');
|
|
198
|
-
throw error;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
private log(type: 'request' | 'response', method: HttpMethod, route: string, data: unknown, color: string) {
|
|
204
|
-
if (type === 'request' && !this.options.logRequests) return;
|
|
205
|
-
if (type === 'response' && !this.options.logResponses) return;
|
|
206
|
-
const typeDisplay = type === 'request' ? ' [req]' : '[resp]';
|
|
207
|
-
const strippedRoute = route.match(/[^?]*(?=\?)?/)?.[0];
|
|
208
|
-
console.log(`%c${typeDisplay} ${method}: ${this.options.logUrlPrefix}${strippedRoute}`, `color:${color};`, data);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/**
|
|
213
|
-
* Base class for generated easy network stubs.
|
|
214
|
-
*/
|
|
215
|
-
export class EasyNetworkStubBase {
|
|
216
|
-
protected readonly stubWrapper: EasyNetworkStubWrapper;
|
|
217
|
-
protected readonly ownsStubWrapper: boolean;
|
|
218
|
-
|
|
219
|
-
constructor(stub: EasyNetworkStub | EasyNetworkStubWrapper, options?: EasyNetworkStubWrapperOptions) {
|
|
220
|
-
this.stubWrapper = stub instanceof EasyNetworkStubWrapper ? stub : new EasyNetworkStubWrapper(stub, options);
|
|
221
|
-
this.ownsStubWrapper = !(stub instanceof EasyNetworkStubWrapper);
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
public reset() {
|
|
225
|
-
if (this.ownsStubWrapper) {
|
|
226
|
-
this.stubWrapper.reset();
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
export type EasyNetworkStubGroup<Group, GroupContainer> = Group & {
|
|
232
|
-
(stubActions: (stubs: Group) => void): GroupContainer;
|
|
233
|
-
};
|
|
234
|
-
|
|
235
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
236
|
-
declare interface GroupType<T> extends Function {
|
|
237
|
-
new (stubWrapper: EasyNetworkStubWrapper): T;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
export function createEasyNetworkStubGroup<GroupContainer, Group>(
|
|
241
|
-
container: GroupContainer,
|
|
242
|
-
stubWrapper: EasyNetworkStubWrapper,
|
|
243
|
-
groupType: GroupType<Group>,
|
|
244
|
-
): EasyNetworkStubGroup<Group, GroupContainer> {
|
|
245
|
-
const group = new groupType(stubWrapper);
|
|
246
|
-
const groupFn = (stubActions: (stubs: Group) => void) => {
|
|
247
|
-
stubActions(group);
|
|
248
|
-
return container;
|
|
249
|
-
};
|
|
250
|
-
Object.setPrototypeOf(groupFn, groupType.prototype);
|
|
251
|
-
return Object.assign(groupFn, group);
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
function sleep(ms: number): Promise<void> {
|
|
255
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
function isHttpResponse(value: unknown): value is ErrorResponse<unknown> {
|
|
259
|
-
return typeof value === 'object' && value !== null && 'statusCode' in value;
|
|
260
|
-
}
|
|
1
|
+
import { EasyNetworkStub } from 'easy-network-stub';
|
|
2
|
+
|
|
3
|
+
import type { HttpMethod, RouteResponseCallback, ErrorResponse } from 'easy-network-stub';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Options for the `EasyNetworkStubWrapper`.
|
|
7
|
+
*/
|
|
8
|
+
export type EasyNetworkStubWrapperOptions = {
|
|
9
|
+
/**
|
|
10
|
+
* Delay in milliseconds before responding.
|
|
11
|
+
* @default 0
|
|
12
|
+
*/
|
|
13
|
+
delay: number;
|
|
14
|
+
/**
|
|
15
|
+
* Whether to log requests to the console.
|
|
16
|
+
* @default false
|
|
17
|
+
*/
|
|
18
|
+
logRequests: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Whether to log responses to the console.
|
|
21
|
+
* @default false
|
|
22
|
+
*/
|
|
23
|
+
logResponses: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Whether to remember requests. Remebered requests can be accessed via the `requests` property.
|
|
26
|
+
* @default false
|
|
27
|
+
*/
|
|
28
|
+
rememberRequests: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Function to call when a response is sent.
|
|
31
|
+
* @param response The response that was sent.
|
|
32
|
+
* @default undefined
|
|
33
|
+
*/
|
|
34
|
+
responseCallback?: (response: unknown) => void;
|
|
35
|
+
/**
|
|
36
|
+
* A prefix to add to the URL in log messages.
|
|
37
|
+
* @default /api/
|
|
38
|
+
*/
|
|
39
|
+
logUrlPrefix: string;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Represents a request intercepted by the `EasyNetworkStubWrapper`.
|
|
44
|
+
*/
|
|
45
|
+
export type StubRequestItem = {
|
|
46
|
+
/**
|
|
47
|
+
* The HTTP method of the request.
|
|
48
|
+
*/
|
|
49
|
+
method: HttpMethod;
|
|
50
|
+
/**
|
|
51
|
+
* The route of the request.
|
|
52
|
+
*/
|
|
53
|
+
route: string;
|
|
54
|
+
/**
|
|
55
|
+
* The request information provided by `easy-network-stub`.
|
|
56
|
+
*/
|
|
57
|
+
request: StubRequestInfo;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Represents the request information provided by `easy-network-stub`.
|
|
62
|
+
*/
|
|
63
|
+
export type StubRequestInfo<TRoute extends string = string, TBody = unknown> = Parameters<
|
|
64
|
+
RouteResponseCallback<TRoute, TBody>
|
|
65
|
+
>[0];
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* A strict version of `RouteResponseCallback` that allows for a responder to be passed as the first argument.
|
|
69
|
+
*/
|
|
70
|
+
export type StrictRouteResponseCallback<T, P extends string, R> = (
|
|
71
|
+
respondWith: R,
|
|
72
|
+
...args: Parameters<RouteResponseCallback<P, T>>
|
|
73
|
+
) => ErrorResponse<any> | Promise<ErrorResponse<any>>;
|
|
74
|
+
|
|
75
|
+
type _UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
|
|
76
|
+
type __StubResponder<T extends Record<number, unknown>, S> = S extends number
|
|
77
|
+
? [T[S]] extends [never]
|
|
78
|
+
? (statusCode: S) => ErrorResponse<T[S]>
|
|
79
|
+
: (statusCode: S, content: T[S]) => ErrorResponse<T[S]>
|
|
80
|
+
: never;
|
|
81
|
+
type _StubResponder<T extends Record<number, unknown>> = _UnionToIntersection<__StubResponder<T, keyof T>>;
|
|
82
|
+
const untypedResponder = (statusCode: number, content?: unknown) => ({ statusCode, content });
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Gets the responder function typed to a specific set of status codes.
|
|
86
|
+
* @returns The responder function.
|
|
87
|
+
*/
|
|
88
|
+
export function getStubResponder<T extends Record<number, unknown>>(): _StubResponder<T> {
|
|
89
|
+
return untypedResponder as _StubResponder<T>;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Wrapper for `EasyNetworkStub` that adds additional functionality.
|
|
94
|
+
*/
|
|
95
|
+
export class EasyNetworkStubWrapper {
|
|
96
|
+
private _requests: StubRequestItem[] = [];
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* The options for the wrapper.
|
|
100
|
+
*/
|
|
101
|
+
public readonly options: Readonly<EasyNetworkStubWrapperOptions>;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The requests that have been intercepted by the wrapper.
|
|
105
|
+
*/
|
|
106
|
+
public get requests(): ReadonlyArray<StubRequestItem> {
|
|
107
|
+
return this._requests;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
constructor(
|
|
111
|
+
public readonly wrappedStub: EasyNetworkStub,
|
|
112
|
+
options?: Partial<EasyNetworkStubWrapperOptions>,
|
|
113
|
+
) {
|
|
114
|
+
this.options = {
|
|
115
|
+
delay: options?.delay ?? 0,
|
|
116
|
+
logRequests: options?.logRequests ?? false,
|
|
117
|
+
logResponses: options?.logResponses ?? false,
|
|
118
|
+
rememberRequests: options?.rememberRequests ?? false,
|
|
119
|
+
responseCallback: options?.responseCallback,
|
|
120
|
+
logUrlPrefix: options?.logUrlPrefix ?? '/api/',
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Add a single api stub to your intercepted routes.
|
|
126
|
+
* @param method The http method that should be stubbed
|
|
127
|
+
* @param route The route that should be stubbed. Supports parameters in the form of {name:type}.
|
|
128
|
+
* @param response The callback in which you can process the request and reply with the stub. When a Promise is returned, the stub response will be delayed until it is resolved.
|
|
129
|
+
*/
|
|
130
|
+
public stub<Route extends string>(
|
|
131
|
+
method: HttpMethod,
|
|
132
|
+
route: Route,
|
|
133
|
+
response: RouteResponseCallback<Route, unknown>,
|
|
134
|
+
): void {
|
|
135
|
+
this.wrappedStub.stub<Route>(method, route, async (request) => {
|
|
136
|
+
if (this.options.delay > 0) await sleep(this.options.delay);
|
|
137
|
+
return await this.runRequest(method, route, response, request);
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* The stub2 method provides a way to set the type of the body that is sent.
|
|
143
|
+
* Due to restrictions in TypeScript (https://github.com/microsoft/TypeScript/issues/10571, https://github.com/Microsoft/TypeScript/pull/26349),
|
|
144
|
+
* We have to hacke a bit here. Calling stub2<T>() will return the stub method with a body of type T.
|
|
145
|
+
*
|
|
146
|
+
* Usage:
|
|
147
|
+
* ```
|
|
148
|
+
* .stub2<MyRequest>()('GET', '/api/test', ({ body }) => {
|
|
149
|
+
* // body is of type MyRequest
|
|
150
|
+
* });
|
|
151
|
+
* ```
|
|
152
|
+
* @returns The stub method with a body of type T.
|
|
153
|
+
*/
|
|
154
|
+
public stub2<T>(): <Route extends string>(
|
|
155
|
+
method: HttpMethod,
|
|
156
|
+
route: Route,
|
|
157
|
+
response: RouteResponseCallback<Route, T>,
|
|
158
|
+
) => void {
|
|
159
|
+
return <Route extends string>(method: HttpMethod, route: Route, response: RouteResponseCallback<Route, T>) => {
|
|
160
|
+
this.wrappedStub.stub2<T>()<Route>(method, route, async (request) => {
|
|
161
|
+
if (this.options.delay > 0) await sleep(this.options.delay);
|
|
162
|
+
return await this.runRequest(method, route, response, request);
|
|
163
|
+
});
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Reset the stubs and requests.
|
|
169
|
+
*/
|
|
170
|
+
public reset(): void {
|
|
171
|
+
this._requests = [];
|
|
172
|
+
this.wrappedStub['_config'].stubs = [];
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
private async runRequest<Route extends string, T>(
|
|
176
|
+
method: HttpMethod,
|
|
177
|
+
route: Route,
|
|
178
|
+
response: RouteResponseCallback<Route, T>,
|
|
179
|
+
request: StubRequestInfo<Route, T>,
|
|
180
|
+
): Promise<unknown> {
|
|
181
|
+
if (this.options.rememberRequests) {
|
|
182
|
+
this._requests.push({ method, route, request });
|
|
183
|
+
}
|
|
184
|
+
this.log('request', method, route, request, '#7FFFFF');
|
|
185
|
+
try {
|
|
186
|
+
const r = await response(request);
|
|
187
|
+
this.options.responseCallback?.(r);
|
|
188
|
+
this.log('response', method, route, r, '#7FFF8E');
|
|
189
|
+
return r;
|
|
190
|
+
} catch (error: unknown) {
|
|
191
|
+
this.options.responseCallback?.(error);
|
|
192
|
+
if (isHttpResponse(error) && error.statusCode >= 200 && error.statusCode < 300) {
|
|
193
|
+
this.log('response', method, route, error.content, '#7FFF8E');
|
|
194
|
+
throw error;
|
|
195
|
+
} else {
|
|
196
|
+
const isError = isHttpResponse(error) && error.statusCode >= 400 && error.statusCode < 600;
|
|
197
|
+
this.log('response', method, route, error, isError ? '#FF7F7F' : '7F7F7F');
|
|
198
|
+
throw error;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
private log(type: 'request' | 'response', method: HttpMethod, route: string, data: unknown, color: string) {
|
|
204
|
+
if (type === 'request' && !this.options.logRequests) return;
|
|
205
|
+
if (type === 'response' && !this.options.logResponses) return;
|
|
206
|
+
const typeDisplay = type === 'request' ? ' [req]' : '[resp]';
|
|
207
|
+
const strippedRoute = route.match(/[^?]*(?=\?)?/)?.[0];
|
|
208
|
+
console.log(`%c${typeDisplay} ${method}: ${this.options.logUrlPrefix}${strippedRoute}`, `color:${color};`, data);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Base class for generated easy network stubs.
|
|
214
|
+
*/
|
|
215
|
+
export class EasyNetworkStubBase {
|
|
216
|
+
protected readonly stubWrapper: EasyNetworkStubWrapper;
|
|
217
|
+
protected readonly ownsStubWrapper: boolean;
|
|
218
|
+
|
|
219
|
+
constructor(stub: EasyNetworkStub | EasyNetworkStubWrapper, options?: EasyNetworkStubWrapperOptions) {
|
|
220
|
+
this.stubWrapper = stub instanceof EasyNetworkStubWrapper ? stub : new EasyNetworkStubWrapper(stub, options);
|
|
221
|
+
this.ownsStubWrapper = !(stub instanceof EasyNetworkStubWrapper);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
public reset() {
|
|
225
|
+
if (this.ownsStubWrapper) {
|
|
226
|
+
this.stubWrapper.reset();
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export type EasyNetworkStubGroup<Group, GroupContainer> = Group & {
|
|
232
|
+
(stubActions: (stubs: Group) => void): GroupContainer;
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
236
|
+
declare interface GroupType<T> extends Function {
|
|
237
|
+
new (stubWrapper: EasyNetworkStubWrapper): T;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export function createEasyNetworkStubGroup<GroupContainer, Group>(
|
|
241
|
+
container: GroupContainer,
|
|
242
|
+
stubWrapper: EasyNetworkStubWrapper,
|
|
243
|
+
groupType: GroupType<Group>,
|
|
244
|
+
): EasyNetworkStubGroup<Group, GroupContainer> {
|
|
245
|
+
const group = new groupType(stubWrapper);
|
|
246
|
+
const groupFn = (stubActions: (stubs: Group) => void) => {
|
|
247
|
+
stubActions(group);
|
|
248
|
+
return container;
|
|
249
|
+
};
|
|
250
|
+
Object.setPrototypeOf(groupFn, groupType.prototype);
|
|
251
|
+
return Object.assign(groupFn, group);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function sleep(ms: number): Promise<void> {
|
|
255
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function isHttpResponse(value: unknown): value is ErrorResponse<unknown> {
|
|
259
|
+
return typeof value === 'object' && value !== null && 'statusCode' in value;
|
|
260
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.instanceType = exports.returnType = exports.parameters = exports.nonNullable = exports.extract = exports.exclude = exports.omit = exports.pick = exports.required = exports.partial = exports.readonly = exports.readonlyArray = exports.promise = exports.set = exports.map = exports.record = exports.array = exports.null_ = exports.undefined_ = exports.object = exports.symbol = exports.bigint = exports.boolean = exports.string = exports.number = exports.never = exports.void_ = exports.unknown = exports.any = void 0;
|
|
3
|
+
exports.blob = exports.instanceType = exports.returnType = exports.parameters = exports.nonNullable = exports.extract = exports.exclude = exports.omit = exports.pick = exports.required = exports.partial = exports.readonly = exports.readonlyArray = exports.promise = exports.set = exports.map = exports.record = exports.array = exports.null_ = exports.undefined_ = exports.object = exports.symbol = exports.bigint = exports.boolean = exports.string = exports.number = exports.never = exports.void_ = exports.unknown = exports.any = void 0;
|
|
4
4
|
const reference_1 = require("../nodes/reference");
|
|
5
5
|
exports.any = reference_1.tsReference.factory('any');
|
|
6
6
|
exports.unknown = reference_1.tsReference.factory('unknown');
|
|
@@ -31,3 +31,4 @@ exports.nonNullable = reference_1.tsReference.genericFactory('NonNullable');
|
|
|
31
31
|
exports.parameters = reference_1.tsReference.genericFactory('Parameters');
|
|
32
32
|
exports.returnType = reference_1.tsReference.genericFactory('ReturnType');
|
|
33
33
|
exports.instanceType = reference_1.tsReference.genericFactory('InstanceType');
|
|
34
|
+
exports.blob = reference_1.tsReference.factory('Blob');
|
|
@@ -203,7 +203,7 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
|
|
|
203
203
|
case 'number':
|
|
204
204
|
return 'number';
|
|
205
205
|
case 'string':
|
|
206
|
-
return
|
|
206
|
+
return this.getStringType(ctx, schema);
|
|
207
207
|
case 'null':
|
|
208
208
|
return 'null';
|
|
209
209
|
case 'unknown':
|
|
@@ -222,6 +222,12 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
|
|
|
222
222
|
return this.getAnyType(ctx);
|
|
223
223
|
}
|
|
224
224
|
}
|
|
225
|
+
getStringType(ctx, schema) {
|
|
226
|
+
if (schema.format === 'binary') {
|
|
227
|
+
return ast_1.ts.refs.blob();
|
|
228
|
+
}
|
|
229
|
+
return 'string';
|
|
230
|
+
}
|
|
225
231
|
getArrayType(ctx, schema) {
|
|
226
232
|
var _a;
|
|
227
233
|
return ast_1.ts.arrayType((_a = this.getType(ctx, schema.items)) !== null && _a !== void 0 ? _a : this.getAnyType(ctx), {
|
|
@@ -290,7 +296,7 @@ class DefaultTypeScriptModelGenerator extends file_generator_1.TypeScriptFileGen
|
|
|
290
296
|
return ast_1.ts.unionType(schema.type.map((x) => {
|
|
291
297
|
switch (x) {
|
|
292
298
|
case 'string':
|
|
293
|
-
return
|
|
299
|
+
return this.getStringType(ctx, schema);
|
|
294
300
|
case 'number':
|
|
295
301
|
case 'integer':
|
|
296
302
|
return 'number';
|
|
@@ -231,13 +231,13 @@ class DefaultTypeScriptAngularServiceGenerator extends file_generator_1.TypeScri
|
|
|
231
231
|
}
|
|
232
232
|
: null,
|
|
233
233
|
'',
|
|
234
|
-
(0, core_1.builderTemplate) `return ${ctx.refs.waitForResponse([responseModelType])}(${core_1.builderTemplate.indent `
|
|
235
|
-
this.http.request(rb.build({${core_1.builderTemplate.indent `
|
|
236
|
-
responseType: ${ast_1.ts.string(responseType)},
|
|
237
|
-
accept: ${ast_1.ts.string(accept)},
|
|
238
|
-
context,`}
|
|
239
|
-
})),
|
|
240
|
-
{${core_1.builderTemplate.indent `
|
|
234
|
+
(0, core_1.builderTemplate) `return ${ctx.refs.waitForResponse([responseModelType])}(${core_1.builderTemplate.indent `
|
|
235
|
+
this.http.request(rb.build({${core_1.builderTemplate.indent `
|
|
236
|
+
responseType: ${ast_1.ts.string(responseType)},
|
|
237
|
+
accept: ${ast_1.ts.string(accept)},
|
|
238
|
+
context,`}
|
|
239
|
+
})),
|
|
240
|
+
{${core_1.builderTemplate.indent `
|
|
241
241
|
errorResponseTypes: ${ast_1.ts.object({
|
|
242
242
|
members: [
|
|
243
243
|
...Object.entries(this.getEndpointStatusCodes(ctx, endpoint))
|
|
@@ -246,8 +246,8 @@ class DefaultTypeScriptAngularServiceGenerator extends file_generator_1.TypeScri
|
|
|
246
246
|
return ast_1.ts.property(key, { value: ast_1.ts.string(value.parser) });
|
|
247
247
|
}),
|
|
248
248
|
],
|
|
249
|
-
})}`}
|
|
250
|
-
}`}
|
|
249
|
+
})}`}
|
|
250
|
+
}`}
|
|
251
251
|
)`,
|
|
252
252
|
], '\n'),
|
|
253
253
|
});
|
|
@@ -97,15 +97,15 @@ class DefaultTypeScriptEasyNetworkStubGenerator extends file_generator_1.TypeScr
|
|
|
97
97
|
],
|
|
98
98
|
returnType: 'this',
|
|
99
99
|
body: (0, core_1.appendValueGroup)([
|
|
100
|
-
(0, core_1.builderTemplate) `this.stubWrapper.stub2<${requestType}>()(${core_1.builderTemplate.indent `
|
|
101
|
-
${ast_1.ts.string(endpoint.method.toUpperCase())},
|
|
102
|
-
${this.getStubClassName(ctx)}.${this.getEndpointPathPropertyName(ctx, endpoint)},
|
|
103
|
-
async (request) => {${core_1.builderTemplate.indent `
|
|
104
|
-
if (this.stubWrapper.options.rememberRequests) {${core_1.builderTemplate.indent `
|
|
105
|
-
this.${this.getEndpointRequestsFieldName(ctx, endpoint)}.push(request);`}
|
|
106
|
-
}
|
|
107
|
-
throw await response(${this.getResponderVariableName(ctx, endpoint)}, request);`}
|
|
108
|
-
}`}
|
|
100
|
+
(0, core_1.builderTemplate) `this.stubWrapper.stub2<${requestType}>()(${core_1.builderTemplate.indent `
|
|
101
|
+
${ast_1.ts.string(endpoint.method.toUpperCase())},
|
|
102
|
+
${this.getStubClassName(ctx)}.${this.getEndpointPathPropertyName(ctx, endpoint)},
|
|
103
|
+
async (request) => {${core_1.builderTemplate.indent `
|
|
104
|
+
if (this.stubWrapper.options.rememberRequests) {${core_1.builderTemplate.indent `
|
|
105
|
+
this.${this.getEndpointRequestsFieldName(ctx, endpoint)}.push(request);`}
|
|
106
|
+
}
|
|
107
|
+
throw await response(${this.getResponderVariableName(ctx, endpoint)}, request);`}
|
|
108
|
+
}`}
|
|
109
109
|
);`,
|
|
110
110
|
'return this;',
|
|
111
111
|
], '\n'),
|
|
@@ -136,7 +136,7 @@ class DefaultTypeScriptFetchClientGenerator extends file_generator_1.TypeScriptF
|
|
|
136
136
|
const method = this.getInterfaceEndpointMethod(ctx, endpoint);
|
|
137
137
|
method.accessModifier = 'public';
|
|
138
138
|
method.body = (0, core_1.appendValueGroup)([
|
|
139
|
-
(0, core_1.builderTemplate) `const url = new ${ctx.refs.urlBuilder()}(this.options.baseUrl)${core_1.builderTemplate.indent `
|
|
139
|
+
(0, core_1.builderTemplate) `const url = new ${ctx.refs.urlBuilder()}(this.options.baseUrl)${core_1.builderTemplate.indent `
|
|
140
140
|
.withPath(${ast_1.ts.string(endpoint.path)})${(b) => b.forEach(endpoint.parameters, (b, p) => {
|
|
141
141
|
switch (p.target) {
|
|
142
142
|
case 'path':
|
|
@@ -146,7 +146,7 @@ class DefaultTypeScriptFetchClientGenerator extends file_generator_1.TypeScriptF
|
|
|
146
146
|
b.append((0, core_1.builderTemplate) `\n.withQueryParam(${ast_1.ts.string(p.name)}, params.${(0, core_1.toCasing)(p.name, ctx.config.paramNameCasing)})`);
|
|
147
147
|
break;
|
|
148
148
|
}
|
|
149
|
-
})}
|
|
149
|
+
})}
|
|
150
150
|
.build();`}`,
|
|
151
151
|
(0, core_1.builderTemplate) `const response = (this.options.fetch ?? fetch)(url, ${ast_1.ts.object({
|
|
152
152
|
members: [
|