Skip to content

Commit 7f2a227

Browse files
committed
feat: add api folder
1 parent 5b26f8b commit 7f2a227

File tree

10 files changed

+238
-0
lines changed

10 files changed

+238
-0
lines changed

app/api/auth.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export function useApiAuth() {
2+
function updateProfile(payload: Partial<{ username: string, name: string, avatar: string }>) {
3+
return $api('/api/auth/me', {
4+
method: 'PATCH',
5+
body: payload,
6+
})
7+
}
8+
9+
function updatePassword(payload: Partial<{ password: string }>) {
10+
return $api('/api/auth/password', {
11+
method: 'POST',
12+
body: payload,
13+
})
14+
}
15+
16+
return {
17+
updateProfile,
18+
updatePassword,
19+
}
20+
}

app/api/casl.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export function useApiCasl() {
2+
function fetchScopes() {
3+
return $api('/api/scopes')
4+
}
5+
6+
return {
7+
fetchScopes,
8+
}
9+
}

app/api/credit.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type { User } from '@base/server/types/models'
2+
3+
export function useApiCredit() {
4+
function fetchCredit() {
5+
// Get user credit from our database instead of depending on Logto data
6+
return $api<{ data: User }>('/api/auth/me')
7+
}
8+
9+
return {
10+
fetchCredit,
11+
}
12+
}

app/api/health.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export function useApiHealth() {
2+
function fetchHealthCheck() {
3+
return $api<{ success: true }>('/api/health')
4+
}
5+
6+
return {
7+
fetchHealthCheck,
8+
}
9+
}

app/api/notification.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import type { ParsedFilterQuery } from '@base/server/utils/filter'
2+
3+
export function useApiNotification() {
4+
function fetchNotifications(query?: Partial<ParsedFilterQuery>) {
5+
return $api(`/api/notifications`, {
6+
query,
7+
})
8+
}
9+
10+
function markRead(id: string) {
11+
return $api(`/api/notifications/${id}`, {
12+
method: 'PATCH',
13+
body: {
14+
read_at: new Date(),
15+
},
16+
})
17+
}
18+
19+
function markUnread(id: string) {
20+
return $api(`/api/notifications/${id}`, {
21+
method: 'PATCH',
22+
body: {
23+
read_at: null,
24+
},
25+
})
26+
}
27+
28+
function markAllRead() {
29+
return $api(`/api/notifications/read`, {
30+
method: 'PATCH',
31+
})
32+
}
33+
34+
function markAllUnread() {
35+
return $api(`/api/notifications/unread`, {
36+
method: 'PATCH',
37+
})
38+
}
39+
40+
function deleteNotification(id: string) {
41+
return $api(`/api/notifications/${id}`, {
42+
method: 'DELETE',
43+
})
44+
}
45+
46+
function countUnreadNotifications() {
47+
return $api(`/api/notifications/unread`)
48+
}
49+
50+
function createTokenDevice(token: string) {
51+
return $api(`/api/devices`, {
52+
method: 'POST',
53+
body: { token },
54+
})
55+
}
56+
57+
function deleteTokenDevice(token: string) {
58+
return $api(`/api/devices`, {
59+
method: 'DELETE',
60+
body: { token },
61+
})
62+
}
63+
64+
function updateSettings(payload: Partial<{
65+
email: boolean | null
66+
desktop: boolean | null
67+
product_updates: boolean | null
68+
weekly_digest: boolean | null
69+
important_updates: boolean | null
70+
}>) {
71+
return $api('/api/auth/notification', {
72+
method: 'PATCH',
73+
body: payload,
74+
})
75+
}
76+
77+
return {
78+
fetchNotifications,
79+
markRead,
80+
markUnread,
81+
markAllRead,
82+
markAllUnread,
83+
deleteNotification,
84+
countUnreadNotifications,
85+
createTokenDevice,
86+
deleteTokenDevice,
87+
updateSettings,
88+
}
89+
}

app/api/payment.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import type { PaymentStatus } from '@base/server/db/schemas'
2+
3+
export function useApiPayment() {
4+
function checkout(type: 'payos' | 'vnpay' | 'sepay', productIdentifier: string) {
5+
if (type !== 'payos' && type !== 'vnpay' && type !== 'sepay')
6+
throw new Error('Invalid payment provider')
7+
8+
return $api<{
9+
data: {
10+
message: string
11+
paymentUrl: string
12+
}
13+
}>(`api/payments/${type}/checkout`, {
14+
method: 'POST',
15+
body: {
16+
productIdentifier,
17+
},
18+
})
19+
}
20+
21+
function checkStatus(type: 'payos' | 'vnpay' | 'sepay', description: string) {
22+
return $api<{
23+
data: {
24+
status: PaymentStatus
25+
}
26+
}>(`api/payments/${type}/status`, {
27+
params: {
28+
description,
29+
},
30+
})
31+
}
32+
33+
return {
34+
checkout,
35+
checkStatus,
36+
}
37+
}

app/api/product.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import type { productTable } from '@base/server/db/schemas'
2+
import type { InferSelectModel } from 'drizzle-orm'
3+
4+
export type Product = InferSelectModel<typeof productTable>
5+
6+
export function useApiProduct() {
7+
function fetchProducts() {
8+
return $api<{ data: Product[] }>('/api/products')
9+
}
10+
11+
function fetchCreditPackages() {
12+
return $api<{ data: Product[] }>('/api/products/credit-packages')
13+
}
14+
15+
return {
16+
fetchProducts,
17+
fetchCreditPackages,
18+
}
19+
}

app/api/s3.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export function useApiS3() {
2+
function getSignedUrl(filename: string) {
3+
return $api<{ uploadUrl: string, assetUrl: string }>('/api/s3', {
4+
method: 'PUT',
5+
body: { filename },
6+
})
7+
}
8+
9+
return {
10+
getSignedUrl,
11+
}
12+
}

app/api/stripe.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
export function useApiStripe() {
2+
function fetchStripeProducts() {
3+
return $api('/api/payments/stripe/products')
4+
}
5+
6+
function fetchStripePrices(productId: string) {
7+
return $api(`/api/payments/stripe/products/${productId}/prices`)
8+
}
9+
10+
function fetchStripeSubscription() {
11+
return $api('/api/payments/stripe/me')
12+
}
13+
14+
async function createSubscriptionCheckoutUrl(customerId: string, priceId: string) {
15+
return $api<{ url: string }>(`/api/payments/stripe/customers/${customerId}/checkout`, {
16+
method: 'POST',
17+
body: {
18+
priceId,
19+
redirectPath: '/settings/billing-plans',
20+
},
21+
})
22+
}
23+
24+
return {
25+
fetchStripeProducts,
26+
fetchStripePrices,
27+
fetchStripeSubscription,
28+
createSubscriptionCheckoutUrl,
29+
}
30+
}

nuxt.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ export default defineNuxtConfig({
150150
dirs: [
151151
fileURLToPath(new URL('./app/@core/utils', import.meta.url)),
152152
fileURLToPath(new URL('./app/@core/composable', import.meta.url)),
153+
fileURLToPath(new URL('./app/api', import.meta.url)),
153154
],
154155
},
155156

0 commit comments

Comments
 (0)