提交 ec95927e 编写于 作者: W William 提交者: Joe Haddad

Fix dynamic APIs with query params (#8386)

* Fix dynamic APIs with query params

If you define a dynamic API such as

```
pages/
  api/
    [id].js
```

This api becomes available at `/api/[id]`. If you send a request with
a query parameter the value of `req.query.id` will include the query
string as well as the path parameter.

E.g. the request to `/api/2?test=123` will result in `req.query`
being

```javascript
{
  id: "2?test=123",
  test: "123",
}
```

instead of

```javascript
{
  id: "2",
  test: "123",
}
```

* Fix url parse in serverless loader

* Add serverless test
上级 177524be
......@@ -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}')
......
......@@ -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,
......
export default ({ query }, res) => {
res.status(200).json(query)
}
......@@ -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', () => {
......
export default ({ query }, res) => {
res.json(query)
}
export default (req, res) => {
res.json({ post: req.query.id })
export default ({ query }, res) => {
res.json(query)
}
......@@ -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 () => {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册