feat [server]: nuxt rc 5

This commit is contained in:
Alice 2022-07-18 22:24:28 +02:00
parent 708c18dc67
commit e493543ca5
4 changed files with 1646 additions and 1356 deletions

View file

@ -1,4 +1,3 @@
import { NuxtApp } from "nuxt3/dist/app/nuxt"
import { useLoadingStore } from "~/stores/loadingStore" import { useLoadingStore } from "~/stores/loadingStore"
type MyHeaders = { [key: string]: string } type MyHeaders = { [key: string]: string }
@ -19,27 +18,27 @@ const makeLoadingKey = (path: string) => {
return words.join("") return words.join("")
} }
const getCsrfCookie = (ctx: NuxtApp) => { const getCsrfCookie = () => {
let cookie = "" let cookie: string
if (ctx?.ssrContext?.req.headers.cookie) { if (process.server) {
cookie = ctx?.ssrContext.req.headers.cookie cookie = useRequestHeaders(["cookie"])["cookie"]
} else if (process.client) { } else {
cookie = document.cookie cookie = document.cookie
} }
if (!cookie) { if (!cookie) {
return null return null
} }
const csfrRow = cookie.split("; ").find((row) => row.startsWith("csrftoken=")) const csrfRow = cookie.split("; ").find((row) => row.startsWith("csrftoken="))
if (!csfrRow) { if (!csrfRow) {
return null return null
} }
return csfrRow.split("=")[1] return csrfRow.split("=")[1]
} }
const getHeaders = (ctx: NuxtApp, includeCsrf = false): MyHeaders => { const getHeaders = (includeCsrf = false): MyHeaders => {
const headers: MyHeaders = useRequestHeaders(["cookie"]) const headers: MyHeaders = useRequestHeaders(["cookie"])
if (includeCsrf) { if (includeCsrf) {
const csfrToken = getCsrfCookie(ctx) const csfrToken = getCsrfCookie()
if (csfrToken) { if (csfrToken) {
headers["X-CSRFTOKEN"] = csfrToken headers["X-CSRFTOKEN"] = csfrToken
} }
@ -72,21 +71,24 @@ export async function apiRequest<Type>(
const key = makeLoadingKey(path) const key = makeLoadingKey(path)
loadingStore.markLoading(key) loadingStore.markLoading(key)
const { data, error } = await useAsyncData<Type>(key, (ctx) => const response = await useAsyncData<Type>(
$fetch(baseUrl + path, { key,
method: method, () =>
body: payload, $fetch(baseUrl + path, {
credentials: "include", method: method,
headers: getHeaders(ctx, true), body: payload,
params: params, credentials: "include",
}) headers: getHeaders(method != "GET"),
params: params,
}),
{ initialCache: false }
) )
if (error.value) { if (response.error.value) {
loadingStore.markError(key) loadingStore.markError(key)
} else { } else {
loadingStore.markDone(key) loadingStore.markDone(key)
} }
return { data, error } return response
} }
export async function apiGet<Type>(path: string, params = {}) { export async function apiGet<Type>(path: string, params = {}) {

View file

@ -1,4 +1,4 @@
import { defineNuxtConfig } from "nuxt3" import { defineNuxtConfig } from "nuxt"
// https://v3.nuxtjs.org/docs/directory-structure/nuxt.config // https://v3.nuxtjs.org/docs/directory-structure/nuxt.config
export default defineNuxtConfig({ export default defineNuxtConfig({

View file

@ -15,7 +15,7 @@
"typescript": "^4.6.2" "typescript": "^4.6.2"
}, },
"devDependencies": { "devDependencies": {
"@pinia/nuxt": "^0.1.8", "@pinia/nuxt": "^0.3.0",
"@typescript-eslint/eslint-plugin": "^5.13.0", "@typescript-eslint/eslint-plugin": "^5.13.0",
"@typescript-eslint/parser": "^5.13.0", "@typescript-eslint/parser": "^5.13.0",
"@vue/eslint-config-standard": "^6.1.0", "@vue/eslint-config-standard": "^6.1.0",
@ -25,8 +25,8 @@
"eslint-plugin-nuxt": "^3.1.0", "eslint-plugin-nuxt": "^3.1.0",
"eslint-plugin-prettier": "^4.0.0", "eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-vue": "^8.5.0", "eslint-plugin-vue": "^8.5.0",
"nuxt3": "^3.0.0-27447229.20f3171", "nuxt": "^3.0.0-rc.5",
"pinia": "^2.0.13", "pinia": "^2.0.16",
"sass": "^1.49.9", "sass": "^1.49.9",
"sass-loader": "10" "sass-loader": "10"
} }

File diff suppressed because it is too large Load diff