Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/07akioni/create-merged-request
Merge scattered requests into one, distribute corresponding result to requestor.
https://github.com/07akioni/create-merged-request
http request
Last synced: 28 days ago
JSON representation
Merge scattered requests into one, distribute corresponding result to requestor.
- Host: GitHub
- URL: https://github.com/07akioni/create-merged-request
- Owner: 07akioni
- Created: 2022-06-17T16:02:59.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-06-17T16:10:06.000Z (over 2 years ago)
- Last Synced: 2024-11-24T16:25:58.691Z (30 days ago)
- Topics: http, request
- Language: TypeScript
- Homepage:
- Size: 4.88 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# create-merged-request · [![npm version](https://badge.fury.io/js/create-merged-request.svg)](https://badge.fury.io/js/create-merged-request)
Merge scattered requests into one, distribute corresponding result to requestor.
## Install
```bash
npm i create-merged-request rxjs # Please note that rxjs is peer dependency of it
```## Usage
For example if you have tons of user query in different components and you want
to combine theme into one.```ts
import { createMergedRequest } from 'create-merged-request'// `string` is user id
// `User[]` is the response data type that `createRequest` resolves
// `User` is the data type that `createResponse` returns
const mergedRequest = createMergedRequest({
thresholdMs: 0, // time threshold to merged request into one request
createRequest(ids) {
return fetchUsers(ids) // `fetchUsers` type: (ids: string[]) => Promise
},
createResponse(id, users) {
return users.find((user) => user.id === id)! // return type: User
}
})// `fetchUsers` will be called only once
mergedRequest(userId1).then((user) => console.log(user))
mergedRequest(userId2).then((user) => console.log(user))
mergedRequest(userId3).then((user) => console.log(user))
```