Skip to content

Commit

Permalink
fix: not requiring @aws-sdk/client-s3 installed if not using lib-stor…
Browse files Browse the repository at this point in the history
…age mock (#51)

* fix: not requiring @aws-sdk/client-s3 installed if not using lib-storage mock

* chore: add verdaccio config and local publish script
  • Loading branch information
m-radzikowski authored Sep 19, 2021
1 parent 1afdce7 commit 67d55d2
Show file tree
Hide file tree
Showing 6 changed files with 1,476 additions and 62 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules/
dist/
coverage/
compatibility/
verdaccio-storage/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ await s3Upload.done();

You can call `mockLibStorageUpload()` without providing an S3Client mock.
In that case, the client mock will be created and returned from the function.
However, you still need to have `@aws-sdk/client-s3` installed as a dependency.

#### Paginated operations

Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
"size": "size-limit",
"size:analyze": "size-limit --why",
"release": "standard-version",
"generate-compatibility": "ts-node ./misc/generate-compatibility.ts"
"generate-compatibility": "ts-node ./misc/generate-compatibility.ts",
"verdaccio": "rimraf verdaccio-storage && verdaccio -c verdaccio.yml",
"local-publish": "npm publish --registry http://localhost:4873/"
},
"main": "dist/cjs/index.js",
"types": "dist/types/index.d.ts",
Expand Down Expand Up @@ -77,7 +79,8 @@
"ts-node": "10.2.1",
"tsd": "0.17.0",
"typedoc": "0.22.3",
"typescript": "4.2.4"
"typescript": "4.2.4",
"verdaccio": "5.1.5"
},
"jest": {
"preset": "ts-jest",
Expand Down
17 changes: 15 additions & 2 deletions src/libStorage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {AwsClientStub} from './awsClientStub';
import {CreateMultipartUploadCommand, S3Client, UploadPartCommand} from '@aws-sdk/client-s3';
import type {S3Client as S3ClientType} from '@aws-sdk/client-s3';
import {mockClient} from './mockClient';

/**
Expand All @@ -9,7 +9,20 @@ import {mockClient} from './mockClient';
* If S3Client mocks is not provided, a new one is created.
* @param s3Mock S3Client mock created with {@link mockClient} function
*/
export const mockLibStorageUpload = (s3Mock?: AwsClientStub<S3Client>): AwsClientStub<S3Client> => {
export const mockLibStorageUpload = (s3Mock?: AwsClientStub<S3ClientType>): AwsClientStub<S3ClientType> => {
/*
* Not all library consumers also use @aws-sdk/client-s3. Importing it in a standard TS way
* would cause errors for them, as the module would be tried to found and import.
* Instead, we require classes from it dynamically in the scope of the function.
*
* Another solution would be to not export this function in the index.ts
* and instead have consumers to import it from 'aws-sdk-client-mock/libStorage'.
* This however turned out to be complicated to achieve in terms of exposing modules properly
* for CommonJS and ES modules.
*/
// eslint-disable-next-line @typescript-eslint/no-var-requires,@typescript-eslint/no-unsafe-assignment
const {CreateMultipartUploadCommand, S3Client, UploadPartCommand} = require('@aws-sdk/client-s3');

if (!s3Mock) {
s3Mock = mockClient(S3Client);
}
Expand Down
6 changes: 6 additions & 0 deletions verdaccio.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
storage: verdaccio-storage

packages:
"**":
access: $all
publish: $all
Loading

0 comments on commit 67d55d2

Please sign in to comment.