naystack 1.5.12 → 1.5.14

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.
@@ -20,7 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/auth/instagram/client.ts
21
21
  var client_exports = {};
22
22
  __export(client_exports, {
23
- getInstagramAuthorizationURLSetup: () => getInstagramAuthorizationURLSetup
23
+ getInstagramAuthorizationURL: () => getInstagramAuthorizationURL
24
24
  });
25
25
  module.exports = __toCommonJS(client_exports);
26
26
 
@@ -74,10 +74,10 @@ function getEnv(key, skipCheck) {
74
74
  }
75
75
 
76
76
  // src/auth/instagram/client.ts
77
- var getInstagramAuthorizationURLSetup = (redirectURL) => (token) => `https://www.instagram.com/oauth/authorize?client_id=${getEnv(
77
+ var getInstagramAuthorizationURL = (token) => `https://www.instagram.com/oauth/authorize?client_id=${getEnv(
78
78
  "INSTAGRAM_CLIENT_ID" /* INSTAGRAM_CLIENT_ID */
79
- )}&response_type=code&enable_fb_login=0&force_authentication=1&scope=instagram_business_basic&state=${token}&redirect_uri=${redirectURL}`;
79
+ )}&response_type=code&enable_fb_login=0&force_authentication=1&scope=instagram_business_basic&state=${token}&redirect_uri=${getEnv("NEXT_PUBLIC_INSTAGRAM_AUTH_ENDPOINT" /* NEXT_PUBLIC_INSTAGRAM_AUTH_ENDPOINT */)}`;
80
80
  // Annotate the CommonJS export names for ESM import in node:
81
81
  0 && (module.exports = {
82
- getInstagramAuthorizationURLSetup
82
+ getInstagramAuthorizationURL
83
83
  });
@@ -1,22 +1,24 @@
1
1
  /**
2
- * Creates a function that builds the Instagram OAuth authorization URL for a given state token.
3
- * The state token is typically the user's access token, used to link the Instagram account to the logged-in user.
2
+ * Builds the Instagram OAuth authorization URL for the given state token.
3
+ * The state token is passed back by Instagram after authorization and is used to link
4
+ * the Instagram account to the logged-in user.
4
5
  *
5
- * @param redirectURL - The OAuth redirect URI registered with Instagram (e.g. `NEXT_PUBLIC_INSTAGRAM_AUTH_ENDPOINT`).
6
- * @returns A function `(stateToken: string) => string` that returns the full authorization URL.
6
+ * Reads `INSTAGRAM_CLIENT_ID` and `NEXT_PUBLIC_INSTAGRAM_AUTH_ENDPOINT` from the environment.
7
+ *
8
+ * @param token - The state token to embed in the authorization URL (typically the user's session or access token).
9
+ * @returns The full Instagram OAuth authorization URL.
7
10
  *
8
11
  * @example
9
12
  * ```ts
10
- * import { getInstagramAuthorizationURLSetup } from "naystack/auth/instagram/client";
13
+ * import { getInstagramAuthorizationURL } from "naystack/auth/instagram/client";
11
14
  *
12
- * const getAuthURL = getInstagramAuthorizationURLSetup("/api/instagram");
13
- * const url = getAuthURL(userAccessToken);
15
+ * const url = getInstagramAuthorizationURL(userAccessToken);
14
16
  * // => "https://www.instagram.com/oauth/authorize?client_id=...&state=...&redirect_uri=..."
15
17
  * window.location.href = url;
16
18
  * ```
17
19
  *
18
20
  * @category Auth
19
21
  */
20
- declare const getInstagramAuthorizationURLSetup: (redirectURL: string) => (token: string) => string;
22
+ declare const getInstagramAuthorizationURL: (token: string) => string;
21
23
 
22
- export { getInstagramAuthorizationURLSetup };
24
+ export { getInstagramAuthorizationURL };
@@ -1,22 +1,24 @@
1
1
  /**
2
- * Creates a function that builds the Instagram OAuth authorization URL for a given state token.
3
- * The state token is typically the user's access token, used to link the Instagram account to the logged-in user.
2
+ * Builds the Instagram OAuth authorization URL for the given state token.
3
+ * The state token is passed back by Instagram after authorization and is used to link
4
+ * the Instagram account to the logged-in user.
4
5
  *
5
- * @param redirectURL - The OAuth redirect URI registered with Instagram (e.g. `NEXT_PUBLIC_INSTAGRAM_AUTH_ENDPOINT`).
6
- * @returns A function `(stateToken: string) => string` that returns the full authorization URL.
6
+ * Reads `INSTAGRAM_CLIENT_ID` and `NEXT_PUBLIC_INSTAGRAM_AUTH_ENDPOINT` from the environment.
7
+ *
8
+ * @param token - The state token to embed in the authorization URL (typically the user's session or access token).
9
+ * @returns The full Instagram OAuth authorization URL.
7
10
  *
8
11
  * @example
9
12
  * ```ts
10
- * import { getInstagramAuthorizationURLSetup } from "naystack/auth/instagram/client";
13
+ * import { getInstagramAuthorizationURL } from "naystack/auth/instagram/client";
11
14
  *
12
- * const getAuthURL = getInstagramAuthorizationURLSetup("/api/instagram");
13
- * const url = getAuthURL(userAccessToken);
15
+ * const url = getInstagramAuthorizationURL(userAccessToken);
14
16
  * // => "https://www.instagram.com/oauth/authorize?client_id=...&state=...&redirect_uri=..."
15
17
  * window.location.href = url;
16
18
  * ```
17
19
  *
18
20
  * @category Auth
19
21
  */
20
- declare const getInstagramAuthorizationURLSetup: (redirectURL: string) => (token: string) => string;
22
+ declare const getInstagramAuthorizationURL: (token: string) => string;
21
23
 
22
- export { getInstagramAuthorizationURLSetup };
24
+ export { getInstagramAuthorizationURL };
@@ -48,9 +48,9 @@ function getEnv(key, skipCheck) {
48
48
  }
49
49
 
50
50
  // src/auth/instagram/client.ts
51
- var getInstagramAuthorizationURLSetup = (redirectURL) => (token) => `https://www.instagram.com/oauth/authorize?client_id=${getEnv(
51
+ var getInstagramAuthorizationURL = (token) => `https://www.instagram.com/oauth/authorize?client_id=${getEnv(
52
52
  "INSTAGRAM_CLIENT_ID" /* INSTAGRAM_CLIENT_ID */
53
- )}&response_type=code&enable_fb_login=0&force_authentication=1&scope=instagram_business_basic&state=${token}&redirect_uri=${redirectURL}`;
53
+ )}&response_type=code&enable_fb_login=0&force_authentication=1&scope=instagram_business_basic&state=${token}&redirect_uri=${getEnv("NEXT_PUBLIC_INSTAGRAM_AUTH_ENDPOINT" /* NEXT_PUBLIC_INSTAGRAM_AUTH_ENDPOINT */)}`;
54
54
  export {
55
- getInstagramAuthorizationURLSetup
55
+ getInstagramAuthorizationURL
56
56
  };
