@goast/typescript 0.7.5 → 0.8.0-beta.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 +448 -448
- package/assets/client/k6/request-builder.ts +366 -366
- package/assets/stubs/easy-network-stub/easy-network-stub.utils.ts +257 -257
- 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 +4 -4
- package/esm/src/generators/services/k6-clients/k6-client-generator.js +11 -11
- package/package.json +2 -2
- 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 +4 -4
- package/script/src/generators/services/k6-clients/k6-client-generator.js +11 -11
- package/src/mod.ts +0 -7
- package/src/src/assets.ts +0 -9
- package/src/src/ast/_index.ts +0 -54
- package/src/src/ast/common.ts +0 -1
- package/src/src/ast/index.ts +0 -1
- package/src/src/ast/node.ts +0 -10
- package/src/src/ast/nodes/argument.ts +0 -40
- package/src/src/ast/nodes/array-type.ts +0 -49
- package/src/src/ast/nodes/arrow-function.ts +0 -91
- package/src/src/ast/nodes/call.ts +0 -55
- package/src/src/ast/nodes/class.ts +0 -133
- package/src/src/ast/nodes/constructor-parameter.ts +0 -70
- package/src/src/ast/nodes/constructor.ts +0 -77
- package/src/src/ast/nodes/decorator.ts +0 -76
- package/src/src/ast/nodes/doc-tag.ts +0 -291
- package/src/src/ast/nodes/doc.ts +0 -102
- package/src/src/ast/nodes/enum-value.ts +0 -72
- package/src/src/ast/nodes/enum.ts +0 -77
- package/src/src/ast/nodes/export.ts +0 -54
- package/src/src/ast/nodes/function-type.ts +0 -60
- package/src/src/ast/nodes/function.ts +0 -107
- package/src/src/ast/nodes/generic-parameter.ts +0 -71
- package/src/src/ast/nodes/indexer.ts +0 -60
- package/src/src/ast/nodes/interface.ts +0 -117
- package/src/src/ast/nodes/intersection-type.ts +0 -67
- package/src/src/ast/nodes/lookup-type.ts +0 -53
- package/src/src/ast/nodes/method.ts +0 -138
- package/src/src/ast/nodes/object-type.ts +0 -52
- package/src/src/ast/nodes/object.ts +0 -48
- package/src/src/ast/nodes/parameter.ts +0 -85
- package/src/src/ast/nodes/property-accessor.ts +0 -173
- package/src/src/ast/nodes/property.ts +0 -192
- package/src/src/ast/nodes/reference.ts +0 -194
- package/src/src/ast/nodes/string.ts +0 -49
- package/src/src/ast/nodes/tuple.ts +0 -53
- package/src/src/ast/nodes/type-alias.ts +0 -75
- package/src/src/ast/nodes/typeof.ts +0 -42
- package/src/src/ast/nodes/types.ts +0 -40
- package/src/src/ast/nodes/union-type.ts +0 -62
- package/src/src/ast/nodes/variable.ts +0 -73
- package/src/src/ast/references/angular.ts +0 -24
- package/src/src/ast/references/easy-network-stub.ts +0 -21
- package/src/src/ast/references/index.ts +0 -5
- package/src/src/ast/references/k6.ts +0 -8
- package/src/src/ast/references/rxjs.ts +0 -7
- package/src/src/ast/references/typescript.ts +0 -35
- package/src/src/ast/utils/get-type-script-builder-options.ts +0 -19
- package/src/src/ast/utils/to-ts-node.ts +0 -27
- package/src/src/ast/utils/write-ts-generic-parameters.ts +0 -25
- package/src/src/ast/utils/write-ts-members.ts +0 -28
- package/src/src/ast/utils/write-ts-nodes.ts +0 -35
- package/src/src/ast/utils/write-ts-parameters.ts +0 -25
- package/src/src/common-results.ts +0 -26
- package/src/src/config.ts +0 -96
- package/src/src/file-builder.ts +0 -137
- package/src/src/generators/file-generator.ts +0 -15
- package/src/src/generators/index.ts +0 -6
- package/src/src/generators/models/index.ts +0 -3
- package/src/src/generators/models/model-generator.ts +0 -493
- package/src/src/generators/models/models-generator.ts +0 -108
- package/src/src/generators/models/models.ts +0 -121
- package/src/src/generators/services/angular-services/angular-service-generator.ts +0 -431
- package/src/src/generators/services/angular-services/angular-services-generator.ts +0 -295
- package/src/src/generators/services/angular-services/index.ts +0 -3
- package/src/src/generators/services/angular-services/models.ts +0 -277
- package/src/src/generators/services/angular-services/refs.ts +0 -54
- package/src/src/generators/services/easy-network-stub/easy-network-stub-generator.ts +0 -250
- package/src/src/generators/services/easy-network-stub/easy-network-stubs-generator.ts +0 -211
- package/src/src/generators/services/easy-network-stub/index.ts +0 -3
- package/src/src/generators/services/easy-network-stub/models.ts +0 -124
- package/src/src/generators/services/easy-network-stub/refs.ts +0 -47
- package/src/src/generators/services/fetch-clients/fetch-client-generator.ts +0 -305
- package/src/src/generators/services/fetch-clients/fetch-clients-generator.ts +0 -163
- package/src/src/generators/services/fetch-clients/index.ts +0 -3
- package/src/src/generators/services/fetch-clients/models.ts +0 -130
- package/src/src/generators/services/fetch-clients/refs.ts +0 -21
- package/src/src/generators/services/k6-clients/index.ts +0 -3
- package/src/src/generators/services/k6-clients/k6-client-generator.ts +0 -505
- package/src/src/generators/services/k6-clients/k6-clients-generator.ts +0 -171
- package/src/src/generators/services/k6-clients/models.ts +0 -247
- package/src/src/generators/services/k6-clients/refs.ts +0 -16
- package/src/src/import-collection.ts +0 -236
- package/src/src/utils.ts +0 -79
|
@@ -1,257 +1,257 @@
|
|
|
1
|
-
import type { EasyNetworkStub, ErrorResponse, HttpMethod, RouteResponseCallback, StubHandle } from 'easy-network-stub';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Options for the `EasyNetworkStubWrapper`.
|
|
5
|
-
*/
|
|
6
|
-
export type EasyNetworkStubWrapperOptions = {
|
|
7
|
-
/**
|
|
8
|
-
* Delay in milliseconds before responding.
|
|
9
|
-
* @default 0
|
|
10
|
-
*/
|
|
11
|
-
delay: number;
|
|
12
|
-
/**
|
|
13
|
-
* Whether to log requests to the console.
|
|
14
|
-
* @default false
|
|
15
|
-
*/
|
|
16
|
-
logRequests: boolean;
|
|
17
|
-
/**
|
|
18
|
-
* Whether to log responses to the console.
|
|
19
|
-
* @default false
|
|
20
|
-
*/
|
|
21
|
-
logResponses: boolean;
|
|
22
|
-
/**
|
|
23
|
-
* Whether to remember requests. Remebered requests can be accessed via the `requests` property.
|
|
24
|
-
* @default false
|
|
25
|
-
*/
|
|
26
|
-
rememberRequests: boolean;
|
|
27
|
-
/**
|
|
28
|
-
* Function to call when a response is sent.
|
|
29
|
-
* @param response The response that was sent.
|
|
30
|
-
* @default undefined
|
|
31
|
-
*/
|
|
32
|
-
responseCallback?: (response: unknown) => void;
|
|
33
|
-
/**
|
|
34
|
-
* A prefix to add to the URL in log messages.
|
|
35
|
-
* @default /api/
|
|
36
|
-
*/
|
|
37
|
-
logUrlPrefix: string;
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Represents a request intercepted by the `EasyNetworkStubWrapper`.
|
|
42
|
-
*/
|
|
43
|
-
export type StubRequestItem = {
|
|
44
|
-
/**
|
|
45
|
-
* The HTTP method of the request.
|
|
46
|
-
*/
|
|
47
|
-
method: HttpMethod;
|
|
48
|
-
/**
|
|
49
|
-
* The route of the request.
|
|
50
|
-
*/
|
|
51
|
-
route: string;
|
|
52
|
-
/**
|
|
53
|
-
* The request information provided by `easy-network-stub`.
|
|
54
|
-
*/
|
|
55
|
-
request: StubRequestInfo;
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Represents the request information provided by `easy-network-stub`.
|
|
60
|
-
*/
|
|
61
|
-
export type StubRequestInfo<TRoute extends string = string, TBody = unknown> = Parameters<
|
|
62
|
-
RouteResponseCallback<TRoute, TBody>
|
|
63
|
-
>[0];
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* A strict version of `RouteResponseCallback` that allows for a responder to be passed as the first argument.
|
|
67
|
-
*/
|
|
68
|
-
export type StrictRouteResponseCallback<T, P extends string, R> = (
|
|
69
|
-
respondWith: R,
|
|
70
|
-
...args: Parameters<RouteResponseCallback<P, T>>
|
|
71
|
-
) => ErrorResponse<any> | Promise<ErrorResponse<any>>;
|
|
72
|
-
|
|
73
|
-
type _UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
|
|
74
|
-
type __StubResponder<T extends Record<number, unknown>, S> = S extends number
|
|
75
|
-
? [T[S]] extends [never] ? (statusCode: S) => ErrorResponse<T[S]>
|
|
76
|
-
: (statusCode: S, content: T[S]) => ErrorResponse<T[S]>
|
|
77
|
-
: never;
|
|
78
|
-
type _StubResponder<T extends Record<number, unknown>> = _UnionToIntersection<__StubResponder<T, keyof T>>;
|
|
79
|
-
const untypedResponder = (statusCode: number, content?: unknown) => ({ statusCode, content });
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Gets the responder function typed to a specific set of status codes.
|
|
83
|
-
* @returns The responder function.
|
|
84
|
-
*/
|
|
85
|
-
export function getStubResponder<T extends Record<number, unknown>>(): _StubResponder<T> {
|
|
86
|
-
return untypedResponder as _StubResponder<T>;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Wrapper for `EasyNetworkStub` that adds additional functionality.
|
|
91
|
-
*/
|
|
92
|
-
export class EasyNetworkStubWrapper {
|
|
93
|
-
private _requests: StubRequestItem[] = [];
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* The options for the wrapper.
|
|
97
|
-
*/
|
|
98
|
-
public readonly options: Readonly<EasyNetworkStubWrapperOptions>;
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* The requests that have been intercepted by the wrapper.
|
|
102
|
-
*/
|
|
103
|
-
public get requests(): ReadonlyArray<StubRequestItem> {
|
|
104
|
-
return this._requests;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
constructor(
|
|
108
|
-
public readonly wrappedStub: EasyNetworkStub,
|
|
109
|
-
options?: Partial<EasyNetworkStubWrapperOptions>,
|
|
110
|
-
) {
|
|
111
|
-
this.options = {
|
|
112
|
-
delay: options?.delay ?? 0,
|
|
113
|
-
logRequests: options?.logRequests ?? false,
|
|
114
|
-
logResponses: options?.logResponses ?? false,
|
|
115
|
-
rememberRequests: options?.rememberRequests ?? false,
|
|
116
|
-
responseCallback: options?.responseCallback,
|
|
117
|
-
logUrlPrefix: options?.logUrlPrefix ?? '/api/',
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Add a single api stub to your intercepted routes.
|
|
123
|
-
* @param method The http method that should be stubbed
|
|
124
|
-
* @param route The route that should be stubbed. Supports parameters in the form of {name:type}.
|
|
125
|
-
* @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.
|
|
126
|
-
*/
|
|
127
|
-
public stub<Route extends string>(
|
|
128
|
-
method: HttpMethod,
|
|
129
|
-
route: Route,
|
|
130
|
-
response: RouteResponseCallback<Route, unknown>,
|
|
131
|
-
): StubHandle {
|
|
132
|
-
return this.wrappedStub.stub<Route>(method, route, async (request) => {
|
|
133
|
-
if (this.options.delay > 0) await sleep(this.options.delay);
|
|
134
|
-
return await this.runRequest(method, route, response, request);
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* The stub2 method provides a way to set the type of the body that is sent.
|
|
140
|
-
* Due to restrictions in TypeScript (https://github.com/microsoft/TypeScript/issues/10571, https://github.com/Microsoft/TypeScript/pull/26349),
|
|
141
|
-
* We have to hacke a bit here. Calling stub2<T>() will return the stub method with a body of type T.
|
|
142
|
-
*
|
|
143
|
-
* Usage:
|
|
144
|
-
* ```
|
|
145
|
-
* .stub2<MyRequest>()('GET', '/api/test', ({ body }) => {
|
|
146
|
-
* // body is of type MyRequest
|
|
147
|
-
* });
|
|
148
|
-
* ```
|
|
149
|
-
* @returns The stub method with a body of type T.
|
|
150
|
-
*/
|
|
151
|
-
public stub2<T>(): <Route extends string>(
|
|
152
|
-
method: HttpMethod,
|
|
153
|
-
route: Route,
|
|
154
|
-
response: RouteResponseCallback<Route, T>,
|
|
155
|
-
) => StubHandle {
|
|
156
|
-
return <Route extends string>(method: HttpMethod, route: Route, response: RouteResponseCallback<Route, T>) => {
|
|
157
|
-
return this.wrappedStub.stub2<T>()<Route>(method, route, async (request) => {
|
|
158
|
-
if (this.options.delay > 0) await sleep(this.options.delay);
|
|
159
|
-
return await this.runRequest(method, route, response, request);
|
|
160
|
-
});
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Reset the stubs and requests.
|
|
166
|
-
*/
|
|
167
|
-
public reset(): void {
|
|
168
|
-
this._requests = [];
|
|
169
|
-
this.wrappedStub.unregisterAll();
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
private async runRequest<Route extends string, T>(
|
|
173
|
-
method: HttpMethod,
|
|
174
|
-
route: Route,
|
|
175
|
-
response: RouteResponseCallback<Route, T>,
|
|
176
|
-
request: StubRequestInfo<Route, T>,
|
|
177
|
-
): Promise<unknown> {
|
|
178
|
-
if (this.options.rememberRequests) {
|
|
179
|
-
this._requests.push({ method, route, request });
|
|
180
|
-
}
|
|
181
|
-
this.log('request', method, route, request, '#7FFFFF');
|
|
182
|
-
try {
|
|
183
|
-
const r = await response(request);
|
|
184
|
-
this.options.responseCallback?.(r);
|
|
185
|
-
this.log('response', method, route, r, '#7FFF8E');
|
|
186
|
-
return r;
|
|
187
|
-
} catch (error: unknown) {
|
|
188
|
-
this.options.responseCallback?.(error);
|
|
189
|
-
if (isHttpResponse(error) && error.statusCode >= 200 && error.statusCode < 300) {
|
|
190
|
-
this.log('response', method, route, error.content, '#7FFF8E');
|
|
191
|
-
throw error;
|
|
192
|
-
} else {
|
|
193
|
-
const isError = isHttpResponse(error) && error.statusCode >= 400 && error.statusCode < 600;
|
|
194
|
-
this.log('response', method, route, error, isError ? '#FF7F7F' : '7F7F7F');
|
|
195
|
-
throw error;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
private log(type: 'request' | 'response', method: HttpMethod, route: string, data: unknown, color: string) {
|
|
201
|
-
if (type === 'request' && !this.options.logRequests) return;
|
|
202
|
-
if (type === 'response' && !this.options.logResponses) return;
|
|
203
|
-
const typeDisplay = type === 'request' ? ' [req]' : '[resp]';
|
|
204
|
-
const strippedRoute = route.match(/[^?]*(?=\?)?/)?.[0];
|
|
205
|
-
console.log(`%c${typeDisplay} ${method}: ${this.options.logUrlPrefix}${strippedRoute}`, `color:${color};`, data);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* Base class for generated easy network stubs.
|
|
211
|
-
*/
|
|
212
|
-
export class EasyNetworkStubBase {
|
|
213
|
-
protected readonly stubWrapper: EasyNetworkStubWrapper;
|
|
214
|
-
protected readonly ownsStubWrapper: boolean;
|
|
215
|
-
|
|
216
|
-
constructor(stub: EasyNetworkStub | EasyNetworkStubWrapper, options?: EasyNetworkStubWrapperOptions) {
|
|
217
|
-
this.stubWrapper = stub instanceof EasyNetworkStubWrapper ? stub : new EasyNetworkStubWrapper(stub, options);
|
|
218
|
-
this.ownsStubWrapper = !(stub instanceof EasyNetworkStubWrapper);
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
public reset() {
|
|
222
|
-
if (this.ownsStubWrapper) {
|
|
223
|
-
this.stubWrapper.reset();
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
export type EasyNetworkStubGroup<Group, GroupContainer> = Group & {
|
|
229
|
-
(stubActions: (stubs: Group) => void): GroupContainer;
|
|
230
|
-
};
|
|
231
|
-
|
|
232
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
233
|
-
declare interface GroupType<T> extends Function {
|
|
234
|
-
new (stubWrapper: EasyNetworkStubWrapper): T;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
export function createEasyNetworkStubGroup<GroupContainer, Group>(
|
|
238
|
-
container: GroupContainer,
|
|
239
|
-
stubWrapper: EasyNetworkStubWrapper,
|
|
240
|
-
groupType: GroupType<Group>,
|
|
241
|
-
): EasyNetworkStubGroup<Group, GroupContainer> {
|
|
242
|
-
const group = new groupType(stubWrapper);
|
|
243
|
-
const groupFn = (stubActions: (stubs: Group) => void) => {
|
|
244
|
-
stubActions(group);
|
|
245
|
-
return container;
|
|
246
|
-
};
|
|
247
|
-
Object.setPrototypeOf(groupFn, groupType.prototype);
|
|
248
|
-
return Object.assign(groupFn, group);
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
function sleep(ms: number): Promise<void> {
|
|
252
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
function isHttpResponse(value: unknown): value is ErrorResponse<unknown> {
|
|
256
|
-
return typeof value === 'object' && value !== null && 'statusCode' in value;
|
|
257
|
-
}
|
|
1
|
+
import type { EasyNetworkStub, ErrorResponse, HttpMethod, RouteResponseCallback, StubHandle } from 'easy-network-stub';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Options for the `EasyNetworkStubWrapper`.
|
|
5
|
+
*/
|
|
6
|
+
export type EasyNetworkStubWrapperOptions = {
|
|
7
|
+
/**
|
|
8
|
+
* Delay in milliseconds before responding.
|
|
9
|
+
* @default 0
|
|
10
|
+
*/
|
|
11
|
+
delay: number;
|
|
12
|
+
/**
|
|
13
|
+
* Whether to log requests to the console.
|
|
14
|
+
* @default false
|
|
15
|
+
*/
|
|
16
|
+
logRequests: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Whether to log responses to the console.
|
|
19
|
+
* @default false
|
|
20
|
+
*/
|
|
21
|
+
logResponses: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Whether to remember requests. Remebered requests can be accessed via the `requests` property.
|
|
24
|
+
* @default false
|
|
25
|
+
*/
|
|
26
|
+
rememberRequests: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Function to call when a response is sent.
|
|
29
|
+
* @param response The response that was sent.
|
|
30
|
+
* @default undefined
|
|
31
|
+
*/
|
|
32
|
+
responseCallback?: (response: unknown) => void;
|
|
33
|
+
/**
|
|
34
|
+
* A prefix to add to the URL in log messages.
|
|
35
|
+
* @default /api/
|
|
36
|
+
*/
|
|
37
|
+
logUrlPrefix: string;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Represents a request intercepted by the `EasyNetworkStubWrapper`.
|
|
42
|
+
*/
|
|
43
|
+
export type StubRequestItem = {
|
|
44
|
+
/**
|
|
45
|
+
* The HTTP method of the request.
|
|
46
|
+
*/
|
|
47
|
+
method: HttpMethod;
|
|
48
|
+
/**
|
|
49
|
+
* The route of the request.
|
|
50
|
+
*/
|
|
51
|
+
route: string;
|
|
52
|
+
/**
|
|
53
|
+
* The request information provided by `easy-network-stub`.
|
|
54
|
+
*/
|
|
55
|
+
request: StubRequestInfo;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Represents the request information provided by `easy-network-stub`.
|
|
60
|
+
*/
|
|
61
|
+
export type StubRequestInfo<TRoute extends string = string, TBody = unknown> = Parameters<
|
|
62
|
+
RouteResponseCallback<TRoute, TBody>
|
|
63
|
+
>[0];
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* A strict version of `RouteResponseCallback` that allows for a responder to be passed as the first argument.
|
|
67
|
+
*/
|
|
68
|
+
export type StrictRouteResponseCallback<T, P extends string, R> = (
|
|
69
|
+
respondWith: R,
|
|
70
|
+
...args: Parameters<RouteResponseCallback<P, T>>
|
|
71
|
+
) => ErrorResponse<any> | Promise<ErrorResponse<any>>;
|
|
72
|
+
|
|
73
|
+
type _UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
|
|
74
|
+
type __StubResponder<T extends Record<number, unknown>, S> = S extends number
|
|
75
|
+
? [T[S]] extends [never] ? (statusCode: S) => ErrorResponse<T[S]>
|
|
76
|
+
: (statusCode: S, content: T[S]) => ErrorResponse<T[S]>
|
|
77
|
+
: never;
|
|
78
|
+
type _StubResponder<T extends Record<number, unknown>> = _UnionToIntersection<__StubResponder<T, keyof T>>;
|
|
79
|
+
const untypedResponder = (statusCode: number, content?: unknown) => ({ statusCode, content });
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Gets the responder function typed to a specific set of status codes.
|
|
83
|
+
* @returns The responder function.
|
|
84
|
+
*/
|
|
85
|
+
export function getStubResponder<T extends Record<number, unknown>>(): _StubResponder<T> {
|
|
86
|
+
return untypedResponder as _StubResponder<T>;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Wrapper for `EasyNetworkStub` that adds additional functionality.
|
|
91
|
+
*/
|
|
92
|
+
export class EasyNetworkStubWrapper {
|
|
93
|
+
private _requests: StubRequestItem[] = [];
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The options for the wrapper.
|
|
97
|
+
*/
|
|
98
|
+
public readonly options: Readonly<EasyNetworkStubWrapperOptions>;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The requests that have been intercepted by the wrapper.
|
|
102
|
+
*/
|
|
103
|
+
public get requests(): ReadonlyArray<StubRequestItem> {
|
|
104
|
+
return this._requests;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
constructor(
|
|
108
|
+
public readonly wrappedStub: EasyNetworkStub,
|
|
109
|
+
options?: Partial<EasyNetworkStubWrapperOptions>,
|
|
110
|
+
) {
|
|
111
|
+
this.options = {
|
|
112
|
+
delay: options?.delay ?? 0,
|
|
113
|
+
logRequests: options?.logRequests ?? false,
|
|
114
|
+
logResponses: options?.logResponses ?? false,
|
|
115
|
+
rememberRequests: options?.rememberRequests ?? false,
|
|
116
|
+
responseCallback: options?.responseCallback,
|
|
117
|
+
logUrlPrefix: options?.logUrlPrefix ?? '/api/',
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Add a single api stub to your intercepted routes.
|
|
123
|
+
* @param method The http method that should be stubbed
|
|
124
|
+
* @param route The route that should be stubbed. Supports parameters in the form of {name:type}.
|
|
125
|
+
* @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.
|
|
126
|
+
*/
|
|
127
|
+
public stub<Route extends string>(
|
|
128
|
+
method: HttpMethod,
|
|
129
|
+
route: Route,
|
|
130
|
+
response: RouteResponseCallback<Route, unknown>,
|
|
131
|
+
): StubHandle {
|
|
132
|
+
return this.wrappedStub.stub<Route>(method, route, async (request) => {
|
|
133
|
+
if (this.options.delay > 0) await sleep(this.options.delay);
|
|
134
|
+
return await this.runRequest(method, route, response, request);
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* The stub2 method provides a way to set the type of the body that is sent.
|
|
140
|
+
* Due to restrictions in TypeScript (https://github.com/microsoft/TypeScript/issues/10571, https://github.com/Microsoft/TypeScript/pull/26349),
|
|
141
|
+
* We have to hacke a bit here. Calling stub2<T>() will return the stub method with a body of type T.
|
|
142
|
+
*
|
|
143
|
+
* Usage:
|
|
144
|
+
* ```
|
|
145
|
+
* .stub2<MyRequest>()('GET', '/api/test', ({ body }) => {
|
|
146
|
+
* // body is of type MyRequest
|
|
147
|
+
* });
|
|
148
|
+
* ```
|
|
149
|
+
* @returns The stub method with a body of type T.
|
|
150
|
+
*/
|
|
151
|
+
public stub2<T>(): <Route extends string>(
|
|
152
|
+
method: HttpMethod,
|
|
153
|
+
route: Route,
|
|
154
|
+
response: RouteResponseCallback<Route, T>,
|
|
155
|
+
) => StubHandle {
|
|
156
|
+
return <Route extends string>(method: HttpMethod, route: Route, response: RouteResponseCallback<Route, T>) => {
|
|
157
|
+
return this.wrappedStub.stub2<T>()<Route>(method, route, async (request) => {
|
|
158
|
+
if (this.options.delay > 0) await sleep(this.options.delay);
|
|
159
|
+
return await this.runRequest(method, route, response, request);
|
|
160
|
+
});
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Reset the stubs and requests.
|
|
166
|
+
*/
|
|
167
|
+
public reset(): void {
|
|
168
|
+
this._requests = [];
|
|
169
|
+
this.wrappedStub.unregisterAll();
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
private async runRequest<Route extends string, T>(
|
|
173
|
+
method: HttpMethod,
|
|
174
|
+
route: Route,
|
|
175
|
+
response: RouteResponseCallback<Route, T>,
|
|
176
|
+
request: StubRequestInfo<Route, T>,
|
|
177
|
+
): Promise<unknown> {
|
|
178
|
+
if (this.options.rememberRequests) {
|
|
179
|
+
this._requests.push({ method, route, request });
|
|
180
|
+
}
|
|
181
|
+
this.log('request', method, route, request, '#7FFFFF');
|
|
182
|
+
try {
|
|
183
|
+
const r = await response(request);
|
|
184
|
+
this.options.responseCallback?.(r);
|
|
185
|
+
this.log('response', method, route, r, '#7FFF8E');
|
|
186
|
+
return r;
|
|
187
|
+
} catch (error: unknown) {
|
|
188
|
+
this.options.responseCallback?.(error);
|
|
189
|
+
if (isHttpResponse(error) && error.statusCode >= 200 && error.statusCode < 300) {
|
|
190
|
+
this.log('response', method, route, error.content, '#7FFF8E');
|
|
191
|
+
throw error;
|
|
192
|
+
} else {
|
|
193
|
+
const isError = isHttpResponse(error) && error.statusCode >= 400 && error.statusCode < 600;
|
|
194
|
+
this.log('response', method, route, error, isError ? '#FF7F7F' : '7F7F7F');
|
|
195
|
+
throw error;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
private log(type: 'request' | 'response', method: HttpMethod, route: string, data: unknown, color: string) {
|
|
201
|
+
if (type === 'request' && !this.options.logRequests) return;
|
|
202
|
+
if (type === 'response' && !this.options.logResponses) return;
|
|
203
|
+
const typeDisplay = type === 'request' ? ' [req]' : '[resp]';
|
|
204
|
+
const strippedRoute = route.match(/[^?]*(?=\?)?/)?.[0];
|
|
205
|
+
console.log(`%c${typeDisplay} ${method}: ${this.options.logUrlPrefix}${strippedRoute}`, `color:${color};`, data);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Base class for generated easy network stubs.
|
|
211
|
+
*/
|
|
212
|
+
export class EasyNetworkStubBase {
|
|
213
|
+
protected readonly stubWrapper: EasyNetworkStubWrapper;
|
|
214
|
+
protected readonly ownsStubWrapper: boolean;
|
|
215
|
+
|
|
216
|
+
constructor(stub: EasyNetworkStub | EasyNetworkStubWrapper, options?: EasyNetworkStubWrapperOptions) {
|
|
217
|
+
this.stubWrapper = stub instanceof EasyNetworkStubWrapper ? stub : new EasyNetworkStubWrapper(stub, options);
|
|
218
|
+
this.ownsStubWrapper = !(stub instanceof EasyNetworkStubWrapper);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
public reset() {
|
|
222
|
+
if (this.ownsStubWrapper) {
|
|
223
|
+
this.stubWrapper.reset();
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export type EasyNetworkStubGroup<Group, GroupContainer> = Group & {
|
|
229
|
+
(stubActions: (stubs: Group) => void): GroupContainer;
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
233
|
+
declare interface GroupType<T> extends Function {
|
|
234
|
+
new (stubWrapper: EasyNetworkStubWrapper): T;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export function createEasyNetworkStubGroup<GroupContainer, Group>(
|
|
238
|
+
container: GroupContainer,
|
|
239
|
+
stubWrapper: EasyNetworkStubWrapper,
|
|
240
|
+
groupType: GroupType<Group>,
|
|
241
|
+
): EasyNetworkStubGroup<Group, GroupContainer> {
|
|
242
|
+
const group = new groupType(stubWrapper);
|
|
243
|
+
const groupFn = (stubActions: (stubs: Group) => void) => {
|
|
244
|
+
stubActions(group);
|
|
245
|
+
return container;
|
|
246
|
+
};
|
|
247
|
+
Object.setPrototypeOf(groupFn, groupType.prototype);
|
|
248
|
+
return Object.assign(groupFn, group);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function sleep(ms: number): Promise<void> {
|
|
252
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function isHttpResponse(value: unknown): value is ErrorResponse<unknown> {
|
|
256
|
+
return typeof value === 'object' && value !== null && 'statusCode' in value;
|
|
257
|
+
}
|
|
@@ -235,13 +235,13 @@ export class DefaultTypeScriptAngularServiceGenerator extends TypeScriptFileGene
|
|
|
235
235
|
], '\n'), { multiline: true })
|
|
236
236
|
: null,
|
|
237
237
|
'',
|
|
238
|
-
s `return ${ctx.refs.waitForResponse([responseModelType])}(${s.indent `
|
|
239
|
-
this.http.request(rb.build({${s.indent `
|
|
240
|
-
responseType: ${ts.string(responseType)},
|
|
241
|
-
accept: ${ts.string(accept)},
|
|
242
|
-
context,`}
|
|
243
|
-
})),
|
|
244
|
-
{${s.indent `
|
|
238
|
+
s `return ${ctx.refs.waitForResponse([responseModelType])}(${s.indent `
|
|
239
|
+
this.http.request(rb.build({${s.indent `
|
|
240
|
+
responseType: ${ts.string(responseType)},
|
|
241
|
+
accept: ${ts.string(accept)},
|
|
242
|
+
context,`}
|
|
243
|
+
})),
|
|
244
|
+
{${s.indent `
|
|
245
245
|
errorResponseTypes: ${ts.object({
|
|
246
246
|
members: [
|
|
247
247
|
...Object.entries(this.getEndpointStatusCodes(ctx, endpoint))
|
|
@@ -250,8 +250,8 @@ export class DefaultTypeScriptAngularServiceGenerator extends TypeScriptFileGene
|
|
|
250
250
|
return ts.property(key, { value: ts.string(value.parser) });
|
|
251
251
|
}),
|
|
252
252
|
],
|
|
253
|
-
})}`}
|
|
254
|
-
}`}
|
|
253
|
+
})}`}
|
|
254
|
+
}`}
|
|
255
255
|
)`,
|
|
256
256
|
], '\n'),
|
|
257
257
|
});
|
|
@@ -99,15 +99,15 @@ export class DefaultTypeScriptEasyNetworkStubGenerator extends TypeScriptFileGen
|
|
|
99
99
|
],
|
|
100
100
|
returnType: 'this',
|
|
101
101
|
body: appendValueGroup([
|
|
102
|
-
s `this.stubWrapper.stub2<${requestType}>()(${s.indent `
|
|
103
|
-
${ts.string(endpoint.method.toUpperCase())},
|
|
104
|
-
${this.getStubClassName(ctx)}.${this.getEndpointPathPropertyName(ctx, endpoint)},
|
|
105
|
-
async (request) => {${s.indent `
|
|
106
|
-
if (this.stubWrapper.options.rememberRequests) {${s.indent `
|
|
107
|
-
this.${this.getEndpointRequestsFieldName(ctx, endpoint)}.push(request);`}
|
|
108
|
-
}
|
|
109
|
-
throw await response(${this.getResponderVariableName(ctx, endpoint)}, request);`}
|
|
110
|
-
}`}
|
|
102
|
+
s `this.stubWrapper.stub2<${requestType}>()(${s.indent `
|
|
103
|
+
${ts.string(endpoint.method.toUpperCase())},
|
|
104
|
+
${this.getStubClassName(ctx)}.${this.getEndpointPathPropertyName(ctx, endpoint)},
|
|
105
|
+
async (request) => {${s.indent `
|
|
106
|
+
if (this.stubWrapper.options.rememberRequests) {${s.indent `
|
|
107
|
+
this.${this.getEndpointRequestsFieldName(ctx, endpoint)}.push(request);`}
|
|
108
|
+
}
|
|
109
|
+
throw await response(${this.getResponderVariableName(ctx, endpoint)}, request);`}
|
|
110
|
+
}`}
|
|
111
111
|
);`,
|
|
112
112
|
'return this;',
|
|
113
113
|
], '\n'),
|
|
@@ -140,7 +140,7 @@ export class DefaultTypeScriptFetchClientGenerator extends TypeScriptFileGenerat
|
|
|
140
140
|
const method = this.getInterfaceEndpointMethod(ctx, endpoint);
|
|
141
141
|
method.accessModifier = 'public';
|
|
142
142
|
method.body = appendValueGroup([
|
|
143
|
-
s `const url = new ${ctx.refs.urlBuilder()}(this.options.baseUrl)${s.indent `
|
|
143
|
+
s `const url = new ${ctx.refs.urlBuilder()}(this.options.baseUrl)${s.indent `
|
|
144
144
|
.withPath(${ts.string(endpoint.path)})${(b) => b.forEach(endpoint.parameters, (b, p) => {
|
|
145
145
|
switch (p.target) {
|
|
146
146
|
case 'path':
|
|
@@ -150,11 +150,11 @@ export class DefaultTypeScriptFetchClientGenerator extends TypeScriptFileGenerat
|
|
|
150
150
|
b.append(s `\n.withQueryParam(${ts.string(p.name)}, params.${toCasing(p.name, ctx.config.paramNameCasing)})`);
|
|
151
151
|
break;
|
|
152
152
|
}
|
|
153
|
-
})}
|
|
153
|
+
})}
|
|
154
154
|
.build();`}`,
|
|
155
155
|
'const headers = { ...this.options.headers };',
|
|
156
|
-
...endpoint.parameters.filter((x) => x.target === 'header').map((p) => s `if (params && params.${toCasing(p.name, ctx.config.paramNameCasing)} !== undefined) {${s.indent `
|
|
157
|
-
headers[${ts.string(p.name)}] = String(params.${toCasing(p.name, ctx.config.paramNameCasing)});`}
|
|
156
|
+
...endpoint.parameters.filter((x) => x.target === 'header').map((p) => s `if (params && params.${toCasing(p.name, ctx.config.paramNameCasing)} !== undefined) {${s.indent `
|
|
157
|
+
headers[${ts.string(p.name)}] = String(params.${toCasing(p.name, ctx.config.paramNameCasing)});`}
|
|
158
158
|
}`),
|
|
159
159
|
s `const response = (this.options.fetch ?? fetch)(url, ${ts.object({
|
|
160
160
|
members: [
|
|
@@ -245,7 +245,7 @@ export class DefaultTypeScriptK6ClientGenerator extends TypeScriptFileGenerator
|
|
|
245
245
|
ts.constructorParameter('rootUrl', { type: isJs ? undefined : rootUrlType }),
|
|
246
246
|
ts.constructorParameter('defaultK6ParamsFactory', { type: isJs ? undefined : paramType, optional: true }),
|
|
247
247
|
],
|
|
248
|
-
body: s `this.rootUrl = rootUrl;
|
|
248
|
+
body: s `this.rootUrl = rootUrl;
|
|
249
249
|
this._defaultK6ParamsFactory = defaultK6ParamsFactory;`,
|
|
250
250
|
}),
|
|
251
251
|
...ctx.service.endpoints.map((e) => this.getEndpointMethod(ctx, e)),
|
|
@@ -307,17 +307,17 @@ export class DefaultTypeScriptK6ClientGenerator extends TypeScriptFileGenerator
|
|
|
307
307
|
: null,
|
|
308
308
|
'',
|
|
309
309
|
ctx.config.language === 'javascript'
|
|
310
|
-
? s `return /** @type {${returnType}} */ (${s.indent `
|
|
311
|
-
${ctx.config.async ? 'await rb.buildAsync' : 'rb.build'}({${s.indent `
|
|
312
|
-
accept: ${ts.string(accept)},
|
|
313
|
-
params: this.getK6Params(k6Params),`}
|
|
314
|
-
})`}
|
|
310
|
+
? s `return /** @type {${returnType}} */ (${s.indent `
|
|
311
|
+
${ctx.config.async ? 'await rb.buildAsync' : 'rb.build'}({${s.indent `
|
|
312
|
+
accept: ${ts.string(accept)},
|
|
313
|
+
params: this.getK6Params(k6Params),`}
|
|
314
|
+
})`}
|
|
315
315
|
);`
|
|
316
|
-
: s `return (${s.indent `
|
|
317
|
-
${ctx.config.async ? 'await rb.buildAsync' : 'rb.build'}({${s.indent `
|
|
318
|
-
accept: ${ts.string(accept)},
|
|
319
|
-
params: this.getK6Params(k6Params),`}
|
|
320
|
-
})`}
|
|
316
|
+
: s `return (${s.indent `
|
|
317
|
+
${ctx.config.async ? 'await rb.buildAsync' : 'rb.build'}({${s.indent `
|
|
318
|
+
accept: ${ts.string(accept)},
|
|
319
|
+
params: this.getK6Params(k6Params),`}
|
|
320
|
+
})`}
|
|
321
321
|
) as unknown as ${returnType};`,
|
|
322
322
|
], '\n'),
|
|
323
323
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@goast/typescript",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0-beta.1",
|
|
4
4
|
"description": "Provides gOAst generators for generating TypeScript code from OpenAPI specifications.",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Marc Schmidt (MaSch0212)",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"tslib": "^2.6.2",
|
|
27
27
|
"fs-extra": "^11.3.3",
|
|
28
|
-
"@goast/core": "0.
|
|
28
|
+
"@goast/core": "0.6.0-beta.1"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@types/node": "^20.9.0",
|