monkeeShark/packages/misskey-js/src/schemas.ts

177 lines
6.1 KiB
TypeScript
Raw Normal View History

2023-05-02 17:31:17 +00:00
import { IdSchema } from './schemas/id.js';
import {
packedUserLiteSchema,
packedUserDetailedNotMeOnlySchema,
packedMeDetailedOnlySchema,
packedUserDetailedNotMeSchema,
packedMeDetailedSchema,
packedUserDetailedSchema,
packedUserSchema,
} from './schemas/user.js';
2023-05-10 19:18:48 +00:00
import {
2023-05-29 17:03:20 +00:00
notificationTypeSchema,
2023-05-10 19:18:48 +00:00
packedNotificationSchema,
packedNotificationStrictSchema,
} from './schemas/notification.js';
2023-05-02 17:31:17 +00:00
import { packedNoteSchema } from './schemas/note.js';
import { packedUserListSchema } from './schemas/user-list.js';
import { packedAppSchema } from './schemas/app.js';
import { packedDriveFileSchema } from './schemas/drive-file.js';
import { packedDriveFolderSchema } from './schemas/drive-folder.js';
import { packedFollowingSchema } from './schemas/following.js';
import { packedMutingSchema } from './schemas/muting.js';
import { packedRenoteMutingSchema } from './schemas/renote-muting.js';
import { packedBlockingSchema } from './schemas/blocking.js';
import { packedNoteReactionSchema } from './schemas/note-reaction.js';
import { packedHashtagSchema } from './schemas/hashtag.js';
import { packedPageSchema } from './schemas/page.js';
import { packedNoteFavoriteSchema } from './schemas/note-favorite.js';
import { packedChannelSchema } from './schemas/channel.js';
import { packedAntennaSchema } from './schemas/antenna.js';
import { packedClipSchema } from './schemas/clip.js';
import { packedFederationInstanceSchema } from './schemas/federation-instance.js';
import { packedQueueCountSchema } from './schemas/queue.js';
import { packedGalleryPostSchema } from './schemas/gallery-post.js';
import { packedEmojiDetailedSchema, packedEmojiSimpleSchema } from './schemas/emoji.js';
import { packedFlashSchema } from './schemas/flash.js';
2023-05-15 08:42:47 +00:00
import { packedAdSchema } from './schemas/ad.js';
import { packedAnnouncementSchema } from './schemas/announcement.js';
2023-05-27 16:06:42 +00:00
import { packedRelaySchema } from './schemas/relay.js';
2023-05-29 13:28:31 +00:00
import { packedAbuseUserReportSchema } from './schemas/abuse-user-report.js';
2023-05-27 19:17:29 +00:00
import {
packedRoleSchema,
packedRoleAssignSchema,
packedRolePolicySchema,
packedRoleCondFormulaSchema,
} from './schemas/role.js';
2023-05-29 16:01:04 +00:00
import {
InstanceMetaSharedSchema,
InstanceMetaAdminSchema,
} from './schemas/instance-meta.js';
2023-05-29 16:26:16 +00:00
import {
ServerInfoSchema,
ServerInfoAdminSchema,
} from './schemas/server-info.js';
2023-05-29 16:47:49 +00:00
import {
SignInSchema,
} from './schemas/sign-in.js';
2023-05-29 16:39:03 +00:00
import { packedModerationLogSchema } from './schemas/moderation-log.js';
2023-06-04 16:41:45 +00:00
import { packedAuthSessionSchema } from './schemas/auth-session.js';
2023-05-22 05:20:19 +00:00
import { Error, ApiError } from './schemas/error.js';
2023-06-05 05:46:35 +00:00
import type { JSONSchema7, JSONSchema7Definition, GetDef, GetRefs, GetKeys, UnionToArray, Serialized, Projected } from 'schema-type';
2023-05-02 17:31:17 +00:00
export const refs = {
2023-05-10 17:48:21 +00:00
Id: IdSchema,
2023-05-02 17:31:17 +00:00
UserLite: packedUserLiteSchema,
UserDetailedNotMeOnly: packedUserDetailedNotMeOnlySchema,
MeDetailedOnly: packedMeDetailedOnlySchema,
UserDetailedNotMe: packedUserDetailedNotMeSchema,
MeDetailed: packedMeDetailedSchema,
UserDetailed: packedUserDetailedSchema,
User: packedUserSchema,
UserList: packedUserListSchema,
App: packedAppSchema,
Note: packedNoteSchema,
NoteReaction: packedNoteReactionSchema,
NoteFavorite: packedNoteFavoriteSchema,
2023-05-29 17:03:20 +00:00
NotificationType: notificationTypeSchema,
2023-05-02 17:31:17 +00:00
Notification: packedNotificationSchema,
2023-05-10 19:18:48 +00:00
NotificationStrict: packedNotificationStrictSchema,
2023-05-02 17:31:17 +00:00
DriveFile: packedDriveFileSchema,
DriveFolder: packedDriveFolderSchema,
Following: packedFollowingSchema,
Muting: packedMutingSchema,
RenoteMuting: packedRenoteMutingSchema,
Blocking: packedBlockingSchema,
Hashtag: packedHashtagSchema,
Page: packedPageSchema,
Channel: packedChannelSchema,
QueueCount: packedQueueCountSchema,
Antenna: packedAntennaSchema,
Clip: packedClipSchema,
FederationInstance: packedFederationInstanceSchema,
GalleryPost: packedGalleryPostSchema,
EmojiSimple: packedEmojiSimpleSchema,
EmojiDetailed: packedEmojiDetailedSchema,
Flash: packedFlashSchema,
2023-05-15 08:42:47 +00:00
Ad: packedAdSchema,
Announcement: packedAnnouncementSchema,
2023-05-27 16:06:42 +00:00
Relay: packedRelaySchema,
2023-05-27 19:17:29 +00:00
Role: packedRoleSchema,
2023-05-27 16:42:44 +00:00
RoleAssign: packedRoleAssignSchema,
2023-05-27 19:17:29 +00:00
RolePolicy: packedRolePolicySchema,
RoleCondFormula: packedRoleCondFormulaSchema,
2023-05-29 13:28:31 +00:00
AbuseUserReport: packedAbuseUserReportSchema,
2023-05-29 16:01:04 +00:00
InstanceMetaShared: InstanceMetaSharedSchema,
InstanceMetaAdmin: InstanceMetaAdminSchema,
2023-05-29 16:26:16 +00:00
ServerInfo: ServerInfoSchema,
ServerInfoAdmin: ServerInfoAdminSchema,
2023-05-29 16:39:03 +00:00
ModerationLog: packedModerationLogSchema,
2023-05-29 16:47:49 +00:00
SignIn: SignInSchema,
2023-06-04 16:41:45 +00:00
AuthSession: packedAuthSessionSchema,
2023-05-27 16:42:44 +00:00
2023-05-22 05:20:19 +00:00
Error: Error,
ApiError: ApiError,
2023-05-10 16:55:36 +00:00
} as const satisfies { [x: string]: JSONSchema7Definition };
2023-05-02 17:31:17 +00:00
2023-05-21 12:54:39 +00:00
export type References = GetRefs<typeof refs>;
2023-05-02 17:31:17 +00:00
export type Packed<x extends GetKeys<References, 'https://misskey-hub.net/api/schemas/'>> = Serialized<GetDef<References, x, false, 'https://misskey-hub.net/api/schemas/'>>;
2023-05-04 10:56:24 +00:00
export type Def<x extends GetKeys<References>> = GetDef<References, x>;
2023-06-05 05:46:35 +00:00
//#reginon Chart
export type ChartSchema = Record<string, {
uniqueIncrement?: boolean;
intersection?: string[] | ReadonlyArray<string>;
range?: 'big' | 'small' | 'medium';
// previousな値を引き継ぐかどうか
accumulate?: boolean;
}>;
export type ChartResult<T extends ChartSchema> = {
[P in keyof T]: number[];
};
type UnionToIntersection<T> = (T extends any ? (x: T) => any : never) extends (x: infer R) => any ? R : never;
type UnflattenSingleton<K extends string, V> = K extends `${infer A}.${infer B}`
? { [_ in A]: UnflattenSingleton<B, V>; }
: { [_ in K]: V; };
export type Unflatten<T extends Record<string, any>> = Projected<UnionToIntersection<
{
[K in Extract<keyof T, string>]: UnflattenSingleton<K, T[K]>;
}[Extract<keyof T, string>]
>>;
type ToJsonSchema<S> = {
type: 'object';
properties: {
[K in keyof S]: S[K] extends number[] ? { type: 'array'; items: { type: 'number'; }; } : ToJsonSchema<S[K]>;
},
required: (keyof S)[];
};
export function getJsonSchema<S extends ChartSchema>(schema: S): ToJsonSchema<Unflatten<ChartResult<S>>> {
const jsonSchema = {
type: 'object',
properties: {} as Record<string, unknown>,
required: [],
};
for (const k in schema) {
jsonSchema.properties[k] = {
type: 'array',
items: { type: 'number' },
};
}
return jsonSchema as ToJsonSchema<Unflatten<ChartResult<S>>>;
}
//#endregion