Skip to content

Commit

Permalink
fix(server): fix compile function api params schema error (#540)
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow authored Dec 14, 2022
1 parent 3a2669e commit 37c049c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
10 changes: 10 additions & 0 deletions server/src/function/dto/compile-function.dto.ts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ApiProperty } from '@nestjs/swagger'
import { IsNotEmpty } from 'class-validator'

export class CompileFunctionDto {
@ApiProperty({
description: 'The source code of the function',
})
@IsNotEmpty()
code: string
}
13 changes: 11 additions & 2 deletions server/src/function/function.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { JwtAuthGuard } from '../auth/jwt.auth.guard'
import { ApplicationAuthGuard } from '../auth/application.auth.guard'
import { FunctionService } from './function.service'
import { IRequest } from '../utils/types'
import { CompileFunctionDto } from './dto/compile-function.dto.ts'

@ApiTags('Function')
@ApiBearerAuth('Authorization')
Expand Down Expand Up @@ -154,13 +155,21 @@ export class FunctionController {
@ApiOperation({ summary: 'Compile a function ' })
@UseGuards(JwtAuthGuard, ApplicationAuthGuard)
@Post(':name/compile')
async compile(@Param('appid') appid: string, @Param('name') name: string) {
async compile(
@Param('appid') appid: string,
@Param('name') name: string,
@Body() dto: CompileFunctionDto,
) {
if (!dto.code) {
return ResponseUtil.error('code is required')
}

const func = await this.functionsService.findOne(appid, name)
if (!func) {
throw new HttpException('function not found', HttpStatus.NOT_FOUND)
}

const res = this.functionsService.compile(func)
const res = await this.functionsService.compile(func, dto)
return res
}
}
21 changes: 15 additions & 6 deletions server/src/function/function.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { CreateFunctionDto } from './dto/create-function.dto'
import { UpdateFunctionDto } from './dto/update-function.dto'
import * as assert from 'node:assert'
import { JwtService } from '@nestjs/jwt'
import { CompileFunctionDto } from './dto/compile-function.dto.ts'

@Injectable()
export class FunctionService {
Expand Down Expand Up @@ -108,10 +109,18 @@ export class FunctionService {
}
}

compile(func: CloudFunction) {
const code = func.source.code
func.source.compiled = compileTs2js(code)
return func
async compile(func: CloudFunction, dto: CompileFunctionDto) {
const data: CloudFunction = {
...func,
source: {
...func.source,
code: dto.code,
compiled: compileTs2js(dto.code),
version: func.source.version + 1,
},
updatedAt: new Date(),
}
return data
}

async getDebugFunctionToken(appid: string) {
Expand All @@ -135,7 +144,7 @@ export class FunctionService {
return token
}

async findLogs(
async getLogs(
appid: string,
params: {
page: number
Expand All @@ -151,7 +160,7 @@ export class FunctionService {
const coll = db.collection(CN_FUNCTION_LOGS)
const query: any = {}
if (requestId) {
query.requestId = requestId
query.request_id = requestId
}
if (functionName) {
query.func = functionName
Expand Down
2 changes: 1 addition & 1 deletion server/src/log/log.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class LogController {
page = page || 1
limit = limit || 10

const res = await this.funcService.findLogs(appid, {
const res = await this.funcService.getLogs(appid, {
requestId,
functionName,
limit,
Expand Down

0 comments on commit 37c049c

Please sign in to comment.