encoredev / encore

Open Source Development Platform for building robust type-safe distributed systems with declarative infrastructure
https://encore.dev
Mozilla Public License 2.0
7.73k stars 327 forks source link

Proposal: Adding multiple values for Set-Cookie and allowing *http.Cookie type #1548

Open linmeii opened 2 weeks ago

linmeii commented 2 weeks ago

Hey, recently I came across an interesting problem.

One of the endpoints in my service needs to return two or more Set-Cookie cookies at the same time. Currently there is no way to create multiple Set-Cookies at the same time, and any attempt to change the type to an array will raise an error such as: unsupported type in header: []string

I have two suggestions:

  1. Let's add the possibility to apply an array type for the Set-Cookie type header
  2. Allow for passing http.Cookie (and the array) the same as the request comes with http.Cookie, this would unify the types coming in and out

Example response from documentation

type LoginResponse struct {
    SessionID string `header:"Set-Cookie"`
}

Example request from documentation

type MyAuthParams struct {
    SessionCookie *http.Cookie `cookie:"session"`
}

Example of how I would see the proposal to be integrated:

type Response struct {
    // example param with `header:"Set-Cookie"` as a string
    Cookie string `header:"Set-Cookie"`

    // example param with `header:"Set-Cookie"` as a []string
    Cookies []string `header:"Set-Cookie"`

    // example param with `header:"Set-Cookie"` as a *http.Cookie
    Cookie *http.Cookie `header:"Set-Cookie"`

        // example param with `header:"Set-Cookie"` as a []*http.Cookie
     Cookies []*http.Cookie `header:"Set-Cookie"`
}

(obviously only one parameter with header:"Set-Cookie", just an example of multiple different types)