Gathering detailed insights and metrics for @app-elements/use-request
Gathering detailed insights and metrics for @app-elements/use-request
Gathering detailed insights and metrics for @app-elements/use-request
Gathering detailed insights and metrics for @app-elements/use-request
npm install @app-elements/use-request
Typescript
Module System
Node Version
NPM Version
JavaScript (84.43%)
Less (15.57%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
543 Commits
12 Watchers
20 Branches
20 Contributors
Updated on Sep 02, 2022
Latest Version
2.1.4
Package Id
@app-elements/use-request@2.1.4
Unpacked Size
249.83 kB
Size
57.47 kB
File Count
15
NPM Version
lerna/3.22.1/node@v14.5.0+x64 (darwin)
Node Version
14.5.0
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
useRequest is a simple hook for loading data. It's efficient and caches results automatically.
npm install --save @app-elements/use-request
1import { useRequest } from '@app-elements/use-request' 2import createStore from 'atom' 3 4const store = createStore([], {}) 5 6const Users = () => { 7 // return an object with the keys: result, clear 8 // result is the JSON response from the given URL. 9 // clear is a function to clear the cache, and reload the data from the URL. 10 const { result, error, isLoading, clear } = useRequest(store, 'https://jsonplaceholder.typicode.com/users') 11 if (isLoading) { 12 return <div>Loading...</div> 13 } 14 if (error != null) { 15 return <div>Error!</div> 16 } 17 return ( 18 <ul> 19 {result.map(({ name }) => <li>{name}</li>)} 20 </ul> 21 ) 22}
Sometimes you need to load a request based on data from another request. Here's how you can orchestrate that:
1const { result: user } = useRequest(store, '/api/user') 2const { result: projects } = useRequest(store, user != null ? `/api/user/${user.id}/projects` : null)
The core request function that useRequest utilizes comes with some nice defaults that you may want to reuse outside of a hook context:
apiUrl
>= 400
1import { request } from '@app-elements/use-request/request' 2 3const { promise, xhr } = request({ 4 url: 'https://my-cool-api.com/resource', 5 method: 'POST', 6 data: { 7 // ... 8 } 9})
useRequest
also ships with an optional reducer and actions that help with
managing the cached request results. To use it, you must import the reducer and include it when creating your store.
1import { requestsReducer, actions as requestActions } from '@app-elements/use-request/reducer' 2 3const store = createStore([requestsReducer], initialState) 4 5// It's also convenient to export the actions from your store file: 6export const clearRequest = requestActions.clearRequest 7export const clearRequests = requestActions.clearRequests 8export const patchListRequest = requestActions.patchListRequest
appendRequest({ endpointOrUid: String, item: Object, path?: String }): FluxStandardAction
This is for appending a new item for an endpoint that represents an array of items to render, ex. api/listings
. Say, you create a new listing, instead of getting the whole listing array from your API again, you can just append the new listing to the end of the existing array of items you have.
1appendRequest({ 2 endpointOrUid: '/users', 3 path: '', 4 item: { id: 6, name: 'Margo' } 5})
clearRequest(endpointOrUid: String): FluxStandardAction
Removes the matching endpointOrUid
from the requests
object.
1clearRequest('/api/listings')
clearRequests(predicate: Function): FluxStandardAction
Filters out any endpointOrUid
s on the requests
object that match the predicate function.
1clearRequests(uid => uid.indexOf('listings') > -1)
patchListRequest({ endpointOrUid: String, dataToMerge: Object, matchKey?: String, path?: String }): FluxStandardAction
This is for patching an endpoint that represents an array of items to render, ex. api/listings
. Let's say you are rendering this array of items, and then perform an update to one of the items contained in that array. When you go back to view the list, it very likely contains that one item but with outdated data. You could clear the entire listings request, re-rendering the whole list, but it would be much nicer to just update the one item in the listings that we know has updated. This way, the entire list isn't re-rendered, rather only the one item.
1patchListRequest({ 2 endpointOrUid: '/api/listings', 3 dataToMerge: { id: 4, title: 'Updated title' }, // Must include the `matchKey` value. In this case, `id: 4`. 4 matchKey = 'id', // Optional, defaults to 'id'. 5 path = 'results' // Optional, defaults to 'results'. This matches the response shape of Django-Rest-Framework. It should be the path to the actual array data returned in the API response. 6})
Prop | Type | Default | Description |
---|---|---|---|
store | Object | None | An (atom) store instance |
url | String | None | A URL to GET data from |
options | Object | None | Various options to customize your request |
Prop | Type | Default | Description |
---|---|---|---|
maxTime | Number | 30000 | Time (ms) to cache result |
uid | String | None | A unique ID to store cached result under |
headers | Object | None | Your request headers |
method | String | None | Request method |
data | Object | None | Data to send. Typically with POST or PATCH requests |
noAuth | Boolean | false | Skip automatically setting the Authorization header |
contentType | String | 'application/json' | Set the Content-Type, or falsy for no Content-Type header |
Prop | Type | Description |
---|---|---|
result | JSON | The body returned by the request. Could be null, or a string if not a JSON endpoint. |
error | Error | If the response header is >= 400, this will contain an Error instance. |
isLoading | Boolean | true until either result or error is set. |
clear | Function | Call this to clear the cached result of the request. |
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
Found 1/26 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
license file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
71 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More