2021-09-11 18:12:23 +02:00
|
|
|
import { SimpleObj, SimpleSchema } from './simple-schema';
|
|
|
|
import { packedUserSchema } from '@/models/repositories/user';
|
|
|
|
import { packedNoteSchema } from '@/models/repositories/note';
|
|
|
|
import { packedUserListSchema } from '@/models/repositories/user-list';
|
|
|
|
import { packedAppSchema } from '@/models/repositories/app';
|
|
|
|
import { packedMessagingMessageSchema } from '@/models/repositories/messaging-message';
|
|
|
|
import { packedNotificationSchema } from '@/models/repositories/notification';
|
|
|
|
import { packedDriveFileSchema } from '@/models/repositories/drive-file';
|
|
|
|
import { packedDriveFolderSchema } from '@/models/repositories/drive-folder';
|
|
|
|
import { packedFollowingSchema } from '@/models/repositories/following';
|
|
|
|
import { packedMutingSchema } from '@/models/repositories/muting';
|
|
|
|
import { packedBlockingSchema } from '@/models/repositories/blocking';
|
|
|
|
import { packedNoteReactionSchema } from '@/models/repositories/note-reaction';
|
|
|
|
import { packedHashtagSchema } from '@/models/repositories/hashtag';
|
|
|
|
import { packedPageSchema } from '@/models/repositories/page';
|
|
|
|
import { packedUserGroupSchema } from '@/models/repositories/user-group';
|
|
|
|
import { packedNoteFavoriteSchema } from '@/models/repositories/note-favorite';
|
|
|
|
import { packedChannelSchema } from '@/models/repositories/channel';
|
|
|
|
import { packedAntennaSchema } from '@/models/repositories/antenna';
|
|
|
|
import { packedClipSchema } from '@/models/repositories/clip';
|
|
|
|
import { packedFederationInstanceSchema } from '@/models/repositories/federation-instance';
|
|
|
|
import { packedQueueCountSchema } from '@/models/repositories/queue';
|
|
|
|
import { packedGalleryPostSchema } from '@/models/repositories/gallery-post';
|
2021-09-22 15:35:55 +02:00
|
|
|
import { packedEmojiSchema } from '@/models/repositories/emoji';
|
2021-09-11 18:12:23 +02:00
|
|
|
|
|
|
|
export const refs = {
|
|
|
|
User: packedUserSchema,
|
|
|
|
UserList: packedUserListSchema,
|
|
|
|
UserGroup: packedUserGroupSchema,
|
|
|
|
App: packedAppSchema,
|
|
|
|
MessagingMessage: packedMessagingMessageSchema,
|
|
|
|
Note: packedNoteSchema,
|
|
|
|
NoteReaction: packedNoteReactionSchema,
|
|
|
|
NoteFavorite: packedNoteFavoriteSchema,
|
|
|
|
Notification: packedNotificationSchema,
|
|
|
|
DriveFile: packedDriveFileSchema,
|
|
|
|
DriveFolder: packedDriveFolderSchema,
|
|
|
|
Following: packedFollowingSchema,
|
|
|
|
Muting: packedMutingSchema,
|
|
|
|
Blocking: packedBlockingSchema,
|
|
|
|
Hashtag: packedHashtagSchema,
|
|
|
|
Page: packedPageSchema,
|
|
|
|
Channel: packedChannelSchema,
|
|
|
|
QueueCount: packedQueueCountSchema,
|
|
|
|
Antenna: packedAntennaSchema,
|
|
|
|
Clip: packedClipSchema,
|
|
|
|
FederationInstance: packedFederationInstanceSchema,
|
|
|
|
GalleryPost: packedGalleryPostSchema,
|
2021-09-22 15:35:55 +02:00
|
|
|
Emoji: packedEmojiSchema,
|
2021-09-11 18:12:23 +02:00
|
|
|
};
|
|
|
|
|
2021-09-22 15:35:55 +02:00
|
|
|
export type Packed<x extends keyof typeof refs> = ObjType<(typeof refs[x])['properties']>;
|
|
|
|
|
2021-09-11 18:12:23 +02:00
|
|
|
export interface Schema extends SimpleSchema {
|
2019-02-24 01:45:27 +01:00
|
|
|
items?: Schema;
|
|
|
|
properties?: Obj;
|
2021-09-11 18:12:23 +02:00
|
|
|
ref?: keyof typeof refs;
|
|
|
|
}
|
2019-02-24 01:45:27 +01:00
|
|
|
|
2019-04-23 15:35:26 +02:00
|
|
|
type NonUndefinedPropertyNames<T extends Obj> = {
|
|
|
|
[K in keyof T]: T[K]['optional'] extends true ? never : K
|
|
|
|
}[keyof T];
|
|
|
|
|
|
|
|
type UndefinedPropertyNames<T extends Obj> = {
|
|
|
|
[K in keyof T]: T[K]['optional'] extends true ? K : never
|
|
|
|
}[keyof T];
|
|
|
|
|
|
|
|
type OnlyRequired<T extends Obj> = Pick<T, NonUndefinedPropertyNames<T>>;
|
|
|
|
type OnlyOptional<T extends Obj> = Pick<T, UndefinedPropertyNames<T>>;
|
|
|
|
|
2021-09-11 18:12:23 +02:00
|
|
|
export interface Obj extends SimpleObj { [key: string]: Schema; }
|
2019-02-24 01:45:27 +01:00
|
|
|
|
2019-04-23 15:35:26 +02:00
|
|
|
export type ObjType<s extends Obj> =
|
|
|
|
{ [P in keyof OnlyOptional<s>]?: SchemaType<s[P]> } &
|
|
|
|
{ [P in keyof OnlyRequired<s>]: SchemaType<s[P]> };
|
2019-02-24 01:45:27 +01:00
|
|
|
|
|
|
|
// https://qiita.com/hrsh7th@github/items/84e8968c3601009cdcf2
|
|
|
|
type MyType<T extends Schema> = {
|
|
|
|
0: any;
|
|
|
|
1: SchemaType<T>;
|
|
|
|
}[T extends Schema ? 1 : 0];
|
|
|
|
|
2019-04-23 15:35:26 +02:00
|
|
|
type NullOrUndefined<p extends Schema, T> =
|
|
|
|
p['nullable'] extends true
|
|
|
|
? p['optional'] extends true
|
|
|
|
? (T | null | undefined)
|
|
|
|
: (T | null)
|
|
|
|
: p['optional'] extends true
|
|
|
|
? (T | undefined)
|
|
|
|
: T;
|
|
|
|
|
2019-02-24 01:45:27 +01:00
|
|
|
export type SchemaType<p extends Schema> =
|
2019-04-23 15:35:26 +02:00
|
|
|
p['type'] extends 'number' ? NullOrUndefined<p, number> :
|
|
|
|
p['type'] extends 'string' ? NullOrUndefined<p, string> :
|
|
|
|
p['type'] extends 'boolean' ? NullOrUndefined<p, boolean> :
|
|
|
|
p['type'] extends 'array' ? NullOrUndefined<p, MyType<NonNullable<p['items']>>[]> :
|
2021-09-11 18:12:23 +02:00
|
|
|
p['type'] extends 'object' ? (
|
|
|
|
p['ref'] extends keyof typeof refs
|
2021-09-22 15:35:55 +02:00
|
|
|
? NullOrUndefined<p, Packed<p['ref']>>
|
2021-09-11 18:12:23 +02:00
|
|
|
: NullOrUndefined<p, ObjType<NonNullable<p['properties']>>>
|
|
|
|
) :
|
2019-04-23 15:35:26 +02:00
|
|
|
p['type'] extends 'any' ? NullOrUndefined<p, any> :
|
2019-02-24 01:45:27 +01:00
|
|
|
any;
|