Skip to content

Commit

Permalink
enable image key generation
Browse files Browse the repository at this point in the history
  • Loading branch information
dhairyadwivedi committed Nov 29, 2023
1 parent 2eed923 commit 43c9bc8
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
2 changes: 2 additions & 0 deletions components/js-api-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
},
"dependencies": {
"dotenv": "^16.0.0",
"fs": "^0.0.1-security",
"json-to-graphql-query": "^2.2.4",
"mime-lite": "^1.0.3",
"node-fetch": "^2",
"tiny-invariant": "^1.2.0",
"typescript": "^4.6.3",
Expand Down
94 changes: 94 additions & 0 deletions components/js-api-client/src/core/uploadImage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import * as fs from 'fs';
import * as mime from 'mime-lite';
import { ClientInterface } from './client';

type ImageInputWithReferenceId = {
id: string;
filename: string;
mimeType: string;
};

type UploadHandler = ImageInputWithReferenceId & {
buffer: Buffer;
stats: fs.Stats;
apiClient: ClientInterface;
};

const MUTATION_UPLOAD_FILE = `#graphql
mutation UPLOAD_FILE ($tenantId: ID!, $filename: String!, $mimeType: String!) {
fileUpload {
generatePresignedRequest(
tenantId: $tenantId
filename: $filename
contentType: $mimeType
type: MEDIA
) {
url
fields {
name
value
}
}
}
}`;

export async function uploadImageToTenant({
id,
mimeType,
filename,
buffer,
stats,
apiClient,
}: UploadHandler): Promise<string | false> {
const signedRequestResult = await apiClient.pimApi(MUTATION_UPLOAD_FILE, {
tenantId: id,
filename,
mimeType,
});

const payload = signedRequestResult.fileUpload.generatePresignedRequest;
const formData: FormData = new FormData();
payload.fields.forEach((field: { name: string; value: string }) => {
formData.append(field.name, field.value);
});
formData.append('file', new Blob([buffer]));

const response = await fetch(payload.url, {
method: 'POST',
headers: new Headers({ 'Content-Length': String(stats.size) }),
body: formData,
});

return response.status === 201 ? (formData.get('key') as string) : false;
}

export async function handleImageUpload(imagePath: any, apiClient: ClientInterface, tenantId: string): Promise<any> {
if (!imagePath) return;

const extension = imagePath.split('.').pop() as string;
const mimeType = mime.getType(extension);
const filename = imagePath.split('T/').pop() as string;

if (!mimeType) {
console.log('Could not find mime type for file. Halting upload');
return null;
}

const stats = fs.statSync(imagePath);
const buffer = fs.readFileSync(imagePath);

const data: Omit<UploadHandler, 'id'> = {
mimeType,
filename,
stats,
buffer,
apiClient,
};

const imageKey = await uploadImageToTenant({
id: tenantId,
...data,
});

return imageKey;
}
1 change: 1 addition & 0 deletions components/js-api-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export * from './types/address';
export * from './types/customer';
export * from './types/signature';
export * from './types/pricing';
export * from './core/uploadImage';

import { createClient } from './core/client';
import { createNavigationFetcher } from './core/navigation';
Expand Down

0 comments on commit 43c9bc8

Please sign in to comment.