@aws-amplify/core
Version:
Core category of aws-amplify
54 lines (52 loc) • 2.14 kB
JavaScript
;
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchTransferHandler = void 0;
const memoization_1 = require("../utils/memoization");
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) {
// TODO: needs to revise error handling in v6
// For now this is a thin wrapper over original fetch error similar to cognito-identity-js package.
// Ref: https://github.com/aws-amplify/amplify-js/blob/4fbc8c0a2be7526aab723579b4c95b552195a80b/packages/amazon-cognito-identity-js/src/Client.js#L103-L108
if (e instanceof TypeError) {
throw new Error('Network error');
}
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: (0, memoization_1.withMemoization)(() => resp.text()),
blob: (0, memoization_1.withMemoization)(() => resp.blob()),
json: (0, memoization_1.withMemoization)(() => resp.json()),
});
return {
...httpResponse,
body: bodyWithMixin,
};
};
exports.fetchTransferHandler = fetchTransferHandler;
//# sourceMappingURL=fetch.js.map