Skip to content

Commit

Permalink
Only apply response interceptor transformations to JSON responses (#79)
Browse files Browse the repository at this point in the history
If the response returns other types of content, for instance 'application/pdf' or 'image/png', applying the transformation is incorrect.
  • Loading branch information
rbruggem authored Dec 16, 2024
1 parent a081f52 commit bb3e42b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 45 deletions.
36 changes: 21 additions & 15 deletions src/templates/luneClient.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,27 @@ export class LuneClient {
}
this.client = axios.create()

// Convert to camelCase when receiving request
const camelCaseResponse = (response: AxiosResponse): ExtendedAxiosResponse => ({
...response,
_meta: {
...extractRequestFromResponseInterceptor(response),
response: response.data,
},
// SAFETY: The camelcase-keys type definitions are overly restrictive. The function
// handles all kinds of values just fine: arrays, numbers, strings, null etc.
//
// Instead of writing a bunch of type-detecting conditional code to satisfy the
// TS compiler let's just wholesale ignore this type mismatch – we don't know what
// value do we actually deal with here but the library will handle it.
data: camelCaseKeys(response.data, { deep: true }),
})
// Convert to camelCase when receiving JSON responses
const camelCaseResponse = (response: AxiosResponse): ExtendedAxiosResponse => {
const contentType = response.headers['content-type']
if (!contentType || !contentType.includes('application/json')) {
return response
}
return {
...response,
_meta: {
...extractRequestFromResponseInterceptor(response),
response: response.data,
},
// SAFETY: The camelcase-keys type definitions are overly restrictive. The function
// handles all kinds of values just fine: arrays, numbers, strings, null etc.
//
// Instead of writing a bunch of type-detecting conditional code to satisfy the
// TS compiler let's just wholesale ignore this type mismatch – we don't know what
// value do we actually deal with here but the library will handle it.
data: camelCaseKeys(response.data, { deep: true }),
}
}
this.client.interceptors.response.use(
camelCaseResponse,
(error: ExtendedAxiosError): Promise<ExtendedAxiosError> => {
Expand Down
72 changes: 42 additions & 30 deletions test/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -780,21 +780,27 @@ export class LuneClient {
}
this.client = axios.create()

// Convert to camelCase when receiving request
const camelCaseResponse = (response: AxiosResponse): ExtendedAxiosResponse => ({
...response,
_meta: {
...extractRequestFromResponseInterceptor(response),
response: response.data,
},
// SAFETY: The camelcase-keys type definitions are overly restrictive. The function
// handles all kinds of values just fine: arrays, numbers, strings, null etc.
//
// Instead of writing a bunch of type-detecting conditional code to satisfy the
// TS compiler let's just wholesale ignore this type mismatch – we don't know what
// value do we actually deal with here but the library will handle it.
data: camelCaseKeys(response.data, { deep: true }),
})
// Convert to camelCase when receiving JSON responses
const camelCaseResponse = (response: AxiosResponse): ExtendedAxiosResponse => {
const contentType = response.headers['content-type']
if (!contentType || !contentType.includes('application/json')) {
return response
}
return {
...response,
_meta: {
...extractRequestFromResponseInterceptor(response),
response: response.data,
},
// SAFETY: The camelcase-keys type definitions are overly restrictive. The function
// handles all kinds of values just fine: arrays, numbers, strings, null etc.
//
// Instead of writing a bunch of type-detecting conditional code to satisfy the
// TS compiler let's just wholesale ignore this type mismatch – we don't know what
// value do we actually deal with here but the library will handle it.
data: camelCaseKeys(response.data, { deep: true }),
}
}
this.client.interceptors.response.use(
camelCaseResponse,
(error: ExtendedAxiosError): Promise<ExtendedAxiosError> => {
Expand Down Expand Up @@ -5438,21 +5444,27 @@ export class LuneClient {
}
this.client = axios.create()

// Convert to camelCase when receiving request
const camelCaseResponse = (response: AxiosResponse): ExtendedAxiosResponse => ({
...response,
_meta: {
...extractRequestFromResponseInterceptor(response),
response: response.data,
},
// SAFETY: The camelcase-keys type definitions are overly restrictive. The function
// handles all kinds of values just fine: arrays, numbers, strings, null etc.
//
// Instead of writing a bunch of type-detecting conditional code to satisfy the
// TS compiler let's just wholesale ignore this type mismatch – we don't know what
// value do we actually deal with here but the library will handle it.
data: camelCaseKeys(response.data, { deep: true }),
})
// Convert to camelCase when receiving JSON responses
const camelCaseResponse = (response: AxiosResponse): ExtendedAxiosResponse => {
const contentType = response.headers['content-type']
if (!contentType || !contentType.includes('application/json')) {
return response
}
return {
...response,
_meta: {
...extractRequestFromResponseInterceptor(response),
response: response.data,
},
// SAFETY: The camelcase-keys type definitions are overly restrictive. The function
// handles all kinds of values just fine: arrays, numbers, strings, null etc.
//
// Instead of writing a bunch of type-detecting conditional code to satisfy the
// TS compiler let's just wholesale ignore this type mismatch – we don't know what
// value do we actually deal with here but the library will handle it.
data: camelCaseKeys(response.data, { deep: true }),
}
}
this.client.interceptors.response.use(
camelCaseResponse,
(error: ExtendedAxiosError): Promise<ExtendedAxiosError> => {
Expand Down

0 comments on commit bb3e42b

Please sign in to comment.