-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2dedfab
commit dae1459
Showing
7 changed files
with
807 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Pre Handlers | ||
|
||
## Sponsors | ||
|
||
The following companies, organizations, and individuals support Nanotron ongoing maintenance and development. Become a Sponsor to get your logo on our README and website. | ||
|
||
[![Exir Studio](https://avatars.githubusercontent.com/u/181194967?s=200&v=4)](https://exirstudio.com) | ||
|
||
### Contributing | ||
|
||
Contributions are welcome! Please read our [contribution guidelines](https://github.com/Alwatr/.github/blob/next/CONTRIBUTING.md) before submitting a pull request. | ||
|
||
### License | ||
|
||
This project is licensed under the [AGPL-3.0 License](LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
{ | ||
"name": "@alwatr/pre-handlers", | ||
"version": "1.0.0", | ||
"description": "Functions that will run before processing every defined route.", | ||
"author": "S. Ali Mihandoost <[email protected]>", | ||
"keywords": [ | ||
"server", | ||
"nanoservice", | ||
"api", | ||
"typescript", | ||
"esm", | ||
"route-pre-handlers", | ||
"handlers", | ||
"middleware", | ||
"alwatr" | ||
], | ||
"type": "module", | ||
"main": "./dist/main.cjs", | ||
"module": "./dist/main.mjs", | ||
"types": "./dist/main.d.ts", | ||
"exports": { | ||
".": { | ||
"types": "./dist/main.d.ts", | ||
"import": "./dist/main.mjs", | ||
"require": "./dist/main.cjs" | ||
} | ||
}, | ||
"license": "AGPL-3.0-only", | ||
"files": [ | ||
"**/*.{js,mjs,cjs,map,d.ts,html,md}", | ||
"!demo/**/*" | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/Alwatr/pre-handlers", | ||
"directory": "packages/pre-handlers" | ||
}, | ||
"homepage": "https://github.com/Alwatr/pre-handlers#readme", | ||
"bugs": { | ||
"url": "https://github.com/Alwatr/pre-handlers/issues" | ||
}, | ||
"prettier": "@alwatr/prettier-config", | ||
"scripts": { | ||
"b": "yarn run build", | ||
"t": "yarn run test", | ||
"w": "yarn run watch", | ||
"c": "yarn run clean", | ||
"cb": "yarn run clean && yarn run build", | ||
"d": "yarn run build:es && yarn node --enable-source-maps --trace-warnings", | ||
"build": "yarn run build:ts & yarn run build:es", | ||
"build:es": "nano-build --preset=module", | ||
"build:ts": "tsc --build", | ||
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --enable-source-maps --experimental-vm-modules\" jest", | ||
"watch": "yarn run watch:ts & yarn run watch:es", | ||
"watch:es": "yarn run build:es --watch", | ||
"watch:ts": "yarn run build:ts --watch --preserveWatchOutput", | ||
"clean": "rm -rfv dist *.tsbuildinfo" | ||
}, | ||
"dependencies": { | ||
"@alwatr/nanolib": "^1.3.0", | ||
"@alwatr/nanotron-api-server": "workspace:^" | ||
}, | ||
"devDependencies": { | ||
"@alwatr/nano-build": "^2.0.2", | ||
"@alwatr/prettier-config": "^1.0.6", | ||
"@alwatr/tsconfig-base": "^1.3.2", | ||
"@alwatr/type-helper": "^2.0.2", | ||
"@types/node": "^22.7.5", | ||
"jest": "^29.7.0", | ||
"typescript": "^5.6.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import {createLogger} from '@alwatr/nanolib'; | ||
|
||
export const logger = /* #__PURE__ */ createLogger(__package_name__); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import {packageTracer} from '@alwatr/nanolib'; | ||
|
||
export * from './parse-body-as-json.js'; | ||
|
||
__dev_mode__: packageTracer.add(__package_name__, __package_version__); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import {HttpStatusCodes} from '@alwatr/nanotron-api-server'; | ||
|
||
import {logger} from './logger.js'; | ||
|
||
import type {NanotronClientRequest} from '@alwatr/nanotron-api-server'; | ||
|
||
export async function parseBodyAsJson( | ||
this: NanotronClientRequest<{body: DictionaryOpt}>, | ||
): Promise<void> { | ||
const bodyBuffer = await this.getBodyRaw(); | ||
if (bodyBuffer.length === 0) { | ||
logger.error('parseBodyAsJson', 'body_required'); | ||
this.serverResponse.statusCode = HttpStatusCodes.Error_Client_422_Unprocessable_Entity; | ||
this.serverResponse.replyErrorResponse({ | ||
ok: false, | ||
errorCode: 'body_required', | ||
errorMessage: 'Request body is required.', | ||
}); | ||
return; | ||
} | ||
|
||
try { | ||
this.sharedMeta.body = JSON.parse(bodyBuffer.toString()) as DictionaryOpt; | ||
} | ||
catch (error) { | ||
logger.error('parseBodyAsJson', 'invalid_body_json', error); | ||
this.serverResponse.statusCode = HttpStatusCodes.Error_Client_422_Unprocessable_Entity; | ||
this.serverResponse.replyErrorResponse({ | ||
ok: false, | ||
errorCode: 'invalid_body_json', | ||
errorMessage: 'Invalid JSON in request body.', | ||
}); | ||
} | ||
|
||
logger.logMethodArgs?.('parseBodyAsJson', {body: this.sharedMeta.body}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"extends": "@alwatr/tsconfig-base/tsconfig.json", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"outDir": "dist", | ||
"emitDeclarationOnly": true, | ||
"composite": true, | ||
"types": ["node", "@alwatr/nano-build", "@alwatr/type-helper"] | ||
}, | ||
"include": ["src/**/*.ts"], | ||
"references": [{"path": "../api-server"}], | ||
} |