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: switch to ESM output #678

Merged
merged 11 commits into from
Feb 24, 2024
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ Node
Install with <code>npm install @octokit/request</code>

```js
const { request } = require("@octokit/request");
// or: import { request } from "@octokit/request";
import { request } from "@octokit/request";
```

</td></tr>
Expand Down
359 changes: 226 additions & 133 deletions package-lock.json

Large diffs are not rendered by default.

22 changes: 14 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "@octokit/request",
"version": "0.0.0-development",
"type": "module",
"publishConfig": {
"access": "public"
},
Expand All @@ -10,7 +11,7 @@
"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.js",
"keywords": [
Expand All @@ -22,36 +23,40 @@
"author": "Gregor Martynus (https://github.com/gr2m)",
"license": "MIT",
"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"
},
"devDependencies": {
"@octokit/auth-app": "^6.0.0",
"@octokit/tsconfig": "^2.0.0",
"@octokit/tsconfig": "^3.0.0",
"@types/fetch-mock": "^7.2.4",
"@types/jest": "^29.0.0",
"@types/lolex": "^5.1.0",
"@types/node": "^20.0.0",
"@types/once": "^1.4.0",
"@types/sinonjs__fake-timers": "^8.1.5",
"@sinonjs/fake-timers": "^11.2.2",
"esbuild": "^0.20.0",
"fetch-mock": "npm:@gr2m/fetch-mock@^9.11.0-pull-request-644.1",
"glob": "^10.2.4",
"jest": "^29.0.0",
"lolex": "^6.0.0",
"prettier": "3.2.5",
"semantic-release-plugin-update-version-in-files": "^1.0.0",
"string-to-arraybuffer": "^1.0.2",
"ts-jest": "^29.0.0",
"typescript": "^5.0.0"
},
"jest": {
"extensionsToTreatAsEsm": [
".ts"
],
"transform": {
"^.+\\.(ts|tsx)$": [
"ts-jest",
{
"tsconfig": "test/tsconfig.test.json"
"tsconfig": "test/tsconfig.test.json",
"useESM": true
}
]
},
Expand All @@ -66,6 +71,7 @@
"modulePathIgnorePatterns": [
"<rootDir>/pkg"
],
"testEnvironment": "node",
"moduleNameMapper": {
"^(.+)\\.jsx?$": "$1"
}
Expand Down
39 changes: 14 additions & 25 deletions scripts/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,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: "node14",
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");
Expand All @@ -74,10 +61,12 @@ 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",
},
},
sideEffects: false,
},
null,
Expand Down
8 changes: 7 additions & 1 deletion src/fetch-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ export default function fetchWrapper(
return fetch(requestOptions.url, {
method: requestOptions.method,
body: requestOptions.body,
headers: requestOptions.headers as HeadersInit,
// Header values must be `string`
headers: Object.fromEntries(
Object.entries(requestOptions.headers).map(([name, value]) => [
name,
String(value),
]),
),
signal: requestOptions.request?.signal,
// duplex must be set if request.body is ReadableStream or Async Iterables.
// See https://fetch.spec.whatwg.org/#dom-requestinit-duplex.
Expand Down
10 changes: 7 additions & 3 deletions test/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@ import { ReadableStream } from "node:stream/web";
import { getUserAgent } from "universal-user-agent";
import fetchMock from "fetch-mock";
import { createAppAuth } from "@octokit/auth-app";
import lolex from "lolex";
import fakeTimers from "@sinonjs/fake-timers";
import type {
EndpointOptions,
RequestInterface,
ResponseHeaders,
} from "@octokit/types";

import { request } from "../src/index.ts";
import { jest } from "@jest/globals";

const userAgent = `octokit-request.js/0.0.0-development ${getUserAgent()}`;
const stringToArrayBuffer = require("string-to-arraybuffer");
const __filename = new URL(import.meta.url);
function stringToArrayBuffer(str: string) {
return new TextEncoder().encode(str).buffer;
}

describe("request()", () => {
it("is a function", () => {
Expand Down Expand Up @@ -69,7 +73,7 @@ describe("request()", () => {
});

it("README authentication example", async () => {
const clock = lolex.install({
const clock = fakeTimers.install({
now: 0,
toFake: ["Date"],
});
Expand Down
1 change: 0 additions & 1 deletion test/tsconfig.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"compilerOptions": {
"emitDeclarationOnly": false,
"noEmit": true,
"verbatimModuleSyntax": false,
"allowImportingTsExtensions": true
},
"include": ["src/**/*"]
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "@octokit/tsconfig",
"compilerOptions": {
"lib": ["dom", "DOM.Iterable", "es2023"],
"esModuleInterop": true,
"declaration": true,
"outDir": "pkg/dist-types",
Expand Down