From a4b52b127571bf5f5adebff9f0e50bf9dbbdc2e5 Mon Sep 17 00:00:00 2001 From: Rui Costa Date: Mon, 11 Dec 2023 13:48:19 +0000 Subject: [PATCH] refactor: use types instead of interfaces --- sh/build.ts | 2 +- src/github.test.ts | 1 - src/github.ts | 17 ++++++++++------- src/index.test.ts | 2 -- src/index.ts | 12 ++++++------ tsconfig.build.json | 3 ++- vitest.config.ts => vitest.config.mts | 3 ++- 7 files changed, 21 insertions(+), 19 deletions(-) rename vitest.config.ts => vitest.config.mts (99%) diff --git a/sh/build.ts b/sh/build.ts index 7d22e6e..e3dc4a4 100644 --- a/sh/build.ts +++ b/sh/build.ts @@ -25,7 +25,7 @@ async function main() { await $`esbuild src/cjs/index.cjs --outfile=dist/index.cjs ${flags}`; await $`esbuild src/index.ts --format=esm --outfile=dist/index.mjs ${flags}`; - await $`rm dist/github.js dist/index.js`; + // await $`rm dist/github.js dist/index.js`; await $`rm -rf dist/helpers`; } diff --git a/src/github.test.ts b/src/github.test.ts index 36569b8..739186e 100644 --- a/src/github.test.ts +++ b/src/github.test.ts @@ -1,5 +1,4 @@ import { type FetchOptions } from "@tuplo/fetch"; -import { vi } from "vitest"; import githubIssuesList from "./__data__/github-list-issues.json"; import { buildUrl, createIssue, findIssue, listIssues } from "./github"; diff --git a/src/github.ts b/src/github.ts index d9cdfc1..3f184df 100644 --- a/src/github.ts +++ b/src/github.ts @@ -3,25 +3,28 @@ import { Agent } from "node:https"; import fetch, { type FetchOptions } from "@tuplo/fetch"; -export interface IGithubIssue { +export type IGithubIssue = { id?: number; title: string; labels: string[]; body: string; -} +}; -export interface IGitHubOptions { +export type IGitHubOptions = { user: string; repo: string; token: string; labels?: string[]; -} +}; export function buildUrl(template: string, githubOptions: IGitHubOptions) { const { user, repo: repoName } = githubOptions; const repo = !/\//.test(repoName) ? `${user}/${repoName}` : repoName; - return ["https://api.github.com", template.replace(/:repo/, repo)].join(""); + const uri = new URL("https://api.github.com"); + uri.pathname = template.replace(/:repo/, repo); + + return uri.href; } async function client( @@ -72,9 +75,9 @@ export async function listIssues(githubOptions: IGitHubOptions) { ); } -interface IFinderFn { +type IFinderFn = { (issue: IGithubIssue): boolean; -} +}; export async function findIssue( finder: IFinderFn, diff --git a/src/index.test.ts b/src/index.test.ts index 2c081f4..3481324 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -1,5 +1,3 @@ -import { vi } from "vitest"; - import { type IGithubIssue, type IGitHubOptions } from "./github"; import { submitError, uncaughtHandlerFn } from "./index"; diff --git a/src/index.ts b/src/index.ts index 8b3ae34..55d9001 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,21 +4,21 @@ import * as github from "./github"; import { type IGitHubOptions } from "./github"; import { stringify } from "./helpers/stringify"; -export interface IUnhandlerError extends Error { +export type IUnhandlerError = Error & { body?: unknown; -} +}; -interface IProviders { +type IProviders = { github?: IGitHubOptions; -} +}; -export interface IUnhandlerOptions { +export type IUnhandlerOptions = { appName?: string; onAfterSubmitError?: (error: Error) => void | Promise; onBeforeSubmitError?: (error: Error) => void | Promise; providers: IProviders; shouldSubmitError?: boolean; -} +}; export async function submitError( error: IUnhandlerError, diff --git a/tsconfig.build.json b/tsconfig.build.json index 9d9c0fe..6e789d1 100644 --- a/tsconfig.build.json +++ b/tsconfig.build.json @@ -1,7 +1,8 @@ { "extends": "./tsconfig.json", "compilerOptions": { - "outDir": "./dist" + "outDir": "./dist", + "types": ["./node_modules/@types/node"] }, "exclude": ["node_modules/**/*", "**/*.test.ts", "sh/**/*"] } diff --git a/vitest.config.ts b/vitest.config.mts similarity index 99% rename from vitest.config.ts rename to vitest.config.mts index 463f1f6..5d02dd4 100644 --- a/vitest.config.ts +++ b/vitest.config.mts @@ -1,6 +1,7 @@ -import { defineConfig } from "vitest/config"; import path from "node:path"; +import { defineConfig } from "vitest/config"; + export default defineConfig({ test: { globals: true,