Skip to content

Commit

Permalink
chore(core/queryApi): rename text to queryRaw
Browse files Browse the repository at this point in the history
  • Loading branch information
sranka committed Sep 9, 2020
1 parent 3922e49 commit 9b32a55
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 37 deletions.
18 changes: 9 additions & 9 deletions packages/core/src/QueryApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ export default interface QueryApi {
consumer: FluxResultObserver<string[]>
): void

/**
* QueryRaw executes a query and returns the full response as a string.
* Use with caution, a possibly huge stream is copied to memory.
*
* @param query - query
* @returns Promise of response text
*/
queryRaw(query: string | ParameterizedQuery): Promise<string>

/**
* CollectRows executes the query and collects all the results in the returned Promise.
* This method is suitable to collect simple results. Use with caution,
Expand Down Expand Up @@ -119,13 +128,4 @@ export default interface QueryApi {
* @returns Promise of returned csv lines
*/
collectLines(query: string | ParameterizedQuery): Promise<Array<string>>

/**
* Text executes a query and returns the full response as a string.
* Use with caution, a possibly huge stream is copied to memory.
*
* @param query - query
* @returns Promise of response text
*/
text(query: string | ParameterizedQuery): Promise<string>
}
2 changes: 1 addition & 1 deletion packages/core/src/impl/QueryApiImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class QueryApiImpl implements QueryApi {
})
}

text(query: string | ParameterizedQuery): Promise<string> {
queryRaw(query: string | ParameterizedQuery): Promise<string> {
const {org, type, gzip} = this.options
return this.transport.request(
`/api/v2/query?org=${encodeURIComponent(org)}`,
Expand Down
37 changes: 10 additions & 27 deletions packages/core/test/unit/QueryApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('QueryApi', () => {
nock.cleanAll()
nock.enableNetConnect()
})
it('receives raw lines', async () => {
it('receives lines', async () => {
const subject = new InfluxDB(clientOptions).getQueryApi(ORG).with({})
nock(clientOptions.url)
.post(QUERY_PATH)
Expand Down Expand Up @@ -209,7 +209,7 @@ describe('QueryApi', () => {
expect(body?.now).to.deep.equal(pair.now)
}
})
it('collectLines collects raw lines', async () => {
it('collectLines collects lines', async () => {
const subject = new InfluxDB(clientOptions).getQueryApi(ORG).with({})
nock(clientOptions.url)
.post(QUERY_PATH)
Expand Down Expand Up @@ -304,24 +304,7 @@ describe('QueryApi', () => {
() => true // error is expected
)
})
it('collectLines collects raw lines', async () => {
const subject = new InfluxDB(clientOptions).getQueryApi(ORG).with({})
nock(clientOptions.url)
.post(QUERY_PATH)
.reply((_uri, _requestBody) => {
return [
200,
fs.createReadStream('test/fixture/query/simpleResponse.txt'),
{'retry-after': '1'},
]
})
.persist()
const data = await subject.collectLines(
'from(bucket:"my-bucket") |> range(start: 0)'
)
expect(data).to.deep.equal(simpleResponseLines)
})
it('text returns the whole response text', async () => {
it('queryRaw returns the whole response text', async () => {
const subject = new InfluxDB(clientOptions).getQueryApi(ORG).with({})
const expected = fs
.readFileSync('test/fixture/query/simpleResponse.txt')
Expand All @@ -332,12 +315,12 @@ describe('QueryApi', () => {
return [200, expected, {'retry-after': '1', 'content-type': 'text/csv'}]
})
.persist()
const data = await subject.text(
const data = await subject.queryRaw(
'from(bucket:"my-bucket") |> range(start: 0)'
)
expect(data).equals(expected)
})
it('text returns the whole response even if response content type is not text', async () => {
it('queryRaw returns the whole response even if response content type is not text', async () => {
const subject = new InfluxDB(clientOptions).getQueryApi(ORG).with({})
const expected = fs
.readFileSync('test/fixture/query/simpleResponse.txt')
Expand All @@ -348,12 +331,12 @@ describe('QueryApi', () => {
return [200, expected, {'retry-after': '1'}]
})
.persist()
const data = await subject.text(
const data = await subject.queryRaw(
'from(bucket:"my-bucket") |> range(start: 0)'
)
expect(data).equals(expected)
})
it('text returns the plain response text even it is gzip encoded', async () => {
it('queryRaw returns the plain response text even it is gzip encoded', async () => {
const subject = new InfluxDB(clientOptions)
.getQueryApi(ORG)
.with({gzip: true})
Expand All @@ -369,15 +352,15 @@ describe('QueryApi', () => {
]
})
.persist()
const data = await subject.text(
const data = await subject.queryRaw(
'from(bucket:"my-bucket") |> range(start: 0)'
)
const expected = fs
.readFileSync('test/fixture/query/simpleResponse.txt')
.toString()
expect(data).equals(expected)
})
it('text fails on server error', async () => {
it('queryRaw fails on server error', async () => {
const subject = new InfluxDB(clientOptions).getQueryApi(ORG).with({})
nock(clientOptions.url)
.post(QUERY_PATH)
Expand All @@ -389,7 +372,7 @@ describe('QueryApi', () => {
]
})
.persist()
await subject.text('from(bucket:"my-bucket") |> range(start: 0)').then(
await subject.queryRaw('from(bucket:"my-bucket") |> range(start: 0)').then(
() => expect.fail('client error expected on server error'),
() => true // error is expected
)
Expand Down

0 comments on commit 9b32a55

Please sign in to comment.