Skip to content

Commit 80b84e7

Browse files
authored
maint: use ts imports in src (#1144)
allow ts imports in src
1 parent dbc80d3 commit 80b84e7

File tree

18 files changed

+149
-51
lines changed

18 files changed

+149
-51
lines changed

.github/workflows/test.yml

+17-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,20 @@ name: Test
88
- opened
99
- synchronize
1010
jobs:
11+
test-building:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v4
15+
- uses: actions/setup-node@v4
16+
with:
17+
cache: npm
18+
node-version: lts/*
19+
- run: npm ci
20+
- run: npm run build
21+
1122
test_matrix:
23+
needs:
24+
- test-building
1225
runs-on: ubuntu-latest
1326
strategy:
1427
matrix:
@@ -25,9 +38,12 @@ jobs:
2538
cache: npm
2639
- run: npm ci
2740
- run: npm test
41+
2842
test:
2943
runs-on: ubuntu-latest
30-
needs: test_matrix
44+
needs:
45+
- test-building
46+
- test_matrix
3147
steps:
3248
- run: exit 1
3349
if: ${{ needs.test_matrix.result != 'success' }}

scripts/build.mjs

+81
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,87 @@
11
import esbuild from "esbuild";
2+
import { dirname } from "node:path";
3+
import { mkdirSync, writeFileSync } from "node:fs";
24
import { copyFile, readFile, writeFile, rm } from "node:fs/promises";
35
import { glob } from "glob";
46

7+
/**
8+
* Internal function to rewrite relative import extensions.
9+
*
10+
* based on:
11+
* @onyx/esbuild-plugin-rewrite-relative-import-extensions
12+
*
13+
* MIT License
14+
*
15+
* Copyright (c) 2025 Santiago Aguilar Hernández
16+
*
17+
* Permission is hereby granted, free of charge, to any person obtaining a copy
18+
* of this software and associated documentation files (the "Software"), to deal
19+
* in the Software without restriction, including without limitation the rights
20+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21+
* copies of the Software, and to permit persons to whom the Software is
22+
* furnished to do so, subject to the following conditions:
23+
*
24+
* The above copyright notice and this permission notice shall be included in all
25+
* copies or substantial portions of the Software.
26+
*
27+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33+
* SOFTWARE.
34+
*/
35+
export function rewriteRelativeImportExtensionsPlugin() {
36+
return {
37+
name: "rewrite-relative-import-extensions",
38+
setup(build) {
39+
const write = build.initialOptions.write;
40+
build.initialOptions.write = false;
41+
42+
build.onEnd((result) => {
43+
const files = result.outputFiles ?? [];
44+
45+
for (const file of files) {
46+
let output = file.text;
47+
48+
const matches = output.matchAll(
49+
/(?<=(?:import|export\s*[*{])[^"']+["'])([^"']+)(?=["'])/g,
50+
);
51+
52+
for (const match of matches) {
53+
output = output.replaceAll(match[0], (filePath, index) => {
54+
if (match.index !== index) {
55+
return filePath;
56+
}
57+
58+
if (!/^\.\.?\//.test(filePath)) {
59+
return filePath;
60+
}
61+
62+
return filePath.replace(
63+
/\.([jt]sx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i,
64+
function (m, tsx, d, ext, cm) {
65+
return tsx
66+
? ".js"
67+
: d && (!ext || !cm)
68+
? m
69+
: d + ext + "." + cm.toLowerCase() + "js";
70+
},
71+
);
72+
});
73+
}
74+
75+
if (write === undefined || write) {
76+
mkdirSync(dirname(file.path), { recursive: true });
77+
writeFileSync(file.path, output);
78+
}
79+
}
80+
});
81+
},
82+
};
83+
}
84+
585
/** @type {esbuild.BuildOptions} */
686
const sharedOptions = {
787
sourcemap: "external",
@@ -24,6 +104,7 @@ async function main() {
24104
bundle: false,
25105
...sharedOptions,
26106
sourcemap: false,
107+
plugins: [rewriteRelativeImportExtensionsPlugin()],
27108
});
28109

29110
// Remove the types file from the dist-src folder

scripts/generate-types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { strict as assert } from "node:assert";
44
import * as fs from "node:fs";
5-
import type { OpenAPI3, OperationObject, PathItemObject } from "./types.js";
5+
import type { OpenAPI3, OperationObject, PathItemObject } from "./types.ts";
66
import { format } from "prettier";
77

88
const schema = (await import("@octokit/openapi-webhooks")).schemas[
@@ -115,7 +115,7 @@ const run = async () => {
115115
"// THIS FILE IS GENERATED - DO NOT EDIT DIRECTLY",
116116
"// make edits in scripts/generate-types.ts",
117117
"",
118-
"import type { WebhookEventDefinition } from '../types.js';",
118+
"import type { WebhookEventDefinition } from '../types.ts';",
119119
"",
120120
"export type EventPayloadMap = {",
121121
...Object.keys(eventsMap).map(

src/event-handler/index.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createLogger } from "../createLogger.js";
1+
import { createLogger } from "../createLogger.ts";
22
import type {
33
EmitterWebhookEvent,
44
EmitterWebhookEventName,
@@ -7,14 +7,14 @@ import type {
77
State,
88
WebhookError,
99
WebhookEventHandlerError,
10-
} from "../types.js";
10+
} from "../types.ts";
1111
import {
1212
receiverOn as on,
1313
receiverOnAny as onAny,
1414
receiverOnError as onError,
15-
} from "./on.js";
16-
import { receiverHandle as receive } from "./receive.js";
17-
import { removeListener } from "./remove-listener.js";
15+
} from "./on.ts";
16+
import { receiverHandle as receive } from "./receive.ts";
17+
import { removeListener } from "./remove-listener.ts";
1818

1919
export interface EventHandler<TTransformed = unknown> {
2020
on<E extends EmitterWebhookEventName>(

src/event-handler/on.ts

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

99
function handleEventHandlers(
1010
state: State,

src/event-handler/receive.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import type {
44
WebhookError,
55
WebhookEventName,
66
WebhookEventHandlerError,
7-
} from "../types.js";
8-
import { wrapErrorHandler } from "./wrap-error-handler.js";
7+
} from "../types.ts";
8+
import { wrapErrorHandler } from "./wrap-error-handler.ts";
99

1010
type EventAction = Extract<
1111
EmitterWebhookEvent["payload"],

src/event-handler/remove-listener.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { EmitterWebhookEventName, State } from "../types.js";
1+
import type { EmitterWebhookEventName, State } from "../types.ts";
22

33
export function removeListener(
44
state: State,

src/generated/webhook-identifiers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// THIS FILE IS GENERATED - DO NOT EDIT DIRECTLY
22
// make edits in scripts/generate-types.ts
33

4-
import type { WebhookEventDefinition } from "../types.js";
4+
import type { WebhookEventDefinition } from "../types.ts";
55

66
export type EventPayloadMap = {
77
branch_protection_configuration:

src/index.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { createLogger } from "./createLogger.js";
1+
import { createLogger } from "./createLogger.ts";
22
import {
33
createEventHandler,
44
type EventHandler,
5-
} from "./event-handler/index.js";
5+
} from "./event-handler/index.ts";
66
import { sign, verify } from "@octokit/webhooks-methods";
7-
import { verifyAndReceive } from "./verify-and-receive.js";
7+
import { verifyAndReceive } from "./verify-and-receive.ts";
88
import type {
99
EmitterWebhookEvent,
1010
EmitterWebhookEventName,
@@ -15,11 +15,11 @@ import type {
1515
WebhookError,
1616
WebhookEventHandlerError,
1717
EmitterWebhookEventWithStringPayloadAndSignature,
18-
} from "./types.js";
18+
} from "./types.ts";
1919

20-
export { createNodeMiddleware } from "./middleware/node/index.js";
21-
export { createWebMiddleware } from "./middleware/web/index.js";
22-
export { emitterEventNames } from "./generated/webhook-names.js";
20+
export { createNodeMiddleware } from "./middleware/node/index.ts";
21+
export { createWebMiddleware } from "./middleware/web/index.ts";
22+
export { emitterEventNames } from "./generated/webhook-names.ts";
2323

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

src/middleware/node/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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";
1+
import { createLogger } from "../../createLogger.ts";
2+
import type { Webhooks } from "../../index.ts";
3+
import { middleware } from "./middleware.ts";
4+
import type { MiddlewareOptions } from "../types.ts";
55

66
export function createNodeMiddleware(
77
webhooks: Webhooks,

src/middleware/node/middleware.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
// see https://github.com/octokit/octokit.js/issues/2075#issuecomment-817361886
33
import type { IncomingMessage, ServerResponse } from "node:http";
44

5-
import type { Webhooks } from "../../index.js";
6-
import type { WebhookEventHandlerError } from "../../types.js";
7-
import type { MiddlewareOptions } from "../types.js";
8-
import { getMissingHeaders } from "./get-missing-headers.js";
9-
import { getPayload } from "./get-payload.js";
10-
import { onUnhandledRequestDefault } from "./on-unhandled-request-default.js";
5+
import type { Webhooks } from "../../index.ts";
6+
import type { WebhookEventHandlerError } from "../../types.ts";
7+
import type { MiddlewareOptions } from "../types.ts";
8+
import { getMissingHeaders } from "./get-missing-headers.ts";
9+
import { getPayload } from "./get-payload.ts";
10+
import { onUnhandledRequestDefault } from "./on-unhandled-request-default.ts";
1111

1212
export async function middleware(
1313
webhooks: Webhooks,

src/middleware/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Logger } from "../createLogger.js";
1+
import type { Logger } from "../createLogger.ts";
22

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

src/middleware/web/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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";
1+
import { createLogger } from "../../createLogger.ts";
2+
import type { Webhooks } from "../../index.ts";
3+
import { middleware } from "./middleware.ts";
4+
import type { MiddlewareOptions } from "../types.ts";
55

66
export function createWebMiddleware(
77
webhooks: Webhooks,

src/middleware/web/middleware.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import type { WebhookEventName } from "../../generated/webhook-identifiers.js";
2-
3-
import type { Webhooks } from "../../index.js";
4-
import type { WebhookEventHandlerError } from "../../types.js";
5-
import type { MiddlewareOptions } from "../types.js";
6-
import { getMissingHeaders } from "./get-missing-headers.js";
7-
import { getPayload } from "./get-payload.js";
8-
import { onUnhandledRequestDefault } from "./on-unhandled-request-default.js";
1+
import type { WebhookEventName } from "../../generated/webhook-identifiers.ts";
2+
3+
import type { Webhooks } from "../../index.ts";
4+
import type { WebhookEventHandlerError } from "../../types.ts";
5+
import type { MiddlewareOptions } from "../types.ts";
6+
import { getMissingHeaders } from "./get-missing-headers.ts";
7+
import { getPayload } from "./get-payload.ts";
8+
import { onUnhandledRequestDefault } from "./on-unhandled-request-default.ts";
99

1010
export async function middleware(
1111
webhooks: Webhooks,

src/types.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { RequestError } from "@octokit/request-error";
22
import type { webhooks as OpenAPIWebhooks } from "@octokit/openapi-webhooks-types";
3-
import type { EventPayloadMap } from "./generated/webhook-identifiers.js";
4-
import type { Logger } from "./createLogger.js";
5-
import type { EventHandler } from "./event-handler/index.js";
6-
import type { emitterEventNames } from "./generated/webhook-names.js";
3+
import type { EventPayloadMap } from "./generated/webhook-identifiers.ts";
4+
import type { Logger } from "./createLogger.ts";
5+
import type { EventHandler } from "./event-handler/index.ts";
6+
import type { emitterEventNames } from "./generated/webhook-names.ts";
77

88
export type WebhookEventName = keyof EventPayloadMap;
99
export type ExtractEvents<TEventName> =

src/verify-and-receive.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import type {
55
EmitterWebhookEventWithStringPayloadAndSignature,
66
State,
77
WebhookError,
8-
} from "./types.js";
9-
import type { EventHandler } from "./event-handler/index.js";
8+
} from "./types.ts";
9+
import type { EventHandler } from "./event-handler/index.ts";
1010

1111
export async function verifyAndReceive(
1212
state: State & { secret: string; eventHandler: EventHandler<unknown> },

test/fixtures/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { WebhookEventDefinition } from "../../src/types";
1+
import type { WebhookEventDefinition } from "../../src/types.ts";
22
import pushEvent from "./push-payload.json";
33
import installationCreatedEvent from "./installation-created-payload.json";
44
import installationDeletedEvent from "./installation-deleted-payload.json";

tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
}
88
},
99
"compilerOptions": {
10+
"allowImportingTsExtensions": true,
1011
"esModuleInterop": true,
1112
"declaration": true,
1213
"outDir": "pkg/dist-types",

0 commit comments

Comments
 (0)