Skip to content

Commit cc4bf7a

Browse files
committed
refactor(validation): replace zod schemas with type interfaces and utility functions
1 parent 16289a1 commit cc4bf7a

File tree

6 files changed

+83
-89
lines changed

6 files changed

+83
-89
lines changed

src/managers/objects.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import type { SquareCloudBlob } from "..";
2+
import { SquareCloudBlobError } from "../structures/error";
23
import { BlobObject } from "../structures/object";
3-
import type { CreateObjectResponse, CreateObjectType } from "../types/create";
4+
import type {
5+
CreateObjectOptions,
6+
CreateObjectResponse,
7+
} from "../types/create";
48
import type { ListObjectsResponse, ListObjectsType } from "../types/list";
59
import { parsePathLike } from "../utils/path-like";
6-
import { assertCreateObjectResponse } from "../validation/assertions/create";
10+
import { makeCreateObjectPayload } from "../utils/payloads";
711
import { assertListObjectsResponse } from "../validation/assertions/list";
8-
import { createObjectPayloadSchema } from "../validation/schemas/create";
912
import { listObjectsPayloadSchema } from "../validation/schemas/list";
1013

1114
export class ObjectsManager {
@@ -46,7 +49,7 @@ export class ObjectsManager {
4649
/**
4750
* Uploads an object to the storage.
4851
*
49-
* @param object - An object to upload
52+
* @param data - An object to upload
5053
*
5154
* @example
5255
* ```js
@@ -64,24 +67,29 @@ export class ObjectsManager {
6467
* })
6568
* ```
6669
*/
67-
async create(object: CreateObjectType) {
68-
const payload = createObjectPayloadSchema.parse(object);
70+
async create(data: CreateObjectOptions) {
71+
if (data.file instanceof Buffer && !data.mimeType) {
72+
throw new SquareCloudBlobError(
73+
"MIME_TYPE_REQUIRED",
74+
"Mime type is required when using a Buffer",
75+
);
76+
}
77+
78+
const payload = makeCreateObjectPayload(data);
6979
const file = await parsePathLike(payload.file);
70-
const type = payload.mimeType || object.mimeType;
80+
const type = payload.mimeType || data.mimeType;
7181

7282
const formData = new FormData();
73-
formData.append("file", new Blob([file], { type }));
83+
formData.append("file", new Blob([new Uint8Array(file)], { type }));
7484

7585
const { response } = await this.client.api.request<CreateObjectResponse>(
7686
"objects",
7787
{ method: "POST", body: formData, params: payload.params },
7888
);
7989

80-
const objectData = assertCreateObjectResponse(response);
81-
8290
return new BlobObject({
83-
idOrUrl: objectData.id,
84-
size: objectData.size,
91+
idOrUrl: response.id,
92+
size: response.size,
8593
});
8694
}
8795

src/types/create.ts

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,37 @@
1-
import type { z } from "zod";
2-
import type {
3-
createObjectResponseSchema,
4-
createObjectSchema,
5-
} from "../validation/schemas/create";
1+
import type { MimeTypes } from "../utils/mimetype";
62

7-
export type CreateObjectType = z.infer<typeof createObjectSchema>;
8-
export type CreateObjectResponse = z.infer<typeof createObjectResponseSchema>;
3+
export interface CreateObjectOptions {
4+
/**
5+
* A string representing the name of the file.
6+
* Must adhere to the a to z, A to Z, 0 to 9, and _ pattern. (3 to 32 characters)
7+
*/
8+
name: string;
9+
/** Use absolute path, Buffer or Blob */
10+
file: string | Buffer;
11+
/**
12+
* A string representing the prefix for the file.
13+
* Must adhere to the a to z, A to Z, 0 to 9, and _ pattern. (3 to 32 characters)
14+
*/
15+
prefix?: string;
16+
/** A string representing the MIME type of the file. */
17+
mimeType?: MimeTypes;
18+
/** A number indicating the expiration period of the file, ranging from 1 to 365 days. */
19+
expiresIn?: number;
20+
/** Set to true if a security hash is required. */
21+
securityHash?: boolean;
22+
/** Set to true if the file should be set for automatic download. */
23+
autoDownload?: boolean;
24+
}
25+
26+
export interface CreateObjectResponse {
27+
/** The id of the uploaded file. */
28+
id: string;
29+
/** The name of the uploaded file. */
30+
name: string;
31+
/** The size of the uploaded file. */
32+
size: number;
33+
/** The URL of the uploaded file. (File distributed in Square Cloud CDN) */
34+
url: string;
35+
/** The prefix of the uploaded file. */
36+
prefix?: string;
37+
}

src/utils/payloads.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { CreateObjectOptions } from "../types/create";
2+
import { MimeTypeUtil } from "./mimetype";
3+
4+
export function makeCreateObjectPayload({
5+
file,
6+
securityHash,
7+
autoDownload,
8+
expiresIn,
9+
...rest
10+
}: CreateObjectOptions) {
11+
return {
12+
file,
13+
mimeType:
14+
typeof file === "string"
15+
? MimeTypeUtil.fromExtension(file.split(".")[1])
16+
: undefined,
17+
params: {
18+
...rest,
19+
expire: expiresIn,
20+
security_hash: securityHash,
21+
auto_download: autoDownload,
22+
},
23+
};
24+
}

src/validation/assertions/create.ts

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

src/validation/schemas/create.ts

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

test/create.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { type CreateObjectType, MimeTypes } from "../src";
1+
import { type CreateObjectOptions, MimeTypes } from "../src";
22
import { blob } from "./index.test";
33

44
// Create object using absolute path
55

6-
const createWithPathPayload: CreateObjectType = {
6+
const createWithPathPayload: CreateObjectOptions = {
77
file: "package.json",
88
name: "testing",
99
};
@@ -12,7 +12,7 @@ blob.objects.create(createWithPathPayload).then(console.log);
1212

1313
// Create object using buffer
1414

15-
const createWithBufferPayload: CreateObjectType = {
15+
const createWithBufferPayload: CreateObjectOptions = {
1616
file: Buffer.from("content".repeat(100)),
1717
name: "testing",
1818
mimeType: MimeTypes.TEXT_PLAIN,

0 commit comments

Comments
 (0)