This commit is contained in:
tamaina
2023-05-22 04:50:18 +00:00
parent e689dcdd73
commit 86f4e206f4
10 changed files with 135 additions and 88 deletions

View File

@@ -1,7 +1,7 @@
module.exports = {
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json'],
project: ['./tsconfig.json', './test/tsconfig.json'],
},
extends: [
'../shared/.eslintrc.js',

View File

@@ -26,6 +26,7 @@
"@types/node": "18.16.3",
"@typescript-eslint/eslint-plugin": "5.59.5",
"@typescript-eslint/parser": "5.59.5",
"ajv": "8.12.0",
"eslint": "8.40.0",
"jest": "29.5.0",
"jest-fetch-mock": "3.0.3",

View File

@@ -446,3 +446,17 @@ export const endpoints = {
}],
},
} as const satisfies { [x: string]: IEndpointMeta; };
export function getEndpointSchema(reqres: 'req' | 'res', key: keyof typeof endpoints) {
const endpoint = endpoints[key];
const schemas = endpoint.defines.map(d => d[reqres]).filter(d => d !== undefined);
if (schemas.length === 0) {
return null;
}
if (schemas.length === 1) {
return schemas[0];
}
return {
oneOf: schemas,
};
}

View File

@@ -3,7 +3,6 @@ import Stream, { Connection } from './streaming.js';
import { Channels } from './streaming.types.js';
import { Acct } from './acct.js';
import type { Packed, Def } from './schemas.js';
import { refs as _refs } from './schemas.js';
import * as consts from './consts.js';
export {
@@ -15,8 +14,6 @@ export {
Packed, Def,
};
export const refs = _refs;
export const permissions = consts.permissions;
export const notificationTypes = consts.notificationTypes;
export const obsoleteNotificationTypes = consts.obsoleteNotificationTypes;

View File

@@ -1,5 +1,31 @@
import { enableFetchMocks } from 'jest-fetch-mock';
import { APIClient, isAPIError } from '../src/api';
import Ajv from 'ajv';
import { endpoints, getEndpointSchema } from '../src/endpoints';
import { Endpoints } from '@/endpoints.types';
describe('schemas', () => {
describe.each(Object.keys(endpoints))('validate schema of %s', async (key) => {
const ajv = new Ajv({
useDefaults: true,
});
ajv.addFormat('misskey:id', /^[a-zA-Z0-9]+$/);
const endpoint = (endpoints as any)[key] as unknown as Endpoints[keyof Endpoints];
test('each schemas', async () => {
for (const def of endpoint.defines) {
if (def.res === undefined) continue;
ajv.compile(def.req);
}
});
test('jointed schema (oneOf)', () => {
const req = getEndpointSchema('req', key as keyof Endpoints);
if (req) ajv.compile(req);
});
});
});
enableFetchMocks();

View File

@@ -0,0 +1,42 @@
{
"compilerOptions": {
"allowJs": true,
"noEmitOnError": false,
"noImplicitAny": true,
"noImplicitReturns": true,
"noUnusedParameters": false,
"noUnusedLocals": true,
"noFallthroughCasesInSwitch": true,
"declaration": false,
"sourceMap": true,
"target": "es2021",
"module": "es2020",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"removeComments": false,
"noLib": false,
"strict": true,
"strictNullChecks": true,
"strictPropertyInitialization": false,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"resolveJsonModule": true,
"isolatedModules": true,
"baseUrl": "./",
"paths": {
"@/*": ["../src/*"]
},
"typeRoots": [
"../node_modules/@types",
"../src/@types"
],
"lib": [
"esnext"
],
"types": ["jest", "node"]
},
"compileOnSave": false,
"include": [
"./**/*.ts",
]
}