Skip to content

Commit

Permalink
Merge pull request #17 from shopware/shopware/add-return-value-for-me…
Browse files Browse the repository at this point in the history
…dia-upload

feat: return mediaId on successful uploadMediaFile
  • Loading branch information
shyim authored Oct 11, 2024
2 parents a0da4b7 + 78fe4d4 commit 3f5be53
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/helper/media.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,12 @@ describe("Test Media helper", () => {
},
} as unknown as HttpClient;

await uploadMediaFile(client, {
const response = await uploadMediaFile(client, {
file: new Blob(["test"], { type: "text/plain" }),
fileName: "test.text",
});

expect(typeof response).toBe("string");
expect(requests).toBeArrayOfSize(2);
expect(requests[0]?.url).toBe("/_action/sync");
expect(requests[0]?.data).toEqual([
Expand Down
5 changes: 4 additions & 1 deletion src/helper/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Criteria } from "./criteria.js";
* @param {string} [options.mediaFolderId] - The ID of the media folder to upload the file to.
* @param {string} options.fileName - The name of the file to upload.
* @param {Blob|Promise<Blob>} options.file - The file to upload.
* @returns {Promise<string>} - The ID of the uploaded media file.
*/
export async function uploadMediaFile(
httpClient: HttpClient,
Expand All @@ -25,7 +26,7 @@ export async function uploadMediaFile(
fileName: string;
file: Blob | Promise<Blob>;
},
) {
): Promise<string> {
const repository = new EntityRepository(httpClient, "media");

const mediaId = uuid();
Expand Down Expand Up @@ -62,6 +63,8 @@ export async function uploadMediaFile(
"Content-Type": resolved.type,
},
);

return mediaId;
} catch (e) {
await repository.delete([{ id: mediaId }]);
throw e;
Expand Down

0 comments on commit 3f5be53

Please sign in to comment.