Completed
Pull Request — master (#4)
by Aaron
02:20
created

main/src/depromisifi.ts   A

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 35
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 29
mnd 2
bc 2
fnc 0
dl 0
loc 35
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import {
2
    GetPublicKeyOrSecret as GetPublicKeyOrSecretCb,
3
    JwtHeader,
4
    Secret,
5
    SigningKeyCallback
6
} from 'jsonwebtoken'
7
export {
8
    JwtHeader,
9
    Secret,
10
    SigningKeyCallback
11
} from 'jsonwebtoken'
12
13
export type GetPublicKeyOrSecretPromise =
14
    (header: JwtHeader) => Promise<Secret>
15
16
export type SecretOrPublicKeyCb = string | Buffer | GetPublicKeyOrSecretCb
17
export type SecretOrPublicKeyCbP = SecretOrPublicKeyCb | GetPublicKeyOrSecretPromise
18
19
const depromisify = (key: SecretOrPublicKeyCbP): SecretOrPublicKeyCb => {
20
  if (typeof key === 'string' || key instanceof Buffer) {
21
    return key
22
  }
23
  const getKey: GetPublicKeyOrSecretCb | GetPublicKeyOrSecretPromise = key
24
  return (header: JwtHeader, callback: SigningKeyCallback): void => {
25
    const promise: Promise<Secret> | void = getKey(header, callback)
26
    if (promise) {
27
      promise
28
        .then((secret: Secret) => callback(null, secret))
29
        .catch((err: any) => callback(err))
30
    }
31
  }
32
}
33
34
export default depromisify
35