Skip to content

Commit

Permalink
refactor: add api call method
Browse files Browse the repository at this point in the history
Refactor all api requests to unified format
  • Loading branch information
kyzia551 committed Feb 12, 2019
1 parent c109c66 commit 141701d
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 99 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "ethlend-js",
"name": "aave-js",
"version": "0.0.0",
"description": "",
"keywords": [],
"main": "dist/ethlend-js.umd.js",
"module": "dist/ethlend-js.es5.js",
"typings": "dist/types/ethlend-js.d.ts",
"main": "dist/aave-js.umd.js",
"module": "dist/aave-js.es5.js",
"typings": "dist/types/aave-js.d.ts",
"files": [
"dist"
],
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import json from 'rollup-plugin-json'

const pkg = require('./package.json')

const libraryName = 'ethlend-js'
const libraryName = 'aave-js'

export default {
input: `src/${libraryName}.ts`,
Expand Down
87 changes: 0 additions & 87 deletions src/LoanRequest.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/ethlend-js.ts → src/aave-js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// import "core-js/fn/array.find"
// ...
import { LoanAPIInstance } from './types'
import LoanRequest from './LoanRequest'
import LoanRequest from './services/LoanRequest'

export class Marketplace {
export class Marketplace implements Marketplace {
public request: LoanAPIInstance

constructor(token: string) {
Expand Down
2 changes: 1 addition & 1 deletion src/BaseService.ts → src/services/BaseService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios, { AxiosError, AxiosInstance } from 'axios'
import keccak from 'keccak'

import { BaseResponse } from './types'
import { BaseResponse } from '../types'

export default class BaseService {
protected readonly api: AxiosInstance
Expand Down
98 changes: 98 additions & 0 deletions src/services/LoanRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import {
LoanTransactionResponse,
LoanMetadataResponse,
LoanRequestModel,
LoanRequestResponse,
LoanAddressesByBorrowerResponse,
LoansAddressesResponse,
LoanAPIInstance,
BaseResponse
} from '../types'

import BaseService from './BaseService'

export default class LoanRequest extends BaseService implements LoanAPIInstance {
constructor(token: string) {
super(token)
}

private async apiRequest(
endpoint: string,
resourceType: string,
errorParam: string = '',
method: 'get' | 'post' = 'get',
params?: object
): Promise<BaseResponse> {
const api = method === 'post' ? this.api.post : this.api.get

try {
const { data } = await api(endpoint, params)

return { data, code: 200 }
} catch (e) {
return LoanRequest.errorHandler(e, resourceType, errorParam)
}
}

public async create(creatorWalletAddress: string, params: LoanRequestModel): Promise<LoanTransactionResponse> {
BaseService.checkAddressChecksum(creatorWalletAddress)

return await this.apiRequest(`/request/create/${creatorWalletAddress}`, 'loan request creation', '', 'post', params)
}

public async placeCollateral(loanAddress: string, borrowerAddress: string): Promise<LoanTransactionResponse> {
BaseService.checkAddressChecksum(loanAddress)
BaseService.checkAddressChecksum(borrowerAddress)

return await this.apiRequest(
`/request/placecollateral/${loanAddress}/${borrowerAddress}`,
'placing loan request collateral',
loanAddress,
'post'
)
}

public async fund(loanAddress: string, lenderAddress: string, amount: number): Promise<LoanTransactionResponse> {
BaseService.checkAddressChecksum(loanAddress)
BaseService.checkAddressChecksum(lenderAddress)

return await this.apiRequest(
`/request/fund/${loanAddress}/${lenderAddress}/${amount}`,
'funding loan request',
loanAddress,
'post'
)
}

public async payback(loanAddress: string, borrowerAddress: string): Promise<LoanTransactionResponse> {
BaseService.checkAddressChecksum(loanAddress)
BaseService.checkAddressChecksum(borrowerAddress)

return await this.apiRequest(
`/request/payback/${loanAddress}/${borrowerAddress}`,
'placing loan request payback',
loanAddress,
'post'
)
}

public async getLoanData(loanAddress: string): Promise<LoanRequestResponse> {
BaseService.checkAddressChecksum(loanAddress)

return await this.apiRequest(`/request/${loanAddress}`, 'loan request', loanAddress)
}

public async getAllAddresses(): Promise<LoansAddressesResponse> {
return await this.apiRequest('/requests', 'loan request addresses')
}

public async getLoansByBorrower(borrowerAddress: string): Promise<LoanAddressesByBorrowerResponse> {
BaseService.checkAddressChecksum(borrowerAddress)

return await this.apiRequest(`/requests/${borrowerAddress}`, 'loan addresses by borrower', borrowerAddress)
}

public async getMetadata(): Promise<LoanMetadataResponse> {
return await this.apiRequest('/requests/metadata', 'loan requests metadata')
}
}
9 changes: 7 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,18 @@ export interface LoanAddressesByBorrowerResponse extends BaseResponse {
export interface LoanAPIInstance {
create(creatorWalletAddress: string, params: LoanRequestModel): Promise<LoanTransactionResponse>
placeCollateral(loanAddress: string, borrowerAddress: string): Promise<LoanTransactionResponse>
fund(loanAddress: string, lenderAddress: string, amount: number): Promise<LoanTransactionResponse>
payback(loanAddress: string, borrowerAddress: string): Promise<LoanTransactionResponse>
getLoanData(loanAddress: string): Promise<LoanRequestResponse>
getAllAddresses(): Promise<LoansAddressesResponse>
getLoansByBorrower(borrowerAddress: string): Promise<LoanAddressesByBorrowerResponse>
getMetadata(): Promise<LoanMetadataResponse>
}

declare const EthlendAPI: LoanAPIInstance
export interface MarketplaceInstance {
request: LoanAPIInstance
}

declare const Marketplace: MarketplaceInstance

export default EthlendAPI
export default Marketplace
2 changes: 1 addition & 1 deletion test/ethlend-js.test.ts → test/aave-js.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Marketplace } from '../src/ethlend-js'
import { Marketplace } from '../src/aave-js'

/**
* Dummy test
Expand Down

0 comments on commit 141701d

Please sign in to comment.