usebecky 0.0.0-stage → 0.2.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.
Files changed (135) hide show
  1. package/README.md +701 -2
  2. package/dist/analytics/client.d.ts +219 -0
  3. package/dist/analytics/client.js +923 -0
  4. package/dist/analytics/cmp.d.ts +20 -0
  5. package/dist/analytics/cmp.js +166 -0
  6. package/dist/analytics/context.d.ts +29 -0
  7. package/dist/analytics/context.js +97 -0
  8. package/dist/auth/client.d.ts +401 -0
  9. package/dist/auth/client.js +1081 -0
  10. package/dist/auth/webauthn.d.ts +18 -0
  11. package/dist/auth/webauthn.js +138 -0
  12. package/dist/cli/ai.d.ts +54 -0
  13. package/dist/cli/ai.js +202 -0
  14. package/dist/cli/analytics.d.ts +53 -0
  15. package/dist/cli/analytics.js +357 -0
  16. package/dist/cli/config.d.ts +75 -0
  17. package/dist/cli/config.js +718 -0
  18. package/dist/cli/db.d.ts +48 -0
  19. package/dist/cli/db.js +215 -0
  20. package/dist/cli/deploy-api.d.ts +168 -0
  21. package/dist/cli/deploy-api.js +177 -0
  22. package/dist/cli/deploy-ci.d.ts +15 -0
  23. package/dist/cli/deploy-ci.js +54 -0
  24. package/dist/cli/deploy-detect.d.ts +64 -0
  25. package/dist/cli/deploy-detect.js +249 -0
  26. package/dist/cli/deploy-files.d.ts +112 -0
  27. package/dist/cli/deploy-files.js +778 -0
  28. package/dist/cli/deploy.d.ts +10 -0
  29. package/dist/cli/deploy.js +430 -0
  30. package/dist/cli/emails.d.ts +29 -0
  31. package/dist/cli/emails.js +122 -0
  32. package/dist/cli/framework.d.ts +135 -0
  33. package/dist/cli/framework.js +459 -0
  34. package/dist/cli/functions.d.ts +59 -0
  35. package/dist/cli/functions.js +426 -0
  36. package/dist/cli/hosting.d.ts +4 -0
  37. package/dist/cli/hosting.js +532 -0
  38. package/dist/cli/index.d.ts +2 -0
  39. package/dist/cli/index.js +9 -0
  40. package/dist/cli/payments.d.ts +35 -0
  41. package/dist/cli/payments.js +176 -0
  42. package/dist/cli/push.d.ts +36 -0
  43. package/dist/cli/push.js +172 -0
  44. package/dist/cli/run.d.ts +65 -0
  45. package/dist/cli/run.js +850 -0
  46. package/dist/cli/social.d.ts +52 -0
  47. package/dist/cli/social.js +532 -0
  48. package/dist/cli/tokens.d.ts +28 -0
  49. package/dist/cli/tokens.js +114 -0
  50. package/dist/cli/ui.d.ts +38 -0
  51. package/dist/cli/ui.js +140 -0
  52. package/dist/client.d.ts +94 -0
  53. package/dist/client.js +156 -0
  54. package/dist/db/query.d.ts +162 -0
  55. package/dist/db/query.js +336 -0
  56. package/dist/db/types.d.ts +93 -0
  57. package/dist/db/types.js +4 -0
  58. package/dist/forms/client.d.ts +65 -0
  59. package/dist/forms/client.js +106 -0
  60. package/dist/functions/client.d.ts +37 -0
  61. package/dist/functions/client.js +76 -0
  62. package/dist/index.d.ts +17 -0
  63. package/dist/index.js +16 -0
  64. package/dist/lib/dashboard-paths.d.ts +4 -0
  65. package/dist/lib/dashboard-paths.js +8 -0
  66. package/dist/lib/env.d.ts +9 -0
  67. package/dist/lib/env.js +55 -0
  68. package/dist/lib/errors.d.ts +22 -0
  69. package/dist/lib/errors.js +90 -0
  70. package/dist/lib/hosted.d.ts +2 -0
  71. package/dist/lib/hosted.js +7 -0
  72. package/dist/lib/http.d.ts +59 -0
  73. package/dist/lib/http.js +172 -0
  74. package/dist/lib/jwt.d.ts +1 -0
  75. package/dist/lib/jwt.js +31 -0
  76. package/dist/lib/locale.d.ts +12 -0
  77. package/dist/lib/locale.js +51 -0
  78. package/dist/lib/pkce.d.ts +20 -0
  79. package/dist/lib/pkce.js +124 -0
  80. package/dist/lib/storage-adapter.d.ts +15 -0
  81. package/dist/lib/storage-adapter.js +91 -0
  82. package/dist/lib/types.d.ts +339 -0
  83. package/dist/lib/types.js +1 -0
  84. package/dist/newsletter/client.d.ts +140 -0
  85. package/dist/newsletter/client.js +85 -0
  86. package/dist/payments/client.d.ts +352 -0
  87. package/dist/payments/client.js +323 -0
  88. package/dist/push/client.d.ts +110 -0
  89. package/dist/push/client.js +179 -0
  90. package/dist/realtime/client.d.ts +131 -0
  91. package/dist/realtime/client.js +513 -0
  92. package/dist/server/analytics.d.ts +176 -0
  93. package/dist/server/analytics.js +49 -0
  94. package/dist/server/cache.d.ts +74 -0
  95. package/dist/server/cache.js +154 -0
  96. package/dist/server/emails.d.ts +142 -0
  97. package/dist/server/emails.js +123 -0
  98. package/dist/server/events.d.ts +357 -0
  99. package/dist/server/events.js +219 -0
  100. package/dist/server/functions.d.ts +40 -0
  101. package/dist/server/functions.js +1 -0
  102. package/dist/server/http-utils.d.ts +10 -0
  103. package/dist/server/http-utils.js +115 -0
  104. package/dist/server/index.d.ts +195 -0
  105. package/dist/server/index.js +314 -0
  106. package/dist/server/payments.d.ts +218 -0
  107. package/dist/server/payments.js +150 -0
  108. package/dist/server/push.d.ts +85 -0
  109. package/dist/server/push.js +37 -0
  110. package/dist/server/usage.d.ts +43 -0
  111. package/dist/server/usage.js +8 -0
  112. package/dist/storage/client.d.ts +143 -0
  113. package/dist/storage/client.js +224 -0
  114. package/package.json +52 -4
  115. package/skill/usebecky/SKILL.md +339 -0
  116. package/skill/usebecky/references/access-rules.md +307 -0
  117. package/skill/usebecky/references/addons.md +68 -0
  118. package/skill/usebecky/references/analytics.md +166 -0
  119. package/skill/usebecky/references/android.md +434 -0
  120. package/skill/usebecky/references/auth.md +379 -0
  121. package/skill/usebecky/references/cache.md +51 -0
  122. package/skill/usebecky/references/components.md +127 -0
  123. package/skill/usebecky/references/emails.md +105 -0
  124. package/skill/usebecky/references/expo.md +147 -0
  125. package/skill/usebecky/references/functions.md +99 -0
  126. package/skill/usebecky/references/hosting.md +119 -0
  127. package/skill/usebecky/references/http-api.md +233 -0
  128. package/skill/usebecky/references/ios.md +355 -0
  129. package/skill/usebecky/references/payments.md +493 -0
  130. package/skill/usebecky/references/realtime.md +143 -0
  131. package/skill/usebecky/references/scheduled-jobs.md +82 -0
  132. package/skill/usebecky/references/sdk.md +838 -0
  133. package/skill/usebecky/references/storage.md +210 -0
  134. package/skill/usebecky/references/templates.md +191 -0
  135. package/skill/usebecky/references/wordpress.md +87 -0
