Skip to content

Commit 4311dc4

Browse files
committed
no longer accept rest at objects.delete
1 parent 9cd4d56 commit 4311dc4

File tree

4 files changed

+48
-33
lines changed

4 files changed

+48
-33
lines changed

.changeset/big-houses-taste.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@squarecloud/blob": patch
3+
---
4+
5+
No longer accept rest arrays at `objects.delete`

src/managers/api.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ export class APIManager {
3131
.filter(([, value]) => Boolean(value))
3232
.map(([key, value]) => [key, String(value)]),
3333
);
34-
const params = new URLSearchParams(paramsObject).toString();
34+
const params = new URLSearchParams(paramsObject);
35+
36+
const { params: _, headers, body, ...rest } = options;
3537

36-
const { method, headers, body, ...rest } = options;
3738
const init: RequestInit = {
38-
method: method || "GET",
39-
headers: { Authorization: this.apiKey, ...(headers || {}) },
4039
...rest,
40+
headers: { ...(headers || {}), Authorization: this.apiKey },
4141
};
4242

4343
if (body) {

src/managers/objects.ts

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { SquareCloudBlob } from "..";
22
import { BlobObject } from "../structures/object";
33
import type { CreateObjectResponse, CreateObjectType } from "../types/create";
44
import type { ListObjectsResponse } from "../types/list";
5-
import { MimeTypeUtil } from "../utils/mimetype";
65
import { parsePathLike } from "../utils/path-like";
76
import { assertCreateObjectResponse } from "../validation/assertions/create";
87
import { assertListObjectsResponse } from "../validation/assertions/list";
@@ -16,7 +15,7 @@ export class ObjectsManager {
1615
*
1716
* @example
1817
* ```js
19-
* const objects = await blob.objects.list();
18+
* blob.objects.list();
2019
* ```
2120
*/
2221
async list() {
@@ -28,17 +27,19 @@ export class ObjectsManager {
2827
return [];
2928
}
3029

31-
return list.objects.map(
32-
(object) =>
33-
new BlobObject({
34-
idOrUrl: object.id,
35-
size: object.size,
36-
createdAt: new Date(object.created_at),
37-
expiresAt: object.expires_at
38-
? new Date(object.expires_at)
39-
: undefined,
40-
}),
41-
);
30+
return list.objects.map((objectData) => {
31+
const createdAt = new Date(objectData.created_at);
32+
const expiresAt = objectData.expires_at
33+
? new Date(objectData.expires_at)
34+
: undefined;
35+
36+
return new BlobObject({
37+
idOrUrl: objectData.id,
38+
size: objectData.size,
39+
createdAt,
40+
expiresAt,
41+
});
42+
});
4243
}
4344

4445
/**
@@ -48,27 +49,31 @@ export class ObjectsManager {
4849
*
4950
* @example
5051
* ```js
51-
* await blob.objects.create({ file: "path/to/file.jpeg", name: "my_image" });
52+
* // Basic usage with absolute path
53+
* blob.objects.create({
54+
* file: "path/to/file.jpeg",
55+
* name: "my_image"
56+
* });
57+
*
58+
* // Advanced usage with Buffer
59+
* blob.objects.create({
60+
* file: Buffer.from("content"),
61+
* name: "my_image",
62+
* mimeType: "image/jpeg"
63+
* })
5264
* ```
5365
*/
5466
async create(object: CreateObjectType) {
5567
const payload = createObjectPayloadSchema.parse(object);
5668
const file = await parsePathLike(payload.file);
57-
const mimeType =
58-
typeof object.file === "string"
59-
? MimeTypeUtil.fromExtension(object.file.split(".")[1])
60-
: object.mimeType;
69+
const type = payload.mimeType || object.mimeType;
6170

6271
const formData = new FormData();
63-
formData.append("file", new Blob([file], { type: mimeType }));
72+
formData.append("file", new Blob([file], { type }));
6473

6574
const { response } = await this.client.api.request<CreateObjectResponse>(
6675
"objects",
67-
{
68-
method: "POST",
69-
body: formData,
70-
params: payload.params,
71-
},
76+
{ method: "POST", body: formData, params: payload.params },
7277
);
7378

7479
const objectData = assertCreateObjectResponse(response);
@@ -86,15 +91,16 @@ export class ObjectsManager {
8691
*
8792
* @example
8893
* ```js
89-
* await blob.objects.delete("ID/prefix/name1_xxx-xxx.mp4", "ID/prefix/name_xxx-xxx-xxx.png");
94+
* blob.objects.delete([
95+
* "userId/prefix/name1_xxx-xxx.mp4",
96+
* "userId/prefix/name_xxx-xxx-xxx.png"
97+
* ]);
9098
* ```
9199
*/
92-
async delete(...objects: string[] | string[][]) {
93-
const ids = objects.flat();
94-
100+
async delete(objects: string[]) {
95101
const { status } = await this.client.api.request("objects", {
96102
method: "DELETE",
97-
body: { objects: ids },
103+
body: { objects },
98104
});
99105

100106
return status === "success";

src/validation/schemas/create.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ export const createObjectSchema = z
2727
export const createObjectPayloadSchema = createObjectSchema.transform(
2828
({ file, securityHash, autoDownload, expiresIn, ...rest }) => ({
2929
file,
30+
mimeType:
31+
typeof file === "string"
32+
? MimeTypeUtil.fromExtension(file.split(".")[1])
33+
: undefined,
3034
params: {
3135
...rest,
3236
expire: expiresIn,

0 commit comments

Comments
 (0)