Skip to content

Commit

Permalink
feat(generator): add the possibility to override the title
Browse files Browse the repository at this point in the history
  • Loading branch information
anymaniax committed Nov 2, 2020
1 parent 45dcdc4 commit 7551f07
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 36 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ type OverrideOutput = {
};

interface OutputOptions = {
// override title
title?: (title: string) => string
// default axios
client?: OutputClient
// path to the file which will contains the implementation
Expand Down
14 changes: 8 additions & 6 deletions src/core/generators/angular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,23 @@ export const generateAngularTitle = (title: string) => {
};
};

export const generateAngularHeader = (title: string) => {
const angularTitle = generateAngularTitle(title);

export const generateAngularHeader = (titles: {
definition: string;
implementation: string;
implementationMock: string;
}) => {
return {
definition: `
export abstract class ${angularTitle.definition} {`,
export abstract class ${titles.definition} {`,
implementation: `
@Injectable()
export class ${angularTitle.implementation} implements ${angularTitle.definition} {
export class ${titles.implementation} implements ${titles.definition} {
constructor(
private http: HttpClient,
) {}`,
implementationMock: `
@Injectable()
export class ${angularTitle.implementationMock} extends ${angularTitle.definition} {`,
export class ${titles.implementationMock} extends ${titles.definition} {`,
};
};

Expand Down
13 changes: 8 additions & 5 deletions src/core/generators/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,15 @@ export const generateAxiosTitle = (title: string) => {
};
};

export const generateAxiosHeader = (title: string) => {
const axiosTitle = generateAxiosTitle(title);
export const generateAxiosHeader = (titles: {
definition: string;
implementation: string;
implementationMock: string;
}) => {
return {
definition: `export interface ${axiosTitle.definition} {`,
implementation: `export const ${axiosTitle.implementation} = (axios: AxiosInstance): ${axiosTitle.definition} => ({\n`,
implementationMock: `export const ${axiosTitle.implementationMock} = (): ${axiosTitle.definition} => ({\n`,
definition: `export interface ${titles.definition} {`,
implementation: `export const ${titles.implementation} = (axios: AxiosInstance): ${titles.definition} => ({\n`,
implementationMock: `export const ${titles.implementationMock} = (): ${titles.definition} => ({\n`,
};
};

Expand Down
19 changes: 16 additions & 3 deletions src/core/generators/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ export const generateClientImports = (
export const generateClientHeader = (
outputClient: OutputClient = DEFAULT_CLIENT,
title: string,
customTitleFunc?: (title: string) => string,
): GeneratorClientExtra => {
const titles = generateClientTitle(outputClient, title, customTitleFunc);
return {
...GENERATOR_CLIENT[outputClient].header(title),
implementationMSW: `export const get${pascal(title)}MSW = () => [\n`,
...GENERATOR_CLIENT[outputClient].header(titles),
implementationMSW: `export const ${titles.implementationMSW} = () => [\n`,
};
};

Expand All @@ -94,8 +96,19 @@ export const generateClientFooter = (
export const generateClientTitle = (
outputClient: OutputClient = DEFAULT_CLIENT,
title: string,
customTitleFunc?: (title: string) => string,
) => {
return GENERATOR_CLIENT[outputClient].title(title);
if (customTitleFunc) {
const customTitle = customTitleFunc(title);
return {
...GENERATOR_CLIENT[outputClient].title(customTitleFunc(title)),
implementationMSW: `get${pascal(customTitle)}MSW`,
};
}
return {
...GENERATOR_CLIENT[outputClient].title(title),
implementationMSW: `get${pascal(title)}MSW`,
};
};

export const generateClient = (
Expand Down
6 changes: 5 additions & 1 deletion src/core/generators/react-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ export const generateReactQueryTitle = (title: string) => {
};
};

export const generateReactQueryHeader = (title: string) => {
export const generateReactQueryHeader = (title: {
definition: string;
implementation: string;
implementationMock: string;
}) => {
return {
definition: ``,
implementation: ``,
Expand Down
6 changes: 1 addition & 5 deletions src/core/writers/singleMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ export const writeSingleMode = ({
implementation,
implementationMocks,
implementationMSW,
} = generateTarget(
operations,
info,
isObject(output) ? output.client : undefined,
);
} = generateTarget(operations, info, isObject(output) ? output : undefined);

let data = getFilesHeader(info);

Expand Down
8 changes: 6 additions & 2 deletions src/core/writers/splitMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const writeSplitMode = ({
implementation,
implementationMocks,
implementationMSW,
} = generateTarget(operations, info, output.client);
} = generateTarget(operations, info, output);

const header = getFilesHeader(info);

Expand All @@ -47,7 +47,11 @@ export const writeSplitMode = ({
let mswData = header;

const defaultImports = generateClientImports(output.client);
const title = generateClientTitle(output.client, info.title);
const title = generateClientTitle(
output.client,
info.title,
output.override?.title,
);

definitionData += defaultImports.definition;

Expand Down
8 changes: 6 additions & 2 deletions src/core/writers/splitTagsMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const writeSplitTagsMode = ({
mkdirSync(dirname);
}

const target = generateTargetForTags(operations, info, output.client);
const target = generateTargetForTags(operations, info, output);

Object.entries(target).forEach(([tag, target]) => {
const {
Expand All @@ -50,7 +50,11 @@ export const writeSplitTagsMode = ({
let mswData = header;

const defaultImports = generateClientImports(output.client);
const title = generateClientTitle(output.client, pascal(tag));
const title = generateClientTitle(
output.client,
pascal(tag),
output.override?.title,
);

definitionData += defaultImports.definition;

Expand Down
2 changes: 1 addition & 1 deletion src/core/writers/tagsMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const writeTagsMode = ({
mkdirSync(dirname);
}

const target = generateTargetForTags(operations, info, output.client);
const target = generateTargetForTags(operations, info, output);

Object.entries(target).forEach(([tag, target]) => {
const {
Expand Down
12 changes: 8 additions & 4 deletions src/core/writers/target.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InfoObject } from 'openapi3-ts';
import { OutputClient } from '../../types';
import { OutputOptions } from '../../types';
import { GeneratorOperations } from '../../types/generator';
import { pascal } from '../../utils/case';
import { generalTypesFilter } from '../../utils/filters';
Expand All @@ -11,12 +11,16 @@ import {
export const generateTarget = (
operations: GeneratorOperations,
info: InfoObject,
outputClient?: OutputClient,
options?: OutputOptions,
) =>
Object.values(operations).reduce(
(acc, operation, index, arr) => {
if (!index) {
const header = generateClientHeader(outputClient, pascal(info.title));
const header = generateClientHeader(
options?.client,
pascal(info.title),
options?.override?.title,
);
acc.definition += header.definition;
acc.implementation += header.implementation;
acc.implementationMocks += header.implementationMock;
Expand All @@ -34,7 +38,7 @@ export const generateTarget = (
acc.implementationMSW += operation.implementationMSW;

if (index === arr.length - 1) {
const footer = generateClientFooter(outputClient);
const footer = generateClientFooter(options?.client);
acc.definition += footer.definition;
acc.implementation += footer.implementation;
acc.implementationMocks += footer.implementationMock;
Expand Down
17 changes: 10 additions & 7 deletions src/core/writers/targetTags.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InfoObject } from 'openapi3-ts';
import { OutputClient } from '../../types';
import { OutputOptions } from '../../types';
import {
GeneratorOperation,
GeneratorOperations,
Expand All @@ -20,13 +20,16 @@ const addDefaultTagIfEmpty = (operation: GeneratorOperation) => ({
const generateTargetTags = (
currentAcc: { [key: string]: GeneratorTarget },
operation: GeneratorOperation,
info: InfoObject,
outputClient?: OutputClient,
options?: OutputOptions,
) =>
operation.tags.reduce((acc, tag) => {
const currentOperation = acc[tag];
if (!currentOperation) {
const header = generateClientHeader(outputClient, pascal(tag));
const header = generateClientHeader(
options?.client,
pascal(tag),
options?.override?.title,
);

return {
...acc,
Expand Down Expand Up @@ -65,15 +68,15 @@ const generateTargetTags = (
export const generateTargetForTags = (
operations: GeneratorOperations,
info: InfoObject,
outputClient?: OutputClient,
options?: OutputOptions,
) =>
Object.values(operations)
.map(addDefaultTagIfEmpty)
.reduce((acc, operation, index, arr) => {
const targetTags = generateTargetTags(acc, operation, info, outputClient);
const targetTags = generateTargetTags(acc, operation, options);

if (index === arr.length - 1) {
const footer = generateClientFooter(outputClient);
const footer = generateClientFooter(options?.client);

return Object.entries(targetTags).reduce((acc, [tag, target]) => {
return {
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export interface ExternalConfigFile {
}

export type OverrideOutput = {
title?: (title: string) => string;
transformer?: string;
mutator?: string;
operations?: { [key: string]: OperationOptions };
Expand Down

0 comments on commit 7551f07

Please sign in to comment.