Skip to content

Commit

Permalink
Refactor native-http port
Browse files Browse the repository at this point in the history
  • Loading branch information
mxthevs authored and fdaciuk committed Dec 1, 2021
1 parent bda7e69 commit 86d1aa1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 31 deletions.
11 changes: 8 additions & 3 deletions src/ports/native-http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import { app } from './server'
const PORT = env('PORT')

export function start () {
http
.createServer(app)
.listen(PORT, () => console.log(`Server is listening on port ${PORT}`))
return new Promise((resolve) => {
http
.createServer(app)
.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`)
resolve(null)
})
})
}
12 changes: 6 additions & 6 deletions src/ports/native-http/middlewares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export const withAuth: WithAuthMiddleware = (handler) => {
pipe(
authMiddleware(request.headers.authorization),
TE.map(
async (payload) => {
(payload) => {
request.auth = payload
await handler(request, response)
return handler(request, response)
},
),
TE.mapLeft(
Expand All @@ -26,15 +26,15 @@ export const withTryAuth: WithAuthMiddleware = (handler) => {
pipe(
authMiddleware(request.headers.authorization),
TE.map(
async (payload) => {
(payload) => {
request.auth = payload
await handler(request, response)
return handler(request, response)
},
),
TE.mapLeft(
async () => {
() => {
request.auth = undefined
await handler(request, response)
return handler(request, response)
},
),
)()
Expand Down
25 changes: 11 additions & 14 deletions src/ports/native-http/modules/article.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pipe } from 'fp-ts/function'
import { pipe, identity } from 'fp-ts/function'
import * as E from 'fp-ts/Either'
import * as TE from 'fp-ts/TaskEither'
import * as article from '@/ports/adapters/http/modules/article'
Expand All @@ -16,10 +16,7 @@ const getPropFromRequestBody: GetPropFromRequestBody = (data, prop) => {
() => JSON.parse(data),
E.toError,
),
E.fold(
(error) => error,
(body) => body[prop] || {},
),
E.fold(identity, (body) => body[prop] ?? {}),
)
}

Expand Down Expand Up @@ -48,7 +45,7 @@ const articleRoutes: Routes = {

pipe(
article.fetchArticle({
slug: request.params![slug] ?? '',
slug: request.params?.[slug] ?? '',
userId: payload.id,
}),
TE.map(result => httpResponse(response, result)),
Expand Down Expand Up @@ -76,7 +73,7 @@ const articleRoutes: Routes = {
for await (const body of request) {
const data: UpdateArticle = {
...getPropFromRequestBody<UpdateArticle>(body, 'article'),
slug: request.params![slug] as Slug,
slug: request.params?.[slug] as Slug,
authorId: payload.id,
}

Expand All @@ -95,7 +92,7 @@ const articleRoutes: Routes = {

pipe(
article.deleteArticle({
slug: request.params![slug] ?? '',
slug: request.params?.[slug] ?? '',
userId: payload.id,
}),
TE.map(() => httpResponse(response)),
Expand Down Expand Up @@ -123,7 +120,7 @@ const articleRoutes: Routes = {
pipe(
article.favoriteArticle({
userId: payload.id,
slug: request.params![slug] ?? '',
slug: request.params?.[slug] ?? '',
}),
TE.map(result => httpResponse(response, result)),
TE.mapLeft(result => httpResponse(response, result.error, result.code)),
Expand All @@ -137,7 +134,7 @@ const articleRoutes: Routes = {
pipe(
article.unfavoriteArticle({
userId: payload.id,
slug: request.params![slug] ?? '',
slug: request.params?.[slug] ?? '',
}),
TE.map(result => httpResponse(response, result)),
TE.mapLeft(result => httpResponse(response, result.error, result.code)),
Expand All @@ -152,7 +149,7 @@ const articleRoutes: Routes = {
const data: CreateComment = {
...getPropFromRequestBody<CreateComment>(body, 'comment'),
authorId: payload.id,
articleSlug: request.params![slug] as Slug,
articleSlug: request.params?.[slug] as Slug,
}

pipe(
Expand All @@ -169,7 +166,7 @@ const articleRoutes: Routes = {
const slug = 'slug'

const data = {
slug: request.params![slug] ?? '',
slug: request.params?.[slug] ?? '',
userId: payload.id,
}

Expand All @@ -187,8 +184,8 @@ const articleRoutes: Routes = {
const id = 'id'

const data = {
commentId: Number(request.params![id]),
slug: request.params![slug] ?? '',
commentId: Number(request.params?.[id]),
slug: request.params?.[slug] ?? '',
userId: payload.id,
}

Expand Down
6 changes: 3 additions & 3 deletions src/ports/native-http/modules/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const profileRoutes: Routes = {

pipe(
user.getProfile({
username: request.params![username] ?? '',
username: request.params?.[username] ?? '',
userId: payload.id,
}),
TE.map(result => httpResponse(response, result)),
Expand All @@ -26,7 +26,7 @@ const profileRoutes: Routes = {

pipe(
user.followUser({
userToFollow: request.params![username] ?? '',
userToFollow: request.params?.[username] ?? '',
userId: payload.id,
}),
TE.map(result => httpResponse(response, result)),
Expand All @@ -40,7 +40,7 @@ const profileRoutes: Routes = {

pipe(
user.unfollowUser({
userToUnfollow: request.params![username] ?? '',
userToUnfollow: request.params?.[username] ?? '',
userId: payload.id,
}),
TE.map(result => httpResponse(response, result)),
Expand Down
7 changes: 2 additions & 5 deletions src/ports/native-http/modules/user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pipe } from 'fp-ts/function'
import { pipe, identity } from 'fp-ts/function'
import * as E from 'fp-ts/Either'
import * as TE from 'fp-ts/TaskEither'
import * as user from '@/ports/adapters/http/modules/user'
Expand All @@ -14,10 +14,7 @@ const getUserFromRequestBody: GetUserFromRequestBody = (data) => {
() => JSON.parse(data),
E.toError,
),
E.fold(
(error) => error,
(body) => body.user || {},
),
E.fold(identity, (body) => body.user ?? {}),
)
}

Expand Down

0 comments on commit 86d1aa1

Please sign in to comment.