Skip to content

Commit

Permalink
Merge pull request #19 from mysteriumnetwork/tk/fix-exports
Browse files Browse the repository at this point in the history
Spring cleanup
  • Loading branch information
tadaskay authored May 6, 2019
2 parents 177e9a4 + dec6f27 commit 8f3ab67
Show file tree
Hide file tree
Showing 88 changed files with 581 additions and 1,274 deletions.
31 changes: 15 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Library "mysterium-vpn-js"
# mysterium-vpn-js

[![npm version](https://badge.fury.io/js/mysterium-vpn-js.svg)](https://badge.fury.io/js/mysterium-vpn-js)
[![Build Status](https://travis-ci.com/mysteriumnetwork/mysterium-vpn-js.svg?branch=master)](https://travis-ci.com/mysteriumnetwork/mysterium-vpn-js)

Javascript SDK for [mysteriumnetwork/node](https://github.com/mysteriumnetwork/node)
Javascript SDK for [mysteriumnetwork/node](https://github.com/mysteriumnetwork/node)

* [API documentation](http://tequilapi.mysterium.network)

Expand All @@ -21,7 +21,7 @@ npm i mysterium-tequilapi
import TequilapiClientFactory, { TEQUILAPI_URL } from 'mysterium-vpn-js'
const factory = new TequilapiClientFactory(TEQUILAPI_URL)
const client = factory.build(factory.buildAdapter())
client.identitiesList().then((identities) => {
client.identityList().then((identities) => {
console.log(identities)
})
```
Expand Down Expand Up @@ -59,31 +59,30 @@ Output:
```

Client object fulfills the following interface:
```javascript
```typescript
interface TequilapiClient {
healthCheck (timeout: ?number): Promise<NodeHealthcheckDTO>,
healthCheck (timeout: ?number): Promise<NodeHealthcheck>,
stop (): Promise<void>,

identitiesList (): Promise<Array<IdentityDTO>>,
identityCreate (passphrase: string): Promise<IdentityDTO>,
identityList (): Promise<Array<Identity>>,
identityCreate (passphrase: string): Promise<Identity>,
identityUnlock (id: string, passphrase: string): Promise<void>,
identityRegistration (id: string): Promise<IdentityRegistrationDTO>,
identityRegistration (id: string): Promise<IdentityRegistration>,

findProposals (query: ?ProposalsQuery): Promise<Array<ProposalDTO>>,
findProposals (query: ?ProposalQuery): Promise<Array<Proposal>>,

connectionCreate (request: ConnectionRequest, timeout: ?number): Promise<ConnectionStatusDTO>,
connectionStatus (): Promise<ConnectionStatusDTO>,
connectionCreate (request: ConnectionRequest, timeout: ?number): Promise<ConnectionStatusResponse>,
connectionStatus (): Promise<ConnectionStatusResponse>,
connectionCancel (): Promise<void>,
connectionIP (timeout: ?number): Promise<ConnectionIPDTO>,
connectionStatistics (): Promise<ConnectionStatisticsDTO>,
location (timeout: ?number): Promise<ConsumerLocationDTO>
connectionIp (timeout: ?number): Promise<ConnectionIp>,
connectionStatistics (): Promise<ConnectionStatistics>,
location (timeout: ?number): Promise<ConsumerLocation>
}
```

## Contributing

* Run CI build: `npm run ci`
* Check in updated/new typescript and flow definitions in `lib/`
* Run CI build: `yarn ci`

## Authors
* **Andrej Novikov** - [shroomist](https://github.com/shroomist)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { parseAccessPoliciesDTO, parseAccessPolicyDTO } from './access-policies'
import { parseAccessPolicyList, parseAccessPolicy } from './access-policy'

describe('.parseAccessPoliciesDTO', () => {
describe('.parseAccessPolicyList', () => {
const accessPolicy = {
id: 'mysterium',
title: 'mysterium verified',
Expand All @@ -31,32 +31,32 @@ describe('.parseAccessPoliciesDTO', () => {
}

it('sets properties with full structure', async () => {
const services = parseAccessPoliciesDTO({ entries: [accessPolicy] })
const services = parseAccessPolicyList({ entries: [accessPolicy] })

expect(services).toHaveLength(1)
expect(services[0]).toEqual(parseAccessPolicyDTO(accessPolicy))
expect(services[0]).toEqual(parseAccessPolicy(accessPolicy))
})

it('sets properties with an empty structure', async () => {
const services = parseAccessPoliciesDTO({ entries: [] })
const services = parseAccessPolicyList({ entries: [] })
expect(services).toEqual([])
})

it('throws an error if services in array does not validate', async () => {
expect(() => {
parseAccessPoliciesDTO({ entries: [{}] })
}).toThrowError('AccessPolicyDTO: id is not provided')
parseAccessPolicyList({ entries: [{}] })
}).toThrowError('AccessPolicy: id is not provided')
})

it('throws an error if access policy list in not an object', async () => {
expect(() => {
parseAccessPoliciesDTO({})
}).toThrowError('AccessPolicyDTO[]: entries is not provided')
parseAccessPolicyList({})
}).toThrowError('AccessPolicy[]: entries is not provided')
})

it('throws an error if service list in not an array', async () => {
expect(() => {
parseAccessPoliciesDTO({ entries: {} })
}).toThrowError('AccessPolicyDTO[]: entries should be "array"')
parseAccessPolicyList({ entries: {} })
}).toThrowError('AccessPolicy[]: entries should be "array"')
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,44 @@

import { validate, validateMultiple } from '../fmt/validation'

interface AccessRule {
export interface AccessRule {
type: string
value?: string
}

export interface AccessPolicyDTO {
export interface AccessPolicy {
id: string
title: string
description: string
allow: AccessRule[]
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function parseAccessRule(responseData: any): AccessRule {
validateMultiple('AccessRule', responseData, [{ name: 'type', type: 'string' }])

if (responseData.value) {
validate('AccessRule.value', responseData, { name: 'value', type: 'string' })
}

return {
type: responseData.type,
value: responseData.value,
}
return responseData
}

export function parseAccessPolicyDTO(responseData: any): AccessPolicyDTO {
validateMultiple('AccessPolicyDTO', responseData, [
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function parseAccessPolicy(responseData: any): AccessPolicy {
validateMultiple('AccessPolicy', responseData, [
{ name: 'id', type: 'string' },
{ name: 'title', type: 'string' },
{ name: 'description', type: 'string' },
{ name: 'allow', type: 'array' },
])

return {
id: responseData.id,
title: responseData.title,
description: responseData.description,
...responseData,
allow: responseData.allow.map(parseAccessRule),
}
}

export function parseAccessPoliciesDTO(responseData: any): AccessPolicyDTO[] {
validate('AccessPolicyDTO[]', responseData, { name: 'entries', type: 'array' })

return responseData.entries.map(parseAccessPolicyDTO)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function parseAccessPolicyList(responseData: any): AccessPolicy[] {
validate('AccessPolicy[]', responseData, { name: 'entries', type: 'array' })
return responseData.entries.map(parseAccessPolicy)
}
18 changes: 0 additions & 18 deletions src/access-policy/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { ConnectCountDTO, parseConnectionCountDTO } from './connect-count-dto'
import { ConnectionCount, parseConnectionCount } from './count'

describe('.parseConnectionCountDTO', () => {
describe('.parseConnectionCount', () => {
it('returns ConnectionCountDTO', () => {
const dto: ConnectCountDTO = parseConnectionCountDTO({ success: 1, fail: 2, timeout: 3 })
const dto: ConnectionCount = parseConnectionCount({ success: 1, fail: 2, timeout: 3 })
expect(dto).toBeDefined()
})

it('throws error for invalid data', () => {
expect(() => parseConnectionCountDTO({ success: 1, timeout: 3 })).toThrow(
'ConnectCountDTO: fail is not provided'
expect(() => parseConnectionCount({ success: 1, timeout: 3 })).toThrow(
'ConnectCount: fail is not provided'
)
expect(() => parseConnectionCountDTO({ success: '1', fail: 2, timeout: 3 })).toThrow(
'ConnectCountDTO: success should be "number"'
expect(() => parseConnectionCount({ success: '1', fail: 2, timeout: 3 })).toThrow(
'ConnectCount: success should be "number"'
)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@

import { validateMultiple } from '../fmt/validation'

export interface ConnectCountDTO {
export interface ConnectionCount {
success: number
fail: number
timeout: number
}

export function parseConnectionCountDTO(data: any): ConnectCountDTO {
validateMultiple('ConnectCountDTO', data, [
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function parseConnectionCount(data: any): ConnectionCount {
validateMultiple('ConnectCount', data, [
{ name: 'success', type: 'number' },
{ name: 'fail', type: 'number' },
])
return { success: 1, fail: 2, timeout: 3 }
return data
}
24 changes: 0 additions & 24 deletions src/connection/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { ConnectionIPDTO, parseConnectionIPDTO } from './connection-ip'
import { ConnectionIp, parseConnectionIp } from './ip'

describe('TequilapiClient DTO', () => {
describe('.parseConnectionIPDTO', () => {
describe('.parseConnectionIp', () => {
it('sets properties', async () => {
const model: ConnectionIPDTO = parseConnectionIPDTO({ ip: 'mock ip' })
const model: ConnectionIp = parseConnectionIp({ ip: 'mock ip' })

expect(model.ip).toEqual('mock ip')
})

it('sets empty properties', async () => {
const model: ConnectionIPDTO = parseConnectionIPDTO({})
const model: ConnectionIp = parseConnectionIp({})

expect(model.ip).toBeUndefined()
})
Expand Down
7 changes: 4 additions & 3 deletions src/connection/connection-ip.ts → src/connection/ip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

export interface ConnectionIPDTO {
export interface ConnectionIp {
ip?: string
}

export function parseConnectionIPDTO(data: any): ConnectionIPDTO {
return { ip: data.ip }
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function parseConnectionIp(data: any): ConnectionIp {
return data
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import { validateMultiple } from '../fmt/validation'

export interface ConnectionSessionDTO {
export interface ConnectionSession {
sessionId: string
providerId: string
providerCountry: string
Expand All @@ -27,8 +27,9 @@ export interface ConnectionSessionDTO {
duration: number
}

export function validateSession(data: any): ConnectionSessionDTO {
validateMultiple('ConnectionSessionDTO', data, [
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function validateSession(data: any): ConnectionSession {
validateMultiple('ConnectionSession', data, [
{ name: 'sessionId', type: 'string' },
{ name: 'providerId', type: 'string' },
{ name: 'providerCountry', type: 'string' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { parseConnectionStatisticsDTO } from './connection-statistics'
import { parseConnectionStatistics } from './statistics'

describe('TequilapiClient DTO', () => {
describe('.parseConnectionStatisticsDTO', () => {
describe('.parseConnectionStatistics', () => {
it('sets properties', async () => {
const stats = parseConnectionStatisticsDTO({
const stats = parseConnectionStatistics({
duration: 13325,
bytesReceived: 1232133, // 1.17505 MB
bytesSent: 123321, // 0.117608 MB
Expand All @@ -32,8 +32,8 @@ describe('TequilapiClient DTO', () => {
})

it('throws error without required fields', async () => {
expect(() => parseConnectionStatisticsDTO({})).toThrow()
expect(() => parseConnectionStatisticsDTO('I am wrong')).toThrow()
expect(() => parseConnectionStatistics({})).toThrow()
expect(() => parseConnectionStatistics('I am wrong')).toThrow()
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,18 @@

import { validateMultiple } from '../fmt/validation'

export interface ConnectionStatisticsDTO {
export interface ConnectionStatistics {
duration: number
bytesReceived: number
bytesSent: number
}

export function parseConnectionStatisticsDTO(data: any): ConnectionStatisticsDTO {
validateMultiple('ConnectionStatisticsDTO', data, [
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function parseConnectionStatistics(data: any): ConnectionStatistics {
validateMultiple('ConnectionStatistics', data, [
{ name: 'duration', type: 'number' },
{ name: 'bytesReceived', type: 'number' },
{ name: 'bytesSent', type: 'number' },
])

return {
duration: data.duration,
bytesReceived: data.bytesReceived,
bytesSent: data.bytesSent,
}
return data
}
Loading

0 comments on commit 8f3ab67

Please sign in to comment.