@alwatr/pre-handlers 9.2.1 → 9.3.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/dist/main.js +2 -2
- package/dist/main.js.map +1 -1
- package/package.json +5 -5
package/dist/main.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
/* 📦 @alwatr/pre-handlers v9.
|
|
1
|
+
/* 📦 @alwatr/pre-handlers v9.3.0 */
|
|
2
2
|
function x(B){let z=B?.split(" ");if(!z||z[0].toLowerCase()!=="bearer"||!z[1])return null;return z[1]}import{HttpStatusCodes as E}from"@alwatr/nanotron-api-server";async function Q(){this.logger_.logMethod?.("parseBodyAsJson");let B=await this.getBodyRaw();if(B.length===0){this.logger_.error("parseBodyAsJson","body_required"),this.serverResponse.statusCode=E.Error_Client_422_Unprocessable_Entity,this.serverResponse.replyErrorResponse({ok:!1,errorCode:"body_required",errorMessage:"Request body is required."});return}try{this.sharedMeta.body=JSON.parse(B.toString())}catch(z){this.logger_.error("parseBodyAsJson","invalid_body_json",z),this.serverResponse.statusCode=E.Error_Client_422_Unprocessable_Entity,this.serverResponse.replyErrorResponse({ok:!1,errorCode:"invalid_body_json",errorMessage:"Invalid JSON in request body."})}this.logger_.logProperty?.("body",this.sharedMeta.body)}import{HttpStatusCodes as L}from"@alwatr/nanotron-api-server";var X=(B)=>async function(){let g=x(this.headers.authorization);if(this.logger_.logMethodArgs?.("requireAccessToken",{userToken:g}),g===null){this.serverResponse.statusCode=L.Error_Client_401_Unauthorized,this.serverResponse.replyErrorResponse({ok:!1,errorCode:"authorization_required",errorMessage:"Authorization token required"});return}if(g!==B)this.serverResponse.statusCode=L.Error_Client_403_Forbidden,this.serverResponse.replyErrorResponse({ok:!1,errorCode:"access_denied",errorMessage:"Access denied, token is invalid!"})};export{X as requireAccessToken,Q as parseBodyAsJson,x as getAuthBearer};
|
|
3
3
|
|
|
4
|
-
//# debugId=
|
|
4
|
+
//# debugId=6104A60C56A782D064756E2164756E21
|
|
5
5
|
//# sourceMappingURL=main.js.map
|
package/dist/main.js.map
CHANGED
|
@@ -7,6 +7,6 @@
|
|
|
7
7
|
"import {HttpStatusCodes, type NanotronClientRequest} from '@alwatr/nanotron-api-server';\n\nimport {getAuthBearer} from '../lib/get-auth-bearer.js';\n\n/**\n * Middleware to require a valid access token for a Nanotron API request.\n *\n * This function checks the authorization header for a Bearer token and compares it to the provided access token.\n * If the token is missing or invalid, it sends an appropriate error response and prevents further handlers from executing.\n *\n * @param {string} accessToken - The valid access token to compare against.\n * @returns {Function} A middleware function for Nanotron API requests.\n *\n * @example\n * ```ts\n * nanotronApiServer.defineRoute({\n * method: 'POST',\n * url: 'secure-endpoint',\n * preHandlers: [requireAccessToken('mySecretToken')],\n * async handler() {\n * this.serverResponse.replyJson({\n * ok: true,\n * message: 'Access granted!',\n * });\n * },\n * });\n * ```\n */\nexport const requireAccessToken = (accessToken: string) =>\n async function requireAccessToken_(this: NanotronClientRequest): Promise<void> {\n const userToken = getAuthBearer(this.headers.authorization);\n this.logger_.logMethodArgs?.('requireAccessToken', {userToken});\n\n if (userToken === null) {\n this.serverResponse.statusCode = HttpStatusCodes.Error_Client_401_Unauthorized;\n this.serverResponse.replyErrorResponse({\n ok: false,\n errorCode: 'authorization_required',\n errorMessage: 'Authorization token required',\n });\n return;\n }\n\n if (userToken !== accessToken) {\n this.serverResponse.statusCode = HttpStatusCodes.Error_Client_403_Forbidden;\n this.serverResponse.replyErrorResponse({\n ok: false,\n errorCode: 'access_denied',\n errorMessage: 'Access denied, token is invalid!',\n });\n }\n };\n"
|
|
8
8
|
],
|
|
9
9
|
"mappings": ";AAmBO,SAAS,CAAa,CAAC,EAA6C,CACzE,IAAM,EAAO,GAAqB,MAAM,GAAG,EAC3C,GAAI,CAAC,GAAQ,EAAK,GAAG,YAAY,IAAM,UAAY,CAAC,EAAK,GACvD,OAAO,KAET,OAAO,EAAK,GCxBd,0BAAQ,oCAsBR,eAAsB,CAAe,EAA8E,CACjH,KAAK,QAAQ,YAAY,iBAAiB,EAC1C,IAAM,EAAa,MAAM,KAAK,WAAW,EAEzC,GAAI,EAAW,SAAW,EAAG,CAC3B,KAAK,QAAQ,MAAM,kBAAmB,eAAe,EACrD,KAAK,eAAe,WAAa,EAAgB,sCACjD,KAAK,eAAe,mBAAmB,CACrC,GAAI,GACJ,UAAW,gBACX,aAAc,2BAChB,CAAC,EACD,OAGF,GAAI,CACF,KAAK,WAAW,KAAO,KAAK,MAAM,EAAW,SAAS,CAAC,EAEzD,MAAO,EAAO,CACZ,KAAK,QAAQ,MAAM,kBAAmB,oBAAqB,CAAK,EAChE,KAAK,eAAe,WAAa,EAAgB,sCACjD,KAAK,eAAe,mBAAmB,CACrC,GAAI,GACJ,UAAW,oBACX,aAAc,+BAChB,CAAC,EAGH,KAAK,QAAQ,cAAc,OAAQ,KAAK,WAAW,IAAI,EClDzD,0BAAQ,oCA4BD,IAAM,EAAqB,CAAC,IACjC,cAAkC,EAA6C,CAC7E,IAAM,EAAY,EAAc,KAAK,QAAQ,aAAa,EAG1D,GAFA,KAAK,QAAQ,gBAAgB,qBAAsB,CAAC,WAAS,CAAC,EAE1D,IAAc,KAAM,CACtB,KAAK,eAAe,WAAa,EAAgB,8BACjD,KAAK,eAAe,mBAAmB,CACrC,GAAI,GACJ,UAAW,yBACX,aAAc,8BAChB,CAAC,EACD,OAGF,GAAI,IAAc,EAChB,KAAK,eAAe,WAAa,EAAgB,2BACjD,KAAK,eAAe,mBAAmB,CACrC,GAAI,GACJ,UAAW,gBACX,aAAc,kCAChB,CAAC",
|
|
10
|
-
"debugId": "
|
|
10
|
+
"debugId": "6104A60C56A782D064756E2164756E21",
|
|
11
11
|
"names": []
|
|
12
12
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alwatr/pre-handlers",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.3.0",
|
|
4
4
|
"description": "Functions that will run before processing every defined route.",
|
|
5
5
|
"license": "MPL-2.0",
|
|
6
6
|
"author": "S. Ali Mihandoost <ali.mihandoost@gmail.com> (https://ali.mihandoost.com)",
|
|
@@ -21,11 +21,11 @@
|
|
|
21
21
|
},
|
|
22
22
|
"sideEffects": false,
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@alwatr/nanotron-api-server": "9.
|
|
24
|
+
"@alwatr/nanotron-api-server": "9.3.0"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@alwatr/nano-build": "9.
|
|
28
|
-
"@alwatr/
|
|
27
|
+
"@alwatr/nano-build": "9.3.0",
|
|
28
|
+
"@alwatr/standard": "9.3.0",
|
|
29
29
|
"@alwatr/type-helper": "9.1.1",
|
|
30
30
|
"@types/node": "^24.12.2",
|
|
31
31
|
"typescript": "^6.0.2"
|
|
@@ -67,5 +67,5 @@
|
|
|
67
67
|
"server",
|
|
68
68
|
"typescript"
|
|
69
69
|
],
|
|
70
|
-
"gitHead": "
|
|
70
|
+
"gitHead": "adf6d486667eefee42dfc87938e3af96f87c4738"
|
|
71
71
|
}
|