UNPKG

@aws-amplify/core

Version:
56 lines (53 loc) 1.95 kB
import { AmplifyError } from '../../errors/AmplifyError.mjs'; import { AmplifyErrorCode } from '../../types/errors.mjs'; import '../../errors/errorHelpers.mjs'; import { withMemoization } from '../utils/memoization.mjs'; // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 const shouldSendBody = (method) => !['HEAD', 'GET', 'DELETE'].includes(method.toUpperCase()); // TODO[AllanZhengYP]: we need to provide isCanceledError utility const fetchTransferHandler = async ({ url, method, headers, body }, { abortSignal, cache, withCrossDomainCredentials }) => { let resp; try { resp = await fetch(url, { method, headers, body: shouldSendBody(method) ? body : undefined, signal: abortSignal, cache, credentials: withCrossDomainCredentials ? 'include' : 'same-origin', }); } catch (e) { if (e instanceof TypeError) { throw new AmplifyError({ name: AmplifyErrorCode.NetworkError, message: 'A network error has occurred.', underlyingError: e, }); } throw e; } const responseHeaders = {}; resp.headers?.forEach((value, key) => { responseHeaders[key.toLowerCase()] = value; }); const httpResponse = { statusCode: resp.status, headers: responseHeaders, body: null, }; // resp.body is a ReadableStream according to Fetch API spec, but React Native // does not implement it. const bodyWithMixin = Object.assign(resp.body ?? {}, { text: withMemoization(() => resp.text()), blob: withMemoization(() => resp.blob()), json: withMemoization(() => resp.json()), }); return { ...httpResponse, body: bodyWithMixin, }; }; export { fetchTransferHandler }; //# sourceMappingURL=fetch.mjs.map