Skip to content

Commit 93ebe06

Browse files
committed
new BlobObject structure
1 parent a86d7dc commit 93ebe06

File tree

7 files changed

+103
-27
lines changed

7 files changed

+103
-27
lines changed

.changeset/little-ads-fix.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@squarecloud/blob": minor
3+
---
4+
5+
New `BlobObject` structure

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export class SquareCloudBlob {
1515
}
1616
}
1717

18+
export * from "./structures/object";
1819
export * from "./types/create";
1920
export * from "./types/list";
2021
export * from "./utils/mimetype";

src/managers/objects.ts

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { SquareCloudBlob } from "..";
2-
import { SquareCloudBlobError } from "../structures/error";
2+
import { BlobObject } from "../structures/object";
33
import type { CreateObjectResponse, CreateObjectType } from "../types/create";
44
import type { ListObjectsResponse } from "../types/list";
55
import { MimeTypeUtil } from "../utils/mimetype";
6-
import { parsePathLike } from "../utils/pathlike";
6+
import { parsePathLike } from "../utils/path-like";
77
import { assertCreateObjectResponse } from "../validation/assertions/create";
88
import { assertListObjectsResponse } from "../validation/assertions/list";
99
import { createObjectPayloadSchema } from "../validation/schemas/create";
@@ -22,8 +22,23 @@ export class ObjectsManager {
2222
async list() {
2323
const { response } =
2424
await this.client.api.request<ListObjectsResponse>("objects");
25+
const list = assertListObjectsResponse(response);
2526

26-
return assertListObjectsResponse(response)?.objects;
27+
if (!list.objects) {
28+
return [];
29+
}
30+
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+
);
2742
}
2843

2944
/**
@@ -56,7 +71,12 @@ export class ObjectsManager {
5671
},
5772
);
5873

59-
return assertCreateObjectResponse(response);
74+
const objectData = assertCreateObjectResponse(response);
75+
76+
return new BlobObject({
77+
idOrUrl: objectData.id,
78+
size: objectData.size,
79+
});
6080
}
6181

6282
/**
@@ -79,27 +99,4 @@ export class ObjectsManager {
7999

80100
return status === "success";
81101
}
82-
83-
/**
84-
* Parses the object URL to extract id, prefix, and name.
85-
*
86-
* @param url - The object URL to parse.
87-
*/
88-
parseObjectUrl(url: string) {
89-
const pattern =
90-
/^https:\/\/public-blob\.squarecloud\.dev\/([^\/]+)\/([^\/]+\/)?([^_]+)_[\w-]+\.\w+$/;
91-
const match = pattern.exec(url);
92-
93-
if (!match) {
94-
throw new SquareCloudBlobError(
95-
"INVALID_BLOB_URL",
96-
"Invalid blob object URL",
97-
);
98-
}
99-
100-
let [, id, prefix, name] = match;
101-
prefix = prefix ? prefix.slice(0, -1) : "";
102-
103-
return { id, prefix, name };
104-
}
105102
}

src/structures/object.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import type { BlobObjectData } from "../types/object";
2+
import { type MimeType, MimeTypeUtil } from "../utils/mimetype";
3+
import { parseObjectUrl } from "../utils/object-url";
4+
5+
export class BlobObject {
6+
id: string;
7+
url: string;
8+
name: string;
9+
prefix?: string;
10+
hash: string;
11+
userId: string;
12+
extension: string;
13+
mimeType: MimeType;
14+
size: number;
15+
expiresAt?: Date;
16+
createdAt?: Date;
17+
18+
constructor(data: BlobObjectData) {
19+
const { id, name, prefix, hash, userId, extension } = parseObjectUrl(
20+
data.idOrUrl,
21+
);
22+
23+
this.id = id;
24+
this.url = `https://public-blob.squarecloud.dev/${id}`;
25+
this.name = name;
26+
this.prefix = prefix;
27+
this.hash = hash;
28+
this.userId = userId;
29+
this.extension = extension;
30+
this.mimeType = MimeTypeUtil.fromExtension(extension);
31+
this.size = data.size;
32+
this.expiresAt = data.expiresAt;
33+
this.createdAt = data.createdAt;
34+
}
35+
}

src/types/object.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export type BlobObjectData = {
2+
idOrUrl: string;
3+
size: number;
4+
expiresAt?: Date;
5+
createdAt?: Date;
6+
};

src/utils/object-url.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { SquareCloudBlobError } from "../structures/error";
2+
3+
const objectUrlRegex =
4+
/^(?<url>https:\/\/public-blob\.squarecloud\.dev)?\/?(?<userId>\d+\/)(?<prefix>[\w\d-_]+\/)?(?<name>[\w\d_]+)-(?<hash>[\w\d]+)\.(?<extension>\w+)$/;
5+
6+
/**
7+
* Parses the object URL to extract id, userId, prefix, name, hash and extension.
8+
*
9+
* @param url - The object URL to parse.
10+
*/
11+
export function parseObjectUrl(url: string) {
12+
const match = url.match(objectUrlRegex);
13+
14+
if (!match?.groups) {
15+
throw new SquareCloudBlobError("Invalid object URL");
16+
}
17+
18+
const payload = {
19+
userId: match.groups.userId.replace("/", ""),
20+
prefix: match.groups.prefix?.replace("/", ""),
21+
name: match.groups.name,
22+
hash: match.groups.hash,
23+
extension: match.groups.extension,
24+
};
25+
26+
return {
27+
id: `${payload.userId}/${payload.prefix ? `${payload.prefix}/` : ""}${
28+
payload.name
29+
}-${payload.hash}.${payload.extension}`,
30+
...payload,
31+
};
32+
}
File renamed without changes.

0 commit comments

Comments
 (0)