Skip to content

Commit

Permalink
feat: initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed May 16, 2019
1 parent 78e574f commit 881231a
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
78 changes: 78 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Deprecation } from "deprecation";
import once from "once";
const logOnce = once((deprecation: any) => console.warn(deprecation));

import { RequestOptions, ResponseHeaders, HttpErrorOptions } from "./types";

/**
* Error with extra properties to help with debugging
*/
export class HttpError extends Error {
name: "HttpError";

/**
* http status code
*/
status: number;

/**
* http status code
*
* @deprecated `error.code` is deprecated in favor of `error.status`
*/
code!: number;

/**
* error response headers
*/
headers: ResponseHeaders;

/**
* Request options that lead to the error.
*/
request: RequestOptions;

constructor(message: string, statusCode: number, options: HttpErrorOptions) {
super(message);

// Maintains proper stack trace (only available on V8)
/* istanbul ignore next */
if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor);
}

this.name = "HttpError";
this.status = statusCode;
Object.defineProperty(this, "code", {
get() {
logOnce(
new Deprecation(
"[@octokit/request-error] `error.code` is deprecated, use `error.status`."
)
);
return statusCode;
}
});
this.headers = options.headers;

// redact request credentials without mutating original request options
const requestCopy = Object.assign({}, options.request);
if (options.request.headers.authorization) {
requestCopy.headers = Object.assign({}, options.request.headers, {
authorization: options.request.headers.authorization.replace(
/ .*$/,
" [REDACTED]"
)
});
}

// client_id & client_secret can be passed as URL query parameters to increase rate limit
// see https://developer.github.com/v3/#increasing-the-unauthenticated-rate-limit-for-oauth-applications
requestCopy.url = requestCopy.url.replace(
/\bclient_secret=\w+/g,
"client_secret=[REDACTED]"
);

this.request = requestCopy;
}
}
45 changes: 45 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Relative or absolute URL. Examples: `'/orgs/:org'`, `https://example.com/foo/bar`
*/
export type Url = string;

/**
* Request method
*/
export type Method = "DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT";

export type RequestHeaders = {
/**
* Used for API previews and custom formats
*/
accept?: string;
/**
* Redacted authorization header
*/
authorization?: string;

"user-agent"?: string;

[header: string]: string | number | undefined;
};

export type ResponseHeaders = {
[header: string]: string;
};

export type EndpointRequestOptions = {
[option: string]: any;
};

export type RequestOptions = {
method: Method;
url: Url;
headers: RequestHeaders;
body?: any;
request?: EndpointRequestOptions;
};

export type HttpErrorOptions = {
headers: ResponseHeaders;
request: RequestOptions;
};

0 comments on commit 881231a

Please sign in to comment.