-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from fiit-tp7-2023/#287-prepare-api-endpoints
AB#287 Prepare api endpoints
- Loading branch information
Showing
30 changed files
with
555 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,5 @@ | ||
# REST API configuration | ||
REST_API_URL= | ||
|
||
# Auth configuration - required in production | ||
AUTH_ORIGIN= | ||
AUTH_SECRET= | ||
|
||
# Google configuration | ||
GOOGLE_CLIENT_ID= | ||
GOOGLE_CLIENT_SECRET= | ||
GOOGLE_REFRESH_TOKEN_URL= | ||
|
||
# .NET backend runs locally on HTTPS (must be set to 0), for production must be set to 1 | ||
NODE_TLS_REJECT_UNAUTHORIZED= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { useAuthService } from '~/server/services/auth.service'; | ||
import { VerifyNonce } from '~/types/auth'; | ||
|
||
export default defineEventHandler(async (event) => { | ||
const { signature, address } = await readBody<VerifyNonce>(event); | ||
const service = useAuthService(); | ||
return await service.verifyNonce(signature, address); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { useAuthService } from '~/server/services/auth.service'; | ||
import { GetNonceDTO } from '~/types/auth'; | ||
|
||
export default defineEventHandler(async (event) => { | ||
const { address } = await readBody<GetNonceDTO>(event); | ||
const service = useAuthService(); | ||
return await service.getNonce(address); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { useAuthService } from '~/server/services/auth.service'; | ||
import { AuthenticatedUser, RefreshTokenRequestDTO } from '~/types/auth'; | ||
|
||
type Body = RefreshTokenRequestDTO & AuthenticatedUser; | ||
|
||
export default defineEventHandler(async (event) => { | ||
const { refreshToken: refToken, jwt } = await readBody<Body>(event); | ||
const service = useAuthService(jwt); | ||
return await service.refreshToken(refToken); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { useCommentService } from '~/server/services/comment.service'; | ||
import { AuthenticatedUser } from '~/types/auth'; | ||
|
||
type Body = AuthenticatedUser; | ||
|
||
export default defineEventHandler(async (event) => { | ||
const { jwt } = await readBody<Body>(event); | ||
const commentId = getRouterParam(event, 'id'); | ||
if (Number.isNaN(commentId)) { | ||
return createError({ | ||
message: 'Invalid comment id', | ||
}); | ||
} | ||
const service = useCommentService(jwt); | ||
return await service.remove(Number(commentId)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { useCommentService } from '~/server/services/comment.service'; | ||
import { AuthenticatedUser } from '~/types/auth'; | ||
import { UpdateCommentDTO } from '~/types/dtos'; | ||
|
||
type Body = AuthenticatedUser & UpdateCommentDTO; | ||
|
||
export default defineEventHandler(async (event) => { | ||
const { jwt, content } = await readBody<Body>(event); | ||
const commentId = getRouterParam(event, 'id'); | ||
if (Number.isNaN(commentId)) { | ||
return createError({ | ||
message: 'Invalid comment id', | ||
}); | ||
} | ||
const service = useCommentService(jwt); | ||
return await service.update(Number(commentId), content); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { useCommentService } from '~/server/services/comment.service'; | ||
import { AuthenticatedUser } from '~/types/auth'; | ||
|
||
export default defineEventHandler(async (event) => { | ||
const { jwt } = await readBody<AuthenticatedUser>(event); | ||
const commentId = getRouterParam(event, 'id'); | ||
if (Number.isNaN(commentId)) { | ||
return createError({ | ||
message: 'Invalid comment id', | ||
}); | ||
} | ||
|
||
const service = useCommentService(jwt); | ||
return await service.unlike(Number(commentId)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { useCommentService } from '~/server/services/comment.service'; | ||
import { AuthenticatedUser } from '~/types/auth'; | ||
|
||
export default defineEventHandler(async (event) => { | ||
const { jwt } = await readBody<AuthenticatedUser>(event); | ||
const commentId = getRouterParam(event, 'id'); | ||
if (Number.isNaN(commentId)) { | ||
return createError({ | ||
message: 'Invalid comment id', | ||
}); | ||
} | ||
|
||
const service = useCommentService(jwt); | ||
return await service.getLikes(Number(commentId)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { useCommentService } from '~/server/services/comment.service'; | ||
import { AuthenticatedUser } from '~/types/auth'; | ||
|
||
export default defineEventHandler(async (event) => { | ||
const { jwt } = await readBody<AuthenticatedUser>(event); | ||
const commentId = getRouterParam(event, 'id'); | ||
if (Number.isNaN(commentId)) { | ||
return createError({ | ||
message: 'Invalid comment id', | ||
}); | ||
} | ||
|
||
const service = useCommentService(jwt); | ||
return await service.like(Number(commentId)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { useCommentService } from '~/server/services/comment.service'; | ||
import { AuthenticatedUser } from '~/types/auth'; | ||
import { AddCommentDTO, NFTAddress } from '~/types/dtos'; | ||
|
||
type Body = AuthenticatedUser & AddCommentDTO & NFTAddress; | ||
|
||
export default defineEventHandler(async (event) => { | ||
const { jwt, content, parentCommentId, address } = await readBody<Body>(event); | ||
const service = useCommentService(jwt); | ||
return await service.add(address, content, parentCommentId); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { usePostService } from '~/server/services/post.service'; | ||
import { AuthenticatedUser } from '~/types/auth'; | ||
|
||
export default defineEventHandler(async (event) => { | ||
const { jwt } = await readBody<AuthenticatedUser>(event); | ||
const nftAddress = getRouterParam(event, 'id'); | ||
if (!nftAddress) { | ||
return createError({ | ||
message: 'Invalid NFT address', | ||
}); | ||
} | ||
|
||
const service = usePostService(jwt); | ||
return await service.unlike(nftAddress); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { usePostService } from '~/server/services/post.service'; | ||
import { AuthenticatedUser } from '~/types/auth'; | ||
|
||
export default defineEventHandler(async (event) => { | ||
const { jwt } = await readBody<AuthenticatedUser>(event); | ||
const nftAddress = getRouterParam(event, 'id'); | ||
if (!nftAddress) { | ||
return createError({ | ||
message: 'Invalid NFT address', | ||
}); | ||
} | ||
|
||
const service = usePostService(jwt); | ||
return await service.getLikes(nftAddress); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { usePostService } from '~/server/services/post.service'; | ||
import { AuthenticatedUser } from '~/types/auth'; | ||
|
||
export default defineEventHandler(async (event) => { | ||
const { jwt } = await readBody<AuthenticatedUser>(event); | ||
const nftAddress = getRouterParam(event, 'id'); | ||
if (!nftAddress) { | ||
return createError({ | ||
message: 'Invalid NFT address', | ||
}); | ||
} | ||
|
||
const service = usePostService(jwt); | ||
return await service.like(nftAddress); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { usePostService } from '~/server/services/post.service'; | ||
import { AuthenticatedUser } from '~/types/auth'; | ||
import { PaginationDTO } from '~/types/dtos'; | ||
|
||
export default defineEventHandler(async (event) => { | ||
const { jwt } = await readBody<AuthenticatedUser>(event); | ||
const { pageNumber, pageSize } = getQuery<PaginationDTO>(event); | ||
|
||
const service = usePostService(jwt); | ||
return await service.getMyPosts(pageNumber, pageSize); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { ServerErrorType } from '~/types/error'; | ||
|
||
export class ServerError { | ||
static notFound(_reason: string) { | ||
return createError({ | ||
status: ServerErrorType.NOT_FOUND, | ||
statusText: 'Not found', | ||
statusMessage: _reason, | ||
}); | ||
} | ||
|
||
static unauthorized(_reason: string) { | ||
return createError({ | ||
status: ServerErrorType.UNAUTHORIZED, | ||
statusText: 'Unauthorized', | ||
statusMessage: _reason, | ||
}); | ||
} | ||
|
||
static forbidden(_reason: string) { | ||
return createError({ | ||
status: ServerErrorType.FORBIDDEN, | ||
statusText: 'Forbidden', | ||
statusMessage: _reason, | ||
}); | ||
} | ||
|
||
static internalServerError(_reason: string) { | ||
return createError({ | ||
status: ServerErrorType.INTERNAL_SERVER_ERROR, | ||
statusText: 'Internal server error', | ||
statusMessage: _reason, | ||
}); | ||
} | ||
|
||
static unavailable() { | ||
return createError({ | ||
status: ServerErrorType.UNAVAILABLE, | ||
statusText: 'Service unavailable', | ||
}); | ||
} | ||
|
||
static fromCode(code: number, _reason: string) { | ||
switch (code) { | ||
case 401: | ||
return ServerError.unauthorized(_reason); | ||
case 403: | ||
return ServerError.forbidden(_reason); | ||
case 404: | ||
return ServerError.notFound(_reason); | ||
default: | ||
return ServerError.internalServerError(_reason); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { useApi } from '../utils/api'; | ||
import { GetNonceDTO, RefreshTokenDTO, RefreshTokenRequestDTO, VerifyNonce } from '~/types/auth'; | ||
|
||
export function useAuthService(token?: string) { | ||
const getNonce = async (address: string): Promise<string> => | ||
await useApi<string, GetNonceDTO>('auth/nonce-message', undefined, { | ||
method: 'POST', | ||
body: { | ||
address, | ||
}, | ||
}); | ||
|
||
const verifyNonce = async (signature: string, address: string): Promise<RefreshTokenDTO> => { | ||
const response = await useApi<RefreshTokenDTO, VerifyNonce>('auth/login', undefined, { | ||
method: 'POST', | ||
body: { | ||
signature, | ||
address, | ||
}, | ||
}); | ||
return response; | ||
}; | ||
|
||
const refreshToken = async (refreshToken: string): Promise<RefreshTokenDTO> => { | ||
if (!token) { | ||
throw new Error('Missing user token'); | ||
} | ||
const response = await useApi<RefreshTokenDTO, RefreshTokenRequestDTO>('auth/refresh', token, { | ||
method: 'POST', | ||
body: { | ||
refreshToken, | ||
}, | ||
}); | ||
return response; | ||
}; | ||
|
||
return { | ||
getNonce, | ||
verifyNonce, | ||
refreshToken, | ||
}; | ||
} |
Oops, something went wrong.