Skip to content

Commit 4e454c7

Browse files
committed
Implementing first parts of the client constructor
1 parent 328262a commit 4e454c7

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

src/Client.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { account } from './actions/account'
2+
import { ApiKey } from './entities/Apikey'
3+
4+
export class Client {
5+
private apiKey: ApiKey
6+
constructor(apiKey: string) {
7+
this.apiKey = new ApiKey(apiKey)
8+
this.apiKey.validate()
9+
}
10+
account(action: string): void {
11+
if (!account.get(action)) {
12+
throw new Error('unknown action')
13+
}
14+
}
15+
}

src/entities/Apikey.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import { EntityBase } from './EntityBase'
66
export class ApiKey extends EntityBase implements IEntity {
77
errorMessage: string = 'invalid'
88
private apiKey: string
9-
private keyLength: number = 33
9+
private keyLength: number = 34
1010
constructor(apiKey: string) {
1111
super()
1212
this.apiKey = apiKey
1313
}
1414

1515
valid(): boolean {
16-
return this.apiKey.length !== this.keyLength
16+
return this.apiKey.length === this.keyLength
1717
}
1818
}

test/ClientTest.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import test from 'ava'
2+
import { Client } from '../src/Client'
3+
4+
test('constructor is a function', t => {
5+
t.is(typeof Client, 'function')
6+
})
7+
8+
test('client can be instantiated', t => {
9+
t.notThrows(() => new Client('TRU5Z5MNWIEYP4F6DPH2T53IJWZIZ5GT1W'))
10+
});
11+
12+
test('invalid api key throws', t => {
13+
t.throws(() => new Client('y'))
14+
});

test/entities/ApikeyTest.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@ test('Instantiated', (t)=> {
88
t.truthy(a)
99
})
1010

11-
test('is valid', (t)=> {
11+
test('key TRU5Z5MNWIEYP4F6DPH2T53IJWZIZ5GT1W is valid', (t)=> {
1212
const apiKey = 'TRU5Z5MNWIEYP4F6DPH2T53IJWZIZ5GT1W'
1313
const a = new ApiKey(apiKey)
1414
t.truthy(a.valid())
1515
})
1616

17+
test('foo is valid', (t)=> {
18+
const apiKey = 'foo'
19+
const a = new ApiKey(apiKey)
20+
t.false(a.valid())
21+
})
22+
23+
24+

0 commit comments

Comments
 (0)