-
Notifications
You must be signed in to change notification settings - Fork 4
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 #64 from kenneth-gray/refactor-server
Refactor business logic so that express can be extracted to the edge
- Loading branch information
Showing
17 changed files
with
979 additions
and
691 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
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 |
---|---|---|
@@ -1,77 +1,188 @@ | ||
import { RequestHandler, Response, Request } from 'express'; | ||
import { ScenarioMap } from './types'; | ||
import { getScenarios as utilsGetScenarios } from './utils/get-scenarios'; | ||
import { getScenarioIdsFromCookie } from './cookies'; | ||
import { | ||
Context, | ||
DefaultScenario, | ||
GetCookie, | ||
Result, | ||
ScenarioMap, | ||
SetCookie, | ||
} from './types'; | ||
import { getAllScenarios } from './utils/get-all-scenarios'; | ||
import { getContextFromScenarios } from './utils/get-context-from-scenarios'; | ||
import { updateScenariosAndContext } from './utils/update-scenarios-and-context'; | ||
|
||
export { modifyScenarios, resetScenarios, getScenarios }; | ||
export { resetScenarios, modifyScenarios, getScenarios }; | ||
|
||
function resetScenarios({ | ||
setCookie, | ||
setServerContext, | ||
setServerSelectedScenarioIds, | ||
defaultScenario, | ||
scenarioMap, | ||
cookieMode, | ||
}: { | ||
setCookie: SetCookie; | ||
setServerContext: (context: Context) => void; | ||
setServerSelectedScenarioIds: (selectedScenarioIds: string[]) => void; | ||
defaultScenario: DefaultScenario; | ||
scenarioMap: ScenarioMap; | ||
cookieMode: boolean; | ||
}): Result { | ||
updateScenariosAndContext({ | ||
updatedScenarioIds: [], | ||
setCookie, | ||
setServerContext, | ||
setServerSelectedScenarioIds, | ||
defaultScenario, | ||
scenarioMap, | ||
cookieMode, | ||
}); | ||
|
||
return { | ||
status: 204, | ||
}; | ||
} | ||
|
||
function getScenarios({ | ||
getCookie, | ||
setCookie, | ||
getServerSelectedScenarioIds, | ||
cookieMode, | ||
defaultScenario, | ||
scenarioMap, | ||
}: { | ||
getCookie: GetCookie; | ||
setCookie: SetCookie; | ||
getServerSelectedScenarioIds: () => string[]; | ||
cookieMode: boolean; | ||
defaultScenario: DefaultScenario; | ||
scenarioMap: ScenarioMap; | ||
}): Result { | ||
const allScenarios = getAllScenarios( | ||
scenarioMap, | ||
getSelectedScenarioIds({ | ||
getCookie, | ||
setCookie, | ||
getServerSelectedScenarioIds, | ||
cookieMode, | ||
defaultScenario, | ||
}), | ||
); | ||
|
||
return { | ||
status: 200, | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
response: allScenarios, | ||
}; | ||
} | ||
|
||
function modifyScenarios({ | ||
scenarioNames, | ||
updatedScenarioIds, | ||
scenarioIds, | ||
scenarioMap, | ||
updateScenariosAndContext, | ||
cookieMode, | ||
defaultScenario, | ||
setCookie, | ||
setServerContext, | ||
setServerSelectedScenarioIds, | ||
}: { | ||
scenarioNames: string[]; | ||
updatedScenarioIds: unknown; | ||
scenarioIds: string[]; | ||
scenarioMap: ScenarioMap; | ||
updateScenariosAndContext: (res: Response, scenarios: string[]) => void; | ||
}): RequestHandler { | ||
return ({ body: { scenarios: scenariosBody } }: Request, res: Response) => { | ||
if (!Array.isArray(scenariosBody)) { | ||
res.status(400).json({ | ||
cookieMode: boolean; | ||
defaultScenario: DefaultScenario; | ||
setCookie: SetCookie; | ||
setServerContext: (context: Context) => void; | ||
setServerSelectedScenarioIds: (selectedScenarioIds: string[]) => void; | ||
}) { | ||
if (!isStringArray(updatedScenarioIds)) { | ||
return { | ||
status: 400, | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
response: { | ||
message: | ||
'"scenarios" must be an array of scenario names (empty array allowed)', | ||
}); | ||
return; | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
const scenariosByGroup: { [key: string]: number } = {}; | ||
for (const scenario of scenariosBody) { | ||
if (!scenarioNames.includes(scenario)) { | ||
res.status(400).json({ | ||
const scenariosByGroup: Record<string, string> = {}; | ||
for (const scenario of updatedScenarioIds) { | ||
if (!scenarioIds.includes(scenario)) { | ||
return { | ||
status: 400, | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
response: { | ||
message: `Scenario "${scenario}" does not exist`, | ||
}); | ||
return; | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
const scenarioMock = scenarioMap[scenario]; | ||
if (!Array.isArray(scenarioMock) && scenarioMock.group) { | ||
const { group } = scenarioMock; | ||
if (scenariosByGroup[group]) { | ||
res.status(400).json({ | ||
const scenarioMock = scenarioMap[scenario]; | ||
if (!Array.isArray(scenarioMock) && scenarioMock.group) { | ||
const { group } = scenarioMock; | ||
if (scenariosByGroup[group]) { | ||
return { | ||
status: 400, | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
response: { | ||
message: `Scenario "${scenario}" cannot be selected, because scenario "${scenariosByGroup[group]}" from group "${group}" has already been selected`, | ||
}); | ||
return; | ||
} | ||
|
||
scenariosByGroup[group] = scenario; | ||
}, | ||
}; | ||
} | ||
|
||
scenariosByGroup[group] = scenario; | ||
} | ||
} | ||
|
||
updateScenariosAndContext(res, scenariosBody); | ||
updateScenariosAndContext({ | ||
cookieMode, | ||
defaultScenario, | ||
updatedScenarioIds, | ||
scenarioMap, | ||
setCookie, | ||
setServerContext, | ||
setServerSelectedScenarioIds, | ||
}); | ||
|
||
res.sendStatus(204); | ||
}; | ||
return { status: 204 }; | ||
} | ||
|
||
function resetScenarios({ | ||
updateScenariosAndContext, | ||
}: { | ||
updateScenariosAndContext: (res: Response, scenarios: string[]) => void; | ||
}): RequestHandler { | ||
return (_, res: Response) => { | ||
updateScenariosAndContext(res, []); | ||
res.sendStatus(204); | ||
}; | ||
function isStringArray(value: unknown): value is string[] { | ||
return Array.isArray(value) && value.every(item => typeof item === 'string'); | ||
} | ||
|
||
function getScenarios({ | ||
scenarioMap, | ||
getScenarioNames, | ||
function getSelectedScenarioIds({ | ||
getCookie, | ||
setCookie, | ||
getServerSelectedScenarioIds, | ||
cookieMode, | ||
defaultScenario, | ||
}: { | ||
scenarioMap: ScenarioMap; | ||
getScenarioNames: (req: Request, res: Response) => string[]; | ||
}): RequestHandler { | ||
return (req: Request, res: Response) => { | ||
const data = utilsGetScenarios(scenarioMap, getScenarioNames(req, res)); | ||
getCookie: GetCookie; | ||
setCookie: SetCookie; | ||
getServerSelectedScenarioIds: () => string[]; | ||
cookieMode: boolean; | ||
defaultScenario: DefaultScenario; | ||
}) { | ||
if (cookieMode) { | ||
return getScenarioIdsFromCookie({ | ||
getCookie, | ||
setCookie, | ||
defaultValue: { | ||
context: getContextFromScenarios([defaultScenario]), | ||
scenarios: [], | ||
}, | ||
}); | ||
} | ||
|
||
res.json(data); | ||
}; | ||
return getServerSelectedScenarioIds(); | ||
} |
Oops, something went wrong.