Skip to content

Commit

Permalink
fix: make it faster
Browse files Browse the repository at this point in the history
  • Loading branch information
dadamssg committed Feb 24, 2023
1 parent 2fdf365 commit 417cedf
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/api-route-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,17 @@ export default function (app, options = {}) {
const responseStore = createActivationStore()
const customResponseStore = createCustomResponseStore()

const fileRoutes = getFileRoutes(options)

function getAllRouteResponses (routeId) {
const fileRoute = getFileRoutes(options).find(r => r.id === routeId)
const fileRoute = fileRoutes.find(r => r.id === routeId)
const fileResponses = getRouteResponses(options, fileRoute)
const customResponses = customResponseStore.getResponses(routeId)
return fileResponses.concat(customResponses)
}

// create routes from files
getFileRoutes(options).forEach(route => {
fileRoutes.forEach(route => {
// create express route
const appRoute = app.route(route.path)
// add methods to route
Expand Down Expand Up @@ -87,7 +89,6 @@ export default function (app, options = {}) {
warnDuplicateRoutes(app)

app.get('/_api', (req, res) => {
const fileRoutes = getFileRoutes(options)
const routes = getAPIRoutes(app)
.map(r => {
// attempt to find route file for route
Expand Down Expand Up @@ -250,7 +251,7 @@ export default function (app, options = {}) {

app.get('/_route/:routeId/responses', (req, res) => {
const routeId = req.params.routeId
const fileRoute = getFileRoutes(options).find(r => r.id === routeId) || {}
const fileRoute = fileRoutes.find(r => r.id === routeId) || {}
const responses = getAllRouteResponses(routeId)
const responseId = responseStore.getActivatedResponseId(fileRoute.id)
return res.json({
Expand All @@ -261,7 +262,7 @@ export default function (app, options = {}) {

app.get('/_route/:routeId/responses/:respId/activate', (req, res) => {
const {routeId, respId} = req.params
const fileRoute = getFileRoutes(options).find(r => r.id === routeId)
const fileRoute = fileRoutes.find(r => r.id === routeId)
const responses = getAllRouteResponses(routeId)
if (fileRoute && responses[respId]) {
responseStore.setActiveResponse(routeId, respId)
Expand All @@ -274,7 +275,7 @@ export default function (app, options = {}) {

app.get('/_route/:routeId/responses/deactivate', (req, res) => {
const {routeId} = req.params
const fileRoute = getFileRoutes(options).find(r => r.id === routeId)
const fileRoute = fileRoutes.find(r => r.id === routeId)
responseStore.setActiveResponse(routeId, null)
return res.json({
route: fileRoute,
Expand Down

0 comments on commit 417cedf

Please sign in to comment.