Skip to content

Request

Request module contains constructors and helpers for managing immutable interpretations of HTTP requests.

A Request represents immutable and clonable HTTP request data. It is base element of Request module.

import {
import Request
Request
} from 'fx-fetch';
type
type Type = Request.Request
Type
=
import Request
Request
.
export Request

Represents immutable HTTP request.

@since0.1.0

Request
;

A Request.Input represents all the possible inputs that can be used to create a Request.

Concrete types that make up the Input union:

import {
import Request
Request
} from 'fx-fetch';
type
type Type = Request.Request | Request.Request.Parts | Request.Request.Options | Request
Type
=
import Request
Request
.
export Request

Represents immutable HTTP request.

@since0.1.0

Request
.
type Request.Input = Request.Request | Request.Request.Parts | Request.Request.Options | Request

@since0.1.0

Input
;
  • globalThis.Request — vanilla JS request
  • Request.Request.Parts — Object with request parts
  • Request.Request.Options — Object with request options and URL. This variant was created for better DX.
  • Request.Request — Existing Request can be used too

Creates a Request from any of Request.Input. Throws an IllegalArgumentException if the provided input is invalid.

import {
import Request
Request
} from 'fx-fetch';
// ┌─── Request.Request
// ▼
const
const request: Request.Request
request
=
import Request
Request
.
function unsafeMake(input: Request.Request.Input): Request.Request
export unsafeMake

Creates a immutable Request object. Throws an error if the input is invalid.

@example

import { Request } from 'fx-fetch';
// ┌─── Request.Request
// ▼
const request = Request.unsafeMake({
url: 'https://example.com',
method: 'POST'
});

@since0.1.0

unsafeMake
({
url: string
url
: 'https://example.com',
method: string
method
: 'POST'
});

Similar to unsafeMake, but returns an Option instead of throwing an error if the input is invalid. If the input is invalid, it returns None. If valid, it returns Some containing the Request.

import {
import Option

@since2.0.0

@since2.0.0

Option
} from 'effect';
import {
import Request
Request
} from 'fx-fetch';
// ┌─── Option.Option<Request.Request>
// ▼
const
const request: Option.Option<Request.Request>
request
=
import Request
Request
.
function make(input: import("/home/runner/work/fx-fetch/fx-fetch/packages/fx-fetch/dist/Request/Request").Request.Input): Option.Option<import("/home/runner/work/fx-fetch/fx-fetch/packages/fx-fetch/dist/Request/Request").Request>
export make

Creates a immutable Request object.

@example

import { Request } from 'fx-fetch';
// ┌─── Option.Option<Request.Request>
// ▼
const request = Request.make({
url: 'https://example.com',
method: 'POST'
});

@since0.1.0

make
({
url: string
url
: 'https://example.com',
method: string
method
: 'POST'
});

For creating a Request from an object Request.Parts use same functions make or unsafeMake.

The Parts type is super primitive. It’s basically an object with all the properties of a JS request, but with better DX. Everything except the url is optional.

import {
import Request
Request
} from 'fx-fetch';
const
const parts: Request.Request.Parts
parts
:
import Request
Request
.
export Request

Represents immutable HTTP request.

@since0.1.0

Request
.
type Request.Parts = {
readonly body?: BodyInput;
readonly cache?: RequestCache;
readonly credentials?: RequestCredentials;
readonly headers?: HeadersInput;
readonly integrity?: string;
readonly keepalive?: boolean;
readonly method?: string;
readonly mode?: RequestMode;
readonly priority?: RequestPriority;
readonly redirect?: RequestRedirect;
readonly referrer?: string;
readonly referrerPolicy?: ReferrerPolicy;
readonly signal?: AbortSignal;
readonly url: Url.Input;
}

@since0.1.0

Parts
= {
body?: any
body
: 'Hello, world!',
cache?: any
cache
: 'no-cache',
credentials?: any
credentials
: 'same-origin',
headers?: any
headers
: {
'Content-Type': 'text/plain',
'X-Custom-Header': ['CustomValue', 'AnotherValue'],
},
integrity?: string
integrity
: 'sha256-abcdef',
keepalive?: boolean
keepalive
: true,
method?: string
method
: 'POST',
mode?: any
mode
: 'cors',
priority?: any
priority
: 'high',
redirect?: any
redirect
: 'follow',
referrer?: string
referrer
: 'https://example.com',
referrerPolicy?: any
referrerPolicy
: 'no-referrer',
signal?: AbortSignal
signal
: new
var AbortController: new () => AbortController

The AbortController interface represents a controller object that allows you to abort one or more Web requests as and when desired.

MDN Reference

AbortController
().
AbortController.signal: AbortSignal
signal
,
url: Url.Input
url
: 'https://example.com/api', // ◀︎── required property
};
const
const request: Request.Request
request
=
import Request
Request
.
function unsafeMake(input: Request.Request.Input): Request.Request
export unsafeMake

Creates a immutable Request object. Throws an error if the input is invalid.

@example

import { Request } from 'fx-fetch';
// ┌─── Request.Request
// ▼
const request = Request.unsafeMake({
url: 'https://example.com',
method: 'POST'
});

@since0.1.0

unsafeMake
(
const parts: Request.Request.Parts
parts
);

Properties url and headers have flexible input types.

  • url is of type Url.Input. Read more about it in Url module documentation.
  • headers can be almost anything what you can imagine as headers.
    • Record of strings
    • Entries (array of tuples)
    • Map of strings
    • Same as HeadersInit in Fetch API
    • … many more…

For creating a Request from an object Request.Options use same functions make or unsafeMake.

The Options type is the same as Parts, but with searchParams property. It is basically flattened version of Url.Url.Options merged with Request.Parts.

Reason for having this variant is your comfort.

import {
import Request
Request
} from 'fx-fetch';
const
const options: Request.Request.Options
options
:
import Request
Request
.
export Request

Represents immutable HTTP request.

@since0.1.0

Request
.
type Request.Options = {
readonly searchParams?: SearchParamsInput;
} & Request.Request.Parts

@since0.1.0

Options
= {
url: Url.Input
url
: 'https://example.com/api', // ◀︎── required property
searchParams?: SearchParamsInput
searchParams
: {
id: string
id
: '123',
ReadonlyArray<readonly [key: string, value: SearchParamValueInput]>.filter<S>(predicate: (value: readonly [key: string, value: SearchParamValueInput], index: number, array: readonly (readonly [key: string, value: SearchParamValueInput])[]) => value is S, thisArg?: any): S[] (+1 overload)

Returns the elements of an array that meet the condition specified in a callback function.

@parampredicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.

@paramthisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

filter
: ["one", "two"]
},
};
const
const request: Request.Request
request
=
import Request
Request
.
function unsafeMake(input: Request.Request.Input): Request.Request
export unsafeMake

Creates a immutable Request object. Throws an error if the input is invalid.

@example

import { Request } from 'fx-fetch';
// ┌─── Request.Request
// ▼
const request = Request.unsafeMake({
url: 'https://example.com',
method: 'POST'
});

@since0.1.0

unsafeMake
(
const options: Request.Request.Options
options
);