Replace axios by fetch in oki-api.js
This commit is contained in:
+45
-38
@@ -1,15 +1,25 @@
|
|||||||
import axios from 'axios'
|
|
||||||
import qs from 'qs'
|
import qs from 'qs'
|
||||||
|
|
||||||
const OKI_API = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:1337'
|
const OKI_API = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:1337'
|
||||||
const AWTIS_POU_CHAK_PAJ = process.env.NEXT_PUBLIC_AWTIS_POU_CHAK_PAJ || 6
|
const AWTIS_POU_CHAK_PAJ = process.env.NEXT_PUBLIC_AWTIS_POU_CHAK_PAJ || 6
|
||||||
const readToken = process.env.NEXT_PUBLIC_READ_TOKEN || 'read-token'
|
const readToken = process.env.NEXT_PUBLIC_READ_TOKEN || 'read-token'
|
||||||
|
|
||||||
const readAxiosInstance = axios.create({
|
const headers = {
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${readToken}`
|
Authorization: `Bearer ${readToken}`
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
|
async function request(url, opts = {}) {
|
||||||
|
const {...options} = opts
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch(`${OKI_API}${url}`, options)
|
||||||
|
return res.json()
|
||||||
|
} catch {
|
||||||
|
throw new Error('Eshwe pu jwenn done-a')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function jwennTeksEpiSlug(slug) {
|
export async function jwennTeksEpiSlug(slug) {
|
||||||
const query = qs.stringify({
|
const query = qs.stringify({
|
||||||
@@ -60,13 +70,14 @@ export async function jwennTeksEpiSlug(slug) {
|
|||||||
}, {
|
}, {
|
||||||
encodeValuesOnly: true
|
encodeValuesOnly: true
|
||||||
})
|
})
|
||||||
const {data} = await readAxiosInstance.get(`${OKI_API}/paroles?${query}`)
|
|
||||||
return data.data[0]
|
const {data} = await request(`/paroles?${query}`, headers)
|
||||||
|
return data[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function jwennAwtisEpiSlug(slug) {
|
export async function jwennAwtisEpiSlug(slug) {
|
||||||
const query = qs.stringify({
|
const query = qs.stringify({
|
||||||
populate: ['paroles', 'photo'],
|
populate: ['paroles', 'photo', 'paroles.couverture'],
|
||||||
filters: {
|
filters: {
|
||||||
slug: {
|
slug: {
|
||||||
$eq: slug
|
$eq: slug
|
||||||
@@ -75,13 +86,9 @@ export async function jwennAwtisEpiSlug(slug) {
|
|||||||
}, {
|
}, {
|
||||||
encodeValuesOnly: true
|
encodeValuesOnly: true
|
||||||
})
|
})
|
||||||
const {data} = await readAxiosInstance.get(`${OKI_API}/artistes?${query}`)
|
|
||||||
return data.data[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function jwennTeksKantite() {
|
const {data} = await request(`/artistes?${query}`, headers)
|
||||||
const response = await axios.get(`${OKI_API}/teks/count?_where[published_at_null]=false`)
|
return data[0]
|
||||||
return response.data
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function jwennAwtisPajinasyon(paj) {
|
export async function jwennAwtisPajinasyon(paj) {
|
||||||
@@ -97,7 +104,7 @@ export async function jwennAwtisPajinasyon(paj) {
|
|||||||
encodeValuesOnly: true
|
encodeValuesOnly: true
|
||||||
})
|
})
|
||||||
|
|
||||||
const {data} = await readAxiosInstance.get(`${OKI_API}/artistes?${query}`)
|
const data = await request(`/artistes?${query}`, headers)
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,7 +119,7 @@ export async function jwennToutAwtis() {
|
|||||||
encodeValuesOnly: true
|
encodeValuesOnly: true
|
||||||
})
|
})
|
||||||
|
|
||||||
const {data} = await readAxiosInstance.get(`${OKI_API}/artistes?${query}`)
|
const {data} = await request(`/artistes?${query}`, headers)
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,8 +164,8 @@ export async function jwennTeks() {
|
|||||||
encodeValuesOnly: true
|
encodeValuesOnly: true
|
||||||
})
|
})
|
||||||
|
|
||||||
const {data} = await readAxiosInstance.get(`${OKI_API}/paroles?${query}`)
|
const {data} = await request(`/paroles?${query}`, headers)
|
||||||
return data.data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function jwennAwtisSlug() {
|
export async function jwennAwtisSlug() {
|
||||||
@@ -171,9 +178,8 @@ export async function jwennAwtisSlug() {
|
|||||||
encodeValuesOnly: true
|
encodeValuesOnly: true
|
||||||
})
|
})
|
||||||
|
|
||||||
const response = await readAxiosInstance.get(`${OKI_API}/artistes?${query}`)
|
const {data} = await request(`/artistes?${query}`, headers)
|
||||||
const {data} = response
|
return data.map(({attributes}) => attributes.slug)
|
||||||
return data.data.map(({attributes}) => attributes.slug)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function jwennTeksSlug() {
|
export async function jwennTeksSlug() {
|
||||||
@@ -186,9 +192,8 @@ export async function jwennTeksSlug() {
|
|||||||
encodeValuesOnly: true
|
encodeValuesOnly: true
|
||||||
})
|
})
|
||||||
|
|
||||||
const response = await readAxiosInstance.get(`${OKI_API}/paroles?${query}`)
|
const {data} = await request(`/paroles?${query}`, headers)
|
||||||
const {data} = response
|
return data.map(({attributes}) => attributes.slug)
|
||||||
return data.data.map(({attributes}) => attributes.slug)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function jwennDenyeTeks() {
|
export async function jwennDenyeTeks() {
|
||||||
@@ -202,8 +207,9 @@ export async function jwennDenyeTeks() {
|
|||||||
encodeValuesOnly: true
|
encodeValuesOnly: true
|
||||||
})
|
})
|
||||||
|
|
||||||
const {data} = await readAxiosInstance.get(`${OKI_API}/paroles?${query}`)
|
const {data} = await request(`/paroles?${query}`, headers)
|
||||||
return data.data
|
|
||||||
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function jwennAnTeks(teksId, token) {
|
export async function jwennAnTeks(teksId, token) {
|
||||||
@@ -212,8 +218,8 @@ export async function jwennAnTeks(teksId, token) {
|
|||||||
Authorization: `Bearer ${token}`
|
Authorization: `Bearer ${token}`
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await axios.get(`${OKI_API}/paroles/${teksId}`, {headers})
|
const {data} = await request(`/paroles/${teksId}`, {headers})
|
||||||
return response.data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function jwennKomanteEpiTeksId(teksId) {
|
export async function jwennKomanteEpiTeksId(teksId) {
|
||||||
@@ -231,17 +237,18 @@ export async function jwennKomanteEpiTeksId(teksId) {
|
|||||||
encodeValuesOnly: true
|
encodeValuesOnly: true
|
||||||
})
|
})
|
||||||
|
|
||||||
const {data} = await readAxiosInstance.get(`${OKI_API}/commentaires?${query}`)
|
const {data} = await request(`/commentaires?${query}`, headers)
|
||||||
return data.data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function jwennUserEpiToken(userToken) {
|
export async function jwennUserEpiToken(userToken) {
|
||||||
const response = await axios.get(`${OKI_API}/users/me`, {
|
const data = await request('/users/me', {
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${userToken}`
|
Authorization: `Bearer ${userToken}`
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return response.data
|
|
||||||
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function jwennUserEpiUsername(username) {
|
export async function jwennUserEpiUsername(username) {
|
||||||
@@ -255,8 +262,8 @@ export async function jwennUserEpiUsername(username) {
|
|||||||
encodeValuesOnly: true
|
encodeValuesOnly: true
|
||||||
})
|
})
|
||||||
|
|
||||||
const response = await readAxiosInstance.get(`${OKI_API}/users?${query}`)
|
const data = await request(`/users?${query}`, headers)
|
||||||
return response.data[0]
|
return data[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function jwennUserEpiEmail(email) {
|
export async function jwennUserEpiEmail(email) {
|
||||||
@@ -276,17 +283,17 @@ export async function jwennUserEpiEmail(email) {
|
|||||||
encodeValuesOnly: true
|
encodeValuesOnly: true
|
||||||
})
|
})
|
||||||
|
|
||||||
const response = await readAxiosInstance.get(`${OKI_API}/users?${query}`)
|
const {data} = await request(`/users?${query}`, headers)
|
||||||
return response.data[0]
|
return data[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function passwordRequest(lyen, email) {
|
export async function passwordRequest(lyen, email) {
|
||||||
await axios.post(`${OKI_API}/auth/${lyen}`, {
|
await request(`/auth/${lyen}`, {
|
||||||
email
|
method: 'POST',
|
||||||
|
body: {email}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function jwennStats() {
|
export async function jwennStats() {
|
||||||
const response = await axios.get(`${OKI_API}/stats`)
|
return request('/stats')
|
||||||
return response.data
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user