@gravito/nebula 3.0.0 → 3.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/dist/index.cjs CHANGED
@@ -36,18 +36,15 @@ var LocalStorageProvider = class {
36
36
  /**
37
37
  * Create a new LocalStorageProvider.
38
38
  *
39
- * @param rootDir - The root directory for storage.
40
- * @param baseUrl - The base URL for accessing stored files.
39
+ * @param rootDir - The absolute path to the local storage directory.
40
+ * @param baseUrl - The public URL path for accessing stored files (e.g., '/storage').
41
41
  */
42
42
  constructor(rootDir, baseUrl = "/storage") {
43
43
  this.rootDir = rootDir;
44
44
  this.baseUrl = baseUrl;
45
45
  }
46
46
  /**
47
- * Store data in a file.
48
- *
49
- * @param key - The storage key (path).
50
- * @param data - The data to store.
47
+ * Write data to the local disk.
51
48
  */
52
49
  async put(key, data) {
53
50
  const path = this.resolveKeyPath(key);
@@ -58,10 +55,7 @@ var LocalStorageProvider = class {
58
55
  await this.runtime.writeFile(path, data);
59
56
  }
60
57
  /**
61
- * Retrieve a file.
62
- *
63
- * @param key - The storage key.
64
- * @returns A promise resolving to the file Blob or null if not found.
58
+ * Read data from the local disk.
65
59
  */
66
60
  async get(key) {
67
61
  const path = this.resolveKeyPath(key);
@@ -71,18 +65,13 @@ var LocalStorageProvider = class {
71
65
  return await this.runtime.readFileAsBlob(path);
72
66
  }
73
67
  /**
74
- * Delete a file.
75
- *
76
- * @param key - The storage key.
68
+ * Delete a file from the local disk.
77
69
  */
78
70
  async delete(key) {
79
71
  await this.runtime.deleteFile(this.resolveKeyPath(key));
80
72
  }
81
73
  /**
82
- * Get the public URL for a file.
83
- *
84
- * @param key - The storage key.
85
- * @returns The public URL string.
74
+ * Resolve the public URL for a locally stored file.
86
75
  */
87
76
  getUrl(key) {
88
77
  const safeKey = this.normalizeKey(key);
@@ -149,6 +138,7 @@ var OrbitNebula = class {
149
138
  delete: (key) => provider?.delete(key),
150
139
  getUrl: (key) => provider?.getUrl(key)
151
140
  };
141
+ core.container.instance(exposeAs, storageService);
152
142
  core.adapter.use("*", async (c, next) => {
153
143
  c.set(exposeAs, storageService);
154
144
  await next();
package/dist/index.d.cts CHANGED
@@ -1,13 +1,64 @@
1
1
  import { PlanetCore, GravitoOrbit } from '@gravito/core';
2
2
 
3
+ /**
4
+ * Interface for a file storage provider.
5
+ *
6
+ * All storage backends (Local, S3, Cloudinary, etc.) must implement this interface.
7
+ * It provides a consistent API for file operations across different runtimes
8
+ * and cloud providers.
9
+ *
10
+ * @public
11
+ * @since 3.0.0
12
+ */
3
13
  interface StorageProvider {
14
+ /**
15
+ * Save data to the storage backend.
16
+ *
17
+ * @param key - Unique identifier or path for the file (e.g., 'avatars/user1.jpg').
18
+ * @param data - The file content as a Blob, Buffer, or string.
19
+ * @returns A promise that resolves when the file is successfully saved.
20
+ */
4
21
  put(key: string, data: Blob | Buffer | string): Promise<void>;
22
+ /**
23
+ * Retrieve a file from the storage backend.
24
+ *
25
+ * @param key - The file identifier or path.
26
+ * @returns The file as a Blob, or null if the file does not exist.
27
+ */
5
28
  get(key: string): Promise<Blob | null>;
29
+ /**
30
+ * Remove a file from the storage backend.
31
+ *
32
+ * @param key - The file identifier or path to delete.
33
+ * @returns A promise that resolves when the file is deleted.
34
+ */
6
35
  delete(key: string): Promise<void>;
36
+ /**
37
+ * Get a publicly accessible URL for the given file key.
38
+ *
39
+ * Note: This may return a relative URL for local storage or a full URL
40
+ * for cloud storage providers.
41
+ *
42
+ * @param key - The file identifier or path.
43
+ * @returns The public URL string.
44
+ */
7
45
  getUrl(key: string): string;
8
46
  }
9
47
  /**
10
- * Local storage provider implementation.
48
+ * Local file system storage provider.
49
+ *
50
+ * Stores files on the local disk using the configured root directory and
51
+ * resolves URLs using a provided base URL prefix.
52
+ *
53
+ * @example
54
+ * ```typescript
55
+ * const local = new LocalStorageProvider('./storage', '/files');
56
+ * await local.put('hello.txt', 'Hello World');
57
+ * console.log(local.getUrl('hello.txt')); // "/files/hello.txt"
58
+ * ```
59
+ *
60
+ * @public
61
+ * @since 3.0.0
11
62
  */
12
63
  declare class LocalStorageProvider implements StorageProvider {
13
64
  private rootDir;
@@ -16,54 +67,77 @@ declare class LocalStorageProvider implements StorageProvider {
16
67
  /**
17
68
  * Create a new LocalStorageProvider.
18
69
  *
19
- * @param rootDir - The root directory for storage.
20
- * @param baseUrl - The base URL for accessing stored files.
70
+ * @param rootDir - The absolute path to the local storage directory.
71
+ * @param baseUrl - The public URL path for accessing stored files (e.g., '/storage').
21
72
  */
22
73
  constructor(rootDir: string, baseUrl?: string);
23
74
  /**
24
- * Store data in a file.
25
- *
26
- * @param key - The storage key (path).
27
- * @param data - The data to store.
75
+ * Write data to the local disk.
28
76
  */
29
77
  put(key: string, data: Blob | Buffer | string): Promise<void>;
30
78
  /**
31
- * Retrieve a file.
32
- *
33
- * @param key - The storage key.
34
- * @returns A promise resolving to the file Blob or null if not found.
79
+ * Read data from the local disk.
35
80
  */
36
81
  get(key: string): Promise<Blob | null>;
37
82
  /**
38
- * Delete a file.
39
- *
40
- * @param key - The storage key.
83
+ * Delete a file from the local disk.
41
84
  */
42
85
  delete(key: string): Promise<void>;
43
86
  /**
44
- * Get the public URL for a file.
45
- *
46
- * @param key - The storage key.
47
- * @returns The public URL string.
87
+ * Resolve the public URL for a locally stored file.
48
88
  */
49
89
  getUrl(key: string): string;
50
90
  private normalizeKey;
51
91
  private resolveKeyPath;
52
92
  }
93
+ /**
94
+ * Configuration options for the Nebula Storage Orbit.
95
+ *
96
+ * @public
97
+ * @since 3.0.0
98
+ */
53
99
  interface OrbitNebulaOptions {
100
+ /**
101
+ * Custom storage provider instance (e.g., S3Provider).
102
+ * If not provided, a LocalStorageProvider will be created if `local` options are set.
103
+ */
54
104
  provider?: StorageProvider;
105
+ /**
106
+ * The key used to expose the storage service in the request context.
107
+ * @default 'storage'
108
+ */
55
109
  exposeAs?: string;
110
+ /** Configuration for the default LocalStorageProvider. */
56
111
  local?: {
112
+ /** Absolute or relative path to the root directory on disk. */
57
113
  root: string;
114
+ /** Base URL prefix for serving files (e.g., '/public/storage'). @default '/storage' */
58
115
  baseUrl?: string;
59
116
  };
60
117
  }
61
118
  /** @deprecated Use OrbitNebulaOptions instead */
62
119
  type OrbitStorageOptions = OrbitNebulaOptions;
63
120
  /**
64
- * OrbitNebula - Storage Orbit
121
+ * OrbitNebula provides a unified file storage abstraction for Gravito.
65
122
  *
66
- * Provides file storage functionality for Gravito applications.
123
+ * It supports multiple backends (local, S3, etc.) and provides a consistent API
124
+ * for file operations. It also integrates with Gravito's hook system for
125
+ * filtering uploads (`storage:upload`) and reacting to events (`storage:uploaded`).
126
+ *
127
+ * @example
128
+ * ```typescript
129
+ * const nebula = new OrbitNebula({
130
+ * local: { root: './storage', baseUrl: '/public' }
131
+ * });
132
+ * core.addOrbit(nebula);
133
+ *
134
+ * // Usage in controller
135
+ * const storage = c.get('storage');
136
+ * await storage.put('example.txt', 'Content');
137
+ * ```
138
+ *
139
+ * @public
140
+ * @since 3.0.0
67
141
  */
68
142
  declare class OrbitNebula implements GravitoOrbit {
69
143
  private options?;
@@ -92,5 +166,11 @@ declare function orbitStorage(core: PlanetCore, options: OrbitNebulaOptions): {
92
166
  };
93
167
  /** @deprecated Use OrbitNebula instead */
94
168
  declare const OrbitStorage: typeof OrbitNebula;
169
+ declare module '@gravito/core' {
170
+ interface GravitoVariables {
171
+ /** File storage service */
172
+ storage: StorageProvider;
173
+ }
174
+ }
95
175
 
96
176
  export { LocalStorageProvider, OrbitNebula, type OrbitNebulaOptions, OrbitStorage, type OrbitStorageOptions, type StorageProvider, orbitStorage as default };
package/dist/index.d.ts CHANGED
@@ -1,13 +1,64 @@
1
1
  import { PlanetCore, GravitoOrbit } from '@gravito/core';
2
2
 
3
+ /**
4
+ * Interface for a file storage provider.
5
+ *
6
+ * All storage backends (Local, S3, Cloudinary, etc.) must implement this interface.
7
+ * It provides a consistent API for file operations across different runtimes
8
+ * and cloud providers.
9
+ *
10
+ * @public
11
+ * @since 3.0.0
12
+ */
3
13
  interface StorageProvider {
14
+ /**
15
+ * Save data to the storage backend.
16
+ *
17
+ * @param key - Unique identifier or path for the file (e.g., 'avatars/user1.jpg').
18
+ * @param data - The file content as a Blob, Buffer, or string.
19
+ * @returns A promise that resolves when the file is successfully saved.
20
+ */
4
21
  put(key: string, data: Blob | Buffer | string): Promise<void>;
22
+ /**
23
+ * Retrieve a file from the storage backend.
24
+ *
25
+ * @param key - The file identifier or path.
26
+ * @returns The file as a Blob, or null if the file does not exist.
27
+ */
5
28
  get(key: string): Promise<Blob | null>;
29
+ /**
30
+ * Remove a file from the storage backend.
31
+ *
32
+ * @param key - The file identifier or path to delete.
33
+ * @returns A promise that resolves when the file is deleted.
34
+ */
6
35
  delete(key: string): Promise<void>;
36
+ /**
37
+ * Get a publicly accessible URL for the given file key.
38
+ *
39
+ * Note: This may return a relative URL for local storage or a full URL
40
+ * for cloud storage providers.
41
+ *
42
+ * @param key - The file identifier or path.
43
+ * @returns The public URL string.
44
+ */
7
45
  getUrl(key: string): string;
8
46
  }
9
47
  /**
10
- * Local storage provider implementation.
48
+ * Local file system storage provider.
49
+ *
50
+ * Stores files on the local disk using the configured root directory and
51
+ * resolves URLs using a provided base URL prefix.
52
+ *
53
+ * @example
54
+ * ```typescript
55
+ * const local = new LocalStorageProvider('./storage', '/files');
56
+ * await local.put('hello.txt', 'Hello World');
57
+ * console.log(local.getUrl('hello.txt')); // "/files/hello.txt"
58
+ * ```
59
+ *
60
+ * @public
61
+ * @since 3.0.0
11
62
  */
12
63
  declare class LocalStorageProvider implements StorageProvider {
13
64
  private rootDir;
@@ -16,54 +67,77 @@ declare class LocalStorageProvider implements StorageProvider {
16
67
  /**
17
68
  * Create a new LocalStorageProvider.
18
69
  *
19
- * @param rootDir - The root directory for storage.
20
- * @param baseUrl - The base URL for accessing stored files.
70
+ * @param rootDir - The absolute path to the local storage directory.
71
+ * @param baseUrl - The public URL path for accessing stored files (e.g., '/storage').
21
72
  */
22
73
  constructor(rootDir: string, baseUrl?: string);
23
74
  /**
24
- * Store data in a file.
25
- *
26
- * @param key - The storage key (path).
27
- * @param data - The data to store.
75
+ * Write data to the local disk.
28
76
  */
29
77
  put(key: string, data: Blob | Buffer | string): Promise<void>;
30
78
  /**
31
- * Retrieve a file.
32
- *
33
- * @param key - The storage key.
34
- * @returns A promise resolving to the file Blob or null if not found.
79
+ * Read data from the local disk.
35
80
  */
36
81
  get(key: string): Promise<Blob | null>;
37
82
  /**
38
- * Delete a file.
39
- *
40
- * @param key - The storage key.
83
+ * Delete a file from the local disk.
41
84
  */
42
85
  delete(key: string): Promise<void>;
43
86
  /**
44
- * Get the public URL for a file.
45
- *
46
- * @param key - The storage key.
47
- * @returns The public URL string.
87
+ * Resolve the public URL for a locally stored file.
48
88
  */
49
89
  getUrl(key: string): string;
50
90
  private normalizeKey;
51
91
  private resolveKeyPath;
52
92
  }
93
+ /**
94
+ * Configuration options for the Nebula Storage Orbit.
95
+ *
96
+ * @public
97
+ * @since 3.0.0
98
+ */
53
99
  interface OrbitNebulaOptions {
100
+ /**
101
+ * Custom storage provider instance (e.g., S3Provider).
102
+ * If not provided, a LocalStorageProvider will be created if `local` options are set.
103
+ */
54
104
  provider?: StorageProvider;
105
+ /**
106
+ * The key used to expose the storage service in the request context.
107
+ * @default 'storage'
108
+ */
55
109
  exposeAs?: string;
110
+ /** Configuration for the default LocalStorageProvider. */
56
111
  local?: {
112
+ /** Absolute or relative path to the root directory on disk. */
57
113
  root: string;
114
+ /** Base URL prefix for serving files (e.g., '/public/storage'). @default '/storage' */
58
115
  baseUrl?: string;
59
116
  };
60
117
  }
61
118
  /** @deprecated Use OrbitNebulaOptions instead */
62
119
  type OrbitStorageOptions = OrbitNebulaOptions;
63
120
  /**
64
- * OrbitNebula - Storage Orbit
121
+ * OrbitNebula provides a unified file storage abstraction for Gravito.
65
122
  *
66
- * Provides file storage functionality for Gravito applications.
123
+ * It supports multiple backends (local, S3, etc.) and provides a consistent API
124
+ * for file operations. It also integrates with Gravito's hook system for
125
+ * filtering uploads (`storage:upload`) and reacting to events (`storage:uploaded`).
126
+ *
127
+ * @example
128
+ * ```typescript
129
+ * const nebula = new OrbitNebula({
130
+ * local: { root: './storage', baseUrl: '/public' }
131
+ * });
132
+ * core.addOrbit(nebula);
133
+ *
134
+ * // Usage in controller
135
+ * const storage = c.get('storage');
136
+ * await storage.put('example.txt', 'Content');
137
+ * ```
138
+ *
139
+ * @public
140
+ * @since 3.0.0
67
141
  */
68
142
  declare class OrbitNebula implements GravitoOrbit {
69
143
  private options?;
@@ -92,5 +166,11 @@ declare function orbitStorage(core: PlanetCore, options: OrbitNebulaOptions): {
92
166
  };
93
167
  /** @deprecated Use OrbitNebula instead */
94
168
  declare const OrbitStorage: typeof OrbitNebula;
169
+ declare module '@gravito/core' {
170
+ interface GravitoVariables {
171
+ /** File storage service */
172
+ storage: StorageProvider;
173
+ }
174
+ }
95
175
 
96
176
  export { LocalStorageProvider, OrbitNebula, type OrbitNebulaOptions, OrbitStorage, type OrbitStorageOptions, type StorageProvider, orbitStorage as default };
package/dist/index.js CHANGED
@@ -11,18 +11,15 @@ var LocalStorageProvider = class {
11
11
  /**
12
12
  * Create a new LocalStorageProvider.
13
13
  *
14
- * @param rootDir - The root directory for storage.
15
- * @param baseUrl - The base URL for accessing stored files.
14
+ * @param rootDir - The absolute path to the local storage directory.
15
+ * @param baseUrl - The public URL path for accessing stored files (e.g., '/storage').
16
16
  */
17
17
  constructor(rootDir, baseUrl = "/storage") {
18
18
  this.rootDir = rootDir;
19
19
  this.baseUrl = baseUrl;
20
20
  }
21
21
  /**
22
- * Store data in a file.
23
- *
24
- * @param key - The storage key (path).
25
- * @param data - The data to store.
22
+ * Write data to the local disk.
26
23
  */
27
24
  async put(key, data) {
28
25
  const path = this.resolveKeyPath(key);
@@ -33,10 +30,7 @@ var LocalStorageProvider = class {
33
30
  await this.runtime.writeFile(path, data);
34
31
  }
35
32
  /**
36
- * Retrieve a file.
37
- *
38
- * @param key - The storage key.
39
- * @returns A promise resolving to the file Blob or null if not found.
33
+ * Read data from the local disk.
40
34
  */
41
35
  async get(key) {
42
36
  const path = this.resolveKeyPath(key);
@@ -46,18 +40,13 @@ var LocalStorageProvider = class {
46
40
  return await this.runtime.readFileAsBlob(path);
47
41
  }
48
42
  /**
49
- * Delete a file.
50
- *
51
- * @param key - The storage key.
43
+ * Delete a file from the local disk.
52
44
  */
53
45
  async delete(key) {
54
46
  await this.runtime.deleteFile(this.resolveKeyPath(key));
55
47
  }
56
48
  /**
57
- * Get the public URL for a file.
58
- *
59
- * @param key - The storage key.
60
- * @returns The public URL string.
49
+ * Resolve the public URL for a locally stored file.
61
50
  */
62
51
  getUrl(key) {
63
52
  const safeKey = this.normalizeKey(key);
@@ -124,6 +113,7 @@ var OrbitNebula = class {
124
113
  delete: (key) => provider?.delete(key),
125
114
  getUrl: (key) => provider?.getUrl(key)
126
115
  };
116
+ core.container.instance(exposeAs, storageService);
127
117
  core.adapter.use("*", async (c, next) => {
128
118
  c.set(exposeAs, storageService);
129
119
  await next();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravito/nebula",
3
- "version": "3.0.0",
3
+ "version": "3.0.1",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },