Skip to content
This repository has been archived by the owner on Jun 14, 2021. It is now read-only.

Commit

Permalink
fix: build valid names for resouces with dashes
Browse files Browse the repository at this point in the history
  • Loading branch information
aimed committed Jul 7, 2019
1 parent ef2d62f commit e7e8a42
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
12 changes: 7 additions & 5 deletions codegen/src/generators/ResourcesGenerator/ResourcesGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { groupBy } from 'lodash'
import { OpenAPIV3 } from 'openapi-types'
import * as fsPath from 'path'
import { TSModule } from '../../typescript/TSModule'
import { capitalize } from '../../typescript/utils'
import { camelCaseify, capitalize } from '../../typescript/utils'
import { ComponentSchemaResponsesGenerator } from '../ComponentSchemaResponsesGenerator'
import { ComponentSchemaTypesGenerator } from '../ComponentSchemaTypesGenerator'
import { Generator } from '../Generator'
Expand Down Expand Up @@ -100,15 +100,17 @@ export class ResourcesGenerator implements Generator {
/**
* For a given path create a resource name.
* @example
* / -> Index
* /pets -> Pets
* /pets/:id -> Pets
* / -> Index
* /pets -> Pets
* /pets/:id -> Pets
* /pets-test -> PetsTest
*/
private getResourceNameForPath(path: string): string {
const [, prefix] = path.split('/')
if (!prefix || prefix.startsWith('{')) {
return capitalize('index')
}
return capitalize(prefix)
const normalizedPrefix = camelCaseify(prefix)
return capitalize(normalizedPrefix)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { StatusCodeClassNames } from '../../StatusCodesClassNames'
import { TSClassBuilder } from '../../typescript/TSClassBuilder'
import { TSFile } from '../../typescript/TSFile'
import { capitalize, isReferenceObject } from '../../typescript/utils'
import { camelCaseify, capitalize, isReferenceObject } from '../../typescript/utils'

/**
* Creates a resource operation response.
Expand Down Expand Up @@ -84,7 +84,7 @@ export class ResponseTypeFactory {
throw new Error('A status code reference must point to #/components/responses/')
}

const responseType = `${response.$ref.replace('#/components/responses/', '')}Response`
const responseType = `${camelCaseify(response.$ref.replace('#/components/responses/', ''))}Response`
tsFile.import(responseType)
responseClassBuilder.addConstructorParameter({
name: 'payload',
Expand Down
3 changes: 2 additions & 1 deletion codegen/src/typescript/ModuleResolver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { OpenAPIV3 } from 'openapi-types'
import { capitalize } from '../utils'
import { camelCaseify } from './utils'

export interface ResolvedReference {
path: string
Expand All @@ -17,7 +18,7 @@ export class ModuleResolver {
)
}

const symbol = capitalize(schema.$ref.replace('#/components/schemas/', ''))
const symbol = capitalize(camelCaseify(schema.$ref.replace('#/components/schemas/', '')))
const path = './types'

return {
Expand Down
4 changes: 2 additions & 2 deletions codegen/src/typescript/TSFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as prettier from 'prettier'
import ts from 'typescript'
import { IdentifierImport } from './IdentifierImport'
import { IdentifierRegistry } from './IdentifierRegistry'
import { capitalize, isReferenceObject } from './utils'
import { capitalize, isReferenceObject, camelCaseify } from './utils'

export class TSFile {
private readonly imports: IdentifierImport[] = []
Expand Down Expand Up @@ -136,7 +136,7 @@ export class TSFile {
)
}

const identifier = capitalize(schema.$ref.replace('#/components/schemas/', ''))
const identifier = capitalize(camelCaseify(schema.$ref.replace('#/components/schemas/', '')))
return identifier
}

Expand Down
8 changes: 7 additions & 1 deletion codegen/src/typescript/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ export function isReferenceObject(maybe: unknown): maybe is OpenAPIV3.ReferenceO
return typeof maybe === 'object' && maybe != null && !!(maybe as OpenAPIV3.ReferenceObject).$ref
}

export function capitalize(s: string) {
export function capitalize(s: string): string {
return s.charAt(0).toUpperCase() + s.slice(1)
}

export function camelCaseify(s: string): string {
return s.replace(/[\-\._]{1,}([a-zA-Z])?/g, (...values) => {
return (values[1] || '').toUpperCase()
})
}

0 comments on commit e7e8a42

Please sign in to comment.