Add missing Strapi scalar types to attributes definitions

This commit is contained in:
Convly 2022-06-03 14:47:00 +02:00
parent c2aba34289
commit 4329381635
7 changed files with 82 additions and 10 deletions

View File

@ -0,0 +1,9 @@
import { Attribute } from './base';
export interface DateTimeAttribute extends Attribute<'datetime'> {}
export type DateTimeValue = string;
export type GetDateTimeAttributeValue<T extends Attribute> = T extends DateTimeAttribute
? DateTimeValue
: never;

View File

@ -0,0 +1,9 @@
import { Attribute } from './base';
export interface DateAttribute extends Attribute<'date'> {}
export type DateValue = Date;
export type GetDateAttributeValue<T extends Attribute> = T extends DateAttribute
? DateValue
: never;

View File

@ -0,0 +1,10 @@
import { Attribute } from './base';
import { BaseStringAttribute } from './common';
export interface EmailAttribute extends BaseStringAttribute<'email'> {}
export type EmailValue = string;
export type GetEmailAttributeValue<T extends Attribute> = T extends EmailAttribute
? EmailValue
: never;

View File

@ -16,6 +16,11 @@ export * from './richtext';
export * from './string'; export * from './string';
export * from './text'; export * from './text';
export * from './uid'; export * from './uid';
export * from './email';
export * from './date';
export * from './date-time';
export * from './timestamp';
export * from './time';
export * from './common'; export * from './common';
export * from './utils'; export * from './utils';

View File

@ -0,0 +1,7 @@
import { Attribute } from './base';
export interface TimeAttribute extends Attribute<'time'> {}
export type TimeValue = string;
export type GetTimeAttributeValue<T extends Attibute> = T extends TimeAttribute ? TimeValue : never;

View File

@ -0,0 +1,9 @@
import { Attribute } from './base';
export interface TimestampAttribute extends Attribute<'timestamp'> {}
export type TimestampValue = string;
export type GetTimestampAttributeValue<T extends Attribute> = T extends TimestampAttribute
? TimestampValue
: never;

View File

@ -19,20 +19,28 @@ import {
GetTextAttributeValue, GetTextAttributeValue,
GetUIDAttributeValue, GetUIDAttributeValue,
} from '.'; } from '.';
import { GetDateAttributeValue } from './date';
import { GetDateTimeAttributeValue } from './date-time';
import { GetTimeAttributeValue } from './time';
import { GetTimestampAttributeValue } from './timestamp';
export type PickTypes<T extends AttributeType> = T; export type PickTypes<T extends AttributeType> = T;
export type GetAttributesKeysByType<T extends SchemaUID, U extends AttributeType, P = never> = KeysBy< export type GetAttributesKeysByType<
GetAttributes<T>, T extends SchemaUID,
Attribute<U> & NeverGuard<P, unknown> U extends AttributeType,
>; P = never
> = KeysBy<GetAttributes<T>, Attribute<U> & NeverGuard<P, unknown>>;
export type GetAttributesByType<T extends SchemaUID, U extends AttributeType, P = never> = PickBy< export type GetAttributesByType<T extends SchemaUID, U extends AttributeType, P = never> = PickBy<
GetAttributes<T>, GetAttributes<T>,
Attribute<U> & NeverGuard<P, unknown> Attribute<U> & NeverGuard<P, unknown>
>; >;
export type GetAttribute<T extends SchemaUID, U extends GetAttributesKey<T>> = Get<GetAttributes<T>, U>; export type GetAttribute<T extends SchemaUID, U extends GetAttributesKey<T>> = Get<
GetAttributes<T>,
U
>;
export type GetAttributes<T extends SchemaUID> = Get<Strapi.Schemas[T], 'attributes'>; export type GetAttributes<T extends SchemaUID> = Get<Strapi.Schemas[T], 'attributes'>;
@ -54,20 +62,35 @@ export type GetAttributeValue<T extends Attribute> =
| GetRichTextAttributeValue<T> | GetRichTextAttributeValue<T>
| GetStringAttributeValue<T> | GetStringAttributeValue<T>
| GetTextAttributeValue<T> | GetTextAttributeValue<T>
| GetUIDAttributeValue<T>; | GetUIDAttributeValue<T>
| GetMediaAttributeValue<T>
| GetDateAttributeValue<T>
| GetDateTimeAttributeValue<T>
| GetTimeAttributeValue<T>
| GetTimestampAttributeValue<T>;
export type GetAttributeValueByKey<T extends SchemaUID, U extends GetAttributesKey<T>> = GetAttribute<T, U export type GetAttributeValueByKey<
> extends infer P ? P extends Attribute ? GetAttributeValue<P> : never : never; T extends SchemaUID,
U extends GetAttributesKey<T>
> = GetAttribute<T, U> extends infer P
? P extends Attribute
? GetAttributeValue<P>
: never
: never;
export type GetAttributesValues<T extends SchemaUID> = { export type GetAttributesValues<T extends SchemaUID> = {
// Handle required attributes // Handle required attributes
[key in GetAttributesRequiredKeys<T>]-?: GetAttributeValueByKey<T, key>; [key in GetAttributesRequiredKeys<T>]-?: GetAttributeValueByKey<T, key>;
} & { } &
{
// Handle optional attributes // Handle optional attributes
[key in GetAttributesOptionalKeys<T>]?: GetAttributeValueByKey<T, key>; [key in GetAttributesOptionalKeys<T>]?: GetAttributeValueByKey<T, key>;
}; };
export type GetAttributesRequiredKeys<T extends SchemaUID> = KeysBy<GetAttributes<T>, { required: true }>; export type GetAttributesRequiredKeys<T extends SchemaUID> = KeysBy<
GetAttributes<T>,
{ required: true }
>;
export type GetAttributesOptionalKeys<T extends SchemaUID> = keyof Omit< export type GetAttributesOptionalKeys<T extends SchemaUID> = keyof Omit<
GetAttributes<T>, GetAttributes<T>,
GetAttributesRequiredKeys<T> GetAttributesRequiredKeys<T>