浏览代码

expose raw client response under a symbol key

tags/v1.0.0
ojizero 1年前
父节点
当前提交
adc46c2ac1
没有帐户链接到提交者的电子邮件
共有 4 个文件被更改,包括 18 次插入13 次删除
  1. 0
    4
      examples/sample-elasticsearch-client/index.js
  2. 4
    2
      src/client.ts
  3. 3
    3
      src/index.ts
  4. 11
    4
      test/client.spec.ts

+ 0
- 4
examples/sample-elasticsearch-client/index.js 查看文件

@@ -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) {
{"a":{"test":"bulkoperation"}}
`
})
delete response._rawResponse // This is a HUGE object
console.log({ getDocument: { stringifiedResponse: JSON.stringify(response) } })
})()
}

+ 4
- 2
src/client.ts 查看文件

@@ -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 {
},
body: any,
headers: IncomingHttpHeaders,
_rawResponse: RawResponse,
[rawResponseSymbol]: RawResponse,
}

export type RequestBodyObject = { [k: string]: any }
@@ -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,
}
}
}

+ 3
- 3
src/index.ts 查看文件

@@ -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 {
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 {
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

+ 11
- 4
test/client.spec.ts 查看文件

@@ -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 = {
},
body: 'any',
headers: { some: 'header' },
_rawResponse: mockRawResponse,
[rawResponseSymbol]: mockRawResponse,
}

const mockConfig: Config = {
@@ -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', () => {

expect(requestOptions).to.deep.equal({
baseUrl: "https://dummy.domain",
body: "{}",
body: {},
headers: { some: 'mock' },
json: false,
method: "GET",

正在加载...
取消
保存