Request
Request module contains constructors and helpers for managing immutable interpretations of HTTP requests.
The Request Type
Section titled “The Request Type”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.
Request;The Input Type
Section titled “The Input Type”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.
Request.type Request.Input = Request.Request | Request.Request.Parts | Request.Request.Options | Request
Input;globalThis.Request— vanilla JS requestRequest.Request.Parts— Object with request partsRequest.Request.Options— Object with request options and URL. This variant was created for better DX.Request.Request— Existing Request can be used too
Constructors
Section titled “Constructors”unsafeMake
Section titled “unsafeMake”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.Requestexport unsafeMake
Creates a immutable Request object. Throws an error if the input is invalid.
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
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.
make({ url: string
url: 'https://example.com', method: string
method: 'POST'});from Request.Parts
Section titled “from Request.Parts”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.
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;}
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.
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.Requestexport unsafeMake
Creates a immutable Request object. Throws an error if the input is invalid.
unsafeMake(const parts: Request.Request.Parts
parts);Properties url and headers have flexible input types.
urlis of typeUrl.Input. Read more about it in Url module documentation.headerscan be almost anything what you can imagine as headers.- Record of strings
- Entries (array of tuples)
Mapof strings- Same as
HeadersInitin Fetch API - … many more…
from Request.Options
Section titled “from Request.Options”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.
Request.type Request.Options = { readonly searchParams?: SearchParamsInput;} & Request.Request.Parts
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.
filter: ["one", "two"] },};
const const request: Request.Request
request = import Request
Request.function unsafeMake(input: Request.Request.Input): Request.Requestexport unsafeMake
Creates a immutable Request object. Throws an error if the input is invalid.
unsafeMake(const options: Request.Request.Options
options);