ng-openapi 0.0.24-rc.0 → 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 +130 -235
- package/index.d.ts +8 -1
- package/index.js +120 -122
- package/package.json +1 -1
package/cli.cjs
CHANGED
|
@@ -7,10 +7,6 @@ var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
|
7
7
|
var __getProtoOf = Object.getPrototypeOf;
|
|
8
8
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
9
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
10
|
-
var __export = (target, all) => {
|
|
11
|
-
for (var name in all)
|
|
12
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
13
|
-
};
|
|
14
10
|
var __copyProps = (to, from, except, desc) => {
|
|
15
11
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
16
12
|
for (let key of __getOwnPropNames(from))
|
|
@@ -27,14 +23,8 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
27
23
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
28
24
|
mod
|
|
29
25
|
));
|
|
30
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
31
26
|
|
|
32
27
|
// src/lib/cli.ts
|
|
33
|
-
var cli_exports = {};
|
|
34
|
-
__export(cli_exports, {
|
|
35
|
-
generateFromOptions: () => generateFromOptions
|
|
36
|
-
});
|
|
37
|
-
module.exports = __toCommonJS(cli_exports);
|
|
38
28
|
var import_commander = require("commander");
|
|
39
29
|
var path8 = __toESM(require("path"));
|
|
40
30
|
var fs4 = __toESM(require("fs"));
|
|
@@ -412,10 +402,10 @@ var TokenGenerator = class {
|
|
|
412
402
|
__name(this, "TokenGenerator");
|
|
413
403
|
}
|
|
414
404
|
project;
|
|
415
|
-
|
|
416
|
-
constructor(project,
|
|
405
|
+
clientName;
|
|
406
|
+
constructor(project, clientName = "default") {
|
|
417
407
|
this.project = project;
|
|
418
|
-
this.
|
|
408
|
+
this.clientName = clientName;
|
|
419
409
|
}
|
|
420
410
|
generate(outputDir) {
|
|
421
411
|
const tokensDir = path.join(outputDir, "tokens");
|
|
@@ -425,26 +415,27 @@ var TokenGenerator = class {
|
|
|
425
415
|
});
|
|
426
416
|
sourceFile.addImportDeclaration({
|
|
427
417
|
namedImports: [
|
|
428
|
-
"InjectionToken"
|
|
418
|
+
"InjectionToken",
|
|
419
|
+
"HttpInterceptor"
|
|
429
420
|
],
|
|
430
421
|
moduleSpecifier: "@angular/core"
|
|
431
422
|
});
|
|
432
|
-
const
|
|
433
|
-
const
|
|
423
|
+
const basePathTokenName = this.getBasePathTokenName();
|
|
424
|
+
const interceptorsTokenName = this.getInterceptorsTokenName();
|
|
434
425
|
sourceFile.addVariableStatement({
|
|
435
426
|
isExported: true,
|
|
436
427
|
declarationKind: import_ts_morph2.VariableDeclarationKind.Const,
|
|
437
428
|
declarations: [
|
|
438
429
|
{
|
|
439
|
-
name:
|
|
440
|
-
initializer: `new InjectionToken<string>('${
|
|
430
|
+
name: basePathTokenName,
|
|
431
|
+
initializer: `new InjectionToken<string>('${basePathTokenName}', {
|
|
441
432
|
providedIn: 'root',
|
|
442
|
-
factory: () => '/api',
|
|
433
|
+
factory: () => '/api', // Default fallback
|
|
443
434
|
})`
|
|
444
435
|
}
|
|
445
436
|
],
|
|
446
437
|
leadingTrivia: `/**
|
|
447
|
-
*
|
|
438
|
+
* Injection token for the ${this.clientName} client base API path
|
|
448
439
|
*/
|
|
449
440
|
`
|
|
450
441
|
});
|
|
@@ -453,33 +444,44 @@ var TokenGenerator = class {
|
|
|
453
444
|
declarationKind: import_ts_morph2.VariableDeclarationKind.Const,
|
|
454
445
|
declarations: [
|
|
455
446
|
{
|
|
456
|
-
name:
|
|
457
|
-
initializer: `new InjectionToken<
|
|
447
|
+
name: interceptorsTokenName,
|
|
448
|
+
initializer: `new InjectionToken<HttpInterceptor[]>('${interceptorsTokenName}', {
|
|
449
|
+
providedIn: 'root',
|
|
450
|
+
factory: () => [], // Default empty array
|
|
451
|
+
})`
|
|
458
452
|
}
|
|
459
453
|
],
|
|
460
454
|
leadingTrivia: `/**
|
|
461
|
-
*
|
|
455
|
+
* Injection token for the ${this.clientName} client HTTP interceptors
|
|
462
456
|
*/
|
|
463
457
|
`
|
|
464
458
|
});
|
|
465
|
-
if (
|
|
459
|
+
if (this.clientName === "default") {
|
|
466
460
|
sourceFile.addVariableStatement({
|
|
467
461
|
isExported: true,
|
|
468
462
|
declarationKind: import_ts_morph2.VariableDeclarationKind.Const,
|
|
469
463
|
declarations: [
|
|
470
464
|
{
|
|
471
465
|
name: "BASE_PATH",
|
|
472
|
-
initializer:
|
|
466
|
+
initializer: basePathTokenName
|
|
473
467
|
}
|
|
474
468
|
],
|
|
475
469
|
leadingTrivia: `/**
|
|
476
|
-
* @deprecated Use ${
|
|
470
|
+
* @deprecated Use ${basePathTokenName} instead
|
|
477
471
|
*/
|
|
478
472
|
`
|
|
479
473
|
});
|
|
480
474
|
}
|
|
481
475
|
sourceFile.saveSync();
|
|
482
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
|
+
}
|
|
483
485
|
};
|
|
484
486
|
|
|
485
487
|
// src/lib/generators/utility/file-download.generator.ts
|
|
@@ -1746,8 +1748,7 @@ var ServiceGenerator = class {
|
|
|
1746
1748
|
});
|
|
1747
1749
|
}
|
|
1748
1750
|
addImports(sourceFile, usedTypes) {
|
|
1749
|
-
const
|
|
1750
|
-
const upperCaseClientName = clientName.toUpperCase().replace(/[^A-Z0-9]/g, "_");
|
|
1751
|
+
const basePathTokenName = this.getBasePathTokenName();
|
|
1751
1752
|
sourceFile.addImportDeclarations([
|
|
1752
1753
|
{
|
|
1753
1754
|
namedImports: [
|
|
@@ -1775,15 +1776,11 @@ var ServiceGenerator = class {
|
|
|
1775
1776
|
},
|
|
1776
1777
|
{
|
|
1777
1778
|
namedImports: [
|
|
1778
|
-
|
|
1779
|
-
`${upperCaseClientName}_HTTP_CLIENT`
|
|
1779
|
+
basePathTokenName
|
|
1780
1780
|
],
|
|
1781
1781
|
moduleSpecifier: "../tokens"
|
|
1782
1782
|
}
|
|
1783
1783
|
]);
|
|
1784
|
-
if (!this.config.clientName) {
|
|
1785
|
-
sourceFile.getImportDeclaration("../tokens")?.addNamedImport("BASE_PATH");
|
|
1786
|
-
}
|
|
1787
1784
|
if (usedTypes.size > 0) {
|
|
1788
1785
|
sourceFile.addImportDeclaration({
|
|
1789
1786
|
namedImports: Array.from(usedTypes).sort(),
|
|
@@ -1793,8 +1790,7 @@ var ServiceGenerator = class {
|
|
|
1793
1790
|
}
|
|
1794
1791
|
addServiceClass(sourceFile, controllerName, operations) {
|
|
1795
1792
|
const className = `${controllerName}Service`;
|
|
1796
|
-
const
|
|
1797
|
-
const upperCaseClientName = clientName.toUpperCase().replace(/[^A-Z0-9]/g, "_");
|
|
1793
|
+
const basePathTokenName = this.getBasePathTokenName();
|
|
1798
1794
|
sourceFile.insertText(0, SERVICE_GENERATOR_HEADER_COMMENT(controllerName));
|
|
1799
1795
|
const serviceClass = sourceFile.addClass({
|
|
1800
1796
|
name: className,
|
|
@@ -1813,14 +1809,14 @@ var ServiceGenerator = class {
|
|
|
1813
1809
|
type: "HttpClient",
|
|
1814
1810
|
scope: import_ts_morph3.Scope.Private,
|
|
1815
1811
|
isReadonly: true,
|
|
1816
|
-
initializer:
|
|
1812
|
+
initializer: "inject(HttpClient)"
|
|
1817
1813
|
});
|
|
1818
1814
|
serviceClass.addProperty({
|
|
1819
1815
|
name: "basePath",
|
|
1820
1816
|
type: "string",
|
|
1821
1817
|
scope: import_ts_morph3.Scope.Private,
|
|
1822
1818
|
isReadonly: true,
|
|
1823
|
-
initializer: `inject(${
|
|
1819
|
+
initializer: `inject(${basePathTokenName})`
|
|
1824
1820
|
});
|
|
1825
1821
|
operations.forEach((operation) => {
|
|
1826
1822
|
this.methodGenerator.addServiceMethod(serviceClass, operation);
|
|
@@ -1829,6 +1825,11 @@ var ServiceGenerator = class {
|
|
|
1829
1825
|
throw new Error(`Duplicate method names found in service class ${className}. Please ensure unique method names for each operation.`);
|
|
1830
1826
|
}
|
|
1831
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
|
+
}
|
|
1832
1833
|
hasDuplicateMethodNames(arr) {
|
|
1833
1834
|
return new Set(arr.map((method) => method.getName())).size !== arr.length;
|
|
1834
1835
|
}
|
|
@@ -1874,9 +1875,11 @@ var ProviderGenerator = class {
|
|
|
1874
1875
|
}
|
|
1875
1876
|
project;
|
|
1876
1877
|
config;
|
|
1878
|
+
clientName;
|
|
1877
1879
|
constructor(project, config) {
|
|
1878
1880
|
this.project = project;
|
|
1879
1881
|
this.config = config;
|
|
1882
|
+
this.clientName = config.clientName || "default";
|
|
1880
1883
|
}
|
|
1881
1884
|
generate(outputDir) {
|
|
1882
1885
|
const filePath = path7.join(outputDir, "providers.ts");
|
|
@@ -1884,37 +1887,37 @@ var ProviderGenerator = class {
|
|
|
1884
1887
|
overwrite: true
|
|
1885
1888
|
});
|
|
1886
1889
|
sourceFile.insertText(0, PROVIDER_GENERATOR_HEADER_COMMENT);
|
|
1887
|
-
const
|
|
1888
|
-
const
|
|
1889
|
-
const
|
|
1890
|
+
const basePathTokenName = this.getBasePathTokenName();
|
|
1891
|
+
const interceptorsTokenName = this.getInterceptorsTokenName();
|
|
1892
|
+
const baseInterceptorClassName = `${this.capitalizeFirst(this.clientName)}BaseInterceptor`;
|
|
1890
1893
|
sourceFile.addImportDeclarations([
|
|
1891
1894
|
{
|
|
1892
1895
|
namedImports: [
|
|
1893
1896
|
"EnvironmentProviders",
|
|
1894
1897
|
"Provider",
|
|
1895
|
-
"makeEnvironmentProviders"
|
|
1896
|
-
"Type",
|
|
1897
|
-
"Injector",
|
|
1898
|
-
"InjectionToken"
|
|
1898
|
+
"makeEnvironmentProviders"
|
|
1899
1899
|
],
|
|
1900
1900
|
moduleSpecifier: "@angular/core"
|
|
1901
1901
|
},
|
|
1902
1902
|
{
|
|
1903
1903
|
namedImports: [
|
|
1904
|
-
"
|
|
1905
|
-
"HttpInterceptor"
|
|
1906
|
-
"HttpHandler",
|
|
1907
|
-
"HttpBackend",
|
|
1908
|
-
"HTTP_INTERCEPTORS"
|
|
1904
|
+
"HTTP_INTERCEPTORS",
|
|
1905
|
+
"HttpInterceptor"
|
|
1909
1906
|
],
|
|
1910
1907
|
moduleSpecifier: "@angular/common/http"
|
|
1911
1908
|
},
|
|
1912
1909
|
{
|
|
1913
1910
|
namedImports: [
|
|
1914
|
-
|
|
1915
|
-
|
|
1911
|
+
basePathTokenName,
|
|
1912
|
+
interceptorsTokenName
|
|
1916
1913
|
],
|
|
1917
1914
|
moduleSpecifier: "./tokens"
|
|
1915
|
+
},
|
|
1916
|
+
{
|
|
1917
|
+
namedImports: [
|
|
1918
|
+
baseInterceptorClassName
|
|
1919
|
+
],
|
|
1920
|
+
moduleSpecifier: "./utils/base-interceptor"
|
|
1918
1921
|
}
|
|
1919
1922
|
]);
|
|
1920
1923
|
if (this.config.options.dateType === "Date") {
|
|
@@ -1926,10 +1929,10 @@ var ProviderGenerator = class {
|
|
|
1926
1929
|
});
|
|
1927
1930
|
}
|
|
1928
1931
|
sourceFile.addInterface({
|
|
1929
|
-
name: `${
|
|
1932
|
+
name: `${this.capitalizeFirst(this.clientName)}Config`,
|
|
1930
1933
|
isExported: true,
|
|
1931
1934
|
docs: [
|
|
1932
|
-
`Configuration options for ${clientName}
|
|
1935
|
+
`Configuration options for ${this.clientName} client`
|
|
1933
1936
|
],
|
|
1934
1937
|
properties: [
|
|
1935
1938
|
{
|
|
@@ -1940,116 +1943,76 @@ var ProviderGenerator = class {
|
|
|
1940
1943
|
]
|
|
1941
1944
|
},
|
|
1942
1945
|
{
|
|
1943
|
-
name: "
|
|
1944
|
-
type: "
|
|
1946
|
+
name: "enableDateTransform",
|
|
1947
|
+
type: "boolean",
|
|
1945
1948
|
hasQuestionToken: true,
|
|
1946
1949
|
docs: [
|
|
1947
|
-
"
|
|
1950
|
+
"Enable automatic date transformation (default: true)"
|
|
1948
1951
|
]
|
|
1949
1952
|
},
|
|
1950
1953
|
{
|
|
1951
|
-
name: "
|
|
1952
|
-
type: "
|
|
1954
|
+
name: "interceptors",
|
|
1955
|
+
type: "HttpInterceptor[]",
|
|
1953
1956
|
hasQuestionToken: true,
|
|
1954
1957
|
docs: [
|
|
1955
|
-
"
|
|
1958
|
+
"Array of HTTP interceptors to apply to this client"
|
|
1956
1959
|
]
|
|
1957
1960
|
}
|
|
1958
1961
|
]
|
|
1959
1962
|
});
|
|
1960
|
-
|
|
1961
|
-
isExported: true,
|
|
1962
|
-
declarationKind: "const",
|
|
1963
|
-
declarations: [
|
|
1964
|
-
{
|
|
1965
|
-
name: `${upperCaseClientName}_INTERCEPTORS`,
|
|
1966
|
-
initializer: `new InjectionToken<HttpInterceptor[]>('${upperCaseClientName}_INTERCEPTORS')`
|
|
1967
|
-
}
|
|
1968
|
-
],
|
|
1969
|
-
leadingTrivia: `/**
|
|
1970
|
-
* Interceptor token for ${clientName} client
|
|
1971
|
-
*/
|
|
1972
|
-
`
|
|
1973
|
-
});
|
|
1974
|
-
this.addSimpleProviderFunction(sourceFile, pascalClientName, upperCaseClientName);
|
|
1963
|
+
this.addMainProviderFunction(sourceFile, basePathTokenName, interceptorsTokenName, baseInterceptorClassName);
|
|
1975
1964
|
sourceFile.saveSync();
|
|
1976
1965
|
}
|
|
1977
|
-
|
|
1966
|
+
addMainProviderFunction(sourceFile, basePathTokenName, interceptorsTokenName, baseInterceptorClassName) {
|
|
1978
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`;
|
|
1979
1970
|
const functionBody = `
|
|
1980
1971
|
const providers: Provider[] = [
|
|
1981
|
-
// Base path token
|
|
1972
|
+
// Base path token for this client
|
|
1982
1973
|
{
|
|
1983
|
-
provide: ${
|
|
1974
|
+
provide: ${basePathTokenName},
|
|
1984
1975
|
useValue: config.basePath
|
|
1985
1976
|
},
|
|
1986
|
-
|
|
1987
|
-
// Collect interceptors for this client
|
|
1977
|
+
// Client-specific interceptors token
|
|
1988
1978
|
{
|
|
1989
|
-
provide: ${
|
|
1990
|
-
|
|
1991
|
-
const interceptorInstances: HttpInterceptor[] = [];
|
|
1992
|
-
|
|
1993
|
-
// Add custom interceptors
|
|
1994
|
-
if (config.interceptors?.length) {
|
|
1995
|
-
config.interceptors.forEach(interceptorClass => {
|
|
1996
|
-
interceptorInstances.push(injector.get(interceptorClass));
|
|
1997
|
-
});
|
|
1998
|
-
}
|
|
1999
|
-
|
|
2000
|
-
${hasDateInterceptor ? `
|
|
2001
|
-
// Add date interceptor if enabled (default: true)
|
|
2002
|
-
if (config.enableDateTransform !== false) {
|
|
2003
|
-
interceptorInstances.push(injector.get(DateInterceptor));
|
|
2004
|
-
}` : ""}
|
|
2005
|
-
|
|
2006
|
-
return interceptorInstances;
|
|
2007
|
-
},
|
|
2008
|
-
deps: [Injector]
|
|
1979
|
+
provide: ${interceptorsTokenName},
|
|
1980
|
+
useValue: config.interceptors || []
|
|
2009
1981
|
},
|
|
2010
|
-
|
|
2011
|
-
// Create HTTP client with interceptors
|
|
1982
|
+
// Base interceptor that handles client-specific interceptors
|
|
2012
1983
|
{
|
|
2013
|
-
provide:
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
return new HttpClient(backend);
|
|
2017
|
-
}
|
|
2018
|
-
|
|
2019
|
-
// Create handler chain
|
|
2020
|
-
let handler = backend;
|
|
2021
|
-
for (let i = interceptors.length - 1; i >= 0; i--) {
|
|
2022
|
-
const interceptor = interceptors[i];
|
|
2023
|
-
const currentHandler = handler;
|
|
2024
|
-
handler = {
|
|
2025
|
-
handle: req => interceptor.intercept(req, currentHandler)
|
|
2026
|
-
};
|
|
2027
|
-
}
|
|
2028
|
-
|
|
2029
|
-
return new HttpClient(handler);
|
|
2030
|
-
},
|
|
2031
|
-
deps: [HttpBackend, ${upperCaseClientName}_INTERCEPTORS]
|
|
1984
|
+
provide: HTTP_INTERCEPTORS,
|
|
1985
|
+
useClass: ${baseInterceptorClassName},
|
|
1986
|
+
multi: true
|
|
2032
1987
|
}
|
|
2033
1988
|
];
|
|
2034
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
|
+
|
|
2035
1999
|
return makeEnvironmentProviders(providers);`;
|
|
2036
2000
|
sourceFile.addFunction({
|
|
2037
|
-
name:
|
|
2001
|
+
name: functionName,
|
|
2038
2002
|
isExported: true,
|
|
2039
2003
|
docs: [
|
|
2040
|
-
`Provides configuration for ${
|
|
2004
|
+
`Provides configuration for ${this.clientName} client`,
|
|
2041
2005
|
"",
|
|
2042
2006
|
"@example",
|
|
2043
2007
|
"```typescript",
|
|
2044
|
-
|
|
2045
|
-
`import {
|
|
2046
|
-
`import { AuthInterceptor } from './interceptors/auth.interceptor';`,
|
|
2008
|
+
"// In your app.config.ts",
|
|
2009
|
+
`import { ${functionName} } from './api/providers';`,
|
|
2047
2010
|
"",
|
|
2048
2011
|
"export const appConfig: ApplicationConfig = {",
|
|
2049
2012
|
" providers: [",
|
|
2050
|
-
`
|
|
2013
|
+
` ${functionName}({`,
|
|
2051
2014
|
" basePath: 'https://api.example.com',",
|
|
2052
|
-
" interceptors: [AuthInterceptor]",
|
|
2015
|
+
" interceptors: [new LoggingInterceptor(), new AuthInterceptor()]",
|
|
2053
2016
|
" }),",
|
|
2054
2017
|
" // other providers...",
|
|
2055
2018
|
" ]",
|
|
@@ -2059,15 +2022,41 @@ return makeEnvironmentProviders(providers);`;
|
|
|
2059
2022
|
parameters: [
|
|
2060
2023
|
{
|
|
2061
2024
|
name: "config",
|
|
2062
|
-
type:
|
|
2025
|
+
type: configTypeName
|
|
2063
2026
|
}
|
|
2064
2027
|
],
|
|
2065
2028
|
returnType: "EnvironmentProviders",
|
|
2066
2029
|
statements: functionBody
|
|
2067
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
|
+
}
|
|
2068
2049
|
}
|
|
2069
|
-
|
|
2070
|
-
|
|
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);
|
|
2071
2060
|
}
|
|
2072
2061
|
};
|
|
2073
2062
|
|
|
@@ -2098,7 +2087,7 @@ async function generateFromConfig(config) {
|
|
|
2098
2087
|
typeGenerator.generate();
|
|
2099
2088
|
console.log(`\u2705 TypeScript interfaces generated`);
|
|
2100
2089
|
if (generateServices) {
|
|
2101
|
-
const tokenGenerator = new TokenGenerator(project
|
|
2090
|
+
const tokenGenerator = new TokenGenerator(project);
|
|
2102
2091
|
tokenGenerator.generate(outputPath);
|
|
2103
2092
|
if (config.options.dateType === "Date") {
|
|
2104
2093
|
const dateTransformer = new DateTransformerGenerator(project);
|
|
@@ -2131,94 +2120,6 @@ async function generateFromConfig(config) {
|
|
|
2131
2120
|
}
|
|
2132
2121
|
__name(generateFromConfig, "generateFromConfig");
|
|
2133
2122
|
|
|
2134
|
-
// package.json
|
|
2135
|
-
var package_default = {
|
|
2136
|
-
name: "ng-openapi",
|
|
2137
|
-
version: "0.0.23",
|
|
2138
|
-
description: "Generate Angular services and TypeScript types from OpenAPI/Swagger specifications",
|
|
2139
|
-
keywords: [
|
|
2140
|
-
"angular",
|
|
2141
|
-
"openapi",
|
|
2142
|
-
"swagger",
|
|
2143
|
-
"codegen",
|
|
2144
|
-
"typescript",
|
|
2145
|
-
"generator",
|
|
2146
|
-
"code-generator",
|
|
2147
|
-
"api",
|
|
2148
|
-
"rest",
|
|
2149
|
-
"http",
|
|
2150
|
-
"cli"
|
|
2151
|
-
],
|
|
2152
|
-
author: {
|
|
2153
|
-
name: "Tareq Jami",
|
|
2154
|
-
email: "info@jami-it.de",
|
|
2155
|
-
url: "http://tareqjami.de"
|
|
2156
|
-
},
|
|
2157
|
-
license: "MIT",
|
|
2158
|
-
homepage: "https://ng-openapi.dev/",
|
|
2159
|
-
bugs: {
|
|
2160
|
-
url: "https://github.com/ng-openapi/ng-openapi/issues"
|
|
2161
|
-
},
|
|
2162
|
-
repository: {
|
|
2163
|
-
type: "git",
|
|
2164
|
-
url: "git+https://github.com/ng-openapi/ng-openapi.git",
|
|
2165
|
-
directory: "packages/ng-openapi"
|
|
2166
|
-
},
|
|
2167
|
-
funding: {
|
|
2168
|
-
type: "github",
|
|
2169
|
-
url: "https://github.com/sponsors/ng-openapi"
|
|
2170
|
-
},
|
|
2171
|
-
main: "./index.cjs",
|
|
2172
|
-
module: "./index.js",
|
|
2173
|
-
types: "./index.d.ts",
|
|
2174
|
-
bin: {
|
|
2175
|
-
"ng-openapi": "./cli.cjs"
|
|
2176
|
-
},
|
|
2177
|
-
files: [
|
|
2178
|
-
"index.js",
|
|
2179
|
-
"index.cjs",
|
|
2180
|
-
"index.d.ts",
|
|
2181
|
-
"cli.cjs",
|
|
2182
|
-
"lib/**/*.js",
|
|
2183
|
-
"lib/**/*.cjs",
|
|
2184
|
-
"lib/**/*.d.ts",
|
|
2185
|
-
"README.md",
|
|
2186
|
-
"LICENSE",
|
|
2187
|
-
"CHANGELOG.md"
|
|
2188
|
-
],
|
|
2189
|
-
scripts: {
|
|
2190
|
-
prepublishOnly: "echo 'Build the package using: npm run build:ng-openapi from workspace root'",
|
|
2191
|
-
build: "tsup"
|
|
2192
|
-
},
|
|
2193
|
-
dependencies: {
|
|
2194
|
-
commander: "^14.0.0",
|
|
2195
|
-
"ts-morph": "^26.0.0",
|
|
2196
|
-
"ts-node": "^10.9.2",
|
|
2197
|
-
typescript: "^5.8.3",
|
|
2198
|
-
"@types/swagger-schema-official": "^2.0.25"
|
|
2199
|
-
},
|
|
2200
|
-
peerDependencies: {
|
|
2201
|
-
"@angular/core": ">=15",
|
|
2202
|
-
"@angular/common": ">=15"
|
|
2203
|
-
},
|
|
2204
|
-
peerDependenciesMeta: {
|
|
2205
|
-
"@angular/core": {
|
|
2206
|
-
optional: false
|
|
2207
|
-
},
|
|
2208
|
-
"@angular/common": {
|
|
2209
|
-
optional: false
|
|
2210
|
-
}
|
|
2211
|
-
},
|
|
2212
|
-
engines: {
|
|
2213
|
-
node: ">=18.0.0",
|
|
2214
|
-
npm: ">=8.0.0"
|
|
2215
|
-
},
|
|
2216
|
-
publishConfig: {
|
|
2217
|
-
access: "public",
|
|
2218
|
-
registry: "https://registry.npmjs.org/"
|
|
2219
|
-
}
|
|
2220
|
-
};
|
|
2221
|
-
|
|
2222
2123
|
// src/lib/cli.ts
|
|
2223
2124
|
var program = new import_commander.Command();
|
|
2224
2125
|
async function loadConfigFile(configPath) {
|
|
@@ -2245,19 +2146,17 @@ __name(loadConfigFile, "loadConfigFile");
|
|
|
2245
2146
|
async function generateFromOptions(options) {
|
|
2246
2147
|
try {
|
|
2247
2148
|
if (options.config) {
|
|
2248
|
-
const
|
|
2249
|
-
|
|
2250
|
-
];
|
|
2251
|
-
for (const configPath of configPaths) {
|
|
2252
|
-
const config = await loadConfigFile(configPath);
|
|
2253
|
-
await generateFromConfig(config);
|
|
2254
|
-
console.log(`\u2728 Generated client: ${config.clientName || "default"}`);
|
|
2255
|
-
}
|
|
2149
|
+
const config = await loadConfigFile(options.config);
|
|
2150
|
+
await generateFromConfig(config);
|
|
2256
2151
|
} else if (options.input) {
|
|
2152
|
+
const inputPath = path8.resolve(options.input);
|
|
2153
|
+
if (!fs4.existsSync(inputPath)) {
|
|
2154
|
+
console.error(`Error: Input file not found: ${inputPath}`);
|
|
2155
|
+
process.exit(1);
|
|
2156
|
+
}
|
|
2257
2157
|
const config = {
|
|
2258
|
-
input:
|
|
2158
|
+
input: inputPath,
|
|
2259
2159
|
output: options.output || "./src/generated",
|
|
2260
|
-
clientName: options.clientName,
|
|
2261
2160
|
options: {
|
|
2262
2161
|
dateType: options.dateType || "Date",
|
|
2263
2162
|
enumStyle: "enum",
|
|
@@ -2271,14 +2170,14 @@ async function generateFromOptions(options) {
|
|
|
2271
2170
|
program.help();
|
|
2272
2171
|
process.exit(1);
|
|
2273
2172
|
}
|
|
2274
|
-
console.log("\u2728
|
|
2173
|
+
console.log("\u2728 Generation completed successfully!");
|
|
2275
2174
|
} catch (error) {
|
|
2276
2175
|
console.error("\u274C Generation failed:", error instanceof Error ? error.message : error);
|
|
2277
2176
|
process.exit(1);
|
|
2278
2177
|
}
|
|
2279
2178
|
}
|
|
2280
2179
|
__name(generateFromOptions, "generateFromOptions");
|
|
2281
|
-
program.name("ng-openapi").description("Generate Angular services and types from Swagger/OpenAPI spec").version(
|
|
2180
|
+
program.name("ng-openapi").description("Generate Angular services and types from Swagger/OpenAPI spec").version("0.0.1").option("-c, --config <path>", "Path to configuration file").option("-i, --input <path>", "Path to Swagger/OpenAPI specification file").option("-o, --output <path>", "Output directory", "./src/generated").option("--types-only", "Generate only TypeScript interfaces").option("--date-type <type>", "Date type to use (string | Date)", "Date").action(async (options) => {
|
|
2282
2181
|
await generateFromOptions(options);
|
|
2283
2182
|
});
|
|
2284
2183
|
program.command("generate").alias("gen").description("Generate code from Swagger specification").option("-c, --config <path>", "Path to configuration file").option("-i, --input <path>", "Path to Swagger/OpenAPI specification file").option("-o, --output <path>", "Output directory", "./src/generated").option("--types-only", "Generate only TypeScript interfaces").option("--date-type <type>", "Date type to use (string | Date)", "Date").action(async (options) => {
|
|
@@ -2293,8 +2192,4 @@ program.on("--help", () => {
|
|
|
2293
2192
|
console.log(" $ ng-openapi generate -i ./api.yaml --types-only");
|
|
2294
2193
|
});
|
|
2295
2194
|
program.parse();
|
|
2296
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
2297
|
-
0 && (module.exports = {
|
|
2298
|
-
generateFromOptions
|
|
2299
|
-
});
|
|
2300
2195
|
//# sourceMappingURL=cli.cjs.map
|
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<{
|
|
@@ -47,6 +48,12 @@ interface GeneratorConfig {
|
|
|
47
48
|
strict?: boolean;
|
|
48
49
|
};
|
|
49
50
|
}
|
|
51
|
+
interface NgOpenapiClientConfig {
|
|
52
|
+
clientName: string;
|
|
53
|
+
basePath: string;
|
|
54
|
+
enableDateTransform?: boolean;
|
|
55
|
+
interceptors?: HttpInterceptor[];
|
|
56
|
+
}
|
|
50
57
|
|
|
51
58
|
interface Parameter {
|
|
52
59
|
name: string;
|
|
@@ -168,4 +175,4 @@ declare class SwaggerParser {
|
|
|
168
175
|
*/
|
|
169
176
|
declare function generateFromConfig(config: GeneratorConfig): Promise<void>;
|
|
170
177
|
|
|
171
|
-
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,11 +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, "
|
|
446
|
+
__publicField(this, "clientName");
|
|
447
447
|
this.project = project;
|
|
448
|
-
this.
|
|
448
|
+
this.clientName = clientName;
|
|
449
449
|
}
|
|
450
450
|
generate(outputDir) {
|
|
451
451
|
const tokensDir = path.join(outputDir, "tokens");
|
|
@@ -455,26 +455,27 @@ var _TokenGenerator = class _TokenGenerator {
|
|
|
455
455
|
});
|
|
456
456
|
sourceFile.addImportDeclaration({
|
|
457
457
|
namedImports: [
|
|
458
|
-
"InjectionToken"
|
|
458
|
+
"InjectionToken",
|
|
459
|
+
"HttpInterceptor"
|
|
459
460
|
],
|
|
460
461
|
moduleSpecifier: "@angular/core"
|
|
461
462
|
});
|
|
462
|
-
const
|
|
463
|
-
const
|
|
463
|
+
const basePathTokenName = this.getBasePathTokenName();
|
|
464
|
+
const interceptorsTokenName = this.getInterceptorsTokenName();
|
|
464
465
|
sourceFile.addVariableStatement({
|
|
465
466
|
isExported: true,
|
|
466
467
|
declarationKind: import_ts_morph2.VariableDeclarationKind.Const,
|
|
467
468
|
declarations: [
|
|
468
469
|
{
|
|
469
|
-
name:
|
|
470
|
-
initializer: `new InjectionToken<string>('${
|
|
470
|
+
name: basePathTokenName,
|
|
471
|
+
initializer: `new InjectionToken<string>('${basePathTokenName}', {
|
|
471
472
|
providedIn: 'root',
|
|
472
|
-
factory: () => '/api',
|
|
473
|
+
factory: () => '/api', // Default fallback
|
|
473
474
|
})`
|
|
474
475
|
}
|
|
475
476
|
],
|
|
476
477
|
leadingTrivia: `/**
|
|
477
|
-
*
|
|
478
|
+
* Injection token for the ${this.clientName} client base API path
|
|
478
479
|
*/
|
|
479
480
|
`
|
|
480
481
|
});
|
|
@@ -483,33 +484,44 @@ var _TokenGenerator = class _TokenGenerator {
|
|
|
483
484
|
declarationKind: import_ts_morph2.VariableDeclarationKind.Const,
|
|
484
485
|
declarations: [
|
|
485
486
|
{
|
|
486
|
-
name:
|
|
487
|
-
initializer: `new InjectionToken<
|
|
487
|
+
name: interceptorsTokenName,
|
|
488
|
+
initializer: `new InjectionToken<HttpInterceptor[]>('${interceptorsTokenName}', {
|
|
489
|
+
providedIn: 'root',
|
|
490
|
+
factory: () => [], // Default empty array
|
|
491
|
+
})`
|
|
488
492
|
}
|
|
489
493
|
],
|
|
490
494
|
leadingTrivia: `/**
|
|
491
|
-
*
|
|
495
|
+
* Injection token for the ${this.clientName} client HTTP interceptors
|
|
492
496
|
*/
|
|
493
497
|
`
|
|
494
498
|
});
|
|
495
|
-
if (
|
|
499
|
+
if (this.clientName === "default") {
|
|
496
500
|
sourceFile.addVariableStatement({
|
|
497
501
|
isExported: true,
|
|
498
502
|
declarationKind: import_ts_morph2.VariableDeclarationKind.Const,
|
|
499
503
|
declarations: [
|
|
500
504
|
{
|
|
501
505
|
name: "BASE_PATH",
|
|
502
|
-
initializer:
|
|
506
|
+
initializer: basePathTokenName
|
|
503
507
|
}
|
|
504
508
|
],
|
|
505
509
|
leadingTrivia: `/**
|
|
506
|
-
* @deprecated Use ${
|
|
510
|
+
* @deprecated Use ${basePathTokenName} instead
|
|
507
511
|
*/
|
|
508
512
|
`
|
|
509
513
|
});
|
|
510
514
|
}
|
|
511
515
|
sourceFile.saveSync();
|
|
512
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
|
+
}
|
|
513
525
|
};
|
|
514
526
|
__name(_TokenGenerator, "TokenGenerator");
|
|
515
527
|
var TokenGenerator = _TokenGenerator;
|
|
@@ -1780,9 +1792,7 @@ var _ServiceGenerator = class _ServiceGenerator {
|
|
|
1780
1792
|
});
|
|
1781
1793
|
}
|
|
1782
1794
|
addImports(sourceFile, usedTypes) {
|
|
1783
|
-
|
|
1784
|
-
const clientName = this.config.clientName || "DEFAULT";
|
|
1785
|
-
const upperCaseClientName = clientName.toUpperCase().replace(/[^A-Z0-9]/g, "_");
|
|
1795
|
+
const basePathTokenName = this.getBasePathTokenName();
|
|
1786
1796
|
sourceFile.addImportDeclarations([
|
|
1787
1797
|
{
|
|
1788
1798
|
namedImports: [
|
|
@@ -1810,15 +1820,11 @@ var _ServiceGenerator = class _ServiceGenerator {
|
|
|
1810
1820
|
},
|
|
1811
1821
|
{
|
|
1812
1822
|
namedImports: [
|
|
1813
|
-
|
|
1814
|
-
`${upperCaseClientName}_HTTP_CLIENT`
|
|
1823
|
+
basePathTokenName
|
|
1815
1824
|
],
|
|
1816
1825
|
moduleSpecifier: "../tokens"
|
|
1817
1826
|
}
|
|
1818
1827
|
]);
|
|
1819
|
-
if (!this.config.clientName) {
|
|
1820
|
-
(_a = sourceFile.getImportDeclaration("../tokens")) == null ? void 0 : _a.addNamedImport("BASE_PATH");
|
|
1821
|
-
}
|
|
1822
1828
|
if (usedTypes.size > 0) {
|
|
1823
1829
|
sourceFile.addImportDeclaration({
|
|
1824
1830
|
namedImports: Array.from(usedTypes).sort(),
|
|
@@ -1828,8 +1834,7 @@ var _ServiceGenerator = class _ServiceGenerator {
|
|
|
1828
1834
|
}
|
|
1829
1835
|
addServiceClass(sourceFile, controllerName, operations) {
|
|
1830
1836
|
const className = `${controllerName}Service`;
|
|
1831
|
-
const
|
|
1832
|
-
const upperCaseClientName = clientName.toUpperCase().replace(/[^A-Z0-9]/g, "_");
|
|
1837
|
+
const basePathTokenName = this.getBasePathTokenName();
|
|
1833
1838
|
sourceFile.insertText(0, SERVICE_GENERATOR_HEADER_COMMENT(controllerName));
|
|
1834
1839
|
const serviceClass = sourceFile.addClass({
|
|
1835
1840
|
name: className,
|
|
@@ -1848,14 +1853,14 @@ var _ServiceGenerator = class _ServiceGenerator {
|
|
|
1848
1853
|
type: "HttpClient",
|
|
1849
1854
|
scope: import_ts_morph3.Scope.Private,
|
|
1850
1855
|
isReadonly: true,
|
|
1851
|
-
initializer:
|
|
1856
|
+
initializer: "inject(HttpClient)"
|
|
1852
1857
|
});
|
|
1853
1858
|
serviceClass.addProperty({
|
|
1854
1859
|
name: "basePath",
|
|
1855
1860
|
type: "string",
|
|
1856
1861
|
scope: import_ts_morph3.Scope.Private,
|
|
1857
1862
|
isReadonly: true,
|
|
1858
|
-
initializer: `inject(${
|
|
1863
|
+
initializer: `inject(${basePathTokenName})`
|
|
1859
1864
|
});
|
|
1860
1865
|
operations.forEach((operation) => {
|
|
1861
1866
|
this.methodGenerator.addServiceMethod(serviceClass, operation);
|
|
@@ -1864,6 +1869,11 @@ var _ServiceGenerator = class _ServiceGenerator {
|
|
|
1864
1869
|
throw new Error(`Duplicate method names found in service class ${className}. Please ensure unique method names for each operation.`);
|
|
1865
1870
|
}
|
|
1866
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
|
+
}
|
|
1867
1877
|
hasDuplicateMethodNames(arr) {
|
|
1868
1878
|
return new Set(arr.map((method) => method.getName())).size !== arr.length;
|
|
1869
1879
|
}
|
|
@@ -1908,8 +1918,10 @@ var _ProviderGenerator = class _ProviderGenerator {
|
|
|
1908
1918
|
constructor(project, config) {
|
|
1909
1919
|
__publicField(this, "project");
|
|
1910
1920
|
__publicField(this, "config");
|
|
1921
|
+
__publicField(this, "clientName");
|
|
1911
1922
|
this.project = project;
|
|
1912
1923
|
this.config = config;
|
|
1924
|
+
this.clientName = config.clientName || "default";
|
|
1913
1925
|
}
|
|
1914
1926
|
generate(outputDir) {
|
|
1915
1927
|
const filePath = path7.join(outputDir, "providers.ts");
|
|
@@ -1917,37 +1929,37 @@ var _ProviderGenerator = class _ProviderGenerator {
|
|
|
1917
1929
|
overwrite: true
|
|
1918
1930
|
});
|
|
1919
1931
|
sourceFile.insertText(0, PROVIDER_GENERATOR_HEADER_COMMENT);
|
|
1920
|
-
const
|
|
1921
|
-
const
|
|
1922
|
-
const
|
|
1932
|
+
const basePathTokenName = this.getBasePathTokenName();
|
|
1933
|
+
const interceptorsTokenName = this.getInterceptorsTokenName();
|
|
1934
|
+
const baseInterceptorClassName = `${this.capitalizeFirst(this.clientName)}BaseInterceptor`;
|
|
1923
1935
|
sourceFile.addImportDeclarations([
|
|
1924
1936
|
{
|
|
1925
1937
|
namedImports: [
|
|
1926
1938
|
"EnvironmentProviders",
|
|
1927
1939
|
"Provider",
|
|
1928
|
-
"makeEnvironmentProviders"
|
|
1929
|
-
"Type",
|
|
1930
|
-
"Injector",
|
|
1931
|
-
"InjectionToken"
|
|
1940
|
+
"makeEnvironmentProviders"
|
|
1932
1941
|
],
|
|
1933
1942
|
moduleSpecifier: "@angular/core"
|
|
1934
1943
|
},
|
|
1935
1944
|
{
|
|
1936
1945
|
namedImports: [
|
|
1937
|
-
"
|
|
1938
|
-
"HttpInterceptor"
|
|
1939
|
-
"HttpHandler",
|
|
1940
|
-
"HttpBackend",
|
|
1941
|
-
"HTTP_INTERCEPTORS"
|
|
1946
|
+
"HTTP_INTERCEPTORS",
|
|
1947
|
+
"HttpInterceptor"
|
|
1942
1948
|
],
|
|
1943
1949
|
moduleSpecifier: "@angular/common/http"
|
|
1944
1950
|
},
|
|
1945
1951
|
{
|
|
1946
1952
|
namedImports: [
|
|
1947
|
-
|
|
1948
|
-
|
|
1953
|
+
basePathTokenName,
|
|
1954
|
+
interceptorsTokenName
|
|
1949
1955
|
],
|
|
1950
1956
|
moduleSpecifier: "./tokens"
|
|
1957
|
+
},
|
|
1958
|
+
{
|
|
1959
|
+
namedImports: [
|
|
1960
|
+
baseInterceptorClassName
|
|
1961
|
+
],
|
|
1962
|
+
moduleSpecifier: "./utils/base-interceptor"
|
|
1951
1963
|
}
|
|
1952
1964
|
]);
|
|
1953
1965
|
if (this.config.options.dateType === "Date") {
|
|
@@ -1959,10 +1971,10 @@ var _ProviderGenerator = class _ProviderGenerator {
|
|
|
1959
1971
|
});
|
|
1960
1972
|
}
|
|
1961
1973
|
sourceFile.addInterface({
|
|
1962
|
-
name: `${
|
|
1974
|
+
name: `${this.capitalizeFirst(this.clientName)}Config`,
|
|
1963
1975
|
isExported: true,
|
|
1964
1976
|
docs: [
|
|
1965
|
-
`Configuration options for ${clientName}
|
|
1977
|
+
`Configuration options for ${this.clientName} client`
|
|
1966
1978
|
],
|
|
1967
1979
|
properties: [
|
|
1968
1980
|
{
|
|
@@ -1973,116 +1985,76 @@ var _ProviderGenerator = class _ProviderGenerator {
|
|
|
1973
1985
|
]
|
|
1974
1986
|
},
|
|
1975
1987
|
{
|
|
1976
|
-
name: "
|
|
1977
|
-
type: "
|
|
1988
|
+
name: "enableDateTransform",
|
|
1989
|
+
type: "boolean",
|
|
1978
1990
|
hasQuestionToken: true,
|
|
1979
1991
|
docs: [
|
|
1980
|
-
"
|
|
1992
|
+
"Enable automatic date transformation (default: true)"
|
|
1981
1993
|
]
|
|
1982
1994
|
},
|
|
1983
1995
|
{
|
|
1984
|
-
name: "
|
|
1985
|
-
type: "
|
|
1996
|
+
name: "interceptors",
|
|
1997
|
+
type: "HttpInterceptor[]",
|
|
1986
1998
|
hasQuestionToken: true,
|
|
1987
1999
|
docs: [
|
|
1988
|
-
"
|
|
2000
|
+
"Array of HTTP interceptors to apply to this client"
|
|
1989
2001
|
]
|
|
1990
2002
|
}
|
|
1991
2003
|
]
|
|
1992
2004
|
});
|
|
1993
|
-
|
|
1994
|
-
isExported: true,
|
|
1995
|
-
declarationKind: "const",
|
|
1996
|
-
declarations: [
|
|
1997
|
-
{
|
|
1998
|
-
name: `${upperCaseClientName}_INTERCEPTORS`,
|
|
1999
|
-
initializer: `new InjectionToken<HttpInterceptor[]>('${upperCaseClientName}_INTERCEPTORS')`
|
|
2000
|
-
}
|
|
2001
|
-
],
|
|
2002
|
-
leadingTrivia: `/**
|
|
2003
|
-
* Interceptor token for ${clientName} client
|
|
2004
|
-
*/
|
|
2005
|
-
`
|
|
2006
|
-
});
|
|
2007
|
-
this.addSimpleProviderFunction(sourceFile, pascalClientName, upperCaseClientName);
|
|
2005
|
+
this.addMainProviderFunction(sourceFile, basePathTokenName, interceptorsTokenName, baseInterceptorClassName);
|
|
2008
2006
|
sourceFile.saveSync();
|
|
2009
2007
|
}
|
|
2010
|
-
|
|
2008
|
+
addMainProviderFunction(sourceFile, basePathTokenName, interceptorsTokenName, baseInterceptorClassName) {
|
|
2011
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`;
|
|
2012
2012
|
const functionBody = `
|
|
2013
2013
|
const providers: Provider[] = [
|
|
2014
|
-
// Base path token
|
|
2014
|
+
// Base path token for this client
|
|
2015
2015
|
{
|
|
2016
|
-
provide: ${
|
|
2016
|
+
provide: ${basePathTokenName},
|
|
2017
2017
|
useValue: config.basePath
|
|
2018
2018
|
},
|
|
2019
|
-
|
|
2020
|
-
// Collect interceptors for this client
|
|
2019
|
+
// Client-specific interceptors token
|
|
2021
2020
|
{
|
|
2022
|
-
provide: ${
|
|
2023
|
-
|
|
2024
|
-
const interceptorInstances: HttpInterceptor[] = [];
|
|
2025
|
-
|
|
2026
|
-
// Add custom interceptors
|
|
2027
|
-
if (config.interceptors?.length) {
|
|
2028
|
-
config.interceptors.forEach(interceptorClass => {
|
|
2029
|
-
interceptorInstances.push(injector.get(interceptorClass));
|
|
2030
|
-
});
|
|
2031
|
-
}
|
|
2032
|
-
|
|
2033
|
-
${hasDateInterceptor ? `
|
|
2034
|
-
// Add date interceptor if enabled (default: true)
|
|
2035
|
-
if (config.enableDateTransform !== false) {
|
|
2036
|
-
interceptorInstances.push(injector.get(DateInterceptor));
|
|
2037
|
-
}` : ""}
|
|
2038
|
-
|
|
2039
|
-
return interceptorInstances;
|
|
2040
|
-
},
|
|
2041
|
-
deps: [Injector]
|
|
2021
|
+
provide: ${interceptorsTokenName},
|
|
2022
|
+
useValue: config.interceptors || []
|
|
2042
2023
|
},
|
|
2043
|
-
|
|
2044
|
-
// Create HTTP client with interceptors
|
|
2024
|
+
// Base interceptor that handles client-specific interceptors
|
|
2045
2025
|
{
|
|
2046
|
-
provide:
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
return new HttpClient(backend);
|
|
2050
|
-
}
|
|
2051
|
-
|
|
2052
|
-
// Create handler chain
|
|
2053
|
-
let handler = backend;
|
|
2054
|
-
for (let i = interceptors.length - 1; i >= 0; i--) {
|
|
2055
|
-
const interceptor = interceptors[i];
|
|
2056
|
-
const currentHandler = handler;
|
|
2057
|
-
handler = {
|
|
2058
|
-
handle: req => interceptor.intercept(req, currentHandler)
|
|
2059
|
-
};
|
|
2060
|
-
}
|
|
2061
|
-
|
|
2062
|
-
return new HttpClient(handler);
|
|
2063
|
-
},
|
|
2064
|
-
deps: [HttpBackend, ${upperCaseClientName}_INTERCEPTORS]
|
|
2026
|
+
provide: HTTP_INTERCEPTORS,
|
|
2027
|
+
useClass: ${baseInterceptorClassName},
|
|
2028
|
+
multi: true
|
|
2065
2029
|
}
|
|
2066
2030
|
];
|
|
2067
2031
|
|
|
2032
|
+
${hasDateInterceptor ? `// Add date interceptor to client-specific interceptors if enabled
|
|
2033
|
+
if (config.enableDateTransform !== false) {
|
|
2034
|
+
const currentInterceptors = config.interceptors || [];
|
|
2035
|
+
providers.push({
|
|
2036
|
+
provide: ${interceptorsTokenName},
|
|
2037
|
+
useValue: [new DateInterceptor(), ...currentInterceptors]
|
|
2038
|
+
});
|
|
2039
|
+
}` : `// Date transformation not available (dateType: 'string' was used in generation)`}
|
|
2040
|
+
|
|
2068
2041
|
return makeEnvironmentProviders(providers);`;
|
|
2069
2042
|
sourceFile.addFunction({
|
|
2070
|
-
name:
|
|
2043
|
+
name: functionName,
|
|
2071
2044
|
isExported: true,
|
|
2072
2045
|
docs: [
|
|
2073
|
-
`Provides configuration for ${
|
|
2046
|
+
`Provides configuration for ${this.clientName} client`,
|
|
2074
2047
|
"",
|
|
2075
2048
|
"@example",
|
|
2076
2049
|
"```typescript",
|
|
2077
|
-
|
|
2078
|
-
`import {
|
|
2079
|
-
`import { AuthInterceptor } from './interceptors/auth.interceptor';`,
|
|
2050
|
+
"// In your app.config.ts",
|
|
2051
|
+
`import { ${functionName} } from './api/providers';`,
|
|
2080
2052
|
"",
|
|
2081
2053
|
"export const appConfig: ApplicationConfig = {",
|
|
2082
2054
|
" providers: [",
|
|
2083
|
-
`
|
|
2055
|
+
` ${functionName}({`,
|
|
2084
2056
|
" basePath: 'https://api.example.com',",
|
|
2085
|
-
" interceptors: [AuthInterceptor]",
|
|
2057
|
+
" interceptors: [new LoggingInterceptor(), new AuthInterceptor()]",
|
|
2086
2058
|
" }),",
|
|
2087
2059
|
" // other providers...",
|
|
2088
2060
|
" ]",
|
|
@@ -2092,15 +2064,41 @@ return makeEnvironmentProviders(providers);`;
|
|
|
2092
2064
|
parameters: [
|
|
2093
2065
|
{
|
|
2094
2066
|
name: "config",
|
|
2095
|
-
type:
|
|
2067
|
+
type: configTypeName
|
|
2096
2068
|
}
|
|
2097
2069
|
],
|
|
2098
2070
|
returnType: "EnvironmentProviders",
|
|
2099
2071
|
statements: functionBody
|
|
2100
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
|
+
}
|
|
2091
|
+
}
|
|
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}`;
|
|
2101
2099
|
}
|
|
2102
|
-
|
|
2103
|
-
return str.
|
|
2100
|
+
capitalizeFirst(str) {
|
|
2101
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
2104
2102
|
}
|
|
2105
2103
|
};
|
|
2106
2104
|
__name(_ProviderGenerator, "ProviderGenerator");
|
|
@@ -2134,7 +2132,7 @@ function generateFromConfig(config) {
|
|
|
2134
2132
|
typeGenerator.generate();
|
|
2135
2133
|
console.log(`\u2705 TypeScript interfaces generated`);
|
|
2136
2134
|
if (generateServices) {
|
|
2137
|
-
const tokenGenerator = new TokenGenerator(project
|
|
2135
|
+
const tokenGenerator = new TokenGenerator(project);
|
|
2138
2136
|
tokenGenerator.generate(outputPath);
|
|
2139
2137
|
if (config.options.dateType === "Date") {
|
|
2140
2138
|
const dateTransformer = new DateTransformerGenerator(project);
|