Skip to content

TypeScript Fetch Clients Generator

Marc Schmidt edited this page Jul 14, 2024 · 1 revision

The TypeScriptFetchClientsGenerator class is available in the @goast/typescript NPM package.

Input

The TypeScriptFetchClientsGenerator requires the following input data:

Configuration

Common generator configuration

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.

Common TypeScript generator configuration

Property Type Default Description
importModuleTransformer ImportModuleTransformer 'omit-extension' The transformer to use for transforming import module paths.
fileNameCasing ExtendedStringCasing Kebab case with "-client" suffix 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.

TypeScriptFetchClientsGenerator configuration

Property Type Default Description
clientFileNameCasing ExtendedStringCasing (nullable) Kebab case with "-client" suffix The casing of the client file names. If nullish, the fileNameCasing is used.
clientInterfaceFileNameCasing ExtendedStringCasing (nullable) Kebab case with "-client" suffix The casing of the client interface file names. If nullish, the fileNameCasing is used.
clientNameCasing ExtendedStringCasing (nullable) Pascal case with "Client" suffix The casing of the client names. If nullish, the typeNameCasing is used.
clientInterfaceNameCasing ExtendedStringCasing (nullable) Pascal case with "I" prefix and "Client" suffix The casing of the client interface names. If nullish, the typeNameCasing is used.
clientFileKind 'interface' | 'class' | 'class-and-interface' 'class' Determines how the client files should be generated.
  • 'interface': Generate only the interface.
  • 'class': Generate only the class.
  • 'class-and-interface': Generate both the class and the interface.
clientDir string 'clients' The directory where the clients should be saved. The path is relative to the output directory.
clientInterfaceDir string 'clients' The directory where the client interfaces should be saved. The path is relative to the output directory.
clientsIndexFile string (nullable) 'clients.ts' The path to the index file where all clients should be exported. The path is relative to the output directory. If nullish, no index file will be generated.
clientInterfacesIndexFile string (nullable) 'clients.ts' The path to the index file where all client interfaces 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.

Output

The TypeScriptFetchClientsGenerator generates the following TypeScript files:

  • One file for each client (tag) that is placed in the {outputDir}/{clientDir} directory (if enabled).
  • One file fir each client (tag) interface that is placed in the {outputDir}/{clientInterfaceDir} directory (if enabled).
  • Utility files that are placed in the {outputDir}/{utilsDir} directory.
  • (optional) An index file that exports all clients at {outputDir}/{clientsIndexFile}.
  • (optional) An index file that exports all client interfaces at {outputDir}/{clientInterfacesIndexFile}.

It also provides the following data that can be used by subsequent generators:

  • typescript.clients: A record that maps the service id (ApiService['id']) to the following information about the generated client:
    • client?:
      • component: The name of the client class.
      • filePath: The path to the file where the class was saved.
    • clientInterface?:
      • component: The name of the client interface.
      • filePath: The path to the file where the interface was saved.
  • typescript.indexFiles:
    • clients: The path to the index file exporting all client classes that were generated.
    • clientInterfaces: The path to the index file exporting all client interfaces that were generated.

Extensibility

To extend the functionality of the TypeScriptFetchClientsGenerator, you have two options:

  1. Create a class that extends the DefaultTypeScriptFetchClientGenerator class.
  2. Create a class that implements the TypeScriptFetchClientGenerator interface.

You can use the custom implementation in two ways:

  1. Initialize an instance of the TypeScriptFetchClientsGenerator with the custom class directly.
  2. Create a new class that extends the TypeScriptFetchClientsGenerator and pass the custom implementation to its constructor.

These approaches allow you to customize and enhance the behavior of the TypeScriptFetchClientsGenerator to meet your specific requirements.

Examples

Create a custom generator by extending the DefaultTypeScriptFetchClientGenerator class

import { DefaultTypeScriptFetchClientGenerator } from '@goast/typescript';

class CustomTypeScriptFetchClientGenerator extends DefaultTypeScriptFetchClientGenerator {
  // Implement custom methods and method overrides here
}

Create a custom generator by implementing the TypeScriptFetchClientsGenerator interface

import {
  TypeScriptComponentOutput,
  TypeScriptFetchClientGenerator,
  TypeScriptFetchClientGeneratorContext
} from '@goast/typescript';

class CustomTypeScriptFetchClientGenerator implements TypeScriptFetchClientGenerator {
  public generate(ctx: TypeScriptFetchClientGeneratorContext): TypeScriptComponentOutput {
    // Implement custom generation logic here
  }
}

Use the custom generator directly

import { Factory, OpenApiGenerator } from '@goast/core';
import { TypeScriptFetchClientsGenerator } from '@goast/typescript';

new OpenApiGenerator({ outputDir: 'out' })
  .useValue(new TypeScriptFetchClientsGenerator(Factory.fromType(CustomTypeScriptFetchClientGenerator)))
  .parseAndGenerateFromDir('.openapi');

Use the custom generator by extending the TypeScriptFetchClientsGenerator class

import { Factory, OpenApiGenerator } from '@goast/core';
import { TypeScriptFetchClientsGenerator } from '@goast/typescript';

class CustomTypeScriptFetchClientsGenerator extends TypeScriptFetchClientsGenerator {
  constructor() {
    super(Factory.fromType(CustomTypeScriptFetchClientGenerator));
  }
  
  // Implement custom methods and method overrides here
}

new OpenApiGenerator({ outputDir: 'out' })
  .useType(CustomTypeScriptFetchClientsGenerator)
  .parseAndGenerateFromDir('.openapi');