web: add network plugin
This commit is contained in:
@@ -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)
|
||||
|
74
doric-web/src/plugins/NetworkPlugin.ts
Normal file
74
doric-web/src/plugins/NetworkPlugin.ts
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user