Skip to main content

send()

Description

Sends a necessary HTTP request to the server and returns a promise with or without data depending on the request

All requests to the server are made with the send() method which is a part of the RestDataProvider service.

Usage

send(
url: string,
method: "GET" | "POST" | "PUT" | "DELETE" | string,
data?: object,
headers?: object,
): Promise<obj[]>

Parameters

NameTypeDescription
urlstringRequired. A path to the server where a request is sent to.
methodstringRequired. An HTTP method type (Get, Post, Put, Delete)
dataobjectOptional. Parameters that are sent to the server. By default, parameters of the fired event are sent. But you are free to add additional parameters with the custom object. See the examples below.
headersobjectOptional. A default header is the Content-Type header set to application/json. More optional headers can be added with the customHeaders parameter. See the examples below.

Response

The method returns the promise object with or without data depending on the request.

A promise is returned in response to the success request status. In case of the failed request (response.status == 500), an exception with an error text is thrown.

Examples

The following examples demonstrate how to add more headers to the send() method.

const customHeaders = {
"Authorization": "Bearer",
"Custom header": "some value",
};

api.intercept("add-row", obj => {
restDataProvider.send("", "POST", obj, customHeaders);
});

Or you can add headers in the way as below by redefining RestDataProvider, which can give you more control of the data you send to the server:

<script>
import { RestDataProvider } from "wx-table-data-provider";
import { Grid } from "../src/";

const columns = [
{
id: "name",
header: "Title",
flexgrow: 1,
sort: true,
editor: "text",
},
{
id: "year",
header: "Year",
width: 100,
sort: true,
editor: "text",
},
{
id: "votes",
header: "Votes",
width: 100,
sort: true,
editor: "text",
},
];

let data = [];

class MyDataProvider extends RestDataProvider {
send(url, method, data, headers) {
debugger;
headers = { ...headers, SomeToken: "abc" };
return super.send(url, method, data, headers);
}
}

const provider = new MyDataProvider("https://some-backend-url");

provider.getData().then((v) => (data = v));

const init = (api) => {
api.setNext(provider);
};

</script>

<Grid {data} {columns} {init} />

The example below shows how to add additional parameters to the request. You don't need to call the send() method manually here as it's called via the api.setNext() method.

<script>
import { RestDataProvider } from "wx-table-data-provider";
import { Grid } from "../src/";

const columns = [
{
id: "name",
header: "Title",
flexgrow: 1,
sort: true,
editor: "text",
},
{
id: "year",
header: "Year",
width: 100,
sort: true,
editor: "text",
},
{
id: "votes",
header: "Votes",
width: 100,
sort: true,
editor: "text",
},
];

let data = [];

const provider = new RestDataProvider("https://some-backend-url", (obj) => {
obj.year = obj.year * 1;
obj.votes = obj.votes * 1;
});

provider.getData().then((v) => (data = v));

const init = (api) => {
api.setNext(provider);
api.intercept("update-row", (data) => {
data.custom = "custom event";
});
}

</script>

<Grid {data} {columns} {init} />

Related articles: