forked from blockscout/frontend
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support 429 "Too many requests" API error (blockscout#1004)
* get client key on 429 error * set key to cookie and pass it in query params * move csrf-token header to useApiFetch * pin host for preview * actual layout * support new changes in API * proxy headers from API * add header to request * remove next.js proxy flag * fix ts * refactor * add tests
- Loading branch information
Showing
17 changed files
with
185 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function isBodyAllowed(method: string | undefined | null) { | ||
return method && ![ 'GET', 'HEAD' ].includes(method); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { test, expect } from '@playwright/experimental-ct-react'; | ||
import React from 'react'; | ||
|
||
import TestApp from 'playwright/TestApp'; | ||
import * as configs from 'playwright/utils/configs'; | ||
|
||
import AppErrorTooManyRequests from './AppErrorTooManyRequests'; | ||
|
||
test('default view +@mobile', async({ mount, page }) => { | ||
const component = await mount( | ||
<TestApp> | ||
<AppErrorTooManyRequests/> | ||
</TestApp>, | ||
); | ||
await page.waitForResponse('https://www.google.com/recaptcha/api2/**'); | ||
|
||
await expect(component).toHaveScreenshot({ | ||
mask: [ page.locator('.recaptcha') ], | ||
maskColor: configs.maskColor, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { Box, Heading, Icon, Text, chakra } from '@chakra-ui/react'; | ||
import React from 'react'; | ||
import ReCaptcha from 'react-google-recaptcha'; | ||
|
||
import appConfig from 'configs/app/config'; | ||
import icon429 from 'icons/error-pages/429.svg'; | ||
import buildUrl from 'lib/api/buildUrl'; | ||
import useFetch from 'lib/hooks/useFetch'; | ||
import useToast from 'lib/hooks/useToast'; | ||
|
||
interface Props { | ||
className?: string; | ||
} | ||
|
||
const AppErrorTooManyRequests = ({ className }: Props) => { | ||
const toast = useToast(); | ||
const fetch = useFetch(); | ||
|
||
const handleReCaptchaChange = React.useCallback(async(token: string | null) => { | ||
|
||
if (token) { | ||
try { | ||
const url = buildUrl('api_v2_key'); | ||
|
||
await fetch(url, { | ||
method: 'POST', | ||
body: { recaptcha_response: token }, | ||
credentials: 'include', | ||
}, { | ||
resource: 'api_v2_key', | ||
}); | ||
|
||
window.location.reload(); | ||
|
||
} catch (error) { | ||
toast({ | ||
position: 'top-right', | ||
title: 'Error', | ||
description: 'Unable to get client key.', | ||
status: 'error', | ||
variant: 'subtle', | ||
isClosable: true, | ||
}); | ||
} | ||
} | ||
}, [ toast, fetch ]); | ||
|
||
return ( | ||
<Box | ||
className={ className } | ||
sx={{ | ||
'.recaptcha': { | ||
mt: 8, | ||
h: '78px', // otherwise content will jump after reCaptcha is loaded | ||
}, | ||
}} | ||
> | ||
<Icon as={ icon429 } width="200px" height="auto"/> | ||
<Heading mt={ 8 } size="2xl" fontFamily="body">Too many requests</Heading> | ||
<Text variant="secondary" mt={ 3 }> | ||
You have exceeded the request rate for a given time period. Please reduce the number of requests and try again soon. | ||
</Text> | ||
{ appConfig.reCaptcha.siteKey && ( | ||
<ReCaptcha | ||
className="recaptcha" | ||
sitekey={ appConfig.reCaptcha.siteKey } | ||
onChange={ handleReCaptchaChange } | ||
/> | ||
) } | ||
</Box> | ||
); | ||
}; | ||
|
||
export default chakra(AppErrorTooManyRequests); |
Binary file added
BIN
+22.6 KB
..._screenshots__/AppErrorTooManyRequests.pw.tsx_default_default-view-mobile-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+20.5 KB
...__screenshots__/AppErrorTooManyRequests.pw.tsx_mobile_default-view-mobile-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.