попытка сделать нормальный интерцептор
This commit is contained in:
parent
b75e523a4f
commit
34da06301b
@ -1,37 +1,38 @@
|
|||||||
import { Api } from '~/api/Api'
|
import { Api } from '~/api/Api'
|
||||||
|
|
||||||
|
const requestUrl = 'https://wp.koptilnya.xyz/wp-json'
|
||||||
|
const consumerKey = 'ck_8b5477a1573ce6038ef1367f25d95cede1de4559'
|
||||||
|
const consumerSecret = 'cs_d0ccaa93e8efe4f76eef0b70c9828a58fc53459f'
|
||||||
|
const authString = `${consumerKey}:${consumerSecret}`
|
||||||
|
const encodedAuth = btoa(unescape(encodeURIComponent(authString)))
|
||||||
|
|
||||||
const api = new Api({
|
const api = new Api({
|
||||||
baseUrl: String('https://wp.koptilnya.xyz/wp-json'),
|
baseUrl: String(requestUrl),
|
||||||
|
})
|
||||||
|
|
||||||
|
Object.keys(api.wc).forEach((methodName) => {
|
||||||
|
const nativeMethod = api.wc[methodName] as typeof Function
|
||||||
|
const argsNumber = nativeMethod.length
|
||||||
|
api.wc[methodName] = function (...args) {
|
||||||
|
let params: Record<string, unknown> = {}
|
||||||
|
if (args.length > argsNumber) {
|
||||||
|
params = args.pop()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nativeMethod(...args, {
|
||||||
|
...params,
|
||||||
|
headers: {
|
||||||
|
...(params.headers as Record<string, string> ?? {}),
|
||||||
|
Authorization: `Basic ${encodedAuth}`,
|
||||||
|
},
|
||||||
|
} as unknown as string)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const nativeRequest = api.request
|
const nativeRequest = api.request
|
||||||
|
|
||||||
const consumerKey = 'ck_8b5477a1573ce6038ef1367f25d95cede1de4559'
|
|
||||||
const consumerSecret = 'cs_d0ccaa93e8efe4f76eef0b70c9828a58fc53459f'
|
|
||||||
|
|
||||||
const authString = `${consumerKey}:${consumerSecret}`
|
|
||||||
const encodedAuth = btoa(authString) // Кодируем в Base64 (браузерный метод)
|
|
||||||
|
|
||||||
api.request = async function (...args) {
|
api.request = async function (...args) {
|
||||||
const [url, options = {}] = args
|
const response = await nativeRequest.call(api, ...args)
|
||||||
|
|
||||||
const headers = {
|
|
||||||
...options.headers,
|
|
||||||
'Authorization': `Basic ${encodedAuth}`,
|
|
||||||
'Content-Type': 'application/json', // Явно указываем JSON
|
|
||||||
}
|
|
||||||
|
|
||||||
// Убираем credentials: "omit" (мешает передаче заголовков)
|
|
||||||
const fetchOptions = {
|
|
||||||
...options,
|
|
||||||
headers,
|
|
||||||
credentials: 'same-origin', // или "include" для кросс-доменных запросов
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('Отправляемые заголовки:', headers)
|
|
||||||
console.log('fetchOptions', fetchOptions)
|
|
||||||
|
|
||||||
const response = await nativeRequest.call(api, url, fetchOptions)
|
|
||||||
return await response.json()
|
return await response.json()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -19,7 +19,6 @@
|
|||||||
"@nuxt/ui": "3.2.0",
|
"@nuxt/ui": "3.2.0",
|
||||||
"@tanstack/vue-query": "^5.75.5",
|
"@tanstack/vue-query": "^5.75.5",
|
||||||
"@vueuse/core": "^13.1.0",
|
"@vueuse/core": "^13.1.0",
|
||||||
"axios": "^1.10.0",
|
|
||||||
"dayjs": "^1.11.13",
|
"dayjs": "^1.11.13",
|
||||||
"decimal.js": "^10.5.0",
|
"decimal.js": "^10.5.0",
|
||||||
"nuxt": "^3.17.6",
|
"nuxt": "^3.17.6",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user