-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Organization Invitations API (#227)
Added support for the Create operation of the Organization Invitations API.
- Loading branch information
Showing
4 changed files
with
155 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package clerk | ||
|
||
import "encoding/json" | ||
|
||
type OrganizationInvitation struct { | ||
APIResource | ||
Object string `json:"object"` | ||
ID string `json:"id"` | ||
EmailAddress string `json:"email_address"` | ||
Role string `json:"role"` | ||
OrganizationID string `json:"organization_id"` | ||
Status string `json:"status"` | ||
PublicMetadata json.RawMessage `json:"public_metadata"` | ||
PrivateMetadata json.RawMessage `json:"private_metadata"` | ||
CreatedAt int64 `json:"created_at"` | ||
UpdatedAt int64 `json:"updated_at"` | ||
} |
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Package organizationinvitation provides the Organization Invitations API. | ||
package organizationinvitation | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/clerk/clerk-sdk-go/v2" | ||
) | ||
|
||
//go:generate go run ../cmd/gen/main.go | ||
|
||
const path = "/organizations" | ||
|
||
// Client is used to invoke the Organization Invitations API. | ||
type Client struct { | ||
Backend clerk.Backend | ||
} | ||
|
||
type ClientConfig struct { | ||
clerk.BackendConfig | ||
} | ||
|
||
func NewClient(config *ClientConfig) *Client { | ||
return &Client{ | ||
Backend: clerk.NewBackend(&config.BackendConfig), | ||
} | ||
} | ||
|
||
type CreateParams struct { | ||
clerk.APIParams | ||
EmailAddress *string `json:"email_address,omitempty"` | ||
Role *string `json:"role,omitempty"` | ||
RedirectURL *string `json:"redirect_url,omitempty"` | ||
InviterUserID *string `json:"inviter_user_id,omitempty"` | ||
PublicMetadata *json.RawMessage `json:"public_metadata,omitempty"` | ||
PrivateMetadata *json.RawMessage `json:"private_metadata,omitempty"` | ||
OrganizationID string `json:"-"` | ||
} | ||
|
||
// Create creates and sends an invitation to join an organization. | ||
func (c *Client) Create(ctx context.Context, params *CreateParams) (*clerk.OrganizationInvitation, error) { | ||
path, err := clerk.JoinPath(path, params.OrganizationID, "/invitations") | ||
if err != nil { | ||
return nil, err | ||
} | ||
req := clerk.NewAPIRequest(http.MethodPost, path) | ||
req.SetParams(params) | ||
invitation := &clerk.OrganizationInvitation{} | ||
err = c.Backend.Call(ctx, req, invitation) | ||
return invitation, err | ||
} |
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package organizationinvitation | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/clerk/clerk-sdk-go/v2" | ||
"github.com/clerk/clerk-sdk-go/v2/clerktest" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestOrganizationInvitationClientCreate(t *testing.T) { | ||
t.Parallel() | ||
id := "orginv_123" | ||
organizationID := "org_123" | ||
emailAddress := "[email protected]" | ||
config := &ClientConfig{} | ||
config.HTTPClient = &http.Client{ | ||
Transport: &clerktest.RoundTripper{ | ||
T: t, | ||
In: json.RawMessage(fmt.Sprintf(`{"email_address":"%s"}`, emailAddress)), | ||
Out: json.RawMessage(fmt.Sprintf(`{"id":"%s","email_address":"%s","organization_id":"%s"}`, id, emailAddress, organizationID)), | ||
Method: http.MethodPost, | ||
Path: "/v1/organizations/" + organizationID + "/invitations", | ||
}, | ||
} | ||
client := NewClient(config) | ||
invitation, err := client.Create(context.Background(), &CreateParams{ | ||
OrganizationID: organizationID, | ||
EmailAddress: clerk.String(emailAddress), | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, id, invitation.ID) | ||
require.Equal(t, organizationID, invitation.OrganizationID) | ||
require.Equal(t, emailAddress, invitation.EmailAddress) | ||
} | ||
|
||
func TestOrganizationInvitationClientCreate_Error(t *testing.T) { | ||
t.Parallel() | ||
config := &ClientConfig{} | ||
config.HTTPClient = &http.Client{ | ||
Transport: &clerktest.RoundTripper{ | ||
T: t, | ||
Status: http.StatusBadRequest, | ||
Out: json.RawMessage(`{ | ||
"errors":[{ | ||
"code":"create-error-code" | ||
}], | ||
"clerk_trace_id":"create-trace-id" | ||
}`), | ||
}, | ||
} | ||
client := NewClient(config) | ||
_, err := client.Create(context.Background(), &CreateParams{}) | ||
require.Error(t, err) | ||
apiErr, ok := err.(*clerk.APIErrorResponse) | ||
require.True(t, ok) | ||
require.Equal(t, "create-trace-id", apiErr.TraceID) | ||
require.Equal(t, 1, len(apiErr.Errors)) | ||
require.Equal(t, "create-error-code", apiErr.Errors[0].Code) | ||
} |