Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
✨ Expose data by removing secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Oct 23, 2020
1 parent d1e9e25 commit 93d82e1
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/modules/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Body, Controller, Headers, Ip, Post } from '@nestjs/common';
import { users } from '@prisma/client';
import { RateLimit } from 'nestjs-rate-limiter';
import { OmitSecrets } from 'src/modules/prisma/prisma.interface';
import { Expose } from 'src/modules/prisma/prisma.interface';
import { LoginDto, RegisterDto, ResendEmailVerificationDto } from './auth.dto';
import { AuthService } from './auth.service';

Expand Down Expand Up @@ -29,7 +29,7 @@ export class AuthController {
duration: 60,
errorMessage: 'Wait for 60 seconds before trying to create an account',
})
async register(@Body() data: RegisterDto): Promise<OmitSecrets<users>> {
async register(@Body() data: RegisterDto): Promise<Expose<users>> {
return this.authService.register(data);
}

Expand Down
4 changes: 2 additions & 2 deletions src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { ConfigService } from '@nestjs/config';
import { users } from '@prisma/client';
import { EmailService } from '../email/email.service';
import { OmitSecrets } from '../prisma/prisma.interface';
import { Expose } from '../prisma/prisma.interface';
import { PrismaService } from '../prisma/prisma.service';
import { UsersService } from '../user/user.service';
import { RegisterDto } from './auth.dto';
Expand Down Expand Up @@ -61,7 +61,7 @@ export class AuthService {
};
}

async register(data: RegisterDto): Promise<OmitSecrets<users>> {
async register(data: RegisterDto): Promise<Expose<users>> {
const email = data.email;
const emailSafe = this.users.getSafeEmail(email);
delete data.email;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/prisma/prisma.interface.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export type OmitSecrets<T> = Omit<Omit<T, 'password'>, 'twoFactorSecret'>;
export type Expose<T> = Omit<Omit<T, 'password'>, 'twoFactorSecret'>;
8 changes: 5 additions & 3 deletions src/modules/prisma/prisma.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';
import { OmitSecrets } from 'src/modules/prisma/prisma.interface';
import { PrismaClient, users } from '@prisma/client';
import { Expose } from 'src/modules/prisma/prisma.interface';

@Injectable()
export class PrismaService extends PrismaClient
Expand All @@ -13,8 +13,10 @@ export class PrismaService extends PrismaClient
await this.$disconnect();
}

expose<T>(item: T): OmitSecrets<T> {
expose<T>(item: T): Expose<T> {
if (!item) return null;
delete ((item as any) as users).password;
delete ((item as any) as users).twoFactorSecret;
return item;
}
}
14 changes: 5 additions & 9 deletions src/modules/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
UseGuards,
} from '@nestjs/common';
import { users } from '@prisma/client';
import { OmitSecrets } from 'src/modules/prisma/prisma.interface';
import { Expose } from 'src/modules/prisma/prisma.interface';
import { CursorPipe } from 'src/pipes/cursor.pipe';
import { OptionalIntPipe } from 'src/pipes/optional-int.pipe';
import { OrderByPipe } from 'src/pipes/order-by.pipe';
Expand All @@ -33,31 +33,27 @@ export class UserController {
@Query('cursor', CursorPipe) cursor?: Record<string, number | string>,
@Query('where', WherePipe) where?: Record<string, number | string>,
@Query('orderBy', OrderByPipe) orderBy?: Record<string, 'asc' | 'desc'>,
): Promise<OmitSecrets<users>[]> {
): Promise<Expose<users>[]> {
return this.usersService.users({ skip, take, orderBy, cursor, where });
}

@Get(':id')
@UseGuards(ScopesGuard)
@Scopes('user{id}:read')
async get(
@Param('id', ParseIntPipe) id: number,
): Promise<OmitSecrets<users>> {
async get(@Param('id', ParseIntPipe) id: number): Promise<Expose<users>> {
return this.usersService.user({ id: Number(id) });
}

@Patch(':id')
async update(
@Param('id', ParseIntPipe) id: number,
@Body() data: UpdateUserDto,
): Promise<OmitSecrets<users>> {
): Promise<Expose<users>> {
return this.usersService.updateUser({ where: { id: Number(id) }, data });
}

@Delete(':id')
async remove(
@Param('id', ParseIntPipe) id: number,
): Promise<OmitSecrets<users>> {
async remove(@Param('id', ParseIntPipe) id: number): Promise<Expose<users>> {
return this.usersService.deleteUser({ id: Number(id) });
}
}
10 changes: 5 additions & 5 deletions src/modules/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import {
usersWhereInput,
usersOrderByInput,
} from '@prisma/client';
import { OmitSecrets } from 'src/modules/prisma/prisma.interface';
import { Expose } from 'src/modules/prisma/prisma.interface';

@Injectable()
export class UsersService {
constructor(private prisma: PrismaService) {}

async user(
userWhereUniqueInput: usersWhereUniqueInput,
): Promise<OmitSecrets<users> | null> {
): Promise<Expose<users> | null> {
const user = await this.prisma.users.findOne({
where: userWhereUniqueInput,
});
Expand All @@ -30,7 +30,7 @@ export class UsersService {
cursor?: usersWhereUniqueInput;
where?: usersWhereInput;
orderBy?: usersOrderByInput;
}): Promise<OmitSecrets<users>[]> {
}): Promise<Expose<users>[]> {
const { skip, take, cursor, where, orderBy } = params;
const users = await this.prisma.users.findMany({
skip,
Expand All @@ -51,7 +51,7 @@ export class UsersService {
async updateUser(params: {
where: usersWhereUniqueInput;
data: usersUpdateInput;
}): Promise<OmitSecrets<users>> {
}): Promise<Expose<users>> {
const { where, data } = params;
const user = await this.prisma.users.update({
data,
Expand All @@ -60,7 +60,7 @@ export class UsersService {
return this.prisma.expose<users>(user);
}

async deleteUser(where: usersWhereUniqueInput): Promise<OmitSecrets<users>> {
async deleteUser(where: usersWhereUniqueInput): Promise<Expose<users>> {
const user = await this.prisma.users.delete({
where,
});
Expand Down

0 comments on commit 93d82e1

Please sign in to comment.