From 97048658adc07d856d49310c3d35444d9b1b980d Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 4 Oct 2023 21:22:33 +0200 Subject: [PATCH] refactor: apply strict typescript checks + fixes --- src/_utils.ts | 29 ++++++++++++++++------------- src/giget.ts | 10 +++++++--- src/registry.ts | 3 +-- src/types.ts | 2 +- tsconfig.json | 3 ++- 5 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/_utils.ts b/src/_utils.ts index 997aca8..7966433 100644 --- a/src/_utils.ts +++ b/src/_utils.ts @@ -13,7 +13,7 @@ import type { GitInfo } from "./types"; export async function download( url: string, filePath: string, - options: { headers?: Record } = {}, + options: { headers?: Record } = {}, ) { const infoPath = filePath + ".json"; const info: { etag?: string } = JSON.parse( @@ -29,7 +29,9 @@ export async function download( // Already downloaded return; } - info.etag = etag; + if (typeof etag === "string") { + info.etag = etag; + } const response = await sendFetch(url, { headers: options.headers }); if (response.status >= 400) { @@ -48,7 +50,7 @@ const inputRegex = /^(?[\w.-]+\/[\w.-]+)(?[^#]+)?(?#[\w.-]+)?/; export function parseGitURI(input: string): GitInfo { - const m = input.match(inputRegex)?.groups; + const m = input.match(inputRegex)?.groups || {}; return { repo: m.repo, subdir: m.subdir || "/", @@ -56,15 +58,15 @@ export function parseGitURI(input: string): GitInfo { }; } -export function debug(...arguments_) { +export function debug(...args: unknown[]) { if (process.env.DEBUG) { - console.debug("[giget]", ...arguments_); + console.debug("[giget]", ...args); } } // eslint-disable-next-line no-undef -interface InternalFetchOptions extends Exclude { - headers?: Record; +interface InternalFetchOptions extends Omit { + headers?: Record; agent?: Agent; } @@ -83,11 +85,10 @@ export async function sendFetch( } } - if (options?.headers) { - options.headers = normalizeHeaders(options.headers); - } - - return await fetch(url, options); + return await fetch(url, { + ...options, + headers: normalizeHeaders(options.headers), + }); } export function cacheDirectory() { @@ -96,7 +97,9 @@ export function cacheDirectory() { : resolve(homedir(), ".cache/giget"); } -export function normalizeHeaders(headers: Record = {}) { +export function normalizeHeaders( + headers: Record = {}, +) { const normalized: Record = {}; for (const [key, value] of Object.entries(headers)) { if (!value) { diff --git a/src/giget.ts b/src/giget.ts index 192e395..3a6cdec 100644 --- a/src/giget.ts +++ b/src/giget.ts @@ -45,7 +45,7 @@ export async function downloadTemplate( ? undefined : registryProvider(options.registry, { auth: options.auth }); let providerName: string = - options.provider || (registryProvider ? "registry" : "github"); + options.provider || (registry ? "registry" : "github"); let source: string = input; const sourceProvierMatch = input.match(sourceProtoRe); if (sourceProvierMatch) { @@ -66,6 +66,10 @@ export async function downloadTemplate( ); }); + if (!template) { + throw new Error(`Failed to resolve template from ${providerName}`); + } + // Sanitize name and defaultDir template.name = (template.name || "template").replace(/[^\da-z-]/gi, "-"); template.defaultDir = (template.defaultDir || template.name).replace( @@ -89,7 +93,7 @@ export async function downloadTemplate( const temporaryDirectory = resolve( cacheDirectory(), - options.provider, + providerName, template.name, ); const tarPath = resolve( @@ -105,7 +109,7 @@ export async function downloadTemplate( const s = Date.now(); await download(template.tar, tarPath, { headers: { - authorization: options.auth ? `Bearer ${options.auth}` : undefined, + Authorization: options.auth ? `Bearer ${options.auth}` : undefined, ...normalizeHeaders(template.headers), }, }).catch((error) => { diff --git a/src/registry.ts b/src/registry.ts index 318de78..c552613 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -7,9 +7,8 @@ const DEFAULT_REGISTRY = export const registryProvider = ( registryEndpoint: string = DEFAULT_REGISTRY, - options?: { auth?: string }, + options: { auth?: string } = {}, ) => { - options = options || {}; return (async (input) => { const start = Date.now(); const registryURL = `${registryEndpoint}/${input}.json`; diff --git a/src/types.ts b/src/types.ts index 8ddf252..b9c3ac4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -12,7 +12,7 @@ export interface TemplateInfo { subdir?: string; url?: string; defaultDir?: string; - headers?: Record; + headers?: Record; // Added by giget source?: never; diff --git a/tsconfig.json b/tsconfig.json index 6d02740..e9588d9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,7 +3,8 @@ "target": "ESNext", "module": "ESNext", "moduleResolution": "Node", - "esModuleInterop": true + "esModuleInterop": true, + "strict": true }, "include": ["src"] }