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

orval/core - generating factory method for schema interfaces #1334

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion packages/core/src/generators/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const generateImports = ({
} } from \'./${upath.join(path, camel(name))}\';`;
}

return `import ${!values ? 'type ' : ''}{ ${name}${
return `import { create${name}, ${name}${
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since create exists in the http status, I want to change the function name to the new prefix to avoid confusion.

Suggested change
return `import { create${name}, ${name}${
return `import { new${name}, ${name}${

alias ? ` as ${alias}` : ''
} } from \'./${camel(name)}\';`;
})
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/generators/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const generateInterface = ({
} else {
model += `export type ${name} = ${scalar.value};\n`;
}
model += `export function create${name}(): ${name} ${scalar.factoryMethodValue}\n`;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will add this in the model file here. It's a bit weird no?

Copy link
Contributor Author

@mironbalcerzak mironbalcerzak Apr 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah... was thinking whether it should be in separate file or not. I've decided to leave it with model. Anyone who has imported a model, can either instantiate it through "new", or right away call a factory method - which increases the chances that random dev will learn about it and use it.

People are lazy and do not read the code - especially the one that's generated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Factory methods are just noise for people who don't need them, so I'd like to be able to choose whether to generate them with a property.
For example, generateFactoryMethod: boolean.

And by making it a property, people who want to use it will be aware that it will be generated. Therefore, you will be able to notice it even if you separate the file like .factory.ts, right?


// Filter out imports that refer to the type defined in current file (OpenAPI recursive schema definitions)
const externalModulesImportsOnly = scalar.imports.filter(
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/getters/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const getArray = ({
}
return {
type: 'array',
factoryMethodValue: `[]`,
isEnum: false,
isRef: false,
value: `[${resolvedObjects.map((o) => o.value).join(', ')}]`,
Expand Down Expand Up @@ -71,6 +72,7 @@ export const getArray = ({
? `(${resolvedObject.value})[]`
: `${resolvedObject.value}[]`
}`,
factoryMethodValue: `[]`,
imports: resolvedObject.imports,
schemas: resolvedObject.schemas,
isEnum: false,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/getters/combine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export const combineSchemas = ({

return {
value: `typeof ${pascal(name)}[keyof typeof ${pascal(name)}] ${nullable}`,
factoryMethodValue: `{}`,
imports: [
{
name: pascal(name),
Expand Down Expand Up @@ -206,6 +207,7 @@ export const combineSchemas = ({

return {
value: value + nullable,
factoryMethodValue: `{}`,
imports: resolvedValue
? [...resolvedData.imports, ...resolvedValue.imports]
: resolvedData.imports,
Expand Down
11 changes: 11 additions & 0 deletions packages/core/src/getters/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const getObject = ({
const { name, specKey } = getRefInfo(item.$ref, context);
return {
value: name + nullable,
factoryMethodValue: `''`,
imports: [{ name, specKey }],
schemas: [],
isEnum: false,
Expand Down Expand Up @@ -115,12 +116,16 @@ export const getObject = ({
const isReadOnly = item.readOnly || (schema as SchemaObject).readOnly;
if (!index) {
acc.value += '{';
acc.factoryMethodValue += '{\n return {';
}

const doc = jsDoc(schema as SchemaObject, true);

acc.hasReadonlyProps ||= isReadOnly || false;
acc.imports.push(...resolvedValue.imports);
if (!isReadOnly || isRequired) {
acc.factoryMethodValue += `\n ${getKey(key)}: ${resolvedValue.factoryMethodValue},`;
}
acc.value += `\n ${doc ? `${doc} ` : ''}${
isReadOnly && !context.output.override.suppressReadonlyModifier
? 'readonly '
Expand All @@ -142,6 +147,7 @@ export const getObject = ({
}
} else {
acc.value += '\n}';
acc.factoryMethodValue += '\n };\n}';
}

acc.value += nullable;
Expand All @@ -153,6 +159,7 @@ export const getObject = ({
imports: [],
schemas: [],
value: '',
factoryMethodValue: '',
isEnum: false,
type: 'object' as SchemaType,
isRef: false,
Expand All @@ -168,6 +175,7 @@ export const getObject = ({
if (isBoolean(item.additionalProperties)) {
return {
value: `{ [key: string]: unknown }` + nullable,
factoryMethodValue: `{}`,
imports: [],
schemas: [],
isEnum: false,
Expand All @@ -183,6 +191,7 @@ export const getObject = ({
});
return {
value: `{[key: string]: ${resolvedValue.value}}` + nullable,
factoryMethodValue: `{}`,
imports: resolvedValue.imports ?? [],
schemas: resolvedValue.schemas ?? [],
isEnum: false,
Expand All @@ -196,6 +205,7 @@ export const getObject = ({
if (itemWithConst.const) {
return {
value: `'${itemWithConst.const}'` + nullable,
factoryMethodValue: `null`,
imports: [],
schemas: [],
isEnum: false,
Expand All @@ -209,6 +219,7 @@ export const getObject = ({
value:
(item.type === 'object' ? '{ [key: string]: unknown }' : 'unknown') +
nullable,
factoryMethodValue: `${item.type === 'object' ? '{}' : 'null'}`,
imports: [],
schemas: [],
isEnum: false,
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/getters/res-req-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export const getResReqTypes = (
return [
{
value: name,
factoryMethodValue: name,
imports: [{ name, specKey, schemaName }],
schemas: [],
type: 'unknown',
Expand Down Expand Up @@ -129,6 +130,7 @@ export const getResReqTypes = (
{
value: name,
imports: [{ name, specKey, schemaName }, ...additionalImports],
factoryMethodValue: name,
schemas: [],
type: 'unknown',
isEnum: false,
Expand Down Expand Up @@ -223,6 +225,7 @@ export const getResReqTypes = (
return [
{
value: defaultType,
factoryMethodValue: `''`,
imports: [],
schemas: [],
type: defaultType,
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/getters/scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export const getScalar = ({

return {
value: value + nullable,
factoryMethodValue: `0`,
isEnum,
type: 'number',
schemas: [],
Expand All @@ -79,6 +80,7 @@ export const getScalar = ({

return {
value: value + nullable,
factoryMethodValue: `false`,
type: 'boolean',
isEnum: false,
schemas: [],
Expand Down Expand Up @@ -133,6 +135,7 @@ export const getScalar = ({

return {
value: value + nullable,
factoryMethodValue: `''`,
isEnum,
type: 'string',
imports: [],
Expand All @@ -147,6 +150,7 @@ export const getScalar = ({
case 'null':
return {
value: 'null',
factoryMethodValue: `null`,
isEnum: false,
type: 'null',
imports: [],
Expand All @@ -167,6 +171,7 @@ export const getScalar = ({

return {
value: value + nullable,
factoryMethodValue: enumItems[0],
isEnum: true,
type: 'string',
imports: [],
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/resolvers/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const resolveObjectOriginal = ({
) {
return {
value: propName,
factoryMethodValue: `{}`,
imports: [{ name: propName }],
schemas: [
...resolvedValue.schemas,
Expand Down Expand Up @@ -57,6 +58,7 @@ const resolveObjectOriginal = ({

return {
value: propName,
factoryMethodValue: `${propName}[${resolvedValue.value.split(' | ')[0]}]`,
imports: [{ name: propName }],
schemas: [
...resolvedValue.schemas,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/resolvers/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const resolveValue = ({

return {
value: resolvedImport.name,
factoryMethodValue: `create${resolvedImport.name}()`,
imports: [
{
name: resolvedImport.name,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,7 @@ export const SchemaType = {

export type ScalarValue = {
value: string;
factoryMethodValue: string;
isEnum: boolean;
hasReadonlyProps: boolean;
type: SchemaType;
Expand Down
Loading