@shopware-ag/app-server-sdk 1.1.8 → 1.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,13 +8,14 @@ import type { HttpClient } from "../http-client.js";
8
8
  * @param {string} [options.mediaFolderId] - The ID of the media folder to upload the file to.
9
9
  * @param {string} options.fileName - The name of the file to upload.
10
10
  * @param {Blob|Promise<Blob>} options.file - The file to upload.
11
+ * @returns {Promise<string>} - The ID of the uploaded media file.
11
12
  */
12
13
  export declare function uploadMediaFile(httpClient: HttpClient, { private: isPrivate, mediaFolderId, fileName, file, }: {
13
14
  private?: boolean;
14
15
  mediaFolderId?: string | null;
15
16
  fileName: string;
16
17
  file: Blob | Promise<Blob>;
17
- }): Promise<void>;
18
+ }): Promise<string>;
18
19
  /**
19
20
  * Retrieves the default media folder ID for a given entity.
20
21
  *
@@ -1 +1 @@
1
- {"version":3,"file":"media.d.ts","sourceRoot":"","sources":["../../../src/helper/media.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAIpD;;;;;;;;;GASG;AACH,wBAAsB,eAAe,CACpC,UAAU,EAAE,UAAU,EACtB,EACC,OAAO,EAAE,SAAiB,EAC1B,aAAoB,EACpB,QAAQ,EACR,IAAI,GACJ,EAAE;IACF,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC3B,iBA0CD;AAED;;;;;;GAMG;AACH,wBAAsB,6BAA6B,CAClD,UAAU,EAAE,UAAU,EACtB,MAAM,EAAE,MAAM,GACZ,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAaxB;AAED;;;;;;GAMG;AACH,wBAAsB,oBAAoB,CACzC,UAAU,EAAE,UAAU,EACtB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C/B,OAAO,CAAC,MAAM,CAAC,CAejB"}
@@ -15,6 +15,7 @@ const criteria_js_1 = require("./criteria.js");
15
15
  * @param {string} [options.mediaFolderId] - The ID of the media folder to upload the file to.
16
16
  * @param {string} options.fileName - The name of the file to upload.
17
17
  * @param {Blob|Promise<Blob>} options.file - The file to upload.
18
+ * @returns {Promise<string>} - The ID of the uploaded media file.
18
19
  */
19
20
  async function uploadMediaFile(httpClient, { private: isPrivate = false, mediaFolderId = null, fileName, file, }) {
20
21
  const repository = new admin_api_js_1.EntityRepository(httpClient, "media");
@@ -40,6 +41,7 @@ async function uploadMediaFile(httpClient, { private: isPrivate = false, mediaFo
40
41
  await httpClient.post(`/_action/media/${mediaId}/upload?${params.toString()}`, resolved, {
41
42
  "Content-Type": resolved.type,
42
43
  });
44
+ return mediaId;
43
45
  }
44
46
  catch (e) {
45
47
  await repository.delete([{ id: mediaId }]);
@@ -1 +1 @@
1
- {"version":3,"file":"media.js","sourceRoot":"","sources":["../../../src/helper/media.ts"],"names":[],"mappings":";;AAcA,0CAsDC;AASD,sEAgBC;AASD,oDAeC;AAeD,8CAmBC;AAtJD,iDAAwD;AACxD,+CAAyC;AAEzC;;;;;;;;;GASG;AACI,KAAK,UAAU,eAAe,CACpC,UAAsB,EACtB,EACC,OAAO,EAAE,SAAS,GAAG,KAAK,EAC1B,aAAa,GAAG,IAAI,EACpB,QAAQ,EACR,IAAI,GAMJ;IAED,MAAM,UAAU,GAAG,IAAI,+BAAgB,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;IAE7D,MAAM,OAAO,GAAG,IAAA,mBAAI,GAAE,CAAC;IAEvB,MAAM,UAAU,CAAC,MAAM,CAAC;QACvB;YACC,EAAE,EAAE,OAAO;YACX,OAAO,EAAE,SAAS;YAClB,aAAa;SACb;KACD,CAAC,CAAC;IAEH,MAAM,aAAa,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAE1C,IAAI,aAAa,CAAC,MAAM,GAAG,C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AC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAE1D,MAAM,WAAW,GAAG,OAAO,CAAC,KAAK,EAAE,CAAC;IACpC,OAAO,WAAW,EAAE,MAAM,EAAE,EAAE,IAAI,IAAI,CAAC;AACxC,CAAC;AAED;;;;;;GAMG;AACI,KAAK,UAAU,oBAAoB,CACzC,UAAsB,EACtB,IAAY;IAEZ,MAAM,WAAW,GAAG,IAAI,+BAAgB,CACvC,UAAU,EACV,cAAc,CACd,CAAC;IAEF,MAAM,QAAQ,GAAG,IAAI,sBAAQ,EAAE,CAAC;IAChC,QAAQ,CAAC,SAAS,CAAC,sBAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC;IAElD,MAAM,OAAO,GAAG,MAAM,WAAW,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAEnD,OAAO,OAAO,CAAC,KAAK,EAAE,EAAE,EAAE,IAAI,IAAI,CAAC;AACpC,CAAC;AAMD;;;;;;;;GAQG;AACI,KAAK,UAAU,iBAAiB,CACtC,UAAsB,EACtB,IAAY,EACZ,OAAiC;IAEjC,MAAM,UAAU,GAAG,IAAI,+BAAgB,CAAC,UAAU,EAAE,cAAc,CAAC,CAAC;IAEpE,MAAM,aAAa,GAAG,IAAA,mBAAI,GAAE,CAAC;IAE7B,MAAM,UAAU,CAAC,MAAM,CAAC;QACvB;YACC,EAAE,EAAE,aAAa;YACjB,IAAI;YACJ,QAAQ,EAAE,OAAO,CAAC,QAAQ,IAAI,IAAI;YAClC,aAAa,EAAE,EAAE;SACjB;KACD,CAAC,CAAC;IAEH,OAAO,aAAa,CAAC;AACtB,CAAC","sourcesContent":["import type { HttpClient } from \"../http-client.js\";\nimport { EntityRepository, uuid } from \"./admin-api.js\";\nimport { Criteria } from \"./criteria.js\";\n\n/**\n * Uploads a media file to the Shopware instance.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {Object} options - The options for uploading the media file.\n * @param {boolean} [options.private] - Whether the media file should be private.\n * @param {string} [options.mediaFolderId] - The ID of the media folder to upload the file to.\n * @param {string} options.fileName - The name of the file to upload.\n * @param {Blob|Promise<Blob>} options.file - The file to upload.\n */\nexport async function uploadMediaFile(\n\thttpClient: HttpClient,\n\t{\n\t\tprivate: isPrivate = false,\n\t\tmediaFolderId = null,\n\t\tfileName,\n\t\tfile,\n\t}: {\n\t\tprivate?: boolean;\n\t\tmediaFolderId?: string | null;\n\t\tfileName: string;\n\t\tfile: Blob | Promise<Blob>;\n\t},\n) {\n\tconst repository = new EntityRepository(httpClient, \"media\");\n\n\tconst mediaId = uuid();\n\n\tawait repository.upsert([\n\t\t{\n\t\t\tid: mediaId,\n\t\t\tprivate: isPrivate,\n\t\t\tmediaFolderId,\n\t\t},\n\t]);\n\n\tconst splitFileName = fileName.split(\".\");\n\n\tif (splitFileName.length < 2) {\n\t\tthrow new Error(\"Invalid file name, should have an extension\");\n\t}\n\n\tconst extension = (splitFileName.slice(-1)[0] || \"\").toLowerCase();\n\tconst baseFileName = splitFileName.slice(0, -1).join(\".\");\n\n\tconst params = new URLSearchParams();\n\n\tparams.append(\"extension\", extension);\n\tparams.append(\"fileName\", baseFileName);\n\n\tconst resolved = await file;\n\n\ttry {\n\t\tawait httpClient.post(\n\t\t\t`/_action/media/${mediaId}/upload?${params.toString()}`,\n\t\t\tresolved,\n\t\t\t{\n\t\t\t\t\"Content-Type\": resolved.type,\n\t\t\t},\n\t\t);\n\t} catch (e) {\n\t\tawait repository.delete([{ id: mediaId }]);\n\t\tthrow e;\n\t}\n}\n\n/**\n * Retrieves the default media folder ID for a given entity.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {string} entity - The entity name to get the default folder ID for.\n * @returns {Promise<string|null>} - The ID of the default media folder or null if not found.\n */\nexport async function getMediaDefaultFolderByEntity(\n\thttpClient: HttpClient,\n\tentity: string,\n): Promise<string | null> {\n\tconst mediaDefaultFolder = new EntityRepository<{ folder: { id: string } }>(\n\t\thttpClient,\n\t\t\"media_default_folder\",\n\t);\n\n\tconst criteria = new Criteria();\n\tcriteria.addFilter(Criteria.equals(\"entity\", entity));\n\n\tconst folders = await mediaDefaultFolder.search(criteria);\n\n\tconst firstFolder = folders.first();\n\treturn firstFolder?.folder?.id || null;\n}\n\n/**\n * Retrieves the media folder ID by its name.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {string} name - The name of the media folder.\n * @returns {Promise<string|null>} - The ID of the media folder or null if not found.\n */\nexport async function getMediaFolderByName(\n\thttpClient: HttpClient,\n\tname: string,\n): Promise<string | null> {\n\tconst mediaFolder = new EntityRepository<{ id: string }>(\n\t\thttpClient,\n\t\t\"media_folder\",\n\t);\n\n\tconst criteria = new Criteria();\n\tcriteria.addFilter(Criteria.equals(\"name\", name));\n\n\tconst folders = await mediaFolder.search(criteria);\n\n\treturn folders.first()?.id || null;\n}\n\ninterface CreateMediaFolderOptions {\n\tparentId?: string;\n}\n\n/**\n * Creates a new media folder.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {string} name - The name of the media folder.\n * @param {Object} options - Additional options for creating the media folder.\n * @param {string} [options.parentId] - The ID of the parent folder, if any.\n * @returns {Promise<string>} - The ID of the newly created media folder.\n */\nexport async function createMediaFolder(\n\thttpClient: HttpClient,\n\tname: string,\n\toptions: CreateMediaFolderOptions,\n): Promise<string> {\n\tconst repository = new EntityRepository(httpClient, \"media_folder\");\n\n\tconst mediaFolderId = uuid();\n\n\tawait repository.upsert([\n\t\t{\n\t\t\tid: mediaFolderId,\n\t\t\tname,\n\t\t\tparentId: options.parentId || null,\n\t\t\tconfiguration: {},\n\t\t},\n\t]);\n\n\treturn mediaFolderId;\n}\n"]}
1
+ {"version":3,"file":"media.js","sourceRoot":"","sources":["../../../src/helper/media.ts"],"names":[],"mappings":";;AAeA,0CAwDC;AASD,sEAgBC;AASD,oDAeC;AAeD,8CAmBC;AAzJD,iDAAwD;AACxD,+CAAyC;AAEzC;;;;;;;;;;GAUG;AACI,KAAK,U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AAU,EAAE,cAAc,CAAC,CAAC;IAEpE,MAAM,aAAa,GAAG,IAAA,mBAAI,GAAE,CAAC;IAE7B,MAAM,UAAU,CAAC,MAAM,CAAC;QACvB;YACC,EAAE,EAAE,aAAa;YACjB,IAAI;YACJ,QAAQ,EAAE,OAAO,CAAC,QAAQ,IAAI,IAAI;YAClC,aAAa,EAAE,EAAE;SACjB;KACD,CAAC,CAAC;IAEH,OAAO,aAAa,CAAC;AACtB,CAAC","sourcesContent":["import type { HttpClient } from \"../http-client.js\";\nimport { EntityRepository, uuid } from \"./admin-api.js\";\nimport { Criteria } from \"./criteria.js\";\n\n/**\n * Uploads a media file to the Shopware instance.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {Object} options - The options for uploading the media file.\n * @param {boolean} [options.private] - Whether the media file should be private.\n * @param {string} [options.mediaFolderId] - The ID of the media folder to upload the file to.\n * @param {string} options.fileName - The name of the file to upload.\n * @param {Blob|Promise<Blob>} options.file - The file to upload.\n * @returns {Promise<string>} - The ID of the uploaded media file.\n */\nexport async function uploadMediaFile(\n\thttpClient: HttpClient,\n\t{\n\t\tprivate: isPrivate = false,\n\t\tmediaFolderId = null,\n\t\tfileName,\n\t\tfile,\n\t}: {\n\t\tprivate?: boolean;\n\t\tmediaFolderId?: string | null;\n\t\tfileName: string;\n\t\tfile: Blob | Promise<Blob>;\n\t},\n): Promise<string> {\n\tconst repository = new EntityRepository(httpClient, \"media\");\n\n\tconst mediaId = uuid();\n\n\tawait repository.upsert([\n\t\t{\n\t\t\tid: mediaId,\n\t\t\tprivate: isPrivate,\n\t\t\tmediaFolderId,\n\t\t},\n\t]);\n\n\tconst splitFileName = fileName.split(\".\");\n\n\tif (splitFileName.length < 2) {\n\t\tthrow new Error(\"Invalid file name, should have an extension\");\n\t}\n\n\tconst extension = (splitFileName.slice(-1)[0] || \"\").toLowerCase();\n\tconst baseFileName = splitFileName.slice(0, -1).join(\".\");\n\n\tconst params = new URLSearchParams();\n\n\tparams.append(\"extension\", extension);\n\tparams.append(\"fileName\", baseFileName);\n\n\tconst resolved = await file;\n\n\ttry {\n\t\tawait httpClient.post(\n\t\t\t`/_action/media/${mediaId}/upload?${params.toString()}`,\n\t\t\tresolved,\n\t\t\t{\n\t\t\t\t\"Content-Type\": resolved.type,\n\t\t\t},\n\t\t);\n\n\t\treturn mediaId;\n\t} catch (e) {\n\t\tawait repository.delete([{ id: mediaId }]);\n\t\tthrow e;\n\t}\n}\n\n/**\n * Retrieves the default media folder ID for a given entity.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {string} entity - The entity name to get the default folder ID for.\n * @returns {Promise<string|null>} - The ID of the default media folder or null if not found.\n */\nexport async function getMediaDefaultFolderByEntity(\n\thttpClient: HttpClient,\n\tentity: string,\n): Promise<string | null> {\n\tconst mediaDefaultFolder = new EntityRepository<{ folder: { id: string } }>(\n\t\thttpClient,\n\t\t\"media_default_folder\",\n\t);\n\n\tconst criteria = new Criteria();\n\tcriteria.addFilter(Criteria.equals(\"entity\", entity));\n\n\tconst folders = await mediaDefaultFolder.search(criteria);\n\n\tconst firstFolder = folders.first();\n\treturn firstFolder?.folder?.id || null;\n}\n\n/**\n * Retrieves the media folder ID by its name.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {string} name - The name of the media folder.\n * @returns {Promise<string|null>} - The ID of the media folder or null if not found.\n */\nexport async function getMediaFolderByName(\n\thttpClient: HttpClient,\n\tname: string,\n): Promise<string | null> {\n\tconst mediaFolder = new EntityRepository<{ id: string }>(\n\t\thttpClient,\n\t\t\"media_folder\",\n\t);\n\n\tconst criteria = new Criteria();\n\tcriteria.addFilter(Criteria.equals(\"name\", name));\n\n\tconst folders = await mediaFolder.search(criteria);\n\n\treturn folders.first()?.id || null;\n}\n\ninterface CreateMediaFolderOptions {\n\tparentId?: string;\n}\n\n/**\n * Creates a new media folder.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {string} name - The name of the media folder.\n * @param {Object} options - Additional options for creating the media folder.\n * @param {string} [options.parentId] - The ID of the parent folder, if any.\n * @returns {Promise<string>} - The ID of the newly created media folder.\n */\nexport async function createMediaFolder(\n\thttpClient: HttpClient,\n\tname: string,\n\toptions: CreateMediaFolderOptions,\n): Promise<string> {\n\tconst repository = new EntityRepository(httpClient, \"media_folder\");\n\n\tconst mediaFolderId = uuid();\n\n\tawait repository.upsert([\n\t\t{\n\t\t\tid: mediaFolderId,\n\t\t\tname,\n\t\t\tparentId: options.parentId || null,\n\t\t\tconfiguration: {},\n\t\t},\n\t]);\n\n\treturn mediaFolderId;\n}\n"]}
@@ -8,13 +8,14 @@ import type { HttpClient } from "../http-client.js";
8
8
  * @param {string} [options.mediaFolderId] - The ID of the media folder to upload the file to.
9
9
  * @param {string} options.fileName - The name of the file to upload.
10
10
  * @param {Blob|Promise<Blob>} options.file - The file to upload.
11
+ * @returns {Promise<string>} - The ID of the uploaded media file.
11
12
  */
12
13
  export declare function uploadMediaFile(httpClient: HttpClient, { private: isPrivate, mediaFolderId, fileName, file, }: {
13
14
  private?: boolean;
14
15
  mediaFolderId?: string | null;
15
16
  fileName: string;
16
17
  file: Blob | Promise<Blob>;
17
- }): Promise<void>;
18
+ }): Promise<string>;
18
19
  /**
19
20
  * Retrieves the default media folder ID for a given entity.
20
21
  *
@@ -1 +1 @@
1
- {"version":3,"file":"media.d.ts","sourceRoot":"","sources":["../../../src/helper/media.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAIpD;;;;;;;;;GASG;AACH,wBAAsB,eAAe,CACpC,UAAU,EAAE,UAAU,EACtB,EACC,OAAO,EAAE,SAAiB,EAC1B,aAAoB,EACpB,QAAQ,EACR,IAAI,GACJ,EAAE;IACF,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,IAAI,GAAG,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B,IAAI,EAAE,MAAM,GACV,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAYxB;AAED,UAAU,wBAAwB;IACjC,QAAQ,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;;GAQG;AACH,wBAAsB,iBAAiB,CACtC,UAAU,EAAE,UAAU,EACtB,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,wBAAwB,GAC/B,OAAO,CAAC,MAAM,CAAC,CAejB"}
@@ -9,6 +9,7 @@ import { Criteria } from "./criteria.js";
9
9
  * @param {string} [options.mediaFolderId] - The ID of the media folder to upload the file to.
10
10
  * @param {string} options.fileName - The name of the file to upload.
11
11
  * @param {Blob|Promise<Blob>} options.file - The file to upload.
12
+ * @returns {Promise<string>} - The ID of the uploaded media file.
12
13
  */
13
14
  export async function uploadMediaFile(httpClient, { private: isPrivate = false, mediaFolderId = null, fileName, file, }) {
14
15
  const repository = new EntityRepository(httpClient, "media");
@@ -34,6 +35,7 @@ export async function uploadMediaFile(httpClient, { private: isPrivate = false,
34
35
  await httpClient.post(`/_action/media/${mediaId}/upload?${params.toString()}`, resolved, {
35
36
  "Content-Type": resolved.type,
36
37
  });
38
+ return mediaId;
37
39
  }
38
40
  catch (e) {
39
41
  await repository.delete([{ id: mediaId }]);
@@ -1 +1 @@
1
- {"version":3,"file":"media.js","sourceRoot":"","sources":["../../../src/helper/media.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEzC;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACpC,UAAsB,EACtB,EACC,OAAO,EAAE,SAAS,GAAG,KAAK,EAC1B,aAAa,GAAG,IAAI,EACpB,QAAQ,EACR,IAAI,GAMJ;IAED,MAAM,UAAU,GAAG,IAAI,gBAAgB,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;IAE7D,M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C,aAAa,EAAE,EAAE;SACjB;KACD,CAAC,CAAC;IAEH,OAAO,aAAa,CAAC;AACtB,CAAC","sourcesContent":["import type { HttpClient } from \"../http-client.js\";\nimport { EntityRepository, uuid } from \"./admin-api.js\";\nimport { Criteria } from \"./criteria.js\";\n\n/**\n * Uploads a media file to the Shopware instance.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {Object} options - The options for uploading the media file.\n * @param {boolean} [options.private] - Whether the media file should be private.\n * @param {string} [options.mediaFolderId] - The ID of the media folder to upload the file to.\n * @param {string} options.fileName - The name of the file to upload.\n * @param {Blob|Promise<Blob>} options.file - The file to upload.\n */\nexport async function uploadMediaFile(\n\thttpClient: HttpClient,\n\t{\n\t\tprivate: isPrivate = false,\n\t\tmediaFolderId = null,\n\t\tfileName,\n\t\tfile,\n\t}: {\n\t\tprivate?: boolean;\n\t\tmediaFolderId?: string | null;\n\t\tfileName: string;\n\t\tfile: Blob | Promise<Blob>;\n\t},\n) {\n\tconst repository = new EntityRepository(httpClient, \"media\");\n\n\tconst mediaId = uuid();\n\n\tawait repository.upsert([\n\t\t{\n\t\t\tid: mediaId,\n\t\t\tprivate: isPrivate,\n\t\t\tmediaFolderId,\n\t\t},\n\t]);\n\n\tconst splitFileName = fileName.split(\".\");\n\n\tif (splitFileName.length < 2) {\n\t\tthrow new Error(\"Invalid file name, should have an extension\");\n\t}\n\n\tconst extension = (splitFileName.slice(-1)[0] || \"\").toLowerCase();\n\tconst baseFileName = splitFileName.slice(0, -1).join(\".\");\n\n\tconst params = new URLSearchParams();\n\n\tparams.append(\"extension\", extension);\n\tparams.append(\"fileName\", baseFileName);\n\n\tconst resolved = await file;\n\n\ttry {\n\t\tawait httpClient.post(\n\t\t\t`/_action/media/${mediaId}/upload?${params.toString()}`,\n\t\t\tresolved,\n\t\t\t{\n\t\t\t\t\"Content-Type\": resolved.type,\n\t\t\t},\n\t\t);\n\t} catch (e) {\n\t\tawait repository.delete([{ id: mediaId }]);\n\t\tthrow e;\n\t}\n}\n\n/**\n * Retrieves the default media folder ID for a given entity.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {string} entity - The entity name to get the default folder ID for.\n * @returns {Promise<string|null>} - The ID of the default media folder or null if not found.\n */\nexport async function getMediaDefaultFolderByEntity(\n\thttpClient: HttpClient,\n\tentity: string,\n): Promise<string | null> {\n\tconst mediaDefaultFolder = new EntityRepository<{ folder: { id: string } }>(\n\t\thttpClient,\n\t\t\"media_default_folder\",\n\t);\n\n\tconst criteria = new Criteria();\n\tcriteria.addFilter(Criteria.equals(\"entity\", entity));\n\n\tconst folders = await mediaDefaultFolder.search(criteria);\n\n\tconst firstFolder = folders.first();\n\treturn firstFolder?.folder?.id || null;\n}\n\n/**\n * Retrieves the media folder ID by its name.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {string} name - The name of the media folder.\n * @returns {Promise<string|null>} - The ID of the media folder or null if not found.\n */\nexport async function getMediaFolderByName(\n\thttpClient: HttpClient,\n\tname: string,\n): Promise<string | null> {\n\tconst mediaFolder = new EntityRepository<{ id: string }>(\n\t\thttpClient,\n\t\t\"media_folder\",\n\t);\n\n\tconst criteria = new Criteria();\n\tcriteria.addFilter(Criteria.equals(\"name\", name));\n\n\tconst folders = await mediaFolder.search(criteria);\n\n\treturn folders.first()?.id || null;\n}\n\ninterface CreateMediaFolderOptions {\n\tparentId?: string;\n}\n\n/**\n * Creates a new media folder.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {string} name - The name of the media folder.\n * @param {Object} options - Additional options for creating the media folder.\n * @param {string} [options.parentId] - The ID of the parent folder, if any.\n * @returns {Promise<string>} - The ID of the newly created media folder.\n */\nexport async function createMediaFolder(\n\thttpClient: HttpClient,\n\tname: string,\n\toptions: CreateMediaFolderOptions,\n): Promise<string> {\n\tconst repository = new EntityRepository(httpClient, \"media_folder\");\n\n\tconst mediaFolderId = uuid();\n\n\tawait repository.upsert([\n\t\t{\n\t\t\tid: mediaFolderId,\n\t\t\tname,\n\t\t\tparentId: options.parentId || null,\n\t\t\tconfiguration: {},\n\t\t},\n\t]);\n\n\treturn mediaFolderId;\n}\n"]}
1
+ {"version":3,"file":"media.js","sourceRoot":"","sources":["../../../src/helper/media.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEzC;;;;;;;;;;GAUG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACpC,UAAsB,EACtB,EACC,OAAO,EAAE,SAAS,GAAG,KAAK,EAC1B,aAAa,GAAG,IAAI,EACpB,QAAQ,EACR,IAAI,GAMJ;IAED,MAAM,UAAU,GAAG,IAAI,gBAAgB,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;IAE7D,MAAM,OAAO,GAAG,IAAI,EAAE,CAAC;IAEvB,MAAM,UAAU,CAAC,MAAM,CAAC;QACvB;YACC,EAAE,EAAE,OAAO;YACX,OAAO,EAAE,SAAS;YAClB,aAAa;SACb;KACD,CAAC,CAAC;IAEH,MAAM,aAAa,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAE1C,IAAI,aAAa,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC9B,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAC;IAChE,CAAC;IAED,MAAM,SAAS,GAAG,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC;IACnE,MAAM,YAAY,GAAG,aAAa,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAE1D,MAAM,MAAM,GAAG,IAAI,eAAe,EAAE,CAAC;IAErC,MAAM,CAAC,MAAM,CAAC,WAAW,EAAE,SAAS,CAAC,CAAC;IAC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,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC;IAElD,MAAM,OAAO,GAAG,MAAM,WAAW,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAEnD,OAAO,OAAO,CAAC,KAAK,EAAE,EAAE,EAAE,IAAI,IAAI,CAAC;AACpC,CAAC;AAMD;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACtC,UAAsB,EACtB,IAAY,EACZ,OAAiC;IAEjC,MAAM,UAAU,GAAG,IAAI,gBAAgB,CAAC,UAAU,EAAE,cAAc,CAAC,CAAC;IAEpE,MAAM,aAAa,GAAG,IAAI,EAAE,CAAC;IAE7B,MAAM,UAAU,CAAC,MAAM,CAAC;QACvB;YACC,EAAE,EAAE,aAAa;YACjB,IAAI;YACJ,QAAQ,EAAE,OAAO,CAAC,QAAQ,IAAI,IAAI;YAClC,aAAa,EAAE,EAAE;SACjB;KACD,CAAC,CAAC;IAEH,OAAO,aAAa,CAAC;AACtB,CAAC","sourcesContent":["import type { HttpClient } from \"../http-client.js\";\nimport { EntityRepository, uuid } from \"./admin-api.js\";\nimport { Criteria } from \"./criteria.js\";\n\n/**\n * Uploads a media file to the Shopware instance.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {Object} options - The options for uploading the media file.\n * @param {boolean} [options.private] - Whether the media file should be private.\n * @param {string} [options.mediaFolderId] - The ID of the media folder to upload the file to.\n * @param {string} options.fileName - The name of the file to upload.\n * @param {Blob|Promise<Blob>} options.file - The file to upload.\n * @returns {Promise<string>} - The ID of the uploaded media file.\n */\nexport async function uploadMediaFile(\n\thttpClient: HttpClient,\n\t{\n\t\tprivate: isPrivate = false,\n\t\tmediaFolderId = null,\n\t\tfileName,\n\t\tfile,\n\t}: {\n\t\tprivate?: boolean;\n\t\tmediaFolderId?: string | null;\n\t\tfileName: string;\n\t\tfile: Blob | Promise<Blob>;\n\t},\n): Promise<string> {\n\tconst repository = new EntityRepository(httpClient, \"media\");\n\n\tconst mediaId = uuid();\n\n\tawait repository.upsert([\n\t\t{\n\t\t\tid: mediaId,\n\t\t\tprivate: isPrivate,\n\t\t\tmediaFolderId,\n\t\t},\n\t]);\n\n\tconst splitFileName = fileName.split(\".\");\n\n\tif (splitFileName.length < 2) {\n\t\tthrow new Error(\"Invalid file name, should have an extension\");\n\t}\n\n\tconst extension = (splitFileName.slice(-1)[0] || \"\").toLowerCase();\n\tconst baseFileName = splitFileName.slice(0, -1).join(\".\");\n\n\tconst params = new URLSearchParams();\n\n\tparams.append(\"extension\", extension);\n\tparams.append(\"fileName\", baseFileName);\n\n\tconst resolved = await file;\n\n\ttry {\n\t\tawait httpClient.post(\n\t\t\t`/_action/media/${mediaId}/upload?${params.toString()}`,\n\t\t\tresolved,\n\t\t\t{\n\t\t\t\t\"Content-Type\": resolved.type,\n\t\t\t},\n\t\t);\n\n\t\treturn mediaId;\n\t} catch (e) {\n\t\tawait repository.delete([{ id: mediaId }]);\n\t\tthrow e;\n\t}\n}\n\n/**\n * Retrieves the default media folder ID for a given entity.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {string} entity - The entity name to get the default folder ID for.\n * @returns {Promise<string|null>} - The ID of the default media folder or null if not found.\n */\nexport async function getMediaDefaultFolderByEntity(\n\thttpClient: HttpClient,\n\tentity: string,\n): Promise<string | null> {\n\tconst mediaDefaultFolder = new EntityRepository<{ folder: { id: string } }>(\n\t\thttpClient,\n\t\t\"media_default_folder\",\n\t);\n\n\tconst criteria = new Criteria();\n\tcriteria.addFilter(Criteria.equals(\"entity\", entity));\n\n\tconst folders = await mediaDefaultFolder.search(criteria);\n\n\tconst firstFolder = folders.first();\n\treturn firstFolder?.folder?.id || null;\n}\n\n/**\n * Retrieves the media folder ID by its name.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {string} name - The name of the media folder.\n * @returns {Promise<string|null>} - The ID of the media folder or null if not found.\n */\nexport async function getMediaFolderByName(\n\thttpClient: HttpClient,\n\tname: string,\n): Promise<string | null> {\n\tconst mediaFolder = new EntityRepository<{ id: string }>(\n\t\thttpClient,\n\t\t\"media_folder\",\n\t);\n\n\tconst criteria = new Criteria();\n\tcriteria.addFilter(Criteria.equals(\"name\", name));\n\n\tconst folders = await mediaFolder.search(criteria);\n\n\treturn folders.first()?.id || null;\n}\n\ninterface CreateMediaFolderOptions {\n\tparentId?: string;\n}\n\n/**\n * Creates a new media folder.\n *\n * @param {HttpClient} httpClient - The HTTP client instance.\n * @param {string} name - The name of the media folder.\n * @param {Object} options - Additional options for creating the media folder.\n * @param {string} [options.parentId] - The ID of the parent folder, if any.\n * @returns {Promise<string>} - The ID of the newly created media folder.\n */\nexport async function createMediaFolder(\n\thttpClient: HttpClient,\n\tname: string,\n\toptions: CreateMediaFolderOptions,\n): Promise<string> {\n\tconst repository = new EntityRepository(httpClient, \"media_folder\");\n\n\tconst mediaFolderId = uuid();\n\n\tawait repository.upsert([\n\t\t{\n\t\t\tid: mediaFolderId,\n\t\t\tname,\n\t\t\tparentId: options.parentId || null,\n\t\t\tconfiguration: {},\n\t\t},\n\t]);\n\n\treturn mediaFolderId;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shopware-ag/app-server-sdk",
3
- "version": "1.1.8",
3
+ "version": "1.1.9",
4
4
  "description": "App Server SDK for JavaScript",
5
5
  "type": "module",
6
6
  "license": "MIT",