Skip to content

Commit

Permalink
chore: move all postprocess functions to one file
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanshatford committed Mar 21, 2024
1 parent a4c05ed commit e9f581d
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 105 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { Client } from './types/client';
import type { Config, UserConfig } from './types/config';
import { getOpenApiSpec } from './utils/getOpenApiSpec';
import { registerHandlebarTemplates } from './utils/handlebars';
import { postProcessClient } from './utils/postProcessClient';
import { postProcessClient } from './utils/postprocess';
import { writeClient } from './utils/write/client';

type Dependencies = Record<string, unknown>;
Expand Down
13 changes: 0 additions & 13 deletions src/utils/postProcessClient.ts

This file was deleted.

16 changes: 0 additions & 16 deletions src/utils/postProcessModel.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/utils/postProcessModelEnum.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/utils/postProcessModelEnums.ts

This file was deleted.

13 changes: 0 additions & 13 deletions src/utils/postProcessModelImports.ts

This file was deleted.

13 changes: 0 additions & 13 deletions src/utils/postProcessService.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/utils/postProcessServiceImports.ts

This file was deleted.

24 changes: 0 additions & 24 deletions src/utils/postProcessServiceOperations.ts

This file was deleted.

97 changes: 97 additions & 0 deletions src/utils/postprocess.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import type { Client, Enum, Model, Operation, Service } from '../types/client';
import { sort } from './sort';
import { unique } from './unique';

/**
* Post process client
* @param client Client object with all the models, services, etc.
*/
export function postProcessClient(client: Client): Client {
return {
...client,
models: client.models.map(model => postProcessModel(model)),
services: client.services.map(service => postProcessService(service)),
};
}

/**
* Post processes the model.
* This will clean up any double imports or enum values.
* @param model
*/
export function postProcessModel(model: Model): Model {
return {
...model,
imports: postProcessModelImports(model),
enums: postProcessModelEnums(model),
enum: postProcessModelEnum(model),
};
}

/**
* Set unique enum values for the model
* @param model
*/
export function postProcessModelEnum(model: Model): Enum[] {
return model.enum.filter((property, index, arr) => arr.findIndex(item => item.value === property.value) === index);
}

/**
* Set unique enum values for the model
* @param model The model that is post-processed
*/
export function postProcessModelEnums(model: Model): Model[] {
return model.enums.filter((property, index, arr) => arr.findIndex(item => item.name === property.name) === index);
}

/**
* Set unique imports, sorted by name
* @param model The model that is post-processed
*/
export function postProcessModelImports(model: Model): string[] {
return model.imports
.filter(unique)
.sort(sort)
.filter(name => model.name !== name);
}

export function postProcessService(service: Service): Service {
const clone = { ...service };
clone.operations = postProcessServiceOperations(clone);
clone.operations.forEach(operation => {
clone.imports.push(...operation.imports);
});
clone.imports = postProcessServiceImports(clone);
return clone;
}

/**
* Set unique imports, sorted by name
* @param service
*/
export function postProcessServiceImports(service: Service): string[] {
return service.imports.filter(unique).sort(sort);
}

export function postProcessServiceOperations(service: Service): Operation[] {
const names = new Map<string, number>();

return service.operations.map(operation => {
const clone = { ...operation };

// Parse the service parameters and results, very similar to how we parse
// properties of models. These methods will extend the type if needed.
clone.imports.push(...clone.parameters.flatMap(parameter => parameter.imports));
clone.imports.push(...clone.results.flatMap(result => result.imports));

// Check if the operation name is unique, if not then prefix this with a number
const name = clone.name;
const index = names.get(name) || 0;
if (index > 0) {
clone.name = `${name}${index}`;
}
names.set(name, index + 1);

return clone;
});
}

0 comments on commit e9f581d

Please sign in to comment.