@goast/typescript 0.2.0 → 0.2.2

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.
@@ -226,6 +226,29 @@ export class EasyNetworkStubBase {
226
226
  }
227
227
  }
228
228
 
229
+ export type EasyNetworkStubGroup<Group, GroupContainer> = Group & {
230
+ (stubActions: (stubs: Group) => void): GroupContainer;
231
+ };
232
+
233
+ // eslint-disable-next-line @typescript-eslint/ban-types
234
+ declare interface GroupType<T> extends Function {
235
+ new (stubWrapper: EasyNetworkStubWrapper): T;
236
+ }
237
+
238
+ export function createEasyNetworkStubGroup<GroupContainer, Group>(
239
+ container: GroupContainer,
240
+ stubWrapper: EasyNetworkStubWrapper,
241
+ groupType: GroupType<Group>,
242
+ ): EasyNetworkStubGroup<Group, GroupContainer> {
243
+ const group = new groupType(stubWrapper);
244
+ const groupFn = (stubActions: (stubs: Group) => void) => {
245
+ stubActions(group);
246
+ return container;
247
+ };
248
+ Object.setPrototypeOf(groupFn, groupType.prototype);
249
+ return Object.assign(groupFn, group);
250
+ }
251
+
229
252
  function sleep(ms: number): Promise<void> {
230
253
  return new Promise((resolve) => setTimeout(resolve, ms));
231
254
  }
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.rxjs = exports.angular = void 0;
3
+ exports.rxjs = exports.angular = exports.easyNetworkStub = void 0;
4
4
  const tslib_1 = require("tslib");
5
+ exports.easyNetworkStub = tslib_1.__importStar(require("./easy-network-stub"));
5
6
  exports.angular = tslib_1.__importStar(require("./angular"));
6
7
  exports.rxjs = tslib_1.__importStar(require("./rxjs"));
7
8
  tslib_1.__exportStar(require("./typescript"), exports);
@@ -52,7 +52,12 @@ class DefaultTypeScriptAngularServiceGenerator extends file_generator_1.TypeScri
52
52
  ast_1.ts.unionType([
53
53
  ...Object.entries(statusCodes).map(([key, value]) => ast_1.ts.intersectionType([
54
54
  ast_1.ts.refs.angular.httpResponse([value]),
55
- ast_1.ts.objectType({ members: [ast_1.ts.property('status', { type: key })] }),
55
+ ast_1.ts.objectType({
56
+ members: [
57
+ ast_1.ts.property('status', { type: key }),
58
+ ast_1.ts.property('ok', { type: key.startsWith('2') ? 'true' : 'false' }),
59
+ ],
60
+ }),
56
61
  ])),
57
62
  !ctx.config.strictResponseTypes
58
63
  ? ast_1.ts.intersectionType([
@@ -58,11 +58,71 @@ class TypeScriptEasyNetworkStubsGenerator extends core_1.OpenApiServicesGenerati
58
58
  };
59
59
  }
60
60
  getStubsIndexFileContent(ctx) {
61
- return (0, core_1.appendValueGroup)(Object.values(ctx.output.stubs).map((x) => ast_1.ts.export(x.component, x.filePath)));
61
+ return (0, core_1.appendValueGroup)([
62
+ (0, core_1.appendValueGroup)(Object.values(ctx.output.stubs).map((x) => ast_1.ts.export(x.component, x.filePath))),
63
+ this.getStubsClass(ctx),
64
+ ], '\n');
65
+ }
66
+ getStubsClass(ctx) {
67
+ const fieldCasing = (0, core_1.adjustCasing)(ctx.config.propertyNameCasing, { prefix: '_' });
68
+ const serviceNames = Object.fromEntries(ctx.data.services.map((x) => [x.id, x.name]));
69
+ return ast_1.ts.class(this.getStubsClassName(ctx), {
70
+ export: true,
71
+ members: [
72
+ ast_1.ts.property('_stubWrapper', {
73
+ accessModifier: 'private',
74
+ readonly: true,
75
+ type: ctx.refs.easyNetworkStubWrapper(),
76
+ }),
77
+ ...Object.entries(ctx.output.stubs).map(([id, x]) => ast_1.ts.property((0, core_1.toCasing)(serviceNames[id], fieldCasing), {
78
+ accessModifier: 'private',
79
+ optional: true,
80
+ type: ctx.refs.easyNetworkStubGroup([ast_1.ts.reference(x.component, x.filePath), 'this']),
81
+ })),
82
+ ast_1.ts.constructor({
83
+ parameters: [
84
+ ast_1.ts.constructorParameter('stub', { type: ast_1.ts.refs.easyNetworkStub.easyNetworkStub() }),
85
+ ast_1.ts.constructorParameter('options', {
86
+ type: ast_1.ts.refs.partial([ctx.refs.easyNetworkStubWrapperOptions()]),
87
+ optional: true,
88
+ }),
89
+ ],
90
+ body: (0, core_1.appendValueGroup)([(0, core_1.builderTemplate) `this._stubWrapper = new ${ctx.refs.easyNetworkStubWrapper()}(stub, options);`], '\n'),
91
+ }),
92
+ ast_1.ts.property('requests', {
93
+ accessModifier: 'public',
94
+ type: ast_1.ts.arrayType(ctx.refs.stubRequestItem(), { readonly: true }),
95
+ get: ast_1.ts.property.getter({
96
+ body: (0, core_1.appendValueGroup)(['return this._stubWrapper.requests;'], '\n'),
97
+ }),
98
+ }),
99
+ ...Object.entries(ctx.output.stubs).map(([id, x]) => ast_1.ts.property((0, core_1.toCasing)(serviceNames[id], ctx.config.propertyNameCasing), {
100
+ accessModifier: 'public',
101
+ type: ctx.refs.easyNetworkStubGroup([ast_1.ts.reference(x.component, x.filePath), 'this']),
102
+ get: ast_1.ts.property.getter({
103
+ body: (0, core_1.appendValueGroup)([
104
+ (0, core_1.builderTemplate) `return this.${(0, core_1.toCasing)(serviceNames[id], fieldCasing)} ??= ${ctx.refs.createEasyNetworkStubGroup.infer()}(this, this._stubWrapper, ${ast_1.ts.reference(x.component, x.filePath)});`,
105
+ ], '\n'),
106
+ }),
107
+ })),
108
+ ast_1.ts.method('resetStubs', {
109
+ accessModifier: 'public',
110
+ returnType: 'this',
111
+ body: (0, core_1.appendValueGroup)([
112
+ ...Object.keys(ctx.output.stubs).map((id) => `this.${(0, core_1.toCasing)(serviceNames[id], fieldCasing)}?.reset();`),
113
+ 'return this;',
114
+ ], '\n'),
115
+ }),
116
+ ],
117
+ });
62
118
  }
63
119
  getStubsIndexFilePath(ctx) {
64
120
  return ctx.config.stubsIndexFilePath ? (0, path_1.resolve)(ctx.config.outputDir, ctx.config.stubsIndexFilePath) : null;
65
121
  }
122
+ getStubsClassName(ctx) {
123
+ var _a;
124
+ return (0, core_1.toCasing)(`${(_a = ctx.config.domainName) !== null && _a !== void 0 ? _a : ''}_ApiStubs`, ctx.config.typeNameCasing);
125
+ }
66
126
  copyFile(logName, sourceDir, targetDir, fileName) {
67
127
  const source = (0, path_1.resolve)(sourceDir, fileName);
68
128
  const target = (0, path_1.resolve)(targetDir, fileName);
@@ -16,6 +16,8 @@ function getReferenceFactories(options) {
16
16
  getStubResponder: ast_1.ts.reference.genericFactory('getStubResponder', easyNetworkStubUtilsPath),
17
17
  easyNetworkStubWrapper: ast_1.ts.reference.factory('EasyNetworkStubWrapper', easyNetworkStubUtilsPath),
18
18
  easyNetworkStubBase: ast_1.ts.reference.factory('EasyNetworkStubBase', easyNetworkStubUtilsPath),
19
+ easyNetworkStubGroup: ast_1.ts.reference.genericFactory('EasyNetworkStubGroup', easyNetworkStubUtilsPath),
20
+ createEasyNetworkStubGroup: ast_1.ts.reference.genericFactory('createEasyNetworkStubGroup', easyNetworkStubUtilsPath),
19
21
  };
20
22
  }
21
23
  exports.getReferenceFactories = getReferenceFactories;
@@ -18,8 +18,9 @@ class TypeScriptClientsGenerator extends core_1.OpenApiServicesGenerationProvide
18
18
  initResult() {
19
19
  return {
20
20
  clients: {},
21
- clientIndexFilePath: undefined,
22
- clientInterfaceIndexFilePath: undefined,
21
+ indexFiles: {
22
+ clients: undefined,
23
+ },
23
24
  };
24
25
  }
25
26
  buildContext(context, config) {
@@ -31,7 +32,7 @@ class TypeScriptClientsGenerator extends core_1.OpenApiServicesGenerationProvide
31
32
  onGenerate(ctx) {
32
33
  this.copyUtilsFiles(ctx);
33
34
  const output = super.onGenerate(ctx);
34
- output.clientIndexFilePath = this.generateIndexFile(ctx);
35
+ output.indexFiles.clients = this.generateIndexFile(ctx);
35
36
  return output;
36
37
  }
37
38
  generateService(ctx, service) {
@@ -1,3 +1,5 @@
1
+ import * as easyNetworkStub_1 from './easy-network-stub';
2
+ export { easyNetworkStub_1 as easyNetworkStub };
1
3
  import * as angular_1 from './angular';
2
4
  export { angular_1 as angular };
3
5
  import * as rxjs_1 from './rxjs';
@@ -49,7 +49,12 @@ export class DefaultTypeScriptAngularServiceGenerator extends TypeScriptFileGene
49
49
  ts.unionType([
50
50
  ...Object.entries(statusCodes).map(([key, value]) => ts.intersectionType([
51
51
  ts.refs.angular.httpResponse([value]),
52
- ts.objectType({ members: [ts.property('status', { type: key })] }),
52
+ ts.objectType({
53
+ members: [
54
+ ts.property('status', { type: key }),
55
+ ts.property('ok', { type: key.startsWith('2') ? 'true' : 'false' }),
56
+ ],
57
+ }),
53
58
  ])),
54
59
  !ctx.config.strictResponseTypes
55
60
  ? ts.intersectionType([
@@ -1,6 +1,6 @@
1
1
  import { dirname, resolve } from 'path';
2
2
  import { copyFileSync, ensureDirSync } from 'fs-extra';
3
- import { Factory, OpenApiServicesGenerationProviderBase, appendValueGroup, } from '@goast/core';
3
+ import { Factory, OpenApiServicesGenerationProviderBase, adjustCasing, appendValueGroup, toCasing, builderTemplate as s, } from '@goast/core';
4
4
  import { DefaultTypeScriptEasyNetworkStubGenerator, } from './easy-network-stub-generator';
5
5
  import { defaultTypeScriptEasyNetworkStubsGeneratorConfig, } from './models';
6
6
  import { getReferenceFactories } from './refs';
@@ -55,11 +55,71 @@ export class TypeScriptEasyNetworkStubsGenerator extends OpenApiServicesGenerati
55
55
  };
56
56
  }
57
57
  getStubsIndexFileContent(ctx) {
58
- return appendValueGroup(Object.values(ctx.output.stubs).map((x) => ts.export(x.component, x.filePath)));
58
+ return appendValueGroup([
59
+ appendValueGroup(Object.values(ctx.output.stubs).map((x) => ts.export(x.component, x.filePath))),
60
+ this.getStubsClass(ctx),
61
+ ], '\n');
62
+ }
63
+ getStubsClass(ctx) {
64
+ const fieldCasing = adjustCasing(ctx.config.propertyNameCasing, { prefix: '_' });
65
+ const serviceNames = Object.fromEntries(ctx.data.services.map((x) => [x.id, x.name]));
66
+ return ts.class(this.getStubsClassName(ctx), {
67
+ export: true,
68
+ members: [
69
+ ts.property('_stubWrapper', {
70
+ accessModifier: 'private',
71
+ readonly: true,
72
+ type: ctx.refs.easyNetworkStubWrapper(),
73
+ }),
74
+ ...Object.entries(ctx.output.stubs).map(([id, x]) => ts.property(toCasing(serviceNames[id], fieldCasing), {
75
+ accessModifier: 'private',
76
+ optional: true,
77
+ type: ctx.refs.easyNetworkStubGroup([ts.reference(x.component, x.filePath), 'this']),
78
+ })),
79
+ ts.constructor({
80
+ parameters: [
81
+ ts.constructorParameter('stub', { type: ts.refs.easyNetworkStub.easyNetworkStub() }),
82
+ ts.constructorParameter('options', {
83
+ type: ts.refs.partial([ctx.refs.easyNetworkStubWrapperOptions()]),
84
+ optional: true,
85
+ }),
86
+ ],
87
+ body: appendValueGroup([s `this._stubWrapper = new ${ctx.refs.easyNetworkStubWrapper()}(stub, options);`], '\n'),
88
+ }),
89
+ ts.property('requests', {
90
+ accessModifier: 'public',
91
+ type: ts.arrayType(ctx.refs.stubRequestItem(), { readonly: true }),
92
+ get: ts.property.getter({
93
+ body: appendValueGroup(['return this._stubWrapper.requests;'], '\n'),
94
+ }),
95
+ }),
96
+ ...Object.entries(ctx.output.stubs).map(([id, x]) => ts.property(toCasing(serviceNames[id], ctx.config.propertyNameCasing), {
97
+ accessModifier: 'public',
98
+ type: ctx.refs.easyNetworkStubGroup([ts.reference(x.component, x.filePath), 'this']),
99
+ get: ts.property.getter({
100
+ body: appendValueGroup([
101
+ s `return this.${toCasing(serviceNames[id], fieldCasing)} ??= ${ctx.refs.createEasyNetworkStubGroup.infer()}(this, this._stubWrapper, ${ts.reference(x.component, x.filePath)});`,
102
+ ], '\n'),
103
+ }),
104
+ })),
105
+ ts.method('resetStubs', {
106
+ accessModifier: 'public',
107
+ returnType: 'this',
108
+ body: appendValueGroup([
109
+ ...Object.keys(ctx.output.stubs).map((id) => `this.${toCasing(serviceNames[id], fieldCasing)}?.reset();`),
110
+ 'return this;',
111
+ ], '\n'),
112
+ }),
113
+ ],
114
+ });
59
115
  }
60
116
  getStubsIndexFilePath(ctx) {
61
117
  return ctx.config.stubsIndexFilePath ? resolve(ctx.config.outputDir, ctx.config.stubsIndexFilePath) : null;
62
118
  }
119
+ getStubsClassName(ctx) {
120
+ var _a;
121
+ return toCasing(`${(_a = ctx.config.domainName) !== null && _a !== void 0 ? _a : ''}_ApiStubs`, ctx.config.typeNameCasing);
122
+ }
63
123
  copyFile(logName, sourceDir, targetDir, fileName) {
64
124
  const source = resolve(sourceDir, fileName);
65
125
  const target = resolve(targetDir, fileName);
@@ -13,5 +13,7 @@ export function getReferenceFactories(options) {
13
13
  getStubResponder: ts.reference.genericFactory('getStubResponder', easyNetworkStubUtilsPath),
14
14
  easyNetworkStubWrapper: ts.reference.factory('EasyNetworkStubWrapper', easyNetworkStubUtilsPath),
15
15
  easyNetworkStubBase: ts.reference.factory('EasyNetworkStubBase', easyNetworkStubUtilsPath),
16
+ easyNetworkStubGroup: ts.reference.genericFactory('EasyNetworkStubGroup', easyNetworkStubUtilsPath),
17
+ createEasyNetworkStubGroup: ts.reference.genericFactory('createEasyNetworkStubGroup', easyNetworkStubUtilsPath),
16
18
  };
17
19
  }
@@ -15,8 +15,9 @@ export class TypeScriptClientsGenerator extends OpenApiServicesGenerationProvide
15
15
  initResult() {
16
16
  return {
17
17
  clients: {},
18
- clientIndexFilePath: undefined,
19
- clientInterfaceIndexFilePath: undefined,
18
+ indexFiles: {
19
+ clients: undefined,
20
+ },
20
21
  };
21
22
  }
22
23
  buildContext(context, config) {
@@ -28,7 +29,7 @@ export class TypeScriptClientsGenerator extends OpenApiServicesGenerationProvide
28
29
  onGenerate(ctx) {
29
30
  this.copyUtilsFiles(ctx);
30
31
  const output = super.onGenerate(ctx);
31
- output.clientIndexFilePath = this.generateIndexFile(ctx);
32
+ output.indexFiles.clients = this.generateIndexFile(ctx);
32
33
  return output;
33
34
  }
34
35
  generateService(ctx, service) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@goast/typescript",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "repository": "https://github.com/MaSch0212/goast.git",
5
5
  "author": {
6
6
  "name": "Marc Schmidt (MaSch0212)",
@@ -12,7 +12,7 @@
12
12
  "fs-extra": "^11.2.0",
13
13
  "node-fetch": "^2.7.0",
14
14
  "yaml": "^2.4.1",
15
- "@goast/core": "0.2.0",
15
+ "@goast/core": "0.2.1",
16
16
  "tslib": "2.6.2"
17
17
  },
18
18
  "module": "./esm/index.js",
@@ -1,3 +1,4 @@
1
+ export * as easyNetworkStub from './easy-network-stub';
1
2
  export * as angular from './angular';
2
3
  export * as rxjs from './rxjs';
3
4
  export * from './typescript';
@@ -1,6 +1,7 @@
1
1
  import { ApiService, AppendValueGroup, Factory, OpenApiGeneratorContext, OpenApiServicesGenerationProviderBase } from '@goast/core';
2
2
  import { TypeScriptEasyNetworkStubGenerator } from './easy-network-stub-generator';
3
3
  import { TypeScriptEasyNetworkStubsGeneratorInput, TypeScriptEasyNetworkStubsGeneratorOutput, TypeScriptEasyNetworkStubsGeneratorConfig, TypeScriptEasyNetworkStubGeneratorOutput, TypeScriptEasyNetworkStubsGeneratorContext } from './models';
4
+ import { ts } from '../../../ast';
4
5
  import { TypeScriptExportOutput } from '../../../common-results';
5
6
  import { TypeScriptFileBuilder } from '../../../file-builder';
6
7
  type Input = TypeScriptEasyNetworkStubsGeneratorInput;
@@ -19,7 +20,9 @@ export declare class TypeScriptEasyNetworkStubsGenerator extends OpenApiServices
19
20
  protected generateUtilsFiles(ctx: Context): void;
20
21
  protected generateIndexFiles(ctx: Context): Output['indexFiles'];
21
22
  protected getStubsIndexFileContent(ctx: Context): AppendValueGroup<TypeScriptFileBuilder>;
23
+ protected getStubsClass(ctx: Context): ts.Class<TypeScriptFileBuilder>;
22
24
  protected getStubsIndexFilePath(ctx: Context): string | null;
25
+ protected getStubsClassName(ctx: Context): string;
23
26
  private copyFile;
24
27
  }
25
28
  export {};
@@ -6,6 +6,10 @@ import { TypeScriptGeneratorConfig } from '../../../config';
6
6
  import { TypeScriptFileBuilder } from '../../../file-builder';
7
7
  import { TypeScriptModelGeneratorOutput } from '../../models';
8
8
  export type TypeScriptEasyNetworkStubsGeneratorConfig = TypeScriptGeneratorConfig & {
9
+ /**
10
+ * The domain name of the API. Used as a prefix for exported components (e.g. `ApiConfiguration`).
11
+ */
12
+ domainName?: string;
9
13
  /**
10
14
  * The default response types for status codes that are not defined in the OpenAPI specification.
11
15
  * @example
@@ -46,4 +46,18 @@ export declare function getReferenceFactories(options: TypeScriptEasyNetworkStub
46
46
  } & {
47
47
  moduleNameOrfilePath: string;
48
48
  };
49
+ easyNetworkStubGroup: (<TBuilder_1 extends import("@goast/core").SourceBuilder<never>>(generics: [ts.Type<TBuilder_1>, ts.Type<TBuilder_1>]) => ts.Reference<TBuilder_1, never>) & {
50
+ refName: string;
51
+ moduleNameOrfilePath: import("@goast/core").Nullable<string>;
52
+ infer: <TBuilder_2 extends import("@goast/core").SourceBuilder<never>>() => ts.Reference<TBuilder_2, never>;
53
+ } & {
54
+ moduleNameOrfilePath: string;
55
+ };
56
+ createEasyNetworkStubGroup: (<TBuilder_1 extends import("@goast/core").SourceBuilder<never>>(generics: [ts.Type<TBuilder_1>, ts.Type<TBuilder_1>]) => ts.Reference<TBuilder_1, never>) & {
57
+ refName: string;
58
+ moduleNameOrfilePath: import("@goast/core").Nullable<string>;
59
+ infer: <TBuilder_2 extends import("@goast/core").SourceBuilder<never>>() => ts.Reference<TBuilder_2, never>;
60
+ } & {
61
+ moduleNameOrfilePath: string;
62
+ };
49
63
  };
@@ -22,8 +22,9 @@ export type TypeScriptFetchClientsGeneratorOutput = {
22
22
  clients: {
23
23
  [serviceId: string]: TypeScriptFetchClientGeneratorOutput;
24
24
  };
25
- clientIndexFilePath: string | undefined;
26
- clientInterfaceIndexFilePath: string | undefined;
25
+ indexFiles: {
26
+ clients: Nullable<string>;
27
+ };
27
28
  };
28
29
  export type TypeScriptFetchClientGeneratorOutput = {
29
30
  class?: Omit<TypeScriptComponentOutput, 'additionalImports'>;