feat: support json5 for snippet
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import { BaseModel } from '~/shared/model/base.model'
|
||||
|
||||
export enum SnippetType {
|
||||
JSON = 'json',
|
||||
JSON5 = 'json5',
|
||||
Function = 'function',
|
||||
Text = 'text',
|
||||
YAML = 'yaml',
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user