From a1f67aefaab8bd1a80b70f573cbe910dcee404d5 Mon Sep 17 00:00:00 2001 From: maslow Date: Tue, 10 Jan 2023 11:28:31 +0800 Subject: [PATCH] feat(server): add update-deps api (#613) --- server/package.json | 2 +- .../src/dependency/dependency.controller.ts | 25 ++++++++++++++-- server/src/dependency/dependency.service.ts | 29 +++++++++++++++++++ .../dependency/dto/update-dependency.dto.ts | 14 +++++++++ 4 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 server/src/dependency/dto/update-dependency.dto.ts diff --git a/server/package.json b/server/package.json index 242220326b..4203eaadfe 100644 --- a/server/package.json +++ b/server/package.json @@ -2,7 +2,7 @@ "name": "laf-server", "version": "1.0.0-alpha.0", "description": "", - "author": "maslow(wangfugen@!26.com)", + "author": "maslow(wangfugen@126.com)", "private": true, "license": "UNLICENSED", "scripts": { diff --git a/server/src/dependency/dependency.controller.ts b/server/src/dependency/dependency.controller.ts index 1c6b91c790..97cb1444a1 100644 --- a/server/src/dependency/dependency.controller.ts +++ b/server/src/dependency/dependency.controller.ts @@ -5,6 +5,7 @@ import { Get, Logger, Param, + Patch, Post, UseGuards, } from '@nestjs/common' @@ -20,6 +21,7 @@ import { JwtAuthGuard } from '../auth/jwt.auth.guard' import { ResponseUtil } from '../utils/response' import { DependencyService } from './dependency.service' import { CreateDependencyDto } from './dto/create-dependency.dto' +import { UpdateDependencyDto } from './dto/update-dependency.dto' @ApiTags('Application') @ApiBearerAuth('Authorization') @@ -30,13 +32,13 @@ export class DependencyController { constructor(private readonly depsService: DependencyService) {} /** - * Add a dependency to an app + * Add application dependencies * @param appid * @param dto * @returns */ @ApiResponse({ type: ResponseUtil }) - @ApiOperation({ summary: 'Add a dependency' }) + @ApiOperation({ summary: 'Add application dependencies' }) @UseGuards(JwtAuthGuard, ApplicationAuthGuard) @Post() @ApiBody({ type: [CreateDependencyDto] }) @@ -45,6 +47,25 @@ export class DependencyController { return ResponseUtil.ok(res) } + /** + * Update application dependencies + * @param appid + * @param dto + * @returns + */ + @ApiResponse({ type: ResponseUtil }) + @ApiOperation({ summary: 'Update application dependencies' }) + @UseGuards(JwtAuthGuard, ApplicationAuthGuard) + @Patch() + @ApiBody({ type: [UpdateDependencyDto] }) + async update( + @Param('appid') appid: string, + @Body() dto: UpdateDependencyDto[], + ) { + const res = await this.depsService.update(appid, dto) + return ResponseUtil.ok(res) + } + /** * Get application dependencies * @param appid diff --git a/server/src/dependency/dependency.service.ts b/server/src/dependency/dependency.service.ts index a00fc986a8..ee2d1085af 100644 --- a/server/src/dependency/dependency.service.ts +++ b/server/src/dependency/dependency.service.ts @@ -3,6 +3,7 @@ import { PrismaService } from 'src/prisma.service' import { RUNTIME_BUILTIN_DEPENDENCIES } from 'src/runtime-builtin-deps' import * as npa from 'npm-package-arg' import { CreateDependencyDto } from './dto/create-dependency.dto' +import { UpdateDependencyDto } from './dto/update-dependency.dto' export class Dependency { name: string @@ -66,6 +67,34 @@ export class DependencyService { return true } + /** + * Update the dependencies' version + */ + async update(appid: string, dto: UpdateDependencyDto[]) { + const extras = await this.getExtras(appid) + + // check if the dependency name all valid + const names = extras.map((dep) => npa(dep).name) + const input_names = dto.map((dep) => npa(dep.name).name) + const has_invalid = input_names.some((name) => !names.includes(name)) + if (has_invalid) return false + + // update + const new_deps = dto.map((dep) => `${dep.name}@${dep.spec}`) + const filtered = extras.filter((dep) => { + const { name } = npa(dep) + return !input_names.includes(name) + }) + + const deps = filtered.concat(new_deps) + await this.prisma.applicationConfiguration.update({ + where: { appid }, + data: { dependencies: deps }, + }) + + return true + } + async remove(appid: string, name: string) { const deps = await this.getExtras(appid) const filtered = deps.filter((dep) => { diff --git a/server/src/dependency/dto/update-dependency.dto.ts b/server/src/dependency/dto/update-dependency.dto.ts new file mode 100644 index 0000000000..c592c21c56 --- /dev/null +++ b/server/src/dependency/dto/update-dependency.dto.ts @@ -0,0 +1,14 @@ +import { ApiProperty } from '@nestjs/swagger' +import { IsNotEmpty, Length } from 'class-validator' + +export class UpdateDependencyDto { + @ApiProperty() + @IsNotEmpty() + @Length(1, 64) + name: string + + @ApiProperty() + @IsNotEmpty() + @Length(1, 64) + spec: string +}