Skip to content

Commit

Permalink
refactor: introduce Zod
Browse files Browse the repository at this point in the history
  • Loading branch information
ninoseki committed Mar 2, 2024
1 parent 7cecde3 commit f6b590d
Show file tree
Hide file tree
Showing 58 changed files with 519 additions and 448 deletions.
11 changes: 10 additions & 1 deletion frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"vue-concurrency": "^5.0.0",
"vue-json-pretty": "^2.3.0",
"vue-router": "^4.2.5",
"vue3-ace-editor": "^2.2.4"
"vue3-ace-editor": "^2.2.4",
"zod": "^3.22.4"
},
"devDependencies": {
"@redocly/cli": "1.7.0",
Expand Down
102 changes: 51 additions & 51 deletions frontend/src/api-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,120 +2,120 @@ import { type Task, useAsyncTask } from "vue-concurrency"

import { API } from "@/api"
import type {
Alert,
Alerts,
Artifact,
Artifacts,
Config,
CreateRule,
IPInfo,
Message,
QueueMessage,
Rule,
Rules,
SearchParams,
Tags,
UpdateRule
} from "@/types"

export function generateGetAlertsTask(): Task<Alerts, [SearchParams]> {
return useAsyncTask<Alerts, [SearchParams]>(async (_signal, params) => {
AlertsType,
AlertType,
ArtifactsType,
ArtifactType,
ConfigsType,
CreateRuleType,
IpInfoType,
MessageType,
QueueMessageType,
RulesType,
RuleType,
SearchParamsType,
TagsType,
UpdateRuleType
} from "@/schemas"

export function generateGetAlertsTask(): Task<AlertsType, [SearchParamsType]> {
return useAsyncTask<AlertsType, [SearchParamsType]>(async (_signal, params) => {
return await API.getAlerts(params)
})
}

export function generateDeleteAlertTask(): Task<Message, [number]> {
return useAsyncTask<Message, [number]>(async (_signal, id) => {
export function generateDeleteAlertTask(): Task<MessageType, [number]> {
return useAsyncTask<MessageType, [number]>(async (_signal, id) => {
return await API.deleteAlert(id)
})
}

export function generateGetTagsTask(): Task<Tags, []> {
return useAsyncTask<Tags, []>(async () => {
export function generateGetTagsTask(): Task<TagsType, []> {
return useAsyncTask<TagsType, []>(async () => {
return await API.getTags()
})
}

export function generateDeleteTagTask(): Task<Message, [number]> {
return useAsyncTask<Message, [number]>(async (_signal, tag) => {
export function generateDeleteTagTask(): Task<MessageType, [number]> {
return useAsyncTask<MessageType, [number]>(async (_signal, tag) => {
return await API.deleteTag(tag)
})
}

export function generateGetArtifactTask(): Task<Artifact, [number]> {
return useAsyncTask<Artifact, [number]>(async (_signal, id) => {
export function generateGetArtifactTask(): Task<ArtifactType, [number]> {
return useAsyncTask<ArtifactType, [number]>(async (_signal, id) => {
return await API.getArtifact(id)
})
}

export function generateDeleteArtifactTask(): Task<Message, [number]> {
return useAsyncTask<Message, [number]>(async (_signal, id) => {
export function generateDeleteArtifactTask(): Task<MessageType, [number]> {
return useAsyncTask<MessageType, [number]>(async (_signal, id) => {
return await API.deleteArtifact(id)
})
}

export function generateEnrichArtifactTask(): Task<QueueMessage, [number]> {
return useAsyncTask<QueueMessage, [number]>(async (_signal, id) => {
export function generateEnrichArtifactTask(): Task<QueueMessageType, [number]> {
return useAsyncTask<QueueMessageType, [number]>(async (_signal, id) => {
return await API.enrichArtifact(id)
})
}

export function generateGetConfigsTask(): Task<Config[], []> {
return useAsyncTask<Config[], []>(async () => {
export function generateGetConfigsTask(): Task<ConfigsType, []> {
return useAsyncTask<ConfigsType, []>(async () => {
return await API.getConfigs()
})
}

export function generateGetIPTask(): Task<IPInfo, [string]> {
return useAsyncTask<IPInfo, [string]>(async (_signal, ipAddress: string) => {
return await API.getIPInfo(ipAddress)
export function generateGetIPTask(): Task<IpInfoType, [string]> {
return useAsyncTask<IpInfoType, [string]>(async (_signal, ipAddress: string) => {
return await API.getIpInfo(ipAddress)
})
}

export function generateGetRulesTask(): Task<Rules, [SearchParams]> {
return useAsyncTask<Rules, [SearchParams]>(async (_signal, params: SearchParams) => {
export function generateGetRulesTask(): Task<RulesType, [SearchParamsType]> {
return useAsyncTask<RulesType, [SearchParamsType]>(async (_signal, params: SearchParamsType) => {
return await API.getRules(params)
})
}

export function generateGetRuleTask(): Task<Rule, [string]> {
return useAsyncTask<Rule, [string]>(async (_signal, id: string) => {
export function generateGetRuleTask(): Task<RuleType, [string]> {
return useAsyncTask<RuleType, [string]>(async (_signal, id: string) => {
return await API.getRule(id)
})
}

export function generateDeleteRuleTask(): Task<Message, [string]> {
return useAsyncTask<Message, [string]>(async (_signal, id: string) => {
export function generateDeleteRuleTask(): Task<MessageType, [string]> {
return useAsyncTask<MessageType, [string]>(async (_signal, id: string) => {
return await API.deleteRule(id)
})
}

export function generateSearchRuleTask(): Task<QueueMessage, [string]> {
return useAsyncTask<QueueMessage, [string]>(async (_signal, id) => {
export function generateSearchRuleTask(): Task<QueueMessageType, [string]> {
return useAsyncTask<QueueMessageType, [string]>(async (_signal, id) => {
return await API.searchRule(id)
})
}

export function generateCreateRuleTask(): Task<Rule, [CreateRule]> {
return useAsyncTask<Rule, [CreateRule]>(async (_signal, payload) => {
export function generateCreateRuleTask(): Task<RuleType, [CreateRuleType]> {
return useAsyncTask<RuleType, [CreateRuleType]>(async (_signal, payload) => {
return await API.createRule(payload)
})
}

export function generateUpdateRuleTask(): Task<Rule, [UpdateRule]> {
return useAsyncTask<Rule, [UpdateRule]>(async (_signal, payload) => {
export function generateUpdateRuleTask(): Task<RuleType, [UpdateRuleType]> {
return useAsyncTask<RuleType, [UpdateRuleType]>(async (_signal, payload) => {
return await API.updateRule(payload)
})
}

export function generateGetArtifactsTask(): Task<Artifacts, [SearchParams]> {
return useAsyncTask<Artifacts, [SearchParams]>(async (_signal, params) => {
export function generateGetArtifactsTask(): Task<ArtifactsType, [SearchParamsType]> {
return useAsyncTask<ArtifactsType, [SearchParamsType]>(async (_signal, params) => {
return await API.getArtifacts(params)
})
}

export function generateGetAlertTask(): Task<Alert, [number]> {
return useAsyncTask<Alert, [number]>(async (_signal, id) => {
export function generateGetAlertTask(): Task<AlertType, [number]> {
return useAsyncTask<AlertType, [number]>(async (_signal, id) => {
return await API.getAlert(id)
})
}
142 changes: 76 additions & 66 deletions frontend/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,116 +1,126 @@
import axios from "axios"

import type {
Alert,
Alerts,
Artifact,
Artifacts,
Config,
CreateRule,
IPInfo,
Message,
QueueMessage,
Rule,
Rules,
SearchParams,
Tags,
UpdateRule
} from "@/types"
import {
AlertSchema,
AlertsSchema,
type AlertsType,
type AlertType,
ArtifactSchema,
ArtifactsSchema,
type ArtifactsType,
type ArtifactType,
ConfigsSchema,
type ConfigsType,
type CreateRuleType,
type IpInfoType,
MessageSchema,
type MessageType,
QueueMessageSchema,
type QueueMessageType,
RuleSchema,
RulesSchema,
type RulesType,
type RuleType,
type SearchParamsType,
TagsSchema,
type TagsType,
type UpdateRuleType
} from "@/schemas"

const client = axios.create()

export const API = {
async getConfigs(): Promise<Config[]> {
const res = await client.get<Config[]>("/api/configs")
return res.data
async getConfigs(): Promise<ConfigsType> {
const res = await client.get("/api/configs")
return ConfigsSchema.parse(res.data)
},

async getAlerts(params: SearchParams): Promise<Alerts> {
async getAlerts(params: SearchParamsType): Promise<AlertsType> {
params.page = params.page || 1
const res = await client.get<Alerts>("/api/alerts", {
const res = await client.get("/api/alerts", {
params: params
})
return res.data
return AlertsSchema.parse(res.data)
},

async getAlert(id: number): Promise<Alert> {
const res = await client.get<Alert>(`/api/alerts/${id}`)
return res.data
async getAlert(id: number): Promise<AlertType> {
const res = await client.get(`/api/alerts/${id}`)
return AlertSchema.parse(res.data)
},

async getTags(): Promise<Tags> {
const res = await client.get<Tags>("/api/tags")
return res.data
async getTags(): Promise<TagsType> {
const res = await client.get("/api/tags")
return TagsSchema.parse(res.data)
},

async deleteAlert(id: number): Promise<Message> {
const res = await client.delete<Message>(`/api/alerts/${id}`)
return res.data
async deleteAlert(id: number): Promise<MessageType> {
const res = await client.delete(`/api/alerts/${id}`)
return MessageSchema.parse(res.data)
},

async getArtifact(id: number): Promise<Artifact> {
const res = await client.get<Artifact>(`/api/artifacts/${id}`)
return res.data
async getArtifact(id: number): Promise<ArtifactType> {
const res = await client.get(`/api/artifacts/${id}`)
return ArtifactSchema.parse(res.data)
},

async getArtifacts(params: SearchParams): Promise<Artifacts> {
async getArtifacts(params: SearchParamsType): Promise<ArtifactsType> {
params.page = params.page || 1
const res = await client.get<Artifacts>("/api/artifacts", {
const res = await client.get("/api/artifacts", {
params: params
})
return res.data
return ArtifactsSchema.parse(res.data)
},

async enrichArtifact(id: number): Promise<QueueMessage> {
const res = await client.post<QueueMessage>(`/api/artifacts/${id}/enrich`)
return res.data
async enrichArtifact(id: number): Promise<QueueMessageType> {
const res = await client.post(`/api/artifacts/${id}/enrich`)
return QueueMessageSchema.parse(res.data)
},

async deleteArtifact(id: number): Promise<Message> {
const res = await client.delete<Message>(`/api/artifacts/${id}`)
return res.data
async deleteArtifact(id: number): Promise<MessageType> {
const res = await client.delete(`/api/artifacts/${id}`)
return MessageSchema.parse(res.data)
},

async getRules(params: SearchParams): Promise<Rules> {
async getRules(params: SearchParamsType): Promise<RulesType> {
params.page = params.page || 1
const res = await client.get<Rules>("/api/rules", {
const res = await client.get("/api/rules", {
params: params
})
return res.data
return RulesSchema.parse(res.data)
},

async getRule(id: string): Promise<Rule> {
const res = await client.get<Rule>(`/api/rules/${id}`)
return res.data
async getRule(id: string): Promise<RuleType> {
const res = await client.get(`/api/rules/${id}`)
return RuleSchema.parse(res.data)
},

async searchRule(id: string): Promise<QueueMessage> {
const res = await client.post<QueueMessage>(`/api/rules/${id}/search`)
return res.data
async searchRule(id: string): Promise<QueueMessageType> {
const res = await client.post(`/api/rules/${id}/search`)
return QueueMessageSchema.parse(res.data)
},

async createRule(payload: CreateRule): Promise<Rule> {
const res = await client.post<Rule>("/api/rules/", payload)
return res.data
async createRule(payload: CreateRuleType): Promise<RuleType> {
const res = await client.post("/api/rules/", payload)
return RuleSchema.parse(res.data)
},

async updateRule(payload: UpdateRule): Promise<Rule> {
const res = await client.put<Rule>("/api/rules/", payload)
return res.data
async updateRule(payload: UpdateRuleType): Promise<RuleType> {
const res = await client.put("/api/rules/", payload)
return RuleSchema.parse(res.data)
},

async deleteRule(id: string): Promise<Message> {
const res = await client.delete<Message>(`/api/rules/${id}`)
return res.data
async deleteRule(id: string): Promise<MessageType> {
const res = await client.delete(`/api/rules/${id}`)
return MessageSchema.parse(res.data)
},

async deleteTag(id: number): Promise<Message> {
const res = await client.delete<Message>(`/api/tags/${id}`)
return res.data
async deleteTag(id: number): Promise<MessageType> {
const res = await client.delete(`/api/tags/${id}`)
return MessageSchema.parse(res.data)
},

async getIPInfo(ipAddress: string): Promise<IPInfo> {
const res = await client.get<IPInfo>(`/api/ip_addresses/${ipAddress}`)
async getIpInfo(ipAddress: string): Promise<IpInfoType> {
const res = await client.get<IpInfoType>(`/api/ip_addresses/${ipAddress}`)
return res.data
}
}
Loading

0 comments on commit f6b590d

Please sign in to comment.