MCPcopy
hub / github.com/vueuse/vueuse / execute

Function execute

packages/core/useFetch/index.ts:419–540  ·  view source on GitHub ↗
(throwOnFailed = false)

Source from the content-addressed store, hash-verified

417 let executeCounter = 0
418
419 const execute = async (throwOnFailed = false) => {
420 abort()
421
422 loading(true)
423 error.value = null
424 statusCode.value = null
425 aborted.value = false
426
427 executeCounter += 1
428 const currentExecuteCounter = executeCounter
429
430 const defaultFetchOptions: RequestInit = {
431 method: config.method,
432 headers: {},
433 }
434
435 const payload = toValue(config.payload)
436 if (payload) {
437 const headers = headersToObject(defaultFetchOptions.headers) as Record<string, string>
438 // Set the payload to json type only if it's not provided and a literal object or array is provided and the object is not `formData`
439 // The only case we can deduce the content type and `fetch` can't
440 const proto = Object.getPrototypeOf(payload)
441 if (!config.payloadType && payload && (proto === Object.prototype || Array.isArray(proto)) && !(payload instanceof FormData))
442 config.payloadType = 'json'
443
444 if (config.payloadType)
445 headers['Content-Type'] = payloadMapping[config.payloadType] ?? config.payloadType
446
447 defaultFetchOptions.body = config.payloadType === 'json'
448 ? JSON.stringify(payload)
449 : payload as BodyInit
450 }
451
452 let isCanceled = false
453 const context: BeforeFetchContext = {
454 url: toValue(url),
455 options: {
456 ...defaultFetchOptions,
457 ...fetchOptions,
458 },
459 cancel: () => { isCanceled = true },
460 }
461
462 if (options.beforeFetch)
463 Object.assign(context, await options.beforeFetch(context))
464
465 if (isCanceled || !fetch) {
466 loading(false)
467 return Promise.resolve(null)
468 }
469
470 let responseData: any = null
471
472 if (timer)
473 timer.start()
474
475 return fetch(
476 context.url,

Callers 3

index.test.tsFile · 0.70
useFetchFunction · 0.70
setMethodFunction · 0.70

Calls 3

headersToObjectFunction · 0.85
abortFunction · 0.70
loadingFunction · 0.70

Tested by

no test coverage detected