-
-
Notifications
You must be signed in to change notification settings - Fork 682
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): add enviroments apis (#605)
* feat(server): add enviroments apis * chore: fix changelog links
- Loading branch information
Showing
6 changed files
with
875 additions
and
698 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { ApiProperty } from '@nestjs/swagger' | ||
import { IsNotEmpty, IsString, Length } from 'class-validator' | ||
|
||
export class CreateEnvironmentDto { | ||
@ApiProperty() | ||
@IsNotEmpty() | ||
@IsString() | ||
@Length(1, 64) | ||
name: string | ||
|
||
@ApiProperty() | ||
@Length(0, 4096) | ||
@IsString() | ||
value: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Delete, | ||
Get, | ||
Logger, | ||
Param, | ||
Post, | ||
UseGuards, | ||
} from '@nestjs/common' | ||
import { | ||
ApiBearerAuth, | ||
ApiOperation, | ||
ApiResponse, | ||
ApiTags, | ||
} from '@nestjs/swagger' | ||
import { ApplicationAuthGuard } from 'src/auth/application.auth.guard' | ||
import { JwtAuthGuard } from 'src/auth/jwt.auth.guard' | ||
import { ResponseUtil } from 'src/utils/response' | ||
import { EnvironmentVariableService } from './environment.service' | ||
import { CreateEnvironmentDto } from './dto/create-env.dto' | ||
|
||
@ApiTags('Application') | ||
@ApiBearerAuth('Authorization') | ||
@Controller('apps/:appid/environments') | ||
export class EnvironmentVariableController { | ||
private readonly logger = new Logger(EnvironmentVariableController.name) | ||
|
||
constructor(private readonly confService: EnvironmentVariableService) {} | ||
|
||
/** | ||
* Set a environment variable | ||
* @param appid | ||
* @param dto | ||
* @returns | ||
*/ | ||
@ApiResponse({ type: ResponseUtil }) | ||
@ApiOperation({ summary: 'Set a environment variable (create/update)' }) | ||
@UseGuards(JwtAuthGuard, ApplicationAuthGuard) | ||
@Post() | ||
async add(@Param('appid') appid: string, @Body() dto: CreateEnvironmentDto) { | ||
const res = await this.confService.set(appid, dto) | ||
return ResponseUtil.ok(res) | ||
} | ||
|
||
/** | ||
* Get environment variables | ||
* @param appid | ||
* @returns | ||
*/ | ||
@ApiResponse({ type: ResponseUtil }) | ||
@ApiOperation({ summary: 'Get environment variables' }) | ||
@UseGuards(JwtAuthGuard, ApplicationAuthGuard) | ||
@Get() | ||
async get(@Param('appid') appid: string) { | ||
const res = await this.confService.find(appid) | ||
return ResponseUtil.ok(res) | ||
} | ||
|
||
/** | ||
* Delete an environment variable by name | ||
* @param appid | ||
* @param name | ||
* @returns | ||
*/ | ||
@ApiResponse({ type: ResponseUtil }) | ||
@ApiOperation({ summary: 'Delete an environment variable by name' }) | ||
@UseGuards(JwtAuthGuard, ApplicationAuthGuard) | ||
@Delete(':name') | ||
async delete(@Param('appid') appid: string, @Param('name') name: string) { | ||
const res = await this.confService.delete(appid, name) | ||
return ResponseUtil.ok(res) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Injectable, Logger } from '@nestjs/common' | ||
import { PrismaService } from 'src/prisma.service' | ||
import { CreateEnvironmentDto } from './dto/create-env.dto' | ||
|
||
@Injectable() | ||
export class EnvironmentVariableService { | ||
private readonly logger = new Logger(EnvironmentVariableService.name) | ||
|
||
constructor(private readonly prisma: PrismaService) {} | ||
|
||
/** | ||
* if exists, update, else create | ||
* @param appid | ||
* @param dto | ||
*/ | ||
async set(appid: string, dto: CreateEnvironmentDto) { | ||
const origin = await this.find(appid) | ||
// check if exists | ||
const exists = origin.find((item) => item.name === dto.name) | ||
if (exists) { | ||
exists.value = dto.value | ||
} else { | ||
origin.push(dto) | ||
} | ||
|
||
const res = await this.prisma.applicationConfiguration.update({ | ||
where: { appid }, | ||
data: { | ||
environments: { | ||
set: origin, | ||
}, | ||
}, | ||
}) | ||
|
||
return res.environments | ||
} | ||
|
||
async find(appid: string) { | ||
const res = await this.prisma.applicationConfiguration.findUnique({ | ||
where: { | ||
appid, | ||
}, | ||
}) | ||
|
||
return res.environments | ||
} | ||
|
||
async delete(appid: string, name: string) { | ||
const res = await this.prisma.applicationConfiguration.update({ | ||
where: { | ||
appid, | ||
}, | ||
data: { | ||
environments: { | ||
deleteMany: { | ||
where: { | ||
name, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
return res | ||
} | ||
} |