-
Notifications
You must be signed in to change notification settings - Fork 2
TypeScript Angular Services Generator
Marc Schmidt edited this page Jul 14, 2024
·
1 revision
The TypeScriptAngularServicesGenerator
class is available in the @goast/typescript
NPM package.
The TypeScriptAngularServicesGenerator
requires the following input data:
- Same data structure as the output of the
TypeScriptModelsGenerator
.
Property | Type | Default | Description |
---|---|---|---|
outputDir | string |
(inherited) | The directory where the generated files should be saved. |
clearOutputDir | 'boolean' | (inherited) | No effect on this generator. |
indent | IndentOptions |
2 spaces | The type of indentation to use. |
newLine | string |
os.EOL |
The string to use for new lines. |
Property | Type | Default | Description |
---|---|---|---|
importModuleTransformer | ImportModuleTransformer |
'omit-extension' |
The transformer to use for transforming import module paths. |
fileNameCasing | ExtendedStringCasing |
'kebab' |
The casing to use for file names. |
typeNameCasing | ExtendedStringCasing |
'pascal' |
The casing to use for type names. |
functionNameCasing | ExtendedStringCasing |
'camel' |
The casing to use for function names. |
propertyNameCasing | ExtendedStringCasing |
'camel' |
The casing to use for property names. |
enumValueNameCasing | ExtendedStringCasing |
'pascal' |
The casing to use for enum value names. |
constantNameCasing | ExtendedStringCasing |
Upper snake case | The casing to use for constant names. |
genericParamNameCasing | ExtendedStringCasing |
Pascal case with T prefix |
The casing to use for generic parameter names. |
paramNameCasing | ExtendedStringCasing |
'camel' |
The casing to use for parameter names. |
preferUnknown | boolean |
true |
Whether to prefer unknown over any . |
useSingleQuotes | boolean |
true |
Whether to use single quotes for strings. |
Property | Type | Default | Description |
---|---|---|---|
provideKind |
'root | 'provide-fn'` |
'root' |
How the services should be provided in the Angular application.
|
domainName |
string (nullable) |
undefined |
The domain name of the API. Used as a prefix for exported components (e.g. ApiConfiguration ). |
strictResponseTypes | boolean |
true |
Whether to use strict response types for the services. If enabled only the status codes are included that are defined in the OpenAPI specification or the defaultStatusCodeResponseTypes option. |
defaultStatusCodeResponseTypes | Record<number, { parser, type }> |
Status codes 401, 403 and 500 with null response | The default response types for status codes that are not defined in the OpenAPI specification. |
possibleStatusCodes | number[] |
All known status codes | The possible status codes that any API endpoint can return. This is only used when strictResponseTypes is set to false . |
rootUrl |
string | RegExp | ((rootUrl: string) => string) (nullable) |
undefined |
Explicitly set the root URL of the API. If undefined , the root URL is taken from the first server in the OpenAPI specification. |
pathModifier |
RegExp | ((path: string, endpoint: ApiEndpoint) => string) (nullable) |
undefined |
Modifier that is applied to the path of each endpoint. |
servicesDir | string |
'services' |
The directory where the services should be saved. The path is relative to the output directory. |
serviceFileNameCasing |
ExtendedStringCasing (nullable) |
Kebab case with ".service" suffix | The casing of the file names for the services. If nullish, the fileNameCasing is used. |
servicesIndexFile |
string (nullable) |
'services.ts' |
The path to the index file where all services should be exported. The path is relative to the output directory. If nullish, no index file will be generated. |
responseModelsDir | string |
'models/responses' |
The directory where the response models should be saved. The path is relative to the output directory. |
responseModelFileNameCasing |
ExtendedStringCasing (nullable) |
Kebab case with "-responses.model" suffix | The casing of the file names for the response models. If nullish, the fileNameCasing is used. |
responseModelsIndexFile |
string (nullable) |
'responses.ts' |
The path to the index file where all response models should be exported. The path is relative to the output directory. If nullish, no index file will be generated. |
utilsDir | string |
'utils' |
The directory where the utilies should be saved. The path is relative to the output directory. |
The TypeScriptAngularServicesGenerator
generates the following TypeScript files:
- One file for each service (tag) that is placed in the
{outputDir}/{servicesDir}
directory. - One file for each response model that is placed in the
{outputDir}/{responseModelsDir}
directory. - Utility files that are placed in the
{outputDir}/{utilsDir}
directory. - (optional) An index file that exports all services at
{outputDir}/{servicesIndexFile}
. - (optional) An index file that exports all response models at
{outputDir}/{responseModelsIndexFile}
.
It also provides the following data that can be used by subsequent generators:
-
typescript.services
: A record that maps the service id (ApiService['id']
) to the following information about the generated service:-
component
: The name of the service class. -
filePath
: The path to the file where the class was saved. -
responseModels
: A record that maps the operation id to the following information about the generated response model:-
component
: The name of the response model type. -
filePath
: The path to the file where the type was saved.
-
-
-
typescript.indexFiles.services
: The path to the index file exporting all service classes that were generated. -
typescript.indexFiles.responseModels
: The path to the index file exporting all response models that were generated.
To extend the functionality of the TypeScriptAngularServicesGenerator
, you have two options:
- Create a class that extends the
DefaultTypeScriptAngularServiceGenerator
class. - Create a class that implements the
TypeScriptAngularServiceGenerator
interface.
You can use the custom implementation in two ways:
- Initialize an instance of the
TypeScriptAngularServicesGenerator
with the custom class directly. - Create a new class that extends the
TypeScriptAngularServicesGenerator
and pass the custom implementation to its constructor.
These approaches allow you to customize and enhance the behavior of the TypeScriptAngularServicesGenerator
to meet your specific requirements.
import { DefaultTypeScriptAngularServiceGenerator } from '@goast/typescript';
class CustomTypeScriptAngularServiceGenerator extends DefaultTypeScriptAngularServiceGenerator {
// Implement custom methods and method overrides here
}
import {
TypeScriptComponentOutput,
TypeScriptAngularServiceGenerator,
TypeScriptAngularServiceGeneratorContext
} from '@goast/typescript';
class CustomTypeScriptAngularServiceGenerator implements TypeScriptAngularServiceGenerator {
public generate(ctx: TypeScriptAngularServiceGeneratorContext): TypeScriptComponentOutput {
// Implement custom generation logic here
}
}
import { Factory, OpenApiGenerator } from '@goast/core';
import { TypeScriptAngularServicesGenerator } from '@goast/typescript';
new OpenApiGenerator({ outputDir: 'out' })
.useValue(new TypeScriptAngularServicesGenerator(Factory.fromType(CustomTypeScriptAngularServiceGenerator)))
.parseAndGenerateFromDir('.openapi');
import { Factory, OpenApiGenerator } from '@goast/core';
import { TypeScriptAngularServicesGenerator } from '@goast/typescript';
class CustomTypeScriptAngularServicesGenerator extends TypeScriptAngularServicesGenerator {
constructor() {
super(Factory.fromType(CustomTypeScriptAngularServiceGenerator));
}
// Implement custom methods and method overrides here
}
new OpenApiGenerator({ outputDir: 'out' })
.useType(CustomTypeScriptAngularServicesGenerator)
.parseAndGenerateFromDir('.openapi');