ng-openapi 0.0.24 → 0.0.25-alpha.0
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 +141 -87
- package/index.d.ts +9 -1
- package/index.js +141 -87
- package/package.json +1 -1
package/cli.cjs
CHANGED
|
@@ -402,8 +402,10 @@ var TokenGenerator = class {
|
|
|
402
402
|
__name(this, "TokenGenerator");
|
|
403
403
|
}
|
|
404
404
|
project;
|
|
405
|
-
|
|
405
|
+
clientName;
|
|
406
|
+
constructor(project, clientName = "default") {
|
|
406
407
|
this.project = project;
|
|
408
|
+
this.clientName = clientName;
|
|
407
409
|
}
|
|
408
410
|
generate(outputDir) {
|
|
409
411
|
const tokensDir = path.join(outputDir, "tokens");
|
|
@@ -413,29 +415,73 @@ var TokenGenerator = class {
|
|
|
413
415
|
});
|
|
414
416
|
sourceFile.addImportDeclaration({
|
|
415
417
|
namedImports: [
|
|
416
|
-
"InjectionToken"
|
|
418
|
+
"InjectionToken",
|
|
419
|
+
"HttpInterceptor"
|
|
417
420
|
],
|
|
418
421
|
moduleSpecifier: "@angular/core"
|
|
419
422
|
});
|
|
423
|
+
const basePathTokenName = this.getBasePathTokenName();
|
|
424
|
+
const interceptorsTokenName = this.getInterceptorsTokenName();
|
|
420
425
|
sourceFile.addVariableStatement({
|
|
421
426
|
isExported: true,
|
|
422
427
|
declarationKind: import_ts_morph2.VariableDeclarationKind.Const,
|
|
423
428
|
declarations: [
|
|
424
429
|
{
|
|
425
|
-
name:
|
|
426
|
-
initializer: `new InjectionToken<string>('
|
|
430
|
+
name: basePathTokenName,
|
|
431
|
+
initializer: `new InjectionToken<string>('${basePathTokenName}', {
|
|
427
432
|
providedIn: 'root',
|
|
428
433
|
factory: () => '/api', // Default fallback
|
|
429
434
|
})`
|
|
430
435
|
}
|
|
431
436
|
],
|
|
432
437
|
leadingTrivia: `/**
|
|
433
|
-
* Injection token for the base API path
|
|
438
|
+
* Injection token for the ${this.clientName} client base API path
|
|
434
439
|
*/
|
|
435
440
|
`
|
|
436
441
|
});
|
|
442
|
+
sourceFile.addVariableStatement({
|
|
443
|
+
isExported: true,
|
|
444
|
+
declarationKind: import_ts_morph2.VariableDeclarationKind.Const,
|
|
445
|
+
declarations: [
|
|
446
|
+
{
|
|
447
|
+
name: interceptorsTokenName,
|
|
448
|
+
initializer: `new InjectionToken<HttpInterceptor[]>('${interceptorsTokenName}', {
|
|
449
|
+
providedIn: 'root',
|
|
450
|
+
factory: () => [], // Default empty array
|
|
451
|
+
})`
|
|
452
|
+
}
|
|
453
|
+
],
|
|
454
|
+
leadingTrivia: `/**
|
|
455
|
+
* Injection token for the ${this.clientName} client HTTP interceptors
|
|
456
|
+
*/
|
|
457
|
+
`
|
|
458
|
+
});
|
|
459
|
+
if (this.clientName === "default") {
|
|
460
|
+
sourceFile.addVariableStatement({
|
|
461
|
+
isExported: true,
|
|
462
|
+
declarationKind: import_ts_morph2.VariableDeclarationKind.Const,
|
|
463
|
+
declarations: [
|
|
464
|
+
{
|
|
465
|
+
name: "BASE_PATH",
|
|
466
|
+
initializer: basePathTokenName
|
|
467
|
+
}
|
|
468
|
+
],
|
|
469
|
+
leadingTrivia: `/**
|
|
470
|
+
* @deprecated Use ${basePathTokenName} instead
|
|
471
|
+
*/
|
|
472
|
+
`
|
|
473
|
+
});
|
|
474
|
+
}
|
|
437
475
|
sourceFile.saveSync();
|
|
438
476
|
}
|
|
477
|
+
getBasePathTokenName() {
|
|
478
|
+
const clientSuffix = this.clientName.toUpperCase().replace(/[^A-Z0-9]/g, "_");
|
|
479
|
+
return `BASE_PATH_${clientSuffix}`;
|
|
480
|
+
}
|
|
481
|
+
getInterceptorsTokenName() {
|
|
482
|
+
const clientSuffix = this.clientName.toUpperCase().replace(/[^A-Z0-9]/g, "_");
|
|
483
|
+
return `HTTP_INTERCEPTORS_${clientSuffix}`;
|
|
484
|
+
}
|
|
439
485
|
};
|
|
440
486
|
|
|
441
487
|
// src/lib/generators/utility/file-download.generator.ts
|
|
@@ -1702,6 +1748,7 @@ var ServiceGenerator = class {
|
|
|
1702
1748
|
});
|
|
1703
1749
|
}
|
|
1704
1750
|
addImports(sourceFile, usedTypes) {
|
|
1751
|
+
const basePathTokenName = this.getBasePathTokenName();
|
|
1705
1752
|
sourceFile.addImportDeclarations([
|
|
1706
1753
|
{
|
|
1707
1754
|
namedImports: [
|
|
@@ -1729,7 +1776,7 @@ var ServiceGenerator = class {
|
|
|
1729
1776
|
},
|
|
1730
1777
|
{
|
|
1731
1778
|
namedImports: [
|
|
1732
|
-
|
|
1779
|
+
basePathTokenName
|
|
1733
1780
|
],
|
|
1734
1781
|
moduleSpecifier: "../tokens"
|
|
1735
1782
|
}
|
|
@@ -1743,6 +1790,7 @@ var ServiceGenerator = class {
|
|
|
1743
1790
|
}
|
|
1744
1791
|
addServiceClass(sourceFile, controllerName, operations) {
|
|
1745
1792
|
const className = `${controllerName}Service`;
|
|
1793
|
+
const basePathTokenName = this.getBasePathTokenName();
|
|
1746
1794
|
sourceFile.insertText(0, SERVICE_GENERATOR_HEADER_COMMENT(controllerName));
|
|
1747
1795
|
const serviceClass = sourceFile.addClass({
|
|
1748
1796
|
name: className,
|
|
@@ -1768,7 +1816,7 @@ var ServiceGenerator = class {
|
|
|
1768
1816
|
type: "string",
|
|
1769
1817
|
scope: import_ts_morph3.Scope.Private,
|
|
1770
1818
|
isReadonly: true,
|
|
1771
|
-
initializer:
|
|
1819
|
+
initializer: `inject(${basePathTokenName})`
|
|
1772
1820
|
});
|
|
1773
1821
|
operations.forEach((operation) => {
|
|
1774
1822
|
this.methodGenerator.addServiceMethod(serviceClass, operation);
|
|
@@ -1777,6 +1825,11 @@ var ServiceGenerator = class {
|
|
|
1777
1825
|
throw new Error(`Duplicate method names found in service class ${className}. Please ensure unique method names for each operation.`);
|
|
1778
1826
|
}
|
|
1779
1827
|
}
|
|
1828
|
+
getBasePathTokenName() {
|
|
1829
|
+
const clientName = this.config.clientName || "default";
|
|
1830
|
+
const clientSuffix = clientName.toUpperCase().replace(/[^A-Z0-9]/g, "_");
|
|
1831
|
+
return `BASE_PATH_${clientSuffix}`;
|
|
1832
|
+
}
|
|
1780
1833
|
hasDuplicateMethodNames(arr) {
|
|
1781
1834
|
return new Set(arr.map((method) => method.getName())).size !== arr.length;
|
|
1782
1835
|
}
|
|
@@ -1822,9 +1875,11 @@ var ProviderGenerator = class {
|
|
|
1822
1875
|
}
|
|
1823
1876
|
project;
|
|
1824
1877
|
config;
|
|
1878
|
+
clientName;
|
|
1825
1879
|
constructor(project, config) {
|
|
1826
1880
|
this.project = project;
|
|
1827
1881
|
this.config = config;
|
|
1882
|
+
this.clientName = config.clientName || "default";
|
|
1828
1883
|
}
|
|
1829
1884
|
generate(outputDir) {
|
|
1830
1885
|
const filePath = path7.join(outputDir, "providers.ts");
|
|
@@ -1832,6 +1887,9 @@ var ProviderGenerator = class {
|
|
|
1832
1887
|
overwrite: true
|
|
1833
1888
|
});
|
|
1834
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`;
|
|
1835
1893
|
sourceFile.addImportDeclarations([
|
|
1836
1894
|
{
|
|
1837
1895
|
namedImports: [
|
|
@@ -1843,15 +1901,23 @@ var ProviderGenerator = class {
|
|
|
1843
1901
|
},
|
|
1844
1902
|
{
|
|
1845
1903
|
namedImports: [
|
|
1846
|
-
"HTTP_INTERCEPTORS"
|
|
1904
|
+
"HTTP_INTERCEPTORS",
|
|
1905
|
+
"HttpInterceptor"
|
|
1847
1906
|
],
|
|
1848
1907
|
moduleSpecifier: "@angular/common/http"
|
|
1849
1908
|
},
|
|
1850
1909
|
{
|
|
1851
1910
|
namedImports: [
|
|
1852
|
-
|
|
1911
|
+
basePathTokenName,
|
|
1912
|
+
interceptorsTokenName
|
|
1853
1913
|
],
|
|
1854
1914
|
moduleSpecifier: "./tokens"
|
|
1915
|
+
},
|
|
1916
|
+
{
|
|
1917
|
+
namedImports: [
|
|
1918
|
+
baseInterceptorClassName
|
|
1919
|
+
],
|
|
1920
|
+
moduleSpecifier: "./utils/base-interceptor"
|
|
1855
1921
|
}
|
|
1856
1922
|
]);
|
|
1857
1923
|
if (this.config.options.dateType === "Date") {
|
|
@@ -1863,10 +1929,10 @@ var ProviderGenerator = class {
|
|
|
1863
1929
|
});
|
|
1864
1930
|
}
|
|
1865
1931
|
sourceFile.addInterface({
|
|
1866
|
-
name:
|
|
1932
|
+
name: `${this.capitalizeFirst(this.clientName)}Config`,
|
|
1867
1933
|
isExported: true,
|
|
1868
1934
|
docs: [
|
|
1869
|
-
|
|
1935
|
+
`Configuration options for ${this.clientName} client`
|
|
1870
1936
|
],
|
|
1871
1937
|
properties: [
|
|
1872
1938
|
{
|
|
@@ -1883,49 +1949,70 @@ var ProviderGenerator = class {
|
|
|
1883
1949
|
docs: [
|
|
1884
1950
|
"Enable automatic date transformation (default: true)"
|
|
1885
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
|
+
]
|
|
1886
1960
|
}
|
|
1887
1961
|
]
|
|
1888
1962
|
});
|
|
1889
|
-
this.addMainProviderFunction(sourceFile);
|
|
1890
|
-
this.addAsyncProviderFunction(sourceFile);
|
|
1963
|
+
this.addMainProviderFunction(sourceFile, basePathTokenName, interceptorsTokenName, baseInterceptorClassName);
|
|
1891
1964
|
sourceFile.saveSync();
|
|
1892
1965
|
}
|
|
1893
|
-
addMainProviderFunction(sourceFile) {
|
|
1966
|
+
addMainProviderFunction(sourceFile, basePathTokenName, interceptorsTokenName, baseInterceptorClassName) {
|
|
1894
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`;
|
|
1895
1970
|
const functionBody = `
|
|
1896
1971
|
const providers: Provider[] = [
|
|
1897
|
-
// Base path token
|
|
1972
|
+
// Base path token for this client
|
|
1898
1973
|
{
|
|
1899
|
-
provide:
|
|
1974
|
+
provide: ${basePathTokenName},
|
|
1900
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
|
|
1901
1987
|
}
|
|
1902
1988
|
];
|
|
1903
1989
|
|
|
1904
|
-
${hasDateInterceptor ? `// Add date interceptor if enabled
|
|
1990
|
+
${hasDateInterceptor ? `// Add date interceptor to client-specific interceptors if enabled
|
|
1905
1991
|
if (config.enableDateTransform !== false) {
|
|
1992
|
+
const currentInterceptors = config.interceptors || [];
|
|
1906
1993
|
providers.push({
|
|
1907
|
-
provide:
|
|
1908
|
-
|
|
1909
|
-
multi: true
|
|
1994
|
+
provide: ${interceptorsTokenName},
|
|
1995
|
+
useValue: [new DateInterceptor(), ...currentInterceptors]
|
|
1910
1996
|
});
|
|
1911
1997
|
}` : `// Date transformation not available (dateType: 'string' was used in generation)`}
|
|
1912
1998
|
|
|
1913
1999
|
return makeEnvironmentProviders(providers);`;
|
|
1914
2000
|
sourceFile.addFunction({
|
|
1915
|
-
name:
|
|
2001
|
+
name: functionName,
|
|
1916
2002
|
isExported: true,
|
|
1917
2003
|
docs: [
|
|
1918
|
-
|
|
2004
|
+
`Provides configuration for ${this.clientName} client`,
|
|
1919
2005
|
"",
|
|
1920
2006
|
"@example",
|
|
1921
2007
|
"```typescript",
|
|
1922
2008
|
"// In your app.config.ts",
|
|
1923
|
-
|
|
2009
|
+
`import { ${functionName} } from './api/providers';`,
|
|
1924
2010
|
"",
|
|
1925
2011
|
"export const appConfig: ApplicationConfig = {",
|
|
1926
2012
|
" providers: [",
|
|
1927
|
-
|
|
1928
|
-
" basePath: 'https://api.example.com'",
|
|
2013
|
+
` ${functionName}({`,
|
|
2014
|
+
" basePath: 'https://api.example.com',",
|
|
2015
|
+
" interceptors: [new LoggingInterceptor(), new AuthInterceptor()]",
|
|
1929
2016
|
" }),",
|
|
1930
2017
|
" // other providers...",
|
|
1931
2018
|
" ]",
|
|
@@ -1935,74 +2022,41 @@ return makeEnvironmentProviders(providers);`;
|
|
|
1935
2022
|
parameters: [
|
|
1936
2023
|
{
|
|
1937
2024
|
name: "config",
|
|
1938
|
-
type:
|
|
2025
|
+
type: configTypeName
|
|
1939
2026
|
}
|
|
1940
2027
|
],
|
|
1941
2028
|
returnType: "EnvironmentProviders",
|
|
1942
2029
|
statements: functionBody
|
|
1943
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
|
+
}
|
|
1944
2049
|
}
|
|
1945
|
-
|
|
1946
|
-
const
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
});
|
|
1956
|
-
} else {
|
|
1957
|
-
providers.push({
|
|
1958
|
-
provide: BASE_PATH,
|
|
1959
|
-
useFactory: config.basePath
|
|
1960
|
-
});
|
|
1961
|
-
}
|
|
1962
|
-
|
|
1963
|
-
${hasDateInterceptor ? `// Add date interceptor if enabled (default: true)
|
|
1964
|
-
if (config.enableDateTransform !== false) {
|
|
1965
|
-
providers.push({
|
|
1966
|
-
provide: HTTP_INTERCEPTORS,
|
|
1967
|
-
useClass: DateInterceptor,
|
|
1968
|
-
multi: true
|
|
1969
|
-
});
|
|
1970
|
-
}` : `// Date transformation not available (dateType: 'string' was used in generation)`}
|
|
1971
|
-
|
|
1972
|
-
return makeEnvironmentProviders(providers);`;
|
|
1973
|
-
sourceFile.addFunction({
|
|
1974
|
-
name: "provideNgOpenapiAsync",
|
|
1975
|
-
isExported: true,
|
|
1976
|
-
docs: [
|
|
1977
|
-
"Alternative function for cases where you need to handle async configuration",
|
|
1978
|
-
"",
|
|
1979
|
-
"@example",
|
|
1980
|
-
"```typescript",
|
|
1981
|
-
"// In your app.config.ts",
|
|
1982
|
-
"import { provideNgOpenapiAsync } from './api/providers';",
|
|
1983
|
-
"",
|
|
1984
|
-
"export const appConfig: ApplicationConfig = {",
|
|
1985
|
-
" providers: [",
|
|
1986
|
-
" provideNgOpenapiAsync({",
|
|
1987
|
-
" basePath: () => import('./config').then(c => c.apiConfig.baseUrl)",
|
|
1988
|
-
" }),",
|
|
1989
|
-
" // other providers...",
|
|
1990
|
-
" ]",
|
|
1991
|
-
"};",
|
|
1992
|
-
"```"
|
|
1993
|
-
],
|
|
1994
|
-
parameters: [
|
|
1995
|
-
{
|
|
1996
|
-
name: "config",
|
|
1997
|
-
type: `{
|
|
1998
|
-
basePath: string | (() => Promise<string>);
|
|
1999
|
-
enableDateTransform?: boolean;
|
|
2000
|
-
}`
|
|
2001
|
-
}
|
|
2002
|
-
],
|
|
2003
|
-
returnType: "EnvironmentProviders",
|
|
2004
|
-
statements: functionBody
|
|
2005
|
-
});
|
|
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);
|
|
2006
2060
|
}
|
|
2007
2061
|
};
|
|
2008
2062
|
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ParameterType, XML, ExternalDocs, Info, Path, BodyParameter, QueryParameter, Security, Tag } from 'swagger-schema-official';
|
|
2
2
|
import { ScriptTarget, ModuleKind } from 'ts-morph';
|
|
3
|
+
import { HttpInterceptor } from '@angular/common/http';
|
|
3
4
|
|
|
4
5
|
interface MethodGenerationContext {
|
|
5
6
|
pathParams: Array<{
|
|
@@ -28,6 +29,7 @@ interface TypeSchema {
|
|
|
28
29
|
interface GeneratorConfig {
|
|
29
30
|
input: string;
|
|
30
31
|
output: string;
|
|
32
|
+
clientName?: string;
|
|
31
33
|
options: {
|
|
32
34
|
dateType: "string" | "Date";
|
|
33
35
|
enumStyle: "enum" | "union";
|
|
@@ -46,6 +48,12 @@ interface GeneratorConfig {
|
|
|
46
48
|
strict?: boolean;
|
|
47
49
|
};
|
|
48
50
|
}
|
|
51
|
+
interface NgOpenapiClientConfig {
|
|
52
|
+
clientName: string;
|
|
53
|
+
basePath: string;
|
|
54
|
+
enableDateTransform?: boolean;
|
|
55
|
+
interceptors?: HttpInterceptor[];
|
|
56
|
+
}
|
|
49
57
|
|
|
50
58
|
interface Parameter {
|
|
51
59
|
name: string;
|
|
@@ -167,4 +175,4 @@ declare class SwaggerParser {
|
|
|
167
175
|
*/
|
|
168
176
|
declare function generateFromConfig(config: GeneratorConfig): Promise<void>;
|
|
169
177
|
|
|
170
|
-
export { type EnumValueObject, type GeneratorConfig, type MethodGenerationContext, type Parameter, type PathInfo, type RequestBody, type SwaggerDefinition, SwaggerParser, type SwaggerResponse, type SwaggerSpec, type TypeSchema, generateFromConfig };
|
|
178
|
+
export { type EnumValueObject, type GeneratorConfig, type MethodGenerationContext, type NgOpenapiClientConfig, type Parameter, type PathInfo, type RequestBody, type SwaggerDefinition, SwaggerParser, type SwaggerResponse, type SwaggerSpec, type TypeSchema, generateFromConfig };
|
package/index.js
CHANGED
|
@@ -441,9 +441,11 @@ var TypeGenerator = _TypeGenerator;
|
|
|
441
441
|
var import_ts_morph2 = require("ts-morph");
|
|
442
442
|
var path = __toESM(require("path"));
|
|
443
443
|
var _TokenGenerator = class _TokenGenerator {
|
|
444
|
-
constructor(project) {
|
|
444
|
+
constructor(project, clientName = "default") {
|
|
445
445
|
__publicField(this, "project");
|
|
446
|
+
__publicField(this, "clientName");
|
|
446
447
|
this.project = project;
|
|
448
|
+
this.clientName = clientName;
|
|
447
449
|
}
|
|
448
450
|
generate(outputDir) {
|
|
449
451
|
const tokensDir = path.join(outputDir, "tokens");
|
|
@@ -453,29 +455,73 @@ var _TokenGenerator = class _TokenGenerator {
|
|
|
453
455
|
});
|
|
454
456
|
sourceFile.addImportDeclaration({
|
|
455
457
|
namedImports: [
|
|
456
|
-
"InjectionToken"
|
|
458
|
+
"InjectionToken",
|
|
459
|
+
"HttpInterceptor"
|
|
457
460
|
],
|
|
458
461
|
moduleSpecifier: "@angular/core"
|
|
459
462
|
});
|
|
463
|
+
const basePathTokenName = this.getBasePathTokenName();
|
|
464
|
+
const interceptorsTokenName = this.getInterceptorsTokenName();
|
|
460
465
|
sourceFile.addVariableStatement({
|
|
461
466
|
isExported: true,
|
|
462
467
|
declarationKind: import_ts_morph2.VariableDeclarationKind.Const,
|
|
463
468
|
declarations: [
|
|
464
469
|
{
|
|
465
|
-
name:
|
|
466
|
-
initializer: `new InjectionToken<string>('
|
|
470
|
+
name: basePathTokenName,
|
|
471
|
+
initializer: `new InjectionToken<string>('${basePathTokenName}', {
|
|
467
472
|
providedIn: 'root',
|
|
468
473
|
factory: () => '/api', // Default fallback
|
|
469
474
|
})`
|
|
470
475
|
}
|
|
471
476
|
],
|
|
472
477
|
leadingTrivia: `/**
|
|
473
|
-
* Injection token for the base API path
|
|
478
|
+
* Injection token for the ${this.clientName} client base API path
|
|
474
479
|
*/
|
|
475
480
|
`
|
|
476
481
|
});
|
|
482
|
+
sourceFile.addVariableStatement({
|
|
483
|
+
isExported: true,
|
|
484
|
+
declarationKind: import_ts_morph2.VariableDeclarationKind.Const,
|
|
485
|
+
declarations: [
|
|
486
|
+
{
|
|
487
|
+
name: interceptorsTokenName,
|
|
488
|
+
initializer: `new InjectionToken<HttpInterceptor[]>('${interceptorsTokenName}', {
|
|
489
|
+
providedIn: 'root',
|
|
490
|
+
factory: () => [], // Default empty array
|
|
491
|
+
})`
|
|
492
|
+
}
|
|
493
|
+
],
|
|
494
|
+
leadingTrivia: `/**
|
|
495
|
+
* Injection token for the ${this.clientName} client HTTP interceptors
|
|
496
|
+
*/
|
|
497
|
+
`
|
|
498
|
+
});
|
|
499
|
+
if (this.clientName === "default") {
|
|
500
|
+
sourceFile.addVariableStatement({
|
|
501
|
+
isExported: true,
|
|
502
|
+
declarationKind: import_ts_morph2.VariableDeclarationKind.Const,
|
|
503
|
+
declarations: [
|
|
504
|
+
{
|
|
505
|
+
name: "BASE_PATH",
|
|
506
|
+
initializer: basePathTokenName
|
|
507
|
+
}
|
|
508
|
+
],
|
|
509
|
+
leadingTrivia: `/**
|
|
510
|
+
* @deprecated Use ${basePathTokenName} instead
|
|
511
|
+
*/
|
|
512
|
+
`
|
|
513
|
+
});
|
|
514
|
+
}
|
|
477
515
|
sourceFile.saveSync();
|
|
478
516
|
}
|
|
517
|
+
getBasePathTokenName() {
|
|
518
|
+
const clientSuffix = this.clientName.toUpperCase().replace(/[^A-Z0-9]/g, "_");
|
|
519
|
+
return `BASE_PATH_${clientSuffix}`;
|
|
520
|
+
}
|
|
521
|
+
getInterceptorsTokenName() {
|
|
522
|
+
const clientSuffix = this.clientName.toUpperCase().replace(/[^A-Z0-9]/g, "_");
|
|
523
|
+
return `HTTP_INTERCEPTORS_${clientSuffix}`;
|
|
524
|
+
}
|
|
479
525
|
};
|
|
480
526
|
__name(_TokenGenerator, "TokenGenerator");
|
|
481
527
|
var TokenGenerator = _TokenGenerator;
|
|
@@ -1746,6 +1792,7 @@ var _ServiceGenerator = class _ServiceGenerator {
|
|
|
1746
1792
|
});
|
|
1747
1793
|
}
|
|
1748
1794
|
addImports(sourceFile, usedTypes) {
|
|
1795
|
+
const basePathTokenName = this.getBasePathTokenName();
|
|
1749
1796
|
sourceFile.addImportDeclarations([
|
|
1750
1797
|
{
|
|
1751
1798
|
namedImports: [
|
|
@@ -1773,7 +1820,7 @@ var _ServiceGenerator = class _ServiceGenerator {
|
|
|
1773
1820
|
},
|
|
1774
1821
|
{
|
|
1775
1822
|
namedImports: [
|
|
1776
|
-
|
|
1823
|
+
basePathTokenName
|
|
1777
1824
|
],
|
|
1778
1825
|
moduleSpecifier: "../tokens"
|
|
1779
1826
|
}
|
|
@@ -1787,6 +1834,7 @@ var _ServiceGenerator = class _ServiceGenerator {
|
|
|
1787
1834
|
}
|
|
1788
1835
|
addServiceClass(sourceFile, controllerName, operations) {
|
|
1789
1836
|
const className = `${controllerName}Service`;
|
|
1837
|
+
const basePathTokenName = this.getBasePathTokenName();
|
|
1790
1838
|
sourceFile.insertText(0, SERVICE_GENERATOR_HEADER_COMMENT(controllerName));
|
|
1791
1839
|
const serviceClass = sourceFile.addClass({
|
|
1792
1840
|
name: className,
|
|
@@ -1812,7 +1860,7 @@ var _ServiceGenerator = class _ServiceGenerator {
|
|
|
1812
1860
|
type: "string",
|
|
1813
1861
|
scope: import_ts_morph3.Scope.Private,
|
|
1814
1862
|
isReadonly: true,
|
|
1815
|
-
initializer:
|
|
1863
|
+
initializer: `inject(${basePathTokenName})`
|
|
1816
1864
|
});
|
|
1817
1865
|
operations.forEach((operation) => {
|
|
1818
1866
|
this.methodGenerator.addServiceMethod(serviceClass, operation);
|
|
@@ -1821,6 +1869,11 @@ var _ServiceGenerator = class _ServiceGenerator {
|
|
|
1821
1869
|
throw new Error(`Duplicate method names found in service class ${className}. Please ensure unique method names for each operation.`);
|
|
1822
1870
|
}
|
|
1823
1871
|
}
|
|
1872
|
+
getBasePathTokenName() {
|
|
1873
|
+
const clientName = this.config.clientName || "default";
|
|
1874
|
+
const clientSuffix = clientName.toUpperCase().replace(/[^A-Z0-9]/g, "_");
|
|
1875
|
+
return `BASE_PATH_${clientSuffix}`;
|
|
1876
|
+
}
|
|
1824
1877
|
hasDuplicateMethodNames(arr) {
|
|
1825
1878
|
return new Set(arr.map((method) => method.getName())).size !== arr.length;
|
|
1826
1879
|
}
|
|
@@ -1865,8 +1918,10 @@ var _ProviderGenerator = class _ProviderGenerator {
|
|
|
1865
1918
|
constructor(project, config) {
|
|
1866
1919
|
__publicField(this, "project");
|
|
1867
1920
|
__publicField(this, "config");
|
|
1921
|
+
__publicField(this, "clientName");
|
|
1868
1922
|
this.project = project;
|
|
1869
1923
|
this.config = config;
|
|
1924
|
+
this.clientName = config.clientName || "default";
|
|
1870
1925
|
}
|
|
1871
1926
|
generate(outputDir) {
|
|
1872
1927
|
const filePath = path7.join(outputDir, "providers.ts");
|
|
@@ -1874,6 +1929,9 @@ var _ProviderGenerator = class _ProviderGenerator {
|
|
|
1874
1929
|
overwrite: true
|
|
1875
1930
|
});
|
|
1876
1931
|
sourceFile.insertText(0, PROVIDER_GENERATOR_HEADER_COMMENT);
|
|
1932
|
+
const basePathTokenName = this.getBasePathTokenName();
|
|
1933
|
+
const interceptorsTokenName = this.getInterceptorsTokenName();
|
|
1934
|
+
const baseInterceptorClassName = `${this.capitalizeFirst(this.clientName)}BaseInterceptor`;
|
|
1877
1935
|
sourceFile.addImportDeclarations([
|
|
1878
1936
|
{
|
|
1879
1937
|
namedImports: [
|
|
@@ -1885,15 +1943,23 @@ var _ProviderGenerator = class _ProviderGenerator {
|
|
|
1885
1943
|
},
|
|
1886
1944
|
{
|
|
1887
1945
|
namedImports: [
|
|
1888
|
-
"HTTP_INTERCEPTORS"
|
|
1946
|
+
"HTTP_INTERCEPTORS",
|
|
1947
|
+
"HttpInterceptor"
|
|
1889
1948
|
],
|
|
1890
1949
|
moduleSpecifier: "@angular/common/http"
|
|
1891
1950
|
},
|
|
1892
1951
|
{
|
|
1893
1952
|
namedImports: [
|
|
1894
|
-
|
|
1953
|
+
basePathTokenName,
|
|
1954
|
+
interceptorsTokenName
|
|
1895
1955
|
],
|
|
1896
1956
|
moduleSpecifier: "./tokens"
|
|
1957
|
+
},
|
|
1958
|
+
{
|
|
1959
|
+
namedImports: [
|
|
1960
|
+
baseInterceptorClassName
|
|
1961
|
+
],
|
|
1962
|
+
moduleSpecifier: "./utils/base-interceptor"
|
|
1897
1963
|
}
|
|
1898
1964
|
]);
|
|
1899
1965
|
if (this.config.options.dateType === "Date") {
|
|
@@ -1905,10 +1971,10 @@ var _ProviderGenerator = class _ProviderGenerator {
|
|
|
1905
1971
|
});
|
|
1906
1972
|
}
|
|
1907
1973
|
sourceFile.addInterface({
|
|
1908
|
-
name:
|
|
1974
|
+
name: `${this.capitalizeFirst(this.clientName)}Config`,
|
|
1909
1975
|
isExported: true,
|
|
1910
1976
|
docs: [
|
|
1911
|
-
|
|
1977
|
+
`Configuration options for ${this.clientName} client`
|
|
1912
1978
|
],
|
|
1913
1979
|
properties: [
|
|
1914
1980
|
{
|
|
@@ -1925,49 +1991,70 @@ var _ProviderGenerator = class _ProviderGenerator {
|
|
|
1925
1991
|
docs: [
|
|
1926
1992
|
"Enable automatic date transformation (default: true)"
|
|
1927
1993
|
]
|
|
1994
|
+
},
|
|
1995
|
+
{
|
|
1996
|
+
name: "interceptors",
|
|
1997
|
+
type: "HttpInterceptor[]",
|
|
1998
|
+
hasQuestionToken: true,
|
|
1999
|
+
docs: [
|
|
2000
|
+
"Array of HTTP interceptors to apply to this client"
|
|
2001
|
+
]
|
|
1928
2002
|
}
|
|
1929
2003
|
]
|
|
1930
2004
|
});
|
|
1931
|
-
this.addMainProviderFunction(sourceFile);
|
|
1932
|
-
this.addAsyncProviderFunction(sourceFile);
|
|
2005
|
+
this.addMainProviderFunction(sourceFile, basePathTokenName, interceptorsTokenName, baseInterceptorClassName);
|
|
1933
2006
|
sourceFile.saveSync();
|
|
1934
2007
|
}
|
|
1935
|
-
addMainProviderFunction(sourceFile) {
|
|
2008
|
+
addMainProviderFunction(sourceFile, basePathTokenName, interceptorsTokenName, baseInterceptorClassName) {
|
|
1936
2009
|
const hasDateInterceptor = this.config.options.dateType === "Date";
|
|
2010
|
+
const functionName = `provide${this.capitalizeFirst(this.clientName)}Client`;
|
|
2011
|
+
const configTypeName = `${this.capitalizeFirst(this.clientName)}Config`;
|
|
1937
2012
|
const functionBody = `
|
|
1938
2013
|
const providers: Provider[] = [
|
|
1939
|
-
// Base path token
|
|
2014
|
+
// Base path token for this client
|
|
1940
2015
|
{
|
|
1941
|
-
provide:
|
|
2016
|
+
provide: ${basePathTokenName},
|
|
1942
2017
|
useValue: config.basePath
|
|
2018
|
+
},
|
|
2019
|
+
// Client-specific interceptors token
|
|
2020
|
+
{
|
|
2021
|
+
provide: ${interceptorsTokenName},
|
|
2022
|
+
useValue: config.interceptors || []
|
|
2023
|
+
},
|
|
2024
|
+
// Base interceptor that handles client-specific interceptors
|
|
2025
|
+
{
|
|
2026
|
+
provide: HTTP_INTERCEPTORS,
|
|
2027
|
+
useClass: ${baseInterceptorClassName},
|
|
2028
|
+
multi: true
|
|
1943
2029
|
}
|
|
1944
2030
|
];
|
|
1945
2031
|
|
|
1946
|
-
${hasDateInterceptor ? `// Add date interceptor if enabled
|
|
2032
|
+
${hasDateInterceptor ? `// Add date interceptor to client-specific interceptors if enabled
|
|
1947
2033
|
if (config.enableDateTransform !== false) {
|
|
2034
|
+
const currentInterceptors = config.interceptors || [];
|
|
1948
2035
|
providers.push({
|
|
1949
|
-
provide:
|
|
1950
|
-
|
|
1951
|
-
multi: true
|
|
2036
|
+
provide: ${interceptorsTokenName},
|
|
2037
|
+
useValue: [new DateInterceptor(), ...currentInterceptors]
|
|
1952
2038
|
});
|
|
1953
2039
|
}` : `// Date transformation not available (dateType: 'string' was used in generation)`}
|
|
1954
2040
|
|
|
1955
2041
|
return makeEnvironmentProviders(providers);`;
|
|
1956
2042
|
sourceFile.addFunction({
|
|
1957
|
-
name:
|
|
2043
|
+
name: functionName,
|
|
1958
2044
|
isExported: true,
|
|
1959
2045
|
docs: [
|
|
1960
|
-
|
|
2046
|
+
`Provides configuration for ${this.clientName} client`,
|
|
1961
2047
|
"",
|
|
1962
2048
|
"@example",
|
|
1963
2049
|
"```typescript",
|
|
1964
2050
|
"// In your app.config.ts",
|
|
1965
|
-
|
|
2051
|
+
`import { ${functionName} } from './api/providers';`,
|
|
1966
2052
|
"",
|
|
1967
2053
|
"export const appConfig: ApplicationConfig = {",
|
|
1968
2054
|
" providers: [",
|
|
1969
|
-
|
|
1970
|
-
" basePath: 'https://api.example.com'",
|
|
2055
|
+
` ${functionName}({`,
|
|
2056
|
+
" basePath: 'https://api.example.com',",
|
|
2057
|
+
" interceptors: [new LoggingInterceptor(), new AuthInterceptor()]",
|
|
1971
2058
|
" }),",
|
|
1972
2059
|
" // other providers...",
|
|
1973
2060
|
" ]",
|
|
@@ -1977,74 +2064,41 @@ return makeEnvironmentProviders(providers);`;
|
|
|
1977
2064
|
parameters: [
|
|
1978
2065
|
{
|
|
1979
2066
|
name: "config",
|
|
1980
|
-
type:
|
|
2067
|
+
type: configTypeName
|
|
1981
2068
|
}
|
|
1982
2069
|
],
|
|
1983
2070
|
returnType: "EnvironmentProviders",
|
|
1984
2071
|
statements: functionBody
|
|
1985
2072
|
});
|
|
2073
|
+
if (this.clientName === "default") {
|
|
2074
|
+
sourceFile.addFunction({
|
|
2075
|
+
name: "provideNgOpenapi",
|
|
2076
|
+
isExported: true,
|
|
2077
|
+
docs: [
|
|
2078
|
+
"@deprecated Use provideDefaultClient instead for better clarity",
|
|
2079
|
+
"Provides configuration for the default client"
|
|
2080
|
+
],
|
|
2081
|
+
parameters: [
|
|
2082
|
+
{
|
|
2083
|
+
name: "config",
|
|
2084
|
+
type: configTypeName
|
|
2085
|
+
}
|
|
2086
|
+
],
|
|
2087
|
+
returnType: "EnvironmentProviders",
|
|
2088
|
+
statements: `return ${functionName}(config);`
|
|
2089
|
+
});
|
|
2090
|
+
}
|
|
1986
2091
|
}
|
|
1987
|
-
|
|
1988
|
-
const
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
});
|
|
1998
|
-
} else {
|
|
1999
|
-
providers.push({
|
|
2000
|
-
provide: BASE_PATH,
|
|
2001
|
-
useFactory: config.basePath
|
|
2002
|
-
});
|
|
2003
|
-
}
|
|
2004
|
-
|
|
2005
|
-
${hasDateInterceptor ? `// Add date interceptor if enabled (default: true)
|
|
2006
|
-
if (config.enableDateTransform !== false) {
|
|
2007
|
-
providers.push({
|
|
2008
|
-
provide: HTTP_INTERCEPTORS,
|
|
2009
|
-
useClass: DateInterceptor,
|
|
2010
|
-
multi: true
|
|
2011
|
-
});
|
|
2012
|
-
}` : `// Date transformation not available (dateType: 'string' was used in generation)`}
|
|
2013
|
-
|
|
2014
|
-
return makeEnvironmentProviders(providers);`;
|
|
2015
|
-
sourceFile.addFunction({
|
|
2016
|
-
name: "provideNgOpenapiAsync",
|
|
2017
|
-
isExported: true,
|
|
2018
|
-
docs: [
|
|
2019
|
-
"Alternative function for cases where you need to handle async configuration",
|
|
2020
|
-
"",
|
|
2021
|
-
"@example",
|
|
2022
|
-
"```typescript",
|
|
2023
|
-
"// In your app.config.ts",
|
|
2024
|
-
"import { provideNgOpenapiAsync } from './api/providers';",
|
|
2025
|
-
"",
|
|
2026
|
-
"export const appConfig: ApplicationConfig = {",
|
|
2027
|
-
" providers: [",
|
|
2028
|
-
" provideNgOpenapiAsync({",
|
|
2029
|
-
" basePath: () => import('./config').then(c => c.apiConfig.baseUrl)",
|
|
2030
|
-
" }),",
|
|
2031
|
-
" // other providers...",
|
|
2032
|
-
" ]",
|
|
2033
|
-
"};",
|
|
2034
|
-
"```"
|
|
2035
|
-
],
|
|
2036
|
-
parameters: [
|
|
2037
|
-
{
|
|
2038
|
-
name: "config",
|
|
2039
|
-
type: `{
|
|
2040
|
-
basePath: string | (() => Promise<string>);
|
|
2041
|
-
enableDateTransform?: boolean;
|
|
2042
|
-
}`
|
|
2043
|
-
}
|
|
2044
|
-
],
|
|
2045
|
-
returnType: "EnvironmentProviders",
|
|
2046
|
-
statements: functionBody
|
|
2047
|
-
});
|
|
2092
|
+
getBasePathTokenName() {
|
|
2093
|
+
const clientSuffix = this.clientName.toUpperCase().replace(/[^A-Z0-9]/g, "_");
|
|
2094
|
+
return `BASE_PATH_${clientSuffix}`;
|
|
2095
|
+
}
|
|
2096
|
+
getInterceptorsTokenName() {
|
|
2097
|
+
const clientSuffix = this.clientName.toUpperCase().replace(/[^A-Z0-9]/g, "_");
|
|
2098
|
+
return `HTTP_INTERCEPTORS_${clientSuffix}`;
|
|
2099
|
+
}
|
|
2100
|
+
capitalizeFirst(str) {
|
|
2101
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
2048
2102
|
}
|
|
2049
2103
|
};
|
|
2050
2104
|
__name(_ProviderGenerator, "ProviderGenerator");
|