@dbrij/storage 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +21 -2
- package/dist/index.d.ts +122 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +164 -0
- package/dist/index.js.map +1 -0
- package/package.json +44 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Dbrij
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @dbrij/storage
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The Dbrij Storage SDK. Zero dependencies. Node 18+ and browsers.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { DbrijStorage, transform } from '@dbrij/storage';
|
|
7
|
+
|
|
8
|
+
const storage = new DbrijStorage({ keyId: process.env.DBRIJ_KEY_ID!, secret: process.env.DBRIJ_KEY_SECRET! });
|
|
9
|
+
|
|
10
|
+
const bucket = await storage.buckets.create('my-assets');
|
|
11
|
+
await storage.objects.upload(bucket.id, 'images/logo.png', bytes, { contentType: 'image/png' });
|
|
12
|
+
const link = await storage.delivery.url(bucket.id, 'images/logo.png', { transform: transform({ width: 800, format: 'auto' }) });
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
In a browser, upload with a preset and no key:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
const file = await DbrijStorage.presetUpload('dbup_…', input.files[0], { onProgress: (f) => console.log(f) });
|
|
19
|
+
console.log(file.deliveryUrl);
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Every AWS SDK also works against `https://s3.dbrij.com` with the same key id and secret. Docs: https://dbrij.com/products/storage/docs
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dbrij Storage SDK. Zero dependencies; runs in Node 18+ and in browsers.
|
|
3
|
+
*
|
|
4
|
+
* import { DbrijStorage } from '@dbrij/storage';
|
|
5
|
+
* const storage = new DbrijStorage({ keyId: 'dbsk_…', secret: 'dbss_…' });
|
|
6
|
+
* const bucket = await storage.buckets.create('my-assets');
|
|
7
|
+
* const file = await storage.objects.upload(bucket.id, 'images/logo.png', bytes, { contentType: 'image/png' });
|
|
8
|
+
* const link = await storage.delivery.url(bucket.id, 'images/logo.png', { transform: 'w_800,f_auto' });
|
|
9
|
+
*
|
|
10
|
+
* In a browser, use an upload preset instead of a key:
|
|
11
|
+
* await DbrijStorage.presetUpload('dbup_…', file);
|
|
12
|
+
*
|
|
13
|
+
* The S3 protocol is served too (s3.dbrij.com): any AWS SDK works with the same key
|
|
14
|
+
* id and secret. This client covers what S3 does not: delivery links, transformations,
|
|
15
|
+
* presets, and the confirm step REST uploads need.
|
|
16
|
+
*/
|
|
17
|
+
export interface DbrijStorageOptions {
|
|
18
|
+
/** Access key id (dbsk_…) and secret (dbss_…). Server side only. */
|
|
19
|
+
keyId?: string;
|
|
20
|
+
secret?: string;
|
|
21
|
+
/** API base. Default https://api.dbrij.com/api/v1 */
|
|
22
|
+
baseUrl?: string;
|
|
23
|
+
/** A fetch implementation, when the runtime has none on the global. */
|
|
24
|
+
fetch?: typeof fetch;
|
|
25
|
+
}
|
|
26
|
+
export interface Bucket {
|
|
27
|
+
id: string;
|
|
28
|
+
name: string;
|
|
29
|
+
visibility: 'private' | 'public';
|
|
30
|
+
objectCount: number;
|
|
31
|
+
totalBytes: number;
|
|
32
|
+
createdAt: string;
|
|
33
|
+
}
|
|
34
|
+
export interface StorageObject {
|
|
35
|
+
key: string;
|
|
36
|
+
size: number;
|
|
37
|
+
contentType: string;
|
|
38
|
+
isFolder: boolean;
|
|
39
|
+
/** Stable public link when the bucket or prefix is public, else null. */
|
|
40
|
+
url: string | null;
|
|
41
|
+
updatedAt: string;
|
|
42
|
+
}
|
|
43
|
+
export interface UploadOptions {
|
|
44
|
+
contentType?: string;
|
|
45
|
+
/** Called with 0..1 as bytes go up (browser XMLHttpRequest only). */
|
|
46
|
+
onProgress?: (fraction: number) => void;
|
|
47
|
+
}
|
|
48
|
+
export interface DeliveryOptions {
|
|
49
|
+
/** e.g. "w_800,h_600,c_fill,f_auto,q_80". Images only. */
|
|
50
|
+
transform?: string;
|
|
51
|
+
/** Seconds a signed link lives (private objects). Default 3600, max thirty days. */
|
|
52
|
+
expiresIn?: number;
|
|
53
|
+
download?: boolean;
|
|
54
|
+
}
|
|
55
|
+
export interface DeliveryLink {
|
|
56
|
+
url: string;
|
|
57
|
+
expiresAt: string | null;
|
|
58
|
+
signed: boolean;
|
|
59
|
+
}
|
|
60
|
+
export interface PresetUploadResult {
|
|
61
|
+
key: string;
|
|
62
|
+
size: number;
|
|
63
|
+
contentType: string;
|
|
64
|
+
url: string | null;
|
|
65
|
+
deliveryUrl: string;
|
|
66
|
+
}
|
|
67
|
+
export declare class DbrijStorageError extends Error {
|
|
68
|
+
readonly status: number;
|
|
69
|
+
constructor(message: string, status: number);
|
|
70
|
+
}
|
|
71
|
+
type Bytes = ArrayBuffer | Uint8Array | Blob;
|
|
72
|
+
export declare class DbrijStorage {
|
|
73
|
+
private readonly base;
|
|
74
|
+
private readonly fetchImpl;
|
|
75
|
+
private readonly auth;
|
|
76
|
+
constructor(opts?: DbrijStorageOptions);
|
|
77
|
+
private call;
|
|
78
|
+
readonly buckets: {
|
|
79
|
+
list: () => Promise<Bucket[]>;
|
|
80
|
+
create: (name: string, visibility?: "private" | "public") => Promise<Bucket>;
|
|
81
|
+
};
|
|
82
|
+
readonly objects: {
|
|
83
|
+
list: (bucketId: string, prefix?: string) => Promise<StorageObject[]>;
|
|
84
|
+
/** Presign, PUT the bytes to the store, confirm. Returns the confirmed object. */
|
|
85
|
+
upload: (bucketId: string, key: string, bytes: Bytes, opts?: UploadOptions) => Promise<StorageObject>;
|
|
86
|
+
delete: (bucketId: string, key: string) => Promise<{
|
|
87
|
+
deleted: boolean;
|
|
88
|
+
}>;
|
|
89
|
+
/** A short lived signed URL for the raw bytes (one hour). For links to hand out, prefer delivery.url. */
|
|
90
|
+
downloadUrl: (bucketId: string, key: string) => Promise<string>;
|
|
91
|
+
};
|
|
92
|
+
readonly delivery: {
|
|
93
|
+
/** A delivery link: permanent for public objects, signed and expiring for private ones, transformed on request. */
|
|
94
|
+
url: (bucketId: string, key: string, opts?: DeliveryOptions) => Promise<DeliveryLink>;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* Keyless upload through a preset (dbup_…): safe in a browser. Starts the upload,
|
|
98
|
+
* PUTs the bytes, confirms, and returns the file with a delivery link.
|
|
99
|
+
*/
|
|
100
|
+
static presetUpload(preset: string, file: Blob & {
|
|
101
|
+
name?: string;
|
|
102
|
+
}, opts?: UploadOptions & {
|
|
103
|
+
baseUrl?: string;
|
|
104
|
+
fetch?: typeof fetch;
|
|
105
|
+
filename?: string;
|
|
106
|
+
}): Promise<PresetUploadResult>;
|
|
107
|
+
}
|
|
108
|
+
/** Build a transformation segment from named options, for readable code. */
|
|
109
|
+
export declare function transform(opts: {
|
|
110
|
+
width?: number;
|
|
111
|
+
height?: number;
|
|
112
|
+
crop?: 'fill' | 'fit' | 'scale' | 'pad' | 'crop';
|
|
113
|
+
format?: 'auto' | 'jpg' | 'png' | 'webp' | 'avif' | 'gif';
|
|
114
|
+
quality?: number;
|
|
115
|
+
dpr?: number;
|
|
116
|
+
radius?: number | 'max';
|
|
117
|
+
gravity?: string;
|
|
118
|
+
grayscale?: boolean;
|
|
119
|
+
blur?: number;
|
|
120
|
+
}): string;
|
|
121
|
+
export {};
|
|
122
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,MAAM,WAAW,mBAAmB;IAClC,oEAAoE;IACpE,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,qDAAqD;IACrD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,uEAAuE;IACvE,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;CACtB;AAED,MAAM,WAAW,MAAM;IACrB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,SAAS,GAAG,QAAQ,CAAC;IACjC,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,aAAa;IAC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AE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,OAAO,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,MAAM,CAavS"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Dbrij Storage SDK. Zero dependencies; runs in Node 18+ and in browsers.
|
|
4
|
+
*
|
|
5
|
+
* import { DbrijStorage } from '@dbrij/storage';
|
|
6
|
+
* const storage = new DbrijStorage({ keyId: 'dbsk_…', secret: 'dbss_…' });
|
|
7
|
+
* const bucket = await storage.buckets.create('my-assets');
|
|
8
|
+
* const file = await storage.objects.upload(bucket.id, 'images/logo.png', bytes, { contentType: 'image/png' });
|
|
9
|
+
* const link = await storage.delivery.url(bucket.id, 'images/logo.png', { transform: 'w_800,f_auto' });
|
|
10
|
+
*
|
|
11
|
+
* In a browser, use an upload preset instead of a key:
|
|
12
|
+
* await DbrijStorage.presetUpload('dbup_…', file);
|
|
13
|
+
*
|
|
14
|
+
* The S3 protocol is served too (s3.dbrij.com): any AWS SDK works with the same key
|
|
15
|
+
* id and secret. This client covers what S3 does not: delivery links, transformations,
|
|
16
|
+
* presets, and the confirm step REST uploads need.
|
|
17
|
+
*/
|
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
+
exports.DbrijStorage = exports.DbrijStorageError = void 0;
|
|
20
|
+
exports.transform = transform;
|
|
21
|
+
class DbrijStorageError extends Error {
|
|
22
|
+
status;
|
|
23
|
+
constructor(message, status) {
|
|
24
|
+
super(message);
|
|
25
|
+
this.status = status;
|
|
26
|
+
this.name = 'DbrijStorageError';
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.DbrijStorageError = DbrijStorageError;
|
|
30
|
+
const DEFAULT_BASE = 'https://api.dbrij.com/api/v1';
|
|
31
|
+
async function unwrap(res) {
|
|
32
|
+
const text = await res.text();
|
|
33
|
+
let body = null;
|
|
34
|
+
try {
|
|
35
|
+
body = text ? JSON.parse(text) : null;
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
body = null;
|
|
39
|
+
}
|
|
40
|
+
if (!res.ok) {
|
|
41
|
+
const message = body?.message ?? `Request failed (${res.status})`;
|
|
42
|
+
throw new DbrijStorageError(message, res.status);
|
|
43
|
+
}
|
|
44
|
+
const env = body;
|
|
45
|
+
return env && typeof env === 'object' && 'data' in env ? env.data : body;
|
|
46
|
+
}
|
|
47
|
+
async function putBytes(fetchImpl, url, headers, bytes, onProgress) {
|
|
48
|
+
if (onProgress && typeof XMLHttpRequest !== 'undefined') {
|
|
49
|
+
await new Promise((resolve, reject) => {
|
|
50
|
+
const xhr = new XMLHttpRequest();
|
|
51
|
+
xhr.open('PUT', url, true);
|
|
52
|
+
for (const [k, v] of Object.entries(headers))
|
|
53
|
+
xhr.setRequestHeader(k, v);
|
|
54
|
+
xhr.upload.onprogress = (e) => {
|
|
55
|
+
if (e.lengthComputable)
|
|
56
|
+
onProgress(e.loaded / e.total);
|
|
57
|
+
};
|
|
58
|
+
xhr.onload = () => (xhr.status >= 200 && xhr.status < 300 ? resolve() : reject(new DbrijStorageError(`Upload failed (${xhr.status})`, xhr.status)));
|
|
59
|
+
xhr.onerror = () => reject(new DbrijStorageError('Upload failed: network error', 0));
|
|
60
|
+
xhr.send(bytes);
|
|
61
|
+
});
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const res = await fetchImpl(url, { method: 'PUT', headers, body: bytes });
|
|
65
|
+
if (!res.ok)
|
|
66
|
+
throw new DbrijStorageError(`Upload failed (${res.status})`, res.status);
|
|
67
|
+
}
|
|
68
|
+
class DbrijStorage {
|
|
69
|
+
base;
|
|
70
|
+
fetchImpl;
|
|
71
|
+
auth;
|
|
72
|
+
constructor(opts = {}) {
|
|
73
|
+
this.base = (opts.baseUrl ?? DEFAULT_BASE).replace(/\/+$/, '');
|
|
74
|
+
this.fetchImpl = opts.fetch ?? globalThis.fetch;
|
|
75
|
+
if (!this.fetchImpl)
|
|
76
|
+
throw new Error('No fetch available: pass one in options');
|
|
77
|
+
this.auth = opts.keyId && opts.secret ? `Bearer ${opts.keyId}:${opts.secret}` : null;
|
|
78
|
+
}
|
|
79
|
+
async call(method, path, body) {
|
|
80
|
+
if (!this.auth)
|
|
81
|
+
throw new Error('This call needs an access key (keyId + secret). In a browser use an upload preset.');
|
|
82
|
+
const res = await this.fetchImpl(`${this.base}${path}`, {
|
|
83
|
+
method,
|
|
84
|
+
headers: { Authorization: this.auth, ...(body !== undefined ? { 'Content-Type': 'application/json' } : {}) },
|
|
85
|
+
body: body !== undefined ? JSON.stringify(body) : undefined,
|
|
86
|
+
});
|
|
87
|
+
return unwrap(res);
|
|
88
|
+
}
|
|
89
|
+
buckets = {
|
|
90
|
+
list: () => this.call('GET', '/storage/buckets'),
|
|
91
|
+
create: (name, visibility = 'private') => this.call('POST', '/storage/buckets', { name, visibility }),
|
|
92
|
+
};
|
|
93
|
+
objects = {
|
|
94
|
+
list: (bucketId, prefix) => this.call('GET', `/storage/buckets/${bucketId}/objects${prefix ? `?prefix=${encodeURIComponent(prefix)}` : ''}`),
|
|
95
|
+
/** Presign, PUT the bytes to the store, confirm. Returns the confirmed object. */
|
|
96
|
+
upload: async (bucketId, key, bytes, opts = {}) => {
|
|
97
|
+
const contentType = opts.contentType ?? (bytes instanceof Blob && bytes.type ? bytes.type : 'application/octet-stream');
|
|
98
|
+
const start = await this.call('POST', `/storage/buckets/${bucketId}/upload-url`, { key, contentType });
|
|
99
|
+
await putBytes(this.fetchImpl, start.url, start.headers, bytes, opts.onProgress);
|
|
100
|
+
return this.call('POST', `/storage/buckets/${bucketId}/confirm`, { key: start.key });
|
|
101
|
+
},
|
|
102
|
+
delete: (bucketId, key) => this.call('DELETE', `/storage/buckets/${bucketId}/object?key=${encodeURIComponent(key)}`),
|
|
103
|
+
/** A short lived signed URL for the raw bytes (one hour). For links to hand out, prefer delivery.url. */
|
|
104
|
+
downloadUrl: async (bucketId, key) => {
|
|
105
|
+
if (!this.auth)
|
|
106
|
+
throw new Error('This call needs an access key');
|
|
107
|
+
const res = await this.fetchImpl(`${this.base}/storage/buckets/${bucketId}/object?key=${encodeURIComponent(key)}`, { method: 'GET', headers: { Authorization: this.auth }, redirect: 'manual' });
|
|
108
|
+
const location = res.headers.get('location');
|
|
109
|
+
if (!location)
|
|
110
|
+
throw new DbrijStorageError(`No download URL (${res.status})`, res.status);
|
|
111
|
+
return location;
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
delivery = {
|
|
115
|
+
/** A delivery link: permanent for public objects, signed and expiring for private ones, transformed on request. */
|
|
116
|
+
url: (bucketId, key, opts = {}) => this.call('POST', `/storage/buckets/${bucketId}/delivery-url`, { key, ...opts }),
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* Keyless upload through a preset (dbup_…): safe in a browser. Starts the upload,
|
|
120
|
+
* PUTs the bytes, confirms, and returns the file with a delivery link.
|
|
121
|
+
*/
|
|
122
|
+
static async presetUpload(preset, file, opts = {}) {
|
|
123
|
+
const fetchImpl = opts.fetch ?? globalThis.fetch;
|
|
124
|
+
const base = (opts.baseUrl ?? DEFAULT_BASE).replace(/\/+$/, '');
|
|
125
|
+
const filename = opts.filename ?? file.name ?? 'file';
|
|
126
|
+
const contentType = opts.contentType ?? file.type ?? 'application/octet-stream';
|
|
127
|
+
const startRes = await fetchImpl(`${base}/storage/uploads/${encodeURIComponent(preset)}`, {
|
|
128
|
+
method: 'POST',
|
|
129
|
+
headers: { 'Content-Type': 'application/json' },
|
|
130
|
+
body: JSON.stringify({ filename, contentType, size: file.size }),
|
|
131
|
+
});
|
|
132
|
+
const start = await unwrap(startRes);
|
|
133
|
+
await putBytes(fetchImpl, start.uploadUrl, start.headers, file, opts.onProgress);
|
|
134
|
+
const confirmRes = await fetchImpl(start.confirmUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ key: start.key }) });
|
|
135
|
+
return unwrap(confirmRes);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
exports.DbrijStorage = DbrijStorage;
|
|
139
|
+
/** Build a transformation segment from named options, for readable code. */
|
|
140
|
+
function transform(opts) {
|
|
141
|
+
const parts = [];
|
|
142
|
+
if (opts.width)
|
|
143
|
+
parts.push(`w_${opts.width}`);
|
|
144
|
+
if (opts.height)
|
|
145
|
+
parts.push(`h_${opts.height}`);
|
|
146
|
+
if (opts.crop)
|
|
147
|
+
parts.push(`c_${opts.crop}`);
|
|
148
|
+
if (opts.gravity)
|
|
149
|
+
parts.push(`g_${opts.gravity}`);
|
|
150
|
+
if (opts.format)
|
|
151
|
+
parts.push(`f_${opts.format}`);
|
|
152
|
+
if (opts.quality)
|
|
153
|
+
parts.push(`q_${opts.quality}`);
|
|
154
|
+
if (opts.dpr)
|
|
155
|
+
parts.push(`dpr_${opts.dpr}`);
|
|
156
|
+
if (opts.radius != null)
|
|
157
|
+
parts.push(`r_${opts.radius}`);
|
|
158
|
+
if (opts.grayscale)
|
|
159
|
+
parts.push('e_grayscale');
|
|
160
|
+
if (opts.blur)
|
|
161
|
+
parts.push(`e_blur:${opts.blur}`);
|
|
162
|
+
return parts.join(',');
|
|
163
|
+
}
|
|
164
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;GAeG;;;AA0LH,8BAaC;AA5ID,MAAa,iBAAkB,SAAQ,KAAK;IAGxB;IAFlB,YACE,OAAe,EACC,MAAc;QAE9B,KAAK,CAAC,OAAO,CAAC,CAAC;QAFC,WAAM,GAAN,MAAM,CAAQ;QAG9B,IAAI,CAAC,IAAI,GAAG,mB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AI,CAAC,KAAK,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;IAC5C,IAAI,IAAI,CAAC,OAAO;QAAE,KAAK,CAAC,IAAI,CAAC,KAAK,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC;IAClD,IAAI,IAAI,CAAC,MAAM;QAAE,KAAK,CAAC,IAAI,CAAC,KAAK,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;IAChD,IAAI,IAAI,CAAC,OAAO;QAAE,KAAK,CAAC,IAAI,CAAC,KAAK,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC;IAClD,IAAI,IAAI,CAAC,GAAG;QAAE,KAAK,CAAC,IAAI,CAAC,OAAO,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;IAC5C,IAAI,IAAI,CAAC,MAAM,IAAI,IAAI;QAAE,KAAK,CAAC,IAAI,CAAC,KAAK,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;IACxD,IAAI,IAAI,CAAC,SAAS;QAAE,KAAK,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;IAC9C,IAAI,IAAI,CAAC,IAAI;QAAE,KAAK,CAAC,IAAI,CAAC,UAAU,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;IACjD,OAAO,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACzB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dbrij/storage",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Dbrij Storage SDK: buckets, uploads, delivery links and transformations from Node or the browser. Zero dependencies.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"README.md",
|
|
17
|
+
"LICENSE"
|
|
18
|
+
],
|
|
19
|
+
"keywords": [
|
|
20
|
+
"object-storage",
|
|
21
|
+
"s3",
|
|
22
|
+
"cdn",
|
|
23
|
+
"upload",
|
|
24
|
+
"image-transformation",
|
|
25
|
+
"dbrij"
|
|
26
|
+
],
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@eslint/js": "^9.18.0",
|
|
29
|
+
"eslint": "^9.18.0",
|
|
30
|
+
"typescript": "^5.7.3",
|
|
31
|
+
"typescript-eslint": "^8.20.0"
|
|
32
|
+
},
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "git+https://github.com/SAW-TECHNOLOGIES-LIMITED/Dbrij.git",
|
|
36
|
+
"directory": "packages/storage"
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "tsc -p tsconfig.json",
|
|
43
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
44
|
+
"lint": "eslint src --max-warnings 0",
|
|
45
|
+
"clean": "rm -rf dist .turbo"
|
|
46
|
+
}
|
|
6
47
|
}
|