Download node_modules/@supabase/auth-js/dist/module/lib/fetch.js from metarm/QMetaRam: direct link, hf CLI and curl.
- Browser
- Download file 8.66 kB
-
https://huggingface.co/metarm/QMetaRam/resolve/main/node_modules/%40supabase/auth-js/dist/module/lib/fetch.js
- Command line
-
hf download hf://metarm/QMetaRam/node_modules/@supabase/auth-js/dist/module/lib/fetch.js
-
curl -L -o fetch.js https://huggingface.co/metarm/QMetaRam/resolve/main/node_modules/%40supabase/auth-js/dist/module/lib/fetch.js
8.66 kB
| import { __rest } from "tslib"; | |
| import { API_VERSIONS, API_VERSION_HEADER_NAME } from './constants'; | |
| import { expiresAt, looksLikeFetchResponse, parseResponseAPIVersion } from './helpers'; | |
| import { AuthApiError, AuthRetryableFetchError, AuthWeakPasswordError, AuthUnknownError, AuthSessionMissingError, } from './errors'; | |
| const _getErrorMessage = (err) => { | |
| if (typeof err === 'object' && err !== null) { | |
| const e = err; | |
| if (typeof e.msg === 'string') | |
| return e.msg; | |
| if (typeof e.message === 'string') | |
| return e.message; | |
| if (typeof e.error_description === 'string') | |
| return e.error_description; | |
| if (typeof e.error === 'string') | |
| return e.error; | |
| } | |
| return JSON.stringify(err); | |
| }; | |
| // 500, 501, 502, 503, 504: Standard server/gateway errors | |
| // 520-529, 530: Cloudflare-specific error codes (web server down, connection timed out, etc.) | |
| // These are infrastructure errors and should not cause session invalidation. | |
| const NETWORK_ERROR_CODES = [ | |
| 500, 501, 502, 503, 504, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 530, | |
| ]; | |
| export async function handleError(error) { | |
| var _a; | |
| if (!looksLikeFetchResponse(error)) { | |
| throw new AuthRetryableFetchError(_getErrorMessage(error), 0); | |
| } | |
| let data; | |
| try { | |
| data = await error.json(); | |
| } | |
| catch (e) { | |
| if (NETWORK_ERROR_CODES.includes(error.status)) { | |
| // statusText can be empty — HTTP/2 has no reason phrase | |
| throw new AuthRetryableFetchError(error.statusText || `HTTP ${error.status}`, error.status); | |
| } | |
| throw new AuthUnknownError(_getErrorMessage(e), e); | |
| } | |
| if (NETWORK_ERROR_CODES.includes(error.status)) { | |
| // status in 500...599 range - server had an error, request might be retryed. | |
| throw new AuthRetryableFetchError(_getErrorMessage(data), error.status); | |
| } | |
| let errorCode = undefined; | |
| const responseAPIVersion = parseResponseAPIVersion(error); | |
| if (responseAPIVersion && | |
| responseAPIVersion.getTime() >= API_VERSIONS['2024-01-01'].timestamp && | |
| typeof data === 'object' && | |
| data && | |
| typeof data.code === 'string') { | |
| errorCode = data.code; | |
| } | |
| else if (typeof data === 'object' && data && typeof data.error_code === 'string') { | |
| errorCode = data.error_code; | |
| } | |
| if (!errorCode) { | |
| // Legacy support for weak password errors, when there were no error codes | |
| if (typeof data === 'object' && | |
| data && | |
| typeof data.weak_password === 'object' && | |
| data.weak_password && | |
| Array.isArray(data.weak_password.reasons) && | |
| data.weak_password.reasons.length && | |
| data.weak_password.reasons.reduce((a, i) => a && typeof i === 'string', true)) { | |
| throw new AuthWeakPasswordError(_getErrorMessage(data), error.status, data.weak_password.reasons); | |
| } | |
| } | |
| else if (errorCode === 'weak_password') { | |
| throw new AuthWeakPasswordError(_getErrorMessage(data), error.status, ((_a = data.weak_password) === null || _a === void 0 ? void 0 : _a.reasons) || []); | |
| } | |
| else if (errorCode === 'session_not_found') { | |
| // The `session_id` inside the JWT does not correspond to a row in the | |
| // `sessions` table. This usually means the user has signed out, has been | |
| // deleted, or their session has somehow been terminated. | |
| throw new AuthSessionMissingError(); | |
| } | |
| throw new AuthApiError(_getErrorMessage(data), error.status || 500, errorCode); | |
| } | |
| const _getRequestParams = (method, options, parameters, body) => { | |
| const params = { method, headers: (options === null || options === void 0 ? void 0 : options.headers) || {} }; | |
| if (method === 'GET') { | |
| return params; | |
| } | |
| params.headers = Object.assign({ 'Content-Type': 'application/json;charset=UTF-8' }, options === null || options === void 0 ? void 0 : options.headers); | |
| params.body = JSON.stringify(body); | |
| return Object.assign(Object.assign({}, params), parameters); | |
| }; | |
| export async function _request(fetcher, method, url, options) { | |
| var _a; | |
| const headers = Object.assign({}, options === null || options === void 0 ? void 0 : options.headers); | |
| if (!headers[API_VERSION_HEADER_NAME]) { | |
| headers[API_VERSION_HEADER_NAME] = API_VERSIONS['2024-01-01'].name; | |
| } | |
| if (options === null || options === void 0 ? void 0 : options.jwt) { | |
| headers['Authorization'] = `Bearer ${options.jwt}`; | |
| } | |
| const qs = (_a = options === null || options === void 0 ? void 0 : options.query) !== null && _a !== void 0 ? _a : {}; | |
| if (options === null || options === void 0 ? void 0 : options.redirectTo) { | |
| qs['redirect_to'] = options.redirectTo; | |
| } | |
| const queryString = Object.keys(qs).length ? '?' + new URLSearchParams(qs).toString() : ''; | |
| const data = await _handleRequest(fetcher, method, url + queryString, { | |
| headers, | |
| noResolveJson: options === null || options === void 0 ? void 0 : options.noResolveJson, | |
| }, {}, options === null || options === void 0 ? void 0 : options.body); | |
| return (options === null || options === void 0 ? void 0 : options.xform) ? options === null || options === void 0 ? void 0 : options.xform(data) : { data: Object.assign({}, data), error: null }; | |
| } | |
| async function _handleRequest(fetcher, method, url, options, parameters, body) { | |
| const requestParams = _getRequestParams(method, options, parameters, body); | |
| let result; | |
| try { | |
| result = await fetcher(url, Object.assign({}, requestParams)); | |
| } | |
| catch (e) { | |
| // fetch failed (network / CORS / aborted request) — surfaced to the | |
| // caller as a retryable error below. Deliberately not logged here: an | |
| // aborted in-flight request (e.g. a superseded page navigation) is a | |
| // transient condition, and logging the raw error pollutes the console. | |
| throw new AuthRetryableFetchError(_getErrorMessage(e), 0); | |
| } | |
| if (!result.ok) { | |
| await handleError(result); | |
| } | |
| if (options === null || options === void 0 ? void 0 : options.noResolveJson) { | |
| return result; | |
| } | |
| try { | |
| return await result.json(); | |
| } | |
| catch (e) { | |
| await handleError(e); | |
| } | |
| } | |
| export function _sessionResponse(data) { | |
| var _a; | |
| let session = null; | |
| if (hasSession(data)) { | |
| session = Object.assign({}, data); | |
| if (!data.expires_at) { | |
| session.expires_at = expiresAt(data.expires_in); | |
| } | |
| } | |
| // Some /verify responses (e.g. secure email_change first-confirmation) return | |
| // only `{ msg, code }` with no user and no session. Treat those as null user. | |
| const user = (_a = data.user) !== null && _a !== void 0 ? _a : (typeof (data === null || data === void 0 ? void 0 : data.id) === 'string' ? data : null); | |
| return { data: { session, user }, error: null }; | |
| } | |
| export function _sessionResponsePassword(data) { | |
| const response = _sessionResponse(data); | |
| if (!response.error && | |
| data.weak_password && | |
| typeof data.weak_password === 'object' && | |
| Array.isArray(data.weak_password.reasons) && | |
| data.weak_password.reasons.length && | |
| data.weak_password.message && | |
| typeof data.weak_password.message === 'string' && | |
| data.weak_password.reasons.reduce((a, i) => a && typeof i === 'string', true)) { | |
| response.data.weak_password = data.weak_password; | |
| } | |
| return response; | |
| } | |
| export function _userResponse(data) { | |
| var _a; | |
| const user = (_a = data.user) !== null && _a !== void 0 ? _a : data; | |
| return { data: { user }, error: null }; | |
| } | |
| export function _ssoResponse(data) { | |
| return { data, error: null }; | |
| } | |
| export function _generateLinkResponse(data) { | |
| const { action_link, email_otp, hashed_token, redirect_to, verification_type } = data, rest = __rest(data, ["action_link", "email_otp", "hashed_token", "redirect_to", "verification_type"]); | |
| const properties = { | |
| action_link, | |
| email_otp, | |
| hashed_token, | |
| redirect_to, | |
| verification_type, | |
| }; | |
| const user = Object.assign({}, rest); | |
| return { | |
| data: { | |
| properties, | |
| user, | |
| }, | |
| error: null, | |
| }; | |
| } | |
| export function _noResolveJsonResponse(data) { | |
| return data; | |
| } | |
| /** | |
| * hasSession checks if the response object contains a valid session | |
| * @param data A response object | |
| * @returns true if a session is in the response | |
| */ | |
| function hasSession(data) { | |
| return !!data.access_token && !!data.refresh_token && !!data.expires_in; | |
| } | |
| //# sourceMappingURL=fetch.js.map |