Skip to content

Commit

Permalink
async request beforeRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberCookie committed May 23, 2024
1 parent c715ed1 commit e7bc441
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
6 changes: 4 additions & 2 deletions client_core/network/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ Receives **1** argument - **Object** wit the next fields:

- `beforeParse` - **Function**. Triggered before request **Object** is beeing parsed<br />
Has **1** argument:
- **reqParams** - **ReqParams**. Concrete request's request params. (Read below)
- **reqParams** - **ReqParams**. Concrete request's request params. (Read below)<br />

Returns **void | Promise<void>**

- `beforeRequest` - **Function**. Preprocess mutable request data right before it passed to Fetch API<br />
Has **1** argument:
Expand All @@ -50,7 +52,7 @@ Receives **1** argument - **Object** wit the next fields:
- `headers` - **Request** headers
- `signal` - **Request** signal

Returns **false** | **void**. Return **false** to abort request execution
Returns **boolean | void | Promise<boolean | void>**. Return **false** to abort request execution

- `afterRequest` - **Function**. Triggered after successful request was made<br />
Has **2** arguments:
Expand Down
29 changes: 25 additions & 4 deletions client_core/network/request/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import populateURLParams from '../../../common/populate_url_params'
import isExists from '../../../common/is/exists'

import type { ReqData, ReqError, RequestParams, SetupParams } from './types'

Expand Down Expand Up @@ -107,6 +108,27 @@ async function extractResponseData(req: RequestParams, res: Response): Promise<a
)}
}

async function isAllowedToProcess(
beforeRequest: RequestParams['beforeRequest'],
reqData: ReturnType<typeof extractRequestData>
) {

const currentBeforeReqResult = beforeRequest?.(reqData)
let isAllowedToProcess: boolean | void = true

if (isExists(currentBeforeReqResult)) {
if (typeof currentBeforeReqResult == 'object') {
await currentBeforeReqResult.then(shouldProcess => {
isAllowedToProcess = shouldProcess
})

} else isAllowedToProcess = currentBeforeReqResult
}


return isAllowedToProcess
}


const createApi = (setupParams: SetupParams = {}) => {
const {
Expand Down Expand Up @@ -137,11 +159,10 @@ const createApi = (setupParams: SetupParams = {}) => {
isSameReqPrevent && (reqKey = `${url}_${options.method}_${options.body}`)


if (
beforeRequest?.(reqData) !== false
&& req.beforeRequest?.(reqData) !== false
) {
const globalReqAllowed = await isAllowedToProcess(beforeRequest, reqData)
const isReqAllowed = globalReqAllowed && await isAllowedToProcess(req.beforeRequest, reqData)

if (isReqAllowed) {
try {
if (isSameReqPrevent) {
if (activeRequest.has(reqKey)) {
Expand Down
2 changes: 1 addition & 1 deletion client_core/network/request/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ type SetupParams = {
*
* @param reqData fetch api request params
*/
beforeRequest?(reqData: ReqData): void | false
beforeRequest?(reqData: ReqData): void | boolean | Promise<void | boolean>

/**
* Triggered after successful request was made
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "siegel",
"version": "0.14.91",
"version": "0.14.92",
"homepage": "https://siegel-qe3q.onrender.com",
"url": "https://github.com/CyberCookie/siegel",
"bugs": "https://github.com/CyberCookie/siegel/issues",
Expand Down Expand Up @@ -31,7 +31,7 @@
},
"engineStrict": true,
"engines": {
"node": ">=16",
"node": ">= 16",
"npm": ">= 7"
},
"config": {
Expand Down

0 comments on commit e7bc441

Please sign in to comment.