1.2.0 • Published 12 months ago

solid-request v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

Solid-request

With a strong ability to manage network requests, Hook has a flying experience

Usage

Install

npm i solid-request

Import

import useRequest from "solid-request"

useRequest Through the plug-in organization code, the core code is easy to understand, and can be easily expanded to more advanced functions. Capacity is now available to include

  • Automatic/manual request
  • Support Typescript
  • Polling
  • Debounce
  • Throttle
  • Refresh on window focus
  • Error retry
  • Loading delay
  • SWR(stale-while-revalidate)
  • Caching
  • Plugins

Default request

By default, the first parameter of useRequest is an asynchronous function, which is automatically executed when the component is initialized. At the same time, it automatically manages the status of loading, data, error of the asynchronous function.

const { data, error, loading } = useRequest(service)

example

export async function getList({ id }: { id: number }): Promise<{
	id: number
	title: string
	body: string
	userId: number
}> {
	console.log(id)

	return fetch(`https://jsonplaceholder.typicode.com/posts/${id}`).then(
		(response) => response.json()
	)
}

function App() {
	const [count, setCount] = createSignal(1)
  
	const { data, loading } = useRequest(() => getList({ id: count() }), {
		manual: false,
		ready: true,
		refreshDeps: true,
		loadingDelay: 300,
	})

	return (
		<div>
			<button type="button" onClick={increment}>
				{count()}
			</button>
			<div>{loading() ? 'loading...' : JSON.stringify(data())}</div>
		</div>
	)
}

The document is under development, for more APIs, please see the vue version of useRequest

Result

PropertyDescriptionType
dataData returned by serviceAccessor<TData \| undefined> `
errorException thrown by serviceAccessor<Error> | undefined
loadingIs the service being executedAccessor<boolean>
paramsAn array of parameters for the service being executed. For example, you triggered run(1, 2, 3), then params is equal to [1, 2, 3]Accessor<TParams \| []>
run Manually trigger the execution of the service, and the parameters will be passed to the serviceAutomatic handling of exceptions, feedback through onError(...params: TParams) => void
runAsyncThe usage is the same as run, but it returns a Promise, so you need to handle the exception yourself.(...params: TParams) => Promise<TData>
refreshUse the last params, call run again() => void
refreshAsyncUse the last params, call runAsync again() => Promise<TData>
mutateMutate data directly(data?: TData / ((oldData?: TData) => (TData / undefined))) => void
cancelIgnore the current promise response() => void

Options

PropertyDescriptionTypeDefault
initialDataInit dataTData | undefined
manual The default is false. That is, the service is automatically executed during initialization.If set to true, you need to manually call run or runAsync to trigger execution. booleanfalse
defaultParamsThe parameters passed to the service at the first default executionTParams-
formatResultFormat the request results, which recommend to use useFormatResult(response: TData) => any-
onBeforeTriggered before service execution(params: TParams) => void-
onSuccessTriggered when service resolve(data: TData, params: TParams) => void-
onErrorTriggered when service reject(e: Error, params: TParams) => void-
onFinallyTriggered when service execution is complete(params: TParams, data?: TData, e?: Error) => void-
1.2.0

12 months ago

1.1.8

1 year ago

1.1.7

1 year ago

1.1.6

1 year ago

1.1.5

1 year ago

1.1.4

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.1.0

1 year ago

1.0.0

1 year ago