Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nestjs Vercel deploy fix #2

Merged
merged 8 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ HOST=https://example.com
PORT=3000
LOCAL=true
DRIVE_ROOT_DIR=Health-test
GOOGLE_API_KEY=xxxxxxxxxxxxxxxxxxx
SLACK_API_URL=https://slack.com/api
SLACK_CLIENT_ID=1234567890123.1234567890123
SLACK_CLIENT_SECRET=xxxxxxxxxxxxxxxxxxx
Expand Down
3 changes: 2 additions & 1 deletion nest-cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true
"deleteOutDir": true,
"assets": ["**/*.njk"]
}
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
"@nestjs/common": "^9.0.0",
"@nestjs/core": "^9.0.0",
"@nestjs/platform-express": "^9.0.0",
"@types/express-fileupload": "^1.4.1",
"@types/moment": "^2.13.0",
"axios": "^1.3.6",
"dotenv": "^16.0.3",
"express-fileupload": "^1.4.0",
"express-serve-static-core": "^0.1.1",
"googleapis": "^118.0.0",
"googleapis-common": "^6.0.4",
"moment": "^2.29.4",
Expand Down
42 changes: 33 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Controller, Get, Req, Res } from '@nestjs/common';
import { Request, Response } from 'express';
import type { Request, Response } from 'express-serve-static-core';
import { GoogleAuthService } from './modules/authentication/services/googleAuth.service';

@Controller()
Expand All @@ -14,7 +14,7 @@ export class AppController {
} catch (ex) {
requiredAuth = true;
}
res.render('views/pages/index.njk', {
res.render('pages/index.njk', {
authUrl: this.googleAuthService.generateAuthUrl(
await this.googleAuthService.getAuthClient(),
),
Expand Down
3 changes: 1 addition & 2 deletions src/common/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import * as path from 'path';
import path from 'path';

// @NOTE: If modifying these scopes, delete token.json.
export const SCOPES = [
Expand Down
32 changes: 18 additions & 14 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import { NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express';
import { join } from 'path';
import path, { join } from 'path';
import { AppModule } from './app.module';

import * as dotenv from 'dotenv';
import * as fs from 'fs';
import * as os from 'os';
import * as fileUpload from 'express-fileupload';
import * as nunjucks from 'nunjucks';
import dotenv from 'dotenv';
import fs from 'fs';
import os from 'os';
import fileUpload from 'express-fileupload';
import nunjucks from 'nunjucks';

dotenv.config();

const ROOT_DIR: string = join(__dirname, '..');
// @TODO: pass proper production flag here
const IS_PRODUCTION: boolean = process.env.LOCAL !== 'true';
const IS_LOCAL: boolean = process.env.LOCAL === 'true';

async function bootstrap() {
const options = {
key: fs.readFileSync(ROOT_DIR + '/localhost-key.pem'),
cert: fs.readFileSync(ROOT_DIR + '/localhost.pem'),
};
const app: NestExpressApplication = await NestFactory.create(AppModule, {
httpsOptions: options,
});
const app: NestExpressApplication = IS_LOCAL
? await NestFactory.create(AppModule, {
httpsOptions: {
key: fs.readFileSync(ROOT_DIR + '/localhost-key.pem'),
cert: fs.readFileSync(ROOT_DIR + '/localhost.pem'),
},
})
: await NestFactory.create(AppModule);

app.use(
fileUpload({
Expand All @@ -41,7 +44,7 @@ async function bootstrap() {
noCache: !IS_PRODUCTION,
};

nunjucks.configure(join(ROOT_DIR, IS_PRODUCTION ? 'dist' : 'src'), opts);
nunjucks.configure(path.resolve(__dirname, 'views'), opts);

app.enableCors();
app.set('trust proxy', 1);
Expand All @@ -51,4 +54,5 @@ async function bootstrap() {
console.log(`server running on ${port}`);
});
}

bootstrap();
4 changes: 2 additions & 2 deletions src/modules/authentication/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Controller, Get, Req, Res } from '@nestjs/common';
import { Request, Response } from 'express';
import { Request, Response } from 'express-serve-static-core';
import { GoogleAuthService } from '../services/googleAuth.service';
import { deleteFile } from '../../../services/utils/files.service';

Expand All @@ -26,7 +26,7 @@ export class AuthController {

@Get('/auth')
async goToAuth(@Res() res: Response) {
res.render('views/pages/authenticate/index.njk', {
res.render('pages/authenticate/index.njk', {
authUrl: this.googleAuthService.generateAuthUrl(
await this.googleAuthService.getAuthClient(),
),
Expand Down
10 changes: 5 additions & 5 deletions src/modules/authentication/services/googleAuth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { Inject, Injectable } from '@nestjs/common';
import { google } from 'googleapis';
import { OAuth2Client } from 'googleapis-common';
import { deleteFile } from '../../../services/utils/files.service';
import { Request, Response } from 'express';
import { Request, Response } from 'express-serve-static-core';
import { CREDENTIALS_PATH, SCOPES } from '../../../common/constants';

import * as fs from 'fs';
import * as url from 'url';
import * as os from 'os';
import * as path from 'path';
import fs from 'fs';
import url from 'url';
import os from 'os';
import path from 'path';

@Injectable()
export class GoogleAuthService {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/calendar/controllers/calendar.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TriggersService } from '../services/triggers.service';
import { GoogleAuthService } from '../../authentication/services/googleAuth.service';
import { EventsService } from '../services/events.service';
import { WatchService } from '../services/watch.service';
import { Request, Response } from 'express';
import { Request, Response } from 'express-serve-static-core';

@Controller()
export class CalendarController {
Expand Down Expand Up @@ -35,7 +35,7 @@ export class CalendarController {

this.watchService.watchCalendarEvents(authClient);

res.render('views/pages/slack/integrate.njk');
res.render('pages/slack/integrate.njk');
});
}
}
5 changes: 1 addition & 4 deletions src/modules/calendar/services/events.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { Injectable } from '@nestjs/common';
import { calendar_v3, google } from 'googleapis';

import * as moment from 'moment';
import moment from 'moment';

@Injectable()
export class EventsService {
Expand All @@ -14,9 +14,6 @@ export class EventsService {
const calendar = google.calendar({
version: 'v3',
auth,
params: {
key: process.env.GOOGLE_API_KEY,
},
});
const res = await calendar.events.list({
calendarId: 'primary',
Expand Down
6 changes: 0 additions & 6 deletions src/modules/calendar/services/watch.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ export class WatchService {
async watchCalendarEvents(auth) {
const calendar = google.calendar({
version: 'v3',
params: {
key: process.env.GOOGLE_API_KEY,
},
auth,
});
const channel = {
Expand All @@ -41,9 +38,6 @@ export class WatchService {
stopWatchingCalendarEvents(auth) {
const calendar = google.calendar({
version: 'v3',
params: {
key: process.env.GOOGLE_API_KEY,
},
auth,
});

Expand Down
4 changes: 2 additions & 2 deletions src/modules/drive/controllers/drive.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Controller, Get, Post, Req, Res } from '@nestjs/common';
import { UploadService } from '../services/upload.service';
import { Request, Response } from 'express';
import { Request, Response } from 'express-serve-static-core';
import { GoogleAuthService } from '../../authentication/services/googleAuth.service';

@Controller()
Expand All @@ -19,7 +19,7 @@ export class DriveController {
requiredAuth = true;
}

res.render('views/pages/drive/upload.njk', {
res.render('pages/drive/upload.njk', {
authUrl: this.googleAuthService.generateAuthUrl(
await this.googleAuthService.getAuthClient(),
),
Expand Down
7 changes: 2 additions & 5 deletions src/modules/drive/services/upload.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Injectable } from '@nestjs/common';
import { google } from 'googleapis';
import { commaSeparatedValues } from '../../../services/utils/strings.service';

import * as fs from 'fs';
import * as moment from 'moment';
import fs from 'fs';
import moment from 'moment';

@Injectable()
export class UploadService {
Expand All @@ -18,9 +18,6 @@ export class UploadService {

const service = google.drive({
version: 'v3',
params: {
key: process.env.GOOGLE_API_KEY,
},
auth: oauth2Client,
});

Expand Down
2 changes: 1 addition & 1 deletion test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import { AppModule } from './../src/app.module';

import * as request from 'supertest';
import request from 'supertest';

describe('AppController (e2e)', () => {
let app: INestApplication;
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2017",
Expand Down
Loading