Skip to content

Commit

Permalink
feat: Support limit & offset to invitations List (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikospapcom authored Aug 30, 2024
1 parent a81a965 commit d2786f4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
8 changes: 8 additions & 0 deletions invitation/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"

"github.com/clerk/clerk-sdk-go/v2"
)
Expand All @@ -26,11 +27,18 @@ func NewClient(config *clerk.ClientConfig) *Client {

type ListParams struct {
clerk.APIParams
clerk.ListParams
}

// ToQuery returns query string values from the params.
func (params *ListParams) ToQuery() url.Values {
return params.ListParams.ToQuery()
}

// List returns all invitations.
func (c *Client) List(ctx context.Context, params *ListParams) (*clerk.InvitationList, error) {
req := clerk.NewAPIRequest(http.MethodGet, fmt.Sprintf("%s?paginated=true", path))
req.SetParams(params)
list := &clerk.InvitationList{}
err := c.Backend.Call(ctx, req, list)
return list, err
Expand Down
41 changes: 41 additions & 0 deletions invitation/invitation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"testing"

"github.com/clerk/clerk-sdk-go/v2"
Expand Down Expand Up @@ -35,6 +36,46 @@ func TestInvitationList(t *testing.T) {
require.Equal(t, "[email protected]", list.Invitations[0].EmailAddress)
}

func TestInvitationListWithParams(t *testing.T) {
limit := int64(10)
offset := int64(20)
clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{
HTTPClient: &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
Out: json.RawMessage(`{
"data": [
{"id":"inv_123","email_address":"[email protected]"},
{"id":"inv_124","email_address":"[email protected]"}
],
"total_count": 2
}`),
Path: "/v1/invitations",
Method: http.MethodGet,
Query: &url.Values{
"limit": []string{fmt.Sprintf("%d", limit)},
"offset": []string{fmt.Sprintf("%d", offset)},
"paginated": []string{"true"},
},
},
},
}))

list, err := List(context.Background(), &ListParams{
ListParams: clerk.ListParams{
Limit: &limit,
Offset: &offset,
},
})
require.NoError(t, err)
require.Equal(t, int64(2), list.TotalCount)
require.Equal(t, 2, len(list.Invitations))
require.Equal(t, "inv_123", list.Invitations[0].ID)
require.Equal(t, "[email protected]", list.Invitations[0].EmailAddress)
require.Equal(t, "inv_124", list.Invitations[1].ID)
require.Equal(t, "[email protected]", list.Invitations[1].EmailAddress)
}

func TestInvitationCreate(t *testing.T) {
emailAddress := "[email protected]"
id := "inv_123"
Expand Down

0 comments on commit d2786f4

Please sign in to comment.