From 1b6ac7888bfa1ab32f351565024751657ec925f0 Mon Sep 17 00:00:00 2001 From: tamaina Date: Mon, 22 May 2023 05:20:19 +0000 Subject: [PATCH] wip --- .../src/server/api/openapi/gen-spec.ts | 14 ++++---- .../backend/src/server/api/openapi/schemas.ts | 27 --------------- packages/backend/test/e2e/antennas.ts | 11 +++--- packages/backend/test/utils.ts | 20 ++++++----- packages/misskey-js/src/entities.ts | 1 + packages/misskey-js/src/schemas.ts | 4 +++ packages/misskey-js/src/schemas/blocking.ts | 2 +- packages/misskey-js/src/schemas/error.ts | 34 +++++++++++++++++++ packages/misskey-js/test-d/schemas.ts | 4 +++ packages/misskey-js/test/api.ts | 2 +- 10 files changed, 68 insertions(+), 51 deletions(-) create mode 100644 packages/misskey-js/src/schemas/error.ts diff --git a/packages/backend/src/server/api/openapi/gen-spec.ts b/packages/backend/src/server/api/openapi/gen-spec.ts index fc6dadc23..fc1e3081e 100644 --- a/packages/backend/src/server/api/openapi/gen-spec.ts +++ b/packages/backend/src/server/api/openapi/gen-spec.ts @@ -38,7 +38,7 @@ export function genOpenapiSpec(config: Config) { }, }; - for (const [name, endpoint] of Object.entries(endpoints).filter(([name, ep]) => !ep.secure)) { + for (const [name, endpoint] of Object.entries(endpoints).filter(([name, ep]) => !('secure' in ep) || !ep.secure)) { const errors = {} as any; if ('errors' in endpoint && endpoint.errors) { @@ -107,7 +107,7 @@ export function genOpenapiSpec(config: Config) { content: { 'application/json': { schema: { - $ref: '#/components/schemas/Error', + $ref: '#/components/schemas/ApiError', }, examples: { ...errors, ...basicErrors['400'] }, }, @@ -118,7 +118,7 @@ export function genOpenapiSpec(config: Config) { content: { 'application/json': { schema: { - $ref: '#/components/schemas/Error', + $ref: '#/components/schemas/ApiError', }, examples: basicErrors['401'], }, @@ -129,7 +129,7 @@ export function genOpenapiSpec(config: Config) { content: { 'application/json': { schema: { - $ref: '#/components/schemas/Error', + $ref: '#/components/schemas/ApiError', }, examples: basicErrors['403'], }, @@ -140,7 +140,7 @@ export function genOpenapiSpec(config: Config) { content: { 'application/json': { schema: { - $ref: '#/components/schemas/Error', + $ref: '#/components/schemas/ApiError', }, examples: basicErrors['418'], }, @@ -152,7 +152,7 @@ export function genOpenapiSpec(config: Config) { content: { 'application/json': { schema: { - $ref: '#/components/schemas/Error', + $ref: '#/components/schemas/ApiError', }, examples: basicErrors['429'], }, @@ -164,7 +164,7 @@ export function genOpenapiSpec(config: Config) { content: { 'application/json': { schema: { - $ref: '#/components/schemas/Error', + $ref: '#/components/schemas/ApiError', }, examples: basicErrors['500'], }, diff --git a/packages/backend/src/server/api/openapi/schemas.ts b/packages/backend/src/server/api/openapi/schemas.ts index bfffddbf8..1faf2bafc 100644 --- a/packages/backend/src/server/api/openapi/schemas.ts +++ b/packages/backend/src/server/api/openapi/schemas.ts @@ -1,32 +1,5 @@ import { refs } from 'misskey-js/built/schemas.js'; export const schemas = { - Error: { - type: 'object', - properties: { - error: { - type: 'object', - description: 'An error object.', - properties: { - code: { - type: 'string', - description: 'An error code. Unique within the endpoint.', - }, - message: { - type: 'string', - description: 'An error message.', - }, - id: { - type: 'string', - format: 'uuid', - description: 'An error ID. This ID is static.', - }, - }, - required: ['code', 'id', 'message'], - }, - }, - required: ['error'], - }, - ...refs, }; diff --git a/packages/backend/test/e2e/antennas.ts b/packages/backend/test/e2e/antennas.ts index dd3b09f85..0065efeeb 100644 --- a/packages/backend/test/e2e/antennas.ts +++ b/packages/backend/test/e2e/antennas.ts @@ -3,7 +3,6 @@ process.env.NODE_ENV = 'test'; import * as assert from 'assert'; import { inspect } from 'node:util'; import { DEFAULT_POLICIES } from '@/core/RoleService.js'; -import type { Packed } from '@/misc/json-schema.js'; import { signup, post, @@ -19,6 +18,7 @@ import { } from '../utils.js'; import type * as misskey from 'misskey-js'; import type { INestApplicationContext } from '@nestjs/common'; +import { WeakSerialized } from 'schema-type'; const compareBy = (selector: (s: T) => string = (s: T): string => s.id) => (a: T, b: T): number => { return selector(a).localeCompare(selector(b)); @@ -28,12 +28,9 @@ describe('アンテナ', () => { // エンティティとしてのアンテナを主眼においたテストを記述する // (Antennaを返すエンドポイント、Antennaエンティティを書き換えるエンドポイント、Antennaからノートを取得するエンドポイントをテストする) - // BUG misskey-jsとjson-schemaが一致していない。 - // - srcのenumにgroupが残っている - // - userGroupIdが残っている, isActiveがない - type Antenna = misskey.entities.Antenna | Packed<'Antenna'>; - type User = misskey.entities.MeDetailed & { token: string }; - type Note = misskey.entities.Note; + type Antenna = WeakSerialized; + type User = WeakSerialized; + type Note = WeakSerialized; // アンテナを作成できる最小のパラメタ const defaultParam = { diff --git a/packages/backend/test/utils.ts b/packages/backend/test/utils.ts index 1a4a0b354..ca6c5d9f2 100644 --- a/packages/backend/test/utils.ts +++ b/packages/backend/test/utils.ts @@ -10,6 +10,7 @@ import { DEFAULT_POLICIES } from '@/core/RoleService.js'; import { entities } from '../src/postgres.js'; import { loadConfig } from '../src/config.js'; import type * as misskey from 'misskey-js'; +import { SchemaOrUndefined } from 'misskey-js/built/endpoints.types.js'; export { server as startServer } from '@/boot/common.js'; @@ -25,15 +26,18 @@ export const api = async (endpoint: string, params: any, me?: any) => { return await request(`api/${normalized}`, params, me); }; -export type ApiRequest = { - endpoint: string, - parameters: object, +export type ApiRequest = { + endpoint: X, + parameters: SchemaOrUndefined

, user: object | undefined, }; -export const successfulApiCall = async (request: ApiRequest, assertion: { - status?: number, -} = {}): Promise => { +export const successfulApiCall = async ( + request: ApiRequest, + assertion: { + status?: number, + } = {} +): Promise> => { const { endpoint, parameters, user } = request; const res = await api(endpoint, parameters, user); const status = assertion.status ?? (res.body == null ? 204 : 200); @@ -41,11 +45,11 @@ export const successfulApiCall = async (request: ApiRequest, assertion: { return res.body; }; -export const failedApiCall = async (request: ApiRequest, assertion: { +export const failedApiCall = async (request: ApiRequest, assertion: { status: number, code: string, id: string -}): Promise => { +}): Promise> => { const { endpoint, parameters, user } = request; const { status, code, id } = assertion; const res = await api(endpoint, parameters, user); diff --git a/packages/misskey-js/src/entities.ts b/packages/misskey-js/src/entities.ts index 76cae7dda..44e795ea0 100644 --- a/packages/misskey-js/src/entities.ts +++ b/packages/misskey-js/src/entities.ts @@ -7,6 +7,7 @@ type TODO = Record; // NOTE: 極力この型を使うのは避け、UserLite か UserDetailed か明示するように export type User = Packed<'User'>; + export type UserLite = Packed<'UserLite'>; export type UserDetailed = Packed<'UserDetailed'>; export type UserList = Packed<'UserList'>; diff --git a/packages/misskey-js/src/schemas.ts b/packages/misskey-js/src/schemas.ts index e51ddb190..a66ccfb08 100644 --- a/packages/misskey-js/src/schemas.ts +++ b/packages/misskey-js/src/schemas.ts @@ -35,6 +35,7 @@ import { packedEmojiDetailedSchema, packedEmojiSimpleSchema } from './schemas/em import { packedFlashSchema } from './schemas/flash.js'; import { packedAdSchema } from './schemas/ad.js'; import { packedAnnouncementSchema } from './schemas/announcement.js'; +import { Error, ApiError } from './schemas/error.js'; import type { JSONSchema7, JSONSchema7Definition, GetDef, GetRefs, GetKeys, UnionToArray } from 'schema-type'; export const refs = { @@ -74,6 +75,9 @@ export const refs = { Flash: packedFlashSchema, Ad: packedAdSchema, Announcement: packedAnnouncementSchema, + + Error: Error, + ApiError: ApiError, } as const satisfies { [x: string]: JSONSchema7Definition }; export type References = GetRefs; diff --git a/packages/misskey-js/src/schemas/blocking.ts b/packages/misskey-js/src/schemas/blocking.ts index 8c21d1e6f..f842a489b 100644 --- a/packages/misskey-js/src/schemas/blocking.ts +++ b/packages/misskey-js/src/schemas/blocking.ts @@ -17,6 +17,6 @@ export const packedBlockingSchema = { 'id', 'createdAt', 'blockeeId', - 'blockee', + //'blockee', ], } as const satisfies JSONSchema7Definition; diff --git a/packages/misskey-js/src/schemas/error.ts b/packages/misskey-js/src/schemas/error.ts new file mode 100644 index 000000000..773713d41 --- /dev/null +++ b/packages/misskey-js/src/schemas/error.ts @@ -0,0 +1,34 @@ +import type { JSONSchema7Definition } from 'schema-type'; + +export const Error = { + $id: 'https://misskey-hub.net/api/schemas/Error', + + type: 'object', + description: 'An error object.', + properties: { + code: { + type: 'string', + description: 'An error code. Unique within the endpoint.', + }, + message: { + type: 'string', + description: 'An error message.', + }, + id: { + type: 'string', + format: 'uuid', + description: 'An error ID. This ID is static.', + }, + }, + required: ['code', 'id', 'message'], +} as const satisfies JSONSchema7Definition; + +export const ApiError = { + $id: 'https://misskey-hub.net/api/schemas/ApiError', + + type: 'object', + properties: { + error: { $ref: 'https://misskey-hub.net/api/schemas/Error' }, + }, + required: ['error'], +} as const satisfies JSONSchema7Definition; diff --git a/packages/misskey-js/test-d/schemas.ts b/packages/misskey-js/test-d/schemas.ts index af2f5dceb..c586611d4 100644 --- a/packages/misskey-js/test-d/schemas.ts +++ b/packages/misskey-js/test-d/schemas.ts @@ -97,4 +97,8 @@ describe('schemas', () => { test('ad', () => { type Ad = Packed<'Ad'>; }); + test('error', () => { + type Error = Packed<'Error'>; + type ApiError = Packed<'ApiError'>; + }); }); diff --git a/packages/misskey-js/test/api.ts b/packages/misskey-js/test/api.ts index c816608a2..f63c4a96a 100644 --- a/packages/misskey-js/test/api.ts +++ b/packages/misskey-js/test/api.ts @@ -20,7 +20,7 @@ describe('schemas', () => { } }); - test('jointed schema (oneOf)', () => { + test('jointed schema', () => { const req = getEndpointSchema('req', key as keyof Endpoints); if (req) ajv.compile(req); });