@lenne.tech/nest-server 3.3.2 → 3.4.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lenne.tech/nest-server",
3
- "version": "3.3.2",
3
+ "version": "3.4.1",
4
4
  "description": "Modern, fast, powerful Node.js web framework in TypeScript based on Nest with a GraphQL API and a connection to MongoDB (or other databases).",
5
5
  "keywords": [
6
6
  "node",
@@ -86,6 +86,7 @@
86
86
  "fastify": "3.27.1",
87
87
  "graphql": "16.3.0",
88
88
  "graphql-subscriptions": "2.0.0",
89
+ "graphql-voyager": "1.0.0-rc.31",
89
90
  "grunt": "1.4.1",
90
91
  "grunt-bg-shell": "2.3.3",
91
92
  "grunt-contrib-clean": "2.0.0",
package/src/config.env.ts CHANGED
@@ -32,8 +32,11 @@ const config: { [env: string]: IServerOptions } = {
32
32
  },
33
33
  env: 'development',
34
34
  graphQl: {
35
- debug: true,
36
- introspection: true,
35
+ driver: {
36
+ debug: true,
37
+ introspection: true,
38
+ },
39
+ voyager: true,
37
40
  },
38
41
  jwt: {
39
42
  secret: 'SECRET_OR_PRIVATE_KEY_DEV',
@@ -79,8 +82,10 @@ const config: { [env: string]: IServerOptions } = {
79
82
  },
80
83
  env: 'productive',
81
84
  graphQl: {
82
- debug: false,
83
- introspection: true,
85
+ driver: {
86
+ debug: false,
87
+ introspection: true,
88
+ },
84
89
  },
85
90
  jwt: {
86
91
  secret: 'SECRET_OR_PRIVATE_KEY_PROD',
@@ -20,7 +20,23 @@ export interface IServerOptions {
20
20
  * see https://docs.nestjs.com/graphql/quick-start
21
21
  * and https://www.apollographql.com/docs/apollo-server/api/apollo-server/
22
22
  */
23
- graphQl?: ApolloDriverConfig;
23
+ graphQl?: {
24
+ /**
25
+ * Driver configuration for Apollo
26
+ */
27
+ driver?: ApolloDriverConfig;
28
+
29
+ /**
30
+ * Subscription authentication
31
+ */
32
+ enableSubscriptionAuth?: boolean;
33
+
34
+ /**
35
+ * Enable GraphQL Voyager
36
+ * https://github.com/APIs-guru/graphql-voyager
37
+ */
38
+ voyager?: boolean;
39
+ };
24
40
 
25
41
  /**
26
42
  * Configuration of JavaScript Web Token (JWT) module
@@ -21,7 +21,7 @@ export class CoreAuthModule {
21
21
  UserModule: Type<any>,
22
22
  UserService: Type<CoreAuthUserService>,
23
23
  options: JwtModuleOptions & {
24
- imports?: Array<Type<any> | DynamicModule | Promise<DynamicModule> | ForwardReference>[];
24
+ imports?: Array<Type<any> | DynamicModule | Promise<DynamicModule> | ForwardReference>;
25
25
  providers?: Provider[];
26
26
  }
27
27
  ): DynamicModule {
@@ -33,4 +33,8 @@ export class CoreAuthService {
33
33
  async validateUser(payload: JwtPayload): Promise<any> {
34
34
  return await this.userService.getViaEmail(payload.email);
35
35
  }
36
+
37
+ decodeJwt(token: string): JwtPayload {
38
+ return this.jwtService.decode(token) as JwtPayload;
39
+ }
36
40
  }
@@ -1,4 +1,4 @@
1
- import { DynamicModule, Global, Module } from '@nestjs/common';
1
+ import { DynamicModule, Global, Module, UnauthorizedException } from '@nestjs/common';
2
2
  import { APP_INTERCEPTOR, APP_PIPE } from '@nestjs/core';
3
3
  import { GraphQLModule } from '@nestjs/graphql';
4
4
  import { Config } from './core/common/helpers/config.helper';
@@ -34,23 +34,43 @@ export class CoreModule {
34
34
  * Dynamic module
35
35
  * see https://docs.nestjs.com/modules#dynamic-modules
36
36
  */
37
- static forRoot(options: Partial<IServerOptions>): DynamicModule {
37
+ static forRoot(AuthService: any, AuthModule: any, options: Partial<IServerOptions>): DynamicModule {
38
38
  // Process config
39
39
  const config: IServerOptions = Config.merge(
40
40
  {
41
41
  env: 'develop',
42
42
  graphQl: {
43
- autoSchemaFile: 'schema.gql',
44
- context: ({ req }) => ({ req }),
45
- installSubscriptionHandlers: true,
46
- subscriptions: {
47
- 'subscriptions-transport-ws': {
48
- onConnect: (connectionParams) => {
49
- // TODO: Handle Authorization
50
- const authToken = connectionParams.Authorization;
43
+ driver: {
44
+ imports: [AuthModule],
45
+ inject: [AuthService],
46
+ useFactory: async (authService: any) => ({
47
+ autoSchemaFile: 'schema.gql',
48
+ context: ({ req }) => ({ req }),
49
+ installSubscriptionHandlers: true,
50
+ subscriptions: {
51
+ 'subscriptions-transport-ws': {
52
+ onConnect: async (connectionParams) => {
53
+ if (config.graphQl.enableSubscriptionAuth) {
54
+ // get authToken from authorization header
55
+ const authToken: string =
56
+ 'Authorization' in connectionParams && connectionParams?.Authorization?.split(' ')[1];
57
+
58
+ if (authToken) {
59
+ // verify authToken/getJwtPayLoad
60
+ const payload = authService.decodeJwt(authToken);
61
+ const user = await authService.validateUser(payload);
62
+ // the user/jwtPayload object found will be available as context.currentUser/jwtPayload in your GraphQL resolvers
63
+ return { user: user, headers: connectionParams };
64
+ }
65
+
66
+ throw new UnauthorizedException();
67
+ }
68
+ },
69
+ },
51
70
  },
52
- },
71
+ }),
53
72
  },
73
+ enableSubscriptionAuth: true,
54
74
  },
55
75
  port: 3000,
56
76
  mongoose: {
@@ -107,7 +127,7 @@ export class CoreModule {
107
127
  module: CoreModule,
108
128
  imports: [
109
129
  MongooseModule.forRoot(config.mongoose.uri, config.mongoose.options),
110
- GraphQLModule.forRoot<ApolloDriverConfig>(Object.assign({ driver: ApolloDriver }, config.graphQl)),
130
+ GraphQLModule.forRootAsync<ApolloDriverConfig>(Object.assign({ driver: ApolloDriver }, config.graphQl.driver)),
111
131
  ],
112
132
  providers,
113
133
  exports: [ConfigService, EmailService, TemplateService, MailjetService],
package/src/main.ts CHANGED
@@ -2,6 +2,7 @@ import { NestFactory } from '@nestjs/core';
2
2
  import { NestExpressApplication } from '@nestjs/platform-express';
3
3
  import envConfig from './config.env';
4
4
  import { ServerModule } from './server/server.module';
5
+ import { express as voyagerMiddleware } from 'graphql-voyager/middleware';
5
6
 
6
7
  /**
7
8
  * Preparations for server start
@@ -23,6 +24,11 @@ async function bootstrap() {
23
24
  // Enable cors to allow requests from other domains
24
25
  server.enableCors();
25
26
 
27
+ // Activate GraphQL Voyager
28
+ if (envConfig.graphQl?.voyager) {
29
+ server.use('/voyager', voyagerMiddleware({ endpointUrl: '/graphql' }));
30
+ }
31
+
26
32
  // Start server on configured port
27
33
  await server.listen(envConfig.port);
28
34
  }
@@ -4,6 +4,7 @@ import { CoreModule } from '../core.module';
4
4
  import { AuthModule } from './modules/auth/auth.module';
5
5
  import { FileController } from './modules/file/file.controller';
6
6
  import { ServerController } from './server.controller';
7
+ import { CoreAuthService } from '../core/modules/auth/services/core-auth.service';
7
8
 
8
9
  /**
9
10
  * Server module (dynamic)
@@ -15,7 +16,7 @@ import { ServerController } from './server.controller';
15
16
  // Include modules
16
17
  imports: [
17
18
  // Include CoreModule for standard processes
18
- CoreModule.forRoot(envConfig),
19
+ CoreModule.forRoot(CoreAuthService, AuthModule.forRoot(envConfig.jwt), envConfig),
19
20
 
20
21
  // Include AuthModule for authorization handling,
21
22
  // which will also include UserModule