@@ -77,11 +77,12 @@ function getEnv(key, skipCheck) {
77
77
  // src/file/client.ts
78
78
  var useFileUpload = () => {
79
79
  const token = (0, import_client.useToken)();
80
- return (file, type, data) => {
80
+ return (file, type, config) => {
81
81
  const formData = new FormData();
82
82
  formData.append("type", type);
83
83
  formData.append("file", file);
84
- if (data) formData.append("data", JSON.stringify(data));
84
+ if (config?.async) formData.append("async", "true");
85
+ if (config?.data) formData.append("data", JSON.stringify(config.data));
85
86
  return fetch(getEnv("NEXT_PUBLIC_FILE_ENDPOINT" /* NEXT_PUBLIC_FILE_ENDPOINT */), {
86
87
  method: "PUT",
87
88
  body: formData,
@@ -42,7 +42,10 @@
42
42
  *
43
43
  * @category File
44
44
  */
45
- declare const useFileUpload: () => (file: File | Blob, type: string, data?: object) => Promise<FileUploadResponseType>;
45
+ declare const useFileUpload: () => (file: File | Blob, type: string, config?: {
46
+ data?: object;
47
+ async?: boolean;
48
+ }) => Promise<FileUploadResponseType>;
46
49
  /**
47
50
  * Shape of the JSON response from the file upload PUT endpoint.
48
51
  *
@@ -42,7 +42,10 @@
42
42
  *
43
43
  * @category File
44
44
  */
45
- declare const useFileUpload: () => (file: File | Blob, type: string, data?: object) => Promise<FileUploadResponseType>;
45
+ declare const useFileUpload: () => (file: File | Blob, type: string, config?: {
46
+ data?: object;
47
+ async?: boolean;
48
+ }) => Promise<FileUploadResponseType>;
46
49
  /**
47
50
  * Shape of the JSON response from the file upload PUT endpoint.
48
51
  *
@@ -53,11 +53,12 @@ function getEnv(key, skipCheck) {
53
53
  // src/file/client.ts
54
54
  var useFileUpload = () => {
55
55
  const token = useToken();
56
- return (file, type, data) => {
56
+ return (file, type, config) => {
57
57
  const formData = new FormData();
58
58
  formData.append("type", type);
59
59
  formData.append("file", file);
60
- if (data) formData.append("data", JSON.stringify(data));
60
+ if (config?.async) formData.append("async", "true");
61
+ if (config?.data) formData.append("data", JSON.stringify(config.data));
61
62
  return fetch(getEnv("NEXT_PUBLIC_FILE_ENDPOINT" /* NEXT_PUBLIC_FILE_ENDPOINT */), {
62
63
  method: "PUT",
63
64
  body: formData,
@@ -20,11 +20,16 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/file/index.ts
21
21
  var file_exports = {};
22
22
  __export(file_exports, {
23
- setupFileUpload: () => setupFileUpload
23
+ deleteFile: () => deleteFile,
24
+ getDownloadURL: () => getDownloadURL,
25
+ getUploadURL: () => getUploadURL,
26
+ setupFileUpload: () => setupFileUpload,
27
+ uploadFile: () => uploadFile
24
28
  });
25
29
  module.exports = __toCommonJS(file_exports);
26
30
 
27
31
  // src/file/put.ts
32
+ var import_functions = require("@vercel/functions");
28
33
  var import_server3 = require("next/server");
29
34
  var import_uuid = require("uuid");
30
35
 
@@ -133,7 +138,7 @@ var getContext = (req) => {
133
138
  // src/file/utils.ts
134
139
  var import_client_s3 = require("@aws-sdk/client-s3");
135
140
  var import_s3_request_presigner = require("@aws-sdk/s3-request-presigner");
136
- var getS3Client = () => new import_client_s3.S3Client({
141
+ var client = new import_client_s3.S3Client({
137
142
  region: getEnv("AWS_REGION" /* AWS_REGION */),
138
143
  credentials: {
139
144
  accessKeyId: getEnv("AWS_ACCESS_KEY_ID" /* AWS_ACCESS_KEY_ID */),
@@ -146,7 +151,8 @@ var URL_PREFIX = `https://${getEnv("AWS_BUCKET" /* AWS_BUCKET */)}.s3.${getEnv(
146
151
  function getKey(keys) {
147
152
  return typeof keys === "string" ? keys : keys.join("/");
148
153
  }
149
- var getUploadURL = (client) => (keys) => {
154
+ var getUploadURL = (keys) => {
155
+ if (!checkClient(client)) return;
150
156
  const command = new import_client_s3.PutObjectCommand({
151
157
  Bucket: getEnv("AWS_BUCKET" /* AWS_BUCKET */),
152
158
  Key: getKey(keys),
@@ -157,21 +163,23 @@ var getUploadURL = (client) => (keys) => {
157
163
  var getDownloadURL = (keys) => {
158
164
  return `${URL_PREFIX}${getKey(keys)}`;
159
165
  };
160
- var uploadFile = (client) => async (keys, {
166
+ var uploadFile = async (keys, {
161
167
  url,
162
168
  blob
163
169
  }) => {
170
+ if (!checkClient(client)) return;
164
171
  if (!blob && !url) return null;
165
172
  const fileBlob = blob || await fetch(url).then((file) => file.blob());
166
173
  if (fileBlob) {
167
174
  const key = getKey(keys);
168
- await uploadBlob(client)(fileBlob, key);
175
+ await uploadBlob(fileBlob, key);
169
176
  return getDownloadURL(key);
170
177
  }
171
178
  return null;
172
179
  };
173
- var deleteFile = (client) => async (url) => {
180
+ var deleteFile = async (url) => {
174
181
  const key = url.split(URL_PREFIX)[1];
182
+ if (!checkClient(client)) return;
175
183
  if (key) {
176
184
  try {
177
185
  await client.send(
@@ -187,7 +195,12 @@ var deleteFile = (client) => async (url) => {
187
195
  }
188
196
  return false;
189
197
  };
190
- var uploadBlob = (client) => async (file, key) => {
198
+ function checkClient(client2) {
199
+ if (!client2) throw new Error("Client does not exist");
200
+ return true;
201
+ }
202
+ var uploadBlob = async (file, key) => {
203
+ if (!checkClient(client)) return;
191
204
  const fileBuffer = await file.arrayBuffer();
192
205
  return client.send(
193
206
  new import_client_s3.PutObjectCommand({
@@ -202,7 +215,7 @@ var uploadBlob = (client) => async (file, key) => {
202
215
  };
203
216
 
204
217
  // src/file/put.ts
205
- var getFileUploadPutRoute = (options, client) => async (req) => {
218
+ var getFileUploadPutRoute = (options) => async (req) => {
206
219
  const ctx = getContext(req);
207
220
  if (!ctx?.userId || ctx.isRefreshID)
208
221
  return import_server3.NextResponse.json({ error: "unauthorized" }, { status: 401 });
@@ -210,6 +223,7 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
210
223
  const file = formData.get("file");
211
224
  if (!file) return import_server3.NextResponse.json({ error: "no file" }, { status: 400 });
212
225
  const data = formData.get("data");
226
+ const async = formData.get("async");
213
227
  const inputData = {
214
228
  type: formData.get("type") + "",
215
229
  userId: ctx.userId,
@@ -217,7 +231,8 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
217
231
  };
218
232
  const fileKey = options.getKey ? await options.getKey(inputData) : (0, import_uuid.v4)();
219
233
  const url = getDownloadURL(fileKey);
220
- await uploadBlob(client)(file, fileKey);
234
+ if (async) (0, import_functions.waitUntil)(uploadBlob(file, fileKey));
235
+ else await uploadBlob(file, fileKey);
221
236
  const onUploadResponse = await options.onUpload({
222
237
  ...inputData,
223
238
  url
@@ -230,16 +245,15 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
230
245
 
231
246
  // src/file/setup.ts
232
247
  function setupFileUpload(options) {
233
- const client = getS3Client();
234
248
  return {
235
- PUT: getFileUploadPutRoute(options, client),
236
- uploadFile: uploadFile(client),
237
- deleteFile: deleteFile(client),
238
- getUploadURL: getUploadURL(client),
239
- getDownloadURL
249
+ PUT: getFileUploadPutRoute(options)
240
250
  };
241
251
  }
242
252
  // Annotate the CommonJS export names for ESM import in node:
243
253
  0 && (module.exports = {
244
- setupFileUpload
254
+ deleteFile,
255
+ getDownloadURL,
256
+ getUploadURL,
257
+ setupFileUpload,
258
+ uploadFile
245
259
  });
@@ -1,2 +1,4 @@
1
1
  export { setupFileUpload } from './setup.mjs';
2
+ export { deleteFile, getDownloadURL, getUploadURL, uploadFile } from './utils.mjs';
2
3
  import 'next/server';
4
+ import '@aws-sdk/client-s3';
@@ -1,2 +1,4 @@
1
1
  export { setupFileUpload } from './setup.js';
2
+ export { deleteFile, getDownloadURL, getUploadURL, uploadFile } from './utils.js';
2
3
  import 'next/server';
4
+ import '@aws-sdk/client-s3';
@@ -1,4 +1,5 @@
1
1
  // src/file/put.ts
2
+ import { waitUntil } from "@vercel/functions";
2
3
  import { NextResponse as NextResponse3 } from "next/server";
3
4
  import { v4 } from "uuid";
4
5
 
@@ -111,7 +112,7 @@ import {
111
112
  S3Client
112
113
  } from "@aws-sdk/client-s3";
113
114
  import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
114
- var getS3Client = () => new S3Client({
115
+ var client = new S3Client({
115
116
  region: getEnv("AWS_REGION" /* AWS_REGION */),
116
117
  credentials: {
117
118
  accessKeyId: getEnv("AWS_ACCESS_KEY_ID" /* AWS_ACCESS_KEY_ID */),
@@ -124,7 +125,8 @@ var URL_PREFIX = `https://${getEnv("AWS_BUCKET" /* AWS_BUCKET */)}.s3.${getEnv(
124
125
  function getKey(keys) {
125
126
  return typeof keys === "string" ? keys : keys.join("/");
126
127
  }
127
- var getUploadURL = (client) => (keys) => {
128
+ var getUploadURL = (keys) => {
129
+ if (!checkClient(client)) return;
128
130
  const command = new PutObjectCommand({
129
131
  Bucket: getEnv("AWS_BUCKET" /* AWS_BUCKET */),
130
132
  Key: getKey(keys),
@@ -135,21 +137,23 @@ var getUploadURL = (client) => (keys) => {
135
137
  var getDownloadURL = (keys) => {
136
138
  return `${URL_PREFIX}${getKey(keys)}`;
137
139
  };
138
- var uploadFile = (client) => async (keys, {
140
+ var uploadFile = async (keys, {
139
141
  url,
140
142
  blob
141
143
  }) => {
144
+ if (!checkClient(client)) return;
142
145
  if (!blob && !url) return null;
143
146
  const fileBlob = blob || await fetch(url).then((file) => file.blob());
144
147
  if (fileBlob) {
145
148
  const key = getKey(keys);
146
- await uploadBlob(client)(fileBlob, key);
149
+ await uploadBlob(fileBlob, key);
147
150
  return getDownloadURL(key);
148
151
  }
149
152
  return null;
150
153
  };
151
- var deleteFile = (client) => async (url) => {
154
+ var deleteFile = async (url) => {
152
155
  const key = url.split(URL_PREFIX)[1];
156
+ if (!checkClient(client)) return;
153
157
  if (key) {
154
158
  try {
155
159
  await client.send(
@@ -165,7 +169,12 @@ var deleteFile = (client) => async (url) => {
165
169
  }
166
170
  return false;
167
171
  };
168
- var uploadBlob = (client) => async (file, key) => {
172
+ function checkClient(client2) {
173
+ if (!client2) throw new Error("Client does not exist");
174
+ return true;
175
+ }
176
+ var uploadBlob = async (file, key) => {
177
+ if (!checkClient(client)) return;
169
178
  const fileBuffer = await file.arrayBuffer();
170
179
  return client.send(
171
180
  new PutObjectCommand({
@@ -180,7 +189,7 @@ var uploadBlob = (client) => async (file, key) => {
180
189
  };
181
190
 
182
191
  // src/file/put.ts
183
- var getFileUploadPutRoute = (options, client) => async (req) => {
192
+ var getFileUploadPutRoute = (options) => async (req) => {
184
193
  const ctx = getContext(req);
185
194
  if (!ctx?.userId || ctx.isRefreshID)
186
195
  return NextResponse3.json({ error: "unauthorized" }, { status: 401 });
@@ -188,6 +197,7 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
188
197
  const file = formData.get("file");
189
198
  if (!file) return NextResponse3.json({ error: "no file" }, { status: 400 });
190
199
  const data = formData.get("data");
200
+ const async = formData.get("async");
191
201
  const inputData = {
192
202
  type: formData.get("type") + "",
193
203
  userId: ctx.userId,
@@ -195,7 +205,8 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
195
205
  };
196
206
  const fileKey = options.getKey ? await options.getKey(inputData) : v4();
197
207
  const url = getDownloadURL(fileKey);
198
- await uploadBlob(client)(file, fileKey);
208
+ if (async) waitUntil(uploadBlob(file, fileKey));
209
+ else await uploadBlob(file, fileKey);
199
210
  const onUploadResponse = await options.onUpload({
200
211
  ...inputData,
201
212
  url
@@ -208,15 +219,14 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
208
219
 
209
220
  // src/file/setup.ts
210
221
  function setupFileUpload(options) {
211
- const client = getS3Client();
212
222
  return {
213
- PUT: getFileUploadPutRoute(options, client),
214
- uploadFile: uploadFile(client),
215
- deleteFile: deleteFile(client),
216
- getUploadURL: getUploadURL(client),
217
- getDownloadURL
223
+ PUT: getFileUploadPutRoute(options)
218
224
  };
219
225
  }
220
226
  export {
221
- setupFileUpload
227
+ deleteFile,
228
+ getDownloadURL,
229
+ getUploadURL,
230
+ setupFileUpload,
231
+ uploadFile
222
232
  };
@@ -23,6 +23,7 @@ __export(put_exports, {
23
23
  getFileUploadPutRoute: () => getFileUploadPutRoute
24
24
  });
25
25
  module.exports = __toCommonJS(put_exports);
26
+ var import_functions = require("@vercel/functions");
26
27
  var import_server3 = require("next/server");
27
28
  var import_uuid = require("uuid");
28
29
 
@@ -131,6 +132,13 @@ var getContext = (req) => {
131
132
  // src/file/utils.ts
132
133
  var import_client_s3 = require("@aws-sdk/client-s3");
133
134
  var import_s3_request_presigner = require("@aws-sdk/s3-request-presigner");
135
+ var client = new import_client_s3.S3Client({
136
+ region: getEnv("AWS_REGION" /* AWS_REGION */),
137
+ credentials: {
138
+ accessKeyId: getEnv("AWS_ACCESS_KEY_ID" /* AWS_ACCESS_KEY_ID */),
139
+ secretAccessKey: getEnv("AWS_ACCESS_KEY_SECRET" /* AWS_ACCESS_KEY_SECRET */)
140
+ }
141
+ });
134
142
  var URL_PREFIX = `https://${getEnv("AWS_BUCKET" /* AWS_BUCKET */)}.s3.${getEnv(
135
143
  "AWS_REGION" /* AWS_REGION */
136
144
  )}.amazonaws.com/`;
@@ -140,7 +148,12 @@ function getKey(keys) {
140
148
  var getDownloadURL = (keys) => {
141
149
  return `${URL_PREFIX}${getKey(keys)}`;
142
150
  };
143
- var uploadBlob = (client) => async (file, key) => {
151
+ function checkClient(client2) {
152
+ if (!client2) throw new Error("Client does not exist");
153
+ return true;
154
+ }
155
+ var uploadBlob = async (file, key) => {
156
+ if (!checkClient(client)) return;
144
157
  const fileBuffer = await file.arrayBuffer();
145
158
  return client.send(
146
159
  new import_client_s3.PutObjectCommand({
@@ -155,7 +168,7 @@ var uploadBlob = (client) => async (file, key) => {
155
168
  };
156
169
 
157
170
  // src/file/put.ts
158
- var getFileUploadPutRoute = (options, client) => async (req) => {
171
+ var getFileUploadPutRoute = (options) => async (req) => {
159
172
  const ctx = getContext(req);
160
173
  if (!ctx?.userId || ctx.isRefreshID)
161
174
  return import_server3.NextResponse.json({ error: "unauthorized" }, { status: 401 });
@@ -163,6 +176,7 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
163
176
  const file = formData.get("file");
164
177
  if (!file) return import_server3.NextResponse.json({ error: "no file" }, { status: 400 });
165
178
  const data = formData.get("data");
179
+ const async = formData.get("async");
166
180
  const inputData = {
167
181
  type: formData.get("type") + "",
168
182
  userId: ctx.userId,
@@ -170,7 +184,8 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
170
184
  };
171
185
  const fileKey = options.getKey ? await options.getKey(inputData) : (0, import_uuid.v4)();
172
186
  const url = getDownloadURL(fileKey);
173
- await uploadBlob(client)(file, fileKey);
187
+ if (async) (0, import_functions.waitUntil)(uploadBlob(file, fileKey));
188
+ else await uploadBlob(file, fileKey);
174
189
  const onUploadResponse = await options.onUpload({
175
190
  ...inputData,
176
191
  url
@@ -1,4 +1,3 @@
1
- import { S3Client } from '@aws-sdk/client-s3';
2
1
  import { NextRequest, NextResponse } from 'next/server';
3
2
  import { SetupFileUploadOptions } from './setup.mjs';
4
3
 
@@ -9,11 +8,10 @@ import { SetupFileUploadOptions } from './setup.mjs';
9
8
  * Expects multipart form data with fields: `file` (File), `type` (string), and optional `data` (JSON string).
10
9
  *
11
10
  * @param options - `SetupFileUploadOptions` (getKey, onUpload).
12
- * @param client - S3 client instance.
13
11
  * @returns Async Next.js route handler for PUT requests.
14
12
  * @category File
15
13
  */
16
- declare const getFileUploadPutRoute: (options: SetupFileUploadOptions, client: S3Client) => (req: NextRequest) => Promise<NextResponse<{
14
+ declare const getFileUploadPutRoute: (options: SetupFileUploadOptions) => (req: NextRequest) => Promise<NextResponse<{
17
15
  error: string;
18
16
  }> | NextResponse<{
19
17
  url: string;
@@ -1,4 +1,3 @@
1
- import { S3Client } from '@aws-sdk/client-s3';
2
1
  import { NextRequest, NextResponse } from 'next/server';
3
2
  import { SetupFileUploadOptions } from './setup.js';
4
3
 
@@ -9,11 +8,10 @@ import { SetupFileUploadOptions } from './setup.js';
9
8
  * Expects multipart form data with fields: `file` (File), `type` (string), and optional `data` (JSON string).
10
9
  *
11
10
  * @param options - `SetupFileUploadOptions` (getKey, onUpload).
12
- * @param client - S3 client instance.
13
11
  * @returns Async Next.js route handler for PUT requests.
14
12
  * @category File
15
13
  */
16
- declare const getFileUploadPutRoute: (options: SetupFileUploadOptions, client: S3Client) => (req: NextRequest) => Promise<NextResponse<{
14
+ declare const getFileUploadPutRoute: (options: SetupFileUploadOptions) => (req: NextRequest) => Promise<NextResponse<{
17
15
  error: string;
18
16
  }> | NextResponse<{
19
17
  url: string;
@@ -1,4 +1,5 @@
1
1
  // src/file/put.ts
2
+ import { waitUntil } from "@vercel/functions";
2
3
  import { NextResponse as NextResponse3 } from "next/server";
3
4
  import { v4 } from "uuid";
4
5
 
@@ -111,6 +112,13 @@ import {
111
112
  S3Client
112
113
  } from "@aws-sdk/client-s3";
113
114
  import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
115
+ var client = new S3Client({
116
+ region: getEnv("AWS_REGION" /* AWS_REGION */),
117
+ credentials: {
118
+ accessKeyId: getEnv("AWS_ACCESS_KEY_ID" /* AWS_ACCESS_KEY_ID */),
119
+ secretAccessKey: getEnv("AWS_ACCESS_KEY_SECRET" /* AWS_ACCESS_KEY_SECRET */)
120
+ }
121
+ });
114
122
  var URL_PREFIX = `https://${getEnv("AWS_BUCKET" /* AWS_BUCKET */)}.s3.${getEnv(
115
123
  "AWS_REGION" /* AWS_REGION */
116
124
  )}.amazonaws.com/`;
@@ -120,7 +128,12 @@ function getKey(keys) {
120
128
  var getDownloadURL = (keys) => {
121
129
  return `${URL_PREFIX}${getKey(keys)}`;
122
130
  };
123
- var uploadBlob = (client) => async (file, key) => {
131
+ function checkClient(client2) {
132
+ if (!client2) throw new Error("Client does not exist");
133
+ return true;
134
+ }
135
+ var uploadBlob = async (file, key) => {
136
+ if (!checkClient(client)) return;
124
137
  const fileBuffer = await file.arrayBuffer();
125
138
  return client.send(
126
139
  new PutObjectCommand({
@@ -135,7 +148,7 @@ var uploadBlob = (client) => async (file, key) => {
135
148
  };
136
149
 
137
150
  // src/file/put.ts
138
- var getFileUploadPutRoute = (options, client) => async (req) => {
151
+ var getFileUploadPutRoute = (options) => async (req) => {
139
152
  const ctx = getContext(req);
140
153
  if (!ctx?.userId || ctx.isRefreshID)
141
154
  return NextResponse3.json({ error: "unauthorized" }, { status: 401 });
@@ -143,6 +156,7 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
143
156
  const file = formData.get("file");
144
157
  if (!file) return NextResponse3.json({ error: "no file" }, { status: 400 });
145
158
  const data = formData.get("data");
159
+ const async = formData.get("async");
146
160
  const inputData = {
147
161
  type: formData.get("type") + "",
148
162
  userId: ctx.userId,
@@ -150,7 +164,8 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
150
164
  };
151
165
  const fileKey = options.getKey ? await options.getKey(inputData) : v4();
152
166
  const url = getDownloadURL(fileKey);
153
- await uploadBlob(client)(file, fileKey);
167
+ if (async) waitUntil(uploadBlob(file, fileKey));
168
+ else await uploadBlob(file, fileKey);
154
169
  const onUploadResponse = await options.onUpload({
155
170
  ...inputData,
156
171
  url
@@ -25,6 +25,7 @@ __export(setup_exports, {
25
25
  module.exports = __toCommonJS(setup_exports);
26
26
 
27
27
  // src/file/put.ts
28
+ var import_functions = require("@vercel/functions");
28
29
  var import_server3 = require("next/server");
29
30
  var import_uuid = require("uuid");
30
31
 
@@ -133,7 +134,7 @@ var getContext = (req) => {
133
134
  // src/file/utils.ts
134
135
  var import_client_s3 = require("@aws-sdk/client-s3");
135
136
  var import_s3_request_presigner = require("@aws-sdk/s3-request-presigner");
136
- var getS3Client = () => new import_client_s3.S3Client({
137
+ var client = new import_client_s3.S3Client({
137
138
  region: getEnv("AWS_REGION" /* AWS_REGION */),
138
139
  credentials: {
139
140
  accessKeyId: getEnv("AWS_ACCESS_KEY_ID" /* AWS_ACCESS_KEY_ID */),
@@ -146,48 +147,15 @@ var URL_PREFIX = `https://${getEnv("AWS_BUCKET" /* AWS_BUCKET */)}.s3.${getEnv(
146
147
  function getKey(keys) {
147
148
  return typeof keys === "string" ? keys : keys.join("/");
148
149
  }
149
- var getUploadURL = (client) => (keys) => {
150
- const command = new import_client_s3.PutObjectCommand({
151
- Bucket: getEnv("AWS_BUCKET" /* AWS_BUCKET */),
152
- Key: getKey(keys),
153
- ACL: "public-read"
154
- });
155
- return (0, import_s3_request_presigner.getSignedUrl)(client, command, { expiresIn: 300 });
156
- };
157
150
  var getDownloadURL = (keys) => {
158
151
  return `${URL_PREFIX}${getKey(keys)}`;
159
152
  };
160
- var uploadFile = (client) => async (keys, {
161
- url,
162
- blob
163
- }) => {
164
- if (!blob && !url) return null;
165
- const fileBlob = blob || await fetch(url).then((file) => file.blob());
166
- if (fileBlob) {
167
- const key = getKey(keys);
168
- await uploadBlob(client)(fileBlob, key);
169
- return getDownloadURL(key);
170
- }
171
- return null;
172
- };
173
- var deleteFile = (client) => async (url) => {
174
- const key = url.split(URL_PREFIX)[1];
175
- if (key) {
176
- try {
177
- await client.send(
178
- new import_client_s3.DeleteObjectCommand({
179
- Bucket: getEnv("AWS_BUCKET" /* AWS_BUCKET */),
180
- Key: key
181
- })
182
- );
183
- return true;
184
- } catch (e) {
185
- console.error("ERROR", url, e);
186
- }
187
- }
188
- return false;
189
- };
190
- var uploadBlob = (client) => async (file, key) => {
153
+ function checkClient(client2) {
154
+ if (!client2) throw new Error("Client does not exist");
155
+ return true;
156
+ }
157
+ var uploadBlob = async (file, key) => {
158
+ if (!checkClient(client)) return;
191
159
  const fileBuffer = await file.arrayBuffer();
192
160
  return client.send(
193
161
  new import_client_s3.PutObjectCommand({
@@ -202,7 +170,7 @@ var uploadBlob = (client) => async (file, key) => {
202
170
  };
203
171
 
204
172
  // src/file/put.ts
205
- var getFileUploadPutRoute = (options, client) => async (req) => {
173
+ var getFileUploadPutRoute = (options) => async (req) => {
206
174
  const ctx = getContext(req);
207
175
  if (!ctx?.userId || ctx.isRefreshID)
208
176
  return import_server3.NextResponse.json({ error: "unauthorized" }, { status: 401 });
@@ -210,6 +178,7 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
210
178
  const file = formData.get("file");
211
179
  if (!file) return import_server3.NextResponse.json({ error: "no file" }, { status: 400 });
212
180
  const data = formData.get("data");
181
+ const async = formData.get("async");
213
182
  const inputData = {
214
183
  type: formData.get("type") + "",
215
184
  userId: ctx.userId,
@@ -217,7 +186,8 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
217
186
  };
218
187
  const fileKey = options.getKey ? await options.getKey(inputData) : (0, import_uuid.v4)();
219
188
  const url = getDownloadURL(fileKey);
220
- await uploadBlob(client)(file, fileKey);
189
+ if (async) (0, import_functions.waitUntil)(uploadBlob(file, fileKey));
190
+ else await uploadBlob(file, fileKey);
221
191
  const onUploadResponse = await options.onUpload({
222
192
  ...inputData,
223
193
  url
@@ -230,13 +200,8 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
230
200
 
231
201
  // src/file/setup.ts
232
202
  function setupFileUpload(options) {
233
- const client = getS3Client();
234
203
  return {
235
- PUT: getFileUploadPutRoute(options, client),
236
- uploadFile: uploadFile(client),
237
- deleteFile: deleteFile(client),
238
- getUploadURL: getUploadURL(client),
239
- getDownloadURL
204
+ PUT: getFileUploadPutRoute(options)
240
205
  };
241
206
  }
242
207
  // Annotate the CommonJS export names for ESM import in node:
@@ -68,13 +68,6 @@ declare function setupFileUpload(options: SetupFileUploadOptions): {
68
68
  url: string;
69
69
  onUploadResponse: object;
70
70
  }>>;
71
- uploadFile: (keys: string | string[], { url, blob, }: {
72
- blob?: Blob;
73
- url?: string;
74
- }) => Promise<string | null>;
75
- deleteFile: (url: string) => Promise<boolean>;
76
- getUploadURL: (keys: string | string[]) => Promise<string>;
77
- getDownloadURL: (keys: string | string[]) => string;
78
71
  };
79
72
 
80
73
  export { type SetupFileUploadOptions, setupFileUpload };
@@ -68,13 +68,6 @@ declare function setupFileUpload(options: SetupFileUploadOptions): {
68
68
  url: string;
69
69
  onUploadResponse: object;
70
70
  }>>;
71
- uploadFile: (keys: string | string[], { url, blob, }: {
72
- blob?: Blob;
73
- url?: string;
74
- }) => Promise<string | null>;
75
- deleteFile: (url: string) => Promise<boolean>;
76
- getUploadURL: (keys: string | string[]) => Promise<string>;
77
- getDownloadURL: (keys: string | string[]) => string;
78
71
  };
79
72
 
80
73
  export { type SetupFileUploadOptions, setupFileUpload };
@@ -1,4 +1,5 @@
1
1
  // src/file/put.ts
2
+ import { waitUntil } from "@vercel/functions";
2
3
  import { NextResponse as NextResponse3 } from "next/server";
3
4
  import { v4 } from "uuid";
4
5
 
@@ -111,7 +112,7 @@ import {
111
112
  S3Client
112
113
  } from "@aws-sdk/client-s3";
113
114
  import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
114
- var getS3Client = () => new S3Client({
115
+ var client = new S3Client({
115
116
  region: getEnv("AWS_REGION" /* AWS_REGION */),
116
117
  credentials: {
117
118
  accessKeyId: getEnv("AWS_ACCESS_KEY_ID" /* AWS_ACCESS_KEY_ID */),
@@ -124,48 +125,15 @@ var URL_PREFIX = `https://${getEnv("AWS_BUCKET" /* AWS_BUCKET */)}.s3.${getEnv(
124
125
  function getKey(keys) {
125
126
  return typeof keys === "string" ? keys : keys.join("/");
126
127
  }
127
- var getUploadURL = (client) => (keys) => {
128
- const command = new PutObjectCommand({
129
- Bucket: getEnv("AWS_BUCKET" /* AWS_BUCKET */),
130
- Key: getKey(keys),
131
- ACL: "public-read"
132
- });
133
- return getSignedUrl(client, command, { expiresIn: 300 });
134
- };
135
128
  var getDownloadURL = (keys) => {
136
129
  return `${URL_PREFIX}${getKey(keys)}`;
137
130
  };
138
- var uploadFile = (client) => async (keys, {
139
- url,
140
- blob
141
- }) => {
142
- if (!blob && !url) return null;
143
- const fileBlob = blob || await fetch(url).then((file) => file.blob());
144
- if (fileBlob) {
145
- const key = getKey(keys);
146
- await uploadBlob(client)(fileBlob, key);
147
- return getDownloadURL(key);
148
- }
149
- return null;
150
- };
151
- var deleteFile = (client) => async (url) => {
152
- const key = url.split(URL_PREFIX)[1];
153
- if (key) {
154
- try {
155
- await client.send(
156
- new DeleteObjectCommand({
157
- Bucket: getEnv("AWS_BUCKET" /* AWS_BUCKET */),
158
- Key: key
159
- })
160
- );
161
- return true;
162
- } catch (e) {
163
- console.error("ERROR", url, e);
164
- }
165
- }
166
- return false;
167
- };
168
- var uploadBlob = (client) => async (file, key) => {
131
+ function checkClient(client2) {
132
+ if (!client2) throw new Error("Client does not exist");
133
+ return true;
134
+ }
135
+ var uploadBlob = async (file, key) => {
136
+ if (!checkClient(client)) return;
169
137
  const fileBuffer = await file.arrayBuffer();
170
138
  return client.send(
171
139
  new PutObjectCommand({
@@ -180,7 +148,7 @@ var uploadBlob = (client) => async (file, key) => {
180
148
  };
181
149
 
182
150
  // src/file/put.ts
183
- var getFileUploadPutRoute = (options, client) => async (req) => {
151
+ var getFileUploadPutRoute = (options) => async (req) => {
184
152
  const ctx = getContext(req);
185
153
  if (!ctx?.userId || ctx.isRefreshID)
186
154
  return NextResponse3.json({ error: "unauthorized" }, { status: 401 });
@@ -188,6 +156,7 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
188
156
  const file = formData.get("file");
189
157
  if (!file) return NextResponse3.json({ error: "no file" }, { status: 400 });
190
158
  const data = formData.get("data");
159
+ const async = formData.get("async");
191
160
  const inputData = {
192
161
  type: formData.get("type") + "",
193
162
  userId: ctx.userId,
@@ -195,7 +164,8 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
195
164
  };
196
165
  const fileKey = options.getKey ? await options.getKey(inputData) : v4();
197
166
  const url = getDownloadURL(fileKey);
198
- await uploadBlob(client)(file, fileKey);
167
+ if (async) waitUntil(uploadBlob(file, fileKey));
168
+ else await uploadBlob(file, fileKey);
199
169
  const onUploadResponse = await options.onUpload({
200
170
  ...inputData,
201
171
  url
@@ -208,13 +178,8 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
208
178
 
209
179
  // src/file/setup.ts
210
180
  function setupFileUpload(options) {
211
- const client = getS3Client();
212
181
  return {
213
- PUT: getFileUploadPutRoute(options, client),
214
- uploadFile: uploadFile(client),
215
- deleteFile: deleteFile(client),
216
- getUploadURL: getUploadURL(client),
217
- getDownloadURL
182
+ PUT: getFileUploadPutRoute(options)
218
183
  };
219
184
  }
220
185
  export {
@@ -22,7 +22,6 @@ var utils_exports = {};
22
22
  __export(utils_exports, {
23
23
  deleteFile: () => deleteFile,
24
24
  getDownloadURL: () => getDownloadURL,
25
- getS3Client: () => getS3Client,
26
25
  getUploadURL: () => getUploadURL,
27
26
  uploadBlob: () => uploadBlob,
28
27
  uploadFile: () => uploadFile
@@ -81,7 +80,7 @@ function getEnv(key, skipCheck) {
81
80
  }
82
81
 
83
82
  // src/file/utils.ts
84
- var getS3Client = () => new import_client_s3.S3Client({
83
+ var client = new import_client_s3.S3Client({
85
84
  region: getEnv("AWS_REGION" /* AWS_REGION */),
86
85
  credentials: {
87
86
  accessKeyId: getEnv("AWS_ACCESS_KEY_ID" /* AWS_ACCESS_KEY_ID */),
@@ -94,7 +93,8 @@ var URL_PREFIX = `https://${getEnv("AWS_BUCKET" /* AWS_BUCKET */)}.s3.${getEnv(
94
93
  function getKey(keys) {
95
94
  return typeof keys === "string" ? keys : keys.join("/");
96
95
  }
97
- var getUploadURL = (client) => (keys) => {
96
+ var getUploadURL = (keys) => {
97
+ if (!checkClient(client)) return;
98
98
  const command = new import_client_s3.PutObjectCommand({
99
99
  Bucket: getEnv("AWS_BUCKET" /* AWS_BUCKET */),
100
100
  Key: getKey(keys),
@@ -105,21 +105,23 @@ var getUploadURL = (client) => (keys) => {
105
105
  var getDownloadURL = (keys) => {
106
106
  return `${URL_PREFIX}${getKey(keys)}`;
107
107
  };
108
- var uploadFile = (client) => async (keys, {
108
+ var uploadFile = async (keys, {
109
109
  url,
110
110
  blob
111
111
  }) => {
112
+ if (!checkClient(client)) return;
112
113
  if (!blob && !url) return null;
113
114
  const fileBlob = blob || await fetch(url).then((file) => file.blob());
114
115
  if (fileBlob) {
115
116
  const key = getKey(keys);
116
- await uploadBlob(client)(fileBlob, key);
117
+ await uploadBlob(fileBlob, key);
117
118
  return getDownloadURL(key);
118
119
  }
119
120
  return null;
120
121
  };
121
- var deleteFile = (client) => async (url) => {
122
+ var deleteFile = async (url) => {
122
123
  const key = url.split(URL_PREFIX)[1];
124
+ if (!checkClient(client)) return;
123
125
  if (key) {
124
126
  try {
125
127
  await client.send(
@@ -135,7 +137,12 @@ var deleteFile = (client) => async (url) => {
135
137
  }
136
138
  return false;
137
139
  };
138
- var uploadBlob = (client) => async (file, key) => {
140
+ function checkClient(client2) {
141
+ if (!client2) throw new Error("Client does not exist");
142
+ return true;
143
+ }
144
+ var uploadBlob = async (file, key) => {
145
+ if (!checkClient(client)) return;
139
146
  const fileBuffer = await file.arrayBuffer();
140
147
  return client.send(
141
148
  new import_client_s3.PutObjectCommand({
@@ -152,7 +159,6 @@ var uploadBlob = (client) => async (file, key) => {
152
159
  0 && (module.exports = {
153
160
  deleteFile,
154
161
  getDownloadURL,
155
- getS3Client,
156
162
  getUploadURL,
157
163
  uploadBlob,
158
164
  uploadFile
@@ -1,21 +1,14 @@
1
1
  import * as _aws_sdk_client_s3 from '@aws-sdk/client-s3';
2
- import { S3Client } from '@aws-sdk/client-s3';
3
2
 
4
3
  /**
5
- * Creates an S3 client using env credentials (`AWS_ACCESS_KEY_ID`, `AWS_ACCESS_KEY_SECRET`, `AWS_REGION`).
6
- * @returns Configured `S3Client` instance.
7
- * @category File
8
- */
9
- declare const getS3Client: () => S3Client;
10
- /**
11
- * Returns a function that generates a presigned PUT URL for uploading to the given S3 key(s).
12
- * The presigned URL expires after 5 minutes.
4
+ * Generates a presigned PUT URL for uploading a file to the given S3 key(s).
5
+ * The URL expires after 5 minutes.
13
6
  *
14
- * @param client - S3 client instance.
15
- * @returns `(keys: string | string[]) => Promise<string>` — the presigned upload URL.
7
+ * @param keys - S3 key or key path segments (array joined by `/`).
8
+ * @returns A presigned upload URL.
16
9
  * @category File
17
10
  */
18
- declare const getUploadURL: (client: S3Client) => (keys: string | string[]) => Promise<string>;
11
+ declare const getUploadURL: (keys: string | string[]) => Promise<string> | undefined;
19
12
  /**
20
13
  * Builds the public download URL for one or more keys in the configured S3 bucket.
21
14
  *
@@ -25,31 +18,34 @@ declare const getUploadURL: (client: S3Client) => (keys: string | string[]) => P
25
18
  */
26
19
  declare const getDownloadURL: (keys: string | string[]) => string;
27
20
  /**
28
- * Returns a function that uploads a file (by URL or Blob) to the given S3 key(s).
21
+ * Uploads a file to S3 at the given key(s), either from a Blob or a remote URL.
29
22
  *
30
- * @param client - S3 client instance.
31
- * @returns `(keys, { url?, blob? }) => Promise<string | null>` — the download URL on success, or `null` if no input.
23
+ * @param keys - S3 key or key path segments (array joined by `/`).
24
+ * @param options.blob - A Blob/File to upload directly.
25
+ * @param options.url - A remote URL to fetch and upload. Ignored if `blob` is provided.
26
+ * @returns The public download URL of the uploaded file, or `null` if neither `blob` nor `url` was provided.
32
27
  * @category File
33
28
  */
34
- declare const uploadFile: (client: S3Client) => (keys: string | string[], { url, blob, }: {
29
+ declare const uploadFile: (keys: string | string[], { url, blob, }: {
35
30
  blob?: Blob;
36
31
  url?: string;
37
- }) => Promise<string | null>;
32
+ }) => Promise<string | null | undefined>;
38
33
  /**
39
- * Returns a function that deletes an S3 object by its full URL.
34
+ * Deletes an S3 object identified by its full public URL.
40
35
  *
41
- * @param client - S3 client instance.
42
- * @returns `(url: string) => Promise<boolean>` `true` if deleted successfully, `false` otherwise.
36
+ * @param url - The full public URL of the S3 object to delete.
37
+ * @returns `true` if the object was deleted successfully, `false` otherwise.
43
38
  * @category File
44
39
  */
45
- declare const deleteFile: (client: S3Client) => (url: string) => Promise<boolean>;
40
+ declare const deleteFile: (url: string) => Promise<boolean | undefined>;
46
41
  /**
47
- * Returns a function that uploads a Blob/File to S3 at the given key.
42
+ * Uploads a Blob or File to S3 at the given key with public-read ACL.
48
43
  *
49
- * @param client - S3 client instance.
50
- * @returns `(file: File | Blob, key: string) => Promise<PutObjectCommandOutput>`.
44
+ * @param file - The Blob or File to upload.
45
+ * @param key - The S3 object key.
46
+ * @returns The `PutObjectCommandOutput` from S3.
51
47
  * @category File
52
48
  */
53
- declare const uploadBlob: (client: S3Client) => (file: File | Blob, key: string) => Promise<_aws_sdk_client_s3.PutObjectCommandOutput>;
49
+ declare const uploadBlob: (file: File | Blob, key: string) => Promise<_aws_sdk_client_s3.PutObjectCommandOutput | undefined>;
54
50
 
55
- export { deleteFile, getDownloadURL, getS3Client, getUploadURL, uploadBlob, uploadFile };
51
+ export { deleteFile, getDownloadURL, getUploadURL, uploadBlob, uploadFile };
@@ -1,21 +1,14 @@
1
1
  import * as _aws_sdk_client_s3 from '@aws-sdk/client-s3';
2
- import { S3Client } from '@aws-sdk/client-s3';
3
2
 
4
3
  /**
5
- * Creates an S3 client using env credentials (`AWS_ACCESS_KEY_ID`, `AWS_ACCESS_KEY_SECRET`, `AWS_REGION`).
6
- * @returns Configured `S3Client` instance.
7
- * @category File
8
- */
9
- declare const getS3Client: () => S3Client;
10
- /**
11
- * Returns a function that generates a presigned PUT URL for uploading to the given S3 key(s).
12
- * The presigned URL expires after 5 minutes.
4
+ * Generates a presigned PUT URL for uploading a file to the given S3 key(s).
5
+ * The URL expires after 5 minutes.
13
6
  *
14
- * @param client - S3 client instance.
15
- * @returns `(keys: string | string[]) => Promise<string>` — the presigned upload URL.
7
+ * @param keys - S3 key or key path segments (array joined by `/`).
8
+ * @returns A presigned upload URL.
16
9
  * @category File
17
10
  */
18
- declare const getUploadURL: (client: S3Client) => (keys: string | string[]) => Promise<string>;
11
+ declare const getUploadURL: (keys: string | string[]) => Promise<string> | undefined;
19
12
  /**
20
13
  * Builds the public download URL for one or more keys in the configured S3 bucket.
21
14
  *
@@ -25,31 +18,34 @@ declare const getUploadURL: (client: S3Client) => (keys: string | string[]) => P
25
18
  */
26
19
  declare const getDownloadURL: (keys: string | string[]) => string;
27
20
  /**
28
- * Returns a function that uploads a file (by URL or Blob) to the given S3 key(s).
21
+ * Uploads a file to S3 at the given key(s), either from a Blob or a remote URL.
29
22
  *
30
- * @param client - S3 client instance.
31
- * @returns `(keys, { url?, blob? }) => Promise<string | null>` — the download URL on success, or `null` if no input.
23
+ * @param keys - S3 key or key path segments (array joined by `/`).
24
+ * @param options.blob - A Blob/File to upload directly.
25
+ * @param options.url - A remote URL to fetch and upload. Ignored if `blob` is provided.
26
+ * @returns The public download URL of the uploaded file, or `null` if neither `blob` nor `url` was provided.
32
27
  * @category File
33
28
  */
34
- declare const uploadFile: (client: S3Client) => (keys: string | string[], { url, blob, }: {
29
+ declare const uploadFile: (keys: string | string[], { url, blob, }: {
35
30
  blob?: Blob;
36
31
  url?: string;
37
- }) => Promise<string | null>;
32
+ }) => Promise<string | null | undefined>;
38
33
  /**
39
- * Returns a function that deletes an S3 object by its full URL.
34
+ * Deletes an S3 object identified by its full public URL.
40
35
  *
41
- * @param client - S3 client instance.
42
- * @returns `(url: string) => Promise<boolean>` `true` if deleted successfully, `false` otherwise.
36
+ * @param url - The full public URL of the S3 object to delete.
37
+ * @returns `true` if the object was deleted successfully, `false` otherwise.
43
38
  * @category File
44
39
  */
45
- declare const deleteFile: (client: S3Client) => (url: string) => Promise<boolean>;
40
+ declare const deleteFile: (url: string) => Promise<boolean | undefined>;
46
41
  /**
47
- * Returns a function that uploads a Blob/File to S3 at the given key.
42
+ * Uploads a Blob or File to S3 at the given key with public-read ACL.
48
43
  *
49
- * @param client - S3 client instance.
50
- * @returns `(file: File | Blob, key: string) => Promise<PutObjectCommandOutput>`.
44
+ * @param file - The Blob or File to upload.
45
+ * @param key - The S3 object key.
46
+ * @returns The `PutObjectCommandOutput` from S3.
51
47
  * @category File
52
48
  */
53
- declare const uploadBlob: (client: S3Client) => (file: File | Blob, key: string) => Promise<_aws_sdk_client_s3.PutObjectCommandOutput>;
49
+ declare const uploadBlob: (file: File | Blob, key: string) => Promise<_aws_sdk_client_s3.PutObjectCommandOutput | undefined>;
54
50
 
55
- export { deleteFile, getDownloadURL, getS3Client, getUploadURL, uploadBlob, uploadFile };
51
+ export { deleteFile, getDownloadURL, getUploadURL, uploadBlob, uploadFile };
@@ -56,7 +56,7 @@ function getEnv(key, skipCheck) {
56
56
  }
57
57
 
58
58
  // src/file/utils.ts
59
- var getS3Client = () => new S3Client({
59
+ var client = new S3Client({
60
60
  region: getEnv("AWS_REGION" /* AWS_REGION */),
61
61
  credentials: {
62
62
  accessKeyId: getEnv("AWS_ACCESS_KEY_ID" /* AWS_ACCESS_KEY_ID */),
@@ -69,7 +69,8 @@ var URL_PREFIX = `https://${getEnv("AWS_BUCKET" /* AWS_BUCKET */)}.s3.${getEnv(
69
69
  function getKey(keys) {
70
70
  return typeof keys === "string" ? keys : keys.join("/");
71
71
  }
72
- var getUploadURL = (client) => (keys) => {
72
+ var getUploadURL = (keys) => {
73
+ if (!checkClient(client)) return;
73
74
  const command = new PutObjectCommand({
74
75
  Bucket: getEnv("AWS_BUCKET" /* AWS_BUCKET */),
75
76
  Key: getKey(keys),
@@ -80,21 +81,23 @@ var getUploadURL = (client) => (keys) => {
80
81
  var getDownloadURL = (keys) => {
81
82
  return `${URL_PREFIX}${getKey(keys)}`;
82
83
  };
83
- var uploadFile = (client) => async (keys, {
84
+ var uploadFile = async (keys, {
84
85
  url,
85
86
  blob
86
87
  }) => {
88
+ if (!checkClient(client)) return;
87
89
  if (!blob && !url) return null;
88
90
  const fileBlob = blob || await fetch(url).then((file) => file.blob());
89
91
  if (fileBlob) {
90
92
  const key = getKey(keys);
91
- await uploadBlob(client)(fileBlob, key);
93
+ await uploadBlob(fileBlob, key);
92
94
  return getDownloadURL(key);
93
95
  }
94
96
  return null;
95
97
  };
96
- var deleteFile = (client) => async (url) => {
98
+ var deleteFile = async (url) => {
97
99
  const key = url.split(URL_PREFIX)[1];
100
+ if (!checkClient(client)) return;
98
101
  if (key) {
99
102
  try {
100
103
  await client.send(
@@ -110,7 +113,12 @@ var deleteFile = (client) => async (url) => {
110
113
  }
111
114
  return false;
112
115
  };
113
- var uploadBlob = (client) => async (file, key) => {
116
+ function checkClient(client2) {
117
+ if (!client2) throw new Error("Client does not exist");
118
+ return true;
119
+ }
120
+ var uploadBlob = async (file, key) => {
121
+ if (!checkClient(client)) return;
114
122
  const fileBuffer = await file.arrayBuffer();
115
123
  return client.send(
116
124
  new PutObjectCommand({
@@ -126,7 +134,6 @@ var uploadBlob = (client) => async (file, key) => {
126
134
  export {
127
135
  deleteFile,
128
136
  getDownloadURL,
129
- getS3Client,
130
137
  getUploadURL,
131
138
  uploadBlob,
132
139
  uploadFile
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "naystack",
3
- "version": "1.5.12",
3
+ "version": "1.5.14",
4
4
  "description": "A stack built with Next + GraphQL + S3 + Auth",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",