web: add network plugin

This commit is contained in:
王劲鹏
2022-08-05 15:20:35 +08:00
committed by osborn
parent fe060bdb7f
commit 46d34e3288
4 changed files with 150 additions and 4 deletions

View File

@@ -20,6 +20,7 @@ import { DoricSwitchNode } from "./shader/DoricSwitchNode"
import { DoricSliderNode } from "./shader/DoricSliderNode"
import { DoricSlideItemNode } from "./shader/DoricSlideItemNode"
import { NotificationPlugin } from "./plugins/NotificationPlugin"
import { NetworkPlugin } from "./plugins/NetworkPlugin"
const bundles: Map<string, string> = new Map
@@ -61,6 +62,7 @@ registerPlugin('navigator', NavigatorPlugin)
registerPlugin('popover', PopoverPlugin)
registerPlugin('animate', AnimatePlugin)
registerPlugin('notification', NotificationPlugin)
registerPlugin('network', NetworkPlugin)
registerViewNode('Stack', DoricStackNode)
registerViewNode('VLayout', DoricVLayoutNode)

View File

@@ -0,0 +1,74 @@
import axios from "axios";
import { DoricPlugin } from "../DoricPlugin";
export class NetworkPlugin extends DoricPlugin {
async request(args: {
url: string,
method: string,
headers: any,
data: any,
timeout: number,
}) {
let result: any
let error: any
if (args.method.toLowerCase() === "get") {
try {
result = await axios.get<any>(
args.url,
{
headers: args.headers ? args.headers : {},
timeout: args.timeout
},
)
} catch (exception) {
error = exception
}
} else if (args.method.toLowerCase() === "post") {
try {
result = await axios.post<any>(
args.url,
args.data,
{
headers: args.headers ? args.headers : {},
timeout: args.timeout
},
)
} catch (exception) {
error = exception
}
} else if (args.method.toLowerCase() === "put") {
try {
result = await axios.put<any>(
args.url,
args.data,
{
headers: args.headers ? args.headers : {},
timeout: args.timeout
},
)
} catch (exception) {
error = exception
}
} else if (args.method.toLowerCase() === "delete") {
try {
result = await axios.delete<any>(
args.url,
{
headers: args.headers ? args.headers : {},
timeout: args.timeout
},
)
} catch (exception) {
error = exception
}
}
result.data = JSON.stringify(result.data)
if (result) {
return Promise.resolve(result)
}
if (error) {
return Promise.reject(error)
}
}
}