Hi,
I’m running a function, login.ts
, which works great when using netlify dev
. However, when I run netlify deploy
I get a 502
error and after a few hours am at a loss on how to solve it.
I’m using babel via .babelrc
in my functions folder and they compile down to JavaScript.
I did try what was recommended in this thread and tried Node versions 12 and 14 which should be supported, with no luck.
Here’s my .babelrc
{
"presets": [
"@babel/preset-typescript",
[
"@babel/preset-env",
{
"targets": {
"node": "12"
}
}
]
],
"plugins": [
"@babel/plugin-proposal-class-properties",
"@babel/plugin-transform-object-assign",
"@babel/plugin-proposal-object-rest-spread",
"@babel/plugin-proposal-optional-chaining",
"@babel/plugin-transform-runtime"
],
"ignore": ["node_modules"]
}
And here’s my function, login.ts
import { APIGatewayEvent } from 'aws-lambda'
import axios from 'axios'
import {
statusReturn,
preparePayload,
shopifyConfig,
SHOPIFY_GRAPHQL_URL,
CUSTOMER_QUERY,
CUSTOMER_TOKEN_QUERY,
} from './requestConfig'
let data: {
email?: string
password?: string
}
let accessToken
export const handler = async (event: APIGatewayEvent): Promise<any> => {
if (event.httpMethod !== 'POST' || !event.body) return statusReturn(400, {})
try {
data = JSON.parse(event.body)
} catch (error) {
console.log('JSON parsing error:', error)
return statusReturn(400, { error: 'Bad Request Body' })
}
const payload = preparePayload(CUSTOMER_TOKEN_QUERY, {
input: {
email: data.email,
password: data.password,
},
})
try {
console.log('GRAPHQL URL', SHOPIFY_GRAPHQL_URL)
const token = await axios({
url: `https://${SHOPIFY_GRAPHQL_URL}`,
method: 'POST',
headers: shopifyConfig,
data: payload,
})
console.log('TOKEN', token.data.data.customerAccessTokenCreate)
if (token.data.data.customerAccessTokenCreate.customerUserErrors.length > 0) {
throw token.data.data.customerAccessTokenCreate.customerUserErrors
} else {
accessToken = token.data.data.customerAccessTokenCreate.customerAccessToken.accessToken
}
} catch (err) {
return statusReturn(200, { error: 'Problem with email or password' })
}
const payloadCustomer = preparePayload(CUSTOMER_QUERY, {
customerAccessToken: accessToken,
})
try {
let customer = await axios({
url: `https://${SHOPIFY_GRAPHQL_URL}`,
method: 'POST',
headers: shopifyConfig,
data: payloadCustomer,
})
customer = customer.data.data.customer
return statusReturn(200, {
token: accessToken,
customer,
})
} catch (err) {
return statusReturn(500, { error: err.message })
}
}
Really appreciate any help narrowing this down!