Skip to content

Commit

Permalink
packages:js:update-types
Browse files Browse the repository at this point in the history
  • Loading branch information
Voldemat committed Sep 9, 2023
1 parent 70d4206 commit c9a83c1
Show file tree
Hide file tree
Showing 8 changed files with 509 additions and 120 deletions.
76 changes: 76 additions & 0 deletions packages/js/src/cli/__tests__/snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ spec:
type: AnyDocument
required: true
errorMessage: null
city:
type: citiesEnum
required: true
errorMessage: null
loginAuth:
type: loginAuthUnion
required: true
errorMessage: null
`
export const formExpectedCode = `export const RegistrationForm = {
login: {
Expand Down Expand Up @@ -162,6 +170,45 @@ export const formExpectedCode = `export const RegistrationForm = {
},
allowedMimeTypes: null
}
},
city: {
type: "citiesEnum",
required: true,
errorMessage: null,
placeholder: null,
helperMessage: null,
typeSpec: {
type: "enum",
itemType: "city",
items: ["Moscow","Tashkent"],
itemTypeSpec: {
type: "string",
regex: new RegExp(".{1,30}")
}
}
},
loginAuth: {
type: "loginAuthUnion",
required: true,
errorMessage: null,
placeholder: null,
helperMessage: null,
typeSpec: {
type: "union",
types: ["login","password"],
typeSpecs: {
login: {
type: "string",
regex: new RegExp(` +
rmTrailingSlashes('"^[\\w_]{4,100}$"') + `)
},
password: {
type: "int",
max: 100,
min: 1
}
}
}
}
};
`
Expand Down Expand Up @@ -250,4 +297,33 @@ spec:
maxSize:
unit: kb
value: 500
...
---
type: type
metadata:
name: city
spec:
type: string
regex: '.{1,30}'
...
---
type: type
metadata:
name: citiesEnum
spec:
type: enum
itemType: city
items:
- Moscow
- Tashkent
...
---
type: type
metadata:
name: loginAuthUnion
spec:
type: union
types:
- login
- password
`
4 changes: 4 additions & 0 deletions packages/js/src/generators/js/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,8 @@ export class BaseAstFactory {
elements
}
}

buildPropertyWithLiteral (key: string, value: any): Record<string, any> {
return this.buildProperty(key, this.buildLiteral(value))
}
}
104 changes: 75 additions & 29 deletions packages/js/src/generators/js/formAstFactory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* eslint-disable max-lines */
import type { Form, FormFieldSpec } from '../../spec/types/forms'
import type {
DateTimeTypeSpec,
DateTypeSpec,
EnumTypeSpec,
FileSize,
FileTypeSpec,
FloatTypeSpec,
Expand All @@ -10,7 +12,8 @@ import type {
IntTypeSpec,
StringTypeSpec,
TimeTypeSpec,
TypeSpec
TypeSpec,
UnionTypeSpec
} from '../../spec/types/type'
import { type BaseAstFactory } from './base'

Expand Down Expand Up @@ -106,6 +109,14 @@ export class FormAstFactory {
properties.push(...this.buildFileTypeProperties(value))
break
}
case 'enum': {
properties.push(...this.buildEnumTypeProperties(value))
break
}
case 'union': {
properties.push(...this.buildUnionTypeProperties(value))
break
}
default: {
throw new Error(`Unhandled type: ${JSON.stringify(value)}`)
}
Expand All @@ -130,11 +141,11 @@ export class FormAstFactory {
value: IntTypeSpec | FloatTypeSpec
): Array<Record<string, any>> {
return [
this.baseAstFactory.buildProperty(
'max', this.baseAstFactory.buildLiteral(value.max)
this.baseAstFactory.buildPropertyWithLiteral(
'max', value.max
),
this.baseAstFactory.buildProperty(
'min', this.baseAstFactory.buildLiteral(value.min)
this.baseAstFactory.buildPropertyWithLiteral(
'min', value.min
)
]
}
Expand All @@ -143,8 +154,8 @@ export class FormAstFactory {
value: DateTypeSpec | TimeTypeSpec | DateTimeTypeSpec
): Array<Record<string, any>> {
return [
this.baseAstFactory.buildProperty(
'allowOnly', this.baseAstFactory.buildLiteral(value.allowOnly)
this.baseAstFactory.buildPropertyWithLiteral(
'allowOnly', value.allowOnly
)
]
}
Expand All @@ -160,24 +171,24 @@ export class FormAstFactory {
this.baseAstFactory.buildProperty(
'maxSize', this.buildFileSizeAstValue(value.maxSize)
),
this.baseAstFactory.buildProperty(
'minWidth', this.baseAstFactory.buildLiteral(value.minWidth)
this.baseAstFactory.buildPropertyWithLiteral(
'minWidth', value.minWidth
),
this.baseAstFactory.buildProperty(
'minHeight', this.baseAstFactory.buildLiteral(value.minHeight)
this.baseAstFactory.buildPropertyWithLiteral(
'minHeight', value.minHeight
),
this.baseAstFactory.buildProperty(
'maxWidth', this.baseAstFactory.buildLiteral(value.minWidth)
this.baseAstFactory.buildPropertyWithLiteral(
'maxWidth', value.minWidth
),
this.baseAstFactory.buildProperty(
'maxHeight', this.baseAstFactory.buildLiteral(value.maxHeight)
this.baseAstFactory.buildPropertyWithLiteral(
'maxHeight', value.maxHeight
),
this.baseAstFactory.buildProperty(
'aspectRatio', this.baseAspectRationAstValue(value.aspectRatio)
),
this.baseAstFactory.buildProperty(
this.baseAstFactory.buildPropertyWithLiteral(
'allowedTypes',
this.baseAstFactory.buildLiteral(value.allowedTypes)
value.allowedTypes
)
]
}
Expand All @@ -186,11 +197,11 @@ export class FormAstFactory {
buildFileSizeAstValue (size: FileSize | null): Record<string, any> {
if (size === null) return this.baseAstFactory.buildLiteral(size)
return this.baseAstFactory.buildObjectExpression([
this.baseAstFactory.buildProperty(
'unit', this.baseAstFactory.buildLiteral(size.unit)
this.baseAstFactory.buildPropertyWithLiteral(
'unit', size.unit
),
this.baseAstFactory.buildProperty(
'value', this.baseAstFactory.buildLiteral(size.value)
this.baseAstFactory.buildPropertyWithLiteral(
'value', size.value
)
])
}
Expand All @@ -202,16 +213,15 @@ export class FormAstFactory {
return this.baseAstFactory.buildLiteral(aspectRatio)
}
return this.baseAstFactory.buildObjectExpression([
this.baseAstFactory.buildProperty(
'width', this.baseAstFactory.buildLiteral(aspectRatio.width)
this.baseAstFactory.buildPropertyWithLiteral(
'width', aspectRatio.width
),
this.baseAstFactory.buildProperty(
'height', this.baseAstFactory.buildLiteral(aspectRatio.height)
this.baseAstFactory.buildPropertyWithLiteral(
'height', aspectRatio.height
)
])
}

/* eslint-disable max-lines-per-function */
buildFileTypeProperties (value: FileTypeSpec): Array<Record<string, any>> {
return [
this.baseAstFactory.buildProperty(
Expand All @@ -220,11 +230,47 @@ export class FormAstFactory {
this.baseAstFactory.buildProperty(
'maxSize', this.buildFileSizeAstValue(value.maxSize)
),
this.baseAstFactory.buildPropertyWithLiteral(
'allowedMimeTypes', value.allowedMimeTypes
)
]
}

buildEnumTypeProperties (
value: EnumTypeSpec
): Array<Record<string, any>> {
return [
this.baseAstFactory.buildPropertyWithLiteral(
'itemType', value.itemType
),
this.baseAstFactory.buildPropertyWithLiteral(
'items', value.items
),
this.baseAstFactory.buildProperty(
'allowedMimeTypes',
this.baseAstFactory.buildLiteral(value.allowedMimeTypes)
'itemTypeSpec', this.buildTypeSpecValue(value.itemTypeSpec)
)
]
}

buildUnionTypeProperties (
value: UnionTypeSpec
): Array<Record<string, any>> {
return [
this.baseAstFactory.buildPropertyWithLiteral(
'types', value.types
),
this.baseAstFactory.buildProperty(
'typeSpecs',
this.baseAstFactory.buildObjectExpression(
Object.entries(value.typeSpecs)
.map(([name, tSpec]) => (
this.baseAstFactory.buildProperty(
name,
this.buildTypeSpecValue(tSpec)
)
))
)
)
]
}
/* eslint-enable max-lines-per-function */
}
17 changes: 11 additions & 6 deletions packages/js/src/spec/types/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,34 @@ export interface ImageTypeSpec {
minHeight: number | null
aspectRatio: ImageAspectRatio | null
}
export type ScalarTypeSpec = (
export type BaseScalarTypeSpec = (
StringTypeSpec |
IntTypeSpec |
FloatTypeSpec |
DateTypeSpec |
TimeTypeSpec |
DateTimeTypeSpec |
DateTimeTypeSpec
)
export type ScalarTypeSpec = (
BaseScalarTypeSpec |
FileTypeSpec |
ImageTypeSpec
)
export interface EnumTypeSpec {
type: 'enum'
itemType: string
itemTypeSpec: ScalarTypeSpec
itemTypeSpec: BaseScalarTypeSpec
items: string[]
}
export type ScalarTypeSpecWithEnum = ScalarTypeSpec | EnumTypeSpec
export type BaseScalarTypeSpecWithEnum = BaseScalarTypeSpec | EnumTypeSpec
export interface UnionTypeSpec {
type: 'union'
types: string[]
typeSpecs: Record<string, ScalarTypeSpecWithEnum>
typeSpecs: Record<string, BaseScalarTypeSpecWithEnum>
}
export type TypeSpec = (
ScalarTypeSpecWithEnum |
ScalarTypeSpec |
EnumTypeSpec |
UnionTypeSpec
)
export type Type = BaseTechSpec<'type', TypeMetadata, TypeSpec>
Loading

0 comments on commit c9a83c1

Please sign in to comment.