Compare commits
2 Commits
from-WP2-t
...
7baf2fbf0c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7baf2fbf0c | ||
|
|
0905a4662e |
@@ -12,6 +12,9 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
repository: 'git@git.koptilnya.xyz:alsaze/paxton-front.git'
|
||||||
|
ref: 'master'
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: |
|
run: |
|
||||||
|
|||||||
18764
api/Api.ts
18764
api/Api.ts
File diff suppressed because it is too large
Load Diff
4
api/endpoints/getProductList.ts
Normal file
4
api/endpoints/getProductList.ts
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
import api from '~/api/instance'
|
||||||
|
|
||||||
|
export const getProductList = () =>
|
||||||
|
api.wp.v2ProductList()
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
import api from '~/api/instance'
|
|
||||||
|
|
||||||
export const getProductsDetail = async (productId: number) =>
|
|
||||||
await api.wc.v1ProductsDetail(productId)
|
|
||||||
@@ -1 +1 @@
|
|||||||
export * from './getProductsDetail'
|
export * from './getProductList'
|
||||||
|
|||||||
@@ -1,77 +1,14 @@
|
|||||||
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)))
|
|
||||||
|
|
||||||
// Создаем инстанс API
|
|
||||||
const api = new Api({
|
const api = new Api({
|
||||||
baseUrl: requestUrl,
|
baseUrl: String('https://wp.koptilnya.xyz/wp-json'),
|
||||||
securityWorker: (securityData) => {
|
|
||||||
return {
|
|
||||||
headers: {
|
|
||||||
'Authorization': `Basic ${encodedAuth}`,
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Модифицируем методы API
|
|
||||||
Object.keys(api.wc).forEach((namespace) => {
|
|
||||||
const namespaceObj = api.wc[namespace]
|
|
||||||
|
|
||||||
Object.keys(namespaceObj).forEach((methodName) => {
|
|
||||||
const originalMethod = namespaceObj[methodName]
|
|
||||||
|
|
||||||
namespaceObj[methodName] = async function (...args: any[]) {
|
|
||||||
try {
|
|
||||||
// Добавляем заголовки аутентификации
|
|
||||||
const lastArg = args[args.length - 1]
|
|
||||||
const options = typeof lastArg === 'object' ? lastArg : {}
|
|
||||||
|
|
||||||
const modifiedOptions = {
|
|
||||||
...options,
|
|
||||||
headers: {
|
|
||||||
...(options.headers || {}),
|
|
||||||
Authorization: `Basic ${encodedAuth}`,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
const modifiedArgs = typeof lastArg === 'object'
|
|
||||||
? [...args.slice(0, -1), modifiedOptions]
|
|
||||||
: [...args, modifiedOptions]
|
|
||||||
|
|
||||||
const response = await originalMethod.apply(this, modifiedArgs)
|
|
||||||
|
|
||||||
// Получаем тело ответа и парсим JSON
|
|
||||||
const data = await response.json()
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
catch (error) {
|
|
||||||
console.error(`API Error in ${namespace}.${methodName}:`, error)
|
|
||||||
throw error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
// Переопределяем базовый request
|
|
||||||
const nativeRequest = api.request
|
const nativeRequest = api.request
|
||||||
|
|
||||||
api.request = async function (...args) {
|
api.request = async function (...args) {
|
||||||
try {
|
|
||||||
const response = await nativeRequest.call(api, ...args)
|
const response = await nativeRequest.call(api, ...args)
|
||||||
const data = await response.json()
|
return await response.json()
|
||||||
return data
|
|
||||||
}
|
|
||||||
catch (error) {
|
|
||||||
console.error('API Request Error:', error)
|
|
||||||
throw error
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default api
|
export default api
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
export * from './useGetProductsDetail'
|
export * from './useGetProductList'
|
||||||
|
|||||||
9
api/queries/useGetProductList.ts
Normal file
9
api/queries/useGetProductList.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import { useQuery } from '@tanstack/vue-query'
|
||||||
|
import { getProductList } from '~/api/endpoints'
|
||||||
|
|
||||||
|
export const useGetProductList = () => {
|
||||||
|
return useQuery({
|
||||||
|
queryKey: ['get-product-list'],
|
||||||
|
queryFn: () => getProductList(),
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
import { useQuery } from '@tanstack/vue-query'
|
|
||||||
import { unref, watch } from 'vue'
|
|
||||||
import { getProductsDetail } from '~/api/endpoints'
|
|
||||||
|
|
||||||
export const useGetProductsDetail = (productId: MaybeRef<number>) => {
|
|
||||||
const q = useQuery({
|
|
||||||
queryKey: ['get-products-detail', unref(productId)],
|
|
||||||
queryFn: () => getProductsDetail(unref(productId)),
|
|
||||||
})
|
|
||||||
|
|
||||||
watch(() => productId, () => q.refetch())
|
|
||||||
|
|
||||||
return q
|
|
||||||
}
|
|
||||||
14
api/useGetProduct.ts
Normal file
14
api/useGetProduct.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import { useAsyncData } from '#app'
|
||||||
|
|
||||||
|
export function useGetProducts() {
|
||||||
|
return useAsyncData(
|
||||||
|
'products',
|
||||||
|
async () => {
|
||||||
|
const response = await $fetch('https://wp.koptilnya.xyz/wp-json/wp/v2/product')
|
||||||
|
return response
|
||||||
|
},
|
||||||
|
{
|
||||||
|
server: false,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -19,6 +19,7 @@
|
|||||||
"@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",
|
||||||
|
|||||||
@@ -1,20 +1,14 @@
|
|||||||
<template>
|
<template>
|
||||||
<div>
|
<div>
|
||||||
|
productsData
|
||||||
<pre>
|
<pre>
|
||||||
{{ productsData?.images?.filter(img => img?.src?.includes(colorVariants.blackCottonPolyester)) }}
|
{{ productsData }}
|
||||||
</pre>
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { useGetProductsDetail } from '~/api/queries'
|
import { useGetProductList } from '~/api/queries'
|
||||||
|
|
||||||
const { data: productsData } = useGetProductsDetail(79)
|
const { data: productsData } = useGetProductList()
|
||||||
|
|
||||||
const colorVariants = {
|
|
||||||
beigeCottonPolyester: 'beige_cotton-polyester_',
|
|
||||||
blackCotton: 'black_cotton_',
|
|
||||||
greyCottonPolyester: 'grey_cotton-polyester_',
|
|
||||||
blackCottonPolyester: 'black_cotton-polyester_',
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user