fix(evolution): проверять настройки и подтверждение сохранённых полей
This commit is contained in:
1 parent
2215007c91
commit
2b62e0e956
8 files changed
+73
-1
No files matched your search
@@ -14,6 +14,10 @@
|
||||
"types": "./dist/index.d.ts",
|
||||
"default": "./dist/index.js"
|
||||
},
|
||||
"./evobgp": {
|
||||
"types": "./dist/evobgp.d.ts",
|
||||
"default": "./dist/evobgp.js"
|
||||
},
|
||||
"./servers": {
|
||||
"types": "./dist/servers.d.ts",
|
||||
"default": "./dist/servers.js"
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import {z} from "zod"
|
||||
export function normalizeEvoBgpBaseUrl(raw:string):string {
|
||||
const value=raw.trim()
|
||||
if(!value)return ""
|
||||
let url:URL
|
||||
try{url=new URL(/^[a-z][a-z0-9+.-]*:\/\//i.test(value)?value:"https://"+value)}catch{throw new Error("Укажите корректный URL EvoBGP")}
|
||||
if(!["http:","https:"].includes(url.protocol)||!url.hostname||url.username||url.password||url.pathname!=="/"||url.search||url.hash)throw new Error("Укажите HTTP(S) URL EvoBGP без пути, ключа, query и fragment")
|
||||
return url.origin
|
||||
}
|
||||
export function normalizeEvoBgpApiKey(raw:string):string {return raw.trim().replace(/^Bearer\s+/i,"").trim()}
|
||||
const baseInput=z.string().transform((raw,ctx)=>{try{return normalizeEvoBgpBaseUrl(raw)}catch(error){ctx.addIssue({code:"custom",message:error instanceof Error?error.message:"Неверный URL EvoBGP"});return z.NEVER}})
|
||||
const keyInput=z.string().refine(raw=>!/[\r\n]/.test(raw),"API-ключ должен быть одной строкой").transform(normalizeEvoBgpApiKey)
|
||||
export const evoBgpSettingsSchema=z.object({baseUrl:z.string().refine(value=>{try{return value===normalizeEvoBgpBaseUrl(value)}catch{return false}},"Некорректный сохранённый URL EvoBGP"),enabled:z.boolean(),secretConfigured:z.boolean()}).strict()
|
||||
export const evoBgpSettingsPatchSchema=z.object({baseUrl:baseInput.optional(),enabled:z.boolean().optional(),apiKey:z.union([keyInput,z.null()]).optional()}).strict().refine(patch=>Object.values(patch).some(value=>value!==undefined),"Укажите изменённые настройки EvoBGP")
|
||||
export const evoBgpTestDraftSchema=z.object({baseUrl:baseInput.optional(),apiKey:keyInput.optional()}).strict()
|
||||
export const evoBgpSaveReceiptSchema=evoBgpSettingsSchema.extend({ok:z.literal(true),savedFields:z.array(z.enum(["baseUrl","enabled","apiKey"])).min(1).refine(fields=>new Set(fields).size===fields.length)})
|
||||
export type EvoBgpSettingsDto=z.infer<typeof evoBgpSettingsSchema>
|
||||
export type EvoBgpSavePayload=z.input<typeof evoBgpSettingsPatchSchema>
|
||||
export type EvoBgpTestDraft=z.input<typeof evoBgpTestDraftSchema>
|
||||
@@ -11,3 +11,5 @@ export * from "./geoip.js"
|
||||
export * from "./statistics.js"
|
||||
|
||||
export * from "./ospf-optimizer.js"
|
||||
|
||||
export * from "./evobgp.js"
|
||||
Reference in new issue
Block a user