Skip to content

Function: createPostClient() ​

createPostClient<FixedOptions, CommonOptions, CommonDelay>(fixedOptions?, commonOptions?, commonDeferOptions?): {<T, Options, Result>(url, data?, options?): IPromise_Fetch<Result>; deferred: <T, Options, TReturn>(...args) => IPromise_Fetch<TReturn>; }

Defined in: packages/fetch/src/createPostClient.ts:63

Create a reusable fetch client with shared options. The returned function comes attached with a .deferred() function for debounced and throttled request. While createClient() is versatile enough for any HTTP method, createPostClient() is specifically designed for methods that require a request body, such as DELETE, PATCH, POST, and PUT. If a method is not provided, it defaults to POST. The generated client accepts an additional second parameter (data) for the request payload.

Similar to createClient, the returned function comes equipped with a .deferred() method, enabling debounced, throttled, or sequential execution.

Type Parameters ​

FixedOptions ​

FixedOptions extends PostOptions | undefined

CommonOptions ​

CommonOptions extends PostOptions | object & Omit<PostOptions, "body" | "cache" | "credentials" | "headers" | "integrity" | "keepalive" | "method" | "mode" | "priority" | "redirect" | "referrer" | "referrerPolicy" | "window" | "retry" | "retryIf" | "retryBackOff" | "retryDelay" | "retryDelayJitter" | "retryDelayJitterMax" | "as" | "errMsgs" | "interceptors" | "ignoreGlobalDefaults" | "fetchFunc" | "onDownloadProgress" | "validateUrl" | keyof TimeoutOptions<[]>> & Partial<Record<"body" | "cache" | "credentials" | "integrity" | "keepalive" | "method" | "mode" | "priority" | "redirect" | "referrer" | "referrerPolicy" | "window" | "retry" | "retryIf" | "retryBackOff" | "retryDelay" | "retryDelayJitter" | "retryDelayJitterMax" | "as" | "errMsgs" | "interceptors" | "ignoreGlobalDefaults" | "fetchFunc" | "onDownloadProgress" | "validateUrl" | keyof TimeoutOptions<[]>, never>> | undefined

CommonDelay ​

CommonDelay extends number = number

Parameters ​

fixedOptions? ​

FixedOptions

Mandatory fetch options that cannot be overriden by individual request

commonOptions? ​

object & Omit<FetchOptions, "method"> & CommonOptions

Common fetch options that can be overriden by individual request

commonDeferOptions? ​

DeferredAsyncOptions<unknown, CommonDelay>

Returns ​

<T, Options, Result>(url, data?, options?): IPromise_Fetch<Result>

Executes the HTTP request using the configured client options.

This function is specifically designed for methods that require a request body (e.g., POST, PUT, PATCH), allowing the payload to be passed directly as the second argument.

Type Parameters ​

T ​

T extends unknown = never

Options ​

Options extends PostOptions | object & Omit<PostOptions, "body" | "cache" | "credentials" | "headers" | "integrity" | "keepalive" | "method" | "mode" | "priority" | "redirect" | "referrer" | "referrerPolicy" | "window" | "retry" | "retryIf" | "retryBackOff" | "retryDelay" | "retryDelayJitter" | "retryDelayJitterMax" | "as" | "errMsgs" | "interceptors" | "ignoreGlobalDefaults" | "fetchFunc" | "onDownloadProgress" | "validateUrl" | keyof TimeoutOptions<[]>> & Partial<Record<"body" | "cache" | "credentials" | "integrity" | "keepalive" | "method" | "mode" | "priority" | "redirect" | "referrer" | "referrerPolicy" | "window" | "retry" | "retryIf" | "retryBackOff" | "retryDelay" | "retryDelayJitter" | "retryDelayJitterMax" | "as" | "errMsgs" | "interceptors" | "ignoreGlobalDefaults" | "fetchFunc" | "onDownloadProgress" | "validateUrl" | keyof TimeoutOptions<[]>, never>> | undefined = ExcludePostOptions<FixedOptions> | undefined

Result ​

Result = GetFetchResult<[FixedOptions, Options, CommonOptions], T>

Parameters ​

url ​

URL | RequestInfo

data? ​

PostBody | (() => ValueOrPromise<PostBody>)

options? ​

Options

Returns ​

IPromise_Fetch<Result>

deferred() ​

deferred<ThisArg, DefaultUrl, DefaultData, DefaultOptions, Delay>(deferOptions?, defaultUrl?, defaultData?, defaultOptions?): <T, Options, TReturn>(...args) => IPromise_Fetch<TReturn>

Returns a version of the client configured for debounced, throttled, or sequential execution.

This is particularly useful for optimizing high-frequency operations, such as auto-saving forms or managing rapid state updates, by automatically handling request cancellation and execution timing.

Type Parameters ​

ThisArg ​

ThisArg

DefaultUrl ​

DefaultUrl extends URL | RequestInfo | undefined

DefaultData ​

DefaultData extends PostBody | (() => ValueOrPromise<PostBody>) | undefined

DefaultOptions ​

DefaultOptions extends PostOptions | object & Omit<PostOptions, "body" | "cache" | "credentials" | "headers" | "integrity" | "keepalive" | "method" | "mode" | "priority" | "redirect" | "referrer" | "referrerPolicy" | "window" | "retry" | "retryIf" | "retryBackOff" | "retryDelay" | "retryDelayJitter" | "retryDelayJitterMax" | "as" | "errMsgs" | "interceptors" | "ignoreGlobalDefaults" | "fetchFunc" | "onDownloadProgress" | "validateUrl" | keyof TimeoutOptions<[]>> & Partial<Record<"body" | "cache" | "credentials" | "integrity" | "keepalive" | "method" | "mode" | "priority" | "redirect" | "referrer" | "referrerPolicy" | "window" | "retry" | "retryIf" | "retryBackOff" | "retryDelay" | "retryDelayJitter" | "retryDelayJitterMax" | "as" | "errMsgs" | "interceptors" | "ignoreGlobalDefaults" | "fetchFunc" | "onDownloadProgress" | "validateUrl" | keyof TimeoutOptions<[]>, never>> | undefined = ExcludePostOptions<FixedOptions> | undefined

Delay ​

Delay extends number = number

Parameters ​

deferOptions? ​

DeferredAsyncOptions<ThisArg, Delay>

defaultUrl? ​

DefaultUrl

defaultData? ​

DefaultData

defaultOptions? ​

DefaultOptions

Returns ​

<T, Options, TReturn>(...args) => IPromise_Fetch<TReturn>

Example ​

Create reusable clients ​

javascript
import { createPostClient, FetchAs } from '@superutils/fetch'

// Create a POST client with 10-second as the default timeout
const postClient = createPostClient(
	{ // fixed options cannot be overrided by client calls
	  method: 'post',
	  headers: { 'content-type': 'application/json' },
	},
	{ timeout: 10000 }, // common options that can be overriden by client calls
)

// Invoking `postClient()` automatically applies the pre-configured options
postClient(
	 '[DUMMYJSON-DOT-COM]/products/add',
	 { title: 'New Product' }, // data/body
	 {}, // other options
).then(console.log)

// create a deferred client using "postClient"
const updateProduct = postClient.deferred(
	{
		delay: 300, // debounce duration
		onResult: console.log, // prints only successful results
	},
	'[DUMMYJSON-DOT-COM]/products/add',
	{ method: 'patch', timeout: 3000 },
)
updateProduct({ title: 'New title 1' }) // ignored by debounce
updateProduct({ title: 'New title 2' }) // executed