This service is a general purpose HTTP service wrapped around the Fetch API. This application uses it to connect to the brewery database using the breweryDB service.
The request factory function can be used to create a request with the appropriate HTTP method.
import {
getRequest,
postRequest,
putRequest,
deleteRequest,
} from '@/services/http';
const request = getRequest('some url');The setHeader function can be used to add some HTTP headers to a Request object. This function does not mutate the original Request.
import { setHeaders, getRequest } from '@/services/http';
import { flow } from "fp-ts/lib/function";
const requestWithHeader = setHeaders(new Headers({
'Accept': 'application/json',
}))(getRequest('some url'));
// create reusable header function
const setAuthorizationHeaders = setHeaders(new Headers({
'Authorization': 'Bearer token',
});
// function that returns a get request with the authorization headers applied
const authorizedGetRequest = flow(
getRequest,
setAuthorizationHeaders
);
const requestWithAuthHeaders = authorizedGetRequest('some url);The setQuery function can be used to add query string variables to a Request object. This function does not mutate the original Request.
import { setQuery, getRequest } from '@/services/http';
const requestWithParam = setQuery(new URLSearchParams({
foo: 'bar',
}))(getRequest('some url'));
// create reusable query function
const addFooBar = setQuery(new URLSearchParams({ foo: 'bar' }));
const requestWithFooBar = addFooBar(getRequest('some url'));For the purposes of this showcase application, a function to set the body was not implemented.
The execute function can be used execute a Request object. It returns a TaskEither<Error, Response>.
import { getRequest, execute } from '@/services/http';
const result = execute(getRequest('some url));