ng-openapi 0.0.25-alpha.0 → 0.0.25-alpha.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.
- package/cli.cjs +357 -220
- package/index.js +362 -216
- package/package.json +1 -1
package/cli.cjs
CHANGED
|
@@ -26,7 +26,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
26
26
|
|
|
27
27
|
// src/lib/cli.ts
|
|
28
28
|
var import_commander = require("commander");
|
|
29
|
-
var
|
|
29
|
+
var path9 = __toESM(require("path"));
|
|
30
30
|
var fs4 = __toESM(require("fs"));
|
|
31
31
|
|
|
32
32
|
// src/lib/core/swagger-parser.ts
|
|
@@ -58,7 +58,7 @@ var SwaggerParser = class {
|
|
|
58
58
|
};
|
|
59
59
|
|
|
60
60
|
// src/lib/core/generator.ts
|
|
61
|
-
var
|
|
61
|
+
var import_ts_morph5 = require("ts-morph");
|
|
62
62
|
|
|
63
63
|
// src/lib/generators/type/type.generator.ts
|
|
64
64
|
var import_ts_morph = require("ts-morph");
|
|
@@ -93,6 +93,10 @@ var PROVIDER_GENERATOR_HEADER_COMMENT = defaultHeaderComment + `* Generated prov
|
|
|
93
93
|
* Do not edit this file manually
|
|
94
94
|
*/
|
|
95
95
|
`;
|
|
96
|
+
var BASE_INTERCEPTOR_HEADER_COMMENT = /* @__PURE__ */ __name((clientName) => defaultHeaderComment + `* Generated Base Interceptor for client ${clientName}
|
|
97
|
+
* Do not edit this file manually
|
|
98
|
+
*/
|
|
99
|
+
`, "BASE_INTERCEPTOR_HEADER_COMMENT");
|
|
96
100
|
|
|
97
101
|
// src/lib/generators/type/type.generator.ts
|
|
98
102
|
var TypeGenerator = class {
|
|
@@ -803,9 +807,332 @@ var MainIndexGenerator = class {
|
|
|
803
807
|
}
|
|
804
808
|
};
|
|
805
809
|
|
|
806
|
-
// src/lib/generators/
|
|
807
|
-
var import_ts_morph3 = require("ts-morph");
|
|
810
|
+
// src/lib/generators/utility/provider.generator.ts
|
|
808
811
|
var path5 = __toESM(require("path"));
|
|
812
|
+
var ProviderGenerator = class {
|
|
813
|
+
static {
|
|
814
|
+
__name(this, "ProviderGenerator");
|
|
815
|
+
}
|
|
816
|
+
project;
|
|
817
|
+
config;
|
|
818
|
+
clientName;
|
|
819
|
+
constructor(project, config) {
|
|
820
|
+
this.project = project;
|
|
821
|
+
this.config = config;
|
|
822
|
+
this.clientName = config.clientName || "default";
|
|
823
|
+
}
|
|
824
|
+
generate(outputDir) {
|
|
825
|
+
const filePath = path5.join(outputDir, "providers.ts");
|
|
826
|
+
const sourceFile = this.project.createSourceFile(filePath, "", {
|
|
827
|
+
overwrite: true
|
|
828
|
+
});
|
|
829
|
+
sourceFile.insertText(0, PROVIDER_GENERATOR_HEADER_COMMENT);
|
|
830
|
+
const basePathTokenName = this.getBasePathTokenName();
|
|
831
|
+
const interceptorsTokenName = this.getInterceptorsTokenName();
|
|
832
|
+
const baseInterceptorClassName = `${this.capitalizeFirst(this.clientName)}BaseInterceptor`;
|
|
833
|
+
sourceFile.addImportDeclarations([
|
|
834
|
+
{
|
|
835
|
+
namedImports: [
|
|
836
|
+
"EnvironmentProviders",
|
|
837
|
+
"Provider",
|
|
838
|
+
"makeEnvironmentProviders"
|
|
839
|
+
],
|
|
840
|
+
moduleSpecifier: "@angular/core"
|
|
841
|
+
},
|
|
842
|
+
{
|
|
843
|
+
namedImports: [
|
|
844
|
+
"HTTP_INTERCEPTORS",
|
|
845
|
+
"HttpInterceptor"
|
|
846
|
+
],
|
|
847
|
+
moduleSpecifier: "@angular/common/http"
|
|
848
|
+
},
|
|
849
|
+
{
|
|
850
|
+
namedImports: [
|
|
851
|
+
basePathTokenName,
|
|
852
|
+
interceptorsTokenName
|
|
853
|
+
],
|
|
854
|
+
moduleSpecifier: "./tokens"
|
|
855
|
+
},
|
|
856
|
+
{
|
|
857
|
+
namedImports: [
|
|
858
|
+
baseInterceptorClassName
|
|
859
|
+
],
|
|
860
|
+
moduleSpecifier: "./utils/base-interceptor"
|
|
861
|
+
}
|
|
862
|
+
]);
|
|
863
|
+
if (this.config.options.dateType === "Date") {
|
|
864
|
+
sourceFile.addImportDeclaration({
|
|
865
|
+
namedImports: [
|
|
866
|
+
"DateInterceptor"
|
|
867
|
+
],
|
|
868
|
+
moduleSpecifier: "./utils/date-transformer"
|
|
869
|
+
});
|
|
870
|
+
}
|
|
871
|
+
sourceFile.addInterface({
|
|
872
|
+
name: `${this.capitalizeFirst(this.clientName)}Config`,
|
|
873
|
+
isExported: true,
|
|
874
|
+
docs: [
|
|
875
|
+
`Configuration options for ${this.clientName} client`
|
|
876
|
+
],
|
|
877
|
+
properties: [
|
|
878
|
+
{
|
|
879
|
+
name: "basePath",
|
|
880
|
+
type: "string",
|
|
881
|
+
docs: [
|
|
882
|
+
"Base API URL"
|
|
883
|
+
]
|
|
884
|
+
},
|
|
885
|
+
{
|
|
886
|
+
name: "enableDateTransform",
|
|
887
|
+
type: "boolean",
|
|
888
|
+
hasQuestionToken: true,
|
|
889
|
+
docs: [
|
|
890
|
+
"Enable automatic date transformation (default: true)"
|
|
891
|
+
]
|
|
892
|
+
},
|
|
893
|
+
{
|
|
894
|
+
name: "interceptors",
|
|
895
|
+
type: "HttpInterceptor[]",
|
|
896
|
+
hasQuestionToken: true,
|
|
897
|
+
docs: [
|
|
898
|
+
"Array of HTTP interceptors to apply to this client"
|
|
899
|
+
]
|
|
900
|
+
}
|
|
901
|
+
]
|
|
902
|
+
});
|
|
903
|
+
this.addMainProviderFunction(sourceFile, basePathTokenName, interceptorsTokenName, baseInterceptorClassName);
|
|
904
|
+
sourceFile.saveSync();
|
|
905
|
+
}
|
|
906
|
+
addMainProviderFunction(sourceFile, basePathTokenName, interceptorsTokenName, baseInterceptorClassName) {
|
|
907
|
+
const hasDateInterceptor = this.config.options.dateType === "Date";
|
|
908
|
+
const functionName = `provide${this.capitalizeFirst(this.clientName)}Client`;
|
|
909
|
+
const configTypeName = `${this.capitalizeFirst(this.clientName)}Config`;
|
|
910
|
+
const functionBody = `
|
|
911
|
+
const providers: Provider[] = [
|
|
912
|
+
// Base path token for this client
|
|
913
|
+
{
|
|
914
|
+
provide: ${basePathTokenName},
|
|
915
|
+
useValue: config.basePath
|
|
916
|
+
},
|
|
917
|
+
// Client-specific interceptors token
|
|
918
|
+
{
|
|
919
|
+
provide: ${interceptorsTokenName},
|
|
920
|
+
useValue: config.interceptors || []
|
|
921
|
+
},
|
|
922
|
+
// Base interceptor that handles client-specific interceptors
|
|
923
|
+
{
|
|
924
|
+
provide: HTTP_INTERCEPTORS,
|
|
925
|
+
useClass: ${baseInterceptorClassName},
|
|
926
|
+
multi: true
|
|
927
|
+
}
|
|
928
|
+
];
|
|
929
|
+
|
|
930
|
+
${hasDateInterceptor ? `// Add date interceptor to client-specific interceptors if enabled
|
|
931
|
+
if (config.enableDateTransform !== false) {
|
|
932
|
+
const currentInterceptors = config.interceptors || [];
|
|
933
|
+
providers.push({
|
|
934
|
+
provide: ${interceptorsTokenName},
|
|
935
|
+
useValue: [new DateInterceptor(), ...currentInterceptors]
|
|
936
|
+
});
|
|
937
|
+
}` : `// Date transformation not available (dateType: 'string' was used in generation)`}
|
|
938
|
+
|
|
939
|
+
return makeEnvironmentProviders(providers);`;
|
|
940
|
+
sourceFile.addFunction({
|
|
941
|
+
name: functionName,
|
|
942
|
+
isExported: true,
|
|
943
|
+
docs: [
|
|
944
|
+
`Provides configuration for ${this.clientName} client`,
|
|
945
|
+
"",
|
|
946
|
+
"@example",
|
|
947
|
+
"```typescript",
|
|
948
|
+
"// In your app.config.ts",
|
|
949
|
+
`import { ${functionName} } from './api/providers';`,
|
|
950
|
+
"",
|
|
951
|
+
"export const appConfig: ApplicationConfig = {",
|
|
952
|
+
" providers: [",
|
|
953
|
+
` ${functionName}({`,
|
|
954
|
+
" basePath: 'https://api.example.com',",
|
|
955
|
+
" interceptors: [new LoggingInterceptor(), new AuthInterceptor()]",
|
|
956
|
+
" }),",
|
|
957
|
+
" // other providers...",
|
|
958
|
+
" ]",
|
|
959
|
+
"};",
|
|
960
|
+
"```"
|
|
961
|
+
],
|
|
962
|
+
parameters: [
|
|
963
|
+
{
|
|
964
|
+
name: "config",
|
|
965
|
+
type: configTypeName
|
|
966
|
+
}
|
|
967
|
+
],
|
|
968
|
+
returnType: "EnvironmentProviders",
|
|
969
|
+
statements: functionBody
|
|
970
|
+
});
|
|
971
|
+
if (this.clientName === "default") {
|
|
972
|
+
sourceFile.addFunction({
|
|
973
|
+
name: "provideNgOpenapi",
|
|
974
|
+
isExported: true,
|
|
975
|
+
docs: [
|
|
976
|
+
"@deprecated Use provideDefaultClient instead for better clarity",
|
|
977
|
+
"Provides configuration for the default client"
|
|
978
|
+
],
|
|
979
|
+
parameters: [
|
|
980
|
+
{
|
|
981
|
+
name: "config",
|
|
982
|
+
type: configTypeName
|
|
983
|
+
}
|
|
984
|
+
],
|
|
985
|
+
returnType: "EnvironmentProviders",
|
|
986
|
+
statements: `return ${functionName}(config);`
|
|
987
|
+
});
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
getBasePathTokenName() {
|
|
991
|
+
const clientSuffix = this.clientName.toUpperCase().replace(/[^A-Z0-9]/g, "_");
|
|
992
|
+
return `BASE_PATH_${clientSuffix}`;
|
|
993
|
+
}
|
|
994
|
+
getInterceptorsTokenName() {
|
|
995
|
+
const clientSuffix = this.clientName.toUpperCase().replace(/[^A-Z0-9]/g, "_");
|
|
996
|
+
return `HTTP_INTERCEPTORS_${clientSuffix}`;
|
|
997
|
+
}
|
|
998
|
+
capitalizeFirst(str) {
|
|
999
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
1000
|
+
}
|
|
1001
|
+
};
|
|
1002
|
+
|
|
1003
|
+
// src/lib/generators/utility/base-interceptor.generator.ts
|
|
1004
|
+
var import_ts_morph3 = require("ts-morph");
|
|
1005
|
+
var path6 = __toESM(require("path"));
|
|
1006
|
+
var BaseInterceptorGenerator = class {
|
|
1007
|
+
static {
|
|
1008
|
+
__name(this, "BaseInterceptorGenerator");
|
|
1009
|
+
}
|
|
1010
|
+
#project;
|
|
1011
|
+
#clientName;
|
|
1012
|
+
constructor(project, clientName = "default") {
|
|
1013
|
+
this.#project = project;
|
|
1014
|
+
this.#clientName = clientName;
|
|
1015
|
+
}
|
|
1016
|
+
generate(outputDir) {
|
|
1017
|
+
const utilsDir = path6.join(outputDir, "utils");
|
|
1018
|
+
const filePath = path6.join(utilsDir, "base-interceptor.ts");
|
|
1019
|
+
const sourceFile = this.#project.createSourceFile(filePath, "", {
|
|
1020
|
+
overwrite: true
|
|
1021
|
+
});
|
|
1022
|
+
sourceFile.insertText(0, BASE_INTERCEPTOR_HEADER_COMMENT(this.#clientName));
|
|
1023
|
+
const basePathTokenName = this.getBasePathTokenName();
|
|
1024
|
+
const interceptorsTokenName = this.getInterceptorsTokenName();
|
|
1025
|
+
sourceFile.addImportDeclarations([
|
|
1026
|
+
{
|
|
1027
|
+
namedImports: [
|
|
1028
|
+
"HttpEvent",
|
|
1029
|
+
"HttpHandler",
|
|
1030
|
+
"HttpInterceptor",
|
|
1031
|
+
"HttpRequest"
|
|
1032
|
+
],
|
|
1033
|
+
moduleSpecifier: "@angular/common/http"
|
|
1034
|
+
},
|
|
1035
|
+
{
|
|
1036
|
+
namedImports: [
|
|
1037
|
+
"inject",
|
|
1038
|
+
"Injectable"
|
|
1039
|
+
],
|
|
1040
|
+
moduleSpecifier: "@angular/core"
|
|
1041
|
+
},
|
|
1042
|
+
{
|
|
1043
|
+
namedImports: [
|
|
1044
|
+
"Observable"
|
|
1045
|
+
],
|
|
1046
|
+
moduleSpecifier: "rxjs"
|
|
1047
|
+
},
|
|
1048
|
+
{
|
|
1049
|
+
namedImports: [
|
|
1050
|
+
basePathTokenName,
|
|
1051
|
+
interceptorsTokenName
|
|
1052
|
+
],
|
|
1053
|
+
moduleSpecifier: "../tokens"
|
|
1054
|
+
}
|
|
1055
|
+
]);
|
|
1056
|
+
sourceFile.addClass({
|
|
1057
|
+
name: `${this.capitalizeFirst(this.#clientName)}BaseInterceptor`,
|
|
1058
|
+
isExported: true,
|
|
1059
|
+
decorators: [
|
|
1060
|
+
{
|
|
1061
|
+
name: "Injectable",
|
|
1062
|
+
arguments: []
|
|
1063
|
+
}
|
|
1064
|
+
],
|
|
1065
|
+
implements: [
|
|
1066
|
+
"HttpInterceptor"
|
|
1067
|
+
],
|
|
1068
|
+
properties: [
|
|
1069
|
+
{
|
|
1070
|
+
name: "basePath",
|
|
1071
|
+
type: "string",
|
|
1072
|
+
scope: import_ts_morph3.Scope.Private,
|
|
1073
|
+
isReadonly: true,
|
|
1074
|
+
initializer: `inject(${basePathTokenName})`
|
|
1075
|
+
},
|
|
1076
|
+
{
|
|
1077
|
+
name: "httpInterceptors",
|
|
1078
|
+
type: "HttpInterceptor[]",
|
|
1079
|
+
scope: import_ts_morph3.Scope.Private,
|
|
1080
|
+
isReadonly: true,
|
|
1081
|
+
initializer: `inject(${interceptorsTokenName})`
|
|
1082
|
+
}
|
|
1083
|
+
],
|
|
1084
|
+
methods: [
|
|
1085
|
+
{
|
|
1086
|
+
name: "intercept",
|
|
1087
|
+
parameters: [
|
|
1088
|
+
{
|
|
1089
|
+
name: "req",
|
|
1090
|
+
type: "HttpRequest<any>"
|
|
1091
|
+
},
|
|
1092
|
+
{
|
|
1093
|
+
name: "next",
|
|
1094
|
+
type: "HttpHandler"
|
|
1095
|
+
}
|
|
1096
|
+
],
|
|
1097
|
+
returnType: "Observable<HttpEvent<any>>",
|
|
1098
|
+
statements: `
|
|
1099
|
+
// Only intercept requests to this client's base path
|
|
1100
|
+
if (!req.url.startsWith(this.basePath)) {
|
|
1101
|
+
return next.handle(req);
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
// Apply client-specific interceptors in reverse order
|
|
1105
|
+
let handler = next;
|
|
1106
|
+
|
|
1107
|
+
handler = this.httpInterceptors.reduceRight(
|
|
1108
|
+
(next, interceptor) => ({
|
|
1109
|
+
handle: (request: HttpRequest<any>) => interceptor.intercept(request, next)
|
|
1110
|
+
}),
|
|
1111
|
+
handler
|
|
1112
|
+
);
|
|
1113
|
+
|
|
1114
|
+
return handler.handle(req);`
|
|
1115
|
+
}
|
|
1116
|
+
]
|
|
1117
|
+
});
|
|
1118
|
+
sourceFile.saveSync();
|
|
1119
|
+
}
|
|
1120
|
+
getBasePathTokenName() {
|
|
1121
|
+
const clientSuffix = this.#clientName.toUpperCase().replace(/[^A-Z0-9]/g, "_");
|
|
1122
|
+
return `BASE_PATH_${clientSuffix}`;
|
|
1123
|
+
}
|
|
1124
|
+
getInterceptorsTokenName() {
|
|
1125
|
+
const clientSuffix = this.#clientName.toUpperCase().replace(/[^A-Z0-9]/g, "_");
|
|
1126
|
+
return `HTTP_INTERCEPTORS_${clientSuffix}`;
|
|
1127
|
+
}
|
|
1128
|
+
capitalizeFirst(str) {
|
|
1129
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
1130
|
+
}
|
|
1131
|
+
};
|
|
1132
|
+
|
|
1133
|
+
// src/lib/generators/service/service.generator.ts
|
|
1134
|
+
var import_ts_morph4 = require("ts-morph");
|
|
1135
|
+
var path7 = __toESM(require("path"));
|
|
809
1136
|
|
|
810
1137
|
// src/lib/utils/string.utils.ts
|
|
811
1138
|
function camelCase(str) {
|
|
@@ -1597,7 +1924,7 @@ var ServiceGenerator = class {
|
|
|
1597
1924
|
this.methodGenerator = new ServiceMethodGenerator(config);
|
|
1598
1925
|
}
|
|
1599
1926
|
generate(outputRoot) {
|
|
1600
|
-
const outputDir =
|
|
1927
|
+
const outputDir = path7.join(outputRoot, "services");
|
|
1601
1928
|
const paths = this.extractPaths();
|
|
1602
1929
|
const controllerGroups = this.groupPathsByController(paths);
|
|
1603
1930
|
Object.entries(controllerGroups).forEach(([controllerName, operations]) => {
|
|
@@ -1607,7 +1934,7 @@ var ServiceGenerator = class {
|
|
|
1607
1934
|
extractPaths() {
|
|
1608
1935
|
const paths = [];
|
|
1609
1936
|
const swaggerPaths = this.spec.paths || {};
|
|
1610
|
-
Object.entries(swaggerPaths).forEach(([
|
|
1937
|
+
Object.entries(swaggerPaths).forEach(([path10, pathItem]) => {
|
|
1611
1938
|
const methods = [
|
|
1612
1939
|
"get",
|
|
1613
1940
|
"post",
|
|
@@ -1621,7 +1948,7 @@ var ServiceGenerator = class {
|
|
|
1621
1948
|
if (pathItem[method]) {
|
|
1622
1949
|
const operation = pathItem[method];
|
|
1623
1950
|
paths.push({
|
|
1624
|
-
path:
|
|
1951
|
+
path: path10,
|
|
1625
1952
|
method: method.toUpperCase(),
|
|
1626
1953
|
operationId: operation.operationId,
|
|
1627
1954
|
summary: operation.summary,
|
|
@@ -1653,12 +1980,12 @@ var ServiceGenerator = class {
|
|
|
1653
1980
|
}
|
|
1654
1981
|
groupPathsByController(paths) {
|
|
1655
1982
|
const groups = {};
|
|
1656
|
-
paths.forEach((
|
|
1983
|
+
paths.forEach((path10) => {
|
|
1657
1984
|
let controllerName = "Default";
|
|
1658
|
-
if (
|
|
1659
|
-
controllerName =
|
|
1985
|
+
if (path10.tags && path10.tags.length > 0) {
|
|
1986
|
+
controllerName = path10.tags[0];
|
|
1660
1987
|
} else {
|
|
1661
|
-
const pathParts =
|
|
1988
|
+
const pathParts = path10.path.split("/").filter((p) => p && !p.startsWith("{"));
|
|
1662
1989
|
if (pathParts.length > 1) {
|
|
1663
1990
|
controllerName = pascalCase(pathParts[1]);
|
|
1664
1991
|
}
|
|
@@ -1667,13 +1994,13 @@ var ServiceGenerator = class {
|
|
|
1667
1994
|
if (!groups[controllerName]) {
|
|
1668
1995
|
groups[controllerName] = [];
|
|
1669
1996
|
}
|
|
1670
|
-
groups[controllerName].push(
|
|
1997
|
+
groups[controllerName].push(path10);
|
|
1671
1998
|
});
|
|
1672
1999
|
return groups;
|
|
1673
2000
|
}
|
|
1674
2001
|
generateServiceFile(controllerName, operations, outputDir) {
|
|
1675
2002
|
const fileName = `${camelCase(controllerName)}.service.ts`;
|
|
1676
|
-
const filePath =
|
|
2003
|
+
const filePath = path7.join(outputDir, fileName);
|
|
1677
2004
|
const sourceFile = this.project.createSourceFile(filePath, "", {
|
|
1678
2005
|
overwrite: true
|
|
1679
2006
|
});
|
|
@@ -1807,14 +2134,14 @@ var ServiceGenerator = class {
|
|
|
1807
2134
|
serviceClass.addProperty({
|
|
1808
2135
|
name: "httpClient",
|
|
1809
2136
|
type: "HttpClient",
|
|
1810
|
-
scope:
|
|
2137
|
+
scope: import_ts_morph4.Scope.Private,
|
|
1811
2138
|
isReadonly: true,
|
|
1812
2139
|
initializer: "inject(HttpClient)"
|
|
1813
2140
|
});
|
|
1814
2141
|
serviceClass.addProperty({
|
|
1815
2142
|
name: "basePath",
|
|
1816
2143
|
type: "string",
|
|
1817
|
-
scope:
|
|
2144
|
+
scope: import_ts_morph4.Scope.Private,
|
|
1818
2145
|
isReadonly: true,
|
|
1819
2146
|
initializer: `inject(${basePathTokenName})`
|
|
1820
2147
|
});
|
|
@@ -1837,7 +2164,7 @@ var ServiceGenerator = class {
|
|
|
1837
2164
|
|
|
1838
2165
|
// src/lib/generators/service/service-index.generator.ts
|
|
1839
2166
|
var fs2 = __toESM(require("fs"));
|
|
1840
|
-
var
|
|
2167
|
+
var path8 = __toESM(require("path"));
|
|
1841
2168
|
var ServiceIndexGenerator = class {
|
|
1842
2169
|
static {
|
|
1843
2170
|
__name(this, "ServiceIndexGenerator");
|
|
@@ -1847,8 +2174,8 @@ var ServiceIndexGenerator = class {
|
|
|
1847
2174
|
this.project = project;
|
|
1848
2175
|
}
|
|
1849
2176
|
generateIndex(outputRoot) {
|
|
1850
|
-
const servicesDir =
|
|
1851
|
-
const indexPath =
|
|
2177
|
+
const servicesDir = path8.join(outputRoot, "services");
|
|
2178
|
+
const indexPath = path8.join(servicesDir, "index.ts");
|
|
1852
2179
|
const sourceFile = this.project.createSourceFile(indexPath, "", {
|
|
1853
2180
|
overwrite: true
|
|
1854
2181
|
});
|
|
@@ -1867,199 +2194,6 @@ var ServiceIndexGenerator = class {
|
|
|
1867
2194
|
}
|
|
1868
2195
|
};
|
|
1869
2196
|
|
|
1870
|
-
// src/lib/generators/utility/provider.generator.ts
|
|
1871
|
-
var path7 = __toESM(require("path"));
|
|
1872
|
-
var ProviderGenerator = class {
|
|
1873
|
-
static {
|
|
1874
|
-
__name(this, "ProviderGenerator");
|
|
1875
|
-
}
|
|
1876
|
-
project;
|
|
1877
|
-
config;
|
|
1878
|
-
clientName;
|
|
1879
|
-
constructor(project, config) {
|
|
1880
|
-
this.project = project;
|
|
1881
|
-
this.config = config;
|
|
1882
|
-
this.clientName = config.clientName || "default";
|
|
1883
|
-
}
|
|
1884
|
-
generate(outputDir) {
|
|
1885
|
-
const filePath = path7.join(outputDir, "providers.ts");
|
|
1886
|
-
const sourceFile = this.project.createSourceFile(filePath, "", {
|
|
1887
|
-
overwrite: true
|
|
1888
|
-
});
|
|
1889
|
-
sourceFile.insertText(0, PROVIDER_GENERATOR_HEADER_COMMENT);
|
|
1890
|
-
const basePathTokenName = this.getBasePathTokenName();
|
|
1891
|
-
const interceptorsTokenName = this.getInterceptorsTokenName();
|
|
1892
|
-
const baseInterceptorClassName = `${this.capitalizeFirst(this.clientName)}BaseInterceptor`;
|
|
1893
|
-
sourceFile.addImportDeclarations([
|
|
1894
|
-
{
|
|
1895
|
-
namedImports: [
|
|
1896
|
-
"EnvironmentProviders",
|
|
1897
|
-
"Provider",
|
|
1898
|
-
"makeEnvironmentProviders"
|
|
1899
|
-
],
|
|
1900
|
-
moduleSpecifier: "@angular/core"
|
|
1901
|
-
},
|
|
1902
|
-
{
|
|
1903
|
-
namedImports: [
|
|
1904
|
-
"HTTP_INTERCEPTORS",
|
|
1905
|
-
"HttpInterceptor"
|
|
1906
|
-
],
|
|
1907
|
-
moduleSpecifier: "@angular/common/http"
|
|
1908
|
-
},
|
|
1909
|
-
{
|
|
1910
|
-
namedImports: [
|
|
1911
|
-
basePathTokenName,
|
|
1912
|
-
interceptorsTokenName
|
|
1913
|
-
],
|
|
1914
|
-
moduleSpecifier: "./tokens"
|
|
1915
|
-
},
|
|
1916
|
-
{
|
|
1917
|
-
namedImports: [
|
|
1918
|
-
baseInterceptorClassName
|
|
1919
|
-
],
|
|
1920
|
-
moduleSpecifier: "./utils/base-interceptor"
|
|
1921
|
-
}
|
|
1922
|
-
]);
|
|
1923
|
-
if (this.config.options.dateType === "Date") {
|
|
1924
|
-
sourceFile.addImportDeclaration({
|
|
1925
|
-
namedImports: [
|
|
1926
|
-
"DateInterceptor"
|
|
1927
|
-
],
|
|
1928
|
-
moduleSpecifier: "./utils/date-transformer"
|
|
1929
|
-
});
|
|
1930
|
-
}
|
|
1931
|
-
sourceFile.addInterface({
|
|
1932
|
-
name: `${this.capitalizeFirst(this.clientName)}Config`,
|
|
1933
|
-
isExported: true,
|
|
1934
|
-
docs: [
|
|
1935
|
-
`Configuration options for ${this.clientName} client`
|
|
1936
|
-
],
|
|
1937
|
-
properties: [
|
|
1938
|
-
{
|
|
1939
|
-
name: "basePath",
|
|
1940
|
-
type: "string",
|
|
1941
|
-
docs: [
|
|
1942
|
-
"Base API URL"
|
|
1943
|
-
]
|
|
1944
|
-
},
|
|
1945
|
-
{
|
|
1946
|
-
name: "enableDateTransform",
|
|
1947
|
-
type: "boolean",
|
|
1948
|
-
hasQuestionToken: true,
|
|
1949
|
-
docs: [
|
|
1950
|
-
"Enable automatic date transformation (default: true)"
|
|
1951
|
-
]
|
|
1952
|
-
},
|
|
1953
|
-
{
|
|
1954
|
-
name: "interceptors",
|
|
1955
|
-
type: "HttpInterceptor[]",
|
|
1956
|
-
hasQuestionToken: true,
|
|
1957
|
-
docs: [
|
|
1958
|
-
"Array of HTTP interceptors to apply to this client"
|
|
1959
|
-
]
|
|
1960
|
-
}
|
|
1961
|
-
]
|
|
1962
|
-
});
|
|
1963
|
-
this.addMainProviderFunction(sourceFile, basePathTokenName, interceptorsTokenName, baseInterceptorClassName);
|
|
1964
|
-
sourceFile.saveSync();
|
|
1965
|
-
}
|
|
1966
|
-
addMainProviderFunction(sourceFile, basePathTokenName, interceptorsTokenName, baseInterceptorClassName) {
|
|
1967
|
-
const hasDateInterceptor = this.config.options.dateType === "Date";
|
|
1968
|
-
const functionName = `provide${this.capitalizeFirst(this.clientName)}Client`;
|
|
1969
|
-
const configTypeName = `${this.capitalizeFirst(this.clientName)}Config`;
|
|
1970
|
-
const functionBody = `
|
|
1971
|
-
const providers: Provider[] = [
|
|
1972
|
-
// Base path token for this client
|
|
1973
|
-
{
|
|
1974
|
-
provide: ${basePathTokenName},
|
|
1975
|
-
useValue: config.basePath
|
|
1976
|
-
},
|
|
1977
|
-
// Client-specific interceptors token
|
|
1978
|
-
{
|
|
1979
|
-
provide: ${interceptorsTokenName},
|
|
1980
|
-
useValue: config.interceptors || []
|
|
1981
|
-
},
|
|
1982
|
-
// Base interceptor that handles client-specific interceptors
|
|
1983
|
-
{
|
|
1984
|
-
provide: HTTP_INTERCEPTORS,
|
|
1985
|
-
useClass: ${baseInterceptorClassName},
|
|
1986
|
-
multi: true
|
|
1987
|
-
}
|
|
1988
|
-
];
|
|
1989
|
-
|
|
1990
|
-
${hasDateInterceptor ? `// Add date interceptor to client-specific interceptors if enabled
|
|
1991
|
-
if (config.enableDateTransform !== false) {
|
|
1992
|
-
const currentInterceptors = config.interceptors || [];
|
|
1993
|
-
providers.push({
|
|
1994
|
-
provide: ${interceptorsTokenName},
|
|
1995
|
-
useValue: [new DateInterceptor(), ...currentInterceptors]
|
|
1996
|
-
});
|
|
1997
|
-
}` : `// Date transformation not available (dateType: 'string' was used in generation)`}
|
|
1998
|
-
|
|
1999
|
-
return makeEnvironmentProviders(providers);`;
|
|
2000
|
-
sourceFile.addFunction({
|
|
2001
|
-
name: functionName,
|
|
2002
|
-
isExported: true,
|
|
2003
|
-
docs: [
|
|
2004
|
-
`Provides configuration for ${this.clientName} client`,
|
|
2005
|
-
"",
|
|
2006
|
-
"@example",
|
|
2007
|
-
"```typescript",
|
|
2008
|
-
"// In your app.config.ts",
|
|
2009
|
-
`import { ${functionName} } from './api/providers';`,
|
|
2010
|
-
"",
|
|
2011
|
-
"export const appConfig: ApplicationConfig = {",
|
|
2012
|
-
" providers: [",
|
|
2013
|
-
` ${functionName}({`,
|
|
2014
|
-
" basePath: 'https://api.example.com',",
|
|
2015
|
-
" interceptors: [new LoggingInterceptor(), new AuthInterceptor()]",
|
|
2016
|
-
" }),",
|
|
2017
|
-
" // other providers...",
|
|
2018
|
-
" ]",
|
|
2019
|
-
"};",
|
|
2020
|
-
"```"
|
|
2021
|
-
],
|
|
2022
|
-
parameters: [
|
|
2023
|
-
{
|
|
2024
|
-
name: "config",
|
|
2025
|
-
type: configTypeName
|
|
2026
|
-
}
|
|
2027
|
-
],
|
|
2028
|
-
returnType: "EnvironmentProviders",
|
|
2029
|
-
statements: functionBody
|
|
2030
|
-
});
|
|
2031
|
-
if (this.clientName === "default") {
|
|
2032
|
-
sourceFile.addFunction({
|
|
2033
|
-
name: "provideNgOpenapi",
|
|
2034
|
-
isExported: true,
|
|
2035
|
-
docs: [
|
|
2036
|
-
"@deprecated Use provideDefaultClient instead for better clarity",
|
|
2037
|
-
"Provides configuration for the default client"
|
|
2038
|
-
],
|
|
2039
|
-
parameters: [
|
|
2040
|
-
{
|
|
2041
|
-
name: "config",
|
|
2042
|
-
type: configTypeName
|
|
2043
|
-
}
|
|
2044
|
-
],
|
|
2045
|
-
returnType: "EnvironmentProviders",
|
|
2046
|
-
statements: `return ${functionName}(config);`
|
|
2047
|
-
});
|
|
2048
|
-
}
|
|
2049
|
-
}
|
|
2050
|
-
getBasePathTokenName() {
|
|
2051
|
-
const clientSuffix = this.clientName.toUpperCase().replace(/[^A-Z0-9]/g, "_");
|
|
2052
|
-
return `BASE_PATH_${clientSuffix}`;
|
|
2053
|
-
}
|
|
2054
|
-
getInterceptorsTokenName() {
|
|
2055
|
-
const clientSuffix = this.clientName.toUpperCase().replace(/[^A-Z0-9]/g, "_");
|
|
2056
|
-
return `HTTP_INTERCEPTORS_${clientSuffix}`;
|
|
2057
|
-
}
|
|
2058
|
-
capitalizeFirst(str) {
|
|
2059
|
-
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
2060
|
-
}
|
|
2061
|
-
};
|
|
2062
|
-
|
|
2063
2197
|
// src/lib/core/generator.ts
|
|
2064
2198
|
var fs3 = __toESM(require("fs"));
|
|
2065
2199
|
async function generateFromConfig(config) {
|
|
@@ -2074,11 +2208,11 @@ async function generateFromConfig(config) {
|
|
|
2074
2208
|
});
|
|
2075
2209
|
}
|
|
2076
2210
|
try {
|
|
2077
|
-
const project = new
|
|
2211
|
+
const project = new import_ts_morph5.Project({
|
|
2078
2212
|
compilerOptions: {
|
|
2079
2213
|
declaration: true,
|
|
2080
|
-
target:
|
|
2081
|
-
module:
|
|
2214
|
+
target: import_ts_morph5.ScriptTarget.ES2022,
|
|
2215
|
+
module: import_ts_morph5.ModuleKind.Preserve,
|
|
2082
2216
|
strict: true,
|
|
2083
2217
|
...config.compilerOptions
|
|
2084
2218
|
}
|
|
@@ -2092,11 +2226,9 @@ async function generateFromConfig(config) {
|
|
|
2092
2226
|
if (config.options.dateType === "Date") {
|
|
2093
2227
|
const dateTransformer = new DateTransformerGenerator(project);
|
|
2094
2228
|
dateTransformer.generate(outputPath);
|
|
2095
|
-
console.log(`\u2705 Date transformer generated`);
|
|
2096
2229
|
}
|
|
2097
2230
|
const fileDownloadHelper = new FileDownloadGenerator(project);
|
|
2098
2231
|
fileDownloadHelper.generate(outputPath);
|
|
2099
|
-
console.log(`\u2705 File download helper generated`);
|
|
2100
2232
|
const serviceGenerator = new ServiceGenerator(config.input, project, config);
|
|
2101
2233
|
serviceGenerator.generate(outputPath);
|
|
2102
2234
|
const indexGenerator = new ServiceIndexGenerator(project);
|
|
@@ -2104,11 +2236,16 @@ async function generateFromConfig(config) {
|
|
|
2104
2236
|
console.log(`\u2705 Angular services generated`);
|
|
2105
2237
|
const providerGenerator = new ProviderGenerator(project, config);
|
|
2106
2238
|
providerGenerator.generate(outputPath);
|
|
2107
|
-
|
|
2239
|
+
const baseInterceptorGenerator = new BaseInterceptorGenerator(project, config.clientName);
|
|
2240
|
+
baseInterceptorGenerator.generate(outputPath);
|
|
2108
2241
|
}
|
|
2109
2242
|
const mainIndexGenerator = new MainIndexGenerator(project, config);
|
|
2110
2243
|
mainIndexGenerator.generateMainIndex(outputPath);
|
|
2111
|
-
|
|
2244
|
+
if (config.clientName) {
|
|
2245
|
+
console.log(`\u{1F389} ${config.clientName} Generation completed successfully at: ${outputPath}`);
|
|
2246
|
+
} else {
|
|
2247
|
+
console.log("\u{1F389} Generation completed successfully at:", outputPath);
|
|
2248
|
+
}
|
|
2112
2249
|
} catch (error) {
|
|
2113
2250
|
if (error instanceof Error) {
|
|
2114
2251
|
console.error("\u274C Error during generation:", error.message);
|
|
@@ -2123,7 +2260,7 @@ __name(generateFromConfig, "generateFromConfig");
|
|
|
2123
2260
|
// src/lib/cli.ts
|
|
2124
2261
|
var program = new import_commander.Command();
|
|
2125
2262
|
async function loadConfigFile(configPath) {
|
|
2126
|
-
const resolvedPath =
|
|
2263
|
+
const resolvedPath = path9.resolve(configPath);
|
|
2127
2264
|
if (!fs4.existsSync(resolvedPath)) {
|
|
2128
2265
|
throw new Error(`Configuration file not found: ${resolvedPath}`);
|
|
2129
2266
|
}
|
|
@@ -2149,7 +2286,7 @@ async function generateFromOptions(options) {
|
|
|
2149
2286
|
const config = await loadConfigFile(options.config);
|
|
2150
2287
|
await generateFromConfig(config);
|
|
2151
2288
|
} else if (options.input) {
|
|
2152
|
-
const inputPath =
|
|
2289
|
+
const inputPath = path9.resolve(options.input);
|
|
2153
2290
|
if (!fs4.existsSync(inputPath)) {
|
|
2154
2291
|
console.error(`Error: Input file not found: ${inputPath}`);
|
|
2155
2292
|
process.exit(1);
|