From 1b9a628b74f36b3e43a1b191c995e28e8aa997d4 Mon Sep 17 00:00:00 2001 From: karashiiro <49822414+karashiiro@users.noreply.github.com> Date: Sat, 18 Jan 2025 23:41:46 -0800 Subject: [PATCH] fix: await ApiError creation in new rate limiter --- package.json | 2 +- src/errors.ts | 2 +- src/rate-limit.test.ts | 41 +++++++++++++++++++++++++++++++++++++++++ src/rate-limit.ts | 4 ++-- src/scraper.test.ts | 24 ------------------------ 5 files changed, 45 insertions(+), 28 deletions(-) create mode 100644 src/rate-limit.test.ts diff --git a/package.json b/package.json index a5462e71..7d5b2235 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "scraper", "crawler" ], - "version": "0.15.0", + "version": "0.15.1", "main": "dist/default/cjs/index.js", "types": "./dist/types/index.d.ts", "exports": { diff --git a/src/errors.ts b/src/errors.ts index 6590be7d..37dd30db 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -1,5 +1,5 @@ export class ApiError extends Error { - private constructor( + constructor( readonly response: Response, readonly data: any, message: string, diff --git a/src/rate-limit.test.ts b/src/rate-limit.test.ts new file mode 100644 index 00000000..e06e6a68 --- /dev/null +++ b/src/rate-limit.test.ts @@ -0,0 +1,41 @@ +import { ApiError } from './errors'; +import { ErrorRateLimitStrategy } from './rate-limit'; + +test('error rate limit strategy throws error when triggered', async () => { + const strategy = new ErrorRateLimitStrategy(); + + await expect(() => + strategy.onRateLimit({ + fetchParameters: ['/', {}], + response: { + headers: new Headers(), + ok: false, + redirected: false, + status: 0, + statusText: '', + type: 'basic', + url: '', + clone: function (): Response { + throw new Error('Function not implemented.'); + }, + body: null, + bodyUsed: false, + arrayBuffer: function (): Promise { + throw new Error('Function not implemented.'); + }, + blob: function (): Promise { + throw new Error('Function not implemented.'); + }, + formData: function (): Promise { + throw new Error('Function not implemented.'); + }, + json: function (): Promise { + throw new Error('Function not implemented.'); + }, + text: function (): Promise { + throw new Error('Function not implemented.'); + }, + }, + }), + ).rejects.toThrow(ApiError); +}); diff --git a/src/rate-limit.ts b/src/rate-limit.ts index d8e8fe3f..232b81bd 100644 --- a/src/rate-limit.ts +++ b/src/rate-limit.ts @@ -70,7 +70,7 @@ export class WaitingRateLimitStrategy implements RateLimitStrategy { * A rate-limiting strategy that throws an {@link ApiError} when a rate limiting event occurs. */ export class ErrorRateLimitStrategy implements RateLimitStrategy { - onRateLimit({ response: res }: RateLimitEvent): Promise { - throw ApiError.fromResponse(res); + async onRateLimit({ response: res }: RateLimitEvent): Promise { + throw await ApiError.fromResponse(res); } } diff --git a/src/scraper.test.ts b/src/scraper.test.ts index 05efd8b5..e88fea97 100644 --- a/src/scraper.test.ts +++ b/src/scraper.test.ts @@ -36,27 +36,3 @@ test('scraper uses response transform when provided', async () => { await expect(scraper.getLatestTweet('twitter')).rejects.toThrowError(); }); - -test('scraper uses error rate limiting strategy when provided', async () => { - const scraper = new Scraper({ - rateLimitStrategy: new ErrorRateLimitStrategy(), - transform: { - response: (response) => - new Proxy(response, { - get(target, p, receiver) { - if (p === 'status') { - return 429; - } - - if (p === 'ok') { - return false; - } - - return Reflect.get(target, p, receiver); - }, - }), - }, - }); - - await expect(scraper.getLatestTweet('twitter')).rejects.toThrow(); -});