Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions apps/web/lib/api/auth-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import type { AuthResponse, LoginRequest, RegisterRequest, User } from '@repo/types'

import { client } from './client'
import { ApiResponse } from './types'

type RegisterResponse = AuthResponse
type LoginResponse = AuthResponse
type RefreshResponse = AuthResponse

const ENDPOINT = '/auth'

export const authService = {
register: async (body: RegisterRequest): Promise<RegisterResponse> => {
const response = await client.post<
RegisterResponse,
ApiResponse<RegisterResponse>,
RegisterRequest
>(`${ENDPOINT}/register`, body)

return response.data
},

login: async (body: LoginRequest): Promise<LoginResponse> => {
const response = await client.post<
LoginResponse,
ApiResponse<LoginResponse>,
LoginRequest
>(`${ENDPOINT}/login`, body)

return response.data
},

refresh: async (): Promise<RefreshResponse> => {
const response = await client.post<RefreshResponse>(`${ENDPOINT}/refresh`)

return response.data
},

logout: async () => {
return await client.post(`${ENDPOINT}/logout`)
},

getMe: async (): Promise<User> => {
const response = await client.get<User>(`${ENDPOINT}/me`)

return response.data
},
}
1 change: 1 addition & 0 deletions apps/web/lib/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const client = axios.create({
'Content-Type': 'application/json',
},
timeout: 10000,
withCredentials: true,
})

// Request interceptor
Expand Down
13 changes: 6 additions & 7 deletions apps/web/lib/api/example-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import { client } from './client'
import {
ApiResponse,
BaseEntity,
CreateDto,
PaginatedResponse,
Expand Down Expand Up @@ -35,18 +34,18 @@ export const exampleService = {
},

getById: async (id: string): Promise<Example> => {
const response = await client.get<ApiResponse<Example>>(`${ENDPOINT}/${id}`)
return response.data.data
const response = await client.get<Example>(`${ENDPOINT}/${id}`)
return response.data
},

create: async (data: CreateExampleDto): Promise<Example> => {
const response = await client.post<ApiResponse<Example>>(ENDPOINT, data)
return response.data.data
const response = await client.post<Example>(ENDPOINT, data)
return response.data
},

update: async (id: string, data: UpdateExampleDto): Promise<Example> => {
const response = await client.patch<ApiResponse<Example>>(`${ENDPOINT}/${id}`, data)
return response.data.data
const response = await client.patch<Example>(`${ENDPOINT}/${id}`, data)
return response.data
},

delete: async (id: string): Promise<void> => {
Expand Down
9 changes: 4 additions & 5 deletions apps/web/lib/api/types.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { AxiosResponse } from 'axios'

// Базовый тип ошибки API
export interface ApiError {
message: string
statusCode: number
error: string
error?: string
}

// Обёртка для одиночного ответа
export interface ApiResponse<T> {
data: T
message?: string
}
export type ApiResponse<T> = AxiosResponse<T>

// Пагинированный ответ
export interface PaginatedResponse<T> {
Expand Down
4 changes: 4 additions & 0 deletions apps/web/lib/api/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { ApiError } from './types'

export const isApiError = (err: unknown): err is ApiError =>
typeof err === 'object' && err !== null && 'message' in err && 'statusCode' in err
7 changes: 7 additions & 0 deletions packages/types/src/auth/schema/auth-response.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { z } from 'zod'

export const authResponseSchema = z.object({
accessToken: z.string(),
})

export type AuthResponse = z.infer<typeof authResponseSchema>
2 changes: 2 additions & 0 deletions packages/types/src/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export type {
} from './auth/schema/register-request.schema'
export { loginRequestSchema } from './auth/schema/login-request.schema'
export type { LoginRequest } from './auth/schema/login-request.schema'
export { authResponseSchema } from './auth/schema/auth-response.schema'
export type { AuthResponse } from './auth/schema/auth-response.schema'
Loading