Skip to content

Function: createPostClient()

createPostClient<FixedOpts, CommonOpts, FixedAs>(fixedOptions?, commonOptions?, commonDeferOptions?): {<T, TOptions, TAs, TReturn>(url, data?, options?): IPromisE<TReturn>; deferred: <TResult, TOptions, TAs, TReturn>(...args) => IPromisE<TReturn>; }

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

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

FixedOpts

FixedOpts extends PostOptions | undefined

CommonOpts

CommonOpts extends PostOptions | object & Omit<PostOptions, "body" | "cache" | "credentials" | "headers" | "integrity" | "keepalive" | "method" | "mode" | "priority" | "redirect" | "referrer" | "referrerPolicy" | "signal" | "window" | "retry" | "retryIf" | "retryBackOff" | "retryDelay" | "retryDelayJitter" | "retryDelayJitterMax" | "as" | "abortCtrl" | "errMsgs" | "interceptors" | "timeout" | "validateUrl"> & Partial<Record<"body" | "cache" | "credentials" | "integrity" | "keepalive" | "method" | "mode" | "priority" | "redirect" | "referrer" | "referrerPolicy" | "signal" | "window" | "retry" | "retryIf" | "retryBackOff" | "retryDelay" | "retryDelayJitter" | "retryDelayJitterMax" | "as" | "abortCtrl" | "errMsgs" | "interceptors" | "timeout" | "validateUrl", never>> | undefined

FixedAs

FixedAs extends FetchAs | undefined = FetchAsFromOptions<FixedOpts, undefined>

Parameters

fixedOptions?

FixedOpts

Mandatory fetch options that cannot be overriden by individual request

commonOptions?

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

Common fetch options that can be overriden by individual request

commonDeferOptions?

DeferredAsyncOptions<unknown, unknown>

Returns

<T, TOptions, TAs, TReturn>(url, data?, options?): IPromisE<TReturn>

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 = unknown

TOptions

TOptions extends PostOptions | object & Omit<PostOptions, "body" | "cache" | "credentials" | "headers" | "integrity" | "keepalive" | "method" | "mode" | "priority" | "redirect" | "referrer" | "referrerPolicy" | "signal" | "window" | "retry" | "retryIf" | "retryBackOff" | "retryDelay" | "retryDelayJitter" | "retryDelayJitterMax" | "as" | "abortCtrl" | "errMsgs" | "interceptors" | "timeout" | "validateUrl"> & Partial<Record<"body" | "cache" | "credentials" | "integrity" | "keepalive" | "method" | "mode" | "priority" | "redirect" | "referrer" | "referrerPolicy" | "signal" | "window" | "retry" | "retryIf" | "retryBackOff" | "retryDelay" | "retryDelayJitter" | "retryDelayJitterMax" | "as" | "abortCtrl" | "errMsgs" | "interceptors" | "timeout" | "validateUrl", never>> | undefined = ExcludePostOptions<FixedOpts> | undefined

TAs

TAs extends FetchAs = FixedAs extends FetchAs ? FixedAs<FixedAs> : FetchAsFromOptions<TOptions, FetchAsFromOptions<CommonOpts>>

TReturn

TReturn = FetchResult<T>[TAs]

Parameters

url

string | URL

data?

PostBody | () => PostBody

options?

TOptions

Returns

IPromisE<TReturn>

deferred()

deferred<ThisArg, Delay, DefaultUrl, DefaultData, DefaultOptions>(deferOptions, defaultUrl?, defaultData?, defaultOptions?): <TResult, TOptions, TAs, TReturn>(...args) => IPromisE<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

Delay

Delay extends number

DefaultUrl

DefaultUrl extends string | URL | undefined

DefaultData

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

DefaultOptions

DefaultOptions extends PostOptions | object & Omit<PostOptions, "body" | "cache" | "credentials" | "headers" | "integrity" | "keepalive" | "method" | "mode" | "priority" | "redirect" | "referrer" | "referrerPolicy" | "signal" | "window" | "retry" | "retryIf" | "retryBackOff" | "retryDelay" | "retryDelayJitter" | "retryDelayJitterMax" | "as" | "abortCtrl" | "errMsgs" | "interceptors" | "timeout" | "validateUrl"> & Partial<Record<"body" | "cache" | "credentials" | "integrity" | "keepalive" | "method" | "mode" | "priority" | "redirect" | "referrer" | "referrerPolicy" | "signal" | "window" | "retry" | "retryIf" | "retryBackOff" | "retryDelay" | "retryDelayJitter" | "retryDelayJitterMax" | "as" | "abortCtrl" | "errMsgs" | "interceptors" | "timeout" | "validateUrl", never>> | undefined = undefined

Parameters

deferOptions

DeferredAsyncOptions<ThisArg, Delay> = ...

defaultUrl?

DefaultUrl

defaultData?

DefaultData

defaultOptions?

DefaultOptions

Returns

<TResult, TOptions, TAs, TReturn>(...args): IPromisE<TReturn>

Type Parameters
TResult

TResult = unknown

TOptions

TOptions extends PostOptions | object & Omit<PostOptions, "body" | "cache" | "credentials" | "headers" | "integrity" | "keepalive" | "method" | "mode" | "priority" | "redirect" | "referrer" | "referrerPolicy" | "signal" | "window" | "retry" | "retryIf" | "retryBackOff" | "retryDelay" | "retryDelayJitter" | "retryDelayJitterMax" | "as" | "abortCtrl" | "errMsgs" | "interceptors" | "timeout" | "validateUrl"> & Partial<Record<"body" | "cache" | "credentials" | "integrity" | "keepalive" | "method" | "mode" | "priority" | "redirect" | "referrer" | "referrerPolicy" | "signal" | "window" | "retry" | "retryIf" | "retryBackOff" | "retryDelay" | "retryDelayJitter" | "retryDelayJitterMax" | "as" | "abortCtrl" | "errMsgs" | "interceptors" | "timeout" | "validateUrl", never>> | undefined = ExcludePostOptions<FixedOpts>

TAs

TAs extends FetchAs = FixedAs extends FetchAs ? FixedAs<FixedAs> : FetchAsFromOptions<TOptions, FetchAsFromOptions<CommonOpts>>

TReturn

TReturn = FetchResult<TResult>[TAs]

Parameters
args

...PostDeferredCbArgs<DefaultUrl, DefaultData, TOptions>

Returns

IPromisE<TReturn>

Example

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

// Create a POST client with 10-second as the default timeout
const postClient = createPostClient(
	{
		method: 'post',
		headers: { 'content-type': 'application/json' },
	},
	{ timeout: 10000 },
)

// Invoking `postClient()` automatically applies the pre-configured options
postClient(
	'https://dummyjson.com/products/add',
	{ title: 'New Product' }, // data/body
	{}, // other options
).then(console.log)

// create a deferred client using "postClient"
const updateProduct = postClient.deferred(
	{
		delayMs: 300, // debounce duration
		onResult: console.log, // prints only successful results
	},
	'https://dummyjson.com/products/add',
	{
		method: 'patch',
		timeout: 3000,
	},
)
updateProduct({ title: 'New title 1' }) // ignored by debounce
updateProduct({ title: 'New title 2' }) // executed