Skip to content
This repository has been archived by the owner on Jun 3, 2022. It is now read-only.

Commit

Permalink
prefix version can now be overridden in production (#334)
Browse files Browse the repository at this point in the history
* prefix version can now be overridden in production

* Checked all data contracts, ready for major v0 release

* added version.e2e.ts
  • Loading branch information
fuxingloh authored Aug 11, 2021
1 parent 1041ffe commit e47b341
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/whale-api-client/src/api/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export interface AddressAggregation {
hex: string
}

statistic: {
statistic: { // TODO(fuxingloh): should be named `count: {}`, too late to change now.
txCount: number
txInCount: number
txOutCount: number
Expand Down
2 changes: 1 addition & 1 deletion packages/whale-api-client/src/api/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export interface Block {
time: number // --------------| block time in seconds since epoch
medianTime: number // --------| median time of the past 11 block timestamps

transactionCount: number
transactionCount: number // TODO(fuxingloh): should be structured as `count: { transaction: number }`, too late to change now

difficulty: number // --------| difficulty of the block.

Expand Down
10 changes: 8 additions & 2 deletions packages/whale-api-client/src/api/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ export interface TokenData {
isLPS: boolean
finalized: boolean
minted: string // BigNumber
creation: { tx: string, height: number }
destruction: { tx: string, height: number }
creation: {
tx: string
height: number
}
destruction: {
tx: string
height: number
}
collateralAddress?: string
}
2 changes: 1 addition & 1 deletion packages/whale-api-client/src/whale.api.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface WhaleApiClientOptions {

/**
* Version of API
* `v{major}.{minor}`: `v${number}.${number}`
* `v{major}.{minor}` or `v{major}`
*/
version?: string

Expand Down
6 changes: 5 additions & 1 deletion src/app.configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
*/
export const AppConfiguration = (): any => ({
isProd: process.env.NODE_ENV === 'production',
/**
* Allows you to override whale endpoint version.
*/
version: process.env.WHALE_VERSION,
network: process.env.WHALE_NETWORK,
defid: {
url: process.env.WHALE_DEFID_URL
},
network: process.env.WHALE_NETWORK,
database: {
// Provider can only be set via environmental variable
provider: process.env.WHALE_DATABASE_PROVIDER,
Expand Down
12 changes: 10 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ export class AppModule {
* - GlobalPrefix 'v{major}.{minor}/${network}' e.g. 'v0.0/regtest'
*/
static configure (app: NestFastifyApplication): void {
const [major, minor] = packageJson.version.split('.')
const version = `v${major}.${minor}`
const version = this.getVersion(app)
const network = app.get(ConfigService).get<string>('network') as string

app.enableCors({
Expand All @@ -71,4 +70,13 @@ export class AppModule {
]
})
}

/**
* @return string version from ConfigService, default to package.json v{major}.{minor} if not found.
*/
static getVersion (app: NestFastifyApplication): string {
const [major, minor] = packageJson.version.split('.')
const defaultVersion = `v${major}.${minor}`
return app.get(ConfigService).get<string>('version', defaultVersion)
}
}
77 changes: 77 additions & 0 deletions src/version.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { NestFastifyApplication } from '@nestjs/platform-fastify'
import { newFastifyAdapter } from '@src/fastify'
import { AppModule } from '@src/app.module'
import { Test } from '@nestjs/testing'
import { ConfigService } from '@nestjs/config'

const container = new MasterNodeRegTestContainer()
let app: NestFastifyApplication

class VersionConfigService extends ConfigService {
constructor (rpcUrl: string) {
super({
defid: {
url: rpcUrl
},
version: 'v0',
network: 'regtest',
database: {
provider: 'memory'
}
})
}
}

beforeAll(async () => {
await container.start()
await container.generate(1)

const url = await container.getCachedRpcUrl()
const module = await Test.createTestingModule({
imports: [AppModule.forRoot('memory')]
})
.overrideProvider(ConfigService).useValue(new VersionConfigService(url))
.compile()

app = module.createNestApplication<NestFastifyApplication>(
newFastifyAdapter({ logger: false })
)
AppModule.configure(app)
await app.init()
})

afterAll(async () => {
try {
await app.close()
} finally {
await container.stop()
}
})

it('should GET /v0/regtest/tokens with overridden endpoint', async () => {
const res = await app.inject({
method: 'GET',
url: '/v0/regtest/tokens'
})

expect(res.statusCode).toStrictEqual(200)
expect(res.json()).toStrictEqual({
data: [
expect.objectContaining({
id: '0',
symbol: 'DFI',
symbolKey: 'DFI'
})
]
})
})

it('should fail GET with /v0.0/regtest/tokens as endpoint is overridden', async () => {
const res = await app.inject({
method: 'GET',
url: '/v0.0/regtest/tokens'
})

expect(res.statusCode).toStrictEqual(404)
})

0 comments on commit e47b341

Please sign in to comment.