|
1 | 1 | import { server } from './mock'
|
2 | 2 | import { API } from '../src'
|
| 3 | +import { rest } from 'msw' |
| 4 | +import { TooManyRequestsError } from '../src/error' |
3 | 5 |
|
4 | 6 | let client: API
|
5 | 7 |
|
@@ -34,3 +36,53 @@ test('getMe unwrapped', async () => {
|
34 | 36 | expect(response).toHaveProperty('userPath')
|
35 | 37 | expect(response).toHaveProperty('photo')
|
36 | 38 | })
|
| 39 | + |
| 40 | +test('should throw axios error object if set wrapResponseErrors to false', async () => { |
| 41 | + const customCilent = new API(process.env.HACKMD_ACCESS_TOKEN!, undefined, { |
| 42 | + wrapResponseErrors: false, |
| 43 | + }) |
| 44 | + |
| 45 | + server.use( |
| 46 | + rest.get('https://api.hackmd.io/v1/me', (req, res, ctx) => { |
| 47 | + return res(ctx.status(429)) |
| 48 | + }), |
| 49 | + ) |
| 50 | + |
| 51 | + try { |
| 52 | + await customCilent.getMe() |
| 53 | + } catch (error: any) { |
| 54 | + expect(error).toHaveProperty('response') |
| 55 | + expect(error.response).toHaveProperty('status', 429) |
| 56 | + } |
| 57 | +}) |
| 58 | + |
| 59 | +test.only('should throw HackMD error object', async () => { |
| 60 | + server.use( |
| 61 | + rest.get('https://api.hackmd.io/v1/me', (req, res, ctx) => { |
| 62 | + return res( |
| 63 | + ctx.status(429), |
| 64 | + ctx.set({ |
| 65 | + 'X-RateLimit-UserLimit': '100', |
| 66 | + 'x-RateLimit-UserRemaining': '0', |
| 67 | + 'x-RateLimit-UserReset': String( |
| 68 | + new Date().getTime() + 1000 * 60 * 60 * 24, |
| 69 | + ), |
| 70 | + }), |
| 71 | + ) |
| 72 | + }), |
| 73 | + ) |
| 74 | + |
| 75 | + try { |
| 76 | + await client.getMe() |
| 77 | + } catch (error: any) { |
| 78 | + expect(error).toBeInstanceOf(TooManyRequestsError) |
| 79 | + |
| 80 | + console.log(JSON.stringify(error)) |
| 81 | + |
| 82 | + expect(error).toHaveProperty('code', 429) |
| 83 | + expect(error).toHaveProperty('statusText', 'Too Many Requests') |
| 84 | + expect(error).toHaveProperty('userLimit', 100) |
| 85 | + expect(error).toHaveProperty('userRemaining', 0) |
| 86 | + expect(error).toHaveProperty('resetAfter') |
| 87 | + } |
| 88 | +}) |
0 commit comments