Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only apply response interceptor transformations to JSON responses #79

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: If we do an early return we can avoid indenting all that code

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 }),
}
}
return response
}
this.client.interceptors.response.use(
camelCaseResponse,
(error: ExtendedAxiosError): Promise<ExtendedAxiosError> => {
Expand Down
68 changes: 40 additions & 28 deletions test/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -781,20 +781,26 @@ 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 }),
})
const camelCaseResponse = (response: AxiosResponse): ExtendedAxiosResponse => {
const contentType = response.headers['content-type']
if (contentType && contentType.includes('application/json')) {
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 }),
}
}
return response
}
this.client.interceptors.response.use(
camelCaseResponse,
(error: ExtendedAxiosError): Promise<ExtendedAxiosError> => {
Expand Down Expand Up @@ -5439,20 +5445,26 @@ 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 }),
})
const camelCaseResponse = (response: AxiosResponse): ExtendedAxiosResponse => {
const contentType = response.headers['content-type']
if (contentType && contentType.includes('application/json')) {
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 }),
}
}
return response
}
this.client.interceptors.response.use(
camelCaseResponse,
(error: ExtendedAxiosError): Promise<ExtendedAxiosError> => {
Expand Down
Loading