Skip to content

Commit

Permalink
Merge pull request #187 from storyofams/beta
Browse files Browse the repository at this point in the history
Release v1.5.5
  • Loading branch information
ggurkal authored Jul 3, 2021
2 parents babb038 + 3c4d562 commit 5f56ba3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
35 changes: 34 additions & 1 deletion lib/internals/parseRequestUrl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('parseRequestUrl', () => {
parseRequestUrl({ url: '/api/articles' }, '/next-api-decorators/.next/server/pages/api', 'articles.js')
).toStrictEqual('/'));

it('Should return "/" when the file name starts with "[..."', () =>
it('Should return "/" when the file name starts with a single bracket and three dots', () =>
expect(
parseRequestUrl(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand All @@ -50,4 +50,37 @@ describe('parseRequestUrl', () => {
'[...id].js'
)
).toStrictEqual('/'));

it('Should return "/" when the file name starts with a double bracket and three dots', () =>
expect(
parseRequestUrl(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
{ url: '/api/article/1', query: { id: '1' } },
'/next-api-decorators/.next/server/pages/api/article',
'[[...id]].js'
)
).toStrictEqual('/1'));

it('Should return "/" when the file name starts with a double bracket and three dots in a nested folder', () =>
expect(
parseRequestUrl(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
{ url: '/api/article/comments/1', query: { id: '1' } },
'/next-api-decorators/.next/server/pages/api/article/comments',
'[[...id]].js'
)
).toStrictEqual('/1'));

it('Should return "/" when the file name starts with a single bracket', () =>
expect(
parseRequestUrl(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
{ url: '/api/article/1', query: { id: '1' } },
'/next-api-decorators/.next/server/pages/api/article',
'[id].js'
)
).toStrictEqual('/'));
});
18 changes: 7 additions & 11 deletions lib/internals/parseRequestUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,18 @@ export function parseRequestUrl(req: NextApiRequest, directoryPath?: string, fil
const url = req.url!;
let path = url.split('?')[0].split('/').slice(3).join('/');

// In order for the main method (e.g: `@Get()`) to be matched,
// the path for catch all routes should be set to "/"
if (fileName?.startsWith('[...')) {
const qsKey = basename(fileName.replace('[...', '').replace(']', ''), extname(fileName));
/* istanbul ignore else */
if (req.query[qsKey]) {
path = '';
}
// The path for parametererized routes should be set to "/", in order for the methods to be matched.
if (fileName?.startsWith('[')) {
path = '/';
}

if (directoryPath && !fileName?.startsWith('[...')) {
const pathRegExp = new RegExp(
// "pages/api/articles/index.ts" is compiled into "pages/api/articles.js" which has to be appended to the directory path for parsing
(directoryPath + (fileName && !fileName.startsWith('[') ? basename(fileName, extname(fileName)) : ''))
.split('/.next/server/pages')[1]
.replace(/(\[\w+\])/, '(\\w+)')
directoryPath.split('/.next/server/pages')[1].replace(/(\[\w+\])/, '(\\w+)') +
(fileName && !fileName.startsWith('[...') && !fileName.startsWith('[[...')
? `/${basename(fileName, extname(fileName))}`
: '')
);
/* istanbul ignore else */
if (pathRegExp.test(url)) {
Expand Down

1 comment on commit 5f56ba3

@vercel
Copy link

@vercel vercel bot commented on 5f56ba3 Jul 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please # to comment.