Skip to content

Commit 90ee847

Browse files
committed
fix: add tests
1 parent bd9eca7 commit 90ee847

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

nodejs/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ export class API {
6262
`Too many requests (${err.response.status} ${err.response.statusText})`,
6363
err.response.status,
6464
err.response.statusText,
65-
parseInt(err.response.headers['x-ratelimit-limit'], 10),
66-
parseInt(err.response.headers['x-ratelimit-remaining'], 10),
65+
parseInt(err.response.headers['x-ratelimit-userlimit'], 10),
66+
parseInt(err.response.headers['x-ratelimit-userremaining'], 10),
6767
parseInt(err.response.headers['x-ratelimit-userreset'], 10),
6868
)
6969
} else {

nodejs/tests/api.spec.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { server } from './mock'
22
import { API } from '../src'
3+
import { rest } from 'msw'
4+
import { TooManyRequestsError } from '../src/error'
35

46
let client: API
57

@@ -34,3 +36,53 @@ test('getMe unwrapped', async () => {
3436
expect(response).toHaveProperty('userPath')
3537
expect(response).toHaveProperty('photo')
3638
})
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

Comments
 (0)