2022-01-18 13:27:10 +00:00
|
|
|
|
import {
|
|
|
|
|
packedUserLiteSchema,
|
|
|
|
|
packedUserDetailedNotMeOnlySchema,
|
|
|
|
|
packedMeDetailedOnlySchema,
|
|
|
|
|
packedUserDetailedNotMeSchema,
|
|
|
|
|
packedMeDetailedSchema,
|
|
|
|
|
packedUserDetailedSchema,
|
|
|
|
|
packedUserSchema,
|
2022-02-27 02:07:39 +00:00
|
|
|
|
} from '@/models/schema/user.js';
|
|
|
|
|
import { packedNoteSchema } from '@/models/schema/note.js';
|
|
|
|
|
import { packedUserListSchema } from '@/models/schema/user-list.js';
|
|
|
|
|
import { packedAppSchema } from '@/models/schema/app.js';
|
|
|
|
|
import { packedNotificationSchema } from '@/models/schema/notification.js';
|
|
|
|
|
import { packedDriveFileSchema } from '@/models/schema/drive-file.js';
|
|
|
|
|
import { packedDriveFolderSchema } from '@/models/schema/drive-folder.js';
|
|
|
|
|
import { packedFollowingSchema } from '@/models/schema/following.js';
|
|
|
|
|
import { packedMutingSchema } from '@/models/schema/muting.js';
|
|
|
|
|
import { packedBlockingSchema } from '@/models/schema/blocking.js';
|
|
|
|
|
import { packedNoteReactionSchema } from '@/models/schema/note-reaction.js';
|
|
|
|
|
import { packedHashtagSchema } from '@/models/schema/hashtag.js';
|
|
|
|
|
import { packedPageSchema } from '@/models/schema/page.js';
|
|
|
|
|
import { packedNoteFavoriteSchema } from '@/models/schema/note-favorite.js';
|
|
|
|
|
import { packedChannelSchema } from '@/models/schema/channel.js';
|
|
|
|
|
import { packedAntennaSchema } from '@/models/schema/antenna.js';
|
|
|
|
|
import { packedClipSchema } from '@/models/schema/clip.js';
|
|
|
|
|
import { packedFederationInstanceSchema } from '@/models/schema/federation-instance.js';
|
|
|
|
|
import { packedQueueCountSchema } from '@/models/schema/queue.js';
|
|
|
|
|
import { packedGalleryPostSchema } from '@/models/schema/gallery-post.js';
|
|
|
|
|
import { packedEmojiSchema } from '@/models/schema/emoji.js';
|
2023-02-17 06:06:52 +00:00
|
|
|
|
import { packedFlashSchema } from '@/models/schema/flash.js';
|
2021-09-11 16:12:23 +00:00
|
|
|
|
|
|
|
|
|
export const refs = {
|
2022-01-18 13:27:10 +00:00
|
|
|
|
UserLite: packedUserLiteSchema,
|
|
|
|
|
UserDetailedNotMeOnly: packedUserDetailedNotMeOnlySchema,
|
|
|
|
|
MeDetailedOnly: packedMeDetailedOnlySchema,
|
|
|
|
|
UserDetailedNotMe: packedUserDetailedNotMeSchema,
|
|
|
|
|
MeDetailed: packedMeDetailedSchema,
|
|
|
|
|
UserDetailed: packedUserDetailedSchema,
|
2021-09-11 16:12:23 +00:00
|
|
|
|
User: packedUserSchema,
|
2022-01-18 13:27:10 +00:00
|
|
|
|
|
2021-09-11 16:12:23 +00:00
|
|
|
|
UserList: packedUserListSchema,
|
|
|
|
|
App: packedAppSchema,
|
|
|
|
|
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 13:35:55 +00:00
|
|
|
|
Emoji: packedEmojiSchema,
|
2023-02-17 06:06:52 +00:00
|
|
|
|
Flash: packedFlashSchema,
|
2021-09-11 16:12:23 +00:00
|
|
|
|
};
|
|
|
|
|
|
2022-02-19 14:21:28 +00:00
|
|
|
|
export type Packed<x extends keyof typeof refs> = SchemaType<typeof refs[x]>;
|
2022-01-18 13:27:10 +00:00
|
|
|
|
|
2022-02-19 05:05:32 +00:00
|
|
|
|
type TypeStringef = 'null' | 'boolean' | 'integer' | 'number' | 'string' | 'array' | 'object' | 'any';
|
2022-01-18 13:27:10 +00:00
|
|
|
|
type StringDefToType<T extends TypeStringef> =
|
2022-02-19 05:05:32 +00:00
|
|
|
|
T extends 'null' ? null :
|
2022-01-18 13:27:10 +00:00
|
|
|
|
T extends 'boolean' ? boolean :
|
2022-02-19 05:05:32 +00:00
|
|
|
|
T extends 'integer' ? number :
|
2022-01-18 13:27:10 +00:00
|
|
|
|
T extends 'number' ? number :
|
|
|
|
|
T extends 'string' ? string | Date :
|
|
|
|
|
T extends 'array' ? ReadonlyArray<any> :
|
|
|
|
|
T extends 'object' ? Record<string, any> :
|
|
|
|
|
any;
|
|
|
|
|
|
|
|
|
|
// https://swagger.io/specification/?sbsearch=optional#schema-object
|
|
|
|
|
type OfSchema = {
|
2022-02-19 05:05:32 +00:00
|
|
|
|
readonly anyOf?: ReadonlyArray<Schema>;
|
|
|
|
|
readonly oneOf?: ReadonlyArray<Schema>;
|
|
|
|
|
readonly allOf?: ReadonlyArray<Schema>;
|
2022-01-18 13:27:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-19 05:05:32 +00:00
|
|
|
|
export interface Schema extends OfSchema {
|
2022-01-18 13:27:10 +00:00
|
|
|
|
readonly type?: TypeStringef;
|
|
|
|
|
readonly nullable?: boolean;
|
|
|
|
|
readonly optional?: boolean;
|
2022-02-19 05:05:32 +00:00
|
|
|
|
readonly items?: Schema;
|
2022-01-18 13:27:10 +00:00
|
|
|
|
readonly properties?: Obj;
|
2022-04-24 02:43:15 +00:00
|
|
|
|
readonly required?: ReadonlyArray<Extract<keyof NonNullable<this['properties']>, string>>;
|
2022-01-18 13:27:10 +00:00
|
|
|
|
readonly description?: string;
|
|
|
|
|
readonly example?: any;
|
|
|
|
|
readonly format?: string;
|
|
|
|
|
readonly ref?: keyof typeof refs;
|
|
|
|
|
readonly enum?: ReadonlyArray<string>;
|
|
|
|
|
readonly default?: (this['type'] extends TypeStringef ? StringDefToType<this['type']> : any) | null;
|
|
|
|
|
readonly maxLength?: number;
|
|
|
|
|
readonly minLength?: number;
|
2022-04-27 01:49:00 +00:00
|
|
|
|
readonly maximum?: number;
|
|
|
|
|
readonly minimum?: number;
|
|
|
|
|
readonly pattern?: string;
|
2022-01-18 13:27:10 +00:00
|
|
|
|
}
|
2021-09-22 13:35:55 +00:00
|
|
|
|
|
2022-02-19 14:21:28 +00:00
|
|
|
|
type RequiredPropertyNames<s extends Obj> = {
|
|
|
|
|
[K in keyof s]:
|
|
|
|
|
// K is not optional
|
|
|
|
|
s[K]['optional'] extends false ? K :
|
|
|
|
|
// K has default value
|
2022-04-27 01:49:00 +00:00
|
|
|
|
s[K]['default'] extends null | string | number | boolean | Record<string, unknown> ? K :
|
|
|
|
|
never
|
2022-02-19 14:21:28 +00:00
|
|
|
|
}[keyof s];
|
2019-04-23 13:35:26 +00:00
|
|
|
|
|
2022-04-27 01:49:00 +00:00
|
|
|
|
export type Obj = Record<string, Schema>;
|
2019-02-24 00:45:27 +00:00
|
|
|
|
|
2022-04-27 01:49:00 +00:00
|
|
|
|
// https://github.com/misskey-dev/misskey/issues/8535
|
|
|
|
|
// To avoid excessive stack depth error,
|
|
|
|
|
// deceive TypeScript with UnionToIntersection (or more precisely, `infer` expression within it).
|
2022-02-19 14:21:28 +00:00
|
|
|
|
export type ObjType<s extends Obj, RequiredProps extends keyof s> =
|
2022-04-27 01:49:00 +00:00
|
|
|
|
UnionToIntersection<
|
|
|
|
|
{ -readonly [R in RequiredPropertyNames<s>]-?: SchemaType<s[R]> } &
|
|
|
|
|
{ -readonly [R in RequiredProps]-?: SchemaType<s[R]> } &
|
|
|
|
|
{ -readonly [P in keyof s]?: SchemaType<s[P]> }
|
|
|
|
|
>;
|
2019-02-24 00:45:27 +00:00
|
|
|
|
|
2022-02-19 05:05:32 +00:00
|
|
|
|
type NullOrUndefined<p extends Schema, T> =
|
2022-04-27 01:49:00 +00:00
|
|
|
|
| (p['nullable'] extends true ? null : never)
|
|
|
|
|
| (p['optional'] extends true ? undefined : never)
|
|
|
|
|
| T;
|
2019-04-23 13:35:26 +00:00
|
|
|
|
|
2022-02-19 14:21:28 +00:00
|
|
|
|
// https://stackoverflow.com/questions/54938141/typescript-convert-union-to-intersection
|
|
|
|
|
// Get intersection from union
|
2022-01-18 13:27:10 +00:00
|
|
|
|
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
|
2023-02-01 11:04:01 +00:00
|
|
|
|
type PartialIntersection<T> = Partial<UnionToIntersection<T>>;
|
2022-01-18 13:27:10 +00:00
|
|
|
|
|
|
|
|
|
// https://github.com/misskey-dev/misskey/pull/8144#discussion_r785287552
|
2022-02-19 14:21:28 +00:00
|
|
|
|
// To get union, we use `Foo extends any ? Hoge<Foo> : never`
|
2022-02-19 05:05:32 +00:00
|
|
|
|
type UnionSchemaType<a extends readonly any[], X extends Schema = a[number]> = X extends any ? SchemaType<X> : never;
|
2023-02-01 11:04:01 +00:00
|
|
|
|
type UnionObjectSchemaType<a extends readonly any[], X extends Schema = a[number]> = X extends any ? ObjectSchemaType<X> : never;
|
|
|
|
|
type ArrayUnion<T> = T extends any ? Array<T> : never;
|
|
|
|
|
|
|
|
|
|
type ObjectSchemaTypeDef<p extends Schema> =
|
|
|
|
|
p['ref'] extends keyof typeof refs ? Packed<p['ref']> :
|
|
|
|
|
p['properties'] extends NonNullable<Obj> ?
|
|
|
|
|
p['anyOf'] extends ReadonlyArray<Schema> ?
|
|
|
|
|
ObjType<p['properties'], NonNullable<p['required']>[number]> & UnionObjectSchemaType<p['anyOf']> & PartialIntersection<UnionObjectSchemaType<p['anyOf']>>
|
|
|
|
|
:
|
|
|
|
|
ObjType<p['properties'], NonNullable<p['required']>[number]>
|
|
|
|
|
:
|
|
|
|
|
p['anyOf'] extends ReadonlyArray<Schema> ? UnionObjectSchemaType<p['anyOf']> & PartialIntersection<UnionObjectSchemaType<p['anyOf']>> :
|
|
|
|
|
p['allOf'] extends ReadonlyArray<Schema> ? UnionToIntersection<UnionSchemaType<p['allOf']>> :
|
|
|
|
|
any
|
|
|
|
|
|
|
|
|
|
type ObjectSchemaType<p extends Schema> = NullOrUndefined<p, ObjectSchemaTypeDef<p>>;
|
2022-01-18 13:27:10 +00:00
|
|
|
|
|
2022-02-19 05:05:32 +00:00
|
|
|
|
export type SchemaTypeDef<p extends Schema> =
|
|
|
|
|
p['type'] extends 'null' ? null :
|
|
|
|
|
p['type'] extends 'integer' ? number :
|
2022-01-18 13:27:10 +00:00
|
|
|
|
p['type'] extends 'number' ? number :
|
|
|
|
|
p['type'] extends 'string' ? (
|
|
|
|
|
p['enum'] extends readonly string[] ?
|
2022-04-27 01:49:00 +00:00
|
|
|
|
p['enum'][number] :
|
|
|
|
|
p['format'] extends 'date-time' ? string : // Dateにする??
|
|
|
|
|
string
|
2022-01-18 13:27:10 +00:00
|
|
|
|
) :
|
|
|
|
|
p['type'] extends 'boolean' ? boolean :
|
2023-02-01 11:04:01 +00:00
|
|
|
|
p['type'] extends 'object' ? ObjectSchemaTypeDef<p> :
|
2022-01-18 13:27:10 +00:00
|
|
|
|
p['type'] extends 'array' ? (
|
|
|
|
|
p['items'] extends OfSchema ? (
|
2022-02-19 05:05:32 +00:00
|
|
|
|
p['items']['anyOf'] extends ReadonlyArray<Schema> ? UnionSchemaType<NonNullable<p['items']['anyOf']>>[] :
|
|
|
|
|
p['items']['oneOf'] extends ReadonlyArray<Schema> ? ArrayUnion<UnionSchemaType<NonNullable<p['items']['oneOf']>>> :
|
|
|
|
|
p['items']['allOf'] extends ReadonlyArray<Schema> ? UnionToIntersection<UnionSchemaType<NonNullable<p['items']['allOf']>>>[] :
|
2022-01-18 13:27:10 +00:00
|
|
|
|
never
|
|
|
|
|
) :
|
2022-02-19 05:05:32 +00:00
|
|
|
|
p['items'] extends NonNullable<Schema> ? SchemaTypeDef<p['items']>[] :
|
2022-01-18 13:27:10 +00:00
|
|
|
|
any[]
|
2021-09-11 16:12:23 +00:00
|
|
|
|
) :
|
2023-02-01 11:04:01 +00:00
|
|
|
|
p['anyOf'] extends ReadonlyArray<Schema> ? UnionSchemaType<p['anyOf']> & PartialIntersection<UnionSchemaType<p['anyOf']>> :
|
2022-02-19 05:05:32 +00:00
|
|
|
|
p['oneOf'] extends ReadonlyArray<Schema> ? UnionSchemaType<p['oneOf']> :
|
2019-02-24 00:45:27 +00:00
|
|
|
|
any;
|
2022-01-18 13:27:10 +00:00
|
|
|
|
|
2022-02-19 05:05:32 +00:00
|
|
|
|
export type SchemaType<p extends Schema> = NullOrUndefined<p, SchemaTypeDef<p>>;
|