@goast/typescript 0.3.7-beta1 → 0.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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/generators/models/model-generator.js +26 -5
- package/cjs/lib/generators/models/models-generator.js +3 -1
- 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/easy-network-stub/easy-network-stubs-generator.js +4 -8
- 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/generators/models/model-generator.js +26 -5
- package/esm/lib/generators/models/models-generator.js +3 -1
- 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/easy-network-stub/easy-network-stubs-generator.js +4 -8
- 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 +3 -2
- package/types/lib/common-results.d.ts +2 -1
- package/types/lib/generators/models/model-generator.d.ts +2 -0
package/README.md
CHANGED
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
# gOAst (@goast/typescript)
|
|
2
|
-
|
|
3
|
-
[](https://www.npmjs.com/package/@goast/typescript)
|
|
4
|
-
|
|
5
|
-
**gOAst** stands for **g**enerative **O**pen **A**PI **s**pecification **t**ransformer, a tool designed to transform OpenAPI specifications into various forms with flexibility and extensibility at its core.
|
|
6
|
-
|
|
7
|
-
For more Information, please visit the [gOAst GitHub Repository](https://github.com/MaSch0212/goast/blob/main).
|
|
8
|
-
|
|
9
|
-
## Purpose 👍
|
|
10
|
-
|
|
11
|
-
The `@goast/typescript` package provides generators for [TypeScript](https://www.typescriptlang.org/) code generation from OpenAPI specifications.
|
|
12
|
-
|
|
13
|
-
## Usage Example 🚀
|
|
14
|
-
|
|
15
|
-
```typescript
|
|
16
|
-
import { OpenApiGenerator } from '@goast/core';
|
|
17
|
-
import { TypeScriptModelsGenerator, TypeScriptFetchClientsGenerator } from '@goast/typescript';
|
|
18
|
-
|
|
19
|
-
async function main() {
|
|
20
|
-
await new OpenApiGenerator({ outputDir: '.api' })
|
|
21
|
-
.useType(TypeScriptModelsGenerator)
|
|
22
|
-
.useType(TypeScriptFetchClientsGenerator)
|
|
23
|
-
.parseAndGenerateFromDir('.openapi');
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
main();
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
## Available Generators 📚
|
|
30
|
-
|
|
31
|
-
- [`TypeScriptModelsGenerator`](https://github.com/MaSch0212/goast/wiki/TypeScript%20Models%20Generator): Generates TypeScript interfaces or types from schemas in the OpenAPI specification(s).
|
|
32
|
-
- [`TypeScriptFetchClientsGenerator`](https://github.com/MaSch0212/goast/wiki/TypeScript%20Fetch%20Clients%20Generator): Generates Fetch clients for TypeScript from paths in the OpenAPI specification(s).
|
|
33
|
-
- Depends on the output of the `TypeScriptModelsGenerator` generator.
|
|
34
|
-
- [`TypeScriptAngularServicesGenerator`](https://github.com/MaSch0212/goast/wiki/TypeScript%20Angular%20Services%20Generator): Generates [Angular](https://angular.dev/) services from paths in the OpenAPI specification(s).
|
|
35
|
-
- Depends on the output of the `TypeScriptModelsGenerator` generator.
|
|
36
|
-
- [`TypeScriptEasyNetworkStubsGenerator`](https://github.com/MaSch0212/goast/wiki/TypeScript%20Easy%20Network%20Stubs%20Generator): Generates [easy-network-stub](https://github.com/LoaderB0T/easy-network-stub) classes from paths in the OpenAPI specification(s).
|
|
37
|
-
- Depends on the output of the `TypeScriptModelsGenerator` generator.
|
|
38
|
-
- [`TypeScriptK6ClientsGenerator`](https://github.com/MaSch0212/goast/wiki/TypeScript%20K6%20Clients%20Generator): Generates [k6](https://k6.io/) clients from paths in the OpenAPI specification(s).
|
|
39
|
-
- Depends on the output of the `TypeScriptModelsGenerator` generator.
|
|
40
|
-
|
|
41
|
-
## API Documentation 📖
|
|
42
|
-
|
|
43
|
-
The API documentation can be found [here](https://github.com/MaSch0212/goast/wiki/TypeScript%20Generators).
|
|
1
|
+
# gOAst (@goast/typescript)
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@goast/typescript)
|
|
4
|
+
|
|
5
|
+
**gOAst** stands for **g**enerative **O**pen **A**PI **s**pecification **t**ransformer, a tool designed to transform OpenAPI specifications into various forms with flexibility and extensibility at its core.
|
|
6
|
+
|
|
7
|
+
For more Information, please visit the [gOAst GitHub Repository](https://github.com/MaSch0212/goast/blob/main).
|
|
8
|
+
|
|
9
|
+
## Purpose 👍
|
|
10
|
+
|
|
11
|
+
The `@goast/typescript` package provides generators for [TypeScript](https://www.typescriptlang.org/) code generation from OpenAPI specifications.
|
|
12
|
+
|
|
13
|
+
## Usage Example 🚀
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { OpenApiGenerator } from '@goast/core';
|
|
17
|
+
import { TypeScriptModelsGenerator, TypeScriptFetchClientsGenerator } from '@goast/typescript';
|
|
18
|
+
|
|
19
|
+
async function main() {
|
|
20
|
+
await new OpenApiGenerator({ outputDir: '.api' })
|
|
21
|
+
.useType(TypeScriptModelsGenerator)
|
|
22
|
+
.useType(TypeScriptFetchClientsGenerator)
|
|
23
|
+
.parseAndGenerateFromDir('.openapi');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
main();
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Available Generators 📚
|
|
30
|
+
|
|
31
|
+
- [`TypeScriptModelsGenerator`](https://github.com/MaSch0212/goast/wiki/TypeScript%20Models%20Generator): Generates TypeScript interfaces or types from schemas in the OpenAPI specification(s).
|
|
32
|
+
- [`TypeScriptFetchClientsGenerator`](https://github.com/MaSch0212/goast/wiki/TypeScript%20Fetch%20Clients%20Generator): Generates Fetch clients for TypeScript from paths in the OpenAPI specification(s).
|
|
33
|
+
- Depends on the output of the `TypeScriptModelsGenerator` generator.
|
|
34
|
+
- [`TypeScriptAngularServicesGenerator`](https://github.com/MaSch0212/goast/wiki/TypeScript%20Angular%20Services%20Generator): Generates [Angular](https://angular.dev/) services from paths in the OpenAPI specification(s).
|
|
35
|
+
- Depends on the output of the `TypeScriptModelsGenerator` generator.
|
|
36
|
+
- [`TypeScriptEasyNetworkStubsGenerator`](https://github.com/MaSch0212/goast/wiki/TypeScript%20Easy%20Network%20Stubs%20Generator): Generates [easy-network-stub](https://github.com/LoaderB0T/easy-network-stub) classes from paths in the OpenAPI specification(s).
|
|
37
|
+
- Depends on the output of the `TypeScriptModelsGenerator` generator.
|
|
38
|
+
- [`TypeScriptK6ClientsGenerator`](https://github.com/MaSch0212/goast/wiki/TypeScript%20K6%20Clients%20Generator): Generates [k6](https://k6.io/) clients from paths in the OpenAPI specification(s).
|
|
39
|
+
- Depends on the output of the `TypeScriptModelsGenerator` generator.
|
|
40
|
+
|
|
41
|
+
## API Documentation 📖
|
|
42
|
+
|
|
43
|
+
The API documentation can be found [here](https://github.com/MaSch0212/goast/wiki/TypeScript%20Generators).
|
|
@@ -1,82 +1,82 @@
|
|
|
1
|
-
import { HttpErrorResponse, HttpResponse } from '@angular/common/http';
|
|
2
|
-
import { Observable, Subscription, filter, take, map, catchError, EMPTY } from 'rxjs';
|
|
3
|
-
|
|
4
|
-
import type { HttpEvent } from '@angular/common/http';
|
|
5
|
-
|
|
6
|
-
export type AbortablePromise<T> = Omit<Promise<T>, 'then' | 'catch' | 'finally'> & {
|
|
7
|
-
abort(): void;
|
|
8
|
-
then<TResult1 = T, TResult2 = never>(
|
|
9
|
-
onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null,
|
|
10
|
-
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null,
|
|
11
|
-
): AbortablePromise<TResult1 | TResult2>;
|
|
12
|
-
catch<TResult = never>(
|
|
13
|
-
onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null,
|
|
14
|
-
): AbortablePromise<T | TResult>;
|
|
15
|
-
finally(onfinally?: (() => void) | undefined | null): AbortablePromise<T>;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export function waitForResponse<T extends HttpResponse<any> | HttpErrorResponse>(
|
|
19
|
-
httpEvents: Observable<HttpEvent<any>>,
|
|
20
|
-
options: { errorResponseTypes: Record<number, 'text' | 'json'> },
|
|
21
|
-
): AbortablePromise<T> {
|
|
22
|
-
return firstValueFromAbortable<T>(
|
|
23
|
-
httpEvents.pipe(
|
|
24
|
-
filter((event) => event instanceof HttpResponse),
|
|
25
|
-
map((response) => response as T),
|
|
26
|
-
),
|
|
27
|
-
).catch((error) => {
|
|
28
|
-
if (error instanceof HttpErrorResponse) {
|
|
29
|
-
if (options.errorResponseTypes[error.status] === 'json' && typeof error.error === 'string') {
|
|
30
|
-
return new HttpErrorResponse({
|
|
31
|
-
error: JSON.parse(error.error),
|
|
32
|
-
headers: error.headers,
|
|
33
|
-
status: error.status,
|
|
34
|
-
statusText: error.statusText,
|
|
35
|
-
url: error.url ?? undefined,
|
|
36
|
-
}) as T;
|
|
37
|
-
}
|
|
38
|
-
return error as T;
|
|
39
|
-
}
|
|
40
|
-
throw error;
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function firstValueFromAbortable<T>(observable: Observable<T>): AbortablePromise<T> {
|
|
45
|
-
let subscription: Subscription | undefined = undefined;
|
|
46
|
-
let _reject: ((reason?: any) => void) | undefined = undefined;
|
|
47
|
-
const abortFn = () => {
|
|
48
|
-
if (!subscription) {
|
|
49
|
-
throw new Error('Not subscribed yet.');
|
|
50
|
-
}
|
|
51
|
-
subscription.unsubscribe();
|
|
52
|
-
_reject?.(new DOMException('The request has been aborted.', 'AbortError'));
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
return createAbortablePromise(
|
|
56
|
-
new Promise<T>((resolve, reject) => {
|
|
57
|
-
_reject = reject;
|
|
58
|
-
subscription = observable
|
|
59
|
-
.pipe(
|
|
60
|
-
take(1),
|
|
61
|
-
catchError((e: unknown) => {
|
|
62
|
-
reject(e);
|
|
63
|
-
return EMPTY;
|
|
64
|
-
}),
|
|
65
|
-
)
|
|
66
|
-
.subscribe((x: T) => resolve(x));
|
|
67
|
-
}),
|
|
68
|
-
abortFn,
|
|
69
|
-
);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function createAbortablePromise<T>(promise: Promise<T>, abortFn: () => void) {
|
|
73
|
-
const thenFn = promise.then.bind(promise);
|
|
74
|
-
const catchFn = promise.catch.bind(promise);
|
|
75
|
-
const finallyFn = promise.finally?.bind(promise);
|
|
76
|
-
return Object.assign(promise, {
|
|
77
|
-
then: (...args: any[]) => createAbortablePromise(thenFn(...args), abortFn),
|
|
78
|
-
catch: (...args: any[]) => createAbortablePromise(catchFn(...args), abortFn),
|
|
79
|
-
finally: finallyFn ? (...args: any[]) => createAbortablePromise(finallyFn(...args), abortFn) : undefined,
|
|
80
|
-
abort: abortFn,
|
|
81
|
-
});
|
|
82
|
-
}
|
|
1
|
+
import { HttpErrorResponse, HttpResponse } from '@angular/common/http';
|
|
2
|
+
import { Observable, Subscription, filter, take, map, catchError, EMPTY } from 'rxjs';
|
|
3
|
+
|
|
4
|
+
import type { HttpEvent } from '@angular/common/http';
|
|
5
|
+
|
|
6
|
+
export type AbortablePromise<T> = Omit<Promise<T>, 'then' | 'catch' | 'finally'> & {
|
|
7
|
+
abort(): void;
|
|
8
|
+
then<TResult1 = T, TResult2 = never>(
|
|
9
|
+
onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null,
|
|
10
|
+
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null,
|
|
11
|
+
): AbortablePromise<TResult1 | TResult2>;
|
|
12
|
+
catch<TResult = never>(
|
|
13
|
+
onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null,
|
|
14
|
+
): AbortablePromise<T | TResult>;
|
|
15
|
+
finally(onfinally?: (() => void) | undefined | null): AbortablePromise<T>;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export function waitForResponse<T extends HttpResponse<any> | HttpErrorResponse>(
|
|
19
|
+
httpEvents: Observable<HttpEvent<any>>,
|
|
20
|
+
options: { errorResponseTypes: Record<number, 'text' | 'json'> },
|
|
21
|
+
): AbortablePromise<T> {
|
|
22
|
+
return firstValueFromAbortable<T>(
|
|
23
|
+
httpEvents.pipe(
|
|
24
|
+
filter((event) => event instanceof HttpResponse),
|
|
25
|
+
map((response) => response as T),
|
|
26
|
+
),
|
|
27
|
+
).catch((error) => {
|
|
28
|
+
if (error instanceof HttpErrorResponse) {
|
|
29
|
+
if (options.errorResponseTypes[error.status] === 'json' && typeof error.error === 'string') {
|
|
30
|
+
return new HttpErrorResponse({
|
|
31
|
+
error: JSON.parse(error.error),
|
|
32
|
+
headers: error.headers,
|
|
33
|
+
status: error.status,
|
|
34
|
+
statusText: error.statusText,
|
|
35
|
+
url: error.url ?? undefined,
|
|
36
|
+
}) as T;
|
|
37
|
+
}
|
|
38
|
+
return error as T;
|
|
39
|
+
}
|
|
40
|
+
throw error;
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function firstValueFromAbortable<T>(observable: Observable<T>): AbortablePromise<T> {
|
|
45
|
+
let subscription: Subscription | undefined = undefined;
|
|
46
|
+
let _reject: ((reason?: any) => void) | undefined = undefined;
|
|
47
|
+
const abortFn = () => {
|
|
48
|
+
if (!subscription) {
|
|
49
|
+
throw new Error('Not subscribed yet.');
|
|
50
|
+
}
|
|
51
|
+
subscription.unsubscribe();
|
|
52
|
+
_reject?.(new DOMException('The request has been aborted.', 'AbortError'));
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
return createAbortablePromise(
|
|
56
|
+
new Promise<T>((resolve, reject) => {
|
|
57
|
+
_reject = reject;
|
|
58
|
+
subscription = observable
|
|
59
|
+
.pipe(
|
|
60
|
+
take(1),
|
|
61
|
+
catchError((e: unknown) => {
|
|
62
|
+
reject(e);
|
|
63
|
+
return EMPTY;
|
|
64
|
+
}),
|
|
65
|
+
)
|
|
66
|
+
.subscribe((x: T) => resolve(x));
|
|
67
|
+
}),
|
|
68
|
+
abortFn,
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function createAbortablePromise<T>(promise: Promise<T>, abortFn: () => void) {
|
|
73
|
+
const thenFn = promise.then.bind(promise);
|
|
74
|
+
const catchFn = promise.catch.bind(promise);
|
|
75
|
+
const finallyFn = promise.finally?.bind(promise);
|
|
76
|
+
return Object.assign(promise, {
|
|
77
|
+
then: (...args: any[]) => createAbortablePromise(thenFn(...args), abortFn),
|
|
78
|
+
catch: (...args: any[]) => createAbortablePromise(catchFn(...args), abortFn),
|
|
79
|
+
finally: finallyFn ? (...args: any[]) => createAbortablePromise(finallyFn(...args), abortFn) : undefined,
|
|
80
|
+
abort: abortFn,
|
|
81
|
+
});
|
|
82
|
+
}
|