From 08bf89e22013ece89ceb0547a095ae9e195eea32 Mon Sep 17 00:00:00 2001 From: andyjy Date: Wed, 6 Nov 2024 10:17:36 +0000 Subject: [PATCH] fix types to use module augmentation instead of @ts-ignore --- export/httpQuery.ts | 3 +-- export/index.ts | 27 +++++++++++---------------- export/pg.internals.types.ts | 29 +++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 18 deletions(-) create mode 100644 export/pg.internals.types.ts diff --git a/export/httpQuery.ts b/export/httpQuery.ts index 70cb814..bea153b 100644 --- a/export/httpQuery.ts +++ b/export/httpQuery.ts @@ -2,9 +2,8 @@ import { types as defaultTypes } from '.'; import { Socket } from '../shims/net'; import { parse } from '../shims/url'; -// @ts-ignore -- this isn't officially exported by pg +// not officially exported by pg - types declared in pg.internals.types.ts import { prepareValue } from 'pg/lib/utils'; -// @ts-ignore -- this isn't officially exported by pg import TypeOverrides from 'pg/lib/type-overrides'; export class NeonDbError extends Error { diff --git a/export/index.ts b/export/index.ts index 6cec68a..0c731dc 100644 --- a/export/index.ts +++ b/export/index.ts @@ -3,15 +3,7 @@ import { Socket } from '../shims/net'; import { neon, NeonDbError } from './httpQuery'; import type { NeonConfigGlobalAndClient } from './neonConfig'; -// @ts-ignore -- this isn't officially exported by pg -import ConnectionParameters from '../node_modules/pg/lib/connection-parameters'; - -interface ConnectionParameters { - user: string; - password: string; - host: string; - database: string; -} +import ConnectionParameters from 'pg/lib/connection-parameters'; /** * We export the pg library mostly unchanged, but we do make a few tweaks. @@ -290,6 +282,15 @@ function promisify(Promise: any, callback: any) { return { callback: cb, result: result }; } +// Type augmentation for pg Pool internals that don't have publicly exported types +// - https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation +declare module 'pg' { + interface Pool { + options: ConstructorParameters[0]; + Promise: PromiseConstructorLike; + } +} + class NeonPool extends Pool { Client = NeonClient; hasFetchUnsupportedListeners = false; @@ -302,7 +303,6 @@ class NeonPool extends Pool { return super.on(event as any, listener); } - // @ts-ignore -- is it even possible to make TS happy with these overloaded function types? query(config?: any, values?: any, cb?: any) { if ( !Socket.poolQueryViaFetch || @@ -319,15 +319,11 @@ class NeonPool extends Pool { } // create a synthetic callback that resolves the returned Promise - // @ts-ignore -- TS doesn't know about this.Promise const response = promisify(this.Promise, cb); cb = response.callback; try { - const cp = new ConnectionParameters( - // @ts-expect-error -- TS doesn't know about this.options - this.options, - ) as ConnectionParameters; + const cp = new ConnectionParameters(this.options); const euc = encodeURIComponent, eu = encodeURI; const connectionString = `postgresql://${euc(cp.user)}:${euc(cp.password)}@${euc(cp.host)}/${eu(cp.database)}`; @@ -341,7 +337,6 @@ class NeonPool extends Pool { }); sql(queryText, queryValues, { - // @ts-expect-error -- TS doesn't know about this.options types: config.types ?? this.options?.types, }) .then((result) => cb(undefined, result)) diff --git a/export/pg.internals.types.ts b/export/pg.internals.types.ts new file mode 100644 index 0000000..6bb6b6a --- /dev/null +++ b/export/pg.internals.types.ts @@ -0,0 +1,29 @@ +// Typescript module augmentation declarations for pg internals that don't have publicly exported types +// - https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation + +declare module 'pg/lib/connection-parameters' { + export default class ConnectionParameters { + constructor(config: any); + + user: string; + password: string; + host: string; + database: string; + } +} + +declare module 'pg/lib/type-overrides' { + export default class TypeOverrides { + constructor(userTypes: any); + + getOverrides(format: string): Record; + + setTypeParser(oid: any, format: string, parseFn: any): void; + + getTypeParser(oid: any, format?: string): any; + } +} + +declare module 'pg/lib/utils' { + export function prepareValue(value: any): any; +}