Skip to content

Commit

Permalink
feat(typescript): adjust and make base type name configurable (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaSch0212 authored Dec 20, 2024
1 parent cca91d0 commit b3bd784
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/typescript/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@goast/typescript",
"version": "0.4.1",
"version": "0.4.2",
"description": "Provides gOAst generators for generating TypeScript code from OpenAPI specifications.",
"exports": "./mod.ts",
"publish": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ export class DefaultTypeScriptModelGenerator extends TypeScriptFileGenerator<Con
}

protected getBaseTypeName(ctx: Context, schema: ApiSchema): string {
return toCasing(schema.name + '_Base', ctx.config.typeNameCasing);
return toCasing(schema.name, ctx.config.discriminatorBaseTypeCasing);
}

protected getInheritedSchemas(ctx: Context, schema: ApiSchema): ApiSchema['inheritedSchemas'] {
Expand Down
7 changes: 7 additions & 0 deletions packages/typescript/src/generators/models/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ export type TypeScriptModelGeneratorConfig = TypeScriptGeneratorConfig & {
* @default 'type'
*/
typeDeclaration: 'type' | 'prefer-interface';

/**
* The casing to use for the base type of discriminator schemas.
* @default `_<SchemaName>Base`
*/
discriminatorBaseTypeCasing: ExtendedStringCasing;
};

export type TypeScriptModelsGeneratorConfig = TypeScriptModelGeneratorConfig & {
Expand All @@ -68,6 +74,7 @@ export const defaultTypeScriptModelGeneratorConfig: DefaultGenerationProviderCon
modelsDir: 'models',
modelFileNameCasing: undefined,
typeDeclaration: 'type',
discriminatorBaseTypeCasing: { casing: 'pascal', prefix: '_', suffix: 'Base' },
};

export const defaultTypeScriptModelsGeneratorConfig: DefaultGenerationProviderConfig<TypeScriptModelsGeneratorConfig> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
models: {
'schema-1': {
__source__: '<root>/test/openapi-files/v3/discriminated-schemas.yml#/components/schemas/Pet',
additionalExports: [ { name: 'PetBase', type: 'type-export' } ],
additionalExports: [ { name: '_PetBase', type: 'type-export' } ],
component: 'Pet',
filePath: '<root>/out/models/pet.ts',
imports: [
Expand Down Expand Up @@ -62,7 +62,7 @@
},
'schema-5': {
__source__: '<root>/test/openapi-files/v3/discriminated-schemas.yml#/components/schemas/Pet',
additionalExports: [ { name: 'PetBase', type: 'type-export' } ],
additionalExports: [ { name: '_PetBase', type: 'type-export' } ],
component: 'Pet',
filePath: '<root>/out/models/pet.ts',
imports: [
Expand Down Expand Up @@ -92,7 +92,7 @@
},
'schema-7': {
__source__: '<root>/test/openapi-files/v3/discriminated-schemas.yml#/components/schemas/Pet',
additionalExports: [ { name: 'PetBase', type: 'type-export' } ],
additionalExports: [ { name: '_PetBase', type: 'type-export' } ],
component: 'Pet',
filePath: '<root>/out/models/pet.ts',
imports: [
Expand Down Expand Up @@ -150,12 +150,12 @@ import type { Dog } from './dog';

type PetDiscriminator = ('dog') | ('cat');

export type PetBase = {
export type _PetBase = {
name: string;
petType: string;
};

export type Pet<TPetType extends PetDiscriminator = PetDiscriminator> = (PetBase) & (({
export type Pet<TPetType extends PetDiscriminator = PetDiscriminator> = (_PetBase) & (({
dog: ({
petType: 'dog';
}) & (Dog);
Expand Down Expand Up @@ -193,10 +193,10 @@ export const SCHEMA_9_VALUES = [
┌──────────────────────────────────────┐
│ <root>/out/models/cat.ts │
├──────────────────────────────────────┤
import type { PetBase } from './pet';
import type { _PetBase } from './pet';
import type { Schema9 } from './schema-9';

export type Cat = (Omit<(PetBase) & ({
export type Cat = (Omit<(_PetBase) & ({
/**
* The measured skill for hunting
*/
Expand All @@ -210,9 +210,9 @@ export type Cat = (Omit<(PetBase) & ({
┌──────────────────────────────────────┐
│ <root>/out/models/dog.ts │
├──────────────────────────────────────┤
import type { PetBase } from './pet';
import type { _PetBase } from './pet';

export type Dog = (Omit<(PetBase) & ({
export type Dog = (Omit<(_PetBase) & ({
/**
* the size of the pack the dog is from
*/
Expand All @@ -230,7 +230,7 @@ export { SCHEMA_9_VALUES } from './models/schema-9';

export type { Cat } from './models/cat';
export type { Dog } from './models/dog';
export type { Pet, PetBase } from './models/pet';
export type { Pet, _PetBase } from './models/pet';
export type { Schema9 } from './models/schema-9';

└──────────────────────────────────────┘
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
models: {
'schema-1': {
__source__: '<root>/test/openapi-files/v3.1/discriminated-schemas.yml#/components/schemas/Pet',
additionalExports: [ { name: 'PetBase', type: 'type-export' } ],
additionalExports: [ { name: '_PetBase', type: 'type-export' } ],
component: 'Pet',
filePath: '<root>/out/models/pet.ts',
imports: [
Expand Down Expand Up @@ -62,7 +62,7 @@
},
'schema-5': {
__source__: '<root>/test/openapi-files/v3.1/discriminated-schemas.yml#/components/schemas/Pet',
additionalExports: [ { name: 'PetBase', type: 'type-export' } ],
additionalExports: [ { name: '_PetBase', type: 'type-export' } ],
component: 'Pet',
filePath: '<root>/out/models/pet.ts',
imports: [
Expand Down Expand Up @@ -92,7 +92,7 @@
},
'schema-7': {
__source__: '<root>/test/openapi-files/v3.1/discriminated-schemas.yml#/components/schemas/Pet',
additionalExports: [ { name: 'PetBase', type: 'type-export' } ],
additionalExports: [ { name: '_PetBase', type: 'type-export' } ],
component: 'Pet',
filePath: '<root>/out/models/pet.ts',
imports: [
Expand Down Expand Up @@ -150,12 +150,12 @@ import type { Dog } from './dog';

type PetDiscriminator = ('dog') | ('cat');

export type PetBase = {
export type _PetBase = {
name: string;
petType: string;
};

export type Pet<TPetType extends PetDiscriminator = PetDiscriminator> = (PetBase) & (({
export type Pet<TPetType extends PetDiscriminator = PetDiscriminator> = (_PetBase) & (({
dog: ({
petType: 'dog';
}) & (Dog);
Expand Down Expand Up @@ -193,10 +193,10 @@ export const SCHEMA_9_VALUES = [
┌──────────────────────────────────────┐
│ <root>/out/models/cat.ts │
├──────────────────────────────────────┤
import type { PetBase } from './pet';
import type { _PetBase } from './pet';
import type { Schema9 } from './schema-9';

export type Cat = (Omit<(PetBase) & ({
export type Cat = (Omit<(_PetBase) & ({
/**
* The measured skill for hunting
*/
Expand All @@ -210,9 +210,9 @@ export type Cat = (Omit<(PetBase) & ({
┌──────────────────────────────────────┐
│ <root>/out/models/dog.ts │
├──────────────────────────────────────┤
import type { PetBase } from './pet';
import type { _PetBase } from './pet';

export type Dog = (Omit<(PetBase) & ({
export type Dog = (Omit<(_PetBase) & ({
/**
* the size of the pack the dog is from
*/
Expand All @@ -230,7 +230,7 @@ export { SCHEMA_9_VALUES } from './models/schema-9';

export type { Cat } from './models/cat';
export type { Dog } from './models/dog';
export type { Pet, PetBase } from './models/pet';
export type { Pet, _PetBase } from './models/pet';
export type { Schema9 } from './models/schema-9';

└──────────────────────────────────────┘

0 comments on commit b3bd784

Please sign in to comment.