Skip to content
This repository has been archived by the owner on Mar 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #171 from adieuadieu/fix/typedefs
Browse files Browse the repository at this point in the history
fix: en/decrypt function type definition issue
  • Loading branch information
adieuadieu committed Jul 20, 2018
2 parents 9c33ce8 + 1ec8f0d commit 7d91d9d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
# This job builds the base project directory (e.g. ~/package.json)
build:
docker:
- image: circleci/node:6@sha256:b8ea1a58a96d8f625212b649183f09996afe482dd6b27dbbcfc4c0d11cbde27e
- image: circleci/node:8
steps:
- checkout
- restore_cache:
Expand All @@ -23,7 +23,7 @@ jobs:
# This job runs the lint tool on the whole repository
lint:
docker:
- image: circleci/node:6@sha256:b8ea1a58a96d8f625212b649183f09996afe482dd6b27dbbcfc4c0d11cbde27e
- image: circleci/node:8
steps:
- checkout
- restore_cache:
Expand All @@ -38,7 +38,7 @@ jobs:
# runs the unit tests
unit-test:
docker:
- image: circleci/node:6@sha256:b8ea1a58a96d8f625212b649183f09996afe482dd6b27dbbcfc4c0d11cbde27e
- image: circleci/node:8
steps:
- checkout
- restore_cache:
Expand Down
17 changes: 16 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ describe('lib', () => {
)

expect(result).toEqual(mockArrayOfEncryptedValues)

// Tests the ReadonlyArray<string> overload
const [zero, one, two, three] = result

expect(zero).toEqual(mockArrayOfEncryptedValues[0])
expect(one).toEqual(mockArrayOfEncryptedValues[1])
expect(two).toEqual(mockArrayOfEncryptedValues[2])
expect(three).toEqual(mockArrayOfEncryptedValues[3])
})

it('should decrypt an encrypted string', async () => {
Expand All @@ -49,10 +57,17 @@ describe('lib', () => {
const result = await decrypt(mockArrayOfEncryptedValues)

expect(result).toEqual(mockArrayOfDecryptedValues)

// Tests the ReadonlyArray<string> overload
const [zero, one, two, three] = result

expect(zero).toEqual(mockArrayOfDecryptedValues[0])
expect(one).toEqual(mockArrayOfDecryptedValues[1])
expect(two).toEqual(mockArrayOfDecryptedValues[2])
expect(three).toEqual(mockArrayOfDecryptedValues[3])
})

it('should be able to handle empty or undefined parameters', async () => {
// @ts-ignore
expect(await decrypt(undefined)).toBe(undefined)
expect(await decrypt('')).toBe('')
})
Expand Down
27 changes: 17 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import kmsDecrypt from './decrypt'
import kmsEncrypt, { InterfaceEncryptParameters } from './encrypt'

export const encrypt = async (
parameters:
| InterfaceEncryptParameters
| ReadonlyArray<InterfaceEncryptParameters>,
): Promise<string | ReadonlyArray<string>> =>
'plaintext' in parameters
export async function encrypt(
parameters: InterfaceEncryptParameters,
): Promise<string>
export async function encrypt(
parameters: ReadonlyArray<InterfaceEncryptParameters>,
): Promise<ReadonlyArray<string>>
export async function encrypt(parameters: any): Promise<any> {
return 'plaintext' in parameters
? kmsEncrypt(parameters)
: Promise.all(parameters.map(kmsEncrypt))
}

export const decrypt = async (
ciphertext: undefined | string | ReadonlyArray<string>,
): Promise<undefined | string | ReadonlyArray<string>> =>
typeof ciphertext === 'undefined'
export async function decrypt(ciphertext: undefined): Promise<undefined>
export async function decrypt(ciphertext: string): Promise<string>
export async function decrypt(
ciphertext: ReadonlyArray<string>,
): Promise<ReadonlyArray<string>>
export async function decrypt(ciphertext: any): Promise<any> {
return typeof ciphertext === 'undefined'
? undefined // useful in development when process.env.SECRET may be unset
: typeof ciphertext === 'string'
? kmsDecrypt(ciphertext)
: Promise.all(ciphertext.map(kmsDecrypt))
}
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"declaration": true,
"esModuleInterop": false,
"forceConsistentCasingInFileNames": true,
"lib": ["es2015", "esnext"],
"lib": ["es2018", "esnext"],
"module": "es2015",
"moduleResolution": "node",
"noImplicitAny": true,
Expand All @@ -20,7 +20,7 @@
"skipLibCheck": false,
"sourceMap": false,
"strictNullChecks": true,
"target": "es2015"
"target": "es2017"
},
"exclude": ["node_modules", "dist", "test"]
}

0 comments on commit 7d91d9d

Please sign in to comment.