Skip to content

Commit

Permalink
Create base component for data models.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Reetz committed Dec 1, 2020
1 parent 1ec0e32 commit 68137f2
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"semi": false,
"trailingComma": "all",
"singleQuote": true
"singleQuote": true,
"printWidth": 100
}
3 changes: 1 addition & 2 deletions packages/http/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ export { default as Client } from './Client'
export { default } from './Client'
export { default as Network } from './Network'
export { default as ResourceList } from './ResourceList'
export { HTTPHotspotObject as Hotspot } from './models/Hotspot'
export { HTTPAccountObject as Account } from './models/Account'
export { HotspotData as Hotspot } from './models/Hotspot'
15 changes: 15 additions & 0 deletions packages/http/src/models/DataModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import type Client from '../Client'

export default abstract class DataModel {
protected client: Client

constructor(client: Client) {
this.client = client
}

get data(): Omit<this, 'client'> {
const { client, ...rest } = this
return { ...rest }
}
}
24 changes: 20 additions & 4 deletions packages/http/src/models/Hotspot.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import camelcaseKeys from 'camelcase-keys'
import type Client from '../Client'
import Transactions from '../resources/Transactions'
import Hotspots from "../resources/Hotspots"
import Hotspots from '../resources/Hotspots'
import DataModel from './DataModel'

export type HotspotData = Omit<Hotspot, 'client'>

export interface HTTPHotspotObject {
score_update_height?: number
Expand Down Expand Up @@ -48,25 +51,38 @@ interface Status {
online: string
}

export default class Hotspot {
private client: Client
export default class Hotspot extends DataModel {
public scoreUpdateHeight?: number

public score?: number

public owner?: string

public name?: string

public location?: string

public lng?: number

public lat?: number

public block?: number

public geocode?: Geocode

public address: string

public status?: Status

public nonce?: number

public blockAdded?: number

public timestampAdded?: string

constructor(client: Client, hotspot: HTTPHotspotObject) {
this.client = client
super(client)

this.scoreUpdateHeight = hotspot.score_update_height
this.score = hotspot.score
this.owner = hotspot.owner
Expand Down
13 changes: 5 additions & 8 deletions packages/http/src/resources/Hotspots.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type Client from '../Client'
import Hotspot, { HTTPHotspotObject } from '../models/Hotspot'
import Hotspot, { HTTPHotspotObject, HotspotData } from '../models/Hotspot'
import ResourceList from '../ResourceList'
import Account from '../models/Account'
import City from '../models/City'
Expand All @@ -26,17 +26,14 @@ export default class Hotspots {

async list(params: ListParams = {}): Promise<ResourceList<Hotspot>> {
const { hotspots, cursor } = await this.fetchList(params)
const data = hotspots.map(
(d: HTTPHotspotObject) => new Hotspot(this.client, d),
)
const data = hotspots.map((d: HTTPHotspotObject) => new Hotspot(this.client, d))
return new ResourceList(data, this.list.bind(this), cursor)
}

async listJson(
params: ListParams = {},
): Promise<ResourceList<HTTPHotspotObject>> {
async listData(params: ListParams = {}): Promise<ResourceList<HotspotData>> {
const { hotspots, cursor } = await this.fetchList(params)
return new ResourceList(hotspots, this.list.bind(this), cursor)
const data = hotspots.map((d: HTTPHotspotObject) => new Hotspot(this.client, d).data)
return new ResourceList(data, this.list.bind(this), cursor)
}

private async fetchList(
Expand Down

0 comments on commit 68137f2

Please sign in to comment.