*Api*: Is not even sure if there is a way to redir...
# help
d
Api: Is not even sure if there is a way to redirect with HTTP APIs but I’m searching how to do this… and then the question might be, how to customize
Integration Responses
in SST’s
Api
.
a
You can do proxy.
Not redirect.
d
@Adrián Mouly with REST API you can actually do redirect. This is what I want, but with HTTP API
If this is not possible, then I guess my next question is does SST have a construct for REST APIs similar to
Api
(HTTP API)
j
t
Yeah HTTP doesn't support it but you can always add a lambda to do the redir
d
add a lambda? @thdxr Not sure I follow. I already have these HTTP API routes backed by lambdas. Using them is what I’d like to do, but it sounds like this is not possible with HTTP APIs
t
Copy code
GET /path/for/redir: "src/redir.handler"
And that would just return a 302
or whatever is appropriate
t
Maybe I'm not understanding, what is it that you'd like to redirect?
d
someone requests
'GET /thingy': lambda.thingy
and I want
lambda.thingy
to respond with a custom 302.
t
Can't your handler return a
{ statusCode: 302}
?
d
Lol…. maybe I can. To be honest I wasn’t sure if it works the same as lambda@edge in that way and I ended up finding a lot of articles making it look much harder…. eg https://kennbrodhagen.net/2016/04/02/how-to-return-302-using-api-gateway-lambda/
s
Just implemented this yesterday using Api http api for oauth flow with statuscode 301 or 302 and it works: here's example gist: https://gist.github.com/DavidWells/99216c20cdb3df334d5b98ff19644fa2
d
@Sione Literally for an oauth flow myself as well. 😛
so it does work? I’m getting Typescript issues with the
APIGatewayProxyHandlerV2
not matching the return shape since its a redirect
s
hah perfect. ya it should work with proxyhandlerv2. What typescript issue you are getting? this is basically what mine looks like:
Copy code
import type {APIGatewayProxyHandlerV2} from 'aws-lambda'

const handler: APIGatewayProxyHandlerV2 = async (event, context) => {
  const oauthUrl = '<https://api.hubspot.com>'

  return {
    statusCode: 301,
    headers: {
      Location: oauthUrl,
    },
  }
}
Copy code
// api construct
new sst.Api(this, 'OAuth', {
   routes: {'GET /{proxy+}': callbackFn},
   defaultPayloadFormatVersion: sst.ApiPayloadFormatVersion.V2,
})
d
my mistake!
it was just that I was using Lamba@Edge response for redirects. I got it all working now with your gist. THANKS! @Sione
s
yaay. I'm glad. lambda@edge is on my list to try out for some usecase. exciting stuff.