import { fetch as nodeFetch } from 'cross-fetch' ; import { QueryError } from './QueryError' ; import { FetchResultTypes } from './types' ; param url param optionsOrType Either the [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) ( RequestInit for TypeScript) or one of the  FetchResultTypes param type Only needs to be provided if the second parameter are [Request options](https://developer.mozilla.org/en-US/docs/Web/API/Request) ( RequestInit for TypeScript). One of the  FetchResultTypes that will determine how the result is returned.
 *  returns The return type is determined by the provided `type`.
 * - When using `FetchResultTypes.JSON` then the return type is `unknown` by default. The type should be specified by filling in the generic type of the function, or casting the result.
 * - When using `FetchResultTypes.Buffer` the return type will be [`Buffer`](https://nodejs.org/api/buffer.html).
 * - When using `FetchResultTypes.Blob` the return type will be a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
 * - When using `FetchResultTypes.Text` the return type will be a `string`
 * - When using `FetchResultTypes.Result` the return type will be a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) ( Response in typescript)
  export async function fetch < R > ( url , type ? ) ; export async function fetch < R > ( url , options , type ? ) ; export async function fetch ( url , type ) ; export async function fetch ( url , options , type ) ; export async function fetch ( url , type ) ; export async function fetch ( url , options , type ) ; export async function fetch ( url , type ) ; export async function fetch ( url , options , type ) ; export async function fetch ( url , type ) ; export async function fetch ( url , options , type ) ; export async function fetch < R > ( url , options , type ) ; export async function fetch ( url , options ? , type ? ) { if ( typeof options === 'undefined' ) { options = { } ; type = FetchResultTypes . JSON ; } else if ( typeof options === 'string' ) { type = options ; options = { } ; } else if ( typeof type === 'undefined' ) { type = FetchResultTypes . JSON ; } const stringUrl = String ( url ) ; const result = await nodeFetch ( stringUrl , options ) ; if ( ! result . ok ) throw new QueryError ( stringUrl , result . status , result , await result . clone ( ) . text ( ) ) ; switch ( type ) { case FetchResultTypes . Result : return result ; case FetchResultTypes . Buffer : return Buffer . from ( await ( await result . blob ( ) ) . arrayBuffer ( ) ) ; case FetchResultTypes . Blob : return result . blob ( ) ; case FetchResultTypes . JSON : return result . json ( ) ; case FetchResultTypes . Text : return result . text ( ) ; default : throw new Error ( `Unknown type "${ type }"` ) ; } }