-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: отказ от enum в пользу простого union
BREAKING CHANGE
- Loading branch information
Showing
17 changed files
with
258 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,176 @@ | ||
import type { NullablePartial } from '../tools' | ||
import type { Entity } from './Entity' | ||
import type { EntityPatchRef, EntityRef } from './EntityRef' | ||
import type { MediaType } from './MediaType' | ||
import type { mediaType, MediaType } from './MediaType' | ||
|
||
export const simpleAttributeTypes = [ | ||
'string', | ||
'long', | ||
'time', | ||
'double', | ||
'boolean', | ||
'text', | ||
'link' | ||
] as const | ||
|
||
// TODO Надо отказываться от Enum (нельзя использовать просто строки там где | ||
// в типе указан Enum) #djbqpgnsda | ||
/** Типы атрибутов, значение которых является сущностью (ссылкой на сущность) */ | ||
export type SimpleAttributeType = typeof simpleAttributeTypes[number] | ||
|
||
export const embeddedEntityAttributeTypes = [ | ||
'organization', | ||
'counterparty', | ||
'employee', | ||
'contract', | ||
'product', | ||
'project', | ||
'store' | ||
] as const | ||
|
||
/** Типы атрибутов, значение которых является сущностью (ссылкой на сущность) */ | ||
export type EmbeddedEntityRefAttributeType = | ||
typeof embeddedEntityAttributeTypes[number] | ||
|
||
export enum AttributeType { | ||
/** Типы атрибутов */ | ||
export type AttributeType = | ||
// Base type | ||
String = 'string', | ||
Long = 'long', | ||
Time = 'time', | ||
File = 'file', | ||
Double = 'double', | ||
Boolean = 'boolean', | ||
Text = 'text', | ||
Link = 'link', | ||
| SimpleAttributeType | ||
|
||
// Embedded entity | ||
Organization = 'organization', | ||
Counterparty = 'counterparty', | ||
Employee = 'employee', | ||
Contract = 'contract', | ||
Product = 'product', | ||
Project = 'project', | ||
Store = 'store', | ||
| EmbeddedEntityRefAttributeType | ||
|
||
// File entity | ||
| 'file' | ||
|
||
// Custom entity | ||
CustomEntity = 'customentity' | ||
} | ||
| 'customentity' | ||
|
||
/** Типы атрибутов, значение которых является сущностью (ссылкой на сущность) */ | ||
export type EntityRefAttributeType = | ||
| AttributeType.Organization | ||
| AttributeType.Counterparty | ||
| AttributeType.Employee | ||
| AttributeType.Contract | ||
| AttributeType.Product | ||
| AttributeType.Project | ||
| AttributeType.Store | ||
| AttributeType.CustomEntity | ||
|
||
export type AttributeJsTypeMap = { | ||
// TODO Нужна отдельная мапа EntityRefAttributeType -> MetaType | ||
// Если перевести на union, то нужно ли? | ||
|
||
// file: { | ||
// filename: string | ||
// content: string | ||
// } | ||
|
||
export type AttributeValueByTypeMap = { | ||
// Base type | ||
[AttributeType.String]: string | ||
[AttributeType.Long]: number | ||
[AttributeType.Time]: string | ||
[AttributeType.File]: string | ||
[AttributeType.Double]: number | ||
[AttributeType.Boolean]: boolean | ||
[AttributeType.Text]: string | ||
[AttributeType.Link]: string | ||
string: { value: string } | ||
|
||
// Embedded entity | ||
[AttributeType.Organization]: EntityRef<'organization'> | ||
[AttributeType.Counterparty]: EntityRef<'counterparty'> | ||
[AttributeType.Employee]: EntityRef<'employee'> | ||
[AttributeType.Contract]: EntityRef<'contract'> | ||
[AttributeType.Product]: EntityRef<'product'> | ||
[AttributeType.Project]: EntityRef<'project'> | ||
[AttributeType.Store]: EntityRef<'store'> | ||
long: { value: number } | ||
|
||
// Custom entity | ||
[AttributeType.CustomEntity]: EntityRef<'customentity'> & { | ||
readonly name: string | ||
time: { value: string } | ||
|
||
file: { | ||
/** Наименование файла */ | ||
value: string | ||
|
||
/** Ссылка для скачивания файла */ | ||
download: { | ||
href: string | ||
mediaType: typeof mediaType.ApplicationOctetStream | ||
} | ||
} | ||
} | ||
|
||
export interface AttributeBase<T extends AttributeType = AttributeType> | ||
extends Entity<'attributemetadata'> { | ||
/** Наименование пользовательского поля */ | ||
readonly name: string | ||
double: { value: number } | ||
|
||
boolean: { value: boolean } | ||
|
||
text: { value: string } | ||
|
||
link: { value: string } | ||
|
||
// Embedded entity | ||
organization: { value: EntityRef<'organization'> } | ||
|
||
counterparty: { value: EntityRef<'counterparty'> } | ||
|
||
employee: { value: EntityRef<'employee'> } | ||
|
||
contract: { value: EntityRef<'contract'> } | ||
|
||
product: { value: EntityRef<'product'> } | ||
|
||
project: { value: EntityRef<'project'> } | ||
|
||
/** Тип значения пользовательского поля */ | ||
readonly type: T | ||
store: { value: EntityRef<'store'> } | ||
|
||
value: AttributeJsTypeMap[T] | ||
customentity: { | ||
value: EntityRef<'customentity'> & { | ||
readonly name: string | ||
} | ||
} | ||
} | ||
|
||
// prettier-ignore | ||
|
||
export type Attribute<T extends AttributeType = AttributeType> = | ||
T extends AttributeType.File | ||
? AttributeBase<T> & { | ||
readonly download: { | ||
readonly href: string | ||
readonly mediaType: MediaType | ||
Entity<'attributemetadata'> & | ||
( | ||
T extends SimpleAttributeType | EmbeddedEntityRefAttributeType | 'customentity' | ||
? { | ||
/** Наименование пользовательского поля */ | ||
readonly name: string | ||
|
||
/** Тип значения пользовательского поля */ | ||
readonly type: T | ||
|
||
readonly value: AttributeValueByTypeMap[T]['value'] | ||
} | ||
|
||
: T extends 'file' | ||
? { | ||
/** Наименование пользовательского поля */ | ||
readonly name: string | ||
|
||
/** Тип значения пользовательского поля */ | ||
readonly type: T | ||
|
||
readonly value: AttributeValueByTypeMap[T]['value'] | ||
|
||
readonly download: AttributeValueByTypeMap[T]['download'] | ||
} | ||
} | ||
: AttributeBase<T> | ||
|
||
: never | ||
) | ||
|
||
// TODO Попробовать оптимизировать см. #dhg06qfl | ||
|
||
// prettier-ignore | ||
|
||
export type AttributePatch<T extends AttributeType = AttributeType> = | ||
T extends AttributeType.File | ||
? EntityPatchRef<'attributemetadata'> & { | ||
file: { | ||
filename: string | ||
content: string | ||
} | null | ||
} | ||
EntityPatchRef<'attributemetadata'> & ( | ||
T extends 'customentity' | ||
? { | ||
/** Наименование пользовательского поля */ | ||
name?: string | ||
|
||
: T extends EntityRefAttributeType | ||
? EntityPatchRef<'attributemetadata'> & { | ||
value: EntityRef<T> | ||
} | ||
/** Тип значения пользовательского поля */ | ||
type?: T | ||
|
||
value: EntityRef<T> | { name: string } | null | ||
} | ||
|
||
: T extends 'file' | ||
? EntityPatchRef<'attributemetadata'> & { | ||
/** Наименование пользовательского поля */ | ||
name?: string | ||
|
||
: EntityPatchRef<'attributemetadata'> & NullablePartial<Pick<Attribute<T>, 'value'>> | ||
/** Тип значения пользовательского поля */ | ||
type?: T | ||
|
||
file: { | ||
filename: string | ||
content: string | ||
} | null | ||
} | ||
|
||
: EntityPatchRef<'attributemetadata'> & { | ||
/** Наименование пользовательского поля */ | ||
name?: string | ||
|
||
/** Тип значения пользовательского поля */ | ||
type?: T | ||
|
||
value: AttributeValueByTypeMap[T]['value'] | null | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.