diff --git a/README.md b/README.md index 60d0a18d..074cb056 100644 --- a/README.md +++ b/README.md @@ -47,8 +47,7 @@ Node Install with npm install @octokit/graphql ```js -const { graphql } = require("@octokit/graphql"); -// or: import { graphql } from "@octokit/graphql"; +import { graphql } from "@octokit/graphql"; ``` @@ -108,7 +107,7 @@ const { repository } = await graphqlWithAuth(` For more complex authentication strategies such as GitHub Apps or Basic, we recommend the according authentication library exported by [`@octokit/auth`](https://github.com/octokit/auth.js). ```js -const { createAppAuth } = require("@octokit/auth-app"); +const { createAppAuth } = await import("@octokit/auth-app"); const auth = createAppAuth({ appId: process.env.APP_ID, privateKey: process.env.PRIVATE_KEY, @@ -167,7 +166,7 @@ const { repository } = await graphql( ### Pass query together with headers and variables ```js -const { graphql } = require("@octokit/graphql"); +import { graphql } from("@octokit/graphql"); const { repository } = await graphql({ query: `query lastIssues($owner: String!, $repo: String!, $num: Int = 3) { repository(owner: $owner, name: $repo) { @@ -191,7 +190,7 @@ const { repository } = await graphql({ ### Use with GitHub Enterprise ```js -let { graphql } = require("@octokit/graphql"); +import { graphql } from "@octokit/graphql"; graphql = graphql.defaults({ baseUrl: "https://github-enterprise.acme-inc.com/api", headers: { @@ -216,8 +215,8 @@ const { repository } = await graphql(` ### Use custom `@octokit/request` instance ```js -const { request } = require("@octokit/request"); -const { withCustomRequest } = require("@octokit/graphql"); +import { request } from "@octokit/request"; +import { withCustomRequest } from "@octokit/graphql"; let requestCounter = 0; const myRequest = request.defaults({ @@ -267,7 +266,7 @@ In case of a GraphQL error, `error.message` is set to a combined message describ All errors can be accessed at `error.errors`. `error.request` has the request options such as query, variables and headers set for easier debugging. ```js -let { graphql, GraphqlResponseError } = require("@octokit/graphql"); +import { graphql, GraphqlResponseError } from "@octokit/graphql"; graphql = graphql.defaults({ headers: { authorization: `token secret123`, @@ -314,7 +313,7 @@ try { A GraphQL query may respond with partial data accompanied by errors. In this case we will throw an error but the partial data will still be accessible through `error.data` ```js -let { graphql } = require("@octokit/graphql"); +import { graphql } from "@octokit/graphql"; graphql = graphql.defaults({ headers: { authorization: `token secret123`, @@ -379,10 +378,10 @@ try { You can pass a replacement for [the built-in fetch implementation](https://github.com/bitinn/node-fetch) as `request.fetch` option. For example, using [fetch-mock](http://www.wheresrhys.co.uk/fetch-mock/) works great to write tests ```js -const assert = require("assert"); -const fetchMock = require("fetch-mock/es5/server"); +import assert from "assert"; +import fetchMock from "fetch-mock"; -const { graphql } = require("@octokit/graphql"); +import { graphql } from "@octokit/graphql"; graphql("{ viewer { login } }", { headers: { diff --git a/package-lock.json b/package-lock.json index 5bdeee67..4a29ba68 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,12 +9,12 @@ "version": "0.0.0-development", "license": "MIT", "dependencies": { - "@octokit/request": "^8.0.1", + "@octokit/request": "^9.0.0", "@octokit/types": "^12.0.0", - "universal-user-agent": "^6.0.0" + "universal-user-agent": "^7.0.0" }, "devDependencies": { - "@octokit/tsconfig": "^2.0.0", + "@octokit/tsconfig": "^3.0.0", "@types/fetch-mock": "^7.2.5", "@types/jest": "^29.0.0", "@types/node": "^20.0.0", @@ -24,8 +24,8 @@ "jest": "^29.0.0", "prettier": "3.2.5", "semantic-release-plugin-update-version-in-files": "^1.0.0", - "ts-jest": "^29.0.0", - "typescript": "^5.0.0" + "ts-jest": "^29.1.0", + "typescript": "^5.3.0" }, "engines": { "node": ">= 18" @@ -1707,12 +1707,12 @@ } }, "node_modules/@octokit/endpoint": { - "version": "9.0.4", - "resolved": "https://registry.npmjs.org/@octokit/endpoint/-/endpoint-9.0.4.tgz", - "integrity": "sha512-DWPLtr1Kz3tv8L0UvXTDP1fNwM0S+z6EJpRcvH66orY6Eld4XBMCSYsaWp4xIm61jTWxK68BrR7ibO+vSDnZqw==", + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/@octokit/endpoint/-/endpoint-10.0.0.tgz", + "integrity": "sha512-emBcNDxBdC1y3+knJonS5zhUB/CG6TihubxM2U1/pG/Z1y3a4oV0Gzz3lmkCvWWQI6h3tqBAX9MgCBFp+M68Jw==", "dependencies": { "@octokit/types": "^12.0.0", - "universal-user-agent": "^6.0.0" + "universal-user-agent": "^7.0.2" }, "engines": { "node": ">= 18" @@ -1724,36 +1724,34 @@ "integrity": "sha512-6G+ywGClliGQwRsjvqVYpklIfa7oRPA0vyhPQG/1Feh+B+wU0vGH1JiJ5T25d3g1JZYBHzR2qefLi9x8Gt+cpw==" }, "node_modules/@octokit/request": { - "version": "8.2.0", - "resolved": "https://registry.npmjs.org/@octokit/request/-/request-8.2.0.tgz", - "integrity": "sha512-exPif6x5uwLqv1N1irkLG1zZNJkOtj8bZxuVHd71U5Ftuxf2wGNvAJyNBcPbPC+EBzwYEbBDdSFb8EPcjpYxPQ==", + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/@octokit/request/-/request-9.0.0.tgz", + "integrity": "sha512-hShsrZ40W8dz3TkyD5ifHNcrYfYpYF7Sh2x+ZKafA+eO0zNeKDTsbF1E0xrUf0mVYGeKL+NLMO5SnfAP22l05Q==", "dependencies": { - "@octokit/endpoint": "^9.0.0", - "@octokit/request-error": "^5.0.0", + "@octokit/endpoint": "^10.0.0", + "@octokit/request-error": "^6.0.1", "@octokit/types": "^12.0.0", - "universal-user-agent": "^6.0.0" + "universal-user-agent": "^7.0.2" }, "engines": { "node": ">= 18" } }, "node_modules/@octokit/request-error": { - "version": "5.0.1", - "resolved": "https://registry.npmjs.org/@octokit/request-error/-/request-error-5.0.1.tgz", - "integrity": "sha512-X7pnyTMV7MgtGmiXBwmO6M5kIPrntOXdyKZLigNfQWSEQzVxR4a4vo49vJjTWX70mPndj8KhfT4Dx+2Ng3vnBQ==", + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/@octokit/request-error/-/request-error-6.0.1.tgz", + "integrity": "sha512-EPeILOAEGqwmEG6g7dYlbvqkjJ5nWNcZvQpgeLEn1MieIcJ0xK0CJtvVcIMXqzIM8DOWzvI6zL5FQAwjTvk/Tg==", "dependencies": { - "@octokit/types": "^12.0.0", - "deprecation": "^2.0.0", - "once": "^1.4.0" + "@octokit/types": "^12.0.0" }, "engines": { "node": ">= 18" } }, "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": { @@ -2628,11 +2626,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", @@ -5238,6 +5231,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" } @@ -6134,9 +6128,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.13", @@ -6347,7 +6341,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 97b5ee9f..1aea91dc 100644 --- a/package.json +++ b/package.json @@ -4,13 +4,14 @@ "publishConfig": { "access": "public" }, + "type": "module", "description": "GitHub GraphQL API client for browsers and Node", "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 lint -s", - "test": "jest --coverage" + "test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest --coverage" }, "repository": "github:octokit/graphql.js", "keywords": [ @@ -22,12 +23,12 @@ "author": "Gregor Martynus (https://github.com/gr2m)", "license": "MIT", "dependencies": { - "@octokit/request": "^8.0.1", + "@octokit/request": "^9.0.0", "@octokit/types": "^12.0.0", - "universal-user-agent": "^6.0.0" + "universal-user-agent": "^7.0.0" }, "devDependencies": { - "@octokit/tsconfig": "^2.0.0", + "@octokit/tsconfig": "^3.0.0", "@types/fetch-mock": "^7.2.5", "@types/jest": "^29.0.0", "@types/node": "^20.0.0", @@ -37,15 +38,19 @@ "jest": "^29.0.0", "prettier": "3.2.5", "semantic-release-plugin-update-version-in-files": "^1.0.0", - "ts-jest": "^29.0.0", - "typescript": "^5.0.0" + "ts-jest": "^29.1.0", + "typescript": "^5.3.0" }, "jest": { + "extensionsToTreatAsEsm": [ + ".ts" + ], "transform": { "^.+\\.(ts|tsx)$": [ "ts-jest", { - "tsconfig": "test/tsconfig.test.json" + "tsconfig": "test/tsconfig.test.json", + "useESM": true } ] }, diff --git a/scripts/build.mjs b/scripts/build.mjs index 60a7f18b..1451e7ca 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -39,27 +39,14 @@ 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, - }), - ]); + await esbuild.build({ + entryPoints, + outdir: "pkg/dist-bundle", + bundle: true, + platform: "neutral", + format: "esm", + ...sharedOptions, + }); // Copy the README, LICENSE to the pkg folder await copyFile("LICENSE", "pkg/LICENSE"); @@ -78,10 +65,12 @@ async function main() { { ...pkg, files: ["dist-*/**", "bin/**"], - main: "dist-node/index.js", - module: "dist-web/index.js", - types: "dist-types/index.d.ts", - source: "dist-src/index.js", + exports: { + ".": { + types: "./dist-types/index.d.ts", + import: "./dist-bundle/index.js", + }, + }, sideEffects: false, }, null, diff --git a/test/graphql.test.ts b/test/graphql.test.ts index 81e2f3c5..caa66746 100644 --- a/test/graphql.test.ts +++ b/test/graphql.test.ts @@ -1,10 +1,10 @@ import fetchMock from "fetch-mock"; import { getUserAgent } from "universal-user-agent"; -import * as OctokitTypes from "@octokit/types"; +import type * as OctokitTypes from "@octokit/types"; import { graphql } from "../src"; import { VERSION } from "../src/version"; -import { RequestParameters } from "../src/types"; +import type { RequestParameters } from "../src/types"; const userAgent = `octokit-graphql.js/${VERSION} ${getUserAgent()}`; 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/**/*"]