From 22adc78906bd8368fd8640a26f9504a367cfda5c Mon Sep 17 00:00:00 2001 From: kakkokari-gtyih <67428053+kakkokari-gtyih@users.noreply.github.com> Date: Sat, 7 Sep 2024 01:02:36 +0900 Subject: [PATCH] fix --- packages/backend/src/misc/json-schema.ts | 24 ++++++++++++------- packages/backend/src/server/api/endpoints.ts | 11 ++++----- .../src/server/api/endpoints/app/create.ts | 4 ++-- packages/backend/src/server/api/error.ts | 4 ++-- 4 files changed, 23 insertions(+), 20 deletions(-) diff --git a/packages/backend/src/misc/json-schema.ts b/packages/backend/src/misc/json-schema.ts index 040e36228c..e6886923f6 100644 --- a/packages/backend/src/misc/json-schema.ts +++ b/packages/backend/src/misc/json-schema.ts @@ -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 = any; // https://swagger.io/specification/?sbsearch=optional#schema-object -type OfSchema = { - readonly anyOf?: ReadonlyArray; - readonly oneOf?: ReadonlyArray; - readonly allOf?: ReadonlyArray; +type OfSchema = { + readonly anyOf?: ReadonlyArray; + readonly oneOf?: ReadonlyArray; + readonly allOf?: ReadonlyArray; } -export interface Schema extends OfSchema { +interface SchemaFactory extends OfSchema { readonly type?: TypeStringef; readonly nullable?: boolean; readonly optional?: boolean; - readonly prefixItems?: ReadonlyArray; - readonly items?: Schema; - readonly unevaluatedItems?: Schema | boolean; + readonly prefixItems?: ReadonlyArray; + readonly items?: T; + readonly unevaluatedItems?: T | boolean; readonly properties?: Obj; readonly required?: ReadonlyArray, string>>; readonly description?: string; @@ -163,6 +165,10 @@ export interface Schema extends OfSchema { readonly pattern?: string; } +export type Schema = SchemaFactory; + +export type ValidatableSchema = SchemaFactory & SomeJTDSchemaType; + type RequiredPropertyNames = { [K in keyof s]: // K is not optional @@ -229,7 +235,7 @@ export type SchemaTypeDef

= p['type'] extends 'boolean' ? boolean : p['type'] extends 'object' ? ObjectSchemaTypeDef

: p['type'] extends 'array' ? ( - p['items'] extends OfSchema ? ( + p['items'] extends OfSchema

? ( p['items']['anyOf'] extends ReadonlyArray ? UnionSchemaType>[] : p['items']['oneOf'] extends ReadonlyArray ? ArrayUnion>> : p['items']['allOf'] extends ReadonlyArray ? UnionToIntersection>>[] : diff --git a/packages/backend/src/server/api/endpoints.ts b/packages/backend/src/server/api/endpoints.ts index 3dfb7fdad4..6d45bb7e58 100644 --- a/packages/backend/src/server/api/endpoints.ts +++ b/packages/backend/src/server/api/endpoints.ts @@ -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; readonly errors?: { - readonly [key: string]: { - readonly message: string; - readonly code: string; - readonly id: string; - }; + readonly [key: string]: ApiErrorInput; }; - readonly res?: Schema; + readonly res?: ValidatableSchema; /** * このエンドポイントにリクエストするのにユーザー情報が必須か否か diff --git a/packages/backend/src/server/api/endpoints/app/create.ts b/packages/backend/src/server/api/endpoints/app/create.ts index 8f6026a2f4..7ace305e25 100644 --- a/packages/backend/src/server/api/endpoints/app/create.ts +++ b/packages/backend/src/server/api/endpoints/app/create.ts @@ -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 { // eslint-disable-line import/no-default-export diff --git a/packages/backend/src/server/api/error.ts b/packages/backend/src/server/api/error.ts index 2f8322a568..60dfdccfd4 100644 --- a/packages/backend/src/server/api/error.ts +++ b/packages/backend/src/server/api/error.ts @@ -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',