@@ -0,0 +1,143 @@
1
+ import type { HttpClient } from '../lib/http.ts';
2
+ import type { Bucket, FileObject, ObjectInfo, Result } from '../lib/types.ts';
3
+ export type FileBody = Blob | ArrayBuffer | ArrayBufferView | ReadableStream | FormData | string;
4
+ export declare function guessContentType(path: string): string | undefined;
5
+ /** Strip leading/trailing slashes and collapse duplicates. */
6
+ export declare function cleanPath(path: string): string;
7
+ export interface UploadOptions {
8
+ /** Overwrite an existing file with the same path. Default false. */
9
+ upsert?: boolean;
10
+ /** MIME type. Default: the file's type, or guessed from the extension. */
11
+ contentType?: string;
12
+ /** Cache-Control max-age in seconds, e.g. '3600'. */
13
+ cacheControl?: string;
14
+ }
15
+ /** What `upload()` and `update()` answer. */
16
+ export interface UploadResult {
17
+ /** The path inside the bucket. */
18
+ path: string;
19
+ /** `<bucket>/<path>` */
20
+ fullPath: string;
21
+ id: string | null;
22
+ /** This upload's version (a new upload with other content or headers gets a new one). Pass it to `getPublicUrl(path, { version })`. */
23
+ version: string | null;
24
+ }
25
+ /**
26
+ * Resize or convert an image on the fly (image transformations). Images are never made bigger than
27
+ * the original; sizes are 1-2500 pixels. The server caches each size, so repeat requests are fast.
28
+ */
29
+ export interface TransformOptions {
30
+ /** Width in pixels (1-2500). With only a width, the height follows the image's proportions. */
31
+ width?: number;
32
+ /** Height in pixels (1-2500). */
33
+ height?: number;
34
+ /** With both sizes: 'cover' (default) fills the box and crops, 'contain' fits inside, 'fill' stretches. */
35
+ resize?: 'cover' | 'contain' | 'fill';
36
+ /** 20-100, default 80. Lower is smaller and blurrier. */
37
+ quality?: number;
38
+ /** 'webp' and 'avif' are much smaller than 'jpeg' and 'png'. Default 'origin' keeps the file's format. */
39
+ format?: 'origin' | 'webp' | 'avif' | 'jpeg' | 'png';
40
+ }
41
+ /** Transform options as query parameters (only the ones that are set). */
42
+ export declare function transformParams(t: TransformOptions | undefined): Array<[string, string]>;
43
+ export declare class BucketApi {
44
+ private http;
45
+ readonly bucketId: string;
46
+ /** Publishable key used for public links (never the secret key). */
47
+ private publicKey;
48
+ constructor(http: HttpClient, bucketId: string, publicKey: string | null);
49
+ private obj;
50
+ /** Upload a file. `path` is the file name inside the bucket, e.g. `user-1/avatar.png`. The answer's `version`
51
+ * changes with every new upload: pass it to `getPublicUrl(path, { version })` for a link that never goes stale. */
52
+ upload(path: string, file: FileBody, options?: UploadOptions): Promise<Result<UploadResult>>;
53
+ /** Replace an existing file. */
54
+ update(path: string, file: FileBody, options?: Omit<UploadOptions, 'upsert'>): Promise<Result<UploadResult>>;
55
+ private write;
56
+ /** Download a file as a Blob (`await data.text()`, `URL.createObjectURL(data)`, ...).
57
+ * Pass `{ transform: { width: 400 } }` to download a resized image instead. */
58
+ download(path: string, options?: {
59
+ transform?: TransformOptions;
60
+ }): Promise<Result<Blob>>;
61
+ /** Metadata for one file. */
62
+ info(path: string): Promise<Result<ObjectInfo>>;
63
+ /** Does the file exist (and can this user read it)? */
64
+ exists(path: string): Promise<Result<boolean>>;
65
+ /** URL for a file in a **public** bucket (no network call). Pass `{ transform: { width: 400, format: 'webp' } }`
66
+ * for a resized image, and the `version` from `upload()` for a link that loads from the edge near your visitors
67
+ * and never shows an old file (on Business; a new upload has a new version, so a new link). */
68
+ getPublicUrl(path: string, options?: {
69
+ download?: boolean | string;
70
+ transform?: TransformOptions;
71
+ version?: string | null;
72
+ }): {
73
+ data: {
74
+ publicUrl: string;
75
+ };
76
+ };
77
+ /** A link to a private file that works for `expiresIn` seconds, for anyone who has it. With
78
+ * `{ transform: { width: 400 } }` the link shows a resized image (the size is part of the link). */
79
+ createSignedUrl(path: string, expiresIn: number, options?: {
80
+ download?: boolean | string;
81
+ transform?: TransformOptions;
82
+ }): Promise<Result<{
83
+ signedUrl: string;
84
+ path: string;
85
+ }>>;
86
+ /** Signed links for several files at once. */
87
+ createSignedUrls(paths: string[], expiresIn: number, options?: {
88
+ transform?: TransformOptions;
89
+ }): Promise<Result<Array<{
90
+ path: string;
91
+ signedUrl: string | null;
92
+ error: string | null;
93
+ }>>>;
94
+ /** A URL that lets someone upload one file to `path` without signing in. */
95
+ createSignedUploadUrl(path: string): Promise<Result<{
96
+ signedUrl: string;
97
+ token: string;
98
+ path: string;
99
+ }>>;
100
+ /** List files and folders under a folder (`''` for the top level). Folders have `id: null`. */
101
+ list(prefix?: string, options?: {
102
+ limit?: number;
103
+ offset?: number;
104
+ search?: string;
105
+ sortBy?: {
106
+ column: 'name' | 'created_at' | 'updated_at';
107
+ order?: 'asc' | 'desc';
108
+ };
109
+ }): Promise<Result<FileObject[]>>;
110
+ /** Delete files. */
111
+ remove(paths: string | string[]): Promise<Result<ObjectInfo[]>>;
112
+ /** Rename / move a file within the bucket. */
113
+ move(fromPath: string, toPath: string): Promise<Result<{
114
+ message: string;
115
+ }>>;
116
+ /** Copy a file within the bucket. */
117
+ copy(fromPath: string, toPath: string): Promise<Result<{
118
+ message: string;
119
+ path: string;
120
+ }>>;
121
+ private transfer;
122
+ }
123
+ export declare class StorageClient {
124
+ private http;
125
+ private publicKey;
126
+ constructor(http: HttpClient, publicKey: string | null);
127
+ /** Work with the files in one bucket: `baas.storage.from('avatars').upload(...)` */
128
+ from(bucketId: string): BucketApi;
129
+ listBuckets(): Promise<Result<Bucket[]>>;
130
+ getBucket(id: string): Promise<Result<Bucket>>;
131
+ createBucket(id: string, options?: {
132
+ public?: boolean;
133
+ fileSizeLimit?: number;
134
+ allowedMimeTypes?: string[];
135
+ }): Promise<Result<Bucket>>;
136
+ updateBucket(id: string, options: {
137
+ public?: boolean;
138
+ fileSizeLimit?: number | null;
139
+ allowedMimeTypes?: string[] | null;
140
+ }): Promise<Result<Bucket>>;
141
+ emptyBucket(id: string): Promise<Result<{}>>;
142
+ deleteBucket(id: string): Promise<Result<{}>>;
143
+ }
@@ -0,0 +1,224 @@
1
+ // File storage: buckets of files, protected by the same RLS policies as your tables.
2
+ import { UseBeckyError } from "../lib/errors.js";
3
+ const MIME = {
4
+ png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg', gif: 'image/gif', webp: 'image/webp', svg: 'image/svg+xml', avif: 'image/avif', ico: 'image/x-icon',
5
+ pdf: 'application/pdf', json: 'application/json', txt: 'text/plain', csv: 'text/csv', html: 'text/html', css: 'text/css', js: 'text/javascript', md: 'text/markdown',
6
+ mp4: 'video/mp4', webm: 'video/webm', mov: 'video/quicktime', mp3: 'audio/mpeg', wav: 'audio/wav', ogg: 'audio/ogg',
7
+ zip: 'application/zip', epub: 'application/epub+zip', xml: 'application/xml', woff2: 'font/woff2',
8
+ };
9
+ export function guessContentType(path) {
10
+ const ext = path.split('.').pop()?.toLowerCase();
11
+ return ext ? MIME[ext] : undefined;
12
+ }
13
+ /** Strip leading/trailing slashes and collapse duplicates. */
14
+ export function cleanPath(path) {
15
+ return String(path).replace(/^\/+|\/+$/g, '').replace(/\/{2,}/g, '/');
16
+ }
17
+ function encodePath(path) {
18
+ return cleanPath(path).split('/').map(encodeURIComponent).join('/');
19
+ }
20
+ /** Transform options as query parameters (only the ones that are set). */
21
+ export function transformParams(t) {
22
+ if (!t)
23
+ return [];
24
+ const out = [];
25
+ for (const k of ['width', 'height', 'resize', 'quality', 'format']) {
26
+ const v = t[k];
27
+ if (v !== undefined && v !== null && v !== '')
28
+ out.push([k, String(v)]);
29
+ }
30
+ return out;
31
+ }
32
+ /** Transform options for a JSON body (signed URLs), or undefined when none are set. */
33
+ function transformBody(t) {
34
+ const params = transformParams(t);
35
+ if (!params.length)
36
+ return undefined;
37
+ return Object.fromEntries(params.map(([k, v]) => [k, k === 'width' || k === 'height' || k === 'quality' ? Number(v) : v]));
38
+ }
39
+ export class BucketApi {
40
+ http;
41
+ bucketId;
42
+ /** Publishable key used for public links (never the secret key). */
43
+ publicKey;
44
+ constructor(http, bucketId, publicKey) {
45
+ this.http = http;
46
+ this.bucketId = bucketId;
47
+ this.publicKey = publicKey;
48
+ }
49
+ obj(path) {
50
+ return `/storage/v1/object/${encodeURIComponent(this.bucketId)}/${encodePath(path)}`;
51
+ }
52
+ /** Upload a file. `path` is the file name inside the bucket, e.g. `user-1/avatar.png`. The answer's `version`
53
+ * changes with every new upload: pass it to `getPublicUrl(path, { version })` for a link that never goes stale. */
54
+ async upload(path, file, options = {}) {
55
+ return this.write('POST', path, file, options);
56
+ }
57
+ /** Replace an existing file. */
58
+ async update(path, file, options = {}) {
59
+ return this.write('PUT', path, file, { ...options, upsert: true });
60
+ }
61
+ async write(method, path, file, options) {
62
+ if (file === undefined || file === null)
63
+ return { data: null, error: new UseBeckyError({ code: 'missing_file', message: 'No file was given to upload.', hint: 'Pass a File, Blob, Buffer or string as the second argument.', status: 400 }) };
64
+ const headers = {};
65
+ const isForm = typeof globalThis.FormData === 'function' && file instanceof globalThis.FormData;
66
+ if (!isForm) {
67
+ const type = options.contentType ?? (file?.type || undefined) ?? guessContentType(path) ?? (typeof file === 'string' ? 'text/plain;charset=UTF-8' : 'application/octet-stream');
68
+ headers['content-type'] = type;
69
+ }
70
+ if (options.upsert)
71
+ headers['x-upsert'] = 'true';
72
+ if (options.cacheControl)
73
+ headers['cache-control'] = `max-age=${options.cacheControl}`;
74
+ const res = await this.http.request(this.obj(path), { method, headers, body: file });
75
+ if (res.error)
76
+ return { data: null, error: res.error };
77
+ const clean = cleanPath(path);
78
+ return { data: { path: clean, fullPath: res.data?.key ?? `${this.bucketId}/${clean}`, id: res.data?.id ?? null, version: res.data?.version ?? null }, error: null };
79
+ }
80
+ /** Download a file as a Blob (`await data.text()`, `URL.createObjectURL(data)`, ...).
81
+ * Pass `{ transform: { width: 400 } }` to download a resized image instead. */
82
+ async download(path, options = {}) {
83
+ const params = transformParams(options.transform);
84
+ const url = params.length ? `/storage/v1/render/image/authenticated/${encodeURIComponent(this.bucketId)}/${encodePath(path)}` : this.obj(path);
85
+ const res = await this.http.request(url, { parse: 'blob', query: params.length ? params : undefined });
86
+ return res.error ? { data: null, error: res.error } : { data: res.data, error: null };
87
+ }
88
+ /** Metadata for one file. */
89
+ async info(path) {
90
+ const res = await this.http.request(`/storage/v1/object/info/${encodeURIComponent(this.bucketId)}/${encodePath(path)}`);
91
+ return res.error ? { data: null, error: res.error } : { data: res.data, error: null };
92
+ }
93
+ /** Does the file exist (and can this user read it)? */
94
+ async exists(path) {
95
+ const res = await this.http.request(this.obj(path), { method: 'HEAD' });
96
+ if (!res.error)
97
+ return { data: true, error: null };
98
+ if (res.error.status === 404 || res.error.status === 400)
99
+ return { data: false, error: null };
100
+ return { data: null, error: res.error };
101
+ }
102
+ /** URL for a file in a **public** bucket (no network call). Pass `{ transform: { width: 400, format: 'webp' } }`
103
+ * for a resized image, and the `version` from `upload()` for a link that loads from the edge near your visitors
104
+ * and never shows an old file (on Business; a new upload has a new version, so a new link). */
105
+ getPublicUrl(path, options = {}) {
106
+ const params = new URLSearchParams();
107
+ if (this.publicKey)
108
+ params.set('apikey', this.publicKey);
109
+ if (options.version)
110
+ params.set('v', options.version);
111
+ if (options.download)
112
+ params.set('download', options.download === true ? '' : options.download);
113
+ const transform = transformParams(options.transform);
114
+ for (const [k, v] of transform)
115
+ params.set(k, v);
116
+ const qs = params.toString();
117
+ const kind = transform.length ? 'render/image/public' : 'object/public';
118
+ return { data: { publicUrl: `${this.http.url}/storage/v1/${kind}/${encodeURIComponent(this.bucketId)}/${encodePath(path)}${qs ? `?${qs}` : ''}` } };
119
+ }
120
+ /** A link to a private file that works for `expiresIn` seconds, for anyone who has it. With
121
+ * `{ transform: { width: 400 } }` the link shows a resized image (the size is part of the link). */
122
+ async createSignedUrl(path, expiresIn, options = {}) {
123
+ const res = await this.http.request(`/storage/v1/object/sign/${encodeURIComponent(this.bucketId)}/${encodePath(path)}`, { method: 'POST', body: { expiresIn, transform: transformBody(options.transform) } });
124
+ if (res.error)
125
+ return { data: null, error: res.error };
126
+ let signedUrl = res.data?.url ?? (res.data?.signedURL ? absolute(this.http.url, res.data.signedURL) : '');
127
+ if (options.download && signedUrl)
128
+ signedUrl += `${signedUrl.includes('?') ? '&' : '?'}download=${options.download === true ? '' : encodeURIComponent(options.download)}`;
129
+ return { data: { signedUrl, path: cleanPath(path) }, error: null };
130
+ }
131
+ /** Signed links for several files at once. */
132
+ async createSignedUrls(paths, expiresIn, options = {}) {
133
+ const results = await Promise.all(paths.map((p) => this.createSignedUrl(p, expiresIn, { transform: options.transform })));
134
+ return { data: results.map((r, i) => ({ path: cleanPath(paths[i]), signedUrl: r.data?.signedUrl ?? null, error: r.error?.message ?? null })), error: null };
135
+ }
136
+ /** A URL that lets someone upload one file to `path` without signing in. */
137
+ async createSignedUploadUrl(path) {
138
+ const res = await this.http.request(`/storage/v1/object/upload/sign/${encodeURIComponent(this.bucketId)}/${encodePath(path)}`, { method: 'POST', body: {} });
139
+ if (res.error)
140
+ return { data: null, error: res.error };
141
+ return { data: { signedUrl: absolute(this.http.url, res.data?.url ?? ''), token: res.data?.token ?? '', path: cleanPath(path) }, error: null };
142
+ }
143
+ /** List files and folders under a folder (`''` for the top level). Folders have `id: null`. */
144
+ async list(prefix = '', options = {}) {
145
+ const body = { prefix: cleanPath(prefix), limit: options.limit ?? 100, offset: options.offset ?? 0 };
146
+ if (options.search)
147
+ body.search = options.search;
148
+ if (options.sortBy)
149
+ body.sortBy = { column: options.sortBy.column, order: options.sortBy.order ?? 'asc' };
150
+ const res = await this.http.request(`/storage/v1/object/list/${encodeURIComponent(this.bucketId)}`, { method: 'POST', body });
151
+ return res.error ? { data: null, error: res.error } : { data: res.data ?? [], error: null };
152
+ }
153
+ /** Delete files. */
154
+ async remove(paths) {
155
+ const list = (Array.isArray(paths) ? paths : [paths]).map(cleanPath);
156
+ const res = await this.http.request(`/storage/v1/object/${encodeURIComponent(this.bucketId)}`, { method: 'DELETE', body: { prefixes: list } });
157
+ return res.error ? { data: null, error: res.error } : { data: res.data ?? [], error: null };
158
+ }
159
+ /** Rename / move a file within the bucket. */
160
+ async move(fromPath, toPath) {
161
+ return this.transfer('move', fromPath, toPath);
162
+ }
163
+ /** Copy a file within the bucket. */
164
+ async copy(fromPath, toPath) {
165
+ const r = await this.transfer('copy', fromPath, toPath);
166
+ return r.error ? r : { data: { ...r.data, path: cleanPath(toPath) }, error: null };
167
+ }
168
+ async transfer(kind, from, to) {
169
+ const res = await this.http.request(`/storage/v1/object/${kind}`, { method: 'POST', body: { bucketId: this.bucketId, sourceKey: cleanPath(from), destinationKey: cleanPath(to) } });
170
+ if (res.error)
171
+ return { data: null, error: res.error };
172
+ return { data: { message: res.data?.message ?? (kind === 'move' ? 'Moved' : 'Copied'), ...(res.data && typeof res.data === 'object' ? res.data : {}) }, error: null };
173
+ }
174
+ }
175
+ function absolute(base, path) {
176
+ if (!path)
177
+ return '';
178
+ if (/^https?:\/\//.test(path))
179
+ return path;
180
+ return `${base}${path.startsWith('/') ? '' : '/'}${path}`;
181
+ }
182
+ export class StorageClient {
183
+ http;
184
+ publicKey;
185
+ constructor(http, publicKey) {
186
+ this.http = http;
187
+ this.publicKey = publicKey;
188
+ }
189
+ /** Work with the files in one bucket: `baas.storage.from('avatars').upload(...)` */
190
+ from(bucketId) {
191
+ return new BucketApi(this.http, bucketId, this.publicKey);
192
+ }
193
+ // Bucket management (needs the secret key, i.e. from your server via `usebecky/server`).
194
+ async listBuckets() {
195
+ const res = await this.http.request('/storage/v1/bucket');
196
+ return res.error ? { data: null, error: res.error } : { data: res.data ?? [], error: null };
197
+ }
198
+ async getBucket(id) {
199
+ const res = await this.http.request(`/storage/v1/bucket/${encodeURIComponent(id)}`);
200
+ return res.error ? { data: null, error: res.error } : { data: res.data, error: null };
201
+ }
202
+ async createBucket(id, options = {}) {
203
+ const res = await this.http.request('/storage/v1/bucket', {
204
+ method: 'POST',
205
+ body: { id, public: options.public ?? false, file_size_limit: options.fileSizeLimit, allowed_mime_types: options.allowedMimeTypes },
206
+ });
207
+ return res.error ? { data: null, error: res.error } : { data: res.data, error: null };
208
+ }
209
+ async updateBucket(id, options) {
210
+ const res = await this.http.request(`/storage/v1/bucket/${encodeURIComponent(id)}`, {
211
+ method: 'PUT',
212
+ body: { public: options.public, file_size_limit: options.fileSizeLimit, allowed_mime_types: options.allowedMimeTypes },
213
+ });
214
+ return res.error ? { data: null, error: res.error } : { data: res.data, error: null };
215
+ }
216
+ async emptyBucket(id) {
217
+ const res = await this.http.request(`/storage/v1/bucket/${encodeURIComponent(id)}/empty`, { method: 'POST' });
218
+ return res.error ? { data: null, error: res.error } : { data: res.data ?? {}, error: null };
219
+ }
220
+ async deleteBucket(id) {
221
+ const res = await this.http.request(`/storage/v1/bucket/${encodeURIComponent(id)}`, { method: 'DELETE' });
222
+ return res.error ? { data: null, error: res.error } : { data: res.data ?? {}, error: null };
223
+ }
224
+ }
package/package.json CHANGED
@@ -1,6 +1,54 @@
1
1
  {
2
2
  "name": "usebecky",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.0",
4
+ "description": "One package for your whole backend: auth, database, storage, realtime and payments.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "default": "./dist/index.js"
13
+ },
14
+ "./server": {
15
+ "types": "./dist/server/index.d.ts",
16
+ "default": "./dist/server/index.js"
17
+ },
18
+ "./package.json": "./package.json"
19
+ },
20
+ "bin": {
21
+ "usebecky": "./dist/cli/index.js"
22
+ },
23
+ "files": [
24
+ "dist",
25
+ "skill",
26
+ "README.md"
27
+ ],
28
+ "scripts": {
29
+ "build": "node scripts/build.mjs",
30
+ "prepack": "npm run build",
31
+ "test": "node --test 'test/**/*.test.ts'",
32
+ "typecheck": "tsc -p ."
33
+ },
34
+ "devDependencies": {
35
+ "@types/node": "^26.6.6",
36
+ "typescript": "5.9"
37
+ },
38
+ "keywords": [
39
+ "backend",
40
+ "baas",
41
+ "auth",
42
+ "database",
43
+ "postgres",
44
+ "storage",
45
+ "realtime",
46
+ "stripe",
47
+ "payments",
48
+ "supabase-alternative"
49
+ ],
50
+ "sideEffects": false,
51
+ "engines": {
52
+ "node": ">=22.18"
53
+ }
54
+ }