Skip to content

Commit

Permalink
Merge pull request #781 from hey-api/fix/operation-name-builder-class
Browse files Browse the repository at this point in the history
fix: use methodNameBuilder when asClass is false
  • Loading branch information
mrlubos authored Jul 15, 2024
2 parents 7a5f91d + df3b799 commit 8bd45bf
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/great-cougars-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix: use methodNameBuilder when asClass is false
51 changes: 47 additions & 4 deletions packages/openapi-ts/src/generate/__tests__/services.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ describe('methodNameBuilder', () => {
);
});

it('call methodNameBuilder', async () => {
const methodNameBuilderMock = vi.fn().mockReturnValue('customName');
it('use methodNameBuilder when asClass is true', async () => {
const methodNameBuilder = vi.fn().mockReturnValue('customName');

setConfig({
client: 'fetch',
Expand All @@ -160,7 +160,7 @@ describe('methodNameBuilder', () => {
schemas: {},
services: {
asClass: true,
methodNameBuilder: methodNameBuilderMock,
methodNameBuilder,
},
types: {},
useOptions: false,
Expand All @@ -183,6 +183,49 @@ describe('methodNameBuilder', () => {
expect.stringContaining('public static customName()'),
);

expect(methodNameBuilderMock).toHaveBeenCalledWith(operation);
expect(methodNameBuilder).toHaveBeenCalledWith(operation);
});

it('use methodNameBuilder when asClass is false', async () => {
const methodNameBuilder = vi.fn().mockReturnValue('customName');

setConfig({
client: 'fetch',
configFile: '',
debug: false,
dryRun: false,
exportCore: true,
input: '',
output: {
path: '',
},
plugins: [],
schemas: {},
services: {
asClass: false,
methodNameBuilder,
},
types: {},
useOptions: false,
});

const file = new TypeScriptFile({
dir: '/',
name: 'services.ts',
});
const files = {
services: file,
};

await generateServices({ client, files });

file.write();

expect(writeFileSync).toHaveBeenCalledWith(
path.resolve('/services.gen.ts'),
expect.stringContaining('public static customName()'),
);

expect(methodNameBuilder).toHaveBeenCalledWith(operation);
});
});
16 changes: 12 additions & 4 deletions packages/openapi-ts/src/generate/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,16 @@ const toRequestOptions = (
});
};

const toOperationName = (operation: Operation) => {
const config = getConfig();

if (!config.services.methodNameBuilder) {
return operation.name;
}

return config.services.methodNameBuilder(operation);
};

const toOperationStatements = (
client: Client,
operation: Operation,
Expand Down Expand Up @@ -534,7 +544,7 @@ const processService = (
const statement = compiler.export.const({
comment: toOperationComment(operation),
expression,
name: operation.name,
name: toOperationName(operation),
});
onNode(statement);
});
Expand All @@ -546,9 +556,7 @@ const processService = (
accessLevel: 'public',
comment: toOperationComment(operation),
isStatic: config.name === undefined && config.client !== 'angular',
name: config.services.methodNameBuilder
? config.services.methodNameBuilder(operation)
: operation.name,
name: toOperationName(operation),
parameters: toOperationParamType(client, operation),
returnType: isStandalone
? undefined
Expand Down

0 comments on commit 8bd45bf

Please sign in to comment.