-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref(fixtures): allow generating router for different user types (#574)
* ref(fixtures): allow generating router for different user types * fix: switch to specifying the siteName directly * feat: allow adding a default user session
- Loading branch information
1 parent
77d780d
commit d9c6159
Showing
7 changed files
with
192 additions
and
33 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,142 @@ | ||
import cookieParser from "cookie-parser" | ||
import express, { Express } from "express" | ||
import _ from "lodash" | ||
|
||
import { errorHandler } from "@middleware/errorHandler" | ||
|
||
import GithubSessionData from "@classes/GithubSessionData" | ||
import UserSessionData from "@classes/UserSessionData" | ||
import UserWithSiteSessionData from "@classes/UserWithSiteSessionData" | ||
|
||
import { RequestHandler } from "@root/types" | ||
|
||
import { | ||
mockUserSessionData, | ||
mockUserWithSiteSessionData, | ||
mockGithubSessionData, | ||
MOCK_USER_SESSION_DATA_ONE, | ||
} from "./sessionData" | ||
import { MOCK_SITE_NAME_ONE } from "./sites" | ||
|
||
/** | ||
* @deprecated | ||
*/ | ||
const attachSessionData: RequestHandler< | ||
unknown, | ||
unknown, | ||
unknown, | ||
unknown, | ||
{ | ||
userSessionData: UserSessionData | ||
userWithSiteSessionData: UserWithSiteSessionData | ||
githubSessionData: GithubSessionData | ||
} | ||
> = (req, res, next) => { | ||
res.locals.userSessionData = mockUserSessionData | ||
res.locals.userWithSiteSessionData = mockUserWithSiteSessionData | ||
res.locals.githubSessionData = mockGithubSessionData | ||
next() | ||
} | ||
|
||
const attachUserSessionData: ( | ||
userSessionData: UserSessionData | ||
) => RequestHandler< | ||
unknown, | ||
unknown, | ||
unknown, | ||
unknown, | ||
{ userSessionData: UserSessionData } | ||
> = (userSessionData) => (req, res, next) => { | ||
res.locals.userSessionData = userSessionData | ||
next() | ||
} | ||
|
||
const attachUserSessionDataWithSite: ( | ||
userSessionData: UserSessionData, | ||
siteName: string | ||
) => RequestHandler< | ||
unknown, | ||
unknown, | ||
unknown, | ||
unknown, | ||
{ | ||
userSessionData: UserSessionData | ||
userWithSiteSessionData: UserWithSiteSessionData | ||
} | ||
> = (userSessionData, siteName) => (req, res, next) => { | ||
const userWithSiteSessionData = new UserWithSiteSessionData({ | ||
isomerUserId: userSessionData.isomerUserId, | ||
email: userSessionData.email, | ||
siteName, | ||
}) | ||
res.locals.userSessionData = userSessionData | ||
res.locals.userWithSiteSessionData = userWithSiteSessionData | ||
next() | ||
} | ||
|
||
const attachDefaultUserSessionData: RequestHandler< | ||
unknown, | ||
unknown, | ||
unknown, | ||
unknown, | ||
{ userSessionData: UserSessionData } | ||
> = attachUserSessionData(MOCK_USER_SESSION_DATA_ONE) | ||
|
||
const attachDefaultUserSessionDataWithSite: RequestHandler< | ||
unknown, | ||
unknown, | ||
unknown, | ||
unknown, | ||
{ | ||
userSessionData: UserSessionData | ||
userWithSiteSessionData: UserWithSiteSessionData | ||
} | ||
> = attachUserSessionDataWithSite( | ||
MOCK_USER_SESSION_DATA_ONE, | ||
MOCK_SITE_NAME_ONE | ||
) | ||
|
||
/** | ||
* @deprecated | ||
*/ | ||
export const generateRouter = (router: Express) => { | ||
const app = express() | ||
app.use(express.json({ limit: "7mb" })) | ||
app.use(express.urlencoded({ extended: false })) | ||
app.use(cookieParser()) | ||
app.use(attachSessionData) | ||
app.use(router) | ||
app.use(errorHandler) | ||
return app | ||
} | ||
|
||
const generateFinalRouter = (router: Express) => { | ||
const app = express() | ||
app.use(express.json({ limit: "7mb" })) | ||
app.use(express.urlencoded({ extended: false })) | ||
app.use(cookieParser()) | ||
app.use(router) | ||
app.use(errorHandler) | ||
return app | ||
} | ||
|
||
export const generateRouterForUser = ( | ||
router: Express, | ||
userSessionData: UserSessionData | ||
) => { | ||
const app = express() | ||
app.use(attachUserSessionData(userSessionData)) | ||
app.use(router) | ||
return generateFinalRouter(app) | ||
} | ||
|
||
export const generateRouterForUserWithSite = ( | ||
router: Express, | ||
userSessionData: UserSessionData, | ||
siteName: string | ||
) => { | ||
const app = express() | ||
app.use(attachUserSessionDataWithSite(userSessionData, siteName)) | ||
app.use(router) | ||
return generateFinalRouter(app) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const MOCK_SITE_NAME_ONE = "test-site-one" | ||
export const MOCK_SITE_NAME_TWO = "test-site-two" |
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,9 @@ | ||
export const MOCK_USER_ID_ONE = 1 | ||
export const MOCK_USER_ID_TWO = 2 | ||
export const MOCK_USER_ID_THREE = 3 | ||
export const MOCK_USER_ID_FOUR = 4 | ||
|
||
export const MOCK_USER_EMAIL_ONE = "[email protected]" | ||
export const MOCK_USER_EMAIL_TWO = "[email protected]" | ||
export const MOCK_USER_EMAIL_THREE = "[email protected]" | ||
export const MOCK_USER_EMAIL_FOUR = "[email protected]" |