fix building by adjusting types
This commit is contained in:
parent
6cf544fc72
commit
27a604dc90
10 changed files with 70 additions and 45 deletions
|
@ -44,7 +44,12 @@ const PasswordModalWrapper = ({ setPost, postId, authorId }: Props) => {
|
|||
return
|
||||
}
|
||||
|
||||
const data = await res.json()
|
||||
// TODO: properly check type
|
||||
const data = (await res.json()) as {
|
||||
post: PostWithFilesAndAuthor
|
||||
error?: string
|
||||
}
|
||||
|
||||
if (data) {
|
||||
if (data.error) {
|
||||
setToast({
|
||||
|
|
|
@ -18,17 +18,30 @@ export function Providers({ children }: PropsWithChildren<unknown>) {
|
|||
)
|
||||
}
|
||||
|
||||
export type ApiResponse<T> = {
|
||||
data: T
|
||||
error: never
|
||||
} | {
|
||||
data: never
|
||||
error: string
|
||||
}
|
||||
|
||||
async function fetcher<T>(url: string): Promise<unknown> {
|
||||
const response = await fetch(url)
|
||||
const data: ApiResponse<T> = await response.json() as ApiResponse<T>
|
||||
|
||||
if (data.error) {
|
||||
throw new Error(data.error)
|
||||
}
|
||||
|
||||
return data.data
|
||||
}
|
||||
|
||||
function SWRProvider({ children }: PropsWithChildren<unknown>) {
|
||||
return (
|
||||
<SWRConfig
|
||||
value={{
|
||||
fetcher: async (url: string) => {
|
||||
const data = await fetch(url).then((res) => res.json())
|
||||
if (data.error) {
|
||||
throw new Error(data.error)
|
||||
}
|
||||
return data
|
||||
},
|
||||
fetcher,
|
||||
keepPreviousData: true
|
||||
}}
|
||||
>
|
||||
|
|
|
@ -10,6 +10,7 @@ import { useRouter } from "next/navigation"
|
|||
import { useSessionSWR } from "@lib/use-session-swr"
|
||||
import { fetchWithUser } from "src/app/lib/fetch-with-user"
|
||||
import FadeIn from "@components/fade-in"
|
||||
import { PostWithFiles } from "@lib/server/prisma"
|
||||
|
||||
type Props = {
|
||||
authorId: string
|
||||
|
@ -42,7 +43,7 @@ function VisibilityControl({
|
|||
})
|
||||
|
||||
if (res.ok) {
|
||||
const json = await res.json()
|
||||
const json = await res.json() as PostWithFiles
|
||||
setVisibility(json.visibility)
|
||||
router.refresh()
|
||||
setToast({
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import Button from "@components/button"
|
||||
import React from "react"
|
||||
import React, { ReactNode } from "react"
|
||||
import styles from "./dropdown.module.css"
|
||||
import * as DropdownMenu from "@radix-ui/react-dropdown-menu"
|
||||
import { ArrowDown } from "react-feather"
|
||||
|
@ -14,14 +14,11 @@ type ButtonDropdownProps = Props & Attrs
|
|||
const ButtonDropdown: React.FC<
|
||||
React.PropsWithChildren<ButtonDropdownProps>
|
||||
> = ({ type, ...props }) => {
|
||||
if (!Array.isArray(props.children)) {
|
||||
return null
|
||||
}
|
||||
|
||||
return (
|
||||
<DropdownMenu.Root>
|
||||
<div className={styles.dropdown} style={{ height: props.height }}>
|
||||
{props.children[0]}
|
||||
<>
|
||||
{Array.isArray(props.children) ? props.children[0] : props.children}
|
||||
<DropdownMenu.Trigger
|
||||
style={{
|
||||
display: "flex",
|
||||
|
@ -36,13 +33,18 @@ const ButtonDropdown: React.FC<
|
|||
className={styles.icon}
|
||||
/>
|
||||
</DropdownMenu.Trigger>
|
||||
{Array.isArray(props.children) ? (
|
||||
<DropdownMenu.Portal>
|
||||
<DropdownMenu.Content align="end">
|
||||
{props.children.slice(1).map((child, index) => (
|
||||
{(props.children as ReactNode[])
|
||||
?.slice(1)
|
||||
.map((child, index) => (
|
||||
<DropdownMenu.Item key={index}>{child}</DropdownMenu.Item>
|
||||
))}
|
||||
</DropdownMenu.Content>
|
||||
</DropdownMenu.Portal>
|
||||
) : null}
|
||||
</>
|
||||
</div>
|
||||
</DropdownMenu.Root>
|
||||
)
|
||||
|
|
|
@ -59,7 +59,7 @@ const PostList = ({
|
|||
}
|
||||
}
|
||||
)
|
||||
const json = await res.json()
|
||||
const json = await res.json() as PostWithFiles[]
|
||||
setPosts(json)
|
||||
setSearching(false)
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { ApiToken } from "@prisma/client"
|
||||
import { ApiResponse } from "src/app/(drift)/providers"
|
||||
import useSWR from "swr"
|
||||
|
||||
type ConvertDateToString<T> = {
|
||||
|
@ -35,15 +36,15 @@ export function useApiTokens({ userId, initialTokens }: UseApiTokens) {
|
|||
}
|
||||
)
|
||||
|
||||
const response = await res.json()
|
||||
const response = await res.json() as ApiResponse<SerializedApiToken>
|
||||
if (response.error) {
|
||||
throw new Error(response.error)
|
||||
return
|
||||
}
|
||||
|
||||
mutate([...(data || []), response])
|
||||
mutate([...(data || []), response.data])
|
||||
|
||||
return response as SerializedApiToken
|
||||
return response.data
|
||||
}
|
||||
|
||||
const expireToken = async (id: string) => {
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-nocheck
|
||||
|
||||
import { Gist, GistFile } from "./types"
|
||||
|
||||
async function fetchHelper(response: Response): Promise<Response> {
|
||||
|
@ -6,7 +9,7 @@ async function fetchHelper(response: Response): Promise<Response> {
|
|||
.get("content-type")
|
||||
?.includes("application/json")
|
||||
const err = await (isJson ? response.json() : response.text())
|
||||
throw new Error(err)
|
||||
throw new Error(err as string)
|
||||
}
|
||||
return response
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||
}
|
||||
})
|
||||
|
||||
return res.json(tokens)
|
||||
return res.json({ data: tokens })
|
||||
}
|
||||
case "POST": {
|
||||
const name = parseQueryParam(req.query.name)
|
||||
|
@ -33,7 +33,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||
return res.status(400).json({ error: "Missing token name" })
|
||||
}
|
||||
const token = await createApiToken(userId, name)
|
||||
return res.json(token)
|
||||
return res.json({ data: token })
|
||||
}
|
||||
case "DELETE": {
|
||||
const tokenId = parseQueryParam(req.query.tokenId)
|
||||
|
|
2
types/index.d.ts
vendored
Normal file
2
types/index.d.ts
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
|
||||
import "@total-typescript/ts-reset";
|
2
types/next-auth.d.ts
vendored
2
types/next-auth.d.ts
vendored
|
@ -1,5 +1,3 @@
|
|||
import "@total-typescript/ts-reset";
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
import type { User } from "next-auth"
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
|
|
Loading…
Reference in a new issue