Skip to content

Commit cc9ed84

Browse files
RohitRajendranpieh
andauthored
fix: support api routes at index of folder (#2101)
* feat: support finding source file when index of folder * chore: add tests * fix: fix prettier error * Update test/helpers/files.spec.ts Co-authored-by: Michal Piechowiak <[email protected]> --------- Co-authored-by: Michal Piechowiak <[email protected]>
1 parent 2eba019 commit cc9ed84

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

packages/runtime/src/helpers/files.ts

+5
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,11 @@ export const getSourceFileForPage = (page: string, roots: string[], pageExtensio
353353
if (existsSync(file)) {
354354
return file
355355
}
356+
357+
const fileAtFolderIndex = join(root, page, `index.${extension}`)
358+
if (existsSync(fileAtFolderIndex)) {
359+
return fileAtFolderIndex
360+
}
356361
}
357362
}
358363
console.log('Could not find source file for page', page)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// noop

test/helpers/files.spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,13 @@ describe('getSourceFileForPage', () => {
237237

238238
expect(filePath.replace(TEST_DIR, '')).toBe('/fixtures/page-extensions/custom/pages/api/custom.api.js')
239239
})
240+
241+
it('handles getting file when index of folder', () => {
242+
const pagesDir = resolve(__dirname, '../fixtures/page-extensions/index/pages')
243+
const apiRoute = '/api/index'
244+
245+
const filePath = getSourceFileForPage(apiRoute, [pagesDir])
246+
247+
expect(filePath.replace(TEST_DIR, '')).toBe('/fixtures/page-extensions/index/pages/api/index/index.js')
248+
})
240249
})

0 commit comments

Comments
 (0)