-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: drop aws-sdk dep, accept it as a parameter when working with Sets
- Loading branch information
Showing
9 changed files
with
363 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { DocumentClient as DocClientV2 } from "aws-sdk/clients/dynamodb"; | ||
|
||
import { AwsSdkDocumentClient } from "./document-client"; | ||
|
||
describe("aws sdk document client", () => { | ||
afterEach(() => { | ||
AwsSdkDocumentClient.setDocumentClient(null); | ||
}); | ||
|
||
it("throws an error when there's no AWS SKD provided", () => { | ||
const docClient = new AwsSdkDocumentClient(); | ||
const fn = () => docClient.createSet([1, 2, 3]); | ||
|
||
const expected = | ||
"dynoexpr: When working with Sets, please provide the AWS DocumentClient (v2)."; | ||
expect(fn).toThrowError(expected); | ||
}); | ||
|
||
it("creates a AWS Set using AWS SDK DocumentClient v2", () => { | ||
AwsSdkDocumentClient.setDocumentClient(DocClientV2); | ||
const docClient = new AwsSdkDocumentClient(); | ||
const actual = docClient.createSet([1, 2, 3]); | ||
|
||
const awsDocClient = new DocClientV2(); | ||
const expected = awsDocClient.createSet([1, 2, 3]); | ||
expect(actual).toStrictEqual(expected); | ||
}); | ||
|
||
describe("creates sets", () => { | ||
const docClient = new AwsSdkDocumentClient(); | ||
|
||
beforeEach(() => { | ||
AwsSdkDocumentClient.setDocumentClient(DocClientV2); | ||
}); | ||
|
||
it("creates DynamoDBSet instances for strings", () => { | ||
const args = ["hello", "world"]; | ||
const actual = docClient.createSet(args); | ||
|
||
expect(actual.type).toBe("String"); | ||
expect(actual.values).toHaveLength(args.length); | ||
expect(actual.values).toContain("hello"); | ||
expect(actual.values).toContain("world"); | ||
}); | ||
|
||
it("creates DynamoDBSet instances for numbers", () => { | ||
const args = [42, 1, 2]; | ||
const actual = docClient.createSet(args); | ||
|
||
expect(actual.type).toBe("Number"); | ||
expect(actual.values).toHaveLength(args.length); | ||
expect(actual.values).toContain(42); | ||
expect(actual.values).toContain(1); | ||
expect(actual.values).toContain(2); | ||
}); | ||
|
||
it("creates DynamoDBSet instances for binary types", () => { | ||
const args = [ | ||
Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]), | ||
Buffer.from([0x61, 0x62, 0x63]), | ||
]; | ||
const actual = docClient.createSet(args); | ||
|
||
expect(actual.type).toBe("Binary"); | ||
expect(actual.values).toHaveLength(args.length); | ||
expect(actual.values).toContainEqual(args[0]); | ||
expect(actual.values).toContain(args[1]); | ||
}); | ||
|
||
it("does not throw an error with mixed set types if validation is not explicitly enabled", () => { | ||
const args = ["hello", 42]; | ||
const actual = docClient.createSet(args); | ||
|
||
expect(actual.type).toBe("String"); | ||
expect(actual.values).toHaveLength(args.length); | ||
expect(actual.values).toContain("hello"); | ||
expect(actual.values).toContain(42); | ||
}); | ||
|
||
it("throws an error with mixed set types if validation is enabled", () => { | ||
const params = ["hello", 42]; | ||
const expression = () => docClient.createSet(params, { validate: true }); | ||
|
||
const expected = "String Set contains Number value"; | ||
expect(expression).toThrow(expected); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* eslint-disable class-methods-use-this */ | ||
let AwsSdk: unknown = null; | ||
|
||
export class AwsSdkDocumentClient { | ||
static setDocumentClient(clientAwsSdk: unknown) { | ||
AwsSdk = clientAwsSdk; | ||
} | ||
|
||
createSet( | ||
list: unknown[] | Record<string, unknown>, | ||
options?: Record<string, unknown> | ||
) { | ||
if (!AwsSdk) { | ||
throw Error( | ||
"dynoexpr: When working with Sets, please provide the AWS DocumentClient (v2)." | ||
); | ||
} | ||
|
||
// @ts-expect-error Property 'prototype' does not exist on type '{}'. | ||
return AwsSdk.prototype.createSet(list, options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.