Skip to content

Commit 5563b34

Browse files
authored
fix: Update how types are exported (#236)
* Update how types are exported * update README to reflect export changes
1 parent 6aa96cc commit 5563b34

File tree

2 files changed

+59
-27
lines changed

2 files changed

+59
-27
lines changed

README.md

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,35 +80,67 @@ testHelpers.expectOutput(output, 'expected output');
8080

8181
### Types
8282

83+
#### Error Classes
84+
8385
```js
84-
import { types } from '@heroku/heroku-cli-util';
86+
import { utils } from '@heroku/heroku-cli-util';
8587

8688
// Error types
8789
try {
88-
throw new types.errors.AmbiguousError([{ name: 'foo' }, { name: 'bar' }], 'addon');
90+
throw new utils.errors.AmbiguousError([{ name: 'foo' }, { name: 'bar' }], 'addon');
8991
} catch (err) {
90-
if (err instanceof types.errors.AmbiguousError) {
92+
if (err instanceof utils.errors.AmbiguousError) {
9193
console.error('Ambiguous:', err.message);
9294
}
9395
}
9496

9597
try {
96-
throw new types.errors.NotFound();
98+
throw new utils.errors.NotFound();
9799
} catch (err) {
98-
if (err instanceof types.errors.NotFound) {
100+
if (err instanceof utils.errors.NotFound) {
99101
console.error('Not found:', err.message);
100102
}
101103
}
104+
```
105+
106+
#### PostgreSQL Types (TypeScript)
107+
108+
Import PG types using the `pg` namespace:
109+
110+
```typescript
111+
import type { pg } from '@heroku/heroku-cli-util';
112+
113+
// Use the types
114+
const connection: pg.ConnectionDetails = {
115+
database: 'mydb',
116+
host: 'localhost',
117+
password: 'pass',
118+
pathname: '/mydb',
119+
port: '5432',
120+
url: 'postgres://...',
121+
user: 'admin'
122+
};
123+
124+
function processDatabase(details: pg.ConnectionDetailsWithAttachment) {
125+
// ...
126+
}
127+
128+
const addon: pg.AddOnWithRelatedData = { /* ... */ };
129+
const link: pg.Link = { /* ... */ };
130+
const tunnel: pg.TunnelConfig = { /* ... */ };
131+
```
102132

103-
// PG types (for TypeScript)
104-
/**
105-
* types.pg.ExtendedAddonAttachment
106-
* types.pg.AddOnWithRelatedData
107-
* types.pg.ConnectionDetails
108-
* types.pg.ConnectionDetailsWithAttachment
109-
* types.pg.Link
110-
* types.pg.TunnelConfig
111-
*/
133+
Alternatively, you can import types directly:
134+
135+
```typescript
136+
import type {
137+
ConnectionDetails,
138+
ConnectionDetailsWithAttachment,
139+
AddOnWithRelatedData,
140+
ExtendedAddonAttachment,
141+
Link,
142+
TunnelConfig
143+
} from '@heroku/heroku-cli-util';
112144
```
113145

114146
### Database and Utility Helpers

src/index.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import {APIClient} from '@heroku-cli/command'
22

3+
import type * as DataApiTypes from './types/pg/data-api.js'
4+
import type * as TunnelTypes from './types/pg/tunnel.js'
5+
36
import {AmbiguousError} from './errors/ambiguous.js'
47
import {NotFound} from './errors/not-found.js'
5-
import {AddOnWithRelatedData, ExtendedAddonAttachment, Link} from './types/pg/data-api.js'
6-
import {ConnectionDetails, ConnectionDetailsWithAttachment, TunnelConfig} from './types/pg/tunnel.js'
78
import {getPsqlConfigs, sshTunnel} from './utils/pg/bastion.js'
89
import {getConfigVarNameFromAttachment} from './utils/pg/config-vars.js'
910
import DatabaseResolver from './utils/pg/databases.js'
@@ -17,15 +18,14 @@ import {styledObject} from './ux/styled-object.js'
1718
import {table} from './ux/table.js'
1819
import {wait} from './ux/wait.js'
1920

20-
export const types = {
21-
pg: {
22-
AddOnWithRelatedData: {} as AddOnWithRelatedData,
23-
ConnectionDetails: {} as ConnectionDetails,
24-
ConnectionDetailsWithAttachment: {} as ConnectionDetailsWithAttachment,
25-
ExtendedAddonAttachment: {} as ExtendedAddonAttachment,
26-
Link: {} as Link,
27-
TunnelConfig: {} as TunnelConfig,
28-
},
21+
// eslint-disable-next-line @typescript-eslint/no-namespace
22+
export namespace pg {
23+
export type AddOnWithRelatedData = DataApiTypes.AddOnWithRelatedData
24+
export type ExtendedAddonAttachment = DataApiTypes.ExtendedAddonAttachment
25+
export type Link = DataApiTypes.Link
26+
export type ConnectionDetails = TunnelTypes.ConnectionDetails
27+
export type ConnectionDetailsWithAttachment = TunnelTypes.ConnectionDetailsWithAttachment
28+
export type TunnelConfig = TunnelTypes.TunnelConfig
2929
}
3030

3131
export const utils = {
@@ -42,15 +42,15 @@ export const utils = {
4242
appId: string,
4343
attachmentId?: string,
4444
namespace?: string,
45-
): Promise<ConnectionDetailsWithAttachment> {
45+
): Promise<TunnelTypes.ConnectionDetailsWithAttachment> {
4646
const databaseResolver = new DatabaseResolver(heroku)
4747
return databaseResolver.getDatabase(appId, attachmentId, namespace)
4848
},
4949
},
5050
host: getHost,
5151
psql: {
5252
exec(
53-
connectionDetails: ConnectionDetailsWithAttachment,
53+
connectionDetails: TunnelTypes.ConnectionDetailsWithAttachment,
5454
query: string,
5555
psqlCmdArgs: string[] = [],
5656
): Promise<string> {

0 commit comments

Comments
 (0)