Skip to content
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

Image.create sends base64 string with application/octet-stream #1320

Merged
merged 1 commit into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/silver-days-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@clerk/clerk-js': patch
'@clerk/types': patch
---

Add support for dataURLs in User.setProfileImage
20 changes: 15 additions & 5 deletions packages/clerk-js/src/core/fapiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,26 @@ export default function createFapiClient(clerkInstance: Clerk): FapiClient {
sessionId: clerkInstance.session?.id,
});

// Initialize the headers if they're not provided.
if (!requestInit.headers) {
requestInit.headers = new Headers();
}

// In case FormData is provided, we don't want to mess with the headers,
// because for file uploads the header is properly set by the browser.
if (method !== 'GET' && !(body instanceof FormData)) {
requestInit.body = qs.stringify(body, { encoder: camelToSnakeEncoder, indices: false });
// Set the default content type for non-GET requests.
// Skip for FormData, because the browser knows how to construct it later on.
// Skip if the content-type header has already been set, somebody intends to override it.
// @ts-ignore
if (method !== 'GET' && !(body instanceof FormData) && !requestInit.headers.has('content-type')) {
// @ts-ignore
requestInit.headers.set('Content-Type', 'application/x-www-form-urlencoded');
requestInit.headers.set('content-type', 'application/x-www-form-urlencoded');
}

// Massage the body depending on the content type if needed.
// Currently, this is needed only for form-urlencoded, so that the values reach the server in the form
// foo=bar&baz=bar&whatever=1
// @ts-ignore
if (requestInit.headers.get('content-type') === 'application/x-www-form-urlencoded') {
requestInit.body = qs.stringify(body, { encoder: camelToSnakeEncoder, indices: false });
}

const beforeRequestCallbacksResult = await runBeforeRequestCallbacks(requestInit);
Expand Down
10 changes: 8 additions & 2 deletions packages/clerk-js/src/core/resources/Image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ export class Image extends BaseResource implements ImageResource {

static async create(path: string, body: any = {}): Promise<ImageResource> {
let fd = body;

if (body['file']) {
let headers;
if (typeof body['file'] === 'string') {
fd = body['file'];
headers = new Headers({
'Content-Type': 'application/octet-stream',
});
} else if (body['file']) {
fd = new FormData();
fd.append('file', body['file']);
}
Expand All @@ -20,6 +25,7 @@ export class Image extends BaseResource implements ImageResource {
path,
method: 'POST',
body: fd,
headers,
})
)?.response as unknown as ImageJSON;

Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export interface UserResource extends ClerkResource {
export type CreateEmailAddressParams = { email: string };
export type CreatePhoneNumberParams = { phoneNumber: string };
export type CreateWeb3WalletParams = { web3Wallet: string };
export type SetProfileImageParams = { file: Blob | File | null };
export type SetProfileImageParams = { file: Blob | File | string | null };
export type CreateExternalAccountParams = {
strategy: OAuthStrategy;
redirectUrl?: string;
Expand Down