naystack 1.2.17 → 1.2.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/auth/email/client.cjs.js +35 -7
- package/dist/auth/email/client.d.mts +13 -1
- package/dist/auth/email/client.d.ts +13 -1
- package/dist/auth/email/client.esm.js +34 -4
- package/dist/client/hooks.d.mts +2 -2
- package/dist/client/hooks.d.ts +2 -2
- package/dist/file/client.cjs.js +20 -23
- package/dist/file/client.d.mts +4 -10
- package/dist/file/client.d.ts +4 -10
- package/dist/file/client.esm.js +19 -22
- package/dist/file/index.cjs.js +39 -47
- package/dist/file/index.d.mts +0 -1
- package/dist/file/index.d.ts +0 -1
- package/dist/file/index.esm.js +39 -47
- package/dist/file/put.cjs.js +21 -50
- package/dist/file/put.d.mts +2 -1
- package/dist/file/put.d.ts +2 -1
- package/dist/file/put.esm.js +21 -50
- package/dist/file/setup.cjs.js +39 -47
- package/dist/file/setup.d.mts +16 -12
- package/dist/file/setup.d.ts +16 -12
- package/dist/file/setup.esm.js +39 -47
- package/dist/file/utils.cjs.js +29 -27
- package/dist/file/utils.d.mts +9 -6
- package/dist/file/utils.d.ts +9 -6
- package/dist/file/utils.esm.js +24 -22
- package/dist/graphql/client.cjs.js +14 -39
- package/dist/graphql/client.d.mts +5 -14
- package/dist/graphql/client.d.ts +5 -14
- package/dist/graphql/client.esm.js +11 -35
- package/dist/graphql/init.d.mts +1 -1
- package/dist/graphql/init.d.ts +1 -1
- package/dist/graphql/server.d.mts +2 -2
- package/dist/graphql/server.d.ts +2 -2
- package/package.json +1 -1
|
@@ -17,16 +17,40 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
};
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
|
|
20
|
-
// src/auth/email/client.
|
|
20
|
+
// src/auth/email/client.tsx
|
|
21
21
|
var client_exports = {};
|
|
22
22
|
__export(client_exports, {
|
|
23
|
-
|
|
23
|
+
TokenContext: () => TokenContext,
|
|
24
|
+
getApolloWrapper: () => getApolloWrapper,
|
|
25
|
+
getEmailAuthUtils: () => getEmailAuthUtils,
|
|
26
|
+
useSetToken: () => useSetToken,
|
|
27
|
+
useToken: () => useToken
|
|
24
28
|
});
|
|
25
29
|
module.exports = __toCommonJS(client_exports);
|
|
26
|
-
var import_client = require("naystack/graphql/client");
|
|
27
30
|
var import_react = require("react");
|
|
31
|
+
var TokenContext = (0, import_react.createContext)({
|
|
32
|
+
token: null,
|
|
33
|
+
setToken: () => null
|
|
34
|
+
});
|
|
35
|
+
var getApolloWrapper = (endpoint) => ({ children }) => {
|
|
36
|
+
const [token, setToken] = (0, import_react.useState)(null);
|
|
37
|
+
(0, import_react.useEffect)(() => {
|
|
38
|
+
fetch(endpoint, {
|
|
39
|
+
credentials: "include"
|
|
40
|
+
}).then((res) => res.json()).then((data) => setToken(data.accessToken));
|
|
41
|
+
}, []);
|
|
42
|
+
return /* @__PURE__ */ React.createElement(TokenContext.Provider, { value: { token, setToken } }, children);
|
|
43
|
+
};
|
|
44
|
+
function useToken() {
|
|
45
|
+
const { token } = (0, import_react.useContext)(TokenContext);
|
|
46
|
+
return token;
|
|
47
|
+
}
|
|
48
|
+
function useSetToken() {
|
|
49
|
+
const { setToken } = (0, import_react.useContext)(TokenContext);
|
|
50
|
+
return setToken;
|
|
51
|
+
}
|
|
28
52
|
function useSignUpWithEmail(endpoint) {
|
|
29
|
-
const setToken =
|
|
53
|
+
const setToken = useSetToken();
|
|
30
54
|
return (0, import_react.useCallback)(
|
|
31
55
|
async (data) => {
|
|
32
56
|
const res = await fetch(endpoint, {
|
|
@@ -45,7 +69,7 @@ function useSignUpWithEmail(endpoint) {
|
|
|
45
69
|
);
|
|
46
70
|
}
|
|
47
71
|
function useLoginWithEmail(endpoint) {
|
|
48
|
-
const setToken =
|
|
72
|
+
const setToken = useSetToken();
|
|
49
73
|
return (0, import_react.useCallback)(
|
|
50
74
|
async (data) => {
|
|
51
75
|
const res = await fetch(endpoint, {
|
|
@@ -64,7 +88,7 @@ function useLoginWithEmail(endpoint) {
|
|
|
64
88
|
);
|
|
65
89
|
}
|
|
66
90
|
function useLogout(endpoint) {
|
|
67
|
-
const setToken =
|
|
91
|
+
const setToken = useSetToken();
|
|
68
92
|
return (0, import_react.useCallback)(
|
|
69
93
|
async (data) => {
|
|
70
94
|
setToken(null);
|
|
@@ -86,5 +110,9 @@ function getEmailAuthUtils(endpoint) {
|
|
|
86
110
|
}
|
|
87
111
|
// Annotate the CommonJS export names for ESM import in node:
|
|
88
112
|
0 && (module.exports = {
|
|
89
|
-
|
|
113
|
+
TokenContext,
|
|
114
|
+
getApolloWrapper,
|
|
115
|
+
getEmailAuthUtils,
|
|
116
|
+
useSetToken,
|
|
117
|
+
useToken
|
|
90
118
|
});
|
|
@@ -1,7 +1,19 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { Dispatch, SetStateAction } from 'react';
|
|
3
|
+
|
|
4
|
+
declare const TokenContext: react.Context<{
|
|
5
|
+
token: string | null;
|
|
6
|
+
setToken: Dispatch<SetStateAction<string | null>>;
|
|
7
|
+
}>;
|
|
8
|
+
declare const getApolloWrapper: (endpoint: string) => ({ children }: {
|
|
9
|
+
children: React.ReactNode;
|
|
10
|
+
}) => react.JSX.Element;
|
|
11
|
+
declare function useToken(): string | null;
|
|
12
|
+
declare function useSetToken(): Dispatch<SetStateAction<string | null>>;
|
|
1
13
|
declare function getEmailAuthUtils(endpoint: string): {
|
|
2
14
|
useSignUp: () => (data: object) => Promise<string | null>;
|
|
3
15
|
useLogin: () => (data: object) => Promise<string | null>;
|
|
4
16
|
useLogout: () => (data?: object) => Promise<void>;
|
|
5
17
|
};
|
|
6
18
|
|
|
7
|
-
export { getEmailAuthUtils };
|
|
19
|
+
export { TokenContext, getApolloWrapper, getEmailAuthUtils, useSetToken, useToken };
|
|
@@ -1,7 +1,19 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { Dispatch, SetStateAction } from 'react';
|
|
3
|
+
|
|
4
|
+
declare const TokenContext: react.Context<{
|
|
5
|
+
token: string | null;
|
|
6
|
+
setToken: Dispatch<SetStateAction<string | null>>;
|
|
7
|
+
}>;
|
|
8
|
+
declare const getApolloWrapper: (endpoint: string) => ({ children }: {
|
|
9
|
+
children: React.ReactNode;
|
|
10
|
+
}) => react.JSX.Element;
|
|
11
|
+
declare function useToken(): string | null;
|
|
12
|
+
declare function useSetToken(): Dispatch<SetStateAction<string | null>>;
|
|
1
13
|
declare function getEmailAuthUtils(endpoint: string): {
|
|
2
14
|
useSignUp: () => (data: object) => Promise<string | null>;
|
|
3
15
|
useLogin: () => (data: object) => Promise<string | null>;
|
|
4
16
|
useLogout: () => (data?: object) => Promise<void>;
|
|
5
17
|
};
|
|
6
18
|
|
|
7
|
-
export { getEmailAuthUtils };
|
|
19
|
+
export { TokenContext, getApolloWrapper, getEmailAuthUtils, useSetToken, useToken };
|
|
@@ -1,6 +1,32 @@
|
|
|
1
|
-
// src/auth/email/client.
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
// src/auth/email/client.tsx
|
|
2
|
+
import {
|
|
3
|
+
createContext,
|
|
4
|
+
useCallback,
|
|
5
|
+
useContext,
|
|
6
|
+
useEffect,
|
|
7
|
+
useState
|
|
8
|
+
} from "react";
|
|
9
|
+
var TokenContext = createContext({
|
|
10
|
+
token: null,
|
|
11
|
+
setToken: () => null
|
|
12
|
+
});
|
|
13
|
+
var getApolloWrapper = (endpoint) => ({ children }) => {
|
|
14
|
+
const [token, setToken] = useState(null);
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
fetch(endpoint, {
|
|
17
|
+
credentials: "include"
|
|
18
|
+
}).then((res) => res.json()).then((data) => setToken(data.accessToken));
|
|
19
|
+
}, []);
|
|
20
|
+
return /* @__PURE__ */ React.createElement(TokenContext.Provider, { value: { token, setToken } }, children);
|
|
21
|
+
};
|
|
22
|
+
function useToken() {
|
|
23
|
+
const { token } = useContext(TokenContext);
|
|
24
|
+
return token;
|
|
25
|
+
}
|
|
26
|
+
function useSetToken() {
|
|
27
|
+
const { setToken } = useContext(TokenContext);
|
|
28
|
+
return setToken;
|
|
29
|
+
}
|
|
4
30
|
function useSignUpWithEmail(endpoint) {
|
|
5
31
|
const setToken = useSetToken();
|
|
6
32
|
return useCallback(
|
|
@@ -61,5 +87,9 @@ function getEmailAuthUtils(endpoint) {
|
|
|
61
87
|
};
|
|
62
88
|
}
|
|
63
89
|
export {
|
|
64
|
-
|
|
90
|
+
TokenContext,
|
|
91
|
+
getApolloWrapper,
|
|
92
|
+
getEmailAuthUtils,
|
|
93
|
+
useSetToken,
|
|
94
|
+
useToken
|
|
65
95
|
};
|
package/dist/client/hooks.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as react from 'react';
|
|
2
2
|
|
|
3
|
-
declare function useVisibility(onVisible?: () => void):
|
|
3
|
+
declare function useVisibility(onVisible?: () => void): react.RefObject<null>;
|
|
4
4
|
declare function useBreakpoint(query: string): boolean | null;
|
|
5
5
|
|
|
6
6
|
export { useBreakpoint, useVisibility };
|
package/dist/client/hooks.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as react from 'react';
|
|
2
2
|
|
|
3
|
-
declare function useVisibility(onVisible?: () => void):
|
|
3
|
+
declare function useVisibility(onVisible?: () => void): react.RefObject<null>;
|
|
4
4
|
declare function useBreakpoint(query: string): boolean | null;
|
|
5
5
|
|
|
6
6
|
export { useBreakpoint, useVisibility };
|
package/dist/file/client.cjs.js
CHANGED
|
@@ -20,32 +20,29 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/file/client.ts
|
|
21
21
|
var client_exports = {};
|
|
22
22
|
__export(client_exports, {
|
|
23
|
-
|
|
23
|
+
getUseFileUpload: () => getUseFileUpload
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(client_exports);
|
|
26
|
-
var
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}).then(
|
|
45
|
-
async (res) => await res.json() || null
|
|
46
|
-
);
|
|
26
|
+
var import_client = require("naystack/auth/email/client");
|
|
27
|
+
var getUseFileUpload = (route) => () => {
|
|
28
|
+
const token = (0, import_client.useToken)();
|
|
29
|
+
return (file, type, data) => {
|
|
30
|
+
const formData = new FormData();
|
|
31
|
+
formData.append("type", type);
|
|
32
|
+
formData.append("file", file);
|
|
33
|
+
if (data) formData.append("data", JSON.stringify(data));
|
|
34
|
+
return fetch(route, {
|
|
35
|
+
method: "PUT",
|
|
36
|
+
body: formData,
|
|
37
|
+
headers: {
|
|
38
|
+
Authorization: `Bearer ${token}`
|
|
39
|
+
}
|
|
40
|
+
}).then(
|
|
41
|
+
async (res) => await res.json() || null
|
|
42
|
+
);
|
|
43
|
+
};
|
|
47
44
|
};
|
|
48
45
|
// Annotate the CommonJS export names for ESM import in node:
|
|
49
46
|
0 && (module.exports = {
|
|
50
|
-
|
|
47
|
+
getUseFileUpload
|
|
51
48
|
});
|
package/dist/file/client.d.mts
CHANGED
|
@@ -1,13 +1,7 @@
|
|
|
1
|
-
declare const
|
|
2
|
-
|
|
3
|
-
token: string;
|
|
4
|
-
type: string;
|
|
5
|
-
sync?: boolean;
|
|
6
|
-
data?: object;
|
|
7
|
-
}) => Promise<ImageUploadResponseType>;
|
|
8
|
-
interface ImageUploadResponseType {
|
|
1
|
+
declare const getUseFileUpload: (route: string) => () => (file: File | Blob, type: string, data?: object) => Promise<FileUploadResponseType>;
|
|
2
|
+
interface FileUploadResponseType {
|
|
9
3
|
url?: string;
|
|
10
|
-
|
|
4
|
+
onUploadResponse?: object;
|
|
11
5
|
}
|
|
12
6
|
|
|
13
|
-
export {
|
|
7
|
+
export { getUseFileUpload };
|
package/dist/file/client.d.ts
CHANGED
|
@@ -1,13 +1,7 @@
|
|
|
1
|
-
declare const
|
|
2
|
-
|
|
3
|
-
token: string;
|
|
4
|
-
type: string;
|
|
5
|
-
sync?: boolean;
|
|
6
|
-
data?: object;
|
|
7
|
-
}) => Promise<ImageUploadResponseType>;
|
|
8
|
-
interface ImageUploadResponseType {
|
|
1
|
+
declare const getUseFileUpload: (route: string) => () => (file: File | Blob, type: string, data?: object) => Promise<FileUploadResponseType>;
|
|
2
|
+
interface FileUploadResponseType {
|
|
9
3
|
url?: string;
|
|
10
|
-
|
|
4
|
+
onUploadResponse?: object;
|
|
11
5
|
}
|
|
12
6
|
|
|
13
|
-
export {
|
|
7
|
+
export { getUseFileUpload };
|
package/dist/file/client.esm.js
CHANGED
|
@@ -1,26 +1,23 @@
|
|
|
1
1
|
// src/file/client.ts
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}).then(
|
|
21
|
-
async (res) => await res.json() || null
|
|
22
|
-
);
|
|
2
|
+
import { useToken } from "naystack/auth/email/client";
|
|
3
|
+
var getUseFileUpload = (route) => () => {
|
|
4
|
+
const token = useToken();
|
|
5
|
+
return (file, type, data) => {
|
|
6
|
+
const formData = new FormData();
|
|
7
|
+
formData.append("type", type);
|
|
8
|
+
formData.append("file", file);
|
|
9
|
+
if (data) formData.append("data", JSON.stringify(data));
|
|
10
|
+
return fetch(route, {
|
|
11
|
+
method: "PUT",
|
|
12
|
+
body: formData,
|
|
13
|
+
headers: {
|
|
14
|
+
Authorization: `Bearer ${token}`
|
|
15
|
+
}
|
|
16
|
+
}).then(
|
|
17
|
+
async (res) => await res.json() || null
|
|
18
|
+
);
|
|
19
|
+
};
|
|
23
20
|
};
|
|
24
21
|
export {
|
|
25
|
-
|
|
22
|
+
getUseFileUpload
|
|
26
23
|
};
|
package/dist/file/index.cjs.js
CHANGED
|
@@ -25,7 +25,6 @@ __export(file_exports, {
|
|
|
25
25
|
module.exports = __toCommonJS(file_exports);
|
|
26
26
|
|
|
27
27
|
// src/file/put.ts
|
|
28
|
-
var import_functions2 = require("@vercel/functions");
|
|
29
28
|
var import_server3 = require("next/server");
|
|
30
29
|
var import_uuid = require("uuid");
|
|
31
30
|
|
|
@@ -76,10 +75,8 @@ var getContext = (refreshKey, signingKey, req) => {
|
|
|
76
75
|
};
|
|
77
76
|
|
|
78
77
|
// src/file/utils.ts
|
|
79
|
-
var import_node_crypto = require("crypto");
|
|
80
78
|
var import_client_s3 = require("@aws-sdk/client-s3");
|
|
81
79
|
var import_s3_request_presigner = require("@aws-sdk/s3-request-presigner");
|
|
82
|
-
var import_functions = require("@vercel/functions");
|
|
83
80
|
var getS3Client = (options) => new import_client_s3.S3Client({
|
|
84
81
|
region: options.region,
|
|
85
82
|
credentials: {
|
|
@@ -88,30 +85,34 @@ var getS3Client = (options) => new import_client_s3.S3Client({
|
|
|
88
85
|
}
|
|
89
86
|
});
|
|
90
87
|
var getURLPrefix = (options) => `https://${options.bucket}.s3.${options.region}.amazonaws.com/`;
|
|
91
|
-
function
|
|
92
|
-
return
|
|
88
|
+
function getKey(keys) {
|
|
89
|
+
return typeof keys === "string" ? keys : keys.join("/");
|
|
93
90
|
}
|
|
94
|
-
var
|
|
91
|
+
var getUploadURL = (client, Bucket) => (keys) => {
|
|
95
92
|
const command = new import_client_s3.PutObjectCommand({
|
|
96
93
|
Bucket,
|
|
97
|
-
Key:
|
|
98
|
-
ACL:
|
|
94
|
+
Key: getKey(keys),
|
|
95
|
+
ACL: "public-read"
|
|
99
96
|
});
|
|
100
97
|
return (0, import_s3_request_presigner.getSignedUrl)(client, command, { expiresIn: 300 });
|
|
101
98
|
};
|
|
102
|
-
var
|
|
103
|
-
|
|
104
|
-
return `${getURLPrefix(options)}${getHash(keys)}`;
|
|
99
|
+
var getDownloadURL = (options) => (keys) => {
|
|
100
|
+
return `${getURLPrefix(options)}${getKey(keys)}`;
|
|
105
101
|
};
|
|
106
|
-
var
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
102
|
+
var uploadFile = (client, options) => async (keys, {
|
|
103
|
+
url,
|
|
104
|
+
blob
|
|
105
|
+
}) => {
|
|
106
|
+
if (!blob && !url) return null;
|
|
107
|
+
const fileBlob = blob || await fetch(url).then((file) => file.blob());
|
|
108
|
+
if (fileBlob) {
|
|
109
|
+
const key = getKey(keys);
|
|
110
|
+
await uploadBlob(client, options.bucket)(fileBlob, key);
|
|
111
|
+
return getDownloadURL(options)(key);
|
|
111
112
|
}
|
|
112
113
|
return null;
|
|
113
114
|
};
|
|
114
|
-
var
|
|
115
|
+
var deleteFile = (client, options) => async (url) => {
|
|
115
116
|
const key = url.split(getURLPrefix(options))[1];
|
|
116
117
|
if (key) {
|
|
117
118
|
try {
|
|
@@ -128,7 +129,7 @@ var deleteImage = (client, options) => async (url) => {
|
|
|
128
129
|
}
|
|
129
130
|
return false;
|
|
130
131
|
};
|
|
131
|
-
var
|
|
132
|
+
var uploadBlob = (client, Bucket) => async (file, key) => {
|
|
132
133
|
const fileBuffer = await file.arrayBuffer();
|
|
133
134
|
return client.send(
|
|
134
135
|
new import_client_s3.PutObjectCommand({
|
|
@@ -148,33 +149,25 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
|
|
|
148
149
|
if (!ctx?.userId || ctx.isRefreshID)
|
|
149
150
|
return import_server3.NextResponse.json({ error: "unauthorized" }, { status: 401 });
|
|
150
151
|
const formData = await req.formData();
|
|
151
|
-
const type = formData.get("type");
|
|
152
|
-
const sync = Boolean(formData.get("sync"));
|
|
153
152
|
const file = formData.get("file");
|
|
153
|
+
if (!file) return import_server3.NextResponse.json({ error: "no file" }, { status: 400 });
|
|
154
154
|
const data = formData.get("data");
|
|
155
|
-
const
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
if (!type || !ctx.userId || ctx.isRefreshID) return;
|
|
160
|
-
const { deleteURL, response } = await options.processFile({
|
|
161
|
-
url,
|
|
162
|
-
type,
|
|
163
|
-
userId: ctx.userId,
|
|
164
|
-
data: typeof data === "string" ? JSON.parse(data) : void 0
|
|
165
|
-
});
|
|
166
|
-
if (deleteURL) await deleteImage(client, options)(deleteURL);
|
|
167
|
-
return response;
|
|
155
|
+
const inputData = {
|
|
156
|
+
type: formData.get("type") + "",
|
|
157
|
+
userId: ctx.userId,
|
|
158
|
+
data: typeof data === "string" ? JSON.parse(data) : void 0
|
|
168
159
|
};
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
160
|
+
const fileKey = options.getKey ? await options.getKey(inputData) : (0, import_uuid.v4)();
|
|
161
|
+
const url = getDownloadURL(options)(fileKey);
|
|
162
|
+
await uploadBlob(client, options.bucket)(file, fileKey);
|
|
163
|
+
const onUploadResponse = await options.onUpload({
|
|
164
|
+
...inputData,
|
|
165
|
+
url
|
|
166
|
+
});
|
|
167
|
+
return import_server3.NextResponse.json({
|
|
168
|
+
url,
|
|
169
|
+
onUploadResponse
|
|
170
|
+
});
|
|
178
171
|
};
|
|
179
172
|
|
|
180
173
|
// src/file/setup.ts
|
|
@@ -182,11 +175,10 @@ function setupFileUpload(options) {
|
|
|
182
175
|
const client = getS3Client(options);
|
|
183
176
|
return {
|
|
184
177
|
PUT: getFileUploadPutRoute(options, client),
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
uploadFile: uploadFile(client, options.bucket)
|
|
178
|
+
uploadFile: uploadFile(client, options),
|
|
179
|
+
deleteFile: deleteFile(client, options),
|
|
180
|
+
getUploadURL: getUploadURL(client, options.bucket),
|
|
181
|
+
getDownloadURL: getDownloadURL(options)
|
|
190
182
|
};
|
|
191
183
|
}
|
|
192
184
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/file/index.d.mts
CHANGED
package/dist/file/index.d.ts
CHANGED
package/dist/file/index.esm.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
// src/file/put.ts
|
|
2
|
-
import { waitUntil as waitUntil2 } from "@vercel/functions";
|
|
3
2
|
import { NextResponse as NextResponse3 } from "next/server";
|
|
4
3
|
import { v4 } from "uuid";
|
|
5
4
|
|
|
@@ -50,14 +49,12 @@ var getContext = (refreshKey, signingKey, req) => {
|
|
|
50
49
|
};
|
|
51
50
|
|
|
52
51
|
// src/file/utils.ts
|
|
53
|
-
import { createHash } from "crypto";
|
|
54
52
|
import {
|
|
55
53
|
DeleteObjectCommand,
|
|
56
54
|
PutObjectCommand,
|
|
57
55
|
S3Client
|
|
58
56
|
} from "@aws-sdk/client-s3";
|
|
59
57
|
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
|
60
|
-
import { waitUntil } from "@vercel/functions";
|
|
61
58
|
var getS3Client = (options) => new S3Client({
|
|
62
59
|
region: options.region,
|
|
63
60
|
credentials: {
|
|
@@ -66,30 +63,34 @@ var getS3Client = (options) => new S3Client({
|
|
|
66
63
|
}
|
|
67
64
|
});
|
|
68
65
|
var getURLPrefix = (options) => `https://${options.bucket}.s3.${options.region}.amazonaws.com/`;
|
|
69
|
-
function
|
|
70
|
-
return
|
|
66
|
+
function getKey(keys) {
|
|
67
|
+
return typeof keys === "string" ? keys : keys.join("/");
|
|
71
68
|
}
|
|
72
|
-
var
|
|
69
|
+
var getUploadURL = (client, Bucket) => (keys) => {
|
|
73
70
|
const command = new PutObjectCommand({
|
|
74
71
|
Bucket,
|
|
75
|
-
Key:
|
|
76
|
-
ACL:
|
|
72
|
+
Key: getKey(keys),
|
|
73
|
+
ACL: "public-read"
|
|
77
74
|
});
|
|
78
75
|
return getSignedUrl(client, command, { expiresIn: 300 });
|
|
79
76
|
};
|
|
80
|
-
var
|
|
81
|
-
|
|
82
|
-
return `${getURLPrefix(options)}${getHash(keys)}`;
|
|
77
|
+
var getDownloadURL = (options) => (keys) => {
|
|
78
|
+
return `${getURLPrefix(options)}${getKey(keys)}`;
|
|
83
79
|
};
|
|
84
|
-
var
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
80
|
+
var uploadFile = (client, options) => async (keys, {
|
|
81
|
+
url,
|
|
82
|
+
blob
|
|
83
|
+
}) => {
|
|
84
|
+
if (!blob && !url) return null;
|
|
85
|
+
const fileBlob = blob || await fetch(url).then((file) => file.blob());
|
|
86
|
+
if (fileBlob) {
|
|
87
|
+
const key = getKey(keys);
|
|
88
|
+
await uploadBlob(client, options.bucket)(fileBlob, key);
|
|
89
|
+
return getDownloadURL(options)(key);
|
|
89
90
|
}
|
|
90
91
|
return null;
|
|
91
92
|
};
|
|
92
|
-
var
|
|
93
|
+
var deleteFile = (client, options) => async (url) => {
|
|
93
94
|
const key = url.split(getURLPrefix(options))[1];
|
|
94
95
|
if (key) {
|
|
95
96
|
try {
|
|
@@ -106,7 +107,7 @@ var deleteImage = (client, options) => async (url) => {
|
|
|
106
107
|
}
|
|
107
108
|
return false;
|
|
108
109
|
};
|
|
109
|
-
var
|
|
110
|
+
var uploadBlob = (client, Bucket) => async (file, key) => {
|
|
110
111
|
const fileBuffer = await file.arrayBuffer();
|
|
111
112
|
return client.send(
|
|
112
113
|
new PutObjectCommand({
|
|
@@ -126,33 +127,25 @@ var getFileUploadPutRoute = (options, client) => async (req) => {
|
|
|
126
127
|
if (!ctx?.userId || ctx.isRefreshID)
|
|
127
128
|
return NextResponse3.json({ error: "unauthorized" }, { status: 401 });
|
|
128
129
|
const formData = await req.formData();
|
|
129
|
-
const type = formData.get("type");
|
|
130
|
-
const sync = Boolean(formData.get("sync"));
|
|
131
130
|
const file = formData.get("file");
|
|
131
|
+
if (!file) return NextResponse3.json({ error: "no file" }, { status: 400 });
|
|
132
132
|
const data = formData.get("data");
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
if (!type || !ctx.userId || ctx.isRefreshID) return;
|
|
138
|
-
const { deleteURL, response } = await options.processFile({
|
|
139
|
-
url,
|
|
140
|
-
type,
|
|
141
|
-
userId: ctx.userId,
|
|
142
|
-
data: typeof data === "string" ? JSON.parse(data) : void 0
|
|
143
|
-
});
|
|
144
|
-
if (deleteURL) await deleteImage(client, options)(deleteURL);
|
|
145
|
-
return response;
|
|
133
|
+
const inputData = {
|
|
134
|
+
type: formData.get("type") + "",
|
|
135
|
+
userId: ctx.userId,
|
|
136
|
+
data: typeof data === "string" ? JSON.parse(data) : void 0
|
|
146
137
|
};
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
138
|
+
const fileKey = options.getKey ? await options.getKey(inputData) : v4();
|
|
139
|
+
const url = getDownloadURL(options)(fileKey);
|
|
140
|
+
await uploadBlob(client, options.bucket)(file, fileKey);
|
|
141
|
+
const onUploadResponse = await options.onUpload({
|
|
142
|
+
...inputData,
|
|
143
|
+
url
|
|
144
|
+
});
|
|
145
|
+
return NextResponse3.json({
|
|
146
|
+
url,
|
|
147
|
+
onUploadResponse
|
|
148
|
+
});
|
|
156
149
|
};
|
|
157
150
|
|
|
158
151
|
// src/file/setup.ts
|
|
@@ -160,11 +153,10 @@ function setupFileUpload(options) {
|
|
|
160
153
|
const client = getS3Client(options);
|
|
161
154
|
return {
|
|
162
155
|
PUT: getFileUploadPutRoute(options, client),
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
uploadFile: uploadFile(client, options.bucket)
|
|
156
|
+
uploadFile: uploadFile(client, options),
|
|
157
|
+
deleteFile: deleteFile(client, options),
|
|
158
|
+
getUploadURL: getUploadURL(client, options.bucket),
|
|
159
|
+
getDownloadURL: getDownloadURL(options)
|
|
168
160
|
};
|
|
169
161
|
}
|
|
170
162
|
export {
|