Skip to content

Commit

Permalink
orval/core - factory method generated
Browse files Browse the repository at this point in the history
  • Loading branch information
mironbalcerzak committed Apr 27, 2024
1 parent fb32cf5 commit 581b773
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 1 deletion.
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}${
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`;

// 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 All @@ -66,6 +67,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 @@ -179,6 +179,7 @@ export const combineSchemas = ({
value:
`typeof ${pascal(name)}[keyof typeof ${pascal(name)}] ${nullable};` +
newEnum,
factoryMethodValue: `{}`,
imports: resolvedData.imports.map<GeneratorImport>((toImport) => ({
...toImport,
values: true,
Expand All @@ -195,6 +196,7 @@ export const combineSchemas = ({

return {
value: value + nullable,
factoryMethodValue: `{}`,
imports: resolvedValue
? [...resolvedData.imports, ...resolvedValue.imports]
: resolvedData.imports,
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/getters/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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 @@ -114,12 +115,14 @@ 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);
acc.factoryMethodValue += `\n ${getKey(key)}: ${resolvedValue.factoryMethodValue},`;
acc.value += `\n ${doc ? `${doc} ` : ''}${
isReadOnly ? 'readonly ' : ''
}${getKey(key)}${isRequired ? '' : '?'}: ${resolvedValue.value};`;
Expand All @@ -139,6 +142,7 @@ export const getObject = ({
}
} else {
acc.value += '\n}';
acc.factoryMethodValue += '\n };\n}';
}

acc.value += nullable;
Expand All @@ -150,6 +154,7 @@ export const getObject = ({
imports: [],
schemas: [],
value: '',
factoryMethodValue: '',
isEnum: false,
type: 'object' as SchemaType,
isRef: false,
Expand All @@ -165,6 +170,7 @@ export const getObject = ({
if (isBoolean(item.additionalProperties)) {
return {
value: `{ [key: string]: any }` + nullable,
factoryMethodValue: `{}`,
imports: [],
schemas: [],
isEnum: false,
Expand All @@ -180,6 +186,7 @@ export const getObject = ({
});
return {
value: `{[key: string]: ${resolvedValue.value}}` + nullable,
factoryMethodValue: `{}`,
imports: resolvedValue.imports ?? [],
schemas: resolvedValue.schemas ?? [],
isEnum: false,
Expand All @@ -193,6 +200,7 @@ export const getObject = ({
if (itemWithConst.const) {
return {
value: `'${itemWithConst.const}'` + nullable,
factoryMethodValue: `null`,
imports: [],
schemas: [],
isEnum: false,
Expand All @@ -206,6 +214,7 @@ export const getObject = ({
value:
(item.type === 'object' ? '{ [key: string]: any }' : '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 @@ -115,6 +116,7 @@ export const getResReqTypes = (
return [
{
value: name,
factoryMethodValue: name,
imports: [{ name, specKey, schemaName }],
schemas: [],
type: 'unknown',
Expand Down Expand Up @@ -202,6 +204,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 @@ -48,6 +48,7 @@ export const getScalar = ({

return {
value: value + nullable,
factoryMethodValue: `0`,
isEnum,
type: 'number',
schemas: [],
Expand All @@ -62,6 +63,7 @@ export const getScalar = ({
case 'boolean':
return {
value: 'boolean' + nullable,
factoryMethodValue: `false`,
type: 'boolean',
isEnum: false,
schemas: [],
Expand Down Expand Up @@ -111,6 +113,7 @@ export const getScalar = ({

return {
value: value + nullable,
factoryMethodValue: `''`,
isEnum,
type: 'string',
imports: [],
Expand All @@ -125,6 +128,7 @@ export const getScalar = ({
case 'null':
return {
value: 'null',
factoryMethodValue: `null`,
isEnum: false,
type: 'null',
imports: [],
Expand All @@ -145,6 +149,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 @@ -879,6 +879,7 @@ export const SchemaType = {

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

0 comments on commit 581b773

Please sign in to comment.