Skip to content

Commit

Permalink
refactor: updates types to be more accurate
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Sep 6, 2019
1 parent eeb4f7a commit b988fc8
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 19 deletions.
14 changes: 10 additions & 4 deletions adonis-typings/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,22 @@ declare module '@ioc:Adonis/Core/Hash' {
list: { [P in keyof HashList]: HashList[P]['config'] },
}

export type DriverMethod<T, K extends keyof HashDriverContract> = T extends HashDriverContract
? HashDriverContract[K]
: never

/**
* Hash mananger interface
*/
export interface HashContract extends ManagerContract<
export interface HashContract<
DefaultDriver = HashList[HashConfigContract['default']]['implementation']
> extends ManagerContract<
HashDriverContract,
{ [P in keyof HashList]: HashList[P]['implementation'] }
> {
hash (value: string): ReturnType<HashDriverContract['hash']>
verify (hashedValue: string, plainValue: string): ReturnType<HashDriverContract['verify']>
needsReHash (hashedValue: string): ReturnType<HashDriverContract['needsReHash']>
hash (value: string): ReturnType<DriverMethod<DefaultDriver, 'hash'>>
verify (hashedValue: string, plainValue: string): ReturnType<DriverMethod<DefaultDriver, 'verify'>>
needsReHash (hashedValue: string): ReturnType<DriverMethod<DefaultDriver, 'needsReHash'>>
}

const Hash: HashContract
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/Argon.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* @poppinss/hash
* @adonisjs/hash
*
* (c) Harminder Virk <[email protected]>
*
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/Bcrypt.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* @poppinss/hash
* @adonisjs/hash
*
* (c) Harminder Virk <[email protected]>
*
Expand Down
13 changes: 9 additions & 4 deletions src/Hash.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* @poppinss/hash
* @adonisjs/hash
*
* (c) Harminder Virk <[email protected]>
*
Expand All @@ -10,15 +10,20 @@
/// <reference path="../adonis-typings/hash.ts" />

import { Manager } from '@poppinss/manager'
import { HashContract, HashDriverContract, HashConfigContract, HashList } from '@ioc:Adonis/Core/Hash'
import {
HashContract,
HashDriverContract,
HashConfigContract,
HashList,
} from '@ioc:Adonis/Core/Hash'

/**
* The Hash module exposes the API to hash values using an underlying
* Hash driver.
*/
export class Hash <Config extends HashConfigContract>
extends Manager<HashDriverContract, { [P in keyof HashList]: HashList[P]['implementation'] }>
implements HashContract
implements HashContract<HashDriverContract>
{
constructor (container: any, public config: Config) {
super(container)
Expand Down Expand Up @@ -69,7 +74,7 @@ export class Hash <Config extends HashConfigContract>
/**
* Hash value using the default driver
*/
public hash (value: string) {
public hash (value: string): never | any {
return this.use().hash(value)
}

Expand Down
12 changes: 6 additions & 6 deletions standlone.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*
* @poppinss/hash
*
* (c) Harminder Virk <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
* @adonisjs/hash
*
* (c) Harminder Virk <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

export { Hash } from './src/Hash'
2 changes: 1 addition & 1 deletion test/argon2.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* @poppinss/hash
* @adonisjs/hash
*
* (c) Harminder Virk <[email protected]>
*
Expand Down
2 changes: 1 addition & 1 deletion test/bcrypt.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* @poppinss/hash
* @adonisjs/hash
*
* (c) Harminder Virk <[email protected]>
*
Expand Down
2 changes: 1 addition & 1 deletion test/hash.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* @poppinss/hash
* @adonisjs/hash
*
* (c) Harminder Virk <[email protected]>
*
Expand Down

0 comments on commit b988fc8

Please sign in to comment.