Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api-client): auto-generated js api-client sdk #852

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# compiled output
/dist
/node_modules
dist
node_modules
data
# Logs
logs
Expand Down Expand Up @@ -51,4 +51,8 @@ dump.rdb
tmp

#eslint
.eslintcache
.eslintcache

#nestia
lib
/api-client
1 change: 1 addition & 0 deletions apps/config-service/src/config-service.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { DefaultConfigs } from './config.default';
import { ConfigsInterface, ConfigsInterfaceKeys } from './config.interface';
import { ConfigModel } from './config.model';
import * as configDto from './config.dto';
import { sleep } from 'zx-cjs';

const allOptionKeys: Set<ConfigsInterfaceKeys> = new Set();
const map: Record<string, any> = Object.entries(configDto).reduce(
Expand Down
81 changes: 37 additions & 44 deletions apps/core/src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import {
Body,
Controller,
Delete,
Get,
Inject,
Param,
Patch,
Post,
Put,
Query,
} from '@nestjs/common';
import { AppService } from './app.service';
import PKG from '../../../package.json';
Expand Down Expand Up @@ -55,45 +48,45 @@ export class AppController {
);
}

@Get('/*')
@ApiOperation({ summary: '自定义 Path (GET)' })
async customGetPath(@Query() query: any, @Param() param: any) {
return this.appService.transformCustomPath('GET', query, param);
}
// @Get('/*')
// @ApiOperation({ summary: '自定义 Path (GET)' })
// async customGetPath(@Query() query: any, @Param() param: any) {
// return this.appService.transformCustomPath('GET', query, param);
// }

@Post('/*')
@ApiOperation({ summary: '自定义 Path (POST)' })
async customPostPath(
@Query() query: any,
@Param() param: any,
@Body() body: any,
) {
return this.appService.transformCustomPath('POST', query, param, body);
}
// @Post('/*')
// @ApiOperation({ summary: '自定义 Path (POST)' })
// async customPostPath(
// @Query() query: any,
// @Param() param: any,
// @Body() body: any,
// ) {
// return this.appService.transformCustomPath('POST', query, param, body);
// }

@Put('/*')
@ApiOperation({ summary: '自定义 Path (PUT)' })
async customPutPath(
@Query() query: any,
@Param() param: any,
@Body() body: any,
) {
return this.appService.transformCustomPath('PUT', query, param, body);
}
// @Put('/*')
// @ApiOperation({ summary: '自定义 Path (PUT)' })
// async customPutPath(
// @Query() query: any,
// @Param() param: any,
// @Body() body: any,
// ) {
// return this.appService.transformCustomPath('PUT', query, param, body);
// }

@Patch('/*')
@ApiOperation({ summary: '自定义 Path (PATCH)' })
async customPatchPath(
@Query() query: any,
@Param() param: any,
@Body() body: any,
) {
return this.appService.transformCustomPath('PATCH', query, param, body);
}
// @Patch('/*')
// @ApiOperation({ summary: '自定义 Path (PATCH)' })
// async customPatchPath(
// @Query() query: any,
// @Param() param: any,
// @Body() body: any,
// ) {
// return this.appService.transformCustomPath('PATCH', query, param, body);
// }

@Delete('/*')
@ApiOperation({ summary: '自定义 Path (DELETE)' })
async customDeletePath(@Query() query: any, @Param() param: any) {
return this.appService.transformCustomPath('DELETE', query, param);
}
// @Delete('/*')
// @ApiOperation({ summary: '自定义 Path (DELETE)' })
// async customDeletePath(@Query() query: any, @Param() param: any) {
// return this.appService.transformCustomPath('DELETE', query, param);
// }
}
2 changes: 1 addition & 1 deletion apps/core/src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { join } from 'path';
import { cwd } from '@shared/global/env.global';
import { ServicesEnum } from '~/shared/constants/services.constant';
import { readFileSync } from 'fs';
import { YAML } from 'zx-cjs';
import { YAML, fs } from 'zx-cjs';
import { transportReqToMicroservice } from '~/shared/microservice.transporter';

interface IEventConfigItem {
Expand Down
1 change: 1 addition & 0 deletions apps/core/src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { isDev } from '@shared/global/env.global';
import { consola } from '~/shared/global/consola.global';
import { getEnv } from '~/shared/utils/rag-env';
import { ServicesEnum } from '~/shared/constants/services.constant';
import { argv, chalk } from 'zx-cjs';

const Origin = CROSS_DOMAIN.allowedOrigins;

Expand Down
21 changes: 17 additions & 4 deletions apps/core/src/modules/aggregate/aggregate.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ export class AggregateController {
@ApiOperation({ summary: '获取概要' })
@CacheKey(CacheKeys.AggregateCatch)
@CacheTTL(300)
async aggregate() {
async aggregate(): Promise<{
categories: any;
pageMeta: any;
site: any;
}> {
const tasks = await Promise.allSettled([
// this.configService.getMaster(),
this.aggregateService.getAllCategory(),
Expand Down Expand Up @@ -70,7 +74,12 @@ export class AggregateController {
@ApiOperation({ summary: '获取网站sitemap' })
@CacheKey(CacheKeys.SiteMapCatch)
@CacheTTL(3600)
async getSiteMapContent() {
async getSiteMapContent(): Promise<{
data: {
url: URL;
published_at: Date;
}[];
}> {
return { data: await this.aggregateService.getSiteMapContent() };
}

Expand All @@ -84,8 +93,12 @@ export class AggregateController {

@Get('/stat')
@ApiOperation({ summary: '获取网站统计信息' })
// @Auth()
async stat() {
@Auth()
async stat(): Promise<{
posts: number;
pages: number;
categories: number;
}> {
const [count] = await Promise.all([this.aggregateService.getCounts()]);
return {
...count,
Expand Down
11 changes: 6 additions & 5 deletions apps/core/src/modules/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import {
Query,
} from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';

import { MongoIdDto } from '~/shared/dto/id.dto';
import { AuthService } from '~/libs/auth/src';
import { Auth } from '~/shared/common/decorator/auth.decorator';
import { ApiName } from '~/shared/common/decorator/openapi.decorator';
Expand Down Expand Up @@ -54,7 +52,11 @@ export class AuthController {

@Post('token')
@Auth()
async generateToken(@Body() body: TokenDto) {
async generateToken(@Body() body: TokenDto): Promise<{
expired: Date | undefined;
token: string;
name: string;
}> {
const { expired, name } = body;
const token = await this.authService.generateAccessToken();
const model = {
Expand All @@ -67,8 +69,7 @@ export class AuthController {
}
@Delete('token')
@Auth()
async deleteToken(@Query() query: MongoIdDto) {
const { id } = query;
async deleteToken(@Query('id') id: string) {
const token = await this.authService
.getAllAccessToken()
.then((models) =>
Expand Down
17 changes: 8 additions & 9 deletions apps/core/src/modules/category/category.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { ApiBody, ApiOperation, ApiParam, ApiQuery } from '@nestjs/swagger';
import {
MultiCategoriesQueryDto,
MultiQueryTagAndCategoryDto,
SlugorIdDto,
} from '~/apps/page-service/src/dto/category.dto';
import {
CategoryModel,
Expand All @@ -38,7 +37,6 @@ import { Auth } from '~/shared/common/decorator/auth.decorator';
import { ApiName } from '~/shared/common/decorator/openapi.decorator';
import { CategoryEvents } from '~/shared/constants/event.constant';
import { ServicesEnum } from '~/shared/constants/services.constant';
import { MongoIdDto } from '~/shared/dto/id.dto';
import { transportReqToMicroservice } from '~/shared/microservice.transporter';

@Controller('category')
Expand Down Expand Up @@ -87,9 +85,10 @@ export class CategoryController {
required: false, // 是否必填
})
async getCategoryByCategoryIdOrTag(
@Param() { query }: SlugorIdDto,
@Query() { tag }: MultiQueryTagAndCategoryDto, // 如果这个是标签,则tag为true,如果是分类,则tag为分类id
@Query() _query: MultiQueryTagAndCategoryDto, // 如果这个是标签,则tag为true,如果是分类,则tag为分类id
@Param("query") query: string,
) {
const { tag } = _query;
return transportReqToMicroservice<
| {
isTag: boolean;
Expand All @@ -104,7 +103,7 @@ export class CategoryController {
};
isTag: boolean;
}
>(this.category, CategoryEvents.CategoryGet, { _query: query, _tag: tag });
>(this.category, CategoryEvents.CategoryGet, { query, _tag: tag });
}

@Post('/')
Expand Down Expand Up @@ -134,7 +133,7 @@ export class CategoryController {
@ApiParam({ name: 'id', description: '分类id' })
@ApiBody({ description: '分类信息', type: CategoryModel })
async update(
@Param() { id }: MongoIdDto,
@Param("id") id: string,
@Body() { type, slug, name }: CategoryModel,
) {
const send = {
Expand All @@ -152,19 +151,19 @@ export class CategoryController {
@ApiOperation({ summary: '更新分类' })
@Auth()
@HttpCode(204)
async patch(@Param() params: MongoIdDto, @Body() body: PartialCategoryModel) {
async patch(@Param("id") id: string, @Body() body: PartialCategoryModel) {
return transportReqToMicroservice<CategoryModel>(
this.category,
CategoryEvents.CategoryPatch,
{ _id: params.id, _data: body },
{ _id: id, _data: body },
);
}

@Delete('/:id')
@ApiOperation({ summary: '删除分类' })
@Auth()
@ApiParam({ name: 'id', description: '分类id' })
async deleteCategory(@Param() { id }: MongoIdDto) {
async deleteCategory(@Param("id") id: string) {
return transportReqToMicroservice<DeleteResult>(
this.category,
CategoryEvents.CategoryDelete,
Expand Down
2 changes: 1 addition & 1 deletion apps/core/src/modules/console/console.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class ConsoleController {
return true;
}

@Get(['/*', '/'])
// @Get(['/*', '/'])
async console(@Res() res: FastifyReply, @Req() req: FastifyRequest) {
const path = req.url
.replace('/console', '')
Expand Down
6 changes: 3 additions & 3 deletions apps/core/src/modules/friends/friends.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,20 @@ export class FriendsController {
@ApiOperation({ summary: '更新友链' })
async updateFriendStatus(
@Param('id') id: string,
@Body('status') status: FriendStatus,
@Body() body: { status: FriendStatus },
) {
return transportReqToMicroservice(
this.friends,
FriendsEvents.FriendPatchStatusByMaster,
{ id, status },
{ id, status: body.status },
);
}

@Delete('/:id')
@ApiOperation({ summary: '删除友链' })
async deleteFriend(
@Param('id') id: string,
@Body('token') token: string,
@Body() { token }: { token?: string },
@IsMaster() isMaster: boolean,
) {
return transportReqToMicroservice(
Expand Down
22 changes: 18 additions & 4 deletions apps/core/src/modules/migrate/migrate.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,37 @@ import { ApiOperation } from '@nestjs/swagger';
import { MigrateService } from './migrate.service';
import { MigrateData } from './migrate.interface';
import { ApiName } from '~/shared/common/decorator/openapi.decorator';
import { Auth } from '~/shared/common/decorator/auth.decorator';
import { CategoryModel } from '~/apps/page-service/src/model/category.model';

@Controller('migrate')
@ApiName
export class MigrateController {
constructor(private readonly migrateService: MigrateService) {}

@Post('import')
// @Auth()
@Auth()
@ApiOperation({ summary: '使用 JSON 导入数据' })
async import(@Body() body: MigrateData) {
async import(@Body() body: MigrateData): Promise<{
user: any;
friends: any;
pages: any;
categories: never[] | CategoryModel[];
posts: any;
comments:
| never[]
| {
postError: string[];
parentError: string[];
};
}> {
return await this.migrateService.import(body);
}

@Get('export')
// @Auth()
@Auth()
@ApiOperation({ summary: '导出数据为 JSON' })
async export() {
async export(): Promise<any> {
return await this.migrateService.export();
}
}
9 changes: 4 additions & 5 deletions apps/core/src/modules/page/page.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import { ApiName } from '~/shared/common/decorator/openapi.decorator';
import { IsMaster } from '~/shared/common/decorator/role.decorator';
import { PageEvents } from '~/shared/constants/event.constant';
import { ServicesEnum } from '~/shared/constants/services.constant';
import { MongoIdDto } from '~/shared/dto/id.dto';
import { PagerDto } from '~/shared/dto/pager.dto';
import { transportReqToMicroservice } from '~/shared/microservice.transporter';

Expand Down Expand Up @@ -86,21 +85,21 @@ export class PageController {
@Patch('/:id')
@ApiOperation({ summary: '更新页面' })
@Auth()
async modify(@Body() body: PageModel, @Param() params: MongoIdDto) {
async modify(@Body() body: PageModel, @Param("id") id: string) {
return transportReqToMicroservice(this.page, PageEvents.PagePatch, {
id: params.id,
id,
body,
});
}

@Delete('/:id')
@ApiOperation({ summary: '删除页面' })
@Auth()
async deletePage(@Param() params: MongoIdDto) {
async deletePage(@Param("id") id: string) {
return transportReqToMicroservice(
this.page,
PageEvents.PageDelete,
params.id,
id,
);
}
}
Loading