Skip to content

Commit c5f266f

Browse files
authored
refactor: use full relative paths in imports (#552)
* refactor: use full relative paths This should help bring ESM compatibility in the future * chore: update jest config to correctly resolve modules * fix: add some missing file extensions * test: run the `validate:ts` script with `allowImportingTsExtensions` * build: use `node:` specifier imports * chore: switch to use `.js` imports in source
1 parent 6ead0b7 commit c5f266f

25 files changed

+74
-58
lines changed

jest.config.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@ const config: Config.InitialOptions = {
1111
},
1212
},
1313
transform: {
14-
"^.+\\.tsx?$": ["ts-jest",
14+
"^.+\\.tsx?$": [
15+
"ts-jest",
1516
{
1617
tsconfig: "test/tsconfig.json",
17-
}
18+
},
1819
],
1920
},
21+
moduleNameMapper: {
22+
"ipaddr.js": "<rootDir>/node_modules/ipaddr.js/lib/ipaddr.js",
23+
"^(.+)\\.jsx?$": "$1",
24+
},
2025
restoreMocks: true,
2126
testEnvironment: "node",
2227
testRegex: /test\/.*\/.*.test.ts/u.source,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"lint:fix": "prettier --write 'src/**/*.{ts,json}' 'scripts/**/*' 'test/**/*.ts' README.md package.json",
1515
"pretest": "npm run -s lint",
1616
"test": "jest --coverage",
17-
"validate:ts": "tsc --noEmit --noImplicitAny --target es2020 --esModuleInterop --moduleResolution node test/typescript-validate.ts"
17+
"validate:ts": "tsc --noEmit --noImplicitAny --target es2020 --esModuleInterop --moduleResolution node16 --module node16 --allowImportingTsExtensions test/typescript-validate.ts"
1818
},
1919
"prettier": {},
2020
"release": {

scripts/build.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import esbuild from "esbuild";
2-
import { copyFile, readFile, writeFile, rm } from "fs/promises";
2+
import { copyFile, readFile, writeFile, rm } from "node:fs/promises";
33
import { glob } from "glob";
44

55
const sharedOptions = {

scripts/generate-types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env ts-node-transpile-only
22

3-
import { strict as assert } from "assert";
4-
import * as fs from "fs";
3+
import { strict as assert } from "node:assert";
4+
import * as fs from "node:fs";
55
import type { JSONSchema7, JSONSchema7Definition } from "json-schema";
66
import { format } from "prettier";
77

src/event-handler/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import { createLogger } from "../createLogger";
1+
import { createLogger } from "../createLogger.js";
22
import type {
33
EmitterWebhookEvent,
44
EmitterWebhookEventName,
55
HandlerFunction,
66
Options,
77
State,
88
WebhookEventHandlerError,
9-
} from "../types";
9+
} from "../types.js";
1010
import {
1111
receiverOn as on,
1212
receiverOnAny as onAny,
1313
receiverOnError as onError,
14-
} from "./on";
15-
import { receiverHandle as receive } from "./receive";
16-
import { removeListener } from "./remove-listener";
14+
} from "./on.js";
15+
import { receiverHandle as receive } from "./receive.js";
16+
import { removeListener } from "./remove-listener.js";
1717

1818
interface EventHandler<TTransformed> {
1919
on<E extends EmitterWebhookEventName>(

src/event-handler/on.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { emitterEventNames } from "../generated/webhook-names";
1+
import { emitterEventNames } from "../generated/webhook-names.js";
22
import type {
33
EmitterWebhookEvent,
44
EmitterWebhookEventName,
55
State,
66
WebhookEventHandlerError,
7-
} from "../types";
7+
} from "../types.js";
88

99
function handleEventHandlers(
1010
state: State,

src/event-handler/receive.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import type {
66
State,
77
WebhookError,
88
WebhookEventHandlerError,
9-
} from "../types";
10-
import { wrapErrorHandler } from "./wrap-error-handler";
9+
} from "../types.js";
10+
import { wrapErrorHandler } from "./wrap-error-handler.js";
1111

1212
type EventAction = Extract<
1313
EmitterWebhookEvent["payload"],

src/event-handler/remove-listener.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { EmitterWebhookEventName, State } from "../types";
1+
import type { EmitterWebhookEventName, State } from "../types.js";
22

33
export function removeListener(
44
state: State,

src/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { createLogger } from "./createLogger";
2-
import { createEventHandler } from "./event-handler/index";
1+
import { createLogger } from "./createLogger.js";
2+
import { createEventHandler } from "./event-handler/index.js";
33
import { sign, verify } from "@octokit/webhooks-methods";
4-
import { verifyAndReceive } from "./verify-and-receive";
4+
import { verifyAndReceive } from "./verify-and-receive.js";
55
import type {
66
EmitterWebhookEvent,
77
EmitterWebhookEventName,
@@ -12,10 +12,10 @@ import type {
1212
WebhookError,
1313
WebhookEventHandlerError,
1414
EmitterWebhookEventWithStringPayloadAndSignature,
15-
} from "./types";
15+
} from "./types.js";
1616

17-
export { createNodeMiddleware } from "./middleware/node/index";
18-
export { emitterEventNames } from "./generated/webhook-names";
17+
export { createNodeMiddleware } from "./middleware/node/index.js";
18+
export { emitterEventNames } from "./generated/webhook-names.js";
1919

2020
// U holds the return value of `transform` function in Options
2121
class Webhooks<TTransformed = unknown> {

src/middleware/node/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { createLogger } from "../../createLogger";
2-
import type { Webhooks } from "../../index";
3-
import { middleware } from "./middleware";
4-
import type { MiddlewareOptions } from "./types";
1+
import { createLogger } from "../../createLogger.js";
2+
import type { Webhooks } from "../../index.js";
3+
import { middleware } from "./middleware.js";
4+
import type { MiddlewareOptions } from "./types.js";
55

66
export function createNodeMiddleware(
77
webhooks: Webhooks,

src/middleware/node/middleware.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ type ServerResponse = any;
66

77
import type { WebhookEventName } from "@octokit/webhooks-types";
88

9-
import type { Webhooks } from "../../index";
10-
import type { WebhookEventHandlerError } from "../../types";
11-
import type { MiddlewareOptions } from "./types";
12-
import { getMissingHeaders } from "./get-missing-headers";
13-
import { getPayload } from "./get-payload";
14-
import { onUnhandledRequestDefault } from "./on-unhandled-request-default";
9+
import type { Webhooks } from "../../index.js";
10+
import type { WebhookEventHandlerError } from "../../types.js";
11+
import type { MiddlewareOptions } from "./types.js";
12+
import { getMissingHeaders } from "./get-missing-headers.js";
13+
import { getPayload } from "./get-payload.js";
14+
import { onUnhandledRequestDefault } from "./on-unhandled-request-default.js";
1515

1616
export async function middleware(
1717
webhooks: Webhooks,

src/middleware/node/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Logger } from "../../createLogger";
1+
import type { Logger } from "../../createLogger.ts";
22

33
export type MiddlewareOptions = {
44
path?: string;

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import type {
33
WebhookEventMap,
44
WebhookEventName,
55
} from "@octokit/webhooks-types";
6-
import type { Logger } from "./createLogger";
7-
import type { emitterEventNames } from "./generated/webhook-names";
6+
import type { Logger } from "./createLogger.ts";
7+
import type { emitterEventNames } from "./generated/webhook-names.ts";
88

99
export type EmitterWebhookEventName = (typeof emitterEventNames)[number];
1010
export type EmitterWebhookEvent<

src/verify-and-receive.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
EmitterWebhookEvent,
77
EmitterWebhookEventWithStringPayloadAndSignature,
88
State,
9-
} from "./types";
9+
} from "./types.js";
1010

1111
export async function verifyAndReceive(
1212
state: State & { secret: string },

test/integration/event-handler-test.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
import { createEventHandler } from "../../src/event-handler";
2-
import { EmitterWebhookEvent, WebhookEventHandlerError } from "../../src/types";
3-
import { installationCreatedPayload, pushEventPayload } from "../fixtures";
1+
import { createEventHandler } from "../../src/event-handler/index.ts";
2+
import type {
3+
EmitterWebhookEvent,
4+
WebhookEventHandlerError,
5+
} from "../../src/types.ts";
6+
import {
7+
installationCreatedPayload,
8+
pushEventPayload,
9+
} from "../fixtures/index.ts";
410

511
test("events", async () => {
612
const eventHandler = createEventHandler({});

test/integration/node-middleware.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { createServer } from "http";
2-
import { readFileSync } from "fs";
1+
import { createServer } from "node:http";
2+
import { readFileSync } from "node:fs";
33

44
import { sign } from "@octokit/webhooks-methods";
55

66
// import without types
77
const express = require("express");
88

9-
import { createNodeMiddleware, Webhooks } from "../../src";
9+
import { createNodeMiddleware, Webhooks } from "../../src/index.ts";
1010

1111
const pushEventPayload = readFileSync(
1212
"test/fixtures/push-payload.json",

test/integration/smoke-test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { Webhooks, createEventHandler, emitterEventNames } from "../../src";
1+
import {
2+
Webhooks,
3+
createEventHandler,
4+
emitterEventNames,
5+
} from "../../src/index.ts";
26

37
test("@octokit/webhooks", () => {
48
const emitWarningSpy = jest.spyOn(process, "emitWarning");

test/integration/webhooks.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { readFileSync } from "fs";
1+
import { readFileSync } from "node:fs";
22

33
import { sign } from "@octokit/webhooks-methods";
44

5-
import { Webhooks } from "../../src";
5+
import { Webhooks } from "../../src/index.ts";
66

77
const pushEventPayloadString = readFileSync(
88
"test/fixtures/push-payload.json",

test/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"extends": "..",
33
"compilerOptions": {
4-
"verbatimModuleSyntax": false
4+
"verbatimModuleSyntax": false,
5+
"allowImportingTsExtensions": true
56
},
67
"include": ["**/*.ts"]
78
}

test/typescript-validate.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import {
22
Webhooks,
33
createEventHandler,
4-
EmitterWebhookEvent,
4+
type EmitterWebhookEvent,
55
WebhookError,
66
createNodeMiddleware,
7-
} from "../src/index";
7+
} from "../src/index.ts";
88
import { createServer } from "http";
9-
import {
9+
import type {
1010
HandlerFunction,
1111
RemoveHandlerFunction,
1212
EmitterWebhookEventName,
13-
} from "../src/types";
13+
} from "../src/types.ts";
1414

1515
// ************************************************************
1616
// THIS CODE IS NOT EXECUTED. IT IS FOR TYPECHECKING ONLY

test/unit/createLogger-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createLogger } from "../../src/createLogger";
1+
import { createLogger } from "../../src/createLogger.ts";
22

33
const noop = () => {};
44

test/unit/event-handler-on-test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { receiverOn } from "../../src/event-handler/on";
2-
import { State } from "../../src/types";
1+
import { receiverOn } from "../../src/event-handler/on.ts";
2+
import type { State } from "../../src/types.ts";
33

44
function noop() {}
55

test/unit/event-handler-receive-test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { receiverHandle as receive } from "../../src/event-handler/receive";
2-
import { State } from "../../src/types";
1+
import { receiverHandle as receive } from "../../src/event-handler/receive.ts";
2+
import type { State } from "../../src/types.ts";
33

44
const state: State = {
55
secret: "mysecret",

test/unit/event-handler-remove-listener-test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { removeListener } from "../../src/event-handler/remove-listener";
2-
import { State } from "../../src/types";
1+
import { removeListener } from "../../src/event-handler/remove-listener.ts";
2+
import type { State } from "../../src/types.ts";
33

44
test("remove-listener: single listener", () => {
55
const push = () => {};

test/unit/event-handler-wrap-error-handler-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { wrapErrorHandler } from "../../src/event-handler/wrap-error-handler";
1+
import { wrapErrorHandler } from "../../src/event-handler/wrap-error-handler.ts";
22

33
const noop = () => {};
44

0 commit comments

Comments
 (0)