From a65b4c361c7595358776ccc55307f907ecbe2732 Mon Sep 17 00:00:00 2001 From: wolfy1339 <4595477+wolfy1339@users.noreply.github.com> Date: Thu, 9 May 2024 12:56:40 -0400 Subject: [PATCH] feat: v7 (#605) BREAKING CHANGE: package is now ESM --- .github/workflows/release.yml | 3 - .gitignore | 1 + README.md | 17 ++-- package-lock.json | 161 ++++++++++++++++------------------ package.json | 28 ++++-- scripts/build.mjs | 44 ++++------ scripts/fix-package-json.js | 17 ---- src/index.ts | 43 +++++---- test/smoke.test.ts | 48 +++++++--- test/tsconfig.test.json | 1 - 10 files changed, 187 insertions(+), 176 deletions(-) delete mode 100644 scripts/fix-package-json.js diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index c21bcdee..8d3d6ba0 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -22,9 +22,6 @@ jobs: cache: npm - run: npm ci - run: npm run build - # https://github.com/octokit/action.js/pull/494 - - name: "Fix pkg.files file pattern" - run: node scripts/fix-package-json.js - run: npx semantic-release env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index d08c6b36..332114b7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ coverage node_modules/ pkg/ +.vscode/ diff --git a/README.md b/README.md index f9a57eaa..b3050cc3 100644 --- a/README.md +++ b/README.md @@ -23,14 +23,19 @@ Node Install with `npm install @octokit/action` ```js -const { Octokit } = require("@octokit/action"); -// or: import { Octokit } from "@octokit/action"; +import { Octokit } from "@octokit/action"; ``` +> [!IMPORTANT] +> As we use [conditional exports](https://nodejs.org/api/packages.html#conditional-exports), you will need to adapt your `tsconfig.json` by setting `"moduleResolution": "node16", "module": "node16"`. +> +> See the TypeScript docs on [package.json "exports"](https://www.typescriptlang.org/docs/handbook/modules/reference.html#packagejson-exports).
+> See this [helpful guide on transitioning to ESM](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c) from [@sindresorhus](https://github.com/sindresorhus) + You can pass `secret.GITHUB_TOKEN` or any of your own secrets to a Node.js script. For example ```yml @@ -59,7 +64,7 @@ Setting `GITHUB_TOKEN` on either [`with:`](https://help.github.com/en/actions/re ```js // .github/actions/my-script.js -const { Octokit } = require("@octokit/action"); +import { Octokit } from "@octokit/action"; const octokit = new Octokit(); @@ -69,7 +74,7 @@ const octokit = new Octokit(); ### Create an issue using REST API ```js -const { Octokit } = require("@octokit/action"); +import { Octokit } from "@octokit/action"; const octokit = new Octokit(); const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/"); @@ -88,7 +93,7 @@ You can also use `octokit.issues.create({ owner, repo, title })`. See the [REST ### Create an issue using GraphQL ```js -const { Octokit } = require("@octokit/action"); +import { Octokit } from "@octokit/action"; const octokit = new Octokit(); const eventPayload = require(process.env.GITHUB_EVENT_PATH); @@ -130,7 +135,7 @@ type ChecksCreateResponse = ### Proxy Servers -If you use [self-hosted runners](https://docs.github.com/en/actions/hosting-your-own-runners/about-self-hosted-runners) and require a proxy server to access internet resources then you will need to ensure that you have correctly configured the runner for [proxy servers](https://docs.github.com/en/actions/hosting-your-own-runners/using-a-proxy-server-with-self-hosted-runners). `@octokit/action` will pick up the configured proxy server environment variables and configure `@octokit/core` with the correct `request.agent` using [proxy-agent](https://github.com/TooTallNate/node-proxy-agent/blob/master/index.js). If you need to supply a different `request.agent` then you should ensure that it handles proxy servers if needed. +If you use [self-hosted runners](https://docs.github.com/en/actions/hosting-your-own-runners/about-self-hosted-runners) and require a proxy server to access internet resources then you will need to ensure that you have correctly configured the runner for [proxy servers](https://docs.github.com/en/actions/hosting-your-own-runners/using-a-proxy-server-with-self-hosted-runners). `@octokit/action` will pick up the configured proxy server environment variables and configure `@octokit/core` with the correct `request.dispatcher` using [ProxyAgent](https://undici.nodejs.org/#/docs/api/ProxyAgent). If you need to supply a different `request.dispatcher` then you should ensure that it handles proxy servers if needed. ## How it works diff --git a/package-lock.json b/package-lock.json index 5b29d9d9..181604c8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,15 +9,15 @@ "version": "0.0.0-development", "license": "MIT", "dependencies": { - "@octokit/auth-action": "^4.0.0", - "@octokit/core": "^5.0.0", - "@octokit/plugin-paginate-rest": "^9.0.0", - "@octokit/plugin-rest-endpoint-methods": "^10.0.0", - "@octokit/types": "^12.0.0", + "@octokit/auth-action": "^5.1.1", + "@octokit/core": "^6.1.2", + "@octokit/plugin-paginate-rest": "^11.1.1", + "@octokit/plugin-rest-endpoint-methods": "^13.0.1", + "@octokit/types": "^13.0.0", "undici": "^6.0.0" }, "devDependencies": { - "@octokit/tsconfig": "^2.0.0", + "@octokit/tsconfig": "^3.0.0", "@types/jest": "^29.0.0", "@types/node": "^20.0.0", "esbuild": "^0.21.0", @@ -1710,11 +1710,11 @@ } }, "node_modules/@octokit/auth-action": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/@octokit/auth-action/-/auth-action-4.1.0.tgz", - "integrity": "sha512-m+3t7K46IYyMk7Bl6/lF4Rv09GqDZjYmNg8IWycJ2Fa3YE3DE7vQcV6G2hUPmR9NDqenefNJwVtlisMjzymPiQ==", + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/@octokit/auth-action/-/auth-action-5.1.1.tgz", + "integrity": "sha512-JE2gbAZcwwVuww88YY7oB97P6eVAPgKZk2US9Uyz+ZUw5ubeRkZqog7G/gUEAjayIFt8s0UX3qNntP1agVcB0g==", "dependencies": { - "@octokit/auth-token": "^4.0.0", + "@octokit/auth-token": "^5.0.0", "@octokit/types": "^13.0.0" }, "engines": { @@ -1735,25 +1735,25 @@ } }, "node_modules/@octokit/auth-token": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/@octokit/auth-token/-/auth-token-4.0.0.tgz", - "integrity": "sha512-tY/msAuJo6ARbK6SPIxZrPBms3xPbfwBrulZe0Wtr/DIY9lje2HeV1uoebShn6mx7SjCHif6EjMvoREj+gZ+SA==", + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/@octokit/auth-token/-/auth-token-5.1.1.tgz", + "integrity": "sha512-rh3G3wDO8J9wSjfI436JUKzHIxq8NaiL0tVeB2aXmG6p/9859aUOAjA9pmSPNGGZxfwmaJ9ozOJImuNVJdpvbA==", "engines": { "node": ">= 18" } }, "node_modules/@octokit/core": { - "version": "5.2.0", - "resolved": "https://registry.npmjs.org/@octokit/core/-/core-5.2.0.tgz", - "integrity": "sha512-1LFfa/qnMQvEOAdzlQymH0ulepxbxnCYAKJZfMci/5XJyIHWgEYnDmgnKakbTh7CH2tFQ5O60oYDvns4i9RAIg==", - "dependencies": { - "@octokit/auth-token": "^4.0.0", - "@octokit/graphql": "^7.1.0", - "@octokit/request": "^8.3.1", - "@octokit/request-error": "^5.1.0", + "version": "6.1.2", + "resolved": "https://registry.npmjs.org/@octokit/core/-/core-6.1.2.tgz", + "integrity": "sha512-hEb7Ma4cGJGEUNOAVmyfdB/3WirWMg5hDuNFVejGEDFqupeOysLc2sG6HJxY2etBp5YQu5Wtxwi020jS9xlUwg==", + "dependencies": { + "@octokit/auth-token": "^5.0.0", + "@octokit/graphql": "^8.0.0", + "@octokit/request": "^9.0.0", + "@octokit/request-error": "^6.0.1", "@octokit/types": "^13.0.0", - "before-after-hook": "^2.2.0", - "universal-user-agent": "^6.0.0" + "before-after-hook": "^3.0.2", + "universal-user-agent": "^7.0.0" }, "engines": { "node": ">= 18" @@ -1773,12 +1773,12 @@ } }, "node_modules/@octokit/endpoint": { - "version": "9.0.5", - "resolved": "https://registry.npmjs.org/@octokit/endpoint/-/endpoint-9.0.5.tgz", - "integrity": "sha512-ekqR4/+PCLkEBF6qgj8WqJfvDq65RH85OAgrtnVp1mSxaXF03u2xW/hUdweGS5654IlC0wkNYC18Z50tSYTAFw==", + "version": "10.1.1", + "resolved": "https://registry.npmjs.org/@octokit/endpoint/-/endpoint-10.1.1.tgz", + "integrity": "sha512-JYjh5rMOwXMJyUpj028cu0Gbp7qe/ihxfJMLc8VZBMMqSwLgOxDI1911gV4Enl1QSavAQNJcwmwBF9M0VvLh6Q==", "dependencies": { - "@octokit/types": "^13.1.0", - "universal-user-agent": "^6.0.0" + "@octokit/types": "^13.0.0", + "universal-user-agent": "^7.0.2" }, "engines": { "node": ">= 18" @@ -1798,13 +1798,13 @@ } }, "node_modules/@octokit/graphql": { - "version": "7.1.0", - "resolved": "https://registry.npmjs.org/@octokit/graphql/-/graphql-7.1.0.tgz", - "integrity": "sha512-r+oZUH7aMFui1ypZnAvZmn0KSqAUgE1/tUXIWaqUCa1758ts/Jio84GZuzsvUkme98kv0WFY8//n0J1Z+vsIsQ==", + "version": "8.1.1", + "resolved": "https://registry.npmjs.org/@octokit/graphql/-/graphql-8.1.1.tgz", + "integrity": "sha512-ukiRmuHTi6ebQx/HFRCXKbDlOh/7xEV6QUXaE7MJEKGNAncGI/STSbOkl12qVXZrfZdpXctx5O9X1AIaebiDBg==", "dependencies": { - "@octokit/request": "^8.3.0", + "@octokit/request": "^9.0.0", "@octokit/types": "^13.0.0", - "universal-user-agent": "^6.0.0" + "universal-user-agent": "^7.0.0" }, "engines": { "node": ">= 18" @@ -1824,60 +1824,58 @@ } }, "node_modules/@octokit/openapi-types": { - "version": "20.0.0", - "resolved": "https://registry.npmjs.org/@octokit/openapi-types/-/openapi-types-20.0.0.tgz", - "integrity": "sha512-EtqRBEjp1dL/15V7WiX5LJMIxxkdiGJnabzYx5Apx4FkQIFgAfKumXeYAqqJCj1s+BMX4cPFIFC4OLCR6stlnA==" + "version": "22.1.0", + "resolved": "https://registry.npmjs.org/@octokit/openapi-types/-/openapi-types-22.1.0.tgz", + "integrity": "sha512-pGUdSP+eEPfZiQHNkZI0U01HLipxncisdJQB4G//OAmfeO8sqTQ9KRa0KF03TUPCziNsoXUrTg4B2Q1EX++T0Q==" }, "node_modules/@octokit/plugin-paginate-rest": { - "version": "9.2.1", - "resolved": "https://registry.npmjs.org/@octokit/plugin-paginate-rest/-/plugin-paginate-rest-9.2.1.tgz", - "integrity": "sha512-wfGhE/TAkXZRLjksFXuDZdmGnJQHvtU/joFQdweXUgzo1XwvBCD4o4+75NtFfjfLK5IwLf9vHTfSiU3sLRYpRw==", + "version": "11.1.1", + "resolved": "https://registry.npmjs.org/@octokit/plugin-paginate-rest/-/plugin-paginate-rest-11.1.1.tgz", + "integrity": "sha512-joUIZu9TupD4pRXLmWShD1Ur/oxYf/bJjYcnaopmGTReNrmWwcW7DUGSrWOjoTeihnlDig+a79m8koiafc4XQw==", "dependencies": { - "@octokit/types": "^12.6.0" + "@octokit/types": "^13.4.0" }, "engines": { "node": ">= 18" }, "peerDependencies": { - "@octokit/core": "5" + "@octokit/core": ">=6" } }, "node_modules/@octokit/plugin-rest-endpoint-methods": { - "version": "10.4.1", - "resolved": "https://registry.npmjs.org/@octokit/plugin-rest-endpoint-methods/-/plugin-rest-endpoint-methods-10.4.1.tgz", - "integrity": "sha512-xV1b+ceKV9KytQe3zCVqjg+8GTGfDYwaT1ATU5isiUyVtlVAO3HNdzpS4sr4GBx4hxQ46s7ITtZrAsxG22+rVg==", + "version": "13.0.1", + "resolved": "https://registry.npmjs.org/@octokit/plugin-rest-endpoint-methods/-/plugin-rest-endpoint-methods-13.0.1.tgz", + "integrity": "sha512-TuIntHqaf2tbIhhPTLERaJKj6moauSHSksBKsgCAoT/vVlTceCosWmMlYUO/un4ftoYNWBYkntToOgJNlPMFmw==", "dependencies": { - "@octokit/types": "^12.6.0" + "@octokit/types": "^13.4.0" }, "engines": { "node": ">= 18" }, "peerDependencies": { - "@octokit/core": "5" + "@octokit/core": ">=6" } }, "node_modules/@octokit/request": { - "version": "8.4.0", - "resolved": "https://registry.npmjs.org/@octokit/request/-/request-8.4.0.tgz", - "integrity": "sha512-9Bb014e+m2TgBeEJGEbdplMVWwPmL1FPtggHQRkV+WVsMggPtEkLKPlcVYm/o8xKLkpJ7B+6N8WfQMtDLX2Dpw==", + "version": "9.1.1", + "resolved": "https://registry.npmjs.org/@octokit/request/-/request-9.1.1.tgz", + "integrity": "sha512-pyAguc0p+f+GbQho0uNetNQMmLG1e80WjkIaqqgUkihqUp0boRU6nKItXO4VWnr+nbZiLGEyy4TeKRwqaLvYgw==", "dependencies": { - "@octokit/endpoint": "^9.0.1", - "@octokit/request-error": "^5.1.0", + "@octokit/endpoint": "^10.0.0", + "@octokit/request-error": "^6.0.1", "@octokit/types": "^13.1.0", - "universal-user-agent": "^6.0.0" + "universal-user-agent": "^7.0.2" }, "engines": { "node": ">= 18" } }, "node_modules/@octokit/request-error": { - "version": "5.1.0", - "resolved": "https://registry.npmjs.org/@octokit/request-error/-/request-error-5.1.0.tgz", - "integrity": "sha512-GETXfE05J0+7H2STzekpKObFe765O5dlAKUTLNGeH+x47z7JjXHfsHKo5z21D/o/IOZTUEI6nyWyR+bZVP/n5Q==", + "version": "6.1.1", + "resolved": "https://registry.npmjs.org/@octokit/request-error/-/request-error-6.1.1.tgz", + "integrity": "sha512-1mw1gqT3fR/WFvnoVpY/zUM2o/XkMs/2AszUUG9I69xn0JFLv6PGkPhNk5lbfvROs79wiS0bqiJNxfCZcRJJdg==", "dependencies": { - "@octokit/types": "^13.1.0", - "deprecation": "^2.0.0", - "once": "^1.4.0" + "@octokit/types": "^13.0.0" }, "engines": { "node": ">= 18" @@ -1910,17 +1908,17 @@ } }, "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.1.0", + "resolved": "https://registry.npmjs.org/@octokit/tsconfig/-/tsconfig-3.1.0.tgz", + "integrity": "sha512-3jGTGqDnnh/MZlg/sf21J/0cghsmaSnG+ZPK+o++sQwUwgrLVtfbUi/BANHgf22SRnxhdYtOoRX90I9/cP+9BA==", "dev": true }, "node_modules/@octokit/types": { - "version": "12.6.0", - "resolved": "https://registry.npmjs.org/@octokit/types/-/types-12.6.0.tgz", - "integrity": "sha512-1rhSOfRa6H9w4YwK0yrf5faDaDTb+yLyBUKOCV4xtCDB5VmIPqd/v9yr9o6SAzOAlRxMiRiCic6JVM1/kunVkw==", + "version": "13.4.1", + "resolved": "https://registry.npmjs.org/@octokit/types/-/types-13.4.1.tgz", + "integrity": "sha512-Y73oOAzRBAUzR/iRAbGULzpNkX8vaxKCqEtg6K74Ff3w9f5apFnWtE/2nade7dMWWW3bS5Kkd6DJS4HF04xreg==", "dependencies": { - "@octokit/openapi-types": "^20.0.0" + "@octokit/openapi-types": "^22.1.0" } }, "node_modules/@pkgjs/parseargs": { @@ -2316,9 +2314,9 @@ "dev": true }, "node_modules/before-after-hook": { - "version": "2.2.3", - "resolved": "https://registry.npmjs.org/before-after-hook/-/before-after-hook-2.2.3.tgz", - "integrity": "sha512-NzUnlZexiaH/46WDhANlyR2bXRopNg4F/zuSA3OpZnllCUgRaOF2znDioDWrmbNVsuZk6l9pMquQB38cfBZwkQ==" + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/before-after-hook/-/before-after-hook-3.0.2.tgz", + "integrity": "sha512-Nik3Sc0ncrMK4UUdXQmAnRtzmNQTAAXmXIopizwZ1W1t8QmfJj+zL4OA2I7XPTPW5z5TDqv4hRo/JzouDJnX3A==" }, "node_modules/brace-expansion": { "version": "2.0.1", @@ -2419,9 +2417,9 @@ } }, "node_modules/caniuse-lite": { - "version": "1.0.30001616", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001616.tgz", - "integrity": "sha512-RHVYKov7IcdNjVHJFNY/78RdG4oGVjbayxv8u5IO74Wv7Hlq4PnJE6mo/OjFijjVFNy5ijnCt6H3IIo4t+wfEw==", + "version": "1.0.30001610", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001610.tgz", + "integrity": "sha512-QFutAY4NgaelojVMjY63o6XlZyORPaLfyMnsl3HgnWdJUcX6K0oaJymHjH8PT5Gk7sTm8rvC/c5COUQKXqmOMA==", "dev": true, "funding": [ { @@ -2786,11 +2784,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", @@ -2816,9 +2809,9 @@ "dev": true }, "node_modules/electron-to-chromium": { - "version": "1.4.758", - "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.758.tgz", - "integrity": "sha512-/o9x6TCdrYZBMdGeTifAP3wlF/gVT+TtWJe3BSmtNh92Mw81U9hrYwW9OAGUh+sEOX/yz5e34sksqRruZbjYrw==", + "version": "1.4.738", + "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.738.tgz", + "integrity": "sha512-lwKft2CLFztD+vEIpesrOtCrko/TFnEJlHFdRhazU7Y/jx5qc4cqsocfVrBg4So4gGe9lvxnbLIoev47WMpg+A==", "dev": true }, "node_modules/emittery": { @@ -5333,6 +5326,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" } @@ -6216,9 +6210,9 @@ "dev": true }, "node_modules/universal-user-agent": { - "version": "6.0.1", - "resolved": "https://registry.npmjs.org/universal-user-agent/-/universal-user-agent-6.0.1.tgz", - "integrity": "sha512-yCzhz6FN2wU1NiiQRogkTQszlQSlpWaw8SvVegAc+bDxbzHgh1vX8uIe8OYyMH6DwH+sdTJsgMl36+mSMdRJIQ==" + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/universal-user-agent/-/universal-user-agent-7.0.2.tgz", + "integrity": "sha512-0JCqzSKnStlRRQfCdowvqy3cy0Dvtlb8xecj/H8JFZuCze4rwjPZQOgvFvn0Ws/usCHQFGpyr+pB9adaGwXn4Q==" }, "node_modules/update-browserslist-db": { "version": "1.0.15", @@ -6429,7 +6423,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 2db86a8c..251ce552 100644 --- a/package.json +++ b/package.json @@ -5,14 +5,15 @@ "access": "public", "provenance": true }, + "type": "module", "description": "GitHub API client for GitHub Actions", "scripts": { "build": "node scripts/build.mjs && tsc -p tsconfig.json", "lint": "prettier --check '{src,test}/**/*.{js,ts,css,less,scss,vue,json,gql,md}' README.md package.json", "lint:fix": "prettier --write '{src,test}/**/*.{js,ts,css,less,scss,vue,json,gql,md}' README.md package.json", "pretest": "npm run -s lint", - "test": "jest --coverage", - "test:typescript": "npx tsc --noEmit --declaration --noUnusedLocals --esModuleInterop --allowImportingTsExtensions test/typescript-validate.ts" + "test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest --coverage", + "test:typescript": "npx tsc --noEmit --declaration --noUnusedLocals --esModuleInterop --module node16 --allowImportingTsExtensions test/typescript-validate.ts" }, "repository": "github:octokit/action.js", "keywords": [ @@ -25,15 +26,15 @@ "author": "Gregor Martynus (https://twitter.com/gr2m)", "license": "MIT", "dependencies": { - "@octokit/auth-action": "^4.0.0", - "@octokit/core": "^5.0.0", - "@octokit/plugin-paginate-rest": "^9.0.0", - "@octokit/plugin-rest-endpoint-methods": "^10.0.0", - "@octokit/types": "^12.0.0", + "@octokit/auth-action": "^5.1.1", + "@octokit/core": "^6.1.2", + "@octokit/plugin-paginate-rest": "^11.1.1", + "@octokit/plugin-rest-endpoint-methods": "^13.0.1", + "@octokit/types": "^13.0.0", "undici": "^6.0.0" }, "devDependencies": { - "@octokit/tsconfig": "^2.0.0", + "@octokit/tsconfig": "^3.0.0", "@types/jest": "^29.0.0", "@types/node": "^20.0.0", "esbuild": "^0.21.0", @@ -46,11 +47,15 @@ "typescript": "^5.0.0" }, "jest": { + "extensionsToTreatAsEsm": [ + ".ts" + ], "transform": { "^.+\\.(ts|tsx)$": [ "ts-jest", { - "tsconfig": "test/tsconfig.test.json" + "tsconfig": "test/tsconfig.test.json", + "useESM": true } ] }, @@ -62,6 +67,11 @@ "lines": 100 } }, + "testPathIgnorePatterns": [ + "/node_modules/", + "/test/deno/" + ], + "testEnvironment": "node", "moduleNameMapper": { "^(.+)\\.jsx?$": "$1" } diff --git a/scripts/build.mjs b/scripts/build.mjs index 67f82f14..b510e0e4 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -8,6 +8,9 @@ const sharedOptions = { minify: false, allowOverwrite: true, packages: "external", + format: "esm", + platform: "neutral", + target: "es2022" }; async function main() { @@ -18,8 +21,6 @@ async function main() { entryPoints: await glob(["./src/*.ts", "./src/**/*.ts"]), outdir: "pkg/dist-src", bundle: false, - platform: "neutral", - format: "esm", ...sharedOptions, sourcemap: false, }); @@ -35,27 +36,13 @@ async function main() { const entryPoints = ["./pkg/dist-src/index.js"]; - await Promise.all([ - // Build the a CJS Node.js bundle - esbuild.build({ - entryPoints, - outdir: "pkg/dist-node", - bundle: true, - platform: "node", - target: "node18", - format: "cjs", - ...sharedOptions, - }), - // Build an ESM browser bundle - esbuild.build({ - entryPoints, - outdir: "pkg/dist-web", - bundle: true, - platform: "browser", - format: "esm", - ...sharedOptions, - }), - ]); + // Build the source code for Node.js + await esbuild.build({ + entryPoints, + outdir: "pkg/dist-bundle", + bundle: true, + ...sharedOptions, + }); // Copy the README, LICENSE to the pkg folder await copyFile("LICENSE", "pkg/LICENSE"); @@ -74,10 +61,15 @@ 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: { + ".": { + types: "./dist-types/index.d.ts", + import: "./dist-bundle/index.js", + // Tooling currently are having issues with the "exports" field when there is no "default", ex: TypeScript, eslint + default: "./dist-bundle/index.js", + } + }, sideEffects: false, }, null, diff --git a/scripts/fix-package-json.js b/scripts/fix-package-json.js deleted file mode 100644 index 560664d7..00000000 --- a/scripts/fix-package-json.js +++ /dev/null @@ -1,17 +0,0 @@ -// Script to fix the package.json files map. This is needed because npm changed the file matching behavior in npm@9 -// https://github.com/octokit/action.js/pull/494 -const fs = require("fs"); -const path = require("path"); -const { EOL } = require("os"); - -const pkgPath = path.join(__dirname, "../pkg/package.json"); -const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8")); - -pkg.files = pkg.files.map((file) => { - if (file.endsWith("/")) { - return file + "**"; - } - return file; -}); - -fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + EOL, "utf8"); diff --git a/src/index.ts b/src/index.ts index 1bc5f86e..56354a5d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,12 +1,16 @@ import { Octokit as Core } from "@octokit/core"; +import type { Constructor, OctokitOptions } from "@octokit/core/types"; import { createActionAuth } from "@octokit/auth-action"; -import { paginateRest } from "@octokit/plugin-paginate-rest"; +import { + paginateRest, + type PaginateInterface, +} from "@octokit/plugin-paginate-rest"; import { legacyRestEndpointMethods } from "@octokit/plugin-rest-endpoint-methods"; -export type { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods"; +import { fetch as undiciFetch, ProxyAgent } from "undici"; import { VERSION } from "./version.js"; -import type { OctokitOptions } from "@octokit/core/dist-types/types.js"; -import { fetch as undiciFetch, ProxyAgent } from "undici"; + +export type { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods"; const DEFAULTS = { authStrategy: createActionAuth, @@ -28,6 +32,7 @@ export function getProxyAgent() { return undefined; } +/* istanbul ignore next */ export const customFetch = async function (url: string, opts: any) { return await undiciFetch(url, { dispatcher: getProxyAgent(), @@ -35,19 +40,23 @@ export const customFetch = async function (url: string, opts: any) { }); }; -export const Octokit = Core.plugin( - paginateRest, - legacyRestEndpointMethods, -).defaults(function buildDefaults(options: OctokitOptions): OctokitOptions { - return { - ...DEFAULTS, - ...options, - request: { - fetch: customFetch, - ...options.request, - }, - }; -}); +export const Octokit: typeof Core & + Constructor< + { + paginate: PaginateInterface; + } & ReturnType + > = Core.plugin(paginateRest, legacyRestEndpointMethods).defaults( + function buildDefaults(options: OctokitOptions): OctokitOptions { + return { + ...DEFAULTS, + ...options, + request: { + fetch: customFetch, + ...options.request, + }, + }; + }, +); export type Octokit = InstanceType; diff --git a/test/smoke.test.ts b/test/smoke.test.ts index b7d541da..944805a0 100644 --- a/test/smoke.test.ts +++ b/test/smoke.test.ts @@ -1,20 +1,28 @@ import fetchMock from "fetch-mock"; import { createServer, type Server } from "node:https"; -import { Octokit, getProxyAgent, customFetch } from "../src/index.ts"; -import { ProxyAgent } from "undici"; +import { Octokit, getProxyAgent } from "../src/index.ts"; +import { + type Dispatcher, + type RequestInfo, + type RequestInit, + Response, + ProxyAgent, +} from "undici"; +import { jest } from "@jest/globals"; // mock undici such that we can substitute our own fetch implementation // but use the actual ProxyAgent implementation for most tests. the // exception is "should call undiciFetch with the correct dispatcher" // where we want to validate that a mocked ProxyAgent is passed through // to undici.fetch. -jest.mock("undici", () => { +jest.unstable_mockModule("undici", () => { return { + __esModule: true, fetch: jest.fn(), - ProxyAgent: jest.requireActual("undici").ProxyAgent, + ProxyAgent, }; }); -const undici = jest.requireMock("undici"); +const undici = await import("undici"); describe("Smoke test", () => { let server: Server; @@ -279,30 +287,42 @@ describe("Smoke test", () => { it("should call undiciFetch with the correct dispatcher", async () => { process.env.HTTPS_PROXY = "https://127.0.0.1"; const expectedAgent = new ProxyAgent("https://127.0.0.1"); - - jest.mock("../src", () => { - const actualModule = jest.requireActual("../src"); + const actualModule = await import("../src/index.js"); + jest.unstable_mockModule("../src/index.js", () => { return { + __esModule: true, ...actualModule, getProxyAgent: jest.fn(() => expectedAgent), + customFetch: async function (url: string, opts: any) { + return await undici.fetch(url, { + dispatcher: getProxyAgent(), + ...opts, + }); + }, }; }); - expect(JSON.stringify(getProxyAgent())).toBe( + + const { + customFetch: customFetchMocked, + getProxyAgent: getProxyAgentMocked, + } = await import("../src/index.js"); + + expect(JSON.stringify(getProxyAgentMocked())).toBe( JSON.stringify(expectedAgent), ); // mock undici.fetch to extract the `dispatcher` option passed in. // this allows us to verify that `customFetch` correctly sets // the dispatcher to `expectedAgent` when HTTPS_PROXY is set. - let dispatcher: any; - (undici.fetch as jest.Mock).mockImplementation( - (_url: string, options: any) => { - dispatcher = options.dispatcher; + let dispatcher: Dispatcher | undefined; + (undici.fetch as jest.Mock).mockImplementation( + (_url: RequestInfo, options?: RequestInit) => { + dispatcher = options?.dispatcher; return Promise.resolve(new Response()); }, ); - await customFetch("http://api.github.com", {}); + await customFetchMocked("http://api.github.com", {}); expect(JSON.stringify(dispatcher)).toEqual(JSON.stringify(expectedAgent)); }); }); diff --git a/test/tsconfig.test.json b/test/tsconfig.test.json index 5e8dc248..cf1f2005 100644 --- a/test/tsconfig.test.json +++ b/test/tsconfig.test.json @@ -3,7 +3,6 @@ "compilerOptions": { "emitDeclarationOnly": false, "noEmit": true, - "verbatimModuleSyntax": false, "allowImportingTsExtensions": true }, "include": ["src/**/*"]