Skip to content

Commit

Permalink
feat(backend): Add methods for uploading images to User and Organization
Browse files Browse the repository at this point in the history
This commit adds:

- `updateOrganizationLogo` method in OrganizationApi class
- `updateUserProfileImage` method in UserApi class
  • Loading branch information
anagstef committed Jul 7, 2023
1 parent fbed641 commit bb0d69b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-months-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/backend': minor
---

Add `updateUserProfileImage` and `updateOrganizationLogo` methods for uploading images to `User` and `Organization` respectively.
33 changes: 33 additions & 0 deletions packages/backend/src/api/endpoints/OrganizationApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ type UpdateParams = {
maxAllowedMemberships?: number;
} & MetadataParams;

type UpdateLogoParams = {
file: Blob | File | null;
uploaderUserId: string;
};

type UpdateMetadataParams = MetadataParams;

type GetOrganizationMembershipListParams = {
Expand Down Expand Up @@ -116,6 +121,34 @@ export class OrganizationAPI extends AbstractAPI {
});
}

public async updateOrganizationLogo(organizationId: string, params: UpdateLogoParams) {
this.requireId(organizationId);

if (params?.file === null) {
return this.request<Organization>({
method: 'DELETE',
path: joinPaths(basePath, organizationId, 'logo'),
});
}

if (!params?.file) {
throw new Error('A valid Blob or File is required.');
}
if (!params?.uploaderUserId) {
throw new Error('A valid uploaderUserId is required.');
}

const formData = new FormData();
formData.append('file', params.file);
formData.append('uploader_user_id', params.uploaderUserId);

return this.request<Organization>({
method: 'PUT',
path: joinPaths(basePath, organizationId, 'logo'),
formData,
});
}

public async updateOrganizationMetadata(organizationId: string, params: UpdateMetadataParams) {
this.requireId(organizationId);

Expand Down
17 changes: 17 additions & 0 deletions packages/backend/src/api/endpoints/UserApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,23 @@ export class UserAPI extends AbstractAPI {
});
}

public async updateUserProfileImage(userId: string, params: { file: Blob | File }) {
this.requireId(userId);

if (!params?.file) {
throw new Error('A valid Blob or File parameter is required.');
}

const formData = new FormData();
formData.append('file', params.file);

return this.request<User>({
method: 'POST',
path: joinPaths(basePath, userId, 'profile_image'),
formData,
});
}

public async updateUserMetadata(userId: string, params: UserMetadataParams) {
this.requireId(userId);

Expand Down

0 comments on commit bb0d69b

Please sign in to comment.