Skip to content

Commit e2254dc

Browse files
committed
fix: import optional deps gracefully (#554)
1 parent d5e583f commit e2254dc

File tree

5 files changed

+8660
-7
lines changed

5 files changed

+8660
-7
lines changed

src/adapter/http.adapter.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1+
import type { ExtendOptions, Got, RequestError } from 'got'
12
import { ImageAdapter } from '../interfaces'
2-
import got, { Got, ExtendOptions, RequestError } from 'got'
33
import { getLogger } from '../logger'
4+
import { optionalRequire } from '../optional-require'
45

56
export class HttpAdapter implements ImageAdapter {
67
private client: Got
78
private log = getLogger('adapter:http')
89

910
constructor(gotOptions: ExtendOptions) {
11+
const got = optionalRequire<{ default: Got }>('got').default
12+
1013
this.client = got.extend({
1114
...gotOptions,
1215
})
16+
1317
this.log(`Using prefixUrl: ${this.getPrefixUrl()}`)
1418
}
1519

src/adapter/s3.adapter.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1-
import { S3 } from 'aws-sdk'
2-
import { ImageAdapter } from '../interfaces'
1+
import type { S3 as S3Type } from 'aws-sdk'
2+
import { ImageAdapter, Type } from '../interfaces'
33
import { getLogger } from '../logger'
4+
import { optionalRequire } from '../optional-require'
45

56
export class S3Adapter implements ImageAdapter {
67
private log = getLogger('adapter:s3')
78

89
constructor(
910
public readonly bucketName: string,
10-
private readonly s3client = new S3(),
11+
private readonly s3client?: S3Type,
1112
) {
13+
const { S3 } = optionalRequire<{ S3: Type<S3Type> }>('aws-sdk')
14+
this.s3client ??= new S3()
15+
1216
this.log(`Using bucket name: ${bucketName}`)
1317
}
1418

1519
async fetch(id: string): Promise<Buffer | undefined> {
1620
this.log(`Fetching image "${id}" from bucket "${this.bucketName}"`)
1721
try {
18-
const object = await this.s3client
19-
.getObject({ Bucket: this.bucketName, Key: id })
20-
.promise()
22+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
23+
const object = await this.s3client!.getObject({
24+
Bucket: this.bucketName,
25+
Key: id,
26+
}).promise()
2127

2228
if (!Buffer.isBuffer(object.Body)) {
2329
return undefined

src/interfaces.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@ export interface Signer {
3535
sign(string: string | URL): string
3636
verify(string: string): boolean
3737
}
38+
39+
export interface Type<T = unknown> extends Function {
40+
new (...args: unknown[]): T
41+
}

src/optional-require.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export function optionalRequire<T>(
2+
packageName: string,
3+
// eslint-disable-next-line @typescript-eslint/ban-types
4+
): T {
5+
try {
6+
// eslint-disable-next-line security/detect-non-literal-require, @typescript-eslint/no-var-requires
7+
return require(packageName) as unknown as T
8+
} catch {
9+
return {} as unknown as T
10+
}
11+
}

0 commit comments

Comments
 (0)