-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wip on list iteration * add prepublishOnly * wip resource list * use resource list for accounts * working resource list
- Loading branch information
Showing
33 changed files
with
889 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# `@helium/crypto` | ||
![npm](https://img.shields.io/npm/v/@helium/crypto) | ||
|
||
## Installation | ||
|
||
```shell | ||
$ yarn add @helium/http | ||
# or | ||
$ npm install @helium/http | ||
``` | ||
|
||
## Usage | ||
|
||
### Initializing the Client | ||
|
||
By default, the client will be initialized with the production network: `https://api.helium.io` | ||
```js | ||
import { Client } from '@helium/http' | ||
const client = new Client() | ||
client.endpoint //= https://api.helium.io/v1 | ||
``` | ||
|
||
To specify a specific network, such as staging, the client can be initialized with a `Network` instance | ||
```js | ||
import { Client, Network } from '@helium/http' | ||
const client = new Client(Network.staging) | ||
client.endpoint //= https://api.helium.wtf/v1 | ||
``` | ||
|
||
##### Networks | ||
|
||
| Network | Base URL | Version | | ||
|----------------------|--------------------------|---------| | ||
| `Network.production` | https://api.helium.io | v1 | | ||
| `Network.staging` | https://api.helium.wtf | v1 | | ||
|
||
### Paginating Results | ||
|
||
#### Automatic Pagination | ||
|
||
```js | ||
for await (const account of client.accounts.list()) { | ||
// do something with account | ||
|
||
// after some condition is met, stop iterating | ||
if (someConditionMet) | ||
break | ||
} | ||
``` | ||
|
||
#### Manual Pagination | ||
|
||
```js | ||
const firstPage = await client.accounts.list() | ||
firstPage.data //= [Account, Account, ...] | ||
firstPage.hasMore //= true | ||
|
||
const nextPage = await firstPage.nextPage() | ||
firstPage.data //= [Account, Account, ...] | ||
firstPage.hasMore //= false | ||
``` | ||
|
||
### Resources | ||
|
||
#### Accounts | ||
|
||
#### Blocks | ||
|
||
#### Transactions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,41 @@ | ||
import axios from 'axios' | ||
import Transactions from './Transactions' | ||
|
||
interface ClientOptions { | ||
endpoint?: string | ||
version?: number | ||
} | ||
import axios, { AxiosInstance } from 'axios' | ||
import qs from 'qs' | ||
import Network from './Network' | ||
import Transactions from './resources/Transactions' | ||
import Blocks from './resources/Blocks' | ||
import Accounts from './resources/Accounts' | ||
import { getUserAgent } from './util/instrument' | ||
|
||
export default class Client { | ||
public endpoint!: string | ||
public version!: number | ||
public network!: Network | ||
private axios!: AxiosInstance | ||
|
||
public transactions!: Transactions | ||
public blocks!: Blocks | ||
public accounts!: Accounts | ||
|
||
constructor({ | ||
endpoint = 'https://api.helium.io', | ||
version = 1, | ||
}: ClientOptions = {}) { | ||
this.endpoint = endpoint | ||
this.version = version | ||
constructor(network: Network = Network.production) { | ||
this.network = network | ||
|
||
this.axios = axios.create({ | ||
baseURL: this.network.endpoint, | ||
headers: { | ||
'User-Agent': getUserAgent(), | ||
}, | ||
}) | ||
|
||
this.transactions = new Transactions(this) | ||
this.blocks = new Blocks(this) | ||
this.accounts = new Accounts(this) | ||
} | ||
|
||
async post(path: string, params: Object = {}) { | ||
const url = this.toUrl(path) | ||
return axios.post(url, params) | ||
async get(path: string, params: Object = {}) { | ||
const query = qs.stringify(params) | ||
const url = query.length > 0 ? [path, query].join('?') : path | ||
return this.axios.get(url) | ||
} | ||
|
||
private toUrl(path: string): string { | ||
return [this.endpoint, `v${1}`, path.replace(/^\/+/, '')].join('/') | ||
async post(path: string, params: Object = {}) { | ||
return this.axios.post(path, params) | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
interface NetworkOptions { | ||
baseURL: string | ||
version: number | ||
} | ||
|
||
export default class Network { | ||
static production = new Network({ | ||
baseURL: 'https://api.helium.io', | ||
version: 1, | ||
}) | ||
|
||
static staging = new Network({ | ||
baseURL: 'https://api.helium.wtf', | ||
version: 1, | ||
}) | ||
|
||
public baseURL: string | ||
public version: number | ||
|
||
constructor({ baseURL, version }: NetworkOptions) { | ||
this.baseURL = baseURL | ||
this.version = version | ||
} | ||
|
||
get endpoint(): string { | ||
return [this.baseURL, `v${this.version}`].join('/') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
export default class ResourceList { | ||
public data: Array<any> | ||
private fetchMore?: any | ||
private cursor?: string | ||
private takeIterator?: AsyncGenerator<any, void, any> | ||
|
||
constructor(data: Array<any>, fetchMore?: any, cursor?: string) { | ||
this.data = data | ||
this.fetchMore = fetchMore | ||
this.cursor = cursor | ||
} | ||
|
||
async nextPage(): Promise<ResourceList> { | ||
return this.fetchMore({ cursor: this.cursor }) | ||
} | ||
|
||
public get hasMore(): boolean { | ||
return !!this.cursor && !!this.fetchMore | ||
} | ||
|
||
async *[Symbol.asyncIterator]() { | ||
for (const item of this.data) { | ||
yield item | ||
} | ||
if (!this.hasMore) return | ||
yield* await this.fetchMore({ cursor: this.cursor }) | ||
} | ||
|
||
async take(count: number): Promise<Array<any>> { | ||
if (!this.takeIterator) { | ||
this.takeIterator = this[Symbol.asyncIterator]() | ||
} | ||
const values = [] | ||
while (values.length < count) { | ||
const { value, done } = await this.takeIterator.next() | ||
if (value !== undefined) values.push(value) | ||
if (done) return values | ||
} | ||
return values | ||
} | ||
|
||
takeReset() { | ||
this.takeIterator = undefined | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,46 @@ | ||
import axios from 'axios' | ||
import nock from 'nock' | ||
import Client from '..' | ||
|
||
jest.mock('axios') | ||
const mockedAxios = axios as jest.Mocked<typeof axios> | ||
import { Network } from '../' | ||
|
||
test('exposes a client instance with default options', () => { | ||
const prodUrl = 'https://api.helium.io' | ||
const client = new Client() | ||
expect(client.endpoint).toBe(prodUrl) | ||
expect(client.network.baseURL).toBe(prodUrl) | ||
}) | ||
|
||
test('configure client with different endpoint', () => { | ||
const stagingUrl = 'https://api.helium.wtf' | ||
const client = new Client({ endpoint: stagingUrl }) | ||
expect(client.endpoint).toBe(stagingUrl) | ||
const client = new Client(Network.staging) | ||
expect(client.network.baseURL).toBe(stagingUrl) | ||
}) | ||
|
||
test('configure client with different version', () => { | ||
const client = new Client({ version: 2 }) | ||
expect(client.version).toBe(2) | ||
describe('get', () => { | ||
it('creates a GET request to the full url', async () => { | ||
nock('https://api.helium.io') | ||
.get('/v1/greeting') | ||
.reply(200, { | ||
greeting: 'hello', | ||
}) | ||
|
||
const client = new Client() | ||
|
||
const { data } = await client.get('/greeting') | ||
|
||
expect(data.greeting).toBe('hello') | ||
}) | ||
}) | ||
|
||
describe('http methods', () => { | ||
it('posts requests to the full url', async () => { | ||
describe('post', () => { | ||
it('creates a POST request to the full url', async () => { | ||
nock('https://api.helium.io') | ||
.post('/v1/greeting', { greeting: 'hello' }) | ||
.reply(200, { | ||
response: 'hey there!', | ||
}) | ||
const client = new Client() | ||
const params = { greeting: 'hello' } | ||
|
||
await client.post('/greeting', params) | ||
|
||
expect(mockedAxios.post).toHaveBeenCalledWith( | ||
'https://api.helium.io/v1/greeting', | ||
params, | ||
) | ||
const { data } = await client.post('/greeting', params) | ||
expect(data.response).toBe('hey there!') | ||
}) | ||
}) |
Oops, something went wrong.