feat(snippet): yaml support

This commit is contained in:
Innei
2021-12-22 23:08:01 +08:00
parent 806ee16065
commit 99a2ef0dcf
4 changed files with 26 additions and 3 deletions

View File

@@ -134,6 +134,7 @@
"@types/html-minifier": "4.0.1",
"@types/ioredis": "4.28.5",
"@types/jest": "27.0.3",
"@types/js-yaml": "4.0.5",
"@types/lodash": "4.14.178",
"@types/marked": "4.0.1",
"@types/mongoose-paginate-v2": "1.4.1",

6
pnpm-lock.yaml generated
View File

@@ -29,6 +29,7 @@ specifiers:
'@types/html-minifier': 4.0.1
'@types/ioredis': 4.28.5
'@types/jest': 27.0.3
'@types/js-yaml': 4.0.5
'@types/lodash': 4.14.178
'@types/marked': 4.0.1
'@types/mongoose-paginate-v2': 1.4.1
@@ -178,6 +179,7 @@ devDependencies:
'@types/html-minifier': 4.0.1
'@types/ioredis': 4.28.5
'@types/jest': 27.0.3
'@types/js-yaml': 4.0.5
'@types/lodash': 4.14.178
'@types/marked': 4.0.1
'@types/mongoose-paginate-v2': 1.4.1
@@ -1958,6 +1960,10 @@ packages:
pretty-format: 27.3.1
dev: true
/@types/js-yaml/4.0.5:
resolution: {integrity: sha512-FhpRzf927MNQdRZP0J5DLIdTXhjLYzeUTmLAu69mnVksLH9CJY3IuSeEgbKUki7GQZm0WqDkGzyxju2EZGD2wA==}
dev: true
/@types/json-schema/7.0.9:
resolution: {integrity: sha512-qcUXuemtEu+E5wZSJHNxUXeCZhAfXKQ41D+duX+VYPde7xyEVZci+/oXKJL13tnRs9lR2pr4fod59GT6/X1/yQ==}
dev: true

View File

@@ -1,4 +1,5 @@
import { index, modelOptions, prop } from '@typegoose/typegoose'
import { Transform } from 'class-transformer'
import {
IsBoolean,
IsEnum,
@@ -14,6 +15,7 @@ export enum SnippetType {
JSON = 'json',
Function = 'function',
Text = 'text',
YAML = 'yaml',
}
@modelOptions({
@@ -42,6 +44,7 @@ export class SnippetModel extends BaseModel {
@prop({ require: true })
@IsString()
@IsNotEmpty()
@Transform(({ value }) => value.trim())
raw: string
@prop({ require: true, trim: true })

View File

@@ -1,7 +1,7 @@
import { BadRequestException, Injectable } from '@nestjs/common'
import { load } from 'js-yaml'
import { InjectModel } from 'nestjs-typegoose'
import { SnippetModel, SnippetType } from './snippet.model'
@Injectable()
export class SnippetService {
constructor(
@@ -45,12 +45,21 @@ export class SnippetService {
private async validateType(model: SnippetModel) {
switch (model.type) {
case SnippetType.JSON: {
const isValidJSON = JSON.parse(model.raw)
if (!isValidJSON) {
try {
JSON.parse(model.raw)
} catch {
throw new BadRequestException('content is not valid json')
}
break
}
case SnippetType.YAML: {
try {
load(model.raw)
} catch {
throw new BadRequestException('content is not valid yaml')
}
break
}
case SnippetType.Function:
// TODO
throw new BadRequestException(
@@ -86,6 +95,10 @@ export class SnippetService {
Reflect.set(model, 'data', JSON.parse(model.raw))
break
}
case SnippetType.YAML: {
Reflect.set(model, 'data', load(model.raw))
break
}
case SnippetType.Text: {
break
}