-
Notifications
You must be signed in to change notification settings - Fork 29
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 #181 from storyofams/fix/nested-folders
fix: use error stack to determine the directory
- Loading branch information
Showing
5 changed files
with
84 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { getFileDirectory } from './getFileDirectory'; | ||
|
||
it('getFileDirectory', () => { | ||
const spyError = jest.spyOn(global, 'Error'); | ||
spyError.mockImplementation(() => { | ||
const err = { | ||
name: 'Error', | ||
message: 'An error occurred.', | ||
stack: 'Object at (/example-path/.next/server/pages/api/tags/[id]/[[...params]].ts:1:1)' | ||
}; | ||
return err; | ||
}); | ||
|
||
const dir = getFileDirectory(); | ||
expect(dir).toStrictEqual('/example-path/.next/server/pages/api/tags/[id]'); | ||
|
||
spyError.mockRestore(); | ||
}); |
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,21 @@ | ||
import { dirname } from 'path'; | ||
|
||
export function getFileDirectory(): string | undefined { | ||
let directoryPath: string | undefined; | ||
|
||
const parenthesisRegExp = /\(([^)]+)\)/; | ||
const pathInError = new Error().stack | ||
?.split('at ') | ||
.find(line => parenthesisRegExp.test(line) && line.includes('/.next/server/pages/api')); | ||
|
||
/* istanbul ignore else */ | ||
if (pathInError) { | ||
const [, pathWithRowCol] = parenthesisRegExp.exec(pathInError) ?? []; | ||
/* istanbul ignore else */ | ||
if (pathWithRowCol) { | ||
directoryPath = dirname(pathWithRowCol.replace(/:(\d+):(\d+)$/, '')); | ||
} | ||
} | ||
|
||
return directoryPath; | ||
} |
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,23 @@ | ||
import { parseRequestUrl } from './parseRequestUrl'; | ||
|
||
describe('parseRequestUrl', () => { | ||
it('Should return "/"', () => { | ||
expect(parseRequestUrl('/api/user')).toStrictEqual('/'); | ||
}); | ||
|
||
it('Should return "/1"', () => { | ||
expect(parseRequestUrl('/api/user/1')).toStrictEqual('/1'); | ||
}); | ||
|
||
it('Should return "/1/articles"', () => { | ||
expect(parseRequestUrl('/api/user/1/articles')).toStrictEqual('/1/articles'); | ||
}); | ||
|
||
it('Should return "/" when directory name is a paremeter ([id])', () => | ||
expect(parseRequestUrl('/api/user/1', '/next-api-decorators/.next/server/pages/api/user/[id]')).toStrictEqual('/')); | ||
|
||
it('Should return "/articles" when directory name is a paremeter ([id])', () => | ||
expect( | ||
parseRequestUrl('/api/user/1/articles', '/next-api-decorators/.next/server/pages/api/user/[id]') | ||
).toStrictEqual('/articles')); | ||
}); |
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,17 @@ | ||
export function parseRequestUrl(url: string, directoryPath?: string): string { | ||
let path = url.split('?')[0].split('/').slice(3).join('/'); | ||
|
||
if (directoryPath) { | ||
const pathRegExp = new RegExp(directoryPath.split('/.next/server/pages')[1].replace(/(\[\w+\])/, '(\\w+)')); | ||
/* istanbul ignore else */ | ||
if (pathRegExp.test(url)) { | ||
path = url.replace(pathRegExp, ''); | ||
} | ||
} | ||
|
||
if (!path.startsWith('/')) { | ||
path = `/${path}`; | ||
} | ||
|
||
return path; | ||
} |