Skip to content

Commit

Permalink
fix: use _data rather than data to store deserialized response (#49)
Browse files Browse the repository at this point in the history
* fix: use `_data` rather than `data` to store deserialized response

* fix: update type instead
  • Loading branch information
danielroe authored Jan 18, 2022
1 parent 2d59a8d commit babb331
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function createFetchError<T = any> (request: FetchRequest, error?: Error,

Object.defineProperty(fetchError, 'request', { get () { return request } })
Object.defineProperty(fetchError, 'response', { get () { return response } })
Object.defineProperty(fetchError, 'data', { get () { return response && response.data } })
Object.defineProperty(fetchError, 'data', { get () { return response && response._data } })

return fetchError
}
8 changes: 4 additions & 4 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface CreateFetchOptions {
}

export type FetchRequest = RequestInfo
export interface FetchResponse<T> extends Response { data?: T }
export interface FetchResponse<T> extends Response { _data?: T }
export interface SearchParams { [key: string]: any }

export interface FetchContext<T = any, R extends ResponseType = ResponseType> {
Expand Down Expand Up @@ -134,9 +134,9 @@ export function createFetch (globalOptions: CreateFetchOptions): $Fetch {
if (responseType === 'json') {
const data = await ctx.response.text()
const parseFn = ctx.options.parseResponse || destr
ctx.response.data = parseFn(data)
ctx.response._data = parseFn(data)
} else {
ctx.response.data = await ctx.response[responseType]()
ctx.response._data = await ctx.response[responseType]()
}

if (ctx.options.onResponse) {
Expand All @@ -153,7 +153,7 @@ export function createFetch (globalOptions: CreateFetchOptions): $Fetch {
}

const $fetch = function $fetch (request, opts) {
return $fetchRaw(request, opts).then(r => r.data)
return $fetchRaw(request, opts).then(r => r._data)
} as $Fetch

$fetch.raw = $fetchRaw
Expand Down
2 changes: 1 addition & 1 deletion test/index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('ohmyfetch', () => {
const err = await $fetch(getURL('404')).catch(err => err)
expect(err.toString()).to.contain('404 Not Found')
expect(err.data).to.deep.eq({ stack: [], statusCode: 404, statusMessage: 'Not Found' })
expect(err.response?.data).to.deep.eq(err.data)
expect(err.response?._data).to.deep.eq(err.data)
expect(err.request).to.equal(getURL('404'))
})

Expand Down

0 comments on commit babb331

Please sign in to comment.