Functional API
Initiators
createRestClient
(options: Partial<Options>) => useRestClientThe initiator function, returning the useRestClient function using the provided options object as Global Layer options.
For more details about the options object, visit the Request Options section.
useRestClient
() => {
// Request methods
request, get, del, post, patch, put
// Cache methods
cacheKey, cacheClear, cacheClearByKey, cacheSet, cacheGet,
// Client options utils
optionsOverride, getOption, setOption, cloneOptions,
}Returning an object containing rest methods, options and other utilities.
TIP
This is a destructible object, use this pattern to a more simple and readable code:
const { get } = useRestClient()
const { data, status } = await get<string>(`/my-controller`)request
<TResponse, TError>(method: HttpMethod, path?: string, requestOptions?: Partial<Options>) => Promise<IResponse<TResponse, TError>>Parameters
- method (
GET|DELETE|HEAD|OPTIONS|POST|PUT|PATCH|LINK) - path (string), the request path relative to
host+basePath - requestOptions (Options | undefined), local request options that will override the global options provided via
createRestClient.
Returns
<TResponse, TError>(...) => Promise<IResponse<TResponse, TError>>TResponseis theresponse.datatype inferenceTErroris the optionalresponse.error.datatype inference
For more details about the response object, visit the Response Object section.
get / del / post / patch / put
<TResponse, TError>(path?: string, requestOptions?: Partial<Options>) => Promise<IResponse<TResponse, TError>>Like the previous request method but without the need to specify the HTTP Method (implicit), Local Layer options (requestOptions parameter) will override the Global Layer options provided via createRestClient.
INFO
Every shortcut method will internally call the request function.
cacheKey
(url: URL, method: HttpMethod | "*", customKey?: string) => stringEvaluate an internal cache key, giving a URL instance, HTTP method and a key prefix as third parameter (if omitted, inherits from context's options).
cacheClear
() => voidClear the entire internal cache stored on the client's context.
cacheClearByKey
(cacheKey: string) => voidClear any cache entry store using the provided cacheKey.
cacheGet
(url: URL, method: HttpMethod | "*", customKey?: string) => {
response: IResponse,
expireAt: Date | null
} | undefinedReturn a cache entry if exits, if the third parameter is omitted will be inherited from client's context.
cacheSet
(response: IResponse, customKey?: string, expireIn?: number) => voidSet a cache entry using the response object, a custom key to override (if omitted will inherit from client's context) and a expiration in milliseconds (if omitted, no expiration on the entry)
optionsOverride
<TResponse = any, TError = any>(overrides: Partial<Options>, base?: Partial<Options>) => Partial<Options>Will override the current options using the overrides object without modifying anything on the current context. It will override just the base when provided.
TIP
Any override done by this method will follow behaviors from overrideStrategy.
getOption
(key: K) => Options[K]Returns the current value of the provided option key.
setOption
(key: K, value: Options[K]) => voidSets the value provided for the option key.
cloneOptions
() => OptionsReturns a copy of the current global options.
useRestClientBuilder
() => {
getOption, setOption, unsetOption,
cloneOptions, mergeOptions, assignOptions,
checkAndRestoreDefaults,
createRestClient
}Returning a destructible object, containing options utilities and the rest client generator.
builder.getOption
(key: K) => Options[K]Returns the current value of the provided option key.
builder.setOption
(key: K, value: Options[K]) => voidSets the value provided for the option key.
builder.unsetOption
(key: K) => Options[K]Restore the default value for the provided option key.
builder.cloneOptions
() => OptionsReturns a copy of the current options.
builder.mergeOptions
(options: Partial<Options>) => voidOverrides current options with the provided options using a merge strategy.
builder.assignOptions
(options: Partial<Options>) => voidOverrides current options with the provided options using a Object.assign strategy.
builder.checkAndRestoreDefaults
() => voidIterate internally over every option and restores defaults value if undefined or falsy value.
builder.createRestClient
() => { ... } // useRestClient functionCreates a rest client based on the current options, returning a ready useRestClient function.