Skip to content

Commit

Permalink
feat: add pre-handlers package
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammadhonarvar committed Oct 25, 2024
1 parent 2dedfab commit dae1459
Show file tree
Hide file tree
Showing 7 changed files with 807 additions and 0 deletions.
661 changes: 661 additions & 0 deletions packages/pre-handlers/LICENSE

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions packages/pre-handlers/README.md
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).
75 changes: 75 additions & 0 deletions packages/pre-handlers/package.json
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"
}
}
3 changes: 3 additions & 0 deletions packages/pre-handlers/src/logger.ts
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__);
5 changes: 5 additions & 0 deletions packages/pre-handlers/src/main.ts
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__);
36 changes: 36 additions & 0 deletions packages/pre-handlers/src/parse-body-as-json.ts
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});
}
12 changes: 12 additions & 0 deletions packages/pre-handlers/tsconfig.json
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"}],
}

0 comments on commit dae1459

Please sign in to comment.