1 import { HttpTypes, SelectParams } from "@medusajs/types"
2 import { Client } from "../client.js"
3 import { ClientHeaders } from "../types.js"
4
5 export class Upload {
6 /**
7 * @ignore
8 */
9 private client: Client
10 /**
11 * @ignore
12 */
13 constructor(client: Client) {
14 this.client = client
15 }
16
17 /**
18 * This method creates a new upload. It sends a request to the
19 * [Upload Files](https://docs.medusajs.com/api/admin/uploads/upload-files)
20 * API route.
21 *
22 * @param body - The details of the files to upload.
23 * @param query - Configure the fields and relations to retrieve in the uploaded files.
24 * @param headers - Headers to pass in the request.
25 * @returns The upload files' details.
26 *
27 * @privateRemarks
28 *
29 * Note: The creation/upload flow be made more advanced, with support for streaming and progress, but for now we keep it simple
30 *
31 * @example
32 * sdk.admin.upload.create(
33 * {
34 * files: [
35 * // file uploaded as a binary string
36 * {
37 * name: "test.txt",
38 * content: "test", // Should be the binary string of the file
39 * },
40 * // file uploaded as a File object
41 * new File(["test"], "test.txt", { type: "text/plain" })
42 * ],
43 * }
44 * )
45 * .then(({ files }) => {
46 * console.log(files)
47 * })
48 */
49 async create(
50 body: HttpTypes.AdminUploadFile,
51 query?: SelectParams,
52 headers?: ClientHeaders
53 ) {
54 const form = new FormData()
55 if (body instanceof FileList) {
56 Array.from(body).forEach((file) => {
57 form.append("files", file)
58 })
59 } else {
60 body.files.forEach((file) => {
61 form.append(
62 "files",
63 "content" in file
64 ? new Blob([file.content], {
65 type: "text/plain",
66 })
67 : file,
68 file.name
69 )
70 })
71 }
72
73 return this.client.fetch<HttpTypes.AdminFileListResponse>(
74 `/admin/uploads`,
75 {
76 method: "POST",
77 headers: {
78 ...headers,
79 // Let the browser determine the content type.
80 "content-type": null,
81 },
82 body: form,
83 query,
84 }
85 )
86 }
87
88 /**
89 * This method retrieves a file's details by its ID. It sends a request to the
90 * [Get File](https://docs.medusajs.com/api/admin/uploads/get-a-file)
91 * API route.
92 *
93 * @param id - The ID of the file to retrieve.
94 * @param query - Query parameters to pass in the request.
95 * @param headers - Headers to pass in the request.
96 * @returns The file's details.
97 *
98 * @example
99 * sdk.admin.upload.retrieve("test.txt")
100 * .then(({ file }) => {
101 * console.log(file)
102 * })
103 */
104 async retrieve(id: string, query?: SelectParams, headers?: ClientHeaders) {
105 return this.client.fetch<HttpTypes.AdminFileResponse>(
106 `/admin/uploads/${id}`,
107 {
108 query,
109 headers,
110 }
111 )
112 }
113
114 /**
115 * This method deletes a file by its ID from the configured File Module Provider. It sends a request to the
116 * [Delete File](https://docs.medusajs.com/api/admin/uploads/delete-a-file)
117 * API route.
118 *
119 * @param id - The ID of the file to delete.
120 * @param headers - Headers to pass in the request.
121 * @returns The deletion's details.
122 *
123 * @example
124 * sdk.admin.upload.delete("test.txt")
125 * .then(({ deleted }) => {
126 * console.log(deleted)
127 * })
128 */
129 async delete(id: string, headers?: ClientHeaders) {
130 return this.client.fetch<HttpTypes.AdminFileDeleteResponse>(
131 `/admin/uploads/${id}`,
132 {
133 method: "DELETE",
134 headers,
135 }
136 )
137 }
138
139 /**
140 * This method creates a presigned URL for a file upload. It sends a request to the
141 * `/admin/uploads/presigned-urls` API route.
142 *
143 * @param body - The details of the file to upload.
144 * @param query - Query parameters to pass in the request.
145 * @param headers - Headers to pass in the request.
146 * @returns The presigned URL for the file upload.
147 *
148 * @example
149 * sdk.admin.upload.presignedUrl({
150 * name: "test.txt",
151 * size: 1000,
152 * type: "text/plain",
153 * }))
154 */
155 async presignedUrl(
156 body: HttpTypes.AdminUploadPreSignedUrlRequest,
157 query?: SelectParams,
158 headers?: ClientHeaders
159 ) {
160 return this.client.fetch<HttpTypes.AdminUploadPreSignedUrlResponse>(
161 `/admin/uploads/presigned-urls`,
162 {
163 method: "POST",
164 headers,
165 body,
166 query,
167 }
168 )
169 }
170 }