This commit is contained in:
kakkokari-gtyih 2024-09-07 01:02:36 +09:00
parent 619b4fdf52
commit 22adc78906
4 changed files with 23 additions and 20 deletions

View File

@ -3,6 +3,8 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import type { SomeJTDSchemaType } from 'ajv/dist/core.js';
import {
packedMeDetailedOnlySchema,
packedMeDetailedSchema,
@ -134,19 +136,19 @@ type StringDefToType<T extends TypeStringef> =
any;
// https://swagger.io/specification/?sbsearch=optional#schema-object
type OfSchema = {
readonly anyOf?: ReadonlyArray<Schema>;
readonly oneOf?: ReadonlyArray<Schema>;
readonly allOf?: ReadonlyArray<Schema>;
type OfSchema<T> = {
readonly anyOf?: ReadonlyArray<T>;
readonly oneOf?: ReadonlyArray<T>;
readonly allOf?: ReadonlyArray<T>;
}
export interface Schema extends OfSchema {
interface SchemaFactory<T> extends OfSchema<T> {
readonly type?: TypeStringef;
readonly nullable?: boolean;
readonly optional?: boolean;
readonly prefixItems?: ReadonlyArray<Schema>;
readonly items?: Schema;
readonly unevaluatedItems?: Schema | boolean;
readonly prefixItems?: ReadonlyArray<T>;
readonly items?: T;
readonly unevaluatedItems?: T | boolean;
readonly properties?: Obj;
readonly required?: ReadonlyArray<Extract<keyof NonNullable<this['properties']>, string>>;
readonly description?: string;
@ -163,6 +165,10 @@ export interface Schema extends OfSchema {
readonly pattern?: string;
}
export type Schema = SchemaFactory<Schema>;
export type ValidatableSchema = SchemaFactory<Schema & SomeJTDSchemaType> & SomeJTDSchemaType;
type RequiredPropertyNames<s extends Obj> = {
[K in keyof s]:
// K is not optional
@ -229,7 +235,7 @@ export type SchemaTypeDef<p extends Schema> =
p['type'] extends 'boolean' ? boolean :
p['type'] extends 'object' ? ObjectSchemaTypeDef<p> :
p['type'] extends 'array' ? (
p['items'] extends OfSchema ? (
p['items'] extends OfSchema<p> ? (
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']>>>[] :

View File

@ -4,7 +4,8 @@
*/
import { permissions } from 'misskey-js';
import type { KeyOf, Schema } from '@/misc/json-schema.js';
import type { ApiErrorInput } from '@/server/api/error.js';
import type { KeyOf, ValidatableSchema } from '@/misc/json-schema.js';
import * as ep___admin_abuseReport_notificationRecipient_list
from '@/server/api/endpoints/admin/abuse-report/notification-recipient/list.js';
@ -780,14 +781,10 @@ interface IEndpointMetaBase {
readonly tags?: ReadonlyArray<string>;
readonly errors?: {
readonly [key: string]: {
readonly message: string;
readonly code: string;
readonly id: string;
};
readonly [key: string]: ApiErrorInput;
};
readonly res?: Schema;
readonly res?: ValidatableSchema;
/**
*

View File

@ -6,7 +6,7 @@
import { Inject, Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js';
import type { IEndpointMeta } from '@/server/api/endpoints.js';
import type { Schema } from '@/misc/json-schema.js';
import type { Schema, ValidatableSchema } from '@/misc/json-schema.js';
import type { AppsRepository } from '@/models/_.js';
import { IdService } from '@/core/IdService.js';
import { unique } from '@/misc/prelude/array.js';
@ -37,7 +37,7 @@ export const paramDef = {
callbackUrl: { type: 'string', nullable: true },
},
required: ['name', 'description', 'permission'],
} as const satisfies Schema;
} as const satisfies ValidatableSchema;
@Injectable()
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export

View File

@ -3,7 +3,7 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
type E = { message: string, code: string, id: string, kind?: 'client' | 'server' | 'permission', httpStatusCode?: number };
export type ApiErrorInput = { message: string, code: string, id: string, kind?: 'client' | 'server' | 'permission', httpStatusCode?: number };
export class ApiError extends Error {
public message: string;
@ -13,7 +13,7 @@ export class ApiError extends Error {
public httpStatusCode?: number;
public info?: any;
constructor(err?: E | null | undefined, info?: any | null | undefined) {
constructor(err?: ApiErrorInput | null | undefined, info?: any | null | undefined) {
if (err == null) err = {
message: 'Internal error occurred. Please contact us if the error persists.',
code: 'INTERNAL_ERROR',