Skip to content

Commit 0b9233d

Browse files
committed
docs: add all json schema for openapi
1 parent 592e4ab commit 0b9233d

File tree

9 files changed

+3939
-163
lines changed

9 files changed

+3939
-163
lines changed

pnpm-lock.yaml

Lines changed: 400 additions & 160 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/openapi.json

Lines changed: 3361 additions & 0 deletions
Large diffs are not rendered by default.

server/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"admin": "cd admin && pnpm run build && pnpm run start",
2525
"plugin:install": "node ./scripts/installPlugin.js",
2626
"protobuf": "proto-loader-gen-types --longs=String --enums=String --defaults --oneofs --grpcLib=@grpc/grpc-js --outDir=proto/ proto/*.proto",
27-
"gen:swagger": "ts-node ./scripts/swagger.ts"
27+
"gen:swagger:old": "ts-node ./scripts/swagger.ts",
28+
"gen:openapi": "tailchat-service-openapi-generator"
2829
},
2930
"pnpm": {
3031
"peerDependencyRules": {
@@ -117,6 +118,7 @@
117118
"prettier": "^2.3.2",
118119
"socket.io-client": "^4.1.3",
119120
"swagger-jsdoc": "^6.2.8",
121+
"tailchat-service-openapi-generator": "workspace:^",
120122
"tailchat-service-swagger-generator": "workspace:^1.0.0",
121123
"ts-jest": "27.1.4",
122124
"vinyl-fs": "^3.0.3"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "tailchat-service-openapi-generator",
3+
"private": true,
4+
"version": "1.0.0",
5+
"description": "",
6+
"main": "index.js",
7+
"bin": "./dist/index.js",
8+
"scripts": {
9+
"dev": "tsc --watch",
10+
"prepare": "tsc",
11+
"test": "echo \"Error: no test specified\" && exit 1"
12+
},
13+
"keywords": [
14+
"msgbyte",
15+
"moonrailgun",
16+
"tailchat"
17+
],
18+
"author": "moonrailgun <[email protected]>",
19+
"license": "MIT",
20+
"dependencies": {
21+
"@apidevtools/swagger-parser": "^10.1.0",
22+
"globby": "11.1.0",
23+
"openapi3-ts": "^4.3.1",
24+
"tailchat-server-sdk": "workspace:^",
25+
"ts-node": "^10.9.1"
26+
},
27+
"devDependencies": {
28+
"@types/node": "^18.11.18",
29+
"typescript": "^4.9.4"
30+
}
31+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import _ from 'lodash';
2+
import type { TcService } from 'tailchat-server-sdk';
3+
import type { PathsObject, SchemaObject } from 'openapi3-ts/oas31';
4+
5+
export function generateOpenapiPath(service: TcService): PathsObject {
6+
const serviceName = service.serviceName;
7+
const actions = service.getActionList();
8+
9+
const paths: PathsObject = {};
10+
11+
for (const action of actions) {
12+
const pathName = '/' + serviceName + '/' + action.name;
13+
paths[pathName] = {
14+
post: {
15+
requestBody: {
16+
content: {
17+
'application/json': {
18+
schema: {
19+
type: 'object',
20+
properties: generateRequestBodyProperties(action.params),
21+
},
22+
},
23+
},
24+
},
25+
},
26+
};
27+
}
28+
29+
return paths;
30+
}
31+
32+
function generateRequestBodyProperties(params: {
33+
[x: string]: any;
34+
}): Record<string, SchemaObject> {
35+
return _.mapValues(params, (type) => {
36+
return type;
37+
});
38+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import globby from 'globby';
2+
import { TcBroker, TcService } from 'tailchat-server-sdk';
3+
import { generateOpenapiPath } from './generateOpenapiPath';
4+
import type { OpenAPIObject } from 'openapi3-ts/oas31';
5+
import SwaggerParser from '@apidevtools/swagger-parser';
6+
import fs from 'fs-extra';
7+
import path from 'path';
8+
import 'ts-node/register';
9+
10+
/**
11+
* https://ts-morph.com/setup/
12+
*/
13+
14+
/**
15+
* 扫描服务
16+
*/
17+
async function scanServices(): Promise<OpenAPIObject> {
18+
const packageJsonPath = path.resolve(__dirname, '../../../../package.json');
19+
const version = (await fs.readJson(packageJsonPath)).verson || '0.0.0';
20+
const serviceFiles = await globby(
21+
['./services/**/*.service.ts', './plugins/**/*.service.ts'],
22+
{
23+
absolute: true,
24+
}
25+
);
26+
27+
console.log('Service List:', serviceFiles);
28+
29+
const openapiObj: OpenAPIObject = {
30+
openapi: '3.1.0',
31+
info: {
32+
title: 'Tailchat Openapi',
33+
version,
34+
},
35+
paths: {},
36+
};
37+
const broker = new TcBroker({
38+
logger: false,
39+
});
40+
for (const servicePath of serviceFiles) {
41+
const { default: serviceCls } = await import(servicePath);
42+
43+
if (TcService.prototype.isPrototypeOf(serviceCls.prototype)) {
44+
const service: TcService = new serviceCls(broker);
45+
46+
openapiObj.paths = {
47+
...openapiObj.paths,
48+
...generateOpenapiPath(service),
49+
};
50+
}
51+
}
52+
broker.stop();
53+
54+
try {
55+
await SwaggerParser.validate(openapiObj as any);
56+
57+
return openapiObj;
58+
} catch (err) {
59+
console.error(err);
60+
}
61+
}
62+
63+
scanServices().then(async (openapiObj) => {
64+
await fs.writeJSON('./openapi.json', openapiObj, {
65+
spaces: 2,
66+
});
67+
console.log(
68+
'generate completed, if process not exist auto, you can exit it by yourself'
69+
);
70+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"declaration": true,
5+
"outDir": "./dist",
6+
"paths": {}
7+
},
8+
"include": ["./src/**/*"],
9+
"exclude": ["./node_modules/**/*", "./dist/**/*"]
10+
}

server/packages/sdk/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tailchat-server-sdk",
3-
"version": "0.0.17",
3+
"version": "0.0.18",
44
"description": "",
55
"main": "dist/index.js",
66
"bin": {
@@ -10,7 +10,7 @@
1010
"build": "tsc",
1111
"watch": "tsc --watch",
1212
"test": "echo \"Error: no test specified\" && exit 1",
13-
"prepare": "pnpm build",
13+
"prepare": "tsc",
1414
"release": "npm version patch && npm publish --registry https://registry.npmjs.com/"
1515
},
1616
"repository": {

server/packages/sdk/src/services/base.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,26 @@ export abstract class TcService extends Service {
197197
};
198198
}
199199

200+
/**
201+
* 获取服务操作列表
202+
*/
203+
getActionList() {
204+
return Object.entries(this._actions).map(
205+
([name, schema]: [string, ServiceActionSchema]) => {
206+
return {
207+
name,
208+
params: _.mapValues(schema.params, (type) => {
209+
if (typeof type === 'string') {
210+
return { type: type };
211+
} else {
212+
return type;
213+
}
214+
}),
215+
};
216+
}
217+
);
218+
}
219+
200220
registerMixin(mixin: Partial<ServiceSchema>): void {
201221
this._mixins.push(mixin);
202222
}
@@ -423,6 +443,10 @@ export abstract class TcService extends Service {
423443
* NOTICE: 这里使用Redis作为缓存管理器,因此不需要通知所有的service
424444
*/
425445
async cleanActionCache(actionName: string, keys: string[] = []) {
446+
if (!this.broker.cacher) {
447+
console.error('Can not clean cache because no cacher existed.');
448+
}
449+
426450
if (keys.length === 0) {
427451
await this.broker.cacher.clean(`${this.serviceName}.${actionName}`);
428452
} else {

0 commit comments

Comments
 (0)