fork of hey-api/openapi-ts because I need some additional things
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #422 from hey-api/fix/pascalcase-name

fix: transform names of referenced types

authored by

Lubos and committed by
GitHub
b7b0859e ad0a6150

+43 -21
+5
.changeset/long-adults-dress.md
··· 1 + --- 2 + "@hey-api/openapi-ts": patch 3 + --- 4 + 5 + fix: transform names of referenced types
+11
packages/openapi-ts/src/utils/transform.ts
··· 1 + import camelcase from 'camelcase'; 2 + 3 + import { getConfig } from './config'; 4 + 5 + export const transformName = (name: string) => { 6 + const config = getConfig(); 7 + if (config.types.name === 'PascalCase') { 8 + return camelcase(name, { pascalCase: true }); 9 + } 10 + return name; 11 + };
+1 -10
packages/openapi-ts/src/utils/write/models.ts
··· 1 - import camelcase from 'camelcase'; 2 - 3 1 import { type Comments, compiler, type Node, TypeScriptFile } from '../../compiler'; 4 2 import { addLeadingComment } from '../../compiler/utils'; 5 3 import type { Model, OperationParameter, Service } from '../../openApi'; ··· 9 7 import { enumKey, enumName, enumUnionType, enumValue } from '../enum'; 10 8 import { escapeComment } from '../escape'; 11 9 import { sortByName } from '../sort'; 10 + import { transformName } from '../transform'; 12 11 import { serviceExportedNamespace } from './services'; 13 12 import { toType } from './type'; 14 13 ··· 91 90 addLeadingComment(node, comment); 92 91 onNode(node, 'enum'); 93 92 } 94 - }; 95 - 96 - const transformName = (name: string) => { 97 - const config = getConfig(); 98 - if (config.types.name === 'PascalCase') { 99 - return camelcase(name, { pascalCase: true }); 100 - } 101 - return name; 102 93 }; 103 94 104 95 const processType = (client: Client, model: Model, onNode: OnNode) => {
+7
packages/openapi-ts/src/utils/write/type.ts
··· 4 4 import { enumValue } from '../enum'; 5 5 import { escapeComment } from '../escape'; 6 6 import { modelIsRequired } from '../required'; 7 + import { transformName } from '../transform'; 7 8 import { unique } from '../unique'; 8 9 9 10 const base = (model: Model) => { ··· 13 14 } 14 15 if (config.useDateType && model.format === 'date-time') { 15 16 return compiler.typedef.basic('Date'); 17 + } 18 + // transform root level model names 19 + if (model.base === model.type && model.$refs.length) { 20 + if (model.$refs.some(ref => ref.endsWith(model.base))) { 21 + return compiler.typedef.basic(transformName(model.base)); 22 + } 16 23 } 17 24 return compiler.typedef.basic(model.base); 18 25 };
+1 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3/schemas.gen.ts.snap
··· 172 172 type: 'object', 173 173 properties: { 174 174 foo: { 175 - type: 'string', 175 + $ref: '#/components/schemas/camelCaseCommentWithBreaks', 176 176 }, 177 177 bar: { 178 178 type: 'string',
+1 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3/types.gen.ts.snap
··· 140 140 * This is a simple array with properties 141 141 */ 142 142 export type ArrayWithProperties = Array<{ 143 - foo?: string; 143 + foo?: camelCaseCommentWithBreaks; 144 144 bar?: string; 145 145 }>; 146 146
+1 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/schemas.gen.ts.snap
··· 172 172 type: 'object', 173 173 properties: { 174 174 foo: { 175 - type: 'string', 175 + $ref: '#/components/schemas/camelCaseCommentWithBreaks', 176 176 }, 177 177 bar: { 178 178 type: 'string',
+1 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3_angular/types.gen.ts.snap
··· 140 140 * This is a simple array with properties 141 141 */ 142 142 export type ArrayWithProperties = Array<{ 143 - foo?: string; 143 + foo?: camelCaseCommentWithBreaks; 144 144 bar?: string; 145 145 }>; 146 146
+1 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3_client/types.gen.ts.snap
··· 140 140 * This is a simple array with properties 141 141 */ 142 142 export type ArrayWithProperties = Array<{ 143 - foo?: string; 143 + foo?: camelCaseCommentWithBreaks; 144 144 bar?: string; 145 145 }>; 146 146
+1 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3_date/schemas.gen.ts.snap
··· 172 172 type: 'object', 173 173 properties: { 174 174 foo: { 175 - type: 'string', 175 + $ref: '#/components/schemas/camelCaseCommentWithBreaks', 176 176 }, 177 177 bar: { 178 178 type: 'string',
+1 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/schemas.gen.ts.snap
··· 172 172 type: 'object', 173 173 properties: { 174 174 foo: { 175 - type: 'string', 175 + $ref: '#/components/schemas/camelCaseCommentWithBreaks', 176 176 }, 177 177 bar: { 178 178 type: 'string',
+1 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3_enums_typescript/types.gen.ts.snap
··· 140 140 * This is a simple array with properties 141 141 */ 142 142 export type ArrayWithProperties = Array<{ 143 - foo?: string; 143 + foo?: camelCaseCommentWithBreaks; 144 144 bar?: string; 145 145 }>; 146 146
+1 -1
packages/openapi-ts/test/__snapshots__/test/generated/v3_models/types.gen.ts.snap
··· 140 140 * This is a simple array with properties 141 141 */ 142 142 export type ArrayWithProperties = Array<{ 143 - foo?: string; 143 + foo?: camelCaseCommentWithBreaks; 144 144 bar?: string; 145 145 }>; 146 146
+8
packages/openapi-ts/test/__snapshots__/test/generated/v3_pascalcase/types.gen.ts.snap
··· 7 7 * Fourth line 8 8 */ 9 9 export type CamelCaseCommentWithBreaks = number; 10 + 11 + /** 12 + * This is a simple array with properties 13 + */ 14 + export type ArrayWithProperties = Array<{ 15 + foo?: CamelCaseCommentWithBreaks; 16 + bar?: string; 17 + }>;
+1 -1
packages/openapi-ts/test/index.spec.ts
··· 182 182 exportServices: false, 183 183 schemas: false, 184 184 types: { 185 - include: '^camelCaseCommentWithBreaks', 185 + include: '^(camelCaseCommentWithBreaks|ArrayWithProperties)', 186 186 name: 'PascalCase', 187 187 }, 188 188 } as UserConfig,
+1 -1
packages/openapi-ts/test/spec/v3.json
··· 1772 1772 "type": "object", 1773 1773 "properties": { 1774 1774 "foo": { 1775 - "type": "string" 1775 + "$ref": "#/components/schemas/camelCaseCommentWithBreaks" 1776 1776 }, 1777 1777 "bar": { 1778 1778 "type": "string"