@goast/typescript 0.5.1-k6.3 → 0.5.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.
- package/LICENSE +21 -21
- package/README.md +52 -52
- package/assets/client/angular/angular-service.utils.ts +82 -82
- package/assets/client/angular/request-builder.ts +377 -377
- package/assets/client/fetch/fetch-client.utils.ts +52 -52
- package/assets/client/k6/request-builder.js +419 -419
- package/assets/client/k6/request-builder.ts +341 -341
- package/assets/stubs/easy-network-stub/easy-network-stub.utils.ts +259 -259
- package/esm/src/generators/models/models.d.ts +1 -1
- package/esm/src/generators/models/models.d.ts.map +1 -1
- package/esm/src/generators/services/angular-services/angular-service-generator.js +9 -9
- package/esm/src/generators/services/easy-network-stub/easy-network-stub-generator.js +9 -9
- package/esm/src/generators/services/fetch-clients/fetch-client-generator.js +2 -2
- package/esm/src/generators/services/k6-clients/k6-client-generator.js +11 -11
- package/package.json +1 -1
- package/script/src/generators/models/models.d.ts +1 -1
- package/script/src/generators/models/models.d.ts.map +1 -1
- package/script/src/generators/services/angular-services/angular-service-generator.js +9 -9
- package/script/src/generators/services/easy-network-stub/easy-network-stub-generator.js +9 -9
- package/script/src/generators/services/fetch-clients/fetch-client-generator.js +2 -2
- package/script/src/generators/services/k6-clients/k6-client-generator.js +11 -11
- package/src/mod.ts +7 -0
- package/src/src/assets.ts +9 -0
- package/src/src/ast/_index.ts +54 -0
- package/src/src/ast/common.ts +1 -0
- package/src/src/ast/index.ts +1 -0
- package/src/src/ast/node.ts +10 -0
- package/src/src/ast/nodes/argument.ts +40 -0
- package/src/src/ast/nodes/array-type.ts +49 -0
- package/src/src/ast/nodes/arrow-function.ts +91 -0
- package/src/src/ast/nodes/call.ts +55 -0
- package/src/src/ast/nodes/class.ts +133 -0
- package/src/src/ast/nodes/constructor-parameter.ts +70 -0
- package/src/src/ast/nodes/constructor.ts +77 -0
- package/src/src/ast/nodes/decorator.ts +76 -0
- package/src/src/ast/nodes/doc-tag.ts +291 -0
- package/src/src/ast/nodes/doc.ts +102 -0
- package/src/src/ast/nodes/enum-value.ts +72 -0
- package/src/src/ast/nodes/enum.ts +77 -0
- package/src/src/ast/nodes/export.ts +54 -0
- package/src/src/ast/nodes/function-type.ts +60 -0
- package/src/src/ast/nodes/function.ts +107 -0
- package/src/src/ast/nodes/generic-parameter.ts +71 -0
- package/src/src/ast/nodes/indexer.ts +60 -0
- package/src/src/ast/nodes/interface.ts +117 -0
- package/src/src/ast/nodes/intersection-type.ts +67 -0
- package/src/src/ast/nodes/lookup-type.ts +53 -0
- package/src/src/ast/nodes/method.ts +138 -0
- package/src/src/ast/nodes/object-type.ts +52 -0
- package/src/src/ast/nodes/object.ts +48 -0
- package/src/src/ast/nodes/parameter.ts +85 -0
- package/src/src/ast/nodes/property-accessor.ts +173 -0
- package/src/src/ast/nodes/property.ts +192 -0
- package/src/src/ast/nodes/reference.ts +194 -0
- package/src/src/ast/nodes/string.ts +49 -0
- package/src/src/ast/nodes/tuple.ts +53 -0
- package/src/src/ast/nodes/type-alias.ts +75 -0
- package/src/src/ast/nodes/typeof.ts +42 -0
- package/src/src/ast/nodes/types.ts +40 -0
- package/src/src/ast/nodes/union-type.ts +62 -0
- package/src/src/ast/nodes/variable.ts +73 -0
- package/src/src/ast/references/angular.ts +24 -0
- package/src/src/ast/references/easy-network-stub.ts +21 -0
- package/src/src/ast/references/index.ts +5 -0
- package/src/src/ast/references/k6.ts +8 -0
- package/src/src/ast/references/rxjs.ts +7 -0
- package/src/src/ast/references/typescript.ts +35 -0
- package/src/src/ast/utils/get-type-script-builder-options.ts +19 -0
- package/src/src/ast/utils/to-ts-node.ts +27 -0
- package/src/src/ast/utils/write-ts-generic-parameters.ts +25 -0
- package/src/src/ast/utils/write-ts-members.ts +28 -0
- package/src/src/ast/utils/write-ts-nodes.ts +35 -0
- package/src/src/ast/utils/write-ts-parameters.ts +25 -0
- package/src/src/common-results.ts +26 -0
- package/src/src/config.ts +89 -0
- package/src/src/file-builder.ts +136 -0
- package/src/src/generators/file-generator.ts +15 -0
- package/src/src/generators/index.ts +6 -0
- package/src/src/generators/models/index.ts +3 -0
- package/src/src/generators/models/model-generator.ts +469 -0
- package/src/src/generators/models/models-generator.ts +108 -0
- package/src/src/generators/models/models.ts +121 -0
- package/src/src/generators/services/angular-services/angular-service-generator.ts +406 -0
- package/src/src/generators/services/angular-services/angular-services-generator.ts +295 -0
- package/src/src/generators/services/angular-services/index.ts +3 -0
- package/src/src/generators/services/angular-services/models.ts +277 -0
- package/src/src/generators/services/angular-services/refs.ts +54 -0
- package/src/src/generators/services/easy-network-stub/easy-network-stub-generator.ts +242 -0
- package/src/src/generators/services/easy-network-stub/easy-network-stubs-generator.ts +211 -0
- package/src/src/generators/services/easy-network-stub/index.ts +3 -0
- package/src/src/generators/services/easy-network-stub/models.ts +124 -0
- package/src/src/generators/services/easy-network-stub/refs.ts +45 -0
- package/src/src/generators/services/fetch-clients/fetch-client-generator.ts +267 -0
- package/src/src/generators/services/fetch-clients/fetch-clients-generator.ts +163 -0
- package/src/src/generators/services/fetch-clients/index.ts +3 -0
- package/src/src/generators/services/fetch-clients/models.ts +130 -0
- package/src/src/generators/services/fetch-clients/refs.ts +21 -0
- package/src/src/generators/services/k6-clients/index.ts +3 -0
- package/src/src/generators/services/k6-clients/k6-client-generator.ts +465 -0
- package/src/src/generators/services/k6-clients/k6-clients-generator.ts +171 -0
- package/src/src/generators/services/k6-clients/models.ts +247 -0
- package/src/src/generators/services/k6-clients/refs.ts +16 -0
- package/src/src/import-collection.ts +236 -0
- package/src/src/utils.ts +72 -0
|
@@ -1,259 +1,259 @@
|
|
|
1
|
-
import type { EasyNetworkStub } from 'easy-network-stub';
|
|
2
|
-
|
|
3
|
-
import type { ErrorResponse, HttpMethod, RouteResponseCallback } 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] ? (statusCode: S) => ErrorResponse<T[S]>
|
|
78
|
-
: (statusCode: S, content: T[S]) => ErrorResponse<T[S]>
|
|
79
|
-
: never;
|
|
80
|
-
type _StubResponder<T extends Record<number, unknown>> = _UnionToIntersection<__StubResponder<T, keyof T>>;
|
|
81
|
-
const untypedResponder = (statusCode: number, content?: unknown) => ({ statusCode, content });
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Gets the responder function typed to a specific set of status codes.
|
|
85
|
-
* @returns The responder function.
|
|
86
|
-
*/
|
|
87
|
-
export function getStubResponder<T extends Record<number, unknown>>(): _StubResponder<T> {
|
|
88
|
-
return untypedResponder as _StubResponder<T>;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Wrapper for `EasyNetworkStub` that adds additional functionality.
|
|
93
|
-
*/
|
|
94
|
-
export class EasyNetworkStubWrapper {
|
|
95
|
-
private _requests: StubRequestItem[] = [];
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* The options for the wrapper.
|
|
99
|
-
*/
|
|
100
|
-
public readonly options: Readonly<EasyNetworkStubWrapperOptions>;
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* The requests that have been intercepted by the wrapper.
|
|
104
|
-
*/
|
|
105
|
-
public get requests(): ReadonlyArray<StubRequestItem> {
|
|
106
|
-
return this._requests;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
constructor(
|
|
110
|
-
public readonly wrappedStub: EasyNetworkStub,
|
|
111
|
-
options?: Partial<EasyNetworkStubWrapperOptions>,
|
|
112
|
-
) {
|
|
113
|
-
this.options = {
|
|
114
|
-
delay: options?.delay ?? 0,
|
|
115
|
-
logRequests: options?.logRequests ?? false,
|
|
116
|
-
logResponses: options?.logResponses ?? false,
|
|
117
|
-
rememberRequests: options?.rememberRequests ?? false,
|
|
118
|
-
responseCallback: options?.responseCallback,
|
|
119
|
-
logUrlPrefix: options?.logUrlPrefix ?? '/api/',
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Add a single api stub to your intercepted routes.
|
|
125
|
-
* @param method The http method that should be stubbed
|
|
126
|
-
* @param route The route that should be stubbed. Supports parameters in the form of {name:type}.
|
|
127
|
-
* @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.
|
|
128
|
-
*/
|
|
129
|
-
public stub<Route extends string>(
|
|
130
|
-
method: HttpMethod,
|
|
131
|
-
route: Route,
|
|
132
|
-
response: RouteResponseCallback<Route, unknown>,
|
|
133
|
-
): void {
|
|
134
|
-
this.wrappedStub.stub<Route>(method, route, async (request) => {
|
|
135
|
-
if (this.options.delay > 0) await sleep(this.options.delay);
|
|
136
|
-
return await this.runRequest(method, route, response, request);
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* The stub2 method provides a way to set the type of the body that is sent.
|
|
142
|
-
* Due to restrictions in TypeScript (https://github.com/microsoft/TypeScript/issues/10571, https://github.com/Microsoft/TypeScript/pull/26349),
|
|
143
|
-
* We have to hacke a bit here. Calling stub2<T>() will return the stub method with a body of type T.
|
|
144
|
-
*
|
|
145
|
-
* Usage:
|
|
146
|
-
* ```
|
|
147
|
-
* .stub2<MyRequest>()('GET', '/api/test', ({ body }) => {
|
|
148
|
-
* // body is of type MyRequest
|
|
149
|
-
* });
|
|
150
|
-
* ```
|
|
151
|
-
* @returns The stub method with a body of type T.
|
|
152
|
-
*/
|
|
153
|
-
public stub2<T>(): <Route extends string>(
|
|
154
|
-
method: HttpMethod,
|
|
155
|
-
route: Route,
|
|
156
|
-
response: RouteResponseCallback<Route, T>,
|
|
157
|
-
) => void {
|
|
158
|
-
return <Route extends string>(method: HttpMethod, route: Route, response: RouteResponseCallback<Route, T>) => {
|
|
159
|
-
this.wrappedStub.stub2<T>()<Route>(method, route, async (request) => {
|
|
160
|
-
if (this.options.delay > 0) await sleep(this.options.delay);
|
|
161
|
-
return await this.runRequest(method, route, response, request);
|
|
162
|
-
});
|
|
163
|
-
};
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* Reset the stubs and requests.
|
|
168
|
-
*/
|
|
169
|
-
public reset(): void {
|
|
170
|
-
this._requests = [];
|
|
171
|
-
this.wrappedStub['_config'].stubs = [];
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
private async runRequest<Route extends string, T>(
|
|
175
|
-
method: HttpMethod,
|
|
176
|
-
route: Route,
|
|
177
|
-
response: RouteResponseCallback<Route, T>,
|
|
178
|
-
request: StubRequestInfo<Route, T>,
|
|
179
|
-
): Promise<unknown> {
|
|
180
|
-
if (this.options.rememberRequests) {
|
|
181
|
-
this._requests.push({ method, route, request });
|
|
182
|
-
}
|
|
183
|
-
this.log('request', method, route, request, '#7FFFFF');
|
|
184
|
-
try {
|
|
185
|
-
const r = await response(request);
|
|
186
|
-
this.options.responseCallback?.(r);
|
|
187
|
-
this.log('response', method, route, r, '#7FFF8E');
|
|
188
|
-
return r;
|
|
189
|
-
} catch (error: unknown) {
|
|
190
|
-
this.options.responseCallback?.(error);
|
|
191
|
-
if (isHttpResponse(error) && error.statusCode >= 200 && error.statusCode < 300) {
|
|
192
|
-
this.log('response', method, route, error.content, '#7FFF8E');
|
|
193
|
-
throw error;
|
|
194
|
-
} else {
|
|
195
|
-
const isError = isHttpResponse(error) && error.statusCode >= 400 && error.statusCode < 600;
|
|
196
|
-
this.log('response', method, route, error, isError ? '#FF7F7F' : '7F7F7F');
|
|
197
|
-
throw error;
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
private log(type: 'request' | 'response', method: HttpMethod, route: string, data: unknown, color: string) {
|
|
203
|
-
if (type === 'request' && !this.options.logRequests) return;
|
|
204
|
-
if (type === 'response' && !this.options.logResponses) return;
|
|
205
|
-
const typeDisplay = type === 'request' ? ' [req]' : '[resp]';
|
|
206
|
-
const strippedRoute = route.match(/[^?]*(?=\?)?/)?.[0];
|
|
207
|
-
console.log(`%c${typeDisplay} ${method}: ${this.options.logUrlPrefix}${strippedRoute}`, `color:${color};`, data);
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Base class for generated easy network stubs.
|
|
213
|
-
*/
|
|
214
|
-
export class EasyNetworkStubBase {
|
|
215
|
-
protected readonly stubWrapper: EasyNetworkStubWrapper;
|
|
216
|
-
protected readonly ownsStubWrapper: boolean;
|
|
217
|
-
|
|
218
|
-
constructor(stub: EasyNetworkStub | EasyNetworkStubWrapper, options?: EasyNetworkStubWrapperOptions) {
|
|
219
|
-
this.stubWrapper = stub instanceof EasyNetworkStubWrapper ? stub : new EasyNetworkStubWrapper(stub, options);
|
|
220
|
-
this.ownsStubWrapper = !(stub instanceof EasyNetworkStubWrapper);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
public reset() {
|
|
224
|
-
if (this.ownsStubWrapper) {
|
|
225
|
-
this.stubWrapper.reset();
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
export type EasyNetworkStubGroup<Group, GroupContainer> = Group & {
|
|
231
|
-
(stubActions: (stubs: Group) => void): GroupContainer;
|
|
232
|
-
};
|
|
233
|
-
|
|
234
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
235
|
-
declare interface GroupType<T> extends Function {
|
|
236
|
-
new (stubWrapper: EasyNetworkStubWrapper): T;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
export function createEasyNetworkStubGroup<GroupContainer, Group>(
|
|
240
|
-
container: GroupContainer,
|
|
241
|
-
stubWrapper: EasyNetworkStubWrapper,
|
|
242
|
-
groupType: GroupType<Group>,
|
|
243
|
-
): EasyNetworkStubGroup<Group, GroupContainer> {
|
|
244
|
-
const group = new groupType(stubWrapper);
|
|
245
|
-
const groupFn = (stubActions: (stubs: Group) => void) => {
|
|
246
|
-
stubActions(group);
|
|
247
|
-
return container;
|
|
248
|
-
};
|
|
249
|
-
Object.setPrototypeOf(groupFn, groupType.prototype);
|
|
250
|
-
return Object.assign(groupFn, group);
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
function sleep(ms: number): Promise<void> {
|
|
254
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
function isHttpResponse(value: unknown): value is ErrorResponse<unknown> {
|
|
258
|
-
return typeof value === 'object' && value !== null && 'statusCode' in value;
|
|
259
|
-
}
|
|
1
|
+
import type { EasyNetworkStub } from 'easy-network-stub';
|
|
2
|
+
|
|
3
|
+
import type { ErrorResponse, HttpMethod, RouteResponseCallback } 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] ? (statusCode: S) => ErrorResponse<T[S]>
|
|
78
|
+
: (statusCode: S, content: T[S]) => ErrorResponse<T[S]>
|
|
79
|
+
: never;
|
|
80
|
+
type _StubResponder<T extends Record<number, unknown>> = _UnionToIntersection<__StubResponder<T, keyof T>>;
|
|
81
|
+
const untypedResponder = (statusCode: number, content?: unknown) => ({ statusCode, content });
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Gets the responder function typed to a specific set of status codes.
|
|
85
|
+
* @returns The responder function.
|
|
86
|
+
*/
|
|
87
|
+
export function getStubResponder<T extends Record<number, unknown>>(): _StubResponder<T> {
|
|
88
|
+
return untypedResponder as _StubResponder<T>;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Wrapper for `EasyNetworkStub` that adds additional functionality.
|
|
93
|
+
*/
|
|
94
|
+
export class EasyNetworkStubWrapper {
|
|
95
|
+
private _requests: StubRequestItem[] = [];
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The options for the wrapper.
|
|
99
|
+
*/
|
|
100
|
+
public readonly options: Readonly<EasyNetworkStubWrapperOptions>;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The requests that have been intercepted by the wrapper.
|
|
104
|
+
*/
|
|
105
|
+
public get requests(): ReadonlyArray<StubRequestItem> {
|
|
106
|
+
return this._requests;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
constructor(
|
|
110
|
+
public readonly wrappedStub: EasyNetworkStub,
|
|
111
|
+
options?: Partial<EasyNetworkStubWrapperOptions>,
|
|
112
|
+
) {
|
|
113
|
+
this.options = {
|
|
114
|
+
delay: options?.delay ?? 0,
|
|
115
|
+
logRequests: options?.logRequests ?? false,
|
|
116
|
+
logResponses: options?.logResponses ?? false,
|
|
117
|
+
rememberRequests: options?.rememberRequests ?? false,
|
|
118
|
+
responseCallback: options?.responseCallback,
|
|
119
|
+
logUrlPrefix: options?.logUrlPrefix ?? '/api/',
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Add a single api stub to your intercepted routes.
|
|
125
|
+
* @param method The http method that should be stubbed
|
|
126
|
+
* @param route The route that should be stubbed. Supports parameters in the form of {name:type}.
|
|
127
|
+
* @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.
|
|
128
|
+
*/
|
|
129
|
+
public stub<Route extends string>(
|
|
130
|
+
method: HttpMethod,
|
|
131
|
+
route: Route,
|
|
132
|
+
response: RouteResponseCallback<Route, unknown>,
|
|
133
|
+
): void {
|
|
134
|
+
this.wrappedStub.stub<Route>(method, route, async (request) => {
|
|
135
|
+
if (this.options.delay > 0) await sleep(this.options.delay);
|
|
136
|
+
return await this.runRequest(method, route, response, request);
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* The stub2 method provides a way to set the type of the body that is sent.
|
|
142
|
+
* Due to restrictions in TypeScript (https://github.com/microsoft/TypeScript/issues/10571, https://github.com/Microsoft/TypeScript/pull/26349),
|
|
143
|
+
* We have to hacke a bit here. Calling stub2<T>() will return the stub method with a body of type T.
|
|
144
|
+
*
|
|
145
|
+
* Usage:
|
|
146
|
+
* ```
|
|
147
|
+
* .stub2<MyRequest>()('GET', '/api/test', ({ body }) => {
|
|
148
|
+
* // body is of type MyRequest
|
|
149
|
+
* });
|
|
150
|
+
* ```
|
|
151
|
+
* @returns The stub method with a body of type T.
|
|
152
|
+
*/
|
|
153
|
+
public stub2<T>(): <Route extends string>(
|
|
154
|
+
method: HttpMethod,
|
|
155
|
+
route: Route,
|
|
156
|
+
response: RouteResponseCallback<Route, T>,
|
|
157
|
+
) => void {
|
|
158
|
+
return <Route extends string>(method: HttpMethod, route: Route, response: RouteResponseCallback<Route, T>) => {
|
|
159
|
+
this.wrappedStub.stub2<T>()<Route>(method, route, async (request) => {
|
|
160
|
+
if (this.options.delay > 0) await sleep(this.options.delay);
|
|
161
|
+
return await this.runRequest(method, route, response, request);
|
|
162
|
+
});
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Reset the stubs and requests.
|
|
168
|
+
*/
|
|
169
|
+
public reset(): void {
|
|
170
|
+
this._requests = [];
|
|
171
|
+
this.wrappedStub['_config'].stubs = [];
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
private async runRequest<Route extends string, T>(
|
|
175
|
+
method: HttpMethod,
|
|
176
|
+
route: Route,
|
|
177
|
+
response: RouteResponseCallback<Route, T>,
|
|
178
|
+
request: StubRequestInfo<Route, T>,
|
|
179
|
+
): Promise<unknown> {
|
|
180
|
+
if (this.options.rememberRequests) {
|
|
181
|
+
this._requests.push({ method, route, request });
|
|
182
|
+
}
|
|
183
|
+
this.log('request', method, route, request, '#7FFFFF');
|
|
184
|
+
try {
|
|
185
|
+
const r = await response(request);
|
|
186
|
+
this.options.responseCallback?.(r);
|
|
187
|
+
this.log('response', method, route, r, '#7FFF8E');
|
|
188
|
+
return r;
|
|
189
|
+
} catch (error: unknown) {
|
|
190
|
+
this.options.responseCallback?.(error);
|
|
191
|
+
if (isHttpResponse(error) && error.statusCode >= 200 && error.statusCode < 300) {
|
|
192
|
+
this.log('response', method, route, error.content, '#7FFF8E');
|
|
193
|
+
throw error;
|
|
194
|
+
} else {
|
|
195
|
+
const isError = isHttpResponse(error) && error.statusCode >= 400 && error.statusCode < 600;
|
|
196
|
+
this.log('response', method, route, error, isError ? '#FF7F7F' : '7F7F7F');
|
|
197
|
+
throw error;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
private log(type: 'request' | 'response', method: HttpMethod, route: string, data: unknown, color: string) {
|
|
203
|
+
if (type === 'request' && !this.options.logRequests) return;
|
|
204
|
+
if (type === 'response' && !this.options.logResponses) return;
|
|
205
|
+
const typeDisplay = type === 'request' ? ' [req]' : '[resp]';
|
|
206
|
+
const strippedRoute = route.match(/[^?]*(?=\?)?/)?.[0];
|
|
207
|
+
console.log(`%c${typeDisplay} ${method}: ${this.options.logUrlPrefix}${strippedRoute}`, `color:${color};`, data);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Base class for generated easy network stubs.
|
|
213
|
+
*/
|
|
214
|
+
export class EasyNetworkStubBase {
|
|
215
|
+
protected readonly stubWrapper: EasyNetworkStubWrapper;
|
|
216
|
+
protected readonly ownsStubWrapper: boolean;
|
|
217
|
+
|
|
218
|
+
constructor(stub: EasyNetworkStub | EasyNetworkStubWrapper, options?: EasyNetworkStubWrapperOptions) {
|
|
219
|
+
this.stubWrapper = stub instanceof EasyNetworkStubWrapper ? stub : new EasyNetworkStubWrapper(stub, options);
|
|
220
|
+
this.ownsStubWrapper = !(stub instanceof EasyNetworkStubWrapper);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
public reset() {
|
|
224
|
+
if (this.ownsStubWrapper) {
|
|
225
|
+
this.stubWrapper.reset();
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export type EasyNetworkStubGroup<Group, GroupContainer> = Group & {
|
|
231
|
+
(stubActions: (stubs: Group) => void): GroupContainer;
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
235
|
+
declare interface GroupType<T> extends Function {
|
|
236
|
+
new (stubWrapper: EasyNetworkStubWrapper): T;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export function createEasyNetworkStubGroup<GroupContainer, Group>(
|
|
240
|
+
container: GroupContainer,
|
|
241
|
+
stubWrapper: EasyNetworkStubWrapper,
|
|
242
|
+
groupType: GroupType<Group>,
|
|
243
|
+
): EasyNetworkStubGroup<Group, GroupContainer> {
|
|
244
|
+
const group = new groupType(stubWrapper);
|
|
245
|
+
const groupFn = (stubActions: (stubs: Group) => void) => {
|
|
246
|
+
stubActions(group);
|
|
247
|
+
return container;
|
|
248
|
+
};
|
|
249
|
+
Object.setPrototypeOf(groupFn, groupType.prototype);
|
|
250
|
+
return Object.assign(groupFn, group);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function sleep(ms: number): Promise<void> {
|
|
254
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function isHttpResponse(value: unknown): value is ErrorResponse<unknown> {
|
|
258
|
+
return typeof value === 'object' && value !== null && 'statusCode' in value;
|
|
259
|
+
}
|
|
@@ -2,7 +2,7 @@ import type { ApiSchema, DefaultGenerationProviderConfig, ExtendedStringCasing,
|
|
|
2
2
|
import { ts } from '../../ast/index.js';
|
|
3
3
|
import type { TypeScriptComponentOutput } from '../../common-results.js';
|
|
4
4
|
import { type TypeScriptGeneratorConfig } from '../../config.js';
|
|
5
|
-
import { TypeScriptFileBuilder } from '../../file-builder.js';
|
|
5
|
+
import type { TypeScriptFileBuilder } from '../../file-builder.js';
|
|
6
6
|
export type TypeScriptModelGeneratorConfig = TypeScriptGeneratorConfig & {
|
|
7
7
|
/**
|
|
8
8
|
* Determines how to generate enums.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"models.d.ts","sourceRoot":"","sources":["../../../../src/src/generators/models/models.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,SAAS,EACT,+BAA+B,EAC/B,oBAAoB,EACpB,QAAQ,EACR,uCAAuC,EACxC,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,EAAE,EAAE,MAAM,oBAAoB,CAAC;AACxC,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,yBAAyB,CAAC;AACzE,OAAO,EAAoC,KAAK,yBAAyB,EAAE,MAAM,iBAAiB,CAAC;AACnG,OAAO,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"models.d.ts","sourceRoot":"","sources":["../../../../src/src/generators/models/models.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,SAAS,EACT,+BAA+B,EAC/B,oBAAoB,EACpB,QAAQ,EACR,uCAAuC,EACxC,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,EAAE,EAAE,MAAM,oBAAoB,CAAC;AACxC,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,yBAAyB,CAAC;AACzE,OAAO,EAAoC,KAAK,yBAAyB,EAAE,MAAM,iBAAiB,CAAC;AACnG,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAEnE,MAAM,MAAM,8BAA8B,GAAG,yBAAyB,GAAG;IACvE;;;;;OAKG;IACH,cAAc,EAAE,OAAO,GAAG,aAAa,CAAC;IAExC;;;OAGG;IACH,cAAc,EAAE,OAAO,CAAC;IAExB;;;OAGG;IACH,oBAAoB,EAAE,OAAO,CAAC;IAE9B;;;OAGG;IACH,SAAS,EAAE,MAAM,CAAC;IAElB;;;OAGG;IACH,mBAAmB,EAAE,QAAQ,CAAC,oBAAoB,CAAC,CAAC;IAEpD;;;;;OAKG;IACH,eAAe,EAAE,MAAM,GAAG,kBAAkB,CAAC;IAE7C;;;OAGG;IACH,2BAA2B,EAAE,oBAAoB,CAAC;IAElD;;;OAGG;IACH,gBAAgB,EAAE,MAAM,EAAE,CAAC,IAAI,CAAC,qBAAqB,CAAC,CAAC;CACxD,CAAC;AAEF,MAAM,MAAM,+BAA+B,GAAG,8BAA8B,GAAG;IAC7E;;;OAGG;IACH,eAAe,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;CACnC,CAAC;AAEF,eAAO,MAAM,qCAAqC,EAAE,+BAA+B,CAAC,8BAA8B,CAWjH,CAAC;AAEF,eAAO,MAAM,sCAAsC,EAAE,+BAA+B,CAAC,+BAA+B,CAKjH,CAAC;AAGJ,MAAM,MAAM,8BAA8B,GAAG,EAAE,CAAC;AAEhD,MAAM,MAAM,+BAA+B,GAAG;IAC5C,UAAU,EAAE;QACV,MAAM,EAAE;YACN,CAAC,QAAQ,EAAE,MAAM,GAAG,8BAA8B,CAAC;SACpD,CAAC;QACF,UAAU,EAAE;YACV,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;SAC1B,CAAC;KACH,CAAC;CACH,CAAC;AAEF,MAAM,MAAM,8BAA8B,GAAG,yBAAyB,CAAC;AAEvE,MAAM,MAAM,gCAAgC,GAAG,uCAAuC,CACpF,8BAA8B,EAC9B,+BAA+B,EAC/B,+BAA+B,EAC/B,8BAA8B,CAC/B,CAAC;AAEF,MAAM,MAAM,+BAA+B,GAAG,gCAAgC,GAAG;IAC/E,MAAM,EAAE,SAAS,CAAC;CACnB,CAAC"}
|
|
@@ -229,13 +229,13 @@ export class DefaultTypeScriptAngularServiceGenerator extends TypeScriptFileGene
|
|
|
229
229
|
], '\n'), { multiline: true })
|
|
230
230
|
: null,
|
|
231
231
|
'',
|
|
232
|
-
s `return ${ctx.refs.waitForResponse([responseModelType])}(${s.indent `
|
|
233
|
-
this.http.request(rb.build({${s.indent `
|
|
234
|
-
responseType: ${ts.string(responseType)},
|
|
235
|
-
accept: ${ts.string(accept)},
|
|
236
|
-
context,`}
|
|
237
|
-
})),
|
|
238
|
-
{${s.indent `
|
|
232
|
+
s `return ${ctx.refs.waitForResponse([responseModelType])}(${s.indent `
|
|
233
|
+
this.http.request(rb.build({${s.indent `
|
|
234
|
+
responseType: ${ts.string(responseType)},
|
|
235
|
+
accept: ${ts.string(accept)},
|
|
236
|
+
context,`}
|
|
237
|
+
})),
|
|
238
|
+
{${s.indent `
|
|
239
239
|
errorResponseTypes: ${ts.object({
|
|
240
240
|
members: [
|
|
241
241
|
...Object.entries(this.getEndpointStatusCodes(ctx, endpoint))
|
|
@@ -244,8 +244,8 @@ export class DefaultTypeScriptAngularServiceGenerator extends TypeScriptFileGene
|
|
|
244
244
|
return ts.property(key, { value: ts.string(value.parser) });
|
|
245
245
|
}),
|
|
246
246
|
],
|
|
247
|
-
})}`}
|
|
248
|
-
}`}
|
|
247
|
+
})}`}
|
|
248
|
+
}`}
|
|
249
249
|
)`,
|
|
250
250
|
], '\n'),
|
|
251
251
|
});
|
|
@@ -94,15 +94,15 @@ export class DefaultTypeScriptEasyNetworkStubGenerator extends TypeScriptFileGen
|
|
|
94
94
|
],
|
|
95
95
|
returnType: 'this',
|
|
96
96
|
body: appendValueGroup([
|
|
97
|
-
s `this.stubWrapper.stub2<${requestType}>()(${s.indent `
|
|
98
|
-
${ts.string(endpoint.method.toUpperCase())},
|
|
99
|
-
${this.getStubClassName(ctx)}.${this.getEndpointPathPropertyName(ctx, endpoint)},
|
|
100
|
-
async (request) => {${s.indent `
|
|
101
|
-
if (this.stubWrapper.options.rememberRequests) {${s.indent `
|
|
102
|
-
this.${this.getEndpointRequestsFieldName(ctx, endpoint)}.push(request);`}
|
|
103
|
-
}
|
|
104
|
-
throw await response(${this.getResponderVariableName(ctx, endpoint)}, request);`}
|
|
105
|
-
}`}
|
|
97
|
+
s `this.stubWrapper.stub2<${requestType}>()(${s.indent `
|
|
98
|
+
${ts.string(endpoint.method.toUpperCase())},
|
|
99
|
+
${this.getStubClassName(ctx)}.${this.getEndpointPathPropertyName(ctx, endpoint)},
|
|
100
|
+
async (request) => {${s.indent `
|
|
101
|
+
if (this.stubWrapper.options.rememberRequests) {${s.indent `
|
|
102
|
+
this.${this.getEndpointRequestsFieldName(ctx, endpoint)}.push(request);`}
|
|
103
|
+
}
|
|
104
|
+
throw await response(${this.getResponderVariableName(ctx, endpoint)}, request);`}
|
|
105
|
+
}`}
|
|
106
106
|
);`,
|
|
107
107
|
'return this;',
|
|
108
108
|
], '\n'),
|
|
@@ -131,7 +131,7 @@ export class DefaultTypeScriptFetchClientGenerator extends TypeScriptFileGenerat
|
|
|
131
131
|
const method = this.getInterfaceEndpointMethod(ctx, endpoint);
|
|
132
132
|
method.accessModifier = 'public';
|
|
133
133
|
method.body = appendValueGroup([
|
|
134
|
-
s `const url = new ${ctx.refs.urlBuilder()}(this.options.baseUrl)${s.indent `
|
|
134
|
+
s `const url = new ${ctx.refs.urlBuilder()}(this.options.baseUrl)${s.indent `
|
|
135
135
|
.withPath(${ts.string(endpoint.path)})${(b) => b.forEach(endpoint.parameters, (b, p) => {
|
|
136
136
|
switch (p.target) {
|
|
137
137
|
case 'path':
|
|
@@ -141,7 +141,7 @@ export class DefaultTypeScriptFetchClientGenerator extends TypeScriptFileGenerat
|
|
|
141
141
|
b.append(s `\n.withQueryParam(${ts.string(p.name)}, params.${toCasing(p.name, ctx.config.paramNameCasing)})`);
|
|
142
142
|
break;
|
|
143
143
|
}
|
|
144
|
-
})}
|
|
144
|
+
})}
|
|
145
145
|
.build();`}`,
|
|
146
146
|
s `const response = (this.options.fetch ?? fetch)(url, ${ts.object({
|
|
147
147
|
members: [
|
|
@@ -229,7 +229,7 @@ export class DefaultTypeScriptK6ClientGenerator extends TypeScriptFileGenerator
|
|
|
229
229
|
ts.constructorParameter('rootUrl', { type: isJs ? undefined : rootUrlType }),
|
|
230
230
|
ts.constructorParameter('defaultK6ParamsFactory', { type: isJs ? undefined : paramType, optional: true }),
|
|
231
231
|
],
|
|
232
|
-
body: s `this.rootUrl = rootUrl;
|
|
232
|
+
body: s `this.rootUrl = rootUrl;
|
|
233
233
|
this._defaultK6ParamsFactory = defaultK6ParamsFactory;`,
|
|
234
234
|
}),
|
|
235
235
|
...ctx.service.endpoints.map((e) => this.getEndpointMethod(ctx, e)),
|
|
@@ -291,17 +291,17 @@ export class DefaultTypeScriptK6ClientGenerator extends TypeScriptFileGenerator
|
|
|
291
291
|
: null,
|
|
292
292
|
'',
|
|
293
293
|
ctx.config.language === 'javascript'
|
|
294
|
-
? s `return /** @type {${returnType}} */ (${s.indent `
|
|
295
|
-
${ctx.config.async ? 'await rb.buildAsync' : 'rb.build'}({${s.indent `
|
|
296
|
-
accept: ${ts.string(accept)},
|
|
297
|
-
params: this.getK6Params(k6Params),`}
|
|
298
|
-
})`}
|
|
294
|
+
? s `return /** @type {${returnType}} */ (${s.indent `
|
|
295
|
+
${ctx.config.async ? 'await rb.buildAsync' : 'rb.build'}({${s.indent `
|
|
296
|
+
accept: ${ts.string(accept)},
|
|
297
|
+
params: this.getK6Params(k6Params),`}
|
|
298
|
+
})`}
|
|
299
299
|
);`
|
|
300
|
-
: s `return (${s.indent `
|
|
301
|
-
${ctx.config.async ? 'await rb.buildAsync' : 'rb.build'}({${s.indent `
|
|
302
|
-
accept: ${ts.string(accept)},
|
|
303
|
-
params: this.getK6Params(k6Params),`}
|
|
304
|
-
})`}
|
|
300
|
+
: s `return (${s.indent `
|
|
301
|
+
${ctx.config.async ? 'await rb.buildAsync' : 'rb.build'}({${s.indent `
|
|
302
|
+
accept: ${ts.string(accept)},
|
|
303
|
+
params: this.getK6Params(k6Params),`}
|
|
304
|
+
})`}
|
|
305
305
|
) as unknown as ${returnType};`,
|
|
306
306
|
], '\n'),
|
|
307
307
|
});
|