Skip to content

Commit

Permalink
Enable getRouteType() to properly parse full urls (#68)
Browse files Browse the repository at this point in the history
getRouteType() expects url to be a path like `/api/users`.  But when
using AppRouter the url passed is indeed a full url similar to
`https://yourhost.com/api/users`
  • Loading branch information
malaney authored May 2, 2024
1 parent c7b7c05 commit e7aa2b3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 12 additions & 0 deletions __tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,18 @@ describe('getRouteType with query params', () => {
})
})

it('should return CREATE type for full URL', () => {
expect(
getRouteType({
method: 'POST',
url: 'http://localhost:3000/api/users?q=1',
resourceName: 'users',
})
).toEqual<GetRouteType>({
routeType: RouteType.CREATE,
})
})

it('should return UPDATE type', () => {
expect(
getRouteType({
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const getRouteType = ({
resourceName,
}: GetRouteTypeParams): GetRouteType | null => {
// Exclude the query params from the path
const realPath = url.split('?')[0]
const realPath = (url.startsWith("http")) ? new URL(url).pathname : url.split('?')[0]

if (!realPath.includes(`/${resourceName}`)) {
throw new Error(
Expand Down

0 comments on commit e7aa2b3

Please sign in to comment.