功能:同步小程序流程
This commit is contained in:
@@ -1,21 +1,73 @@
|
||||
/**
|
||||
* Purpose: auth supports request.js by holding or executing the auth page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
const auth = require('./auth')
|
||||
/**
|
||||
* Purpose: actionGuard supports request.js by holding or executing the action guard page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
const actionGuard = require('./action-guard')
|
||||
/**
|
||||
* Purpose: idleSession supports request.js by holding or executing the idle session page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
const idleSession = require('./idle-session')
|
||||
const { API_BASE_URL, REQUEST_TIMEOUT, ENABLE_REQUEST_SIGNING } = require('./config')
|
||||
const { getDeviceId } = require('./device')
|
||||
const { sha256Hex, hmacSha256Hex, buildCanonicalParams, buildJsonPayload, randomNonce } = require('./crypto')
|
||||
|
||||
/**
|
||||
* Purpose: HEADER_TIMESTAMP supports request.js by holding or executing the header timestamp page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
const HEADER_TIMESTAMP = 'X-Timestamp'
|
||||
/**
|
||||
* Purpose: HEADER_NONCE supports request.js by holding or executing the header nonce page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
const HEADER_NONCE = 'X-Nonce'
|
||||
/**
|
||||
* Purpose: HEADER_SIGNATURE supports request.js by holding or executing the header signature page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
const HEADER_SIGNATURE = 'X-Signature'
|
||||
/**
|
||||
* Purpose: HEADER_SIGNATURE_VERSION supports request.js by holding or executing the header signature version page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
const HEADER_SIGNATURE_VERSION = 'X-Sign-Version'
|
||||
/**
|
||||
* Purpose: DEVICE_ID_HEADER supports request.js by holding or executing the device id header page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
const DEVICE_ID_HEADER = 'X-Device-Id'
|
||||
|
||||
/**
|
||||
* Purpose: isRefreshing supports request.js by holding or executing the is refreshing page or API role.
|
||||
* Usage: import, call, read, or update this let from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
let isRefreshing = false
|
||||
/**
|
||||
* Purpose: requestQueue supports request.js by holding or executing the request queue page or API role.
|
||||
* Usage: import, call, read, or update this let from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
let requestQueue = []
|
||||
/**
|
||||
* Purpose: redirectingToAuth supports request.js by holding or executing the redirecting to auth page or API role.
|
||||
* Usage: import, call, read, or update this let from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
let redirectingToAuth = false
|
||||
/**
|
||||
* Purpose: inflightRequests supports request.js by holding or executing the inflight requests page or API role.
|
||||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
const inflightRequests = {}
|
||||
|
||||
/**
|
||||
* Purpose: buildRequestUrl supports request.js by holding or executing the build request url page or API role.
|
||||
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
function buildRequestUrl(url, params) {
|
||||
const query = Object.keys(params || {})
|
||||
.filter(key => params[key] !== undefined && params[key] !== null && params[key] !== '')
|
||||
@@ -26,6 +78,10 @@ function buildRequestUrl(url, params) {
|
||||
return query ? `${target}${target.includes('?') ? '&' : '?'}${query}` : target
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: isWhitelistedPath supports request.js by holding or executing the is whitelisted path page or API role.
|
||||
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
function isWhitelistedPath(url) {
|
||||
return [
|
||||
'/user/login',
|
||||
@@ -48,6 +104,10 @@ function isWhitelistedPath(url) {
|
||||
].some(path => String(url || '').includes(path))
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: normalizeError supports request.js by holding or executing the normalize error page or API role.
|
||||
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
function normalizeError(message, statusCode, raw) {
|
||||
const error = new Error(message || '\u8bf7\u6c42\u5931\u8d25')
|
||||
error.statusCode = statusCode
|
||||
@@ -55,6 +115,10 @@ function normalizeError(message, statusCode, raw) {
|
||||
return error
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: rawRequest supports request.js by holding or executing the raw request page or API role.
|
||||
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
function rawRequest(options) {
|
||||
idleSession.markActive()
|
||||
return new Promise((resolve, reject) => {
|
||||
@@ -66,6 +130,10 @@ function rawRequest(options) {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: queueRequest supports request.js by holding or executing the queue request page or API role.
|
||||
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
function queueRequest(retry) {
|
||||
return new Promise((resolve, reject) => {
|
||||
requestQueue.push({
|
||||
@@ -76,6 +144,10 @@ function queueRequest(retry) {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: flushQueue supports request.js by holding or executing the flush queue page or API role.
|
||||
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
function flushQueue(error) {
|
||||
const queue = requestQueue.slice()
|
||||
requestQueue = []
|
||||
@@ -89,6 +161,10 @@ function flushQueue(error) {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: redirectToAuth supports request.js by holding or executing the redirect to auth page or API role.
|
||||
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
function redirectToAuth() {
|
||||
if (redirectingToAuth) {
|
||||
return
|
||||
@@ -119,6 +195,10 @@ function redirectToAuth() {
|
||||
}, 300)
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: buildSignatureHeaders supports request.js by holding or executing the build signature headers page or API role.
|
||||
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
function buildSignatureHeaders(options) {
|
||||
const headers = {
|
||||
[DEVICE_ID_HEADER]: getDeviceId()
|
||||
@@ -156,6 +236,10 @@ function buildSignatureHeaders(options) {
|
||||
return headers
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: refreshToken supports request.js by holding or executing the refresh token page or API role.
|
||||
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
async function refreshToken() {
|
||||
const refreshTokenValue = auth.getRefreshToken()
|
||||
if (!refreshTokenValue) {
|
||||
@@ -191,6 +275,10 @@ async function refreshToken() {
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: handleUnauthorized supports request.js by holding or executing the handle unauthorized page or API role.
|
||||
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
async function handleUnauthorized(options) {
|
||||
if (isWhitelistedPath(options.url)) {
|
||||
throw normalizeError('\u8bf7\u6c42\u9700\u8981\u767b\u5f55\uff0c\u4f46\u5f53\u524d\u9875\u9762\u5141\u8bb8\u533f\u540d\u67e5\u770b', 401)
|
||||
@@ -235,6 +323,10 @@ async function handleUnauthorized(options) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: unwrapResponse supports request.js by holding or executing the unwrap response page or API role.
|
||||
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
async function unwrapResponse(res, options) {
|
||||
idleSession.markActive()
|
||||
|
||||
@@ -261,6 +353,10 @@ async function unwrapResponse(res, options) {
|
||||
return payload
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: buildDedupeKey supports request.js by holding or executing the build dedupe key page or API role.
|
||||
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
function buildDedupeKey(options, method) {
|
||||
if (options.skipDedupe) {
|
||||
return ''
|
||||
@@ -274,10 +370,18 @@ function buildDedupeKey(options, method) {
|
||||
return ''
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: isSafeMethod supports request.js by holding or executing the is safe method page or API role.
|
||||
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
function isSafeMethod(method) {
|
||||
return ['GET', 'HEAD', 'OPTIONS'].includes(method)
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: buildActionFingerprint supports request.js by holding or executing the build action fingerprint page or API role.
|
||||
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
function buildActionFingerprint(options, method) {
|
||||
const paramsText = buildCanonicalParams(options.params)
|
||||
const payloadText = isSafeMethod(method) ? '' : buildJsonPayload(options.data)
|
||||
@@ -288,6 +392,10 @@ function buildActionFingerprint(options, method) {
|
||||
].join('\n'))
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: resolveRepeatMessage supports request.js by holding or executing the resolve repeat message page or API role.
|
||||
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
function resolveRepeatMessage(url) {
|
||||
const path = String(url || '')
|
||||
if (
|
||||
@@ -320,6 +428,10 @@ function resolveRepeatMessage(url) {
|
||||
return '\u64cd\u4f5c\u8fdb\u884c\u4e2d\uff0c\u8bf7\u52ff\u91cd\u590d\u70b9\u51fb'
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: buildActionGuardConfig supports request.js by holding or executing the build action guard config page or API role.
|
||||
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
function buildActionGuardConfig(options, method) {
|
||||
if (options.skipActionGuard || isSafeMethod(method)) {
|
||||
return null
|
||||
@@ -332,6 +444,10 @@ function buildActionGuardConfig(options, method) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Purpose: request supports request.js by holding or executing the request page or API role.
|
||||
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
|
||||
*/
|
||||
function request(options) {
|
||||
const method = String(options.method || 'GET').toUpperCase()
|
||||
const dedupeKey = buildDedupeKey(options, method)
|
||||
|
||||
Reference in New Issue
Block a user