-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add bulk invitation creation support #351
Merged
+90
−0
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b354456
feat: Add bulk invitation creation support
kostaspt 50f9221
chore: Update changelog
kostaspt c9002ee
chore: Update changelog
kostaspt 0d2d9a1
refactor: Improve exported types
kostaspt 1b64d7f
Merge branch 'v2' into kostas/user-683
BrandonRomano e9158d5
chore: use `clerk.JoinPath`
kostaspt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,6 +76,41 @@ func (c *Client) Create(ctx context.Context, params *CreateParams) (*clerk.Invit | |
return invitation, err | ||
} | ||
|
||
type BulkCreateParams struct { | ||
clerk.APIParams | ||
Invitations []*CreateParams | ||
} | ||
|
||
func (b BulkCreateParams) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(b.Invitations) | ||
} | ||
|
||
type bulkCreateResponse struct { | ||
clerk.APIResource | ||
Invitations []*clerk.Invitation | ||
} | ||
|
||
func (b *bulkCreateResponse) UnmarshalJSON(data []byte) error { | ||
return json.Unmarshal(data, &b.Invitations) | ||
} | ||
|
||
// BulkCreate creates multiple invitations. | ||
func (c *Client) BulkCreate(ctx context.Context, params *BulkCreateParams) (*clerk.Invitations, error) { | ||
req := clerk.NewAPIRequest(http.MethodPost, fmt.Sprintf("%s/bulk", path)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔧 Let's use clerk.JoinPath here. |
||
req.SetParams(params) | ||
|
||
res := &bulkCreateResponse{} | ||
err := c.Backend.Call(ctx, req, res) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &clerk.Invitations{ | ||
APIResource: res.APIResource, | ||
Invitations: res.Invitations, | ||
}, nil | ||
} | ||
|
||
// Revoke revokes a pending invitation. | ||
func (c *Client) Revoke(ctx context.Context, id string) (*clerk.Invitation, error) { | ||
path, err := clerk.JoinPath(path, id, "revoke") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,6 +162,46 @@ func TestInvitationCreate_Error(t *testing.T) { | |
require.Equal(t, "create-error-code", apiErr.Errors[0].Code) | ||
} | ||
|
||
func TestBulkInvitationCreate(t *testing.T) { | ||
emailAddresses := []string{"[email protected]", "[email protected]"} | ||
ids := []string{"inv_123", "inv_456"} | ||
invitations := []*clerk.Invitation{ | ||
{ID: ids[0], EmailAddress: emailAddresses[0]}, | ||
{ID: ids[1], EmailAddress: emailAddresses[1]}, | ||
} | ||
|
||
clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{ | ||
HTTPClient: &http.Client{ | ||
Transport: &clerktest.RoundTripper{ | ||
T: t, | ||
In: json.RawMessage(fmt.Sprintf(`[{"email_address":"%s"},{"email_address":"%s"}]`, emailAddresses[0], emailAddresses[1])), | ||
Out: json.RawMessage(fmt.Sprintf( | ||
`[{"id":"%s","email_address":"%s"},{"id":"%s","email_address":"%s"}]`, | ||
ids[0], emailAddresses[0], ids[1], emailAddresses[1], | ||
)), | ||
Method: http.MethodPost, | ||
Path: "/v1/invitations/bulk", | ||
}, | ||
}, | ||
})) | ||
|
||
params := BulkCreateParams{ | ||
Invitations: []*CreateParams{ | ||
{EmailAddress: emailAddresses[0]}, | ||
{EmailAddress: emailAddresses[1]}, | ||
}, | ||
} | ||
|
||
response, err := BulkCreate(context.Background(), ¶ms) | ||
require.NoError(t, err) | ||
require.Len(t, invitations, 2) | ||
|
||
for i, invitation := range response.Invitations { | ||
require.Equal(t, ids[i], invitation.ID) | ||
require.Equal(t, emailAddresses[i], invitation.EmailAddress) | ||
} | ||
} | ||
|
||
func TestInvitationRevoke(t *testing.T) { | ||
id := "inv_123" | ||
clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{ | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❓ Why do we need this custom marshaling method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need the custom marshaling because the API's request for bulk is an array of regular requests, so it’s not bound to a key like
{"invitations": [...]}
. To be accurate in SDK, the correct type would betype BulkCreateParams []*CreateParams
. However this wouldn’t be compatible withreq.SetParams
.Do you have a better alternative to suggest?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for explaining! It is indeed a problem with the current implementation.
Not really. 😞 As discussed offline, let's settle on the exported types for now and solidify the API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’ve refactored the types as we discussed a bit here. Please take another look when you get a chance.