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

refactor: use node: specifier imports and full relative path imports #476

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"lint:fix": "prettier --write '{src,test}/**/*' README.md package.json",
"pretest": "npm run -s lint",
"test": "jest --coverage",
"test:typescript": "npx tsc --noEmit --declaration --noUnusedLocals test/typescript-validate.ts"
"test:typescript": "npx tsc --noEmit --declaration --noUnusedLocals --allowImportingTsExtensions --moduleResolution node16 --module node16 test/typescript-validate.ts"
},
"repository": "github:octokit/oauth-app.js",
"keywords": [
Expand Down Expand Up @@ -60,6 +60,10 @@
"functions": 100,
"lines": 100
}
},
"moduleNameMapper": {
"ipaddr.js": "<rootDir>/node_modules/ipaddr.js/lib/ipaddr.js",
"^(.+)\\.jsx?$": "$1"
}
},
"release": {
Expand Down
2 changes: 1 addition & 1 deletion scripts/build.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-check
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";

/**
Expand Down
2 changes: 1 addition & 1 deletion src/add-event-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
State,
ClientType,
Options,
} from "./types";
} from "./types.js";

export function addEventHandler(
state: State,
Expand Down
7 changes: 6 additions & 1 deletion src/emit-event.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { State, EventHandlerContext, ClientType, Options } from "./types";
import type {
State,
EventHandlerContext,
ClientType,
Options,
} from "./types.js";

export async function emitEvent(
state: State,
Expand Down
34 changes: 17 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,39 @@ import { OAuthAppOctokit } from "./oauth-app-octokit";
import {
getUserOctokitWithState,
type GetUserOctokitWithStateInterface,
} from "./methods/get-user-octokit";
} from "./methods/get-user-octokit.js";
import {
type GetWebFlowAuthorizationUrlInterface,
getWebFlowAuthorizationUrlWithState,
} from "./methods/get-web-flow-authorization-url";
} from "./methods/get-web-flow-authorization-url.js";
import {
type CreateTokenInterface,
createTokenWithState,
} from "./methods/create-token";
} from "./methods/create-token.js";
import {
type CheckTokenInterface,
checkTokenWithState,
} from "./methods/check-token";
} from "./methods/check-token.js";
import {
type ResetTokenInterface,
resetTokenWithState,
} from "./methods/reset-token";
} from "./methods/reset-token.js";
import {
type RefreshTokenInterface,
refreshTokenWithState,
} from "./methods/refresh-token";
} from "./methods/refresh-token.js";
import {
type ScopeTokenInterface,
scopeTokenWithState,
} from "./methods/scope-token";
} from "./methods/scope-token.js";
import {
type DeleteTokenInterface,
deleteTokenWithState,
} from "./methods/delete-token";
} from "./methods/delete-token.js";
import {
type DeleteAuthorizationInterface,
deleteAuthorizationWithState,
} from "./methods/delete-authorization";
} from "./methods/delete-authorization.js";

import type {
AddEventHandler,
Expand All @@ -49,23 +49,23 @@ import type {
OctokitTypeFromOptions,
Options,
State,
} from "./types";
} from "./types.js";

// types required by external handlers (aws-lambda, etc)
export type {
HandlerOptions,
OctokitRequest,
OctokitResponse,
} from "./middleware/types";
} from "./middleware/types.js";

// generic handlers
export { handleRequest } from "./middleware/handle-request";
export { unknownRouteResponse } from "./middleware/unknown-route-response";
export { handleRequest } from "./middleware/handle-request.js";
export { unknownRouteResponse } from "./middleware/unknown-route-response.js";

export { createNodeMiddleware } from "./middleware/node/index";
export { sendResponse as sendNodeResponse } from "./middleware/node/send-response";
export { createWebWorkerHandler } from "./middleware/web-worker/index";
export { createAWSLambdaAPIGatewayV2Handler } from "./middleware/aws-lambda/api-gateway-v2";
export { createNodeMiddleware } from "./middleware/node/index.js";
export { sendResponse as sendNodeResponse } from "./middleware/node/send-response.js";
export { createWebWorkerHandler } from "./middleware/web-worker/index.js";
export { createAWSLambdaAPIGatewayV2Handler } from "./middleware/aws-lambda/api-gateway-v2.js";

type Constructor<T> = new (...args: any[]) => T;

Expand Down
2 changes: 1 addition & 1 deletion src/methods/check-token.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as OAuthMethods from "@octokit/oauth-methods";

import type { ClientType, State } from "../types";
import type { ClientType, State } from "../types.js";

export type CheckTokenOptions = {
token: string;
Expand Down
4 changes: 2 additions & 2 deletions src/methods/create-token.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as OAuthAppAuth from "@octokit/auth-oauth-app";

import type { ClientType, State } from "../types";
import { emitEvent } from "../emit-event";
import type { ClientType, State } from "../types.js";
import { emitEvent } from "../emit-event.js";

export type CreateTokenWebFlowOptions = Omit<
OAuthAppAuth.WebFlowAuthOptions,
Expand Down
4 changes: 2 additions & 2 deletions src/methods/delete-authorization.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as OAuthMethods from "@octokit/oauth-methods";
import { createUnauthenticatedAuth } from "@octokit/auth-unauthenticated";

import type { State } from "../types";
import { emitEvent } from "../emit-event";
import type { State } from "../types.js";
import { emitEvent } from "../emit-event.js";

export type DeleteAuthorizationOptions = {
token: string;
Expand Down
4 changes: 2 additions & 2 deletions src/methods/delete-token.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as OAuthMethods from "@octokit/oauth-methods";
import { createUnauthenticatedAuth } from "@octokit/auth-unauthenticated";

import type { State } from "../types";
import { emitEvent } from "../emit-event";
import type { State } from "../types.js";
import { emitEvent } from "../emit-event.js";

export type DeleteTokenOptions = {
token: string;
Expand Down
2 changes: 1 addition & 1 deletion src/methods/get-oauth-client-code.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export function getOAuthClientCode() {
return `import { Octokit: Core } from "https://cdn.pika.dev/@octokit/core";
return `import { Octokit: Core } from "https://esm.sh/@octokit/core";

export const Octokit = Core.defaults({
oauth: {}
Expand Down
4 changes: 2 additions & 2 deletions src/methods/get-user-octokit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import type {
OAuthAppAuthentication,
} from "@octokit/auth-oauth-user";

import type { State, OctokitInstance, ClientType } from "../types";
import { emitEvent } from "../emit-event";
import type { State, OctokitInstance, ClientType } from "../types.js";
import { emitEvent } from "../emit-event.js";

type StateOptions = "clientType" | "clientId" | "clientSecret" | "request";

Expand Down
2 changes: 1 addition & 1 deletion src/methods/get-web-flow-authorization-url.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as OAuthMethods from "@octokit/oauth-methods";

import type { ClientType, State } from "../types";
import type { ClientType, State } from "../types.js";

type StateOptions = "clientType" | "clientId" | "clientSecret" | "request";

Expand Down
4 changes: 2 additions & 2 deletions src/methods/refresh-token.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as OAuthMethods from "@octokit/oauth-methods";

import type { State } from "../types";
import { emitEvent } from "../emit-event";
import type { State } from "../types.js";
import { emitEvent } from "../emit-event.js";
import { createOAuthUserAuth } from "@octokit/auth-oauth-user";

export type RefreshTokenOptions = {
Expand Down
4 changes: 2 additions & 2 deletions src/methods/reset-token.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as OAuthMethods from "@octokit/oauth-methods";

import type { ClientType, State } from "../types";
import { emitEvent } from "../emit-event";
import type { ClientType, State } from "../types.js";
import { emitEvent } from "../emit-event.js";
import { createOAuthUserAuth } from "@octokit/auth-oauth-user";

export type ResetTokenOptions = {
Expand Down
4 changes: 2 additions & 2 deletions src/methods/scope-token.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as OAuthMethods from "@octokit/oauth-methods";
import { createOAuthUserAuth } from "@octokit/auth-oauth-user";

import type { State } from "../types";
import { emitEvent } from "../emit-event";
import type { State } from "../types.js";
import { emitEvent } from "../emit-event.js";

type StateOptions = "clientType" | "clientId" | "clientSecret" | "request";

Expand Down
2 changes: 1 addition & 1 deletion src/middleware/aws-lambda/api-gateway-v2-parse-request.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { OctokitRequest } from "../types";
import type { OctokitRequest } from "../types.js";
import type { APIGatewayProxyEventV2 } from "aws-lambda";

export function parseRequest(request: APIGatewayProxyEventV2): OctokitRequest {
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/aws-lambda/api-gateway-v2-send-response.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { OctokitResponse } from "../types";
import type { OctokitResponse } from "../types.js";
import type { APIGatewayProxyStructuredResultV2 } from "aws-lambda";

export function sendResponse(
Expand Down
10 changes: 5 additions & 5 deletions src/middleware/aws-lambda/api-gateway-v2.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { parseRequest } from "./api-gateway-v2-parse-request";
import { sendResponse } from "./api-gateway-v2-send-response";
import { handleRequest } from "../handle-request";
import type { HandlerOptions } from "../types";
import type { OAuthApp } from "../../index";
import type { ClientType, Options } from "../../types";
import { sendResponse } from "./api-gateway-v2-send-response.js";
import { handleRequest } from "../handle-request.js";
import type { HandlerOptions } from "../types.js";
import type { OAuthApp } from "../../index.js";
import type { ClientType, Options } from "../../types.js";
import type {
APIGatewayProxyEventV2,
APIGatewayProxyStructuredResultV2,
Expand Down
12 changes: 8 additions & 4 deletions src/middleware/handle-request.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { OAuthApp } from "../index";
import { unknownRouteResponse } from "./unknown-route-response";
import type { HandlerOptions, OctokitRequest, OctokitResponse } from "./types";
import type { ClientType, Options } from "../types";
import { OAuthApp } from "../index.js";
import { unknownRouteResponse } from "./unknown-route-response.js";
import type {
HandlerOptions,
OctokitRequest,
OctokitResponse,
} from "./types.js";
import type { ClientType, Options } from "../types.js";

export async function handleRequest(
app: OAuthApp<Options<ClientType>>,
Expand Down
14 changes: 7 additions & 7 deletions src/middleware/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// remove type imports from http for Deno compatibility
// see https://github.com/octokit/octokit.js/issues/2075#issuecomment-817361886
// import { IncomingMessage, ServerResponse } from "http";
// import { IncomingMessage, ServerResponse } from "node:http";
type IncomingMessage = any;
type ServerResponse = any;

import { parseRequest } from "./parse-request";
import { sendResponse } from "./send-response";
import { handleRequest } from "../handle-request";
import type { OAuthApp } from "../../index";
import type { HandlerOptions } from "../types";
import type { ClientType, Options } from "../../types";
import { parseRequest } from "./parse-request.js";
import { sendResponse } from "./send-response.js";
import { handleRequest } from "../handle-request.js";
import type { OAuthApp } from "../../index.js";
import type { HandlerOptions } from "../types.js";
import type { ClientType, Options } from "../../types.js";

export function createNodeMiddleware(
app: OAuthApp<Options<ClientType>>,
Expand Down
4 changes: 2 additions & 2 deletions src/middleware/node/parse-request.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// remove type imports from http for Deno compatibility
// see https://github.com/octokit/octokit.js/issues/2075#issuecomment-817361886
// import { IncomingMessage } from "http";
// import { IncomingMessage } from "node:http";
type IncomingMessage = any;

import type { OctokitRequest } from "../types";
import type { OctokitRequest } from "../types.js";

export function parseRequest(request: IncomingMessage): OctokitRequest {
const { method, url, headers } = request;
Expand Down
4 changes: 2 additions & 2 deletions src/middleware/node/send-response.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// remove type imports from http for Deno compatibility
// see https://github.com/octokit/octokit.js/issues/2075#issuecomment-817361886
// import { IncomingMessage, ServerResponse } from "http";
// import { IncomingMessage, ServerResponse } from "node:http";
type ServerResponse = any;
import type { OctokitResponse } from "../types";
import type { OctokitResponse } from "../types.js";

export function sendResponse(
octokitResponse: OctokitResponse,
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/unknown-route-response.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { OctokitRequest } from "./types";
import type { OctokitRequest } from "./types.js";

export function unknownRouteResponse(request: OctokitRequest) {
return {
Expand Down
12 changes: 6 additions & 6 deletions src/middleware/web-worker/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { parseRequest } from "./parse-request";
import { sendResponse } from "./send-response";
import { handleRequest } from "../handle-request";
import type { OAuthApp } from "../../index";
import type { HandlerOptions } from "../types";
import type { ClientType, Options } from "../../types";
import { parseRequest } from "./parse-request.js";
import { sendResponse } from "./send-response.js";
import { handleRequest } from "../handle-request.js";
import type { OAuthApp } from "../../index.js";
import type { HandlerOptions } from "../types.js";
import type { ClientType, Options } from "../../types.js";

export function createWebWorkerHandler<T extends Options<ClientType>>(
app: OAuthApp<T>,
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/web-worker/parse-request.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { OctokitRequest } from "../types";
import type { OctokitRequest } from "../types.js";

export function parseRequest(request: Request): OctokitRequest {
// @ts-ignore Worker environment supports fromEntries/entries.
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/web-worker/send-response.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { OctokitResponse } from "../types";
import type { OctokitResponse } from "../types.js";

export function sendResponse(octokitResponse: OctokitResponse): Response {
return new Response(octokitResponse.text, {
Expand Down
2 changes: 1 addition & 1 deletion src/oauth-app-octokit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Octokit } from "@octokit/core";
import { getUserAgent } from "universal-user-agent";
import { VERSION } from "./version";
import { VERSION } from "./version.js";

export const OAuthAppOctokit = Octokit.defaults({
userAgent: `octokit-oauth-app.js/${VERSION} ${getUserAgent()}`,
Expand Down
4 changes: 2 additions & 2 deletions test/app.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fetchMock from "fetch-mock";
import { Octokit } from "@octokit/core";

import { OAuthApp } from "../src";
import { OAuthAppOctokit } from "../src/oauth-app-octokit";
import { OAuthApp } from "../src/index.ts";
import { OAuthAppOctokit } from "../src/oauth-app-octokit.ts";

describe("OAuthApp.defaults", () => {
test("sets default options", () => {
Expand Down
4 changes: 2 additions & 2 deletions test/aws-lambda-api-gateway-v2.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createAWSLambdaAPIGatewayV2Handler, OAuthApp } from "../src/";
import { URL } from "url";
import { createAWSLambdaAPIGatewayV2Handler, OAuthApp } from "../src/index.ts";
import { URL } from "node:url";
import { APIGatewayProxyEventV2 } from "aws-lambda";

describe("createAWSLambdaAPIGatewayV2Handler(app)", () => {
Expand Down
6 changes: 3 additions & 3 deletions test/node-middleware.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createServer, IncomingMessage } from "http";
import { URL } from "url";
import { createServer, IncomingMessage } from "node:http";
import { URL } from "node:url";

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

// import without types
const express = require("express");
Expand Down
2 changes: 1 addition & 1 deletion test/smoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
OAuthApp,
sendNodeResponse,
unknownRouteResponse,
} from "../src";
} from "../src/index.ts";

describe("Smoke test", () => {
it("OAuthApp is a function", () => {
Expand Down
Loading