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'
|
||||
|
||||
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 readToken = process.env.NEXT_PUBLIC_READ_TOKEN || 'read-token'
|
||||
|
||||
const readAxiosInstance = axios.create({
|
||||
const headers = {
|
||||
headers: {
|
||||
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) {
|
||||
const query = qs.stringify({
|
||||
@@ -60,13 +70,14 @@ export async function jwennTeksEpiSlug(slug) {
|
||||
}, {
|
||||
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) {
|
||||
const query = qs.stringify({
|
||||
populate: ['paroles', 'photo'],
|
||||
populate: ['paroles', 'photo', 'paroles.couverture'],
|
||||
filters: {
|
||||
slug: {
|
||||
$eq: slug
|
||||
@@ -75,13 +86,9 @@ export async function jwennAwtisEpiSlug(slug) {
|
||||
}, {
|
||||
encodeValuesOnly: true
|
||||
})
|
||||
const {data} = await readAxiosInstance.get(`${OKI_API}/artistes?${query}`)
|
||||
return data.data[0]
|
||||
}
|
||||
|
||||
export async function jwennTeksKantite() {
|
||||
const response = await axios.get(`${OKI_API}/teks/count?_where[published_at_null]=false`)
|
||||
return response.data
|
||||
const {data} = await request(`/artistes?${query}`, headers)
|
||||
return data[0]
|
||||
}
|
||||
|
||||
export async function jwennAwtisPajinasyon(paj) {
|
||||
@@ -97,7 +104,7 @@ export async function jwennAwtisPajinasyon(paj) {
|
||||
encodeValuesOnly: true
|
||||
})
|
||||
|
||||
const {data} = await readAxiosInstance.get(`${OKI_API}/artistes?${query}`)
|
||||
const data = await request(`/artistes?${query}`, headers)
|
||||
return data
|
||||
}
|
||||
|
||||
@@ -112,7 +119,7 @@ export async function jwennToutAwtis() {
|
||||
encodeValuesOnly: true
|
||||
})
|
||||
|
||||
const {data} = await readAxiosInstance.get(`${OKI_API}/artistes?${query}`)
|
||||
const {data} = await request(`/artistes?${query}`, headers)
|
||||
return data
|
||||
}
|
||||
|
||||
@@ -157,8 +164,8 @@ export async function jwennTeks() {
|
||||
encodeValuesOnly: true
|
||||
})
|
||||
|
||||
const {data} = await readAxiosInstance.get(`${OKI_API}/paroles?${query}`)
|
||||
return data.data
|
||||
const {data} = await request(`/paroles?${query}`, headers)
|
||||
return data
|
||||
}
|
||||
|
||||
export async function jwennAwtisSlug() {
|
||||
@@ -171,9 +178,8 @@ export async function jwennAwtisSlug() {
|
||||
encodeValuesOnly: true
|
||||
})
|
||||
|
||||
const response = await readAxiosInstance.get(`${OKI_API}/artistes?${query}`)
|
||||
const {data} = response
|
||||
return data.data.map(({attributes}) => attributes.slug)
|
||||
const {data} = await request(`/artistes?${query}`, headers)
|
||||
return data.map(({attributes}) => attributes.slug)
|
||||
}
|
||||
|
||||
export async function jwennTeksSlug() {
|
||||
@@ -186,9 +192,8 @@ export async function jwennTeksSlug() {
|
||||
encodeValuesOnly: true
|
||||
})
|
||||
|
||||
const response = await readAxiosInstance.get(`${OKI_API}/paroles?${query}`)
|
||||
const {data} = response
|
||||
return data.data.map(({attributes}) => attributes.slug)
|
||||
const {data} = await request(`/paroles?${query}`, headers)
|
||||
return data.map(({attributes}) => attributes.slug)
|
||||
}
|
||||
|
||||
export async function jwennDenyeTeks() {
|
||||
@@ -202,8 +207,9 @@ export async function jwennDenyeTeks() {
|
||||
encodeValuesOnly: true
|
||||
})
|
||||
|
||||
const {data} = await readAxiosInstance.get(`${OKI_API}/paroles?${query}`)
|
||||
return data.data
|
||||
const {data} = await request(`/paroles?${query}`, headers)
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
export async function jwennAnTeks(teksId, token) {
|
||||
@@ -212,8 +218,8 @@ export async function jwennAnTeks(teksId, token) {
|
||||
Authorization: `Bearer ${token}`
|
||||
}
|
||||
|
||||
const response = await axios.get(`${OKI_API}/paroles/${teksId}`, {headers})
|
||||
return response.data
|
||||
const {data} = await request(`/paroles/${teksId}`, {headers})
|
||||
return data
|
||||
}
|
||||
|
||||
export async function jwennKomanteEpiTeksId(teksId) {
|
||||
@@ -231,17 +237,18 @@ export async function jwennKomanteEpiTeksId(teksId) {
|
||||
encodeValuesOnly: true
|
||||
})
|
||||
|
||||
const {data} = await readAxiosInstance.get(`${OKI_API}/commentaires?${query}`)
|
||||
return data.data
|
||||
const {data} = await request(`/commentaires?${query}`, headers)
|
||||
return data
|
||||
}
|
||||
|
||||
export async function jwennUserEpiToken(userToken) {
|
||||
const response = await axios.get(`${OKI_API}/users/me`, {
|
||||
const data = await request('/users/me', {
|
||||
headers: {
|
||||
Authorization: `Bearer ${userToken}`
|
||||
}
|
||||
})
|
||||
return response.data
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
export async function jwennUserEpiUsername(username) {
|
||||
@@ -255,8 +262,8 @@ export async function jwennUserEpiUsername(username) {
|
||||
encodeValuesOnly: true
|
||||
})
|
||||
|
||||
const response = await readAxiosInstance.get(`${OKI_API}/users?${query}`)
|
||||
return response.data[0]
|
||||
const data = await request(`/users?${query}`, headers)
|
||||
return data[0]
|
||||
}
|
||||
|
||||
export async function jwennUserEpiEmail(email) {
|
||||
@@ -276,17 +283,17 @@ export async function jwennUserEpiEmail(email) {
|
||||
encodeValuesOnly: true
|
||||
})
|
||||
|
||||
const response = await readAxiosInstance.get(`${OKI_API}/users?${query}`)
|
||||
return response.data[0]
|
||||
const {data} = await request(`/users?${query}`, headers)
|
||||
return data[0]
|
||||
}
|
||||
|
||||
export async function passwordRequest(lyen, email) {
|
||||
await axios.post(`${OKI_API}/auth/${lyen}`, {
|
||||
email
|
||||
await request(`/auth/${lyen}`, {
|
||||
method: 'POST',
|
||||
body: {email}
|
||||
})
|
||||
}
|
||||
|
||||
export async function jwennStats() {
|
||||
const response = await axios.get(`${OKI_API}/stats`)
|
||||
return response.data
|
||||
return request('/stats')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user