Skip to content

Commit 27ed2c0

Browse files
authored
Merge pull request #32 from intentjs/feat/http-server
Feat/http server
2 parents d75240e + 66e40a0 commit 27ed2c0

234 files changed

Lines changed: 22516 additions & 1156 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ storage/**/*.log
2828
packages/**/*.d.ts
2929
packages/**/*.js
3030

31+
!packages/hyper-express/**/*.js
32+
!packages/hyper-express/**/*.d.ts
33+
3134
#yarn
3235
.pnp.*
3336
.yarn/*

.husky/pre-commit

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
npm run build

integrations/sample-app/.env.example

Lines changed: 0 additions & 53 deletions
This file was deleted.

integrations/sample-app/app/boot/sp/app.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-unused-vars */
2-
import {
3-
IntentApplication,
4-
IntentApplicationContext,
5-
ServiceProvider,
6-
} from '@intentjs/core';
2+
import { IntentApplicationContext, ServiceProvider } from '@intentjs/core';
3+
import { IntentController } from 'app/http/controllers/icon';
74
import { QueueJobs } from 'app/jobs/job';
85
import { UserDbRepository } from 'app/repositories/userDbRepository';
96
import { UserService } from 'app/services';
@@ -35,5 +32,5 @@ export class AppServiceProvider extends ServiceProvider {
3532
/**
3633
* Bootstrap any application service here.
3734
*/
38-
boot(app: IntentApplication | IntentApplicationContext) {}
35+
boot(app: IntentApplicationContext) {}
3936
}

integrations/sample-app/app/boot/sp/console.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
IntentApplication,
3-
IntentApplicationContext,
4-
ServiceProvider,
5-
} from '@intentjs/core';
1+
import { IntentApplicationContext, ServiceProvider } from '@intentjs/core';
62
import { TestCacheConsoleCommand } from 'app/console/cache';
73
import { GreetingCommand } from 'app/console/greeting';
84
import { TestLogConsoleCommand } from 'app/console/log';
@@ -27,5 +23,5 @@ export class ConsoleServiceProvider extends ServiceProvider {
2723
*
2824
*/
2925
// eslint-disable-next-line @typescript-eslint/no-unused-vars
30-
boot(app: IntentApplication | IntentApplicationContext) {}
26+
boot(app: IntentApplicationContext) {}
3127
}
Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import {
2-
Catch,
2+
ConfigService,
3+
ExecutionContext,
34
HttpException,
4-
HttpStatus,
55
IntentExceptionFilter,
6-
Request,
7-
Response,
86
Type,
9-
ValidationFailed,
107
} from '@intentjs/core';
118

12-
@Catch()
139
export class ApplicationExceptionFilter extends IntentExceptionFilter {
10+
constructor(private config: ConfigService) {
11+
super();
12+
}
13+
1414
doNotReport(): Array<Type<HttpException>> {
1515
return [];
1616
}
@@ -19,19 +19,7 @@ export class ApplicationExceptionFilter extends IntentExceptionFilter {
1919
return '*';
2020
}
2121

22-
handleHttp(exception: any, req: Request, res: Response) {
23-
if (exception instanceof ValidationFailed) {
24-
return res
25-
.status(422)
26-
.json({ message: 'validation failed', errors: exception.getErrors() });
27-
}
28-
29-
return res.status(this.getStatus(exception)).json(exception);
30-
}
31-
32-
getStatus(exception: any): HttpStatus {
33-
return exception instanceof HttpException
34-
? exception.getStatus()
35-
: HttpStatus.INTERNAL_SERVER_ERROR;
22+
handleHttp(context: ExecutionContext, exception: any) {
23+
return super.handleHttp(context, exception);
3624
}
3725
}
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Controller, Get, Req, Request } from '@intentjs/core';
1+
import { Controller, Get, Req } from '@intentjs/core';
22
import { UserService } from 'app/services';
33

44
@Controller()
@@ -7,7 +7,11 @@ export class UserController {
77

88
@Get()
99
async getHello(@Req() req: Request) {
10-
console.log(req.all());
11-
return this.service.getHello();
10+
return { hello: 'Intent' };
11+
}
12+
13+
@Get('hello')
14+
async getHello2(@Req() req: Request) {
15+
return { hello: 'Intent' };
1216
}
1317
}
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
import {
2+
Accepts,
3+
BufferBody,
4+
Controller,
5+
Dto,
6+
File,
7+
findProjectRoot,
8+
Get,
9+
Header,
10+
Host,
11+
IP,
12+
Param,
13+
Post,
14+
Query,
15+
Req,
16+
Res,
17+
Response,
18+
StreamableFile,
19+
UseGuards,
20+
UserAgent,
21+
Validate,
22+
} from '@intentjs/core';
23+
import { CustomGuard } from '../guards/custom';
24+
import { Request, UploadedFile } from '@intentjs/hyper-express';
25+
import { createReadStream } from 'fs';
26+
import { join } from 'path';
27+
import { LoginDto } from 'app/validators/auth';
28+
29+
@Controller('/icon')
30+
@UseGuards(CustomGuard)
31+
export class IntentController {
32+
public service: any;
33+
34+
constructor() {
35+
this.service = null;
36+
}
37+
38+
@Get('/:name')
39+
@UseGuards(CustomGuard)
40+
async getHello(
41+
// @Req() req: Request,
42+
// @Param('name') name: string,
43+
// @Query() query: Record<string, any>,
44+
// @Query('b') bQuery: string,
45+
// @Param() pathParams: string,
46+
// @Host() hostname: string,
47+
// @IP() ips: string,
48+
// @Accepts() accepts: string,
49+
// @BufferBody() bufferBody: Promise<Buffer>,
50+
// @UserAgent() userAgent: string,
51+
// @Header() headers: Record<string, any>,
52+
@Res() res: Response,
53+
) {
54+
// console.log(
55+
// 'query ==> ',
56+
// query,
57+
// 'bQuyery ==> ',
58+
// bQuery,
59+
// 'name ===> ',
60+
// name,
61+
// bufferBody,
62+
// pathParams,
63+
// 'hostname===> ',
64+
// hostname,
65+
// 'accepts ===> ',
66+
// accepts,
67+
// 'ips ===> ',
68+
// ips,
69+
// 'inside get method',
70+
// 'user agent ===> ',
71+
// userAgent,
72+
// );
73+
// console.log('all headers ===> ', headers);
74+
// throw new Error('hello there');
75+
// return { hello: 'world' };
76+
// const readStream = createReadStream(
77+
// join(findProjectRoot(), 'storage/uploads/sample-image.jpg'),
78+
// );
79+
// return new StreamableFile(readStream, { type: 'image/jpeg' });
80+
}
81+
82+
@Get('/plain-with-query-param')
83+
@UseGuards(CustomGuard)
84+
async plainWithQueryParam(@Req() req: Request) {
85+
console.log(req);
86+
return { hello: 'world' };
87+
}
88+
89+
@Get('/:id')
90+
@UseGuards(CustomGuard)
91+
async plainWithPathParam(@Req() req: Request) {
92+
console.log(req);
93+
return { hello: 'world' };
94+
}
95+
96+
@Post('/json')
97+
@Validate(LoginDto)
98+
async postJson(
99+
@Req() req: Request,
100+
@Dto() dto: LoginDto,
101+
@Param('name') name: string,
102+
@Query() query: Record<string, any>,
103+
@Query('b') bQuery: string,
104+
@Param() pathParams: string,
105+
@Host() hostname: string,
106+
@IP() ips: string,
107+
@Accepts() accepts: string,
108+
@BufferBody() bufferBody: Promise<Buffer>,
109+
@UserAgent() userAgent: string,
110+
@Header() headers: Record<string, any>,
111+
@File('file') file: UploadedFile,
112+
@Res() res: Response,
113+
) {
114+
// console.log(
115+
// 'query ==> ',
116+
// query,
117+
// 'bQuyery ==> ',
118+
// bQuery,
119+
// 'name ===> ',
120+
// name,
121+
// bufferBody,
122+
// pathParams,
123+
// 'hostname===> ',
124+
// hostname,
125+
// 'accepts ===> ',
126+
// accepts,
127+
// 'ips ===> ',
128+
// ips,
129+
// 'inside get method',
130+
// 'user agent ===> ',
131+
// userAgent,
132+
// );
133+
134+
// console.log('all headers ===> ', headers);
135+
console.log('uploaded files ==> ', req.dto(), dto);
136+
137+
const readStream = createReadStream(
138+
join(findProjectRoot(), 'storage/uploads/sample-image.jpg'),
139+
);
140+
141+
return new StreamableFile(readStream, { type: 'image/jpeg' });
142+
143+
return { hello: 'world from POST /json' };
144+
}
145+
146+
@Post('/multipart')
147+
@UseGuards(CustomGuard)
148+
async postHello(@Req() req: Request) {
149+
return { hello: 'world' };
150+
}
151+
152+
@Post('/form-data')
153+
@UseGuards(CustomGuard)
154+
async postFormData(@Req() req: Request) {
155+
return { hello: 'world' };
156+
}
157+
158+
@Post('/binary')
159+
@UseGuards(CustomGuard)
160+
async postBinary(@Req() req: Request) {
161+
return { hello: 'world' };
162+
}
163+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { createParamDecorator, ExecutionContext } from '@intentjs/core';
2+
3+
export const CustomParam = createParamDecorator(
4+
(data: any, ctx: ExecutionContext, argIndex: number) => {
5+
return 'data from custom decorator param';
6+
},
7+
);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { ExecutionContext, Injectable, IntentGuard } from '@intentjs/core';
2+
3+
@Injectable()
4+
export class CustomGuard extends IntentGuard {
5+
async guard(ctx: ExecutionContext): Promise<boolean> {
6+
return true;
7+
}
8+
}

0 commit comments

Comments
 (0)