Skip to content

Commit 4cbfb05

Browse files
committed
moved useComposables
1 parent f3d422e commit 4cbfb05

File tree

9 files changed

+117
-99
lines changed

9 files changed

+117
-99
lines changed

app/components/Header/AccountMenu.client.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<script setup lang="ts">
2+
import { useAtproto } from '~/composables/atproto/useAtproto'
23
import { useModal } from '~/composables/useModal'
34
45
const {

app/components/Header/AuthModal.client.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { authRedirect } from '~/composables/useAtproto'
2+
import { authRedirect, useAtproto } from '~/composables/atproto/useAtproto'
33
44
const handleInput = shallowRef('')
55

app/components/Header/MobileMenu.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script setup lang="ts">
22
import { useFocusTrap } from '@vueuse/integrations/useFocusTrap'
3+
import { useAtproto } from '~/composables/atproto/useAtproto'
34
45
const isOpen = defineModel<boolean>('open', { default: false })
56
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import type { UserSession } from '#shared/schemas/userSession'
2+
import type { LocationQueryRaw } from 'vue-router'
3+
4+
export async function authRedirect(identifier: string, create: boolean = false) {
5+
let query: LocationQueryRaw = { handle: identifier }
6+
if (create) {
7+
query = { ...query, create: 'true' }
8+
}
9+
await navigateTo(
10+
{
11+
path: '/api/auth/atproto',
12+
query,
13+
},
14+
{ external: true },
15+
)
16+
}
17+
18+
export function useAtproto() {
19+
const { data: user, pending, clear } = useFetch<UserSession | null>('/api/auth/session')
20+
21+
async function logout() {
22+
await $fetch('/api/auth/session', {
23+
method: 'delete',
24+
})
25+
26+
clear()
27+
}
28+
29+
return { user, pending, logout }
30+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { FetchError } from 'ofetch'
2+
import { useAtproto } from '~/composables/atproto/useAtproto'
3+
import { handleAuthError } from '~/utils/atproto/helpers'
4+
5+
export function useLikePackage(packageName: string) {
6+
const { user } = useAtproto()
7+
const data = ref<PackageLikes | null>(null)
8+
const error = ref<Error | null>(null)
9+
const pending = ref(false)
10+
11+
const mutate = async () => {
12+
pending.value = true
13+
error.value = null
14+
15+
try {
16+
const result = await $fetch<PackageLikes>('/api/auth/social/like', {
17+
method: 'POST',
18+
body: { packageName },
19+
})
20+
21+
data.value = result
22+
return result
23+
} catch (e) {
24+
if (e instanceof FetchError) {
25+
await handleAuthError(e, user.value?.handle)
26+
}
27+
error.value = e as Error
28+
} finally {
29+
pending.value = false
30+
}
31+
}
32+
33+
return { data, error, pending, mutate }
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { FetchError } from 'ofetch'
2+
import { useAtproto } from '~/composables/atproto/useAtproto'
3+
import { handleAuthError } from '~/utils/atproto/helpers'
4+
5+
export function useUnlikePackage(packageName: string) {
6+
const { user } = useAtproto()
7+
const data = ref<PackageLikes | null>(null)
8+
const error = ref<Error | null>(null)
9+
const pending = ref(false)
10+
11+
const mutate = async () => {
12+
pending.value = true
13+
error.value = null
14+
15+
try {
16+
const result = await $fetch<PackageLikes>('/api/auth/social/like', {
17+
method: 'DELETE',
18+
body: { packageName },
19+
})
20+
21+
data.value = result
22+
return result
23+
} catch (e) {
24+
if (e instanceof FetchError) {
25+
await handleAuthError(e, user.value?.handle)
26+
}
27+
error.value = e as Error
28+
} finally {
29+
pending.value = false
30+
}
31+
}
32+
33+
return { data, error, pending, mutate }
34+
}

app/composables/useAtproto.ts

Lines changed: 0 additions & 98 deletions
This file was deleted.

app/pages/package/[...package].vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import { isEditableElement } from '~/utils/input'
1313
import { formatBytes } from '~/utils/formatters'
1414
import { NuxtLink } from '#components'
1515
import { useModal } from '~/composables/useModal'
16+
import { useAtproto } from '~/composables/atproto/useAtproto'
17+
import { useLikePackage } from '~/composables/atproto/useLikePackage'
18+
import { useUnlikePackage } from '~/composables/atproto/useUnlikePackage'
1619
1720
definePageMeta({
1821
name: 'package',

app/utils/atproto/helpers.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { FetchError } from 'ofetch'
2+
import { authRedirect } from '~/composables/atproto/useAtproto'
3+
4+
export async function handleAuthError(
5+
fetchError: FetchError,
6+
userHandle?: string | null,
7+
): Promise<never> {
8+
const errorMessage = fetchError?.data?.message
9+
if (errorMessage === ERROR_NEED_REAUTH && userHandle) {
10+
await authRedirect(userHandle)
11+
}
12+
throw fetchError
13+
}

0 commit comments

Comments
 (0)