docs: add all json schema for openapi

pull/220/head
moonrailgun 1 year ago
parent 592e4ab8bd
commit 0b9233d7f9

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -24,7 +24,8 @@
"admin": "cd admin && pnpm run build && pnpm run start", "admin": "cd admin && pnpm run build && pnpm run start",
"plugin:install": "node ./scripts/installPlugin.js", "plugin:install": "node ./scripts/installPlugin.js",
"protobuf": "proto-loader-gen-types --longs=String --enums=String --defaults --oneofs --grpcLib=@grpc/grpc-js --outDir=proto/ proto/*.proto", "protobuf": "proto-loader-gen-types --longs=String --enums=String --defaults --oneofs --grpcLib=@grpc/grpc-js --outDir=proto/ proto/*.proto",
"gen:swagger": "ts-node ./scripts/swagger.ts" "gen:swagger:old": "ts-node ./scripts/swagger.ts",
"gen:openapi": "tailchat-service-openapi-generator"
}, },
"pnpm": { "pnpm": {
"peerDependencyRules": { "peerDependencyRules": {
@ -117,6 +118,7 @@
"prettier": "^2.3.2", "prettier": "^2.3.2",
"socket.io-client": "^4.1.3", "socket.io-client": "^4.1.3",
"swagger-jsdoc": "^6.2.8", "swagger-jsdoc": "^6.2.8",
"tailchat-service-openapi-generator": "workspace:^",
"tailchat-service-swagger-generator": "workspace:^1.0.0", "tailchat-service-swagger-generator": "workspace:^1.0.0",
"ts-jest": "27.1.4", "ts-jest": "27.1.4",
"vinyl-fs": "^3.0.3" "vinyl-fs": "^3.0.3"

@ -0,0 +1,31 @@
{
"name": "tailchat-service-openapi-generator",
"private": true,
"version": "1.0.0",
"description": "",
"main": "index.js",
"bin": "./dist/index.js",
"scripts": {
"dev": "tsc --watch",
"prepare": "tsc",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"msgbyte",
"moonrailgun",
"tailchat"
],
"author": "moonrailgun <moonrailgun@gmail.com>",
"license": "MIT",
"dependencies": {
"@apidevtools/swagger-parser": "^10.1.0",
"globby": "11.1.0",
"openapi3-ts": "^4.3.1",
"tailchat-server-sdk": "workspace:^",
"ts-node": "^10.9.1"
},
"devDependencies": {
"@types/node": "^18.11.18",
"typescript": "^4.9.4"
}
}

@ -0,0 +1,38 @@
import _ from 'lodash';
import type { TcService } from 'tailchat-server-sdk';
import type { PathsObject, SchemaObject } from 'openapi3-ts/oas31';
export function generateOpenapiPath(service: TcService): PathsObject {
const serviceName = service.serviceName;
const actions = service.getActionList();
const paths: PathsObject = {};
for (const action of actions) {
const pathName = '/' + serviceName + '/' + action.name;
paths[pathName] = {
post: {
requestBody: {
content: {
'application/json': {
schema: {
type: 'object',
properties: generateRequestBodyProperties(action.params),
},
},
},
},
},
};
}
return paths;
}
function generateRequestBodyProperties(params: {
[x: string]: any;
}): Record<string, SchemaObject> {
return _.mapValues(params, (type) => {
return type;
});
}

@ -0,0 +1,70 @@
import globby from 'globby';
import { TcBroker, TcService } from 'tailchat-server-sdk';
import { generateOpenapiPath } from './generateOpenapiPath';
import type { OpenAPIObject } from 'openapi3-ts/oas31';
import SwaggerParser from '@apidevtools/swagger-parser';
import fs from 'fs-extra';
import path from 'path';
import 'ts-node/register';
/**
* https://ts-morph.com/setup/
*/
/**
*
*/
async function scanServices(): Promise<OpenAPIObject> {
const packageJsonPath = path.resolve(__dirname, '../../../../package.json');
const version = (await fs.readJson(packageJsonPath)).verson || '0.0.0';
const serviceFiles = await globby(
['./services/**/*.service.ts', './plugins/**/*.service.ts'],
{
absolute: true,
}
);
console.log('Service List:', serviceFiles);
const openapiObj: OpenAPIObject = {
openapi: '3.1.0',
info: {
title: 'Tailchat Openapi',
version,
},
paths: {},
};
const broker = new TcBroker({
logger: false,
});
for (const servicePath of serviceFiles) {
const { default: serviceCls } = await import(servicePath);
if (TcService.prototype.isPrototypeOf(serviceCls.prototype)) {
const service: TcService = new serviceCls(broker);
openapiObj.paths = {
...openapiObj.paths,
...generateOpenapiPath(service),
};
}
}
broker.stop();
try {
await SwaggerParser.validate(openapiObj as any);
return openapiObj;
} catch (err) {
console.error(err);
}
}
scanServices().then(async (openapiObj) => {
await fs.writeJSON('./openapi.json', openapiObj, {
spaces: 2,
});
console.log(
'generate completed, if process not exist auto, you can exit it by yourself'
);
});

@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"declaration": true,
"outDir": "./dist",
"paths": {}
},
"include": ["./src/**/*"],
"exclude": ["./node_modules/**/*", "./dist/**/*"]
}

@ -1,6 +1,6 @@
{ {
"name": "tailchat-server-sdk", "name": "tailchat-server-sdk",
"version": "0.0.17", "version": "0.0.18",
"description": "", "description": "",
"main": "dist/index.js", "main": "dist/index.js",
"bin": { "bin": {
@ -10,7 +10,7 @@
"build": "tsc", "build": "tsc",
"watch": "tsc --watch", "watch": "tsc --watch",
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"prepare": "pnpm build", "prepare": "tsc",
"release": "npm version patch && npm publish --registry https://registry.npmjs.com/" "release": "npm version patch && npm publish --registry https://registry.npmjs.com/"
}, },
"repository": { "repository": {

@ -197,6 +197,26 @@ export abstract class TcService extends Service {
}; };
} }
/**
*
*/
getActionList() {
return Object.entries(this._actions).map(
([name, schema]: [string, ServiceActionSchema]) => {
return {
name,
params: _.mapValues(schema.params, (type) => {
if (typeof type === 'string') {
return { type: type };
} else {
return type;
}
}),
};
}
);
}
registerMixin(mixin: Partial<ServiceSchema>): void { registerMixin(mixin: Partial<ServiceSchema>): void {
this._mixins.push(mixin); this._mixins.push(mixin);
} }
@ -423,6 +443,10 @@ export abstract class TcService extends Service {
* NOTICE: 使Redisservice * NOTICE: 使Redisservice
*/ */
async cleanActionCache(actionName: string, keys: string[] = []) { async cleanActionCache(actionName: string, keys: string[] = []) {
if (!this.broker.cacher) {
console.error('Can not clean cache because no cacher existed.');
}
if (keys.length === 0) { if (keys.length === 0) {
await this.broker.cacher.clean(`${this.serviceName}.${actionName}`); await this.broker.cacher.clean(`${this.serviceName}.${actionName}`);
} else { } else {

Loading…
Cancel
Save