Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
feat(server): beta.11 - fastify adapter (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiisol authored Aug 24, 2023
1 parent c6a7e89 commit 4928d6e
Show file tree
Hide file tree
Showing 33 changed files with 1,352 additions and 65 deletions.
9 changes: 4 additions & 5 deletions server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
## Installation
Main dependencies
```
npm install @decorators/di --save
npm install @decorators/server --save
npm install @decorators/server @decorators/di --save
```
And adapter specific imports
Adapter specific imports
```
npm install express --save
npm install express body-parser --save
```
Or
```
npm install fastify --save
npm install fastify @fastify/cookie @fastify/static @fastify/view --save
```

## Example
Expand Down
5 changes: 4 additions & 1 deletion server/example/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { APP_VERSION, GLOBAL_PIPE, Module } from '@server';
import { ExpressAdapter } from '@server/express';
import { FastifyAdapter } from '@server/fastify';
import { HttpModule } from '@server/http';
import { SwaggerModule } from '@server/swagger';

Expand All @@ -8,7 +9,9 @@ import { ServerPipe } from './pipes';

@Module({
modules: [
HttpModule.create(ExpressAdapter),
HttpModule.create(
process.env.USE_FASTIFY ? FastifyAdapter : ExpressAdapter,
),
SwaggerModule.forRoot({
description: 'Decorators Example App',
title: '@decorators/server',
Expand Down
16 changes: 13 additions & 3 deletions server/example/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as FastifyView from '@fastify/view';
import { Application } from '@server';
import { HttpModule } from '@server/http';
import { json } from 'body-parser';
Expand All @@ -9,9 +10,18 @@ async function bootstrap() {
const app = await Application.create(AppModule);
const module = await app.inject<HttpModule>(HttpModule);

module.set('view engine', 'ejs');
module.set('views', join(__dirname, 'views'));
module.use(json());
if (process.env.USE_FASTIFY) {
module.use(FastifyView, {
engine: {
ejs: require('ejs'),
},
root: join(__dirname, 'views'),
});
} else {
module.set('view engine', 'ejs');
module.set('views', join(__dirname, 'views'));
module.use(json());
}

await module.listen(3000);
console.info('Server is running on port 3000');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { AppModule } from '../src/app.module';
})
class TestModule { }

describe('App Version', () => {
describe('Express :: App Version', () => {
let app: Application;
let module: HttpModule;

Expand Down
34 changes: 34 additions & 0 deletions server/integration/core/app-version/test/fastify.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Application, HttpStatus, Module } from '@server';
import { FastifyAdapter } from '@server/fastify';
import { HttpModule } from '@server/http';
import * as request from 'supertest';

import { AppModule } from '../src/app.module';

@Module({
modules: [
HttpModule.create(FastifyAdapter),
AppModule,
],
})
class TestModule { }

describe('Fastify :: App Version', () => {
let app: Application;
let module: HttpModule;

beforeEach(async () => {
app = await Application.create(TestModule);
module = await app.inject<HttpModule>(HttpModule);

await module.listen();
});

afterEach(() => module.close());

it('registers `get` request with app version prefix', async () => {
return request(module.getHttpServer())
.get('/app-version/get')
.expect(HttpStatus.OK);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { AppModule } from '../src/app.module';
})
class TestModule { }

describe('Custom Decorators', () => {
describe('Express :: Custom Decorators', () => {
let app: Application;
let module: HttpModule;

Expand Down
44 changes: 44 additions & 0 deletions server/integration/core/custom-decorators/test/fastify.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Application, Module, Reflector } from '@server';
import { FastifyAdapter } from '@server/fastify';
import { HttpModule } from '@server/http';
import * as request from 'supertest';

import { AppModule } from '../src/app.module';

@Module({
modules: [
HttpModule.create(FastifyAdapter),
AppModule,
],
})
class TestModule { }

describe('Fastify :: Custom Decorators', () => {
let app: Application;
let module: HttpModule;

beforeEach(async () => {
app = await Application.create(TestModule);
module = await app.inject<HttpModule>(HttpModule);

await module.listen();
});

afterEach(() => module.close());

it('checks availability of reflector', async () => {
expect(await app.inject(Reflector)).toBeDefined();
});

it('decorates `get` request and its params', async () => {
return request(module.getHttpServer())
.get('/?param=decorated')
.expect('decorated');
});

it('throws error during `get` request', async () => {
return request(module.getHttpServer())
.get('/?param=failure')
.expect(({ body }) => expect(body.message).toEqual('decorated-error'));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Sequence } from '../src/sequence';
})
class TestModule { }

describe('Pipes', () => {
describe('Express :: Pipes', () => {
let app: Application;
let module: HttpModule;
let seq: Sequence;
Expand Down
71 changes: 71 additions & 0 deletions server/integration/core/pipes/test/fastify.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Application, Module } from '@server';
import { FastifyAdapter } from '@server/fastify';
import { HttpModule } from '@server/http';
import * as request from 'supertest';

import { AppModule } from '../src/app.module';
import { Sequence } from '../src/sequence';

@Module({
modules: [
HttpModule.create(FastifyAdapter),
AppModule,
],
})
class TestModule { }

describe('Fastify :: Pipes', () => {
let app: Application;
let module: HttpModule;
let seq: Sequence;

beforeEach(async () => {
app = await Application.create(TestModule);
module = await app.inject<HttpModule>(HttpModule);
seq = await app.inject<Sequence>(Sequence);

jest.spyOn(seq, 'push');

await module.listen();
});

afterEach(() => module.close());

it('executes pipes', async () => {
return request(module.getHttpServer())
.get('/')
.expect(() => {
expect(seq.push).toBeCalledWith('server');
expect(seq.push).toBeCalledWith('controller');
expect(seq.push).toBeCalledWith('method');
expect(seq.push).toBeCalledWith('method');
expect(seq.push).toBeCalledWith('controller');
expect(seq.push).toBeCalledWith('server');
});
});

it('executes pipes with method error', async () => {
return request(module.getHttpServer())
.get('/with-method-error')
.expect((res) => {
expect(res.body.message).toBe('method-error');
expect(seq.push).toBeCalledWith('server');
expect(seq.push).toBeCalledWith('controller');
expect(seq.push).toBeCalledWith('method');
expect(seq.push).toBeCalledWith('method');
expect(seq.push).toBeCalledWith('controller');
expect(seq.push).toBeCalledWith('server');
});
});

it('executes pipes with pipe error', async () => {
return request(module.getHttpServer())
.get('/with-pipe-error')
.expect((res) => {
expect(res.body.message).toBe('pipe-error');
expect(seq.push).toBeCalledWith('server');
expect(seq.push).toBeCalledWith('controller');
expect(seq.push).toBeCalledWith('server');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { AppModule } from '../src/app.module';
})
class TestModule { }

describe('Scopes', () => {
describe('Express :: Scopes', () => {
let app: Application;

beforeEach(async () => {
Expand Down
25 changes: 25 additions & 0 deletions server/integration/core/scopes/test/fastify.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Application, Module } from '@server';
import { FastifyAdapter } from '@server/fastify';
import { HttpModule } from '@server/http';

import { AppModule } from '../src/app.module';

@Module({
modules: [
HttpModule.create(FastifyAdapter),
AppModule,
],
})
class TestModule { }

describe('Fastify :: Scopes', () => {
let app: Application;

beforeEach(async () => {
app = await Application.create(TestModule);
});

it('creates app without errors', () => {
expect(app).toBeDefined();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { AppModule } from '../src/app.module';
})
class TestModule { }

describe('Metadata Scanner', () => {
describe('Express :: Metadata Scanner', () => {
let app: Application;
let scanner: MetadataScanner;

Expand All @@ -33,7 +33,7 @@ describe('Metadata Scanner', () => {
methodName: 'post',
})]),
type: 'post',
url: '',
url: '/',
})]));
});
});
39 changes: 39 additions & 0 deletions server/integration/http/metadata-scanner/test/fastify.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Application, Module } from '@server';
import { FastifyAdapter } from '@server/fastify';
import { HttpModule } from '@server/http';
import { MetadataScanner } from '@server/http';

import { AppModule } from '../src/app.module';

@Module({
modules: [
HttpModule.create(FastifyAdapter),
AppModule,
],
})
class TestModule { }

describe('Fastify :: Metadata Scanner', () => {
let app: Application;
let scanner: MetadataScanner;

beforeEach(async () => {
app = await Application.create(TestModule);
scanner = await app.inject<MetadataScanner>(MetadataScanner);
});

it('provides access to the metadata', () => {
const routesMetadata = scanner.scan();

expect(routesMetadata).toEqual(expect.arrayContaining([expect.objectContaining({
methodName: 'post',
params: expect.arrayContaining([expect.objectContaining({
argName: 'body',
index: 0,
methodName: 'post',
})]),
type: 'post',
url: '/',
})]));
});
});
6 changes: 5 additions & 1 deletion server/integration/http/params/src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ export class AppController {
response(
@Response() res: object,
) {
return res['req']['url'];
if (res['req']) {
return res['req']['url'];
}

return res['request']['url'];
}

@Post('with-class-validator')
Expand Down
2 changes: 1 addition & 1 deletion server/integration/http/params/test/express.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { AppModule } from '../src/app.module';
})
class TestModule { }

describe('Express Params', () => {
describe('Express :: Params', () => {
let app: Application;
let module: HttpModule;

Expand Down
Loading

0 comments on commit 4928d6e

Please sign in to comment.