Skip to content

Commit

Permalink
refactor: use full relative paths in imports (#552)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
wolfy1339 authored Nov 20, 2023
1 parent 6ead0b7 commit c5f266f
Show file tree
Hide file tree
Showing 25 changed files with 74 additions and 58 deletions.
9 changes: 7 additions & 2 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ const config: Config.InitialOptions = {
},
},
transform: {
"^.+\\.tsx?$": ["ts-jest",
"^.+\\.tsx?$": [
"ts-jest",
{
tsconfig: "test/tsconfig.json",
}
},
],
},
moduleNameMapper: {
"ipaddr.js": "<rootDir>/node_modules/ipaddr.js/lib/ipaddr.js",
"^(.+)\\.jsx?$": "$1",
},
restoreMocks: true,
testEnvironment: "node",
testRegex: /test\/.*\/.*.test.ts/u.source,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"lint:fix": "prettier --write 'src/**/*.{ts,json}' 'scripts/**/*' 'test/**/*.ts' README.md package.json",
"pretest": "npm run -s lint",
"test": "jest --coverage",
"validate:ts": "tsc --noEmit --noImplicitAny --target es2020 --esModuleInterop --moduleResolution node test/typescript-validate.ts"
"validate:ts": "tsc --noEmit --noImplicitAny --target es2020 --esModuleInterop --moduleResolution node16 --module node16 --allowImportingTsExtensions test/typescript-validate.ts"
},
"prettier": {},
"release": {
Expand Down
2 changes: 1 addition & 1 deletion scripts/build.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import esbuild from "esbuild";
import { copyFile, readFile, writeFile, rm } from "fs/promises";
import { copyFile, readFile, writeFile, rm } from "node:fs/promises";
import { glob } from "glob";

const sharedOptions = {
Expand Down
4 changes: 2 additions & 2 deletions scripts/generate-types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env ts-node-transpile-only

import { strict as assert } from "assert";
import * as fs from "fs";
import { strict as assert } from "node:assert";
import * as fs from "node:fs";
import type { JSONSchema7, JSONSchema7Definition } from "json-schema";
import { format } from "prettier";

Expand Down
10 changes: 5 additions & 5 deletions src/event-handler/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { createLogger } from "../createLogger";
import { createLogger } from "../createLogger.js";
import type {
EmitterWebhookEvent,
EmitterWebhookEventName,
HandlerFunction,
Options,
State,
WebhookEventHandlerError,
} from "../types";
} from "../types.js";
import {
receiverOn as on,
receiverOnAny as onAny,
receiverOnError as onError,
} from "./on";
import { receiverHandle as receive } from "./receive";
import { removeListener } from "./remove-listener";
} from "./on.js";
import { receiverHandle as receive } from "./receive.js";
import { removeListener } from "./remove-listener.js";

interface EventHandler<TTransformed> {
on<E extends EmitterWebhookEventName>(
Expand Down
4 changes: 2 additions & 2 deletions src/event-handler/on.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { emitterEventNames } from "../generated/webhook-names";
import { emitterEventNames } from "../generated/webhook-names.js";
import type {
EmitterWebhookEvent,
EmitterWebhookEventName,
State,
WebhookEventHandlerError,
} from "../types";
} from "../types.js";

function handleEventHandlers(
state: State,
Expand Down
4 changes: 2 additions & 2 deletions src/event-handler/receive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import type {
State,
WebhookError,
WebhookEventHandlerError,
} from "../types";
import { wrapErrorHandler } from "./wrap-error-handler";
} from "../types.js";
import { wrapErrorHandler } from "./wrap-error-handler.js";

type EventAction = Extract<
EmitterWebhookEvent["payload"],
Expand Down
2 changes: 1 addition & 1 deletion src/event-handler/remove-listener.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { EmitterWebhookEventName, State } from "../types";
import type { EmitterWebhookEventName, State } from "../types.js";

export function removeListener(
state: State,
Expand Down
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createLogger } from "./createLogger";
import { createEventHandler } from "./event-handler/index";
import { createLogger } from "./createLogger.js";
import { createEventHandler } from "./event-handler/index.js";
import { sign, verify } from "@octokit/webhooks-methods";
import { verifyAndReceive } from "./verify-and-receive";
import { verifyAndReceive } from "./verify-and-receive.js";
import type {
EmitterWebhookEvent,
EmitterWebhookEventName,
Expand All @@ -12,10 +12,10 @@ import type {
WebhookError,
WebhookEventHandlerError,
EmitterWebhookEventWithStringPayloadAndSignature,
} from "./types";
} from "./types.js";

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

// U holds the return value of `transform` function in Options
class Webhooks<TTransformed = unknown> {
Expand Down
8 changes: 4 additions & 4 deletions src/middleware/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createLogger } from "../../createLogger";
import type { Webhooks } from "../../index";
import { middleware } from "./middleware";
import type { MiddlewareOptions } from "./types";
import { createLogger } from "../../createLogger.js";
import type { Webhooks } from "../../index.js";
import { middleware } from "./middleware.js";
import type { MiddlewareOptions } from "./types.js";

export function createNodeMiddleware(
webhooks: Webhooks,
Expand Down
12 changes: 6 additions & 6 deletions src/middleware/node/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ type ServerResponse = any;

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

import type { Webhooks } from "../../index";
import type { WebhookEventHandlerError } from "../../types";
import type { MiddlewareOptions } from "./types";
import { getMissingHeaders } from "./get-missing-headers";
import { getPayload } from "./get-payload";
import { onUnhandledRequestDefault } from "./on-unhandled-request-default";
import type { Webhooks } from "../../index.js";
import type { WebhookEventHandlerError } from "../../types.js";
import type { MiddlewareOptions } from "./types.js";
import { getMissingHeaders } from "./get-missing-headers.js";
import { getPayload } from "./get-payload.js";
import { onUnhandledRequestDefault } from "./on-unhandled-request-default.js";

export async function middleware(
webhooks: Webhooks,
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/node/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Logger } from "../../createLogger";
import type { Logger } from "../../createLogger.ts";

export type MiddlewareOptions = {
path?: string;
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type {
WebhookEventMap,
WebhookEventName,
} from "@octokit/webhooks-types";
import type { Logger } from "./createLogger";
import type { emitterEventNames } from "./generated/webhook-names";
import type { Logger } from "./createLogger.ts";
import type { emitterEventNames } from "./generated/webhook-names.ts";

export type EmitterWebhookEventName = (typeof emitterEventNames)[number];
export type EmitterWebhookEvent<
Expand Down
2 changes: 1 addition & 1 deletion src/verify-and-receive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
EmitterWebhookEvent,
EmitterWebhookEventWithStringPayloadAndSignature,
State,
} from "./types";
} from "./types.js";

export async function verifyAndReceive(
state: State & { secret: string },
Expand Down
12 changes: 9 additions & 3 deletions test/integration/event-handler-test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { createEventHandler } from "../../src/event-handler";
import { EmitterWebhookEvent, WebhookEventHandlerError } from "../../src/types";
import { installationCreatedPayload, pushEventPayload } from "../fixtures";
import { createEventHandler } from "../../src/event-handler/index.ts";
import type {
EmitterWebhookEvent,
WebhookEventHandlerError,
} from "../../src/types.ts";
import {
installationCreatedPayload,
pushEventPayload,
} from "../fixtures/index.ts";

test("events", async () => {
const eventHandler = createEventHandler({});
Expand Down
6 changes: 3 additions & 3 deletions test/integration/node-middleware.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { createServer } from "http";
import { readFileSync } from "fs";
import { createServer } from "node:http";
import { readFileSync } from "node:fs";

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

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

import { createNodeMiddleware, Webhooks } from "../../src";
import { createNodeMiddleware, Webhooks } from "../../src/index.ts";

const pushEventPayload = readFileSync(
"test/fixtures/push-payload.json",
Expand Down
6 changes: 5 additions & 1 deletion test/integration/smoke-test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Webhooks, createEventHandler, emitterEventNames } from "../../src";
import {
Webhooks,
createEventHandler,
emitterEventNames,
} from "../../src/index.ts";

test("@octokit/webhooks", () => {
const emitWarningSpy = jest.spyOn(process, "emitWarning");
Expand Down
4 changes: 2 additions & 2 deletions test/integration/webhooks.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { readFileSync } from "fs";
import { readFileSync } from "node:fs";

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

import { Webhooks } from "../../src";
import { Webhooks } from "../../src/index.ts";

const pushEventPayloadString = readFileSync(
"test/fixtures/push-payload.json",
Expand Down
3 changes: 2 additions & 1 deletion test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "..",
"compilerOptions": {
"verbatimModuleSyntax": false
"verbatimModuleSyntax": false,
"allowImportingTsExtensions": true
},
"include": ["**/*.ts"]
}
8 changes: 4 additions & 4 deletions test/typescript-validate.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import {
Webhooks,
createEventHandler,
EmitterWebhookEvent,
type EmitterWebhookEvent,
WebhookError,
createNodeMiddleware,
} from "../src/index";
} from "../src/index.ts";
import { createServer } from "http";
import {
import type {
HandlerFunction,
RemoveHandlerFunction,
EmitterWebhookEventName,
} from "../src/types";
} from "../src/types.ts";

// ************************************************************
// THIS CODE IS NOT EXECUTED. IT IS FOR TYPECHECKING ONLY
Expand Down
2 changes: 1 addition & 1 deletion test/unit/createLogger-test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createLogger } from "../../src/createLogger";
import { createLogger } from "../../src/createLogger.ts";

const noop = () => {};

Expand Down
4 changes: 2 additions & 2 deletions test/unit/event-handler-on-test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { receiverOn } from "../../src/event-handler/on";
import { State } from "../../src/types";
import { receiverOn } from "../../src/event-handler/on.ts";
import type { State } from "../../src/types.ts";

function noop() {}

Expand Down
4 changes: 2 additions & 2 deletions test/unit/event-handler-receive-test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { receiverHandle as receive } from "../../src/event-handler/receive";
import { State } from "../../src/types";
import { receiverHandle as receive } from "../../src/event-handler/receive.ts";
import type { State } from "../../src/types.ts";

const state: State = {
secret: "mysecret",
Expand Down
4 changes: 2 additions & 2 deletions test/unit/event-handler-remove-listener-test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { removeListener } from "../../src/event-handler/remove-listener";
import { State } from "../../src/types";
import { removeListener } from "../../src/event-handler/remove-listener.ts";
import type { State } from "../../src/types.ts";

test("remove-listener: single listener", () => {
const push = () => {};
Expand Down
2 changes: 1 addition & 1 deletion test/unit/event-handler-wrap-error-handler-test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { wrapErrorHandler } from "../../src/event-handler/wrap-error-handler";
import { wrapErrorHandler } from "../../src/event-handler/wrap-error-handler.ts";

const noop = () => {};

Expand Down

0 comments on commit c5f266f

Please sign in to comment.