-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support
$fetch.raw
and improve docs
- Loading branch information
Showing
6 changed files
with
83 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
export * from './types' | ||
export * from './fetch' | ||
export * from './error' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,43 @@ | ||
import destr from 'destr' | ||
import { joinURL } from '@nuxt/ufo' | ||
import type { Fetch, RequestInfo, RequestInit } from './types' | ||
import type { Fetch, RequestInfo, RequestInit, Response } from './types' | ||
import { createFetchError } from './error' | ||
|
||
export interface CreateFetchOptions { | ||
fetch: Fetch | ||
} | ||
export interface CreateFetchOptions { fetch: Fetch } | ||
|
||
export type $FetchInput = RequestInfo | ||
export type FetchRequest = RequestInfo | ||
|
||
export interface $FetchOptions extends RequestInit { | ||
export interface FetchOptions extends RequestInit { | ||
baseURL?: string | ||
response?: boolean | ||
} | ||
|
||
export type $Fetch<T = any> = (input: $FetchInput, opts?: $FetchOptions) => Promise<T> | ||
export interface FetchResponse<T> extends Response { data?: T } | ||
|
||
export interface $Fetch { | ||
<T = any>(request: FetchRequest, opts?: FetchOptions): Promise<T> | ||
raw<T = any>(request: FetchRequest, opts?: FetchOptions): Promise<FetchResponse<T>> | ||
} | ||
|
||
export function createFetch ({ fetch }: CreateFetchOptions): $Fetch { | ||
return async function $fetch (input, opts) { | ||
if (opts && opts.baseURL && typeof input === 'string') { | ||
input = joinURL(opts.baseURL, input) | ||
const raw: $Fetch['raw'] = async function (request, opts) { | ||
if (opts && opts.baseURL && typeof request === 'string') { | ||
request = joinURL(opts.baseURL, request) | ||
} | ||
const response = await fetch(input, opts) | ||
const response: FetchResponse<any> = await fetch(request, opts) | ||
const text = await response.text() | ||
const data = destr(text) | ||
response.data = destr(text) | ||
if (!response.ok) { | ||
throw createFetchError(response, input, data) | ||
throw createFetchError(request, response) | ||
} | ||
return data | ||
return response | ||
} | ||
|
||
const $fetch = function (request, opts) { | ||
return raw(request, opts).then(r => r.data) | ||
} as $Fetch | ||
|
||
$fetch.raw = raw | ||
|
||
return $fetch | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters