Skip to content
Snippets Groups Projects
Commit 006fdcc1 authored by Remi  PLANEL's avatar Remi PLANEL
Browse files

create a plugin and composable to useAPI

parent 506c91ce
No related branches found
No related tags found
1 merge request!12Genomic context
Pipeline #121818 passed
...@@ -25,10 +25,9 @@ const sanitizedAnalyses = computed(() => { ...@@ -25,10 +25,9 @@ const sanitizedAnalyses = computed(() => {
async function deleteAnalysis(analysisId: number) { async function deleteAnalysis(analysisId: number) {
if (csrfToken.value) { if (csrfToken.value) {
await useFetch<null>(`/api/analysis/${analysisId}`, await useAPI<null>(`/api/analysis/${analysisId}`,
{ {
method: "DELETE", method: "DELETE",
headers: [["X-CSRFToken", csrfToken.value]]
} }
) )
refresh() refresh()
...@@ -42,10 +41,9 @@ function editionMode(analysisId: number, name: string) { ...@@ -42,10 +41,9 @@ function editionMode(analysisId: number, name: string) {
} }
async function editAnalysis(analysisId: number) { async function editAnalysis(analysisId: number) {
if (csrfToken.value) { if (csrfToken.value) {
await useFetch<null>(`/api/analysis/${analysisId}`, await useAPI<null>(`/api/analysis/${analysisId}`,
{ {
method: "PUT", method: "PUT",
headers: [["X-CSRFToken", csrfToken.value]],
body: { body: {
name: newTitle.value name: newTitle.value
}, },
......
...@@ -22,10 +22,7 @@ const computedMinWidth = computed(() => { ...@@ -22,10 +22,7 @@ const computedMinWidth = computed(() => {
watchEffect(async () => { watchEffect(async () => {
if (csrfToken.value) { if (csrfToken.value) {
// init session // init session
const { data, pending, error } = await useFetch("/api/analysis/init-session", { const { data, pending, error } = await useAPI("/api/analysis/init-session")
headers: [["X-CSRFToken", csrfToken.value]]
})
sessionExpiryDate.value = data.value?.expire_date ?? "NA" sessionExpiryDate.value = data.value?.expire_date ?? "NA"
} }
......
import type { UseFetchOptions } from 'nuxt/app';
export function useAPI<T>(
url: string | (() => string),
options: UseFetchOptions<T> = {}
) {
console.log("useAPI for ", url)
return useFetch(url, {
...options,
$fetch: useNuxtApp().$api,
})
}
\ No newline at end of file
export const useCsrfToken = async () => { export const useCsrfToken = async () => {
const currentCsrfToken = useCookie('csrftoken') const currentCsrfToken = useCookie('csrftoken')
const csrfToken: Ref<string | null | undefined> = ref(null) const csrfToken: Ref<string | undefined> = ref(undefined)
const pending = ref() const pending = ref()
const error = ref() const error = ref()
// if (!currentCsrfToken.value) { // if (!currentCsrfToken.value) {
......
import { type Fn, useIntervalFn } from "@vueuse/core"; import { type Fn, useIntervalFn } from "@vueuse/core";
import { useFetch } from '#imports'
export const useFetchAnalyses = async (interval = 5000, polling = true,) => { export const useFetchAnalyses = async (interval = 5000, polling = true,) => {
const pause = ref<Fn | null>(null) const pause = ref<Fn | null>(null)
...@@ -10,7 +9,7 @@ export const useFetchAnalyses = async (interval = 5000, polling = true,) => { ...@@ -10,7 +9,7 @@ export const useFetchAnalyses = async (interval = 5000, polling = true,) => {
error, error,
refresh, refresh,
pending, pending,
} = await useFetch<Analysis[]>( } = await useAPI<Analysis[]>(
`/api/analysis/`, `/api/analysis/`,
); );
......
...@@ -36,7 +36,7 @@ export const useFetchAnalysis = async (analysisId: number, interval = 5000, poll ...@@ -36,7 +36,7 @@ export const useFetchAnalysis = async (analysisId: number, interval = 5000, poll
error, error,
refresh, refresh,
pending, pending,
} = await useFetch<Analysis>( } = await useAPI<Analysis>(
`/api/analysis/${analysisId}`, `/api/analysis/${analysisId}`,
); );
......
export default defineNuxtPlugin(() => {
const $api = $fetch.create({
baseURL: '/api',
async onRequest({ request, options, error }) {
const { csrfToken } = await useCsrfToken()
const toValToken = toValue(csrfToken)
if (toValToken !== undefined) {
// Add Authorization header
if (Array.isArray(options.headers)) {
options.headers = options.headers || []
options.headers = [...options.headers, ["X-CSRFToken", toValToken]]
}
else {
options.headers = options.headers || {}
options.headers = { ...options.headers, "X-CSRFToken": toValToken }
}
}
},
onResponseError({ response }) {
console.log(response)
}
})
// Expose to useNuxtApp().$api
return {
provide: {
api: $api
}
}
})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment