@rr-vault/sdk 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rr-vault/sdk",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "\"RR-Vault SDK is a developer-first cloud storage client for securely uploading, managing, and delivering files using API keys, app-based access, and scalable infrastructure.\"",
5
5
  "keywords": [],
6
6
  "homepage": "https://github.com/mdrezuanislamridoy/rr-vault-sdk#readme",
@@ -14,8 +14,17 @@
14
14
  "license": "ISC",
15
15
  "author": "Ridoy781 <ridoy.babu.781@gmail.com>",
16
16
  "type": "module",
17
- "main": "index.js",
17
+ "main": "src/index.js",
18
18
  "scripts": {
19
19
  "test": "echo \"Error: no test specified\" && exit 1"
20
+ },
21
+ "devDependencies": {
22
+ "@types/node": "^25.5.0"
23
+ },
24
+ "dependencies": {
25
+ "@aws-sdk/client-s3": "^3.1019.0",
26
+ "@aws-sdk/lib-storage": "^3.1019.0",
27
+ "@types/axios": "^0.9.36",
28
+ "axios": "^1.14.0"
20
29
  }
21
30
  }
package/src/config.ts ADDED
@@ -0,0 +1,33 @@
1
+ import { RRVaultConfig, R2Config } from "./types";
2
+
3
+ /**
4
+ * Internal R2 settings.
5
+ * Ridoy: Replace these placeholders with your actual Cloudflare R2 values.
6
+ */
7
+ const R2_INTERNAL: Omit<R2Config, "accessKeyId" | "secretAccessKey"> = {
8
+ accountId: "YOUR_ACCOUNT_ID",
9
+ bucketName: "YOUR_BUCKET_NAME",
10
+ publicUrl: "https://pub-your-id.r2.dev",
11
+ };
12
+
13
+ let userConfig: RRVaultConfig | null = null;
14
+
15
+ export function setConfig(newConfig: RRVaultConfig) {
16
+ userConfig = { ...newConfig };
17
+ }
18
+
19
+ export function getConfig(): R2Config {
20
+ if (!userConfig) {
21
+ throw new Error(
22
+ "RRVault: SDK not configured. Please call RRVault.config() first."
23
+ );
24
+ }
25
+
26
+ // Map user-facing appId/secretKey to internal R2 credentials
27
+ return {
28
+ ...R2_INTERNAL,
29
+ accessKeyId: userConfig.appId,
30
+ secretAccessKey: userConfig.secretKey,
31
+ };
32
+ }
33
+
package/src/index.ts ADDED
@@ -0,0 +1,39 @@
1
+ import { setConfig } from "./config";
2
+ import { upload as s3Upload, deleteFile as s3Delete } from "./s3";
3
+ import { RRVaultConfig, R2Config, UploadOptions, UploadResult, DeleteResult } from "./types";
4
+
5
+ export class RRVault {
6
+
7
+ static config(config: RRVaultConfig) {
8
+ setConfig(config);
9
+ }
10
+
11
+
12
+ static async upload(
13
+ file: any,
14
+ fileName: string,
15
+ options: UploadOptions = {}
16
+ ): Promise<UploadResult> {
17
+ return await s3Upload(file, fileName, options);
18
+ }
19
+
20
+ static async delete(key: string): Promise<DeleteResult> {
21
+ return await s3Delete(key);
22
+ }
23
+ }
24
+
25
+ // Functional approach exports
26
+ export {
27
+ setConfig as config,
28
+ s3Upload as upload,
29
+ s3Delete as deleteFile
30
+ };
31
+
32
+ // Type exports
33
+ export type {
34
+ RRVaultConfig,
35
+ R2Config,
36
+ UploadOptions,
37
+ UploadResult,
38
+ DeleteResult
39
+ };
package/src/s3.ts ADDED
@@ -0,0 +1,88 @@
1
+ import { S3Client, PutObjectCommand, DeleteObjectCommand } from "@aws-sdk/client-s3";
2
+ import { getConfig } from "./config";
3
+ import { UploadOptions, UploadResult, DeleteResult } from "./types";
4
+
5
+ let s3Client: S3Client | null = null;
6
+
7
+ function getS3Client() {
8
+ if (s3Client) return s3Client;
9
+ const config = getConfig();
10
+
11
+ s3Client = new S3Client({
12
+ region: "auto",
13
+ endpoint: `https://${config.accountId}.r2.cloudflarestorage.com`,
14
+ credentials: {
15
+ accessKeyId: config.accessKeyId,
16
+ secretAccessKey: config.secretAccessKey,
17
+ },
18
+ });
19
+
20
+ return s3Client;
21
+ }
22
+
23
+ /**
24
+ * Upload a file to Cloudflare R2
25
+ * @param file - File content (Buffer, Blob, Uint8Array)
26
+ * @param fileName - Original name of the file
27
+ * @param options - Additional upload options
28
+ */
29
+ export async function upload(
30
+ file: any,
31
+ fileName: string,
32
+ options: UploadOptions = {}
33
+ ): Promise<UploadResult> {
34
+ const config = getConfig();
35
+ const client = getS3Client();
36
+
37
+ // Clean folder path and construct key
38
+ const folder = options.folder ? `${options.folder.replace(/\/$/, "")}/` : "";
39
+ const key = `${folder}${Date.now()}-${fileName.replace(/\s+/g, "-")}`;
40
+
41
+ const command = new PutObjectCommand({
42
+ Bucket: config.bucketName,
43
+ Key: key,
44
+ Body: file,
45
+ ContentType: options.contentType || "application/octet-stream",
46
+ Metadata: options.metadata,
47
+ CacheControl: options.cacheControl,
48
+ });
49
+
50
+ const response = await client.send(command);
51
+
52
+ // Construct public URL
53
+ const baseUrl = config.publicUrl
54
+ ? config.publicUrl.replace(/\/$/, "")
55
+ : `https://${config.bucketName}.${config.accountId}.r2.dev`;
56
+
57
+ const url = `${baseUrl}/${key}`;
58
+
59
+ return {
60
+ key,
61
+ url,
62
+ etag: response.ETag,
63
+ };
64
+ }
65
+
66
+ /**
67
+ * Delete a file from Cloudflare R2
68
+ * @param key - The storage key (path) of the file
69
+ */
70
+ export async function deleteFile(key: string): Promise<DeleteResult> {
71
+ const config = getConfig();
72
+ const client = getS3Client();
73
+
74
+ const command = new DeleteObjectCommand({
75
+ Bucket: config.bucketName,
76
+ Key: key,
77
+ });
78
+
79
+ try {
80
+ await client.send(command);
81
+ return { success: true };
82
+ } catch (error: any) {
83
+ return {
84
+ success: false,
85
+ message: error.message || "Failed to delete file from R2"
86
+ };
87
+ }
88
+ }
package/src/types.ts ADDED
@@ -0,0 +1,30 @@
1
+ export interface RRVaultConfig {
2
+ appId: string;
3
+ secretKey: string;
4
+ }
5
+
6
+ export interface R2Config {
7
+ accountId: string;
8
+ accessKeyId: string;
9
+ secretAccessKey: string;
10
+ bucketName: string;
11
+ publicUrl?: string;
12
+ }
13
+
14
+ export interface UploadOptions {
15
+ folder?: string;
16
+ contentType?: string;
17
+ metadata?: Record<string, string>;
18
+ cacheControl?: string;
19
+ }
20
+
21
+ export interface UploadResult {
22
+ key: string;
23
+ url: string;
24
+ etag?: string;
25
+ }
26
+
27
+ export interface DeleteResult {
28
+ success: boolean;
29
+ message?: string;
30
+ }
package/verify_r2.ts ADDED
@@ -0,0 +1,39 @@
1
+ import { RRVault } from "./src/index";
2
+
3
+
4
+ const testConfig = {
5
+ appId: "YOUR_APP_ID",
6
+ secretKey: "YOUR_SECRET_KEY",
7
+ };
8
+
9
+
10
+ async function runTest() {
11
+ console.log("🚀 Starting RRVault R2 Test...");
12
+
13
+ try {
14
+ // 1. Configure
15
+ RRVault.config(testConfig);
16
+ console.log("✅ Configured successfully.");
17
+
18
+ // 2. Upload
19
+ const fileContent = Buffer.from("Hello RRVault R2!");
20
+ const uploadResult = await RRVault.upload(fileContent, "test.txt", {
21
+ folder: "tests",
22
+ contentType: "text/plain",
23
+ });
24
+ console.log("✅ Upload successful:", uploadResult);
25
+
26
+ // 3. Delete
27
+ console.log("🕒 Waiting 2 seconds before delete...");
28
+ await new Promise((resolve) => setTimeout(resolve, 2000));
29
+
30
+ const deleteResult = await RRVault.delete(uploadResult.key);
31
+ console.log("✅ Delete result:", deleteResult);
32
+
33
+ } catch (error) {
34
+ console.error("❌ Test failed:", error);
35
+ }
36
+ }
37
+
38
+ // runTest();
39
+ console.log("Verification script ready. Update credentials and uncomment runTest() to execute.");
package/index.js DELETED
@@ -1,3 +0,0 @@
1
- export default function RRVault() {
2
- console.log("My First NPM Package");
3
- }