Skip to content

Commit

Permalink
fix: request
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Apr 12, 2024
1 parent 4c377eb commit c901dbb
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Metadata } from 'next'

import { QueryHydrate } from '~/components/common/QueryHydrate'
import { NormalContainer } from '~/components/layout/container/Normal'
import { attachServerFetch } from '~/lib/attach-ua'
import { attachServerFetch } from '~/lib/attach-fetch'
import { isShallowEqualArray } from '~/lib/lodash'
import { getQueryClient } from '~/lib/query-client.server'
import { definePrerenderPage } from '~/lib/request.server'
Expand Down
2 changes: 1 addition & 1 deletion src/app/(app)/(page-detail)/[slug]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
BottomToUpTransitionView,
} from '~/components/ui/transition'
import { OnlyMobile } from '~/components/ui/viewport/OnlyMobile'
import { attachServerFetch } from '~/lib/attach-ua'
import { attachServerFetch } from '~/lib/attach-fetch'
import { getOgUrl } from '~/lib/helper.server'
import { getSummaryFromMd } from '~/lib/markdown'
import { apiClient } from '~/lib/request'
Expand Down
2 changes: 1 addition & 1 deletion src/app/(app)/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { AggregateRoot } from '@mx-space/api-client'
import { simpleCamelcaseKeys } from '@mx-space/api-client'

import { appStaticConfig } from '~/app.static.config'
import { attachServerFetch } from '~/lib/attach-ua'
import { attachServerFetch } from '~/lib/attach-fetch'
import { getQueryClient } from '~/lib/query-client.server'
import { apiClient } from '~/lib/request'

Expand Down
2 changes: 1 addition & 1 deletion src/app/(app)/categories/[slug]/api.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { cache } from 'react'

import { attachServerFetch } from '~/lib/attach-ua'
import { attachServerFetch } from '~/lib/attach-fetch'
import { apiClient } from '~/lib/request'
import { requestErrorHandler } from '~/lib/request.server'

Expand Down
8 changes: 6 additions & 2 deletions src/app/(app)/notes/[id]/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { cache } from 'react'
import { headers } from 'next/dist/client/components/headers'

import { REQUEST_QUERY } from '~/constants/system'
import { attachServerFetch } from '~/lib/attach-ua'
import { attachServerFetch, getAuthFromCookie } from '~/lib/attach-fetch'
import { getQueryClient } from '~/lib/query-client.server'
import { requestErrorHandler } from '~/lib/request.server'
import { queries } from '~/queries/definition'
Expand All @@ -19,8 +19,12 @@ export const getData = cache(async (params: { id: string }) => {
searchParams.get('password') || undefined,
token ? `${token}` : undefined,
)

const data = await getQueryClient()
.fetchQuery(query)
.fetchQuery({
...query,
staleTime: getAuthFromCookie() ? 0 : undefined,
})
.catch(requestErrorHandler)
return data
})
2 changes: 1 addition & 1 deletion src/app/(app)/notes/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { cookies } from 'next/headers'
import { redirect } from 'next/navigation'
import type { NoteWrappedWithLikedPayload } from '@mx-space/api-client'

import { getAuthFromCookie } from '~/lib/attach-ua'
import { getAuthFromCookie } from '~/lib/attach-fetch'
import { AuthKeyNames } from '~/lib/cookie'
import { apiClient } from '~/lib/request'
import { definePrerenderPage } from '~/lib/request.server'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { cache } from 'react'

import { attachServerFetch } from '~/lib/attach-ua'
import { attachServerFetch } from '~/lib/attach-fetch'
import { getQueryClient } from '~/lib/query-client.server'
import { requestErrorHandler } from '~/lib/request.server'
import { queries } from '~/queries/definition'
Expand Down
2 changes: 1 addition & 1 deletion src/app/(app)/timeline/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { TimelineType } from '@mx-space/api-client'
import { QueryHydrate } from '~/components/common/QueryHydrate'
import { SearchFAB } from '~/components/modules/shared/SearchFAB'
import { REQUEST_QUERY } from '~/constants/system'
import { attachServerFetch } from '~/lib/attach-ua'
import { attachServerFetch } from '~/lib/attach-fetch'
import { getQueryClient } from '~/lib/query-client.server'
import { apiClient } from '~/lib/request'

Expand Down
9 changes: 9 additions & 0 deletions src/lib/attach-ua.ts → src/lib/attach-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ export const attachServerFetch = () => {
'User-Agent',
`${ua} NextJS/v${PKG.dependencies.next} ${PKG.name}/${PKG.version}`,
)

// TODO 安全考虑还是去掉了
// const jwt = getAuthFromCookie()
// clearGlobalSearchParams()

// if (jwt)
// setGlobalSearchParams({
// token: jwt,
// })
}

export const attachServerFetchAuth = () => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/define-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { Metadata } from 'next'
import { getQueryClient } from '~/lib/query-client.server'
import { queries } from '~/queries/definition'

import { attachServerFetch } from './attach-ua'
import { attachServerFetch } from './attach-fetch'

export const defineMetadata = <T extends Record<string, string>>(
fn: (
Expand Down
2 changes: 2 additions & 0 deletions src/lib/query-client.server.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'server-only'

import { QueryClient } from '@tanstack/react-query'

const sharedClient = new QueryClient({
Expand Down
2 changes: 1 addition & 1 deletion src/lib/request.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { RequestError } from '@mx-space/api-client'
import { BizErrorPage } from '~/components/common/BizErrorPage'
import { NormalContainer } from '~/components/layout/container/Normal'

import { attachServerFetch } from './attach-ua'
import { attachServerFetch } from './attach-fetch'
import { getErrorMessageFromRequestError } from './request.shared'

export const requestErrorHandler = (error: Error | RequestError) => {
Expand Down
24 changes: 24 additions & 0 deletions src/lib/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const uuidStorageKey = 'x-uuid'
const uuid = nanoid()

const globalConfigureHeader = {} as any
const globalConfigureSearchParams = {} as any

if (isServerSide) {
globalConfigureHeader['User-Agent'] =
Expand Down Expand Up @@ -43,11 +44,23 @@ const $fetch = createFetch({
}
}

context.options.params ??= {}
Object.assign(context.options.params, globalConfigureSearchParams)
if (context.options.params.token) {
context.options.cache = 'no-store'
}
if (isDev && isServerSide) {
// eslint-disable-next-line no-console
console.log(`[Request]: ${context.request}`)
}
},
onResponse(context) {
// log response
if (isDev && isServerSide) {
// eslint-disable-next-line no-console
console.log(`[Response]: ${context.request}`, context.response.status)
}
},
},
})

Expand Down Expand Up @@ -117,3 +130,14 @@ export const attachFetchHeader = (key: string, value: string | null) => {
}
}
}

export const setGlobalSearchParams = (params: Record<string, any>) => {
clearGlobalSearchParams()
Object.assign(globalConfigureSearchParams, params)
}

export const clearGlobalSearchParams = () => {
Object.keys(globalConfigureSearchParams).forEach((key) => {
delete globalConfigureSearchParams[key]
})
}

0 comments on commit c901dbb

Please sign in to comment.