diff --git a/package-lock.json b/package-lock.json index e9d5e30..870f51e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,15 +9,12 @@ "version": "0.0.0-development", "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", @@ -1490,9 +1487,9 @@ "integrity": "sha512-6G+ywGClliGQwRsjvqVYpklIfa7oRPA0vyhPQG/1Feh+B+wU0vGH1JiJ5T25d3g1JZYBHzR2qefLi9x8Gt+cpw==" }, "node_modules/@octokit/tsconfig": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/@octokit/tsconfig/-/tsconfig-2.0.0.tgz", - "integrity": "sha512-tWnrai3quGt8+gRN2edzo9fmraWekeryXPeXDomMw2oFSpu/lH3VSWGn/q4V+rwjTRMeeXk/ci623/01Zet4VQ==", + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/@octokit/tsconfig/-/tsconfig-3.0.0.tgz", + "integrity": "sha512-tQLwgXYfBq9iUbOq26kWCzsJL6DY7qjOLzqcg5tCFQ4ob48H47iX98NudHW7S5OQ/fpSKYJhb3eQehyBNzYjfA==", "dev": true }, "node_modules/@octokit/types": { @@ -1630,12 +1627,6 @@ "undici-types": "~5.26.4" } }, - "node_modules/@types/once": { - "version": "1.4.4", - "resolved": "https://registry.npmjs.org/@types/once/-/once-1.4.4.tgz", - "integrity": "sha512-KSwEbnmPvnbHLtDj05t0w2CBAYctJ1M6gj/6JfWK+pLeD7/T7QGWGyXHaqLAMOQfDJP+sO/ujPDiPAAlHR3GOg==", - "dev": true - }, "node_modules/@types/stack-utils": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/@types/stack-utils/-/stack-utils-2.0.3.tgz", @@ -2191,11 +2182,6 @@ "node": ">=0.10.0" } }, - "node_modules/deprecation": { - "version": "2.3.1", - "resolved": "https://registry.npmjs.org/deprecation/-/deprecation-2.3.1.tgz", - "integrity": "sha512-xmHIy4F3scKVwMsQ4WnVaS8bHOx0DmVwRywosKhaILI0ywMDWPtBSku2HNxRvF7jtwDRsoEwYQSfbxj8b7RlJQ==" - }, "node_modules/detect-newline": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/detect-newline/-/detect-newline-3.1.0.tgz", @@ -3733,6 +3719,7 @@ "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==", + "dev": true, "dependencies": { "wrappy": "1" } @@ -4686,7 +4673,8 @@ "node_modules/wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", - "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==" + "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", + "dev": true }, "node_modules/write-file-atomic": { "version": "4.0.2", diff --git a/package.json b/package.json index 052d160..170eafa 100644 --- a/package.json +++ b/package.json @@ -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": [ @@ -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", @@ -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, diff --git a/scripts/build.mjs b/scripts/build.mjs index 51561a5..dbd8922 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -43,7 +43,7 @@ async function main() { bundle: true, platform: "node", target: "node18", - format: "cjs", + format: "esm", ...sharedOptions, }), // Build an ESM browser bundle @@ -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", }, diff --git a/src/index.ts b/src/index.ts index 2626118..0fb3ae8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 @@ -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 */ @@ -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 @@ -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 || {}; - }, - }); } } diff --git a/src/types.ts b/src/types.ts index 268c339..4e0e5f6 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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; - request: RequestOptions; - }; +export type RequestErrorOptions = { + response?: OctokitResponse; + request: RequestOptions; +}; diff --git a/test/request-error.test.ts b/test/request-error.test.ts index a14be91..fef4b2c 100644 --- a/test/request-error.test.ts +++ b/test/request-error.test.ts @@ -1,5 +1,5 @@ -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: { @@ -7,6 +7,12 @@ const mockOptions: RequestErrorOptions = { url: "https://api.github.com/", headers: {}, }, + response: { + headers: {}, + status: 200, + url: "https://api.github.com/", + data: {}, + }, }; describe("RequestError", () => { @@ -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); - }); }); diff --git a/test/tsconfig.test.json b/test/tsconfig.test.json index b0961e1..fe99a4e 100644 --- a/test/tsconfig.test.json +++ b/test/tsconfig.test.json @@ -2,8 +2,7 @@ "extends": "../tsconfig.json", "compilerOptions": { "emitDeclarationOnly": false, - "noEmit": true, - "verbatimModuleSyntax": false + "noEmit": true }, "include": ["src/**/*"] }