tiny-essentials 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/FUNDING.yml +4 -0
- package/.github/workflows/node.js.yml +31 -0
- package/LICENSE +674 -0
- package/README.md +67 -0
- package/dist/TinyEssentials.min.js +1 -0
- package/dist/_virtual/_commonjsHelpers.cjs +7 -0
- package/dist/_virtual/common.cjs +5 -0
- package/dist/_virtual/index.cjs +9 -0
- package/dist/_virtual/index2.cjs +5 -0
- package/dist/_virtual/trace.cjs +5 -0
- package/dist/legacy/crypto/decrypt.cjs +57 -0
- package/dist/legacy/crypto/decrypt.d.mts +18 -0
- package/dist/legacy/crypto/decrypt.mjs +47 -0
- package/dist/legacy/crypto/default.cjs +23 -0
- package/dist/legacy/crypto/default.d.mts +35 -0
- package/dist/legacy/crypto/default.mjs +19 -0
- package/dist/legacy/crypto/encrypt.cjs +54 -0
- package/dist/legacy/crypto/encrypt.d.mts +19 -0
- package/dist/legacy/crypto/encrypt.mjs +46 -0
- package/dist/legacy/crypto/index.cjs +11 -0
- package/dist/legacy/crypto/index.d.mts +4 -0
- package/dist/legacy/crypto/index.mjs +6 -0
- package/dist/legacy/firebase/cookieSession.cjs +121 -0
- package/dist/legacy/firebase/cookieSession.d.mts +56 -0
- package/dist/legacy/firebase/cookieSession.mjs +114 -0
- package/dist/legacy/firebase/database/index.cjs +11 -0
- package/dist/legacy/firebase/database/index.d.mts +7 -0
- package/dist/legacy/firebase/database/index.mjs +7 -0
- package/dist/legacy/firebase/database/presence.cjs +131 -0
- package/dist/legacy/firebase/database/presence.d.mts +9 -0
- package/dist/legacy/firebase/database/presence.mjs +111 -0
- package/dist/legacy/firebase/database/saveAsync.cjs +147 -0
- package/dist/legacy/firebase/database/saveAsync.d.mts +54 -0
- package/dist/legacy/firebase/database/saveAsync.mjs +147 -0
- package/dist/legacy/firebase/databaseEscape.cjs +42 -0
- package/dist/legacy/firebase/databaseEscape.d.mts +11 -0
- package/dist/legacy/firebase/databaseEscape.mjs +37 -0
- package/dist/legacy/firebase/databaseLogger.cjs +258 -0
- package/dist/legacy/firebase/databaseLogger.d.mts +7 -0
- package/dist/legacy/firebase/databaseLogger.mjs +234 -0
- package/dist/legacy/firebase/discord/api/addGuildMember.cjs +71 -0
- package/dist/legacy/firebase/discord/api/addGuildMember.d.mts +38 -0
- package/dist/legacy/firebase/discord/api/addGuildMember.mjs +64 -0
- package/dist/legacy/firebase/discord/api/getGuildWidget.cjs +47 -0
- package/dist/legacy/firebase/discord/api/getGuildWidget.d.mts +17 -0
- package/dist/legacy/firebase/discord/api/getGuildWidget.mjs +43 -0
- package/dist/legacy/firebase/discord/api/getToken.cjs +67 -0
- package/dist/legacy/firebase/discord/api/getToken.d.mts +35 -0
- package/dist/legacy/firebase/discord/api/getToken.mjs +63 -0
- package/dist/legacy/firebase/discord/api/getUser.cjs +58 -0
- package/dist/legacy/firebase/discord/api/getUser.d.mts +27 -0
- package/dist/legacy/firebase/discord/api/getUser.mjs +54 -0
- package/dist/legacy/firebase/discord/api/getUserConnections.cjs +57 -0
- package/dist/legacy/firebase/discord/api/getUserConnections.d.mts +26 -0
- package/dist/legacy/firebase/discord/api/getUserConnections.mjs +53 -0
- package/dist/legacy/firebase/discord/api/getUserGuilds.cjs +56 -0
- package/dist/legacy/firebase/discord/api/getUserGuilds.d.mts +25 -0
- package/dist/legacy/firebase/discord/api/getUserGuilds.mjs +52 -0
- package/dist/legacy/firebase/discord/api/index.cjs +24 -0
- package/dist/legacy/firebase/discord/api/index.d.mts +19 -0
- package/dist/legacy/firebase/discord/api/index.mjs +20 -0
- package/dist/legacy/firebase/discord/api/refreshToken.cjs +77 -0
- package/dist/legacy/firebase/discord/api/refreshToken.d.mts +45 -0
- package/dist/legacy/firebase/discord/api/refreshToken.mjs +73 -0
- package/dist/legacy/firebase/discord/api/revokeToken.cjs +61 -0
- package/dist/legacy/firebase/discord/api/revokeToken.d.mts +29 -0
- package/dist/legacy/firebase/discord/api/revokeToken.mjs +58 -0
- package/dist/legacy/firebase/discord/config.cjs +21 -0
- package/dist/legacy/firebase/discord/config.d.mts +27 -0
- package/dist/legacy/firebase/discord/config.mjs +17 -0
- package/dist/legacy/firebase/discord/firebase_redirect/index.cjs +11 -0
- package/dist/legacy/firebase/discord/firebase_redirect/index.d.mts +7 -0
- package/dist/legacy/firebase/discord/firebase_redirect/index.mjs +7 -0
- package/dist/legacy/firebase/discord/firebase_redirect/login.cjs +110 -0
- package/dist/legacy/firebase/discord/firebase_redirect/login.d.mts +4 -0
- package/dist/legacy/firebase/discord/firebase_redirect/login.mjs +107 -0
- package/dist/legacy/firebase/discord/firebase_redirect/logout.cjs +100 -0
- package/dist/legacy/firebase/discord/firebase_redirect/logout.d.mts +4 -0
- package/dist/legacy/firebase/discord/firebase_redirect/logout.mjs +97 -0
- package/dist/legacy/firebase/discord/get/authURLGenerator.cjs +76 -0
- package/dist/legacy/firebase/discord/get/authURLGenerator.d.mts +23 -0
- package/dist/legacy/firebase/discord/get/authURLGenerator.mjs +67 -0
- package/dist/legacy/firebase/discord/get/cookie-session.cjs +32 -0
- package/dist/legacy/firebase/discord/get/cookie-session.d.mts +20 -0
- package/dist/legacy/firebase/discord/get/cookie-session.mjs +28 -0
- package/dist/legacy/firebase/discord/get/credentials.cjs +24 -0
- package/dist/legacy/firebase/discord/get/credentials.d.mts +19 -0
- package/dist/legacy/firebase/discord/get/credentials.mjs +20 -0
- package/dist/legacy/firebase/discord/get/errorValidator.cjs +60 -0
- package/dist/legacy/firebase/discord/get/errorValidator.d.mts +28 -0
- package/dist/legacy/firebase/discord/get/errorValidator.mjs +55 -0
- package/dist/legacy/firebase/discord/get/index.cjs +17 -0
- package/dist/legacy/firebase/discord/get/index.d.mts +13 -0
- package/dist/legacy/firebase/discord/get/index.mjs +13 -0
- package/dist/legacy/firebase/discord/get/randomAvatar.cjs +37 -0
- package/dist/legacy/firebase/discord/get/randomAvatar.d.mts +22 -0
- package/dist/legacy/firebase/discord/get/randomAvatar.mjs +30 -0
- package/dist/legacy/firebase/discord/http/index.cjs +15 -0
- package/dist/legacy/firebase/discord/http/index.d.mts +11 -0
- package/dist/legacy/firebase/discord/http/index.mjs +11 -0
- package/dist/legacy/firebase/discord/http/login.cjs +127 -0
- package/dist/legacy/firebase/discord/http/login.d.mts +21 -0
- package/dist/legacy/firebase/discord/http/login.mjs +123 -0
- package/dist/legacy/firebase/discord/http/logout.cjs +155 -0
- package/dist/legacy/firebase/discord/http/logout.d.mts +22 -0
- package/dist/legacy/firebase/discord/http/logout.mjs +143 -0
- package/dist/legacy/firebase/discord/http/redirect.cjs +188 -0
- package/dist/legacy/firebase/discord/http/redirect.d.mts +24 -0
- package/dist/legacy/firebase/discord/http/redirect.mjs +178 -0
- package/dist/legacy/firebase/discord/http/refreshToken.cjs +133 -0
- package/dist/legacy/firebase/discord/http/refreshToken.d.mts +24 -0
- package/dist/legacy/firebase/discord/http/refreshToken.mjs +121 -0
- package/dist/legacy/firebase/discord/index.cjs +19 -0
- package/dist/legacy/firebase/discord/index.d.mts +15 -0
- package/dist/legacy/firebase/discord/index.mjs +15 -0
- package/dist/legacy/firebase/discord/template/cookie-session.cjs +1430 -0
- package/dist/legacy/firebase/discord/template/cookie-session.d.mts +22 -0
- package/dist/legacy/firebase/discord/template/cookie-session.mjs +1159 -0
- package/dist/legacy/firebase/domainRedirect.cjs +35 -0
- package/dist/legacy/firebase/domainRedirect.d.mts +12 -0
- package/dist/legacy/firebase/domainRedirect.mjs +27 -0
- package/dist/legacy/firebase/escape.cjs +75 -0
- package/dist/legacy/firebase/escape.d.mts +17 -0
- package/dist/legacy/firebase/escape.mjs +58 -0
- package/dist/legacy/firebase/getDBAsync.cjs +33 -0
- package/dist/legacy/firebase/getDBAsync.d.mts +10 -0
- package/dist/legacy/firebase/getDBAsync.mjs +26 -0
- package/dist/legacy/firebase/getDBData.cjs +21 -0
- package/dist/legacy/firebase/getDBData.d.mts +1 -0
- package/dist/legacy/firebase/getDBData.mjs +17 -0
- package/dist/legacy/firebase/getDBValue.cjs +17 -0
- package/dist/legacy/firebase/getDBValue.d.mts +9 -0
- package/dist/legacy/firebase/getDBValue.mjs +14 -0
- package/dist/legacy/firebase/index.cjs +261 -0
- package/dist/legacy/firebase/index.d.mts +51 -0
- package/dist/legacy/firebase/index.mjs +234 -0
- package/dist/legacy/firebase/isEmulator.cjs +14 -0
- package/dist/legacy/firebase/isEmulator.d.mts +8 -0
- package/dist/legacy/firebase/isEmulator.mjs +10 -0
- package/dist/legacy/firebase/logger.cjs +137 -0
- package/dist/legacy/firebase/logger.d.mts +7 -0
- package/dist/legacy/firebase/logger.mjs +125 -0
- package/dist/legacy/firebase/mySQL.cjs +26 -0
- package/dist/legacy/firebase/mySQL.d.mts +10 -0
- package/dist/legacy/firebase/mySQL.mjs +22 -0
- package/dist/legacy/firebase/mysqlConnector/create.cjs +119 -0
- package/dist/legacy/firebase/mysqlConnector/create.d.mts +36 -0
- package/dist/legacy/firebase/mysqlConnector/create.mjs +109 -0
- package/dist/legacy/firebase/mysqlConnector/index.cjs +23 -0
- package/dist/legacy/firebase/mysqlConnector/index.d.mts +11 -0
- package/dist/legacy/firebase/mysqlConnector/index.mjs +16 -0
- package/dist/legacy/firebase/mysqlConnector/nextPrev.cjs +75 -0
- package/dist/legacy/firebase/mysqlConnector/nextPrev.d.mts +31 -0
- package/dist/legacy/firebase/mysqlConnector/nextPrev.mjs +63 -0
- package/dist/legacy/firebase/mysqlConnector/pagination.cjs +91 -0
- package/dist/legacy/firebase/mysqlConnector/pagination.d.mts +33 -0
- package/dist/legacy/firebase/mysqlConnector/pagination.mjs +87 -0
- package/dist/legacy/firebase/mysqlConnector/sameUser.cjs +28 -0
- package/dist/legacy/firebase/mysqlConnector/sameUser.d.mts +12 -0
- package/dist/legacy/firebase/mysqlConnector/sameUser.mjs +23 -0
- package/dist/legacy/firebase/transactionDBAsync.cjs +33 -0
- package/dist/legacy/firebase/transactionDBAsync.d.mts +11 -0
- package/dist/legacy/firebase/transactionDBAsync.mjs +27 -0
- package/dist/legacy/get/countObj.cjs +24 -0
- package/dist/legacy/get/countObj.d.mts +11 -0
- package/dist/legacy/get/countObj.mjs +21 -0
- package/dist/legacy/get/decimalColor.cjs +60 -0
- package/dist/legacy/get/decimalColor.d.mts +12 -0
- package/dist/legacy/get/decimalColor.mjs +54 -0
- package/dist/legacy/get/objType.cjs +43 -0
- package/dist/legacy/get/objType.d.mts +19 -0
- package/dist/legacy/get/objType.mjs +40 -0
- package/dist/legacy/get/pagination.cjs +100 -0
- package/dist/legacy/get/pagination.d.mts +25 -0
- package/dist/legacy/get/pagination.mjs +91 -0
- package/dist/legacy/get/queryUrlByName.cjs +31 -0
- package/dist/legacy/get/queryUrlByName.d.mts +20 -0
- package/dist/legacy/get/queryUrlByName.mjs +28 -0
- package/dist/legacy/get/queryUrlJSON.cjs +54 -0
- package/dist/legacy/get/queryUrlJSON.d.mts +23 -0
- package/dist/legacy/get/queryUrlJSON.mjs +57 -0
- package/dist/legacy/get/super_string_filter.cjs +97 -0
- package/dist/legacy/get/super_string_filter.d.mts +25 -0
- package/dist/legacy/get/super_string_filter.mjs +83 -0
- package/dist/legacy/get/versionCheck.cjs +49 -0
- package/dist/legacy/get/versionCheck.d.mts +17 -0
- package/dist/legacy/get/versionCheck.mjs +38 -0
- package/dist/legacy/http/HTTP-1.0.cjs +151 -0
- package/dist/legacy/http/HTTP-1.0.d.mts +8 -0
- package/dist/legacy/http/HTTP-1.0.mjs +136 -0
- package/dist/legacy/http/auth.cjs +70 -0
- package/dist/legacy/http/auth.d.mts +41 -0
- package/dist/legacy/http/auth.mjs +63 -0
- package/dist/legacy/http/check_domain.cjs +84 -0
- package/dist/legacy/http/check_domain.d.mts +10 -0
- package/dist/legacy/http/check_domain.mjs +82 -0
- package/dist/legacy/http/csrfTokenAnalyze.cjs +46 -0
- package/dist/legacy/http/csrfTokenAnalyze.d.mts +21 -0
- package/dist/legacy/http/csrfTokenAnalyze.mjs +40 -0
- package/dist/legacy/http/domainValidator.cjs +98 -0
- package/dist/legacy/http/domainValidator.d.mts +30 -0
- package/dist/legacy/http/domainValidator.mjs +82 -0
- package/dist/legacy/http/errorsCallback.cjs +55 -0
- package/dist/legacy/http/errorsCallback.d.mts +29 -0
- package/dist/legacy/http/errorsCallback.mjs +48 -0
- package/dist/legacy/http/fetch/json.cjs +48 -0
- package/dist/legacy/http/fetch/json.d.mts +22 -0
- package/dist/legacy/http/fetch/json.mjs +41 -0
- package/dist/legacy/http/fetch/text.cjs +47 -0
- package/dist/legacy/http/fetch/text.d.mts +21 -0
- package/dist/legacy/http/fetch/text.mjs +40 -0
- package/dist/legacy/http/fileCache.cjs +79 -0
- package/dist/legacy/http/fileCache.d.mts +35 -0
- package/dist/legacy/http/fileCache.mjs +70 -0
- package/dist/legacy/http/getDomainURL.cjs +59 -0
- package/dist/legacy/http/getDomainURL.d.mts +18 -0
- package/dist/legacy/http/getDomainURL.mjs +54 -0
- package/dist/legacy/http/userIP.cjs +59 -0
- package/dist/legacy/http/userIP.d.mts +21 -0
- package/dist/legacy/http/userIP.mjs +48 -0
- package/dist/legacy/index.cjs +122 -0
- package/dist/legacy/index.d.mts +89 -0
- package/dist/legacy/index.mjs +92 -0
- package/dist/legacy/libs/arraySortPositions.cjs +30 -0
- package/dist/legacy/libs/arraySortPositions.d.mts +16 -0
- package/dist/legacy/libs/arraySortPositions.mjs +27 -0
- package/dist/legacy/libs/capitalize.cjs +21 -0
- package/dist/legacy/libs/capitalize.d.mts +13 -0
- package/dist/legacy/libs/capitalize.mjs +17 -0
- package/dist/legacy/libs/convertBytes.cjs +64 -0
- package/dist/legacy/libs/convertBytes.d.mts +17 -0
- package/dist/legacy/libs/convertBytes.mjs +55 -0
- package/dist/legacy/libs/custom_module_loader.cjs +82 -0
- package/dist/legacy/libs/custom_module_loader.d.mts +5 -0
- package/dist/legacy/libs/custom_module_loader.mjs +79 -0
- package/dist/legacy/libs/dice.cjs +35 -0
- package/dist/legacy/libs/dice.d.mts +5 -0
- package/dist/legacy/libs/dice.mjs +31 -0
- package/dist/legacy/libs/markdown.cjs +85 -0
- package/dist/legacy/libs/markdown.d.mts +75 -0
- package/dist/legacy/libs/markdown.mjs +78 -0
- package/dist/legacy/libs/percentage.cjs +22 -0
- package/dist/legacy/libs/percentage.d.mts +4 -0
- package/dist/legacy/libs/percentage.mjs +19 -0
- package/dist/legacy/libs/regex/getLetter.cjs +22 -0
- package/dist/legacy/libs/regex/getLetter.d.mts +12 -0
- package/dist/legacy/libs/regex/getLetter.mjs +19 -0
- package/dist/legacy/libs/replaceAsync.cjs +32 -0
- package/dist/legacy/libs/replaceAsync.d.mts +15 -0
- package/dist/legacy/libs/replaceAsync.mjs +25 -0
- package/dist/legacy/libs/rule3.cjs +58 -0
- package/dist/legacy/libs/rule3.d.mts +5 -0
- package/dist/legacy/libs/rule3.mjs +54 -0
- package/dist/legacy/libs/userLevel.cjs +134 -0
- package/dist/legacy/libs/userLevel.d.mts +69 -0
- package/dist/legacy/libs/userLevel.mjs +123 -0
- package/dist/legacy/momentjs/getAge.cjs +28 -0
- package/dist/legacy/momentjs/getAge.d.mts +8 -0
- package/dist/legacy/momentjs/getAge.mjs +22 -0
- package/dist/legacy/momentjs/index.cjs +9 -0
- package/dist/legacy/momentjs/index.d.mts +3 -0
- package/dist/legacy/momentjs/index.mjs +3 -0
- package/dist/legacy/momentjs/timeDuration.cjs +29 -0
- package/dist/legacy/momentjs/timeDuration.d.mts +9 -0
- package/dist/legacy/momentjs/timeDuration.mjs +23 -0
- package/dist/legacy/socket.io/antiFlood/index.cjs +9 -0
- package/dist/legacy/socket.io/antiFlood/index.d.mts +3 -0
- package/dist/legacy/socket.io/antiFlood/index.mjs +3 -0
- package/dist/legacy/socket.io/antiFlood/install.cjs +32 -0
- package/dist/legacy/socket.io/antiFlood/install.d.mts +14 -0
- package/dist/legacy/socket.io/antiFlood/install.mjs +27 -0
- package/dist/legacy/socket.io/antiFlood/verify.cjs +87 -0
- package/dist/legacy/socket.io/antiFlood/verify.d.mts +15 -0
- package/dist/legacy/socket.io/antiFlood/verify.mjs +73 -0
- package/dist/legacy/socket.io/cookie-session.cjs +25 -0
- package/dist/legacy/socket.io/cookie-session.d.mts +9 -0
- package/dist/legacy/socket.io/cookie-session.mjs +20 -0
- package/dist/legacy/socket.io/discord.cjs +61 -0
- package/dist/legacy/socket.io/discord.d.mts +10 -0
- package/dist/legacy/socket.io/discord.mjs +52 -0
- package/dist/legacy/socket.io/index.cjs +13 -0
- package/dist/legacy/socket.io/index.d.mts +9 -0
- package/dist/legacy/socket.io/index.mjs +9 -0
- package/dist/v1/basics/array.cjs +31 -0
- package/dist/v1/basics/array.d.mts +10 -0
- package/dist/v1/basics/array.mjs +22 -0
- package/dist/v1/basics/clock.cjs +187 -0
- package/dist/v1/basics/clock.d.mts +37 -0
- package/dist/v1/basics/clock.mjs +152 -0
- package/dist/v1/basics/objFilter.cjs +55 -0
- package/dist/v1/basics/objFilter.d.mts +27 -0
- package/dist/v1/basics/objFilter.mjs +52 -0
- package/dist/v1/basics/simpleMath.cjs +88 -0
- package/dist/v1/basics/simpleMath.d.mts +54 -0
- package/dist/v1/basics/simpleMath.mjs +75 -0
- package/dist/v1/basics/text.cjs +34 -0
- package/dist/v1/basics/text.d.mts +20 -0
- package/dist/v1/basics/text.mjs +25 -0
- package/dist/v1/index.cjs +26 -0
- package/dist/v1/index.d.mts +15 -0
- package/dist/v1/index.mjs +8 -0
- package/package.json +72 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @function csrfTokenAnalyze
|
|
5
|
+
*
|
|
6
|
+
* Checks if the CSRF token in the request body matches the expected token from the session.
|
|
7
|
+
* If invalid, returns a 401 response and optionally executes a custom callback.
|
|
8
|
+
*
|
|
9
|
+
* @param {Object} req - The Express request object. Should contain:
|
|
10
|
+
* - `req.csrfToken.now`: The expected CSRF token.
|
|
11
|
+
* - `req.body.csrfToken`: The token provided by the client.
|
|
12
|
+
* @param {Object} res - The Express response object.
|
|
13
|
+
* @param {Function} [callback] - Optional custom callback to execute when CSRF validation fails.
|
|
14
|
+
*
|
|
15
|
+
* @returns {boolean} Returns `true` if the token is invalid and a response was sent, otherwise `false`.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* app.post('/submit', (req, res) => {
|
|
19
|
+
* if (csrfTokenAnalyze(req, res)) return;
|
|
20
|
+
* // Continue with request logic if CSRF is valid
|
|
21
|
+
* });
|
|
22
|
+
*/
|
|
23
|
+
function csrfTokenAnalyze(req, res, callback) {
|
|
24
|
+
// Check Values
|
|
25
|
+
if (
|
|
26
|
+
req.csrfToken &&
|
|
27
|
+
typeof req.csrfToken.now === 'string' &&
|
|
28
|
+
(typeof req.body.csrfToken !== 'string' || req.body.csrfToken !== req.csrfToken.now)
|
|
29
|
+
) {
|
|
30
|
+
// Result
|
|
31
|
+
res.status(401);
|
|
32
|
+
|
|
33
|
+
// Normal Callback
|
|
34
|
+
if (typeof callback !== 'function') res.json({ code: 401, text: 'CSRFToken!' });
|
|
35
|
+
// Custom
|
|
36
|
+
else callback();
|
|
37
|
+
|
|
38
|
+
// Complete
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Nope
|
|
43
|
+
else return false;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
module.exports = csrfTokenAnalyze;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @function csrfTokenAnalyze
|
|
3
|
+
*
|
|
4
|
+
* Checks if the CSRF token in the request body matches the expected token from the session.
|
|
5
|
+
* If invalid, returns a 401 response and optionally executes a custom callback.
|
|
6
|
+
*
|
|
7
|
+
* @param {Object} req - The Express request object. Should contain:
|
|
8
|
+
* - `req.csrfToken.now`: The expected CSRF token.
|
|
9
|
+
* - `req.body.csrfToken`: The token provided by the client.
|
|
10
|
+
* @param {Object} res - The Express response object.
|
|
11
|
+
* @param {Function} [callback] - Optional custom callback to execute when CSRF validation fails.
|
|
12
|
+
*
|
|
13
|
+
* @returns {boolean} Returns `true` if the token is invalid and a response was sent, otherwise `false`.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* app.post('/submit', (req, res) => {
|
|
17
|
+
* if (csrfTokenAnalyze(req, res)) return;
|
|
18
|
+
* // Continue with request logic if CSRF is valid
|
|
19
|
+
* });
|
|
20
|
+
*/
|
|
21
|
+
export default function csrfTokenAnalyze(req: Object, res: Object, callback?: Function): boolean;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @function csrfTokenAnalyze
|
|
3
|
+
*
|
|
4
|
+
* Checks if the CSRF token in the request body matches the expected token from the session.
|
|
5
|
+
* If invalid, returns a 401 response and optionally executes a custom callback.
|
|
6
|
+
*
|
|
7
|
+
* @param {Object} req - The Express request object. Should contain:
|
|
8
|
+
* - `req.csrfToken.now`: The expected CSRF token.
|
|
9
|
+
* - `req.body.csrfToken`: The token provided by the client.
|
|
10
|
+
* @param {Object} res - The Express response object.
|
|
11
|
+
* @param {Function} [callback] - Optional custom callback to execute when CSRF validation fails.
|
|
12
|
+
*
|
|
13
|
+
* @returns {boolean} Returns `true` if the token is invalid and a response was sent, otherwise `false`.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* app.post('/submit', (req, res) => {
|
|
17
|
+
* if (csrfTokenAnalyze(req, res)) return;
|
|
18
|
+
* // Continue with request logic if CSRF is valid
|
|
19
|
+
* });
|
|
20
|
+
*/
|
|
21
|
+
export default function csrfTokenAnalyze(req, res, callback) {
|
|
22
|
+
// Check Values
|
|
23
|
+
if (req.csrfToken &&
|
|
24
|
+
typeof req.csrfToken.now === 'string' &&
|
|
25
|
+
(typeof req.body.csrfToken !== 'string' || req.body.csrfToken !== req.csrfToken.now)) {
|
|
26
|
+
// Result
|
|
27
|
+
res.status(401);
|
|
28
|
+
// Normal Callback
|
|
29
|
+
if (typeof callback !== 'function')
|
|
30
|
+
res.json({ code: 401, text: 'CSRFToken!' });
|
|
31
|
+
// Custom
|
|
32
|
+
else
|
|
33
|
+
callback();
|
|
34
|
+
// Complete
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
// Nope
|
|
38
|
+
else
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var check_domain = require('./check_domain.cjs');
|
|
4
|
+
var objType = require('../get/objType.cjs');
|
|
5
|
+
var isEmulator = require('../firebase/isEmulator.cjs');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @function domainValidator
|
|
9
|
+
*
|
|
10
|
+
* Validates the request's domain and optionally checks if the request is for a static path.
|
|
11
|
+
* This is useful for filtering requests by origin or allowing access from specific domains only.
|
|
12
|
+
* Also detects if Firebase is running in emulator mode, which bypasses domain validation.
|
|
13
|
+
*
|
|
14
|
+
* @param {Object} req - The Express request object.
|
|
15
|
+
* - `req.url`: Full URL path.
|
|
16
|
+
* - `req.headers`: Expected to contain 'host', 'x-forwarded-host', etc.
|
|
17
|
+
* @param {Object} cfg - Configuration object.
|
|
18
|
+
* @property {string|string[]} cfg.domain - The allowed domain(s) to validate against.
|
|
19
|
+
* @property {string[]} [cfg.staticPath] - Optional list of static paths to validate.
|
|
20
|
+
*
|
|
21
|
+
* @returns {Object} Returns an object with the following structure:
|
|
22
|
+
* @property {boolean} verified - Whether the domain is verified.
|
|
23
|
+
* @property {string|null} domain - The detected domain from the request.
|
|
24
|
+
* @property {boolean} isStaticPath - Whether the request matches a static path.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* const result = domainValidator(req, {
|
|
28
|
+
* domain: ['example.com', 'sub.example.com'],
|
|
29
|
+
* staticPath: ['/assets/', '/static/']
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* if (result.verified) {
|
|
33
|
+
* console.log('Domain OK:', result.domain);
|
|
34
|
+
* }
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
function domainValidator(req, cfg) {
|
|
38
|
+
// Start Domain Verification
|
|
39
|
+
let domainStatus = { verified: false, domain: check_domain.get(req), isStaticPath: false };
|
|
40
|
+
|
|
41
|
+
// Path
|
|
42
|
+
var prepareUrlPath = req.url.split('/');
|
|
43
|
+
req.url_path = [];
|
|
44
|
+
for (const item in prepareUrlPath) {
|
|
45
|
+
if (Number(item) > 0) {
|
|
46
|
+
// Insert URL Path
|
|
47
|
+
req.url_path.push(prepareUrlPath[item].split(/[?#]/)[0]);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Is Obj
|
|
52
|
+
if (objType(cfg, 'object')) {
|
|
53
|
+
// Firebase Is Emulator
|
|
54
|
+
let firebaseIsEmulator = false;
|
|
55
|
+
if (isEmulator) {
|
|
56
|
+
firebaseIsEmulator = isEmulator();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Verify String
|
|
60
|
+
if (
|
|
61
|
+
(typeof cfg.domain === 'string' && cfg.domain === domainStatus.domain) ||
|
|
62
|
+
firebaseIsEmulator
|
|
63
|
+
) {
|
|
64
|
+
domainStatus.verified = true;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Array Domains
|
|
68
|
+
else if (Array.isArray(cfg.domain)) {
|
|
69
|
+
for (const item in cfg.domain) {
|
|
70
|
+
if (typeof cfg.domain[item] === 'string' && cfg.domain[item] === domainStatus.domain) {
|
|
71
|
+
domainStatus.verified = true;
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// is Valid
|
|
78
|
+
if (domainStatus.verified) {
|
|
79
|
+
// Validate Static Path
|
|
80
|
+
if (Array.isArray(cfg.staticPath)) {
|
|
81
|
+
for (const item in cfg.staticPath) {
|
|
82
|
+
if (
|
|
83
|
+
typeof cfg.staticPath[item] === 'string' &&
|
|
84
|
+
req.url.startsWith(cfg.staticPath[item])
|
|
85
|
+
) {
|
|
86
|
+
domainStatus.isStaticPath = true;
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Result
|
|
95
|
+
return domainStatus;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
module.exports = domainValidator;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @function domainValidator
|
|
3
|
+
*
|
|
4
|
+
* Validates the request's domain and optionally checks if the request is for a static path.
|
|
5
|
+
* This is useful for filtering requests by origin or allowing access from specific domains only.
|
|
6
|
+
* Also detects if Firebase is running in emulator mode, which bypasses domain validation.
|
|
7
|
+
*
|
|
8
|
+
* @param {Object} req - The Express request object.
|
|
9
|
+
* - `req.url`: Full URL path.
|
|
10
|
+
* - `req.headers`: Expected to contain 'host', 'x-forwarded-host', etc.
|
|
11
|
+
* @param {Object} cfg - Configuration object.
|
|
12
|
+
* @property {string|string[]} cfg.domain - The allowed domain(s) to validate against.
|
|
13
|
+
* @property {string[]} [cfg.staticPath] - Optional list of static paths to validate.
|
|
14
|
+
*
|
|
15
|
+
* @returns {Object} Returns an object with the following structure:
|
|
16
|
+
* @property {boolean} verified - Whether the domain is verified.
|
|
17
|
+
* @property {string|null} domain - The detected domain from the request.
|
|
18
|
+
* @property {boolean} isStaticPath - Whether the request matches a static path.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const result = domainValidator(req, {
|
|
22
|
+
* domain: ['example.com', 'sub.example.com'],
|
|
23
|
+
* staticPath: ['/assets/', '/static/']
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* if (result.verified) {
|
|
27
|
+
* console.log('Domain OK:', result.domain);
|
|
28
|
+
* }
|
|
29
|
+
*/
|
|
30
|
+
export default function domainValidator(req: Object, cfg: Object): Object;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import checkDomain from './check_domain.mjs';
|
|
2
|
+
import objType from '../get/objType.mjs';
|
|
3
|
+
import isEmulator from '../firebase/isEmulator.mjs';
|
|
4
|
+
/**
|
|
5
|
+
* @function domainValidator
|
|
6
|
+
*
|
|
7
|
+
* Validates the request's domain and optionally checks if the request is for a static path.
|
|
8
|
+
* This is useful for filtering requests by origin or allowing access from specific domains only.
|
|
9
|
+
* Also detects if Firebase is running in emulator mode, which bypasses domain validation.
|
|
10
|
+
*
|
|
11
|
+
* @param {Object} req - The Express request object.
|
|
12
|
+
* - `req.url`: Full URL path.
|
|
13
|
+
* - `req.headers`: Expected to contain 'host', 'x-forwarded-host', etc.
|
|
14
|
+
* @param {Object} cfg - Configuration object.
|
|
15
|
+
* @property {string|string[]} cfg.domain - The allowed domain(s) to validate against.
|
|
16
|
+
* @property {string[]} [cfg.staticPath] - Optional list of static paths to validate.
|
|
17
|
+
*
|
|
18
|
+
* @returns {Object} Returns an object with the following structure:
|
|
19
|
+
* @property {boolean} verified - Whether the domain is verified.
|
|
20
|
+
* @property {string|null} domain - The detected domain from the request.
|
|
21
|
+
* @property {boolean} isStaticPath - Whether the request matches a static path.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* const result = domainValidator(req, {
|
|
25
|
+
* domain: ['example.com', 'sub.example.com'],
|
|
26
|
+
* staticPath: ['/assets/', '/static/']
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* if (result.verified) {
|
|
30
|
+
* console.log('Domain OK:', result.domain);
|
|
31
|
+
* }
|
|
32
|
+
*/
|
|
33
|
+
export default function domainValidator(req, cfg) {
|
|
34
|
+
// Start Domain Verification
|
|
35
|
+
let domainStatus = { verified: false, domain: checkDomain.get(req), isStaticPath: false };
|
|
36
|
+
// Path
|
|
37
|
+
var prepareUrlPath = req.url.split('/');
|
|
38
|
+
req.url_path = [];
|
|
39
|
+
for (const item in prepareUrlPath) {
|
|
40
|
+
if (Number(item) > 0) {
|
|
41
|
+
// Insert URL Path
|
|
42
|
+
req.url_path.push(prepareUrlPath[item].split(/[?#]/)[0]);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
// Is Obj
|
|
46
|
+
if (objType(cfg, 'object')) {
|
|
47
|
+
// Firebase Is Emulator
|
|
48
|
+
let firebaseIsEmulator = false;
|
|
49
|
+
if (isEmulator) {
|
|
50
|
+
firebaseIsEmulator = isEmulator();
|
|
51
|
+
}
|
|
52
|
+
// Verify String
|
|
53
|
+
if ((typeof cfg.domain === 'string' && cfg.domain === domainStatus.domain) ||
|
|
54
|
+
firebaseIsEmulator) {
|
|
55
|
+
domainStatus.verified = true;
|
|
56
|
+
}
|
|
57
|
+
// Array Domains
|
|
58
|
+
else if (Array.isArray(cfg.domain)) {
|
|
59
|
+
for (const item in cfg.domain) {
|
|
60
|
+
if (typeof cfg.domain[item] === 'string' && cfg.domain[item] === domainStatus.domain) {
|
|
61
|
+
domainStatus.verified = true;
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
// is Valid
|
|
67
|
+
if (domainStatus.verified) {
|
|
68
|
+
// Validate Static Path
|
|
69
|
+
if (Array.isArray(cfg.staticPath)) {
|
|
70
|
+
for (const item in cfg.staticPath) {
|
|
71
|
+
if (typeof cfg.staticPath[item] === 'string' &&
|
|
72
|
+
req.url.startsWith(cfg.staticPath[item])) {
|
|
73
|
+
domainStatus.isStaticPath = true;
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
// Result
|
|
81
|
+
return domainStatus;
|
|
82
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @function errorsCallback
|
|
5
|
+
*
|
|
6
|
+
* Creates an Express error-handling middleware that filters and handles HTTP errors
|
|
7
|
+
* with a status code >= 400 using a custom callback function.
|
|
8
|
+
*
|
|
9
|
+
* If the error does not meet the criteria (e.g., status not defined or < 400), it delegates to the next middleware.
|
|
10
|
+
*
|
|
11
|
+
* @param {Function} callback - A function called when an error with valid status is detected.
|
|
12
|
+
* Receives parameters: (req, res, next, info)
|
|
13
|
+
* - `info.code`: HTTP status code.
|
|
14
|
+
* - `info.path`: `req.url` value.
|
|
15
|
+
* - `info.originalUrl`: `req.originalUrl` value.
|
|
16
|
+
* - `info.err`: The original error object.
|
|
17
|
+
*
|
|
18
|
+
* @returns {Function} Express middleware function: (err, req, res, next)
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* import errorsCallback from './errorsCallback.mjs';
|
|
22
|
+
*
|
|
23
|
+
* app.use(errorsCallback((req, res, next, info) => {
|
|
24
|
+
* res.status(info.code).json({
|
|
25
|
+
* error: true,
|
|
26
|
+
* message: info.err.message || 'Unknown error',
|
|
27
|
+
* path: info.path
|
|
28
|
+
* });
|
|
29
|
+
* }));
|
|
30
|
+
*/
|
|
31
|
+
function errorsCallback(callback) {
|
|
32
|
+
return function (err, req, res, next) {
|
|
33
|
+
// Err Code
|
|
34
|
+
const errCode = Number(err.status);
|
|
35
|
+
|
|
36
|
+
// Error
|
|
37
|
+
if (
|
|
38
|
+
(typeof err.status !== 'number' && typeof err.status !== 'string') ||
|
|
39
|
+
isNaN(errCode) ||
|
|
40
|
+
!isFinite(errCode) ||
|
|
41
|
+
errCode < 400
|
|
42
|
+
)
|
|
43
|
+
next();
|
|
44
|
+
// Nope
|
|
45
|
+
else
|
|
46
|
+
callback(req, res, next, {
|
|
47
|
+
code: err.status,
|
|
48
|
+
path: req.url,
|
|
49
|
+
originalUrl: req.originalUrl,
|
|
50
|
+
err: err,
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
module.exports = errorsCallback;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @function errorsCallback
|
|
3
|
+
*
|
|
4
|
+
* Creates an Express error-handling middleware that filters and handles HTTP errors
|
|
5
|
+
* with a status code >= 400 using a custom callback function.
|
|
6
|
+
*
|
|
7
|
+
* If the error does not meet the criteria (e.g., status not defined or < 400), it delegates to the next middleware.
|
|
8
|
+
*
|
|
9
|
+
* @param {Function} callback - A function called when an error with valid status is detected.
|
|
10
|
+
* Receives parameters: (req, res, next, info)
|
|
11
|
+
* - `info.code`: HTTP status code.
|
|
12
|
+
* - `info.path`: `req.url` value.
|
|
13
|
+
* - `info.originalUrl`: `req.originalUrl` value.
|
|
14
|
+
* - `info.err`: The original error object.
|
|
15
|
+
*
|
|
16
|
+
* @returns {Function} Express middleware function: (err, req, res, next)
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* import errorsCallback from './errorsCallback.mjs';
|
|
20
|
+
*
|
|
21
|
+
* app.use(errorsCallback((req, res, next, info) => {
|
|
22
|
+
* res.status(info.code).json({
|
|
23
|
+
* error: true,
|
|
24
|
+
* message: info.err.message || 'Unknown error',
|
|
25
|
+
* path: info.path
|
|
26
|
+
* });
|
|
27
|
+
* }));
|
|
28
|
+
*/
|
|
29
|
+
export default function errorsCallback(callback: Function): Function;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @function errorsCallback
|
|
3
|
+
*
|
|
4
|
+
* Creates an Express error-handling middleware that filters and handles HTTP errors
|
|
5
|
+
* with a status code >= 400 using a custom callback function.
|
|
6
|
+
*
|
|
7
|
+
* If the error does not meet the criteria (e.g., status not defined or < 400), it delegates to the next middleware.
|
|
8
|
+
*
|
|
9
|
+
* @param {Function} callback - A function called when an error with valid status is detected.
|
|
10
|
+
* Receives parameters: (req, res, next, info)
|
|
11
|
+
* - `info.code`: HTTP status code.
|
|
12
|
+
* - `info.path`: `req.url` value.
|
|
13
|
+
* - `info.originalUrl`: `req.originalUrl` value.
|
|
14
|
+
* - `info.err`: The original error object.
|
|
15
|
+
*
|
|
16
|
+
* @returns {Function} Express middleware function: (err, req, res, next)
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* import errorsCallback from './errorsCallback.mjs';
|
|
20
|
+
*
|
|
21
|
+
* app.use(errorsCallback((req, res, next, info) => {
|
|
22
|
+
* res.status(info.code).json({
|
|
23
|
+
* error: true,
|
|
24
|
+
* message: info.err.message || 'Unknown error',
|
|
25
|
+
* path: info.path
|
|
26
|
+
* });
|
|
27
|
+
* }));
|
|
28
|
+
*/
|
|
29
|
+
export default function errorsCallback(callback) {
|
|
30
|
+
return function (err, req, res, next) {
|
|
31
|
+
// Err Code
|
|
32
|
+
const errCode = Number(err.status);
|
|
33
|
+
// Error
|
|
34
|
+
if ((typeof err.status !== 'number' && typeof err.status !== 'string') ||
|
|
35
|
+
isNaN(errCode) ||
|
|
36
|
+
!isFinite(errCode) ||
|
|
37
|
+
errCode < 400)
|
|
38
|
+
next();
|
|
39
|
+
// Nope
|
|
40
|
+
else
|
|
41
|
+
callback(req, res, next, {
|
|
42
|
+
code: err.status,
|
|
43
|
+
path: req.url,
|
|
44
|
+
originalUrl: req.originalUrl,
|
|
45
|
+
err: err,
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var fetch = require('node-fetch');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Performs a `fetch` request using any provided arguments and returns a Promise
|
|
7
|
+
* that resolves with the parsed JSON response.
|
|
8
|
+
*
|
|
9
|
+
* This function acts as a wrapper around `fetch`, automatically parsing the
|
|
10
|
+
* response body as JSON and handling errors both at the network level and
|
|
11
|
+
* during JSON parsing.
|
|
12
|
+
*
|
|
13
|
+
* @function getJsonFetch
|
|
14
|
+
* @param {...any} arguments - All arguments are forwarded directly to the `fetch` function.
|
|
15
|
+
* Common usage is `getJsonFetch(url, options)`.
|
|
16
|
+
*
|
|
17
|
+
* @returns {Promise<any>} A Promise that resolves with the parsed JSON data
|
|
18
|
+
* if the request succeeds and the response is valid JSON,
|
|
19
|
+
* or rejects with an error if the request or parsing fails.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* getJsonFetch('https://api.example.com/data')
|
|
23
|
+
* .then(data => console.log(data))
|
|
24
|
+
* .catch(err => console.error('Fetch error:', err));
|
|
25
|
+
*/
|
|
26
|
+
function getJsonFetch() {
|
|
27
|
+
const tinyArgs = arguments;
|
|
28
|
+
return new Promise((resolve, reject) =>
|
|
29
|
+
fetch
|
|
30
|
+
.apply(fetch, tinyArgs)
|
|
31
|
+
.then((response) => {
|
|
32
|
+
// Get Response
|
|
33
|
+
response
|
|
34
|
+
.json()
|
|
35
|
+
.then((data) => {
|
|
36
|
+
resolve(data);
|
|
37
|
+
})
|
|
38
|
+
.catch((err) => {
|
|
39
|
+
reject(err);
|
|
40
|
+
});
|
|
41
|
+
})
|
|
42
|
+
.catch((err) => {
|
|
43
|
+
reject(err);
|
|
44
|
+
}),
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
module.exports = getJsonFetch;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Performs a `fetch` request using any provided arguments and returns a Promise
|
|
3
|
+
* that resolves with the parsed JSON response.
|
|
4
|
+
*
|
|
5
|
+
* This function acts as a wrapper around `fetch`, automatically parsing the
|
|
6
|
+
* response body as JSON and handling errors both at the network level and
|
|
7
|
+
* during JSON parsing.
|
|
8
|
+
*
|
|
9
|
+
* @function getJsonFetch
|
|
10
|
+
* @param {...any} arguments - All arguments are forwarded directly to the `fetch` function.
|
|
11
|
+
* Common usage is `getJsonFetch(url, options)`.
|
|
12
|
+
*
|
|
13
|
+
* @returns {Promise<any>} A Promise that resolves with the parsed JSON data
|
|
14
|
+
* if the request succeeds and the response is valid JSON,
|
|
15
|
+
* or rejects with an error if the request or parsing fails.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* getJsonFetch('https://api.example.com/data')
|
|
19
|
+
* .then(data => console.log(data))
|
|
20
|
+
* .catch(err => console.error('Fetch error:', err));
|
|
21
|
+
*/
|
|
22
|
+
export default function getJsonFetch(...args: any[]): Promise<any>;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import fetch from 'node-fetch';
|
|
2
|
+
/**
|
|
3
|
+
* Performs a `fetch` request using any provided arguments and returns a Promise
|
|
4
|
+
* that resolves with the parsed JSON response.
|
|
5
|
+
*
|
|
6
|
+
* This function acts as a wrapper around `fetch`, automatically parsing the
|
|
7
|
+
* response body as JSON and handling errors both at the network level and
|
|
8
|
+
* during JSON parsing.
|
|
9
|
+
*
|
|
10
|
+
* @function getJsonFetch
|
|
11
|
+
* @param {...any} arguments - All arguments are forwarded directly to the `fetch` function.
|
|
12
|
+
* Common usage is `getJsonFetch(url, options)`.
|
|
13
|
+
*
|
|
14
|
+
* @returns {Promise<any>} A Promise that resolves with the parsed JSON data
|
|
15
|
+
* if the request succeeds and the response is valid JSON,
|
|
16
|
+
* or rejects with an error if the request or parsing fails.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* getJsonFetch('https://api.example.com/data')
|
|
20
|
+
* .then(data => console.log(data))
|
|
21
|
+
* .catch(err => console.error('Fetch error:', err));
|
|
22
|
+
*/
|
|
23
|
+
export default function getJsonFetch() {
|
|
24
|
+
const tinyArgs = arguments;
|
|
25
|
+
return new Promise((resolve, reject) => fetch
|
|
26
|
+
.apply(fetch, tinyArgs)
|
|
27
|
+
.then((response) => {
|
|
28
|
+
// Get Response
|
|
29
|
+
response
|
|
30
|
+
.json()
|
|
31
|
+
.then((data) => {
|
|
32
|
+
resolve(data);
|
|
33
|
+
})
|
|
34
|
+
.catch((err) => {
|
|
35
|
+
reject(err);
|
|
36
|
+
});
|
|
37
|
+
})
|
|
38
|
+
.catch((err) => {
|
|
39
|
+
reject(err);
|
|
40
|
+
}));
|
|
41
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var fetch = require('node-fetch');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Performs a `fetch` request using any provided arguments and returns a Promise
|
|
7
|
+
* that resolves with the raw text response.
|
|
8
|
+
*
|
|
9
|
+
* This function is a wrapper around `fetch`, automatically extracting the response body
|
|
10
|
+
* as plain text. It handles both network errors and issues during text parsing.
|
|
11
|
+
*
|
|
12
|
+
* @function getTextFetch
|
|
13
|
+
* @param {...any} arguments - All arguments are forwarded directly to the `fetch` function.
|
|
14
|
+
* Typically used as `getTextFetch(url, options)`.
|
|
15
|
+
*
|
|
16
|
+
* @returns {Promise<string>} A Promise that resolves with the response text
|
|
17
|
+
* if the request is successful and parsing succeeds,
|
|
18
|
+
* or rejects with an error if the request or text conversion fails.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* getTextFetch('https://example.com/page')
|
|
22
|
+
* .then(text => console.log(text))
|
|
23
|
+
* .catch(err => console.error('Fetch error:', err));
|
|
24
|
+
*/
|
|
25
|
+
function getTextFetch() {
|
|
26
|
+
const tinyArgs = arguments;
|
|
27
|
+
return new Promise((resolve, reject) =>
|
|
28
|
+
fetch
|
|
29
|
+
.apply(fetch, tinyArgs)
|
|
30
|
+
.then((response) => {
|
|
31
|
+
// Get Response
|
|
32
|
+
response
|
|
33
|
+
.text()
|
|
34
|
+
.then((data) => {
|
|
35
|
+
resolve(data);
|
|
36
|
+
})
|
|
37
|
+
.catch((err) => {
|
|
38
|
+
reject(err);
|
|
39
|
+
});
|
|
40
|
+
})
|
|
41
|
+
.catch((err) => {
|
|
42
|
+
reject(err);
|
|
43
|
+
}),
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
module.exports = getTextFetch;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Performs a `fetch` request using any provided arguments and returns a Promise
|
|
3
|
+
* that resolves with the raw text response.
|
|
4
|
+
*
|
|
5
|
+
* This function is a wrapper around `fetch`, automatically extracting the response body
|
|
6
|
+
* as plain text. It handles both network errors and issues during text parsing.
|
|
7
|
+
*
|
|
8
|
+
* @function getTextFetch
|
|
9
|
+
* @param {...any} arguments - All arguments are forwarded directly to the `fetch` function.
|
|
10
|
+
* Typically used as `getTextFetch(url, options)`.
|
|
11
|
+
*
|
|
12
|
+
* @returns {Promise<string>} A Promise that resolves with the response text
|
|
13
|
+
* if the request is successful and parsing succeeds,
|
|
14
|
+
* or rejects with an error if the request or text conversion fails.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* getTextFetch('https://example.com/page')
|
|
18
|
+
* .then(text => console.log(text))
|
|
19
|
+
* .catch(err => console.error('Fetch error:', err));
|
|
20
|
+
*/
|
|
21
|
+
export default function getTextFetch(...args: any[]): Promise<string>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import fetch from 'node-fetch';
|
|
2
|
+
/**
|
|
3
|
+
* Performs a `fetch` request using any provided arguments and returns a Promise
|
|
4
|
+
* that resolves with the raw text response.
|
|
5
|
+
*
|
|
6
|
+
* This function is a wrapper around `fetch`, automatically extracting the response body
|
|
7
|
+
* as plain text. It handles both network errors and issues during text parsing.
|
|
8
|
+
*
|
|
9
|
+
* @function getTextFetch
|
|
10
|
+
* @param {...any} arguments - All arguments are forwarded directly to the `fetch` function.
|
|
11
|
+
* Typically used as `getTextFetch(url, options)`.
|
|
12
|
+
*
|
|
13
|
+
* @returns {Promise<string>} A Promise that resolves with the response text
|
|
14
|
+
* if the request is successful and parsing succeeds,
|
|
15
|
+
* or rejects with an error if the request or text conversion fails.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* getTextFetch('https://example.com/page')
|
|
19
|
+
* .then(text => console.log(text))
|
|
20
|
+
* .catch(err => console.error('Fetch error:', err));
|
|
21
|
+
*/
|
|
22
|
+
export default function getTextFetch() {
|
|
23
|
+
const tinyArgs = arguments;
|
|
24
|
+
return new Promise((resolve, reject) => fetch
|
|
25
|
+
.apply(fetch, tinyArgs)
|
|
26
|
+
.then((response) => {
|
|
27
|
+
// Get Response
|
|
28
|
+
response
|
|
29
|
+
.text()
|
|
30
|
+
.then((data) => {
|
|
31
|
+
resolve(data);
|
|
32
|
+
})
|
|
33
|
+
.catch((err) => {
|
|
34
|
+
reject(err);
|
|
35
|
+
});
|
|
36
|
+
})
|
|
37
|
+
.catch((err) => {
|
|
38
|
+
reject(err);
|
|
39
|
+
}));
|
|
40
|
+
}
|