Skip to content

Commit

Permalink
feat(hydra-cli): hydra-cli deploy features (#109)
Browse files Browse the repository at this point in the history
* feat: human readable text from rest response (errors and info messages)

affects: @subsquid/hydra-cli

* feat: deployment url now shows for deployments:ls command

affects: @subsquid/hydra-cli
  • Loading branch information
rightjelkin committed Sep 10, 2021
1 parent a56a0e9 commit 3446596
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 33 deletions.
25 changes: 14 additions & 11 deletions packages/hydra-cli/src/commands/deployments/ls.ts
Expand Up @@ -7,16 +7,19 @@ export default class Ls extends Command {

async run(): Promise<void> {
const deployments = await deploymentList()
cli.table(
deployments,
{
id: {},
status: {},
name: {},
artifactUrl: {},
version: {},
},
{}
)
if (deployments) {
cli.table(
deployments,
{
id: {},
status: {},
name: {},
artifactUrl: {},
version: {},
deploymentUrl: {},
},
{}
)
}
}
}
27 changes: 27 additions & 0 deletions packages/hydra-cli/src/rest-client/request.ts
@@ -0,0 +1,27 @@
import * as fetch from 'node-fetch'
export async function request(
apiUrl: string,
fetchContext: fetch.RequestInit | undefined
): Promise<fetch.Response> {
const response = await fetch.default(apiUrl, fetchContext)
const responseBody = await response.clone().json()
if (response.status === 401) {
throw new Error(
`Bad credentials data. Run hydra-cli login or check your account`
)
} else if (response.status === 400 && responseBody.errors.length === 0) {
throw new Error(responseBody.message)
} else if (response.status === 400 && responseBody.errors.length !== 0) {
let validationErrorString = 'some validation problems\n'
for (const error of responseBody.errors) {
for (const constraint of Object.values(error.constraints)) {
validationErrorString += `${constraint}\n`
}
}
throw new Error(validationErrorString)
} else if (response.status === 200) {
return response
} else {
throw new Error(`Server error`)
}
}
10 changes: 3 additions & 7 deletions packages/hydra-cli/src/rest-client/routes/deploy.ts
@@ -1,6 +1,6 @@
import { baseUrl } from '../baseUrl'
import { getCreds } from '../../creds'
import fetch from 'node-fetch'
import { request } from '../request'

export type ResponseBody = {
id: string
Expand All @@ -13,9 +13,9 @@ export type ResponseBody = {
export async function deploy(
deploymentName: string,
artifactUrl: string
): Promise<string> {
): Promise<string | undefined> {
const apiUrl = `${baseUrl}/client/deployment`
const response = await fetch(apiUrl, {
const response = await request(apiUrl, {
method: 'post',
body: JSON.stringify({
name: deploymentName,
Expand All @@ -30,9 +30,5 @@ export async function deploy(
const responseBody = await response.json()
if (response.status === 200) {
return `Created deployment with name ${responseBody.name}`
} else {
throw new Error(
`Failed, status ${response.status}, message: ${responseBody.message}`
)
}
}
10 changes: 3 additions & 7 deletions packages/hydra-cli/src/rest-client/routes/deployments.ts
@@ -1,6 +1,6 @@
import { baseUrl } from '../baseUrl'
import { getCreds } from '../../creds'
import fetch from 'node-fetch'
import { request } from '../request'

export type ResponseBody = {
id: string
Expand All @@ -10,9 +10,9 @@ export type ResponseBody = {
version: number
}

export async function deploymentList(): Promise<ResponseBody[]> {
export async function deploymentList(): Promise<ResponseBody[] | undefined> {
const apiUrl = `${baseUrl}/client/deployment`
const response = await fetch(apiUrl, {
const response = await request(apiUrl, {
method: 'get',
headers: {
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand All @@ -23,9 +23,5 @@ export async function deploymentList(): Promise<ResponseBody[]> {
const responseBody = await response.json()
if (response.status === 200) {
return responseBody
} else {
throw new Error(
`Failed, status ${response.status}, message: ${responseBody.message}`
)
}
}
10 changes: 3 additions & 7 deletions packages/hydra-cli/src/rest-client/routes/me.ts
@@ -1,19 +1,15 @@
import { baseUrl } from '../baseUrl'
import fetch from 'node-fetch'
import { request } from '../request'

export async function me(authToken: string): Promise<string> {
export async function me(authToken: string): Promise<string | undefined> {
const apiUrl = `${baseUrl}/client/me`
const response = await fetch(apiUrl, {
const response = await request(apiUrl, {
headers: {
authorization: `token ${authToken}`,
},
})
const responseBody = await response.json()
if (response.status === 200) {
return `Successfully logged as ${responseBody.username}`
} else {
throw new Error(
`Failed, status ${response.status}, message: ${responseBody.message}`
)
}
}
4 changes: 3 additions & 1 deletion packages/hydra-cli/test/rest/deploy-command.test.ts
Expand Up @@ -57,7 +57,9 @@ describe('deploy command', () => {
.do(() => creds.setCreds('test_creds')) // perhaps we got an expired or broken creds
.command(['deploy', '-n', successResponseFixture.name])
.catch((err) =>
expect(err.message.indexOf('status 401')).to.satisfy((index) => index > 0)
expect(err.message.indexOf('Run hydra-cli login')).to.satisfy(
(index) => index > 0
)
)
.it('show error message when broken creds')
})

0 comments on commit 3446596

Please sign in to comment.