Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dynamic APIs with query params #8386

Merged
merged 4 commits into from
Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ const nextServerlessLoader: loader.Loader = function() {
`
: ``
}
import { parse } from 'url'
import { apiResolver } from 'next-server/dist/server/api-utils'

export default (req, res) => {
const params = ${
isDynamicRoute(page)
? `getRouteMatcher(getRouteRegex('${page}'))(req.url)`
? `getRouteMatcher(getRouteRegex('${page}'))(parse(req.url).pathname)`
: `{}`
}
const resolver = require('${absolutePagePath}')
Expand Down
30 changes: 30 additions & 0 deletions test/integration/api-support/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,36 @@ function runTests (serverless = false) {
expect(data).toEqual({ post: 'post-1' })
})

it('should work with dynamic params and search string', async () => {
const data = await fetchViaHTTP(
appPort,
'/api/post-1?val=1',
null,
{}
).then(res => res.ok && res.json())

expect(data).toEqual({ val: '1', post: 'post-1' })
})

if (serverless) {
it('should work with dynamic params and search string like lambda', async () => {
await nextBuild(appDir, [])

const port = await findPort()
const resolver = require(join(
appDir,
'.next/serverless/pages/api/[post].js'
)).default

const server = createServer(resolver).listen(port)
const res = await fetchViaHTTP(port, '/api/post-1?val=1')
const json = await res.json()
server.close()

expect(json).toEqual({ val: '1', post: 'post-1' })
})
}

it('should prioritize a non-dynamic page', async () => {
const data = await fetchViaHTTP(
appPort,
Expand Down
3 changes: 3 additions & 0 deletions test/integration/production/pages/api/[post]/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default ({ query }, res) => {
res.status(200).json(query)
}
8 changes: 8 additions & 0 deletions test/integration/production/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ describe('Production Usage', () => {
const body = await res.text()
expect(body).toEqual('API hello works')
})

it('should work with dynamic params and search string', async () => {
const url = `http://localhost:${appPort}/api/post-1?val=1`
const res = await fetch(url)
const body = await res.json()

expect(body).toEqual({ val: '1', post: 'post-1' })
})
})

describe('With navigation', () => {
Expand Down
3 changes: 3 additions & 0 deletions test/integration/serverless/pages/api/dynamic/[path]/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default ({ query }, res) => {
res.json(query)
}
4 changes: 2 additions & 2 deletions test/integration/serverless/pages/api/posts/[id].js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default (req, res) => {
res.json({ post: req.query.id })
export default ({ query }, res) => {
res.json(query)
}
24 changes: 22 additions & 2 deletions test/integration/serverless/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,28 @@ describe('Serverless', () => {

it('should reply on dynamic API request successfully', async () => {
const result = await renderViaHTTP(appPort, '/api/posts/post-1')
const { post } = JSON.parse(result)
expect(post).toBe('post-1')
const { id } = JSON.parse(result)
expect(id).toBe('post-1')
})

it('should reply on dynamic API request successfully with query parameters', async () => {
const result = await renderViaHTTP(appPort, '/api/posts/post-1?param=val')
const { id, param } = JSON.parse(result)
expect(id).toBe('post-1')
expect(param).toBe('val')
})

it('should reply on dynamic API index request successfully', async () => {
const result = await renderViaHTTP(appPort, '/api/dynamic/post-1')
const { path } = JSON.parse(result)
expect(path).toBe('post-1')
})

it('should reply on dynamic API index request successfully with query parameters', async () => {
const result = await renderViaHTTP(appPort, '/api/dynamic/post-1?param=val')
const { path, param } = JSON.parse(result)
expect(path).toBe('post-1')
expect(param).toBe('val')
})

it('should 404 on API request with trailing slash', async () => {
Expand Down