Netlify lambda 函数查询字符串参数在本地环境和生产环境之间以不同的方式传递
Netlify lambda function query string parameters are passed in different way between local environment and production envionment
描述错误
我创建了几个 lambda 函数并将它们部署在 Netlify 服务上。
我通过 GET 请求传递了几个查询,它们在本地以数组形式传递,但部署后它们以字符串形式传递。
重现
重现该行为的步骤:
lambda 函数端:
export const handler = constructNetlifyHandler({
get: async (event, context, postgrest) => {
console.log(event.queryStringParameters);
return await getAllTasks(
postgrest,
event.queryStringParameters,
event.loggedInUserOrg
);
},
});
邮递员方:
{{host}}/tasks?orgUid=06ea7872-32eb-4e7a-ba45-63e2b9e6c747&statusUid=ba92a0b7-2e80-4cd2-8d37-fa18d5ca22b9&statusUid=78dcdbe1-007a-493a-ad94-50a0ec613d0d&statusUid=1cbc65b8-831d-4cba-a1ad-111a0757e76b
本地环境日志:
{
orgUid: '06ea7872-32eb-4e7a-ba45-63e2b9e6c747',
statusUid: [
'ba92a0b7-2e80-4cd2-8d37-fa18d5ca22b9',
'78dcdbe1-007a-493a-ad94-50a0ec613d0d',
'1cbc65b8-831d-4cba-a1ad-111a0757e76b'
]
}
netlify 生产环境日志:
{
orgUid: '06ea7872-32eb-4e7a-ba45-63e2b9e6c747',
statusUid: 'ba92a0b7-2e80-4cd2-8d37-fa18d5ca22b9, 78dcdbe1-007a-493a-ad94-50a0ec613d0d, 1cbc65b8-831d-4cba-a1ad-111a0757e76b'
}
预期行为
在这两种环境中,查询参数的传递方式相同。 (作为字符串数组或字符串)
截图
邮递员方面:
本地环境日志:
生产环境日志:
桌面(请填写以下信息):
运行 以下命令 npx envinfo --system --binaries --npmPackages netlify-lambda
并附上输出
其他上下文
System:
OS: macOS 11.2.3
CPU: (4) x64 Intel(R) Core(TM) i3-9100 CPU @ 3.60GHz
Memory: 54.82 MB / 24.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 12.20.1 - ~/.nvm/versions/node/v12.20.1/bin/node
Yarn: 1.22.10 - /usr/local/bin/yarn
npm: 6.14.10 - ~/.nvm/versions/node/v12.20.1/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
npmPackages:
netlify-lambda: ^2.0.3 => 2.0.3
解决方案 1:在函数内部进行匹配
由于 Netlify 重定向机制无法为您提供它匹配的规则的数据,您可以尝试在您的函数中匹配原始请求以确定它应该做什么。
示例:
_redirects
> /first-thing /.netlify/functions/the-only-function 200! /second-thing
> /.netlify/functions/the-only-function 200!
the-only-function.js
exports.handler = async function(event, context) {
if (event.path === "/first-thing") {
// do one thing
} else if (event.path === "/second-thing") {
// do the other thing
} else {
// return 400 status, because the function was invoked raw
}
}
解决方案 2:代理 headers 破解
重写内容时可以添加自定义代理 headers(点击查看文档)1.
同样的例子用这些解决了:
netlify.toml
[[redirects]]
from = "/first-thing"
to = "/.netlify/functions/the-only-function"
status = 200
force = true
headers = {X-Variant = "first-thing"}
[[redirects]]
from = "/second-thing"
to = "/.netlify/functions/the-only-function"
status = 200
force = true
headers = {X-Variant = "second-thing"}
the-only-function.js
exports.handler = async function(event, context) {
if (event.headers["X-Variant"] === "first-thing") {
// do one thing
} else if (event.headers["X-Variant"] === "second-thing") {
// do the other thing
} else {
// return 400 status, because the function was invoked raw
}
}
希望这能帮助您解决具体问题!
描述错误
我创建了几个 lambda 函数并将它们部署在 Netlify 服务上。
我通过 GET 请求传递了几个查询,它们在本地以数组形式传递,但部署后它们以字符串形式传递。
重现 重现该行为的步骤:
lambda 函数端:
export const handler = constructNetlifyHandler({
get: async (event, context, postgrest) => {
console.log(event.queryStringParameters);
return await getAllTasks(
postgrest,
event.queryStringParameters,
event.loggedInUserOrg
);
},
});
邮递员方:
{{host}}/tasks?orgUid=06ea7872-32eb-4e7a-ba45-63e2b9e6c747&statusUid=ba92a0b7-2e80-4cd2-8d37-fa18d5ca22b9&statusUid=78dcdbe1-007a-493a-ad94-50a0ec613d0d&statusUid=1cbc65b8-831d-4cba-a1ad-111a0757e76b
本地环境日志:
{
orgUid: '06ea7872-32eb-4e7a-ba45-63e2b9e6c747',
statusUid: [
'ba92a0b7-2e80-4cd2-8d37-fa18d5ca22b9',
'78dcdbe1-007a-493a-ad94-50a0ec613d0d',
'1cbc65b8-831d-4cba-a1ad-111a0757e76b'
]
}
netlify 生产环境日志:
{
orgUid: '06ea7872-32eb-4e7a-ba45-63e2b9e6c747',
statusUid: 'ba92a0b7-2e80-4cd2-8d37-fa18d5ca22b9, 78dcdbe1-007a-493a-ad94-50a0ec613d0d, 1cbc65b8-831d-4cba-a1ad-111a0757e76b'
}
预期行为
在这两种环境中,查询参数的传递方式相同。 (作为字符串数组或字符串)
截图
邮递员方面:
本地环境日志:
生产环境日志:
桌面(请填写以下信息):
运行 以下命令 npx envinfo --system --binaries --npmPackages netlify-lambda
并附上输出
其他上下文
System:
OS: macOS 11.2.3
CPU: (4) x64 Intel(R) Core(TM) i3-9100 CPU @ 3.60GHz
Memory: 54.82 MB / 24.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 12.20.1 - ~/.nvm/versions/node/v12.20.1/bin/node
Yarn: 1.22.10 - /usr/local/bin/yarn
npm: 6.14.10 - ~/.nvm/versions/node/v12.20.1/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
npmPackages:
netlify-lambda: ^2.0.3 => 2.0.3
解决方案 1:在函数内部进行匹配
由于 Netlify 重定向机制无法为您提供它匹配的规则的数据,您可以尝试在您的函数中匹配原始请求以确定它应该做什么。
示例:
_redirects
> /first-thing /.netlify/functions/the-only-function 200! /second-thing
> /.netlify/functions/the-only-function 200!
the-only-function.js
exports.handler = async function(event, context) {
if (event.path === "/first-thing") {
// do one thing
} else if (event.path === "/second-thing") {
// do the other thing
} else {
// return 400 status, because the function was invoked raw
}
}
解决方案 2:代理 headers 破解
重写内容时可以添加自定义代理 headers(点击查看文档)1.
同样的例子用这些解决了:
netlify.toml
[[redirects]]
from = "/first-thing"
to = "/.netlify/functions/the-only-function"
status = 200
force = true
headers = {X-Variant = "first-thing"}
[[redirects]]
from = "/second-thing"
to = "/.netlify/functions/the-only-function"
status = 200
force = true
headers = {X-Variant = "second-thing"}
the-only-function.js
exports.handler = async function(event, context) {
if (event.headers["X-Variant"] === "first-thing") {
// do one thing
} else if (event.headers["X-Variant"] === "second-thing") {
// do the other thing
} else {
// return 400 status, because the function was invoked raw
}
}
希望这能帮助您解决具体问题!