パラメータenumをやめる

This commit is contained in:
osamu
2023-11-26 22:54:46 +09:00
parent d62d5a8cdf
commit 1171d2f0be
15 changed files with 13 additions and 72 deletions
-8
View File
@@ -36,9 +36,6 @@ import { packedGalleryPostSchema } from '@/models/json-schema/gallery-post.js';
import { packedEmojiDetailedSchema, packedEmojiSimpleSchema } from '@/models/json-schema/emoji.js';
import { packedFlashSchema } from '@/models/json-schema/flash.js';
import { packedAnnouncementSchema } from '@/models/json-schema/announcement.js';
import { enumUserOriginSchema } from '@/models/json-schema/enum-user-origin.js';
import { enumUserSortingSchema } from '@/models/json-schema/enum-user-sorting.js';
import { enumNotificationTypeSchema } from '@/models/json-schema/enum-notification-type.js';
export const refs = {
UserLite: packedUserLiteSchema,
@@ -74,11 +71,6 @@ export const refs = {
EmojiSimple: packedEmojiSimpleSchema,
EmojiDetailed: packedEmojiDetailedSchema,
Flash: packedFlashSchema,
// enum
UserOrigin: enumUserOriginSchema,
UserSorting: enumUserSortingSchema,
NotificationType: enumNotificationTypeSchema,
};
export type Packed<x extends keyof typeof refs> = SchemaType<typeof refs[x]>;
@@ -1,11 +0,0 @@
/*
* SPDX-FileCopyrightText: syuilo and other misskey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { notificationTypes } from '@/types.js';
export const enumNotificationTypeSchema = {
type: 'string',
enum: [...notificationTypes, 'reaction:grouped', 'renote:grouped'],
} as const;
@@ -1,9 +0,0 @@
/*
* SPDX-FileCopyrightText: syuilo and other misskey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
export const enumUserOriginSchema = {
type: 'string',
enum: ['combined', 'local', 'remote'],
} as const;
@@ -1,16 +0,0 @@
/*
* SPDX-FileCopyrightText: syuilo and other misskey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
export const enumUserSortingSchema = {
type: 'string',
enum: [
'+follower',
'-follower',
'+createdAt',
'-createdAt',
'+updatedAt',
'-updatedAt',
],
} as const;
@@ -21,7 +21,7 @@ export const packedNotificationSchema = {
type: {
type: 'string',
optional: false, nullable: false,
ref: 'NotificationType',
enum: [...notificationTypes, 'reaction:grouped', 'renote:grouped'],
},
user: {
type: 'object',
@@ -9,7 +9,6 @@ import type { Schema, SchemaType } from '@/misc/json-schema.js';
import type { MiLocalUser } from '@/models/User.js';
import type { MiAccessToken } from '@/models/AccessToken.js';
import { ApiError } from './error.js';
import { omitAjvNotSupportProperty } from './openapi/schemas.js';
import type { IEndpointMeta } from './endpoints.js';
const Ajv = _Ajv.default;
@@ -36,8 +35,7 @@ export abstract class Endpoint<T extends IEndpointMeta, Ps extends Schema> {
public exec: (params: any, user: T['requireCredential'] extends true ? MiLocalUser : MiLocalUser | null, token: MiAccessToken | null, file?: File, ip?: string | null, headers?: Record<string, string> | null) => Promise<any>;
constructor(meta: T, paramDef: Ps, cb: Executor<T, Ps>) {
const ajvSchema = omitAjvNotSupportProperty(paramDef);
const validate = ajv.compile(ajvSchema);
const validate = ajv.compile(paramDef);
this.exec = (params: any, user: T['requireCredential'] extends true ? MiLocalUser : MiLocalUser | null, token: MiAccessToken | null, file?: File, ip?: string | null, headers?: Record<string, string> | null) => {
let cleanup: undefined | (() => void) = undefined;
@@ -85,8 +85,8 @@ export const paramDef = {
sinceId: { type: 'string', format: 'misskey:id' },
untilId: { type: 'string', format: 'misskey:id' },
state: { type: 'string', nullable: true, default: null },
reporterOrigin: { type: 'string', ref: 'UserOrigin', default: 'combined' },
targetUserOrigin: { type: 'string', ref: 'UserOrigin', default: 'combined' },
reporterOrigin: { type: 'string', enum: ['combined', 'local', 'remote'], default: 'combined' },
targetUserOrigin: { type: 'string', enum: ['combined', 'local', 'remote'], default: 'combined' },
forwarded: { type: 'boolean', default: false },
},
required: [],
@@ -35,7 +35,7 @@ export const paramDef = {
untilId: { type: 'string', format: 'misskey:id' },
userId: { type: 'string', format: 'misskey:id', nullable: true },
type: { type: 'string', nullable: true, pattern: /^[a-zA-Z0-9\/\-*]+$/.toString().slice(1, -1) },
origin: { type: 'string', ref: 'UserOrigin', default: 'local' },
origin: { type: 'string', enum: ['combined', 'local', 'remote'], default: 'local' },
hostname: {
type: 'string',
nullable: true,
@@ -35,7 +35,7 @@ export const paramDef = {
offset: { type: 'integer', default: 0 },
sort: { type: 'string', enum: ['+follower', '-follower', '+createdAt', '-createdAt', '+updatedAt', '-updatedAt', '+lastActiveDate', '-lastActiveDate'] },
state: { type: 'string', enum: ['all', 'alive', 'available', 'admin', 'moderator', 'adminOrModerator', 'suspended'], default: 'all' },
origin: { type: 'string', ref: 'UserOrigin', default: 'combined' },
origin: { type: 'string', enum: ['combined', 'local', 'remote'], default: 'combined' },
username: { type: 'string', nullable: true, default: null },
hostname: {
type: 'string',
@@ -31,9 +31,9 @@ export const paramDef = {
properties: {
tag: { type: 'string' },
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
sort: { type: 'string', ref: 'UserSorting' },
sort: { type: 'string', enum: ['+follower', '-follower', '+createdAt', '-createdAt', '+updatedAt', '-updatedAt'] },
state: { type: 'string', enum: ['all', 'alive'], default: 'all' },
origin: { type: 'string', ref: 'UserOrigin', default: 'local' },
origin: { type: 'string', enum: ['combined', 'local', 'remote'], default: 'local' },
},
required: ['tag', 'sort'],
} as const;
@@ -31,9 +31,9 @@ export const paramDef = {
properties: {
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
offset: { type: 'integer', default: 0 },
sort: { type: 'string', ref: 'UserSorting' },
sort: { type: 'string', enum: ['+follower', '-follower', '+createdAt', '-createdAt', '+updatedAt', '-updatedAt'] },
state: { type: 'string', enum: ['all', 'alive'], default: 'all' },
origin: { type: 'string', ref: 'UserOrigin', default: 'local' },
origin: { type: 'string', enum: ['combined', 'local', 'remote'], default: 'local' },
hostname: {
type: 'string',
nullable: true,
@@ -36,7 +36,7 @@ export const paramDef = {
query: { type: 'string' },
offset: { type: 'integer', default: 0 },
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
origin: { type: 'string', ref: 'UserOrigin', default: 'combined' },
origin: { type: 'string', enum: ['local', 'remote', 'combined'], default: 'combined' },
detail: { type: 'boolean', default: true },
},
required: ['query'],
@@ -108,7 +108,7 @@ export function genOpenapiSpec(config: Config) {
required: true,
content: {
[requestType]: {
schema: convertSchemaToOpenApiSchema(schema),
schema,
},
},
},
@@ -43,19 +43,6 @@ export function convertSchemaToOpenApiSchema(schema: Schema) {
return res;
}
export function omitAjvNotSupportProperty(schema: Schema): Schema {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { optional, ref, ...res } = JSON.parse(JSON.stringify(schema)) as Schema;
if (res.type === 'object' && res.properties) {
for (const k of Object.keys(res.properties)) {
res.properties[k] = omitAjvNotSupportProperty(res.properties[k]);
}
}
return res;
}
export const schemas = {
Error: {
type: 'object',
File diff suppressed because one or more lines are too long