37 lines
1.1 KiB
TypeScript
37 lines
1.1 KiB
TypeScript
import dayjs from 'dayjs'
|
|
import { ofetch } from 'ofetch'
|
|
import { $backendUrl, $requestRetries, $requestRetryCodes } from '../../../../configs/config.api'
|
|
import { $tokenExpiration, $tokenName } from '../../../../configs/config.security'
|
|
import { encryptRsa } from '../../scripts'
|
|
|
|
const URL = $backendUrl.get()
|
|
const TOKEN_NAME = $tokenName.get()
|
|
const TOKEN_EXPIRATION = $tokenExpiration.get()
|
|
const RETRY = $requestRetries.get()
|
|
const CODES = $requestRetryCodes.get()
|
|
|
|
export default async (api: string, body: Object) => {
|
|
const today = new Date()
|
|
const todayUnix = dayjs(today).unix()
|
|
const expiration = todayUnix + TOKEN_EXPIRATION
|
|
const aes = await encryptRsa(`${api.toString()}-${todayUnix.toString()}-${expiration.toString()}`)
|
|
|
|
const hash = `${TOKEN_NAME}=${aes}token`
|
|
try {
|
|
await ofetch(URL + api, {
|
|
headers: {
|
|
Accept: 'application/json',
|
|
'Cache-Control': 'no-cache',
|
|
'Dasig-Token': hash,
|
|
},
|
|
retry: RETRY,
|
|
retryDelay: 500,
|
|
retryStatusCodes: CODES,
|
|
method: 'POST',
|
|
body: body,
|
|
})
|
|
return true
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|