Skip to content

Commit

Permalink
feat(server): add update-deps api (#613)
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow authored Jan 10, 2023
1 parent 5e3789c commit a1f67ae
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
25 changes: 23 additions & 2 deletions server/src/dependency/dependency.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Get,
Logger,
Param,
Patch,
Post,
UseGuards,
} from '@nestjs/common'
Expand All @@ -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')
Expand All @@ -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] })
Expand All @@ -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
Expand Down
29 changes: 29 additions & 0 deletions server/src/dependency/dependency.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) => {
Expand Down
14 changes: 14 additions & 0 deletions server/src/dependency/dto/update-dependency.dto.ts
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit a1f67ae

Please sign in to comment.