Skip to content

Commit

Permalink
feat: add wrapResponseErrors option
Browse files Browse the repository at this point in the history
  • Loading branch information
Yukaii committed Feb 17, 2023
1 parent 66fa86f commit bd9eca7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 26 deletions.
16 changes: 14 additions & 2 deletions nodejs/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,23 @@ class HttpResponseError extends HackMDError {

class MissingRequiredArgument extends HackMDError {}
class InternalServerError extends HttpResponseError {}

class TooManyRequestsError extends HttpResponseError {
public constructor (
message: string,
readonly code: number,
readonly statusText: string,
readonly userLimit: number,
readonly userRemaining: number,
readonly resetAfter?: number,
) {
super(message, code, statusText)
}
}

export {
HackMDError,
HttpResponseError,
MissingRequiredArgument,
InternalServerError
InternalServerError,
TooManyRequestsError,
}
63 changes: 39 additions & 24 deletions nodejs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ const defaultOption: RequestOptions = {

type OptionReturnType<Opt, T> = Opt extends { unwrapData: false } ? AxiosResponse<T> : Opt extends { unwrapData: true } ? T : T

export type APIClientOptions = {
wrapResponseErrors: boolean
}

export class API {
private axios: AxiosInstance

constructor (readonly accessToken: string, public hackmdAPIEndpointURL: string = "https://api.hackmd.io/v1") {
constructor (readonly accessToken: string, public hackmdAPIEndpointURL: string = "https://api.hackmd.io/v1", public options: APIClientOptions = { wrapResponseErrors: true }) {
if (!accessToken) {
throw new HackMDErrors.MissingRequiredArgument('Missing access token when creating HackMD client')
}
Expand All @@ -37,30 +41,41 @@ export class API {
}
)

this.axios.interceptors.response.use(
(response: AxiosResponse) => {
return response
},
async (err: AxiosError) => {
if (!err.response) {
return Promise.reject(err)
}

if (err.response.status >= 500) {
throw new HackMDErrors.InternalServerError(
`HackMD internal error (${err.response.status} ${err.response.statusText})`,
err.response.status,
err.response.statusText,
)
} else {
throw new HackMDErrors.HttpResponseError(
`Received an error response (${err.response.status} ${err.response.statusText}) from HackMD`,
err.response.status,
err.response.statusText,
)
if (options.wrapResponseErrors) {
this.axios.interceptors.response.use(
(response: AxiosResponse) => {
return response
},
async (err: AxiosError) => {
if (!err.response) {
return Promise.reject(err)
}

if (err.response.status >= 500) {
throw new HackMDErrors.InternalServerError(
`HackMD internal error (${err.response.status} ${err.response.statusText})`,
err.response.status,
err.response.statusText,
)
} else if (err.response.status === 429) {
throw new HackMDErrors.TooManyRequestsError(
`Too many requests (${err.response.status} ${err.response.statusText})`,
err.response.status,
err.response.statusText,
parseInt(err.response.headers['x-ratelimit-limit'], 10),
parseInt(err.response.headers['x-ratelimit-remaining'], 10),
parseInt(err.response.headers['x-ratelimit-userreset'], 10),
)
} else {
throw new HackMDErrors.HttpResponseError(
`Received an error response (${err.response.status} ${err.response.statusText}) from HackMD`,
err.response.status,
err.response.statusText,
)
}
}
}
)
)
}
}

async getMe<Opt extends RequestOptions> (options = defaultOption as Opt): Promise<OptionReturnType<Opt, GetMe>> {
Expand Down

0 comments on commit bd9eca7

Please sign in to comment.