feat: support json5 for snippet

This commit is contained in:
Innei
2022-06-19 13:22:31 +08:00
parent 5835d4f311
commit f359a178aa
5 changed files with 24 additions and 1 deletions

View File

@@ -1,15 +1,21 @@
import JSON5 from 'json5'
declare global {
interface JSON {
safeParse: typeof JSON.parse
JSON5: typeof JSON5
}
}
export const registerJSONGlobal = () => {
JSON.safeParse = (...rest) => {
try {
return JSON.parse(...rest)
return JSON5.parse(...rest)
} catch (error) {
return null
}
}
JSON.JSON5 = JSON5
}

View File

@@ -15,6 +15,7 @@ import { BaseModel } from '~/shared/model/base.model'
export enum SnippetType {
JSON = 'json',
JSON5 = 'json5',
Function = 'function',
Text = 'text',
YAML = 'yaml',

View File

@@ -1,4 +1,5 @@
import { load } from 'js-yaml'
import JSON5 from 'json5'
import {
BadRequestException,
@@ -77,6 +78,14 @@ export class SnippetService {
}
break
}
case SnippetType.JSON5: {
try {
JSON5.parse(model.raw)
} catch {
throw new BadRequestException('content is not valid json5')
}
break
}
case SnippetType.YAML: {
try {
load(model.raw)
@@ -139,6 +148,10 @@ export class SnippetService {
Reflect.set(model, 'data', JSON.parse(model.raw))
break
}
case SnippetType.JSON5: {
Reflect.set(model, 'data', JSON5.parse(model.raw))
break
}
case SnippetType.YAML: {
Reflect.set(model, 'data', load(model.raw))
break