Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use methodNameBuilder when asClass is false #781

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading