Skip to content

Commit

Permalink
feat(core): add optional headers in raw response
Browse files Browse the repository at this point in the history
  • Loading branch information
pviti committed Apr 7, 2022
1 parent 69d27be commit 9bff319
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 13 deletions.
5 changes: 4 additions & 1 deletion specs/commercelayer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,18 @@ describe('SDK:commercelayer suite', () => {
it('commercelayer.rawResponse', async () => {

jest.setTimeout(10000)
const headers = true

const cli = await initClient()

const reader = cli.addRawResponseReader()
const reader = cli.addRawResponseReader({ headers })
expect(reader).not.toBeUndefined()
expect(reader.id).toBeGreaterThanOrEqual(0)

await cli.customers.list({ pageSize: 1 })
expect(reader.rawResponse.data).not.toBeUndefined()
if (headers) expect(reader.headers).not.toBeUndefined()
else expect(reader.headers).toBeUndefined()

cli.removeRawResponseReader(reader.id)
cli.removeRawResponseReader(reader)
Expand Down
4 changes: 3 additions & 1 deletion src/commercelayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,15 +283,17 @@ class CommerceLayerClient {
}


addRawResponseReader(): RawResponseReader {
addRawResponseReader(options?: { headers: boolean }): RawResponseReader {

const reader: RawResponseReader = {
id: undefined,
rawResponse: undefined,
headers: undefined,
}

function rawResponseInterceptor(response: ResponseObj): ResponseObj {
reader.rawResponse = response?.data
if (options?.headers) reader.headers = response.headers
return response
}

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type { QueryParamsRetrieve, QueryParamsList, QueryParams } from './query'
export * from './model'

// Raw response reader and request/response interceptors
export type { RequestObj, ResponseObj, ErrorObj } from './interceptor'
export type { RequestObj, ResponseObj, ErrorObj, HeadersObj } from './interceptor'

// Error types
export type { SdkError, ApiError, ErrorType } from './error'
7 changes: 5 additions & 2 deletions src/interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { AxiosError, AxiosInterceptorManager, AxiosRequestConfig, AxiosResponse } from "axios"
import { AxiosError, AxiosInterceptorManager, AxiosRequestConfig, AxiosResponse, AxiosResponseHeaders } from "axios"


type InterceptorManager = {
Expand All @@ -14,20 +14,23 @@ type RequestInterceptor = (request: RequestObj) => RequestObj | Promise<RequestO
type ResponseObj = AxiosResponse
type ResponseInterceptor = (response: ResponseObj) => ResponseObj

type HeadersObj = AxiosResponseHeaders

type ErrorObj = AxiosError
type ErrorInterceptor = (error: ErrorObj) => ErrorObj

type InterceptorType = 'request' | 'response'


export type { InterceptorManager, RequestInterceptor, ResponseInterceptor, ErrorInterceptor, InterceptorType }
export type { RequestObj, ResponseObj, ErrorObj }
export type { RequestObj, ResponseObj, ErrorObj, HeadersObj }



type RawResponseReader = {
id: number | undefined;
rawResponse: ResponseObj | undefined;
headers: HeadersObj | undefined;
}


Expand Down
15 changes: 7 additions & 8 deletions test/spot.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
/* eslint-disable no-console */
// import commercelayer from '../lib/cjs'
import commercelayer from '../src'
import getToken from './token'


(async () => {

const organization = process.env.CL_SDK_ORGANIZATION || ''
const accessToken = process.env.CL_SDK_ACCESS_TOKEN || ''
const auth = await getToken('integration')
const accessToken = auth? auth.accessToken : ''

const cl = commercelayer({
organization,
accessToken,
timeout: 5000,
})

const customer = await cl.customers.retrieve('OZqohRjoWn')
const rrr = cl.addRawResponseReader({ headers: true })

const id = customer
const customers = await cl.customers.list({ pageSize: 1 })

const cg = await cl.customers.customer_group(id)

console.log(cg)

if (!cg) console.log('No response!')
console.log(rrr.rawResponse)
console.log(rrr.headers)

})()

0 comments on commit 9bff319

Please sign in to comment.