Skip to content

Commit

Permalink
fix types to use module augmentation instead of @ts-ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
andyjy committed Nov 6, 2024
1 parent 5a2a6aa commit 08bf89e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
3 changes: 1 addition & 2 deletions export/httpQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 11 additions & 16 deletions export/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<typeof Pool>[0];
Promise: PromiseConstructorLike;
}
}

class NeonPool extends Pool {
Client = NeonClient;
hasFetchUnsupportedListeners = false;
Expand All @@ -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 ||
Expand All @@ -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)}`;
Expand All @@ -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))
Expand Down
29 changes: 29 additions & 0 deletions export/pg.internals.types.ts
Original file line number Diff line number Diff line change
@@ -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<any, any>;

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;
}

0 comments on commit 08bf89e

Please sign in to comment.