Skip to content

Commit a88b29b

Browse files
committed
update api routes and rename objects.put to create
1 parent 5e053b2 commit a88b29b

File tree

13 files changed

+54
-46
lines changed

13 files changed

+54
-46
lines changed

.changeset/giant-zoos-compete.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@squarecloud/blob": major
3+
---
4+
5+
Rename `objects.put` and related to `objects.create`

.changeset/warm-beds-fry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@squarecloud/blob": major
3+
---
4+
5+
Update to new Blob API routes

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const objects = await blob.objects.list()
4747
- _Check supported file types [here](https://docs.squarecloud.app/services/blob#supported-file-types)._
4848

4949
```ts
50-
const blobObject = await blob.objects.put({
50+
const blobObject = await blob.objects.create({
5151
file: "path/to/file.png", // Absolute path to your file
5252
name: "my_image", // File name without extension
5353
})
@@ -61,7 +61,7 @@ console.log(blobObject.url)
6161
import { MimeTypes } from "@squarecloud/blob"
6262
// CommonJS => const { MimeTypes } = require("@squarecloud/blob")
6363

64-
const blobObject = await blob.objects.put({
64+
const blobObject = await blob.objects.create({
6565
file: Buffer.from("content"),
6666
name: "my_image",
6767
mimeType: MimeTypes.IMAGE_JPEG, // Also accepts an string "image/jpeg"

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { APIManager } from "./managers/api";
2-
import { BlobObjectsManager } from "./managers/objects";
2+
import { ObjectsManager } from "./managers/objects";
33

44
export class SquareCloudBlob {
55
public static apiInfo = {
@@ -8,13 +8,13 @@ export class SquareCloudBlob {
88
};
99

1010
public readonly api: APIManager;
11-
public readonly objects = new BlobObjectsManager(this);
11+
public readonly objects = new ObjectsManager(this);
1212

1313
constructor(apiKey: string) {
1414
this.api = new APIManager(apiKey);
1515
}
1616
}
1717

18+
export * from "./types/create";
1819
export * from "./types/list";
19-
export * from "./types/put";
2020
export { MimeTypes } from "./utils/mimetype";

src/managers/objects.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import type { SquareCloudBlob } from "..";
22
import { SquareCloudBlobError } from "../structures/error";
3+
import type { CreateObjectResponse, CreateObjectType } from "../types/create";
34
import type { ListObjectsResponse } from "../types/list";
4-
import type { PutObjectResponse, PutObjectType } from "../types/put";
55
import { getMimeTypeFromExtension } from "../utils/mimetype";
66
import { parsePathLike } from "../utils/pathlike";
7+
import { assertCreateObjectResponse } from "../validation/assertions/create";
78
import { assertListObjectsResponse } from "../validation/assertions/list";
8-
import { assertPutObjectResponse } from "../validation/assertions/put";
9-
import { putObjectPayloadSchema } from "../validation/schemas/put";
9+
import { createObjectPayloadSchema } from "../validation/schemas/create";
1010

11-
export class BlobObjectsManager {
11+
export class ObjectsManager {
1212
constructor(private readonly client: SquareCloudBlob) {}
1313

1414
/**
@@ -21,7 +21,7 @@ export class BlobObjectsManager {
2121
*/
2222
async list() {
2323
const { response } =
24-
await this.client.api.request<ListObjectsResponse>("list");
24+
await this.client.api.request<ListObjectsResponse>("objects");
2525

2626
return assertListObjectsResponse(response)?.objects;
2727
}
@@ -33,11 +33,11 @@ export class BlobObjectsManager {
3333
*
3434
* @example
3535
* ```js
36-
* await blob.objects.put({ file: "path/to/file.jpeg", name: "my_image" });
36+
* await blob.objects.create({ file: "path/to/file.jpeg", name: "my_image" });
3737
* ```
3838
*/
39-
async put(object: PutObjectType) {
40-
const payload = putObjectPayloadSchema.parse(object);
39+
async create(object: CreateObjectType) {
40+
const payload = createObjectPayloadSchema.parse(object);
4141
const file = await parsePathLike(payload.file);
4242
const mimeType =
4343
typeof object.file === "string"
@@ -47,16 +47,16 @@ export class BlobObjectsManager {
4747
const formData = new FormData();
4848
formData.append("file", new Blob([file], { type: mimeType }));
4949

50-
const { response } = await this.client.api.request<PutObjectResponse>(
51-
"put",
50+
const { response } = await this.client.api.request<CreateObjectResponse>(
51+
"objects",
5252
{
53-
method: "PUT",
53+
method: "POST",
5454
body: formData,
5555
params: payload.params,
5656
},
5757
);
5858

59-
return assertPutObjectResponse(response);
59+
return assertCreateObjectResponse(response);
6060
}
6161

6262
/**
@@ -72,12 +72,12 @@ export class BlobObjectsManager {
7272
async delete(...objects: string[] | string[][]) {
7373
const ids = objects.flat();
7474

75-
const response = await this.client.api.request("delete", {
75+
const { status } = await this.client.api.request("objects", {
7676
method: "DELETE",
7777
body: { objects: ids },
7878
});
7979

80-
return response.status === "success";
80+
return status === "success";
8181
}
8282

8383
/**

src/types/assertions.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@ export type LiteralAssertionProps = BaseAssertionProps & {
1212

1313
export type APIObjectAssertionProps = BaseAssertionProps & {
1414
code: string;
15-
route: string;
1615
};

src/types/create.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { z } from "zod";
2+
import type {
3+
createObjectResponseSchema,
4+
createObjectSchema,
5+
} from "../validation/schemas/create";
6+
7+
export type CreateObjectType = z.infer<typeof createObjectSchema>;
8+
export type CreateObjectResponse = z.infer<typeof createObjectResponseSchema>;

src/types/put.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/validation/assertions/create.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { CreateObjectResponse } from "../../types/create";
2+
import { createObjectResponseSchema } from "../schemas/create";
3+
import { handleAPIObjectAssertion } from "./handlers";
4+
5+
export function assertCreateObjectResponse(
6+
value: unknown,
7+
): CreateObjectResponse {
8+
return handleAPIObjectAssertion({
9+
schema: createObjectResponseSchema,
10+
code: "CREATE_OBJECT",
11+
value,
12+
});
13+
}

src/validation/assertions/handlers.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ export function handleAPIObjectAssertion({
2828
schema,
2929
value,
3030
code,
31-
route,
3231
}: APIObjectAssertionProps) {
3332
const name = code.toLowerCase().replaceAll("_", " ");
3433

@@ -42,7 +41,7 @@ export function handleAPIObjectAssertion({
4241

4342
throw new SquareCloudBlobError(
4443
`INVALID_API_${code}`,
45-
`Invalid ${name} object received from API ${route}`,
44+
`Invalid ${name} object received from API`,
4645
{ cause },
4746
);
4847
}

src/validation/assertions/list.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export function assertListObjectsResponse(value: unknown): ListObjectsResponse {
66
return handleAPIObjectAssertion({
77
schema: listObjectsResponseSchema,
88
code: "LIST_OBJECTS",
9-
route: "/list",
109
value,
1110
});
1211
}

src/validation/assertions/put.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/validation/schemas/put.ts renamed to src/validation/schemas/create.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { z } from "zod";
22
import { mimeTypes } from "../../utils/mimetype";
33
import { nameLikeSchema } from "./common";
44

5-
export const putObjectSchema = z
5+
export const createObjectSchema = z
66
.object({
77
/** A string representing the name for the file. */
88
name: nameLikeSchema,
@@ -24,7 +24,7 @@ export const putObjectSchema = z
2424
path: ["mimeType"],
2525
});
2626

27-
export const putObjectPayloadSchema = putObjectSchema.transform(
27+
export const createObjectPayloadSchema = createObjectSchema.transform(
2828
({ file, securityHash, autoDownload, ...rest }) => ({
2929
file,
3030
params: {
@@ -35,7 +35,7 @@ export const putObjectPayloadSchema = putObjectSchema.transform(
3535
}),
3636
);
3737

38-
export const putObjectResponseSchema = z.object({
38+
export const createObjectResponseSchema = z.object({
3939
/** The id of the uploaded file. */
4040
id: z.string(),
4141
/** The name of the uploaded file. */

0 commit comments

Comments
 (0)