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(server): add func debug params, add envs update api #1055

Merged
merged 1 commit into from
Apr 20, 2023
Merged
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
1 change: 1 addition & 0 deletions server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ model CloudFunction {
desc String
tags String[]
methods HttpMethod[]
params Json?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
createdBy String @db.ObjectId
Expand Down
38 changes: 34 additions & 4 deletions server/src/application/environment.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import {
Get,
Logger,
Param,
Patch,
Post,
UseGuards,
} from '@nestjs/common'
import {
ApiBearerAuth,
ApiBody,
ApiOperation,
ApiResponse,
ApiTags,
Expand All @@ -29,6 +31,34 @@ export class EnvironmentVariableController {

constructor(private readonly confService: EnvironmentVariableService) {}

/**
* Update environment variables (replace all)
* @param appid
* @param dto
* @returns
*/
@ApiResponse({ type: ResponseUtil })
@ApiOperation({ summary: 'Update environment variables (replace all)' })
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Post()
@ApiBody({
type: [CreateEnvironmentDto],
description: 'The environment variables',
})
async updateAll(
@Param('appid') appid: string,
@Body() dto: CreateEnvironmentDto[],
) {
// app secret can not missing or empty
const secret = dto.find((item) => item.name === APPLICATION_SECRET_KEY)
if (!secret || !secret.value) {
return ResponseUtil.error(APPLICATION_SECRET_KEY + ' can not be empty')
}

const res = await this.confService.updateAll(appid, dto)
return ResponseUtil.ok(res)
}

/**
* Set a environment variable
* @param appid
Expand All @@ -38,14 +68,14 @@ export class EnvironmentVariableController {
@ApiResponse({ type: ResponseUtil })
@ApiOperation({ summary: 'Set a environment variable (create/update)' })
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Post()
@Patch()
async add(@Param('appid') appid: string, @Body() dto: CreateEnvironmentDto) {
// can not set empty app secret
if (dto.name === APPLICATION_SECRET_KEY && !dto.value) {
return ResponseUtil.error(APPLICATION_SECRET_KEY + ' can not be empty')
}

const res = await this.confService.set(appid, dto)
const res = await this.confService.setOne(appid, dto)
return ResponseUtil.ok(res)
}

Expand All @@ -59,7 +89,7 @@ export class EnvironmentVariableController {
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Get()
async get(@Param('appid') appid: string) {
const res = await this.confService.find(appid)
const res = await this.confService.findAll(appid)
return ResponseUtil.ok(res)
}

Expand All @@ -79,7 +109,7 @@ export class EnvironmentVariableController {
return ResponseUtil.error(APPLICATION_SECRET_KEY + ' can not be deleted')
}

const res = await this.confService.delete(appid, name)
const res = await this.confService.deleteOne(appid, name)
return ResponseUtil.ok(res)
}
}
41 changes: 17 additions & 24 deletions server/src/application/environment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,22 @@ export class EnvironmentVariableService {

constructor(private readonly prisma: PrismaService) {}

async updateAll(appid: string, dto: CreateEnvironmentDto[]) {
const res = await this.prisma.applicationConfiguration.update({
where: { appid },
data: { environments: { set: dto } },
})

return res.environments
}

/**
* if exists, update, else create
* @param appid
* @param dto
*/
async set(appid: string, dto: CreateEnvironmentDto) {
const origin = await this.find(appid)
async setOne(appid: string, dto: CreateEnvironmentDto) {
const origin = await this.findAll(appid)
// check if exists
const exists = origin.find((item) => item.name === dto.name)
if (exists) {
Expand All @@ -25,40 +34,24 @@ export class EnvironmentVariableService {

const res = await this.prisma.applicationConfiguration.update({
where: { appid },
data: {
environments: {
set: origin,
},
},
data: { environments: { set: origin } },
})

return res.environments
}

async find(appid: string) {
async findAll(appid: string) {
const res = await this.prisma.applicationConfiguration.findUnique({
where: {
appid,
},
where: { appid },
})

return res.environments
}

async delete(appid: string, name: string) {
async deleteOne(appid: string, name: string) {
const res = await this.prisma.applicationConfiguration.update({
where: {
appid,
},
data: {
environments: {
deleteMany: {
where: {
name,
},
},
},
},
where: { appid },
data: { environments: { deleteMany: { where: { name } } } },
})

return res
Expand Down
13 changes: 12 additions & 1 deletion server/src/function/dto/update-function.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'
import { HttpMethod } from '@prisma/client'
import { IsArray, IsIn, IsNotEmpty, IsString, MaxLength } from 'class-validator'
import {
IsArray,
IsIn,
IsNotEmpty,
IsOptional,
IsString,
MaxLength,
} from 'class-validator'
import { HTTP_METHODS } from '../../constants'

export class UpdateFunctionDto {
Expand All @@ -25,6 +32,10 @@ export class UpdateFunctionDto {
@IsNotEmpty({ each: true })
tags: string[]

@ApiPropertyOptional()
@IsOptional()
params: any

validate() {
return null
}
Expand Down
1 change: 1 addition & 0 deletions server/src/function/function.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export class FunctionService {
desc: dto.description,
methods: dto.methods,
tags: dto.tags || [],
params: dto.params,
}
const res = await this.prisma.cloudFunction.update({
where: { appid_name: { appid: func.appid, name: func.name } },
Expand Down