Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: v6 #401

Merged
merged 13 commits into from
Feb 23, 2024
28 changes: 8 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 12 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
"publishConfig": {
"access": "public"
},
"type": "module",
"description": "Error class for Octokit request errors",
"scripts": {
"build": "node scripts/build.mjs && tsc -p tsconfig.json",
"lint": "prettier --check '{src,test}/**/*' README.md package.json",
"lint:fix": "prettier --write '{src,test}/**/*' README.md package.json",
"pretest": "npm run -s lint",
"test": "jest --coverage"
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest --coverage"
},
"repository": "github:octokit/request-error.js",
"keywords": [
Expand All @@ -22,15 +23,12 @@
"author": "Gregor Martynus (https://github.com/gr2m)",
"license": "MIT",
"dependencies": {
"@octokit/types": "^12.0.0",
"deprecation": "^2.0.0",
"once": "^1.4.0"
"@octokit/types": "^12.0.0"
},
"devDependencies": {
"@octokit/tsconfig": "^2.0.0",
"@octokit/tsconfig": "^3.0.0",
"@types/jest": "^29.0.0",
"@types/node": "^20.0.0",
"@types/once": "^1.4.0",
"esbuild": "^0.20.0",
"glob": "^10.2.6",
"jest": "^29.0.0",
Expand All @@ -39,14 +37,21 @@
"typescript": "^5.0.0"
},
"jest": {
"extensionsToTreatAsEsm": [
".ts"
],
"transform": {
"^.+\\.(ts|tsx)$": [
"ts-jest",
{
"tsconfig": "test/tsconfig.test.json"
"tsconfig": "test/tsconfig.test.json",
"useESM": true
}
]
},
"moduleNameMapper": {
"^(\\.{1,2}/.*)\\.js$": "$1"
},
"coverageThreshold": {
"global": {
"statements": 100,
Expand Down
18 changes: 13 additions & 5 deletions scripts/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async function main() {
bundle: true,
platform: "node",
target: "node18",
format: "cjs",
format: "esm",
...sharedOptions,
}),
// Build an ESM browser bundle
Expand Down Expand Up @@ -74,10 +74,18 @@ async function main() {
{
...pkg,
files: ["dist-*/**", "bin/**"],
main: "dist-node/index.js",
browser: "dist-web/index.js",
types: "dist-types/index.d.ts",
module: "dist-src/index.js",
exports: {
".": {
node: {
types: "./dist-types/index.d.ts",
import: "./dist-node/index.js",
},
browser: {
types: "./dist-types/index.d.ts",
import: "./dist-web/index.js",
}
}
},
sideEffects: false,
unpkg: "dist-web/index.js",
},
Expand Down
55 changes: 2 additions & 53 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import { Deprecation } from "deprecation";
import once from "once";
const logOnceCode = once((deprecation: any) => console.warn(deprecation));
const logOnceHeaders = once((deprecation: any) => console.warn(deprecation));

import type {
RequestOptions,
ResponseHeaders,
OctokitResponse,
} from "@octokit/types";
import type { RequestErrorOptions } from "./types";
import type { RequestOptions, OctokitResponse } from "@octokit/types";
import type { RequestErrorOptions } from "./types.js";

/**
* Error with extra properties to help with debugging
Expand All @@ -21,25 +12,11 @@ export class RequestError extends Error {
*/
status: number;

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

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

/**
* error response headers
*
* @deprecated `error.headers` is deprecated in favor of `error.response.headers`
*/
headers!: ResponseHeaders;

/**
* Response object if a response was received
*/
Expand All @@ -60,15 +37,9 @@ export class RequestError extends Error {

this.name = "HttpError";
this.status = statusCode;
let headers: ResponseHeaders;

if ("headers" in options && typeof options.headers !== "undefined") {
headers = options.headers;
}

if ("response" in options) {
this.response = options.response;
headers = options.response.headers;
}

// redact request credentials without mutating original request options
Expand All @@ -91,27 +62,5 @@ export class RequestError extends Error {
.replace(/\baccess_token=\w+/g, "access_token=[REDACTED]");

this.request = requestCopy;

// deprecations
Object.defineProperty(this, "code", {
get() {
logOnceCode(
new Deprecation(
"[@octokit/request-error] `error.code` is deprecated, use `error.status`.",
),
);
return statusCode;
},
});
Object.defineProperty(this, "headers", {
get() {
logOnceHeaders(
new Deprecation(
"[@octokit/request-error] `error.headers` is deprecated, use `error.response.headers`.",
),
);
return headers || {};
},
});
}
}
20 changes: 5 additions & 15 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import type {
RequestOptions,
ResponseHeaders,
OctokitResponse,
} from "@octokit/types";
import type { RequestOptions, OctokitResponse } from "@octokit/types";

export type RequestErrorOptions =
| {
/** @deprecated set `response` instead */
headers?: ResponseHeaders;
request: RequestOptions;
}
| {
response: OctokitResponse<unknown>;
request: RequestOptions;
};
export type RequestErrorOptions = {
response?: OctokitResponse<unknown>;
request: RequestOptions;
};
33 changes: 8 additions & 25 deletions test/request-error.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { RequestError } from "../src";
import type { RequestErrorOptions } from "../src/types";
import { RequestError } from "../src/index.js";
import type { RequestErrorOptions } from "../src/types.js";

const mockOptions: RequestErrorOptions = {
request: {
method: "GET",
url: "https://api.github.com/",
headers: {},
},
response: {
headers: {},
status: 200,
url: "https://api.github.com/",
data: {},
},
};

describe("RequestError", () => {
Expand Down Expand Up @@ -130,27 +136,4 @@ describe("RequestError", () => {
url: "https://api.github.com/",
});
});

test("deprecates .code", () => {
global.console.warn = jest.fn();
expect(new RequestError("test", 123, mockOptions).code).toEqual(123);
expect(new RequestError("test", 404, mockOptions).code).toEqual(404);
expect(console.warn).toHaveBeenCalledTimes(1);
});

test("deprecates .headers", () => {
global.console.warn = jest.fn();
expect(new RequestError("test", 123, mockOptions).headers).toStrictEqual(
{},
);
expect(
new RequestError("test", 404, { ...mockOptions, headers: { foo: "bar" } })
.headers,
).toStrictEqual({ foo: "bar" });
expect(
new RequestError("test", 404, { ...mockOptions, headers: undefined })
.headers,
).toStrictEqual({});
expect(console.warn).toHaveBeenCalledTimes(1);
});
});
3 changes: 1 addition & 2 deletions test/tsconfig.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"extends": "../tsconfig.json",
"compilerOptions": {
"emitDeclarationOnly": false,
"noEmit": true,
"verbatimModuleSyntax": false
"noEmit": true
},
"include": ["src/**/*"]
}
Loading