Browse Source

expose raw client response under a symbol key

master
ojizero 4 years ago
parent
commit
adc46c2ac1
No known key found for this signature in database
GPG Key ID: FEBB7097845B0C7E
  1. 4
      examples/sample-elasticsearch-client/index.js
  2. 6
      src/client.ts
  3. 6
      src/index.ts
  4. 15
      test/client.spec.ts

4
examples/sample-elasticsearch-client/index.js

@ -42,15 +42,12 @@ if (require.main === module) { @@ -42,15 +42,12 @@ if (require.main === module) {
let response
response = await client.createIndex('test-index')
delete response._rawResponse // This is a HUGE object
console.log({ createIndex: { stringifiedResponse: JSON.stringify(response) } })
response = await client.addDocument('test-index', 'test-type', 'test-id', { payload: { a: { test: 'document' } } })
delete response._rawResponse // This is a HUGE object
console.log({ addDocument: { stringifiedResponse: JSON.stringify(response) } })
response = await client.getDocument('test-index', 'test-type', 'test-id')
delete response._rawResponse // This is a HUGE object
console.log({ getDocument: { stringifiedResponse: JSON.stringify(response) } })
response = await client.bulkOperation({
@ -58,7 +55,6 @@ if (require.main === module) { @@ -58,7 +55,6 @@ if (require.main === module) {
{"a":{"test":"bulkoperation"}}
`
})
delete response._rawResponse // This is a HUGE object
console.log({ getDocument: { stringifiedResponse: JSON.stringify(response) } })
})()
}

6
src/client.ts

@ -45,6 +45,8 @@ export interface RawResponse { @@ -45,6 +45,8 @@ export interface RawResponse {
headers: IncomingHttpHeaders,
}
export const rawResponseSymbol = Symbol.for('portal:symbols:raw-response')
export interface Response {
status: {
code?: number,
@ -52,7 +54,7 @@ export interface Response { @@ -52,7 +54,7 @@ export interface Response {
},
body: any,
headers: IncomingHttpHeaders,
_rawResponse: RawResponse,
[rawResponseSymbol]: RawResponse,
}
export type RequestBodyObject = { [k: string]: any }
@ -218,7 +220,7 @@ export class PortalClient implements Client { @@ -218,7 +220,7 @@ export class PortalClient implements Client {
},
body: response.body, // TODO: should it be parsed if needed ?
headers: response.headers,
_rawResponse: response,
[rawResponseSymbol]: response,
}
}
}

6
src/index.ts

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
import PortalClient, { Client, Config } from './client'
import method, { MethodFactory } from './method'
import methodGenerator, { MethodFactory } from './method'
import resrouceGenerator, { ResourceFactory } from './resource'
import got from 'got'
@ -15,7 +15,7 @@ export function createPortalClient (config: Config): Portal { @@ -15,7 +15,7 @@ export function createPortalClient (config: Config): Portal {
const client = new PortalClient(got, config)
const portal: Portal = {
route: method(client),
route: methodGenerator(client),
resource: resrouceGenerator(client),
_client: client,
}
@ -23,8 +23,8 @@ export function createPortalClient (config: Config): Portal { @@ -23,8 +23,8 @@ export function createPortalClient (config: Config): Portal {
return portal
}
export { Client } from './client'
export { MethodSpec } from './method'
export { Client, rawResponseSymbol } from './client'
// Can't a the moment run export { * as Joi } from 'joi'
// so this clumsy way is the solution re-export

15
test/client.spec.ts

@ -1,6 +1,13 @@ @@ -1,6 +1,13 @@
/// <reference path='typings/globals.d.ts' />
import Client, { Config, RawResponse, Authentication, ClientFn, Response } from '../src/client'
import Client, {
Config,
RawResponse,
Authentication,
ClientFn,
Response,
rawResponseSymbol,
} from '../src/client'
const mockRawResponse: RawResponse = {
body: 'any',
@ -16,7 +23,7 @@ const mockTransformedResponse: Response = { @@ -16,7 +23,7 @@ const mockTransformedResponse: Response = {
},
body: 'any',
headers: { some: 'header' },
_rawResponse: mockRawResponse,
[rawResponseSymbol]: mockRawResponse,
}
const mockConfig: Config = {
@ -53,7 +60,7 @@ describe('Client', () => { @@ -53,7 +60,7 @@ describe('Client', () => {
expect(requestOptions).to.deep.equal({
baseUrl: "https://dummy.domain",
body: "{}",
body: {},
headers: {},
json: false,
method: "GET",
@ -73,7 +80,7 @@ describe('Client', () => { @@ -73,7 +80,7 @@ describe('Client', () => {
expect(requestOptions).to.deep.equal({
baseUrl: "https://dummy.domain",
body: "{}",
body: {},
headers: { some: 'mock' },
json: false,
method: "GET",

Loading…
Cancel
Save