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

Commit

Permalink
🏷️ Fix types are updating TS
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Oct 31, 2020
1 parent 1262a0f commit be293ae
Show file tree
Hide file tree
Showing 40 changed files with 313 additions and 274 deletions.
77 changes: 41 additions & 36 deletions src/helpers/parse-object-literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,52 +25,57 @@ export const parseObjectLiteral = (
'g',
);
const divisionLookBehind = /[\])"'A-Za-z0-9_$]+$/;
const keywordRegexLookBehind = { in: 1, return: 1, typeof: 1 };
const keywordRegexLookBehind: Record<string, number> = {
in: 1,
return: 1,
typeof: 1,
};
let str = objectLiteralString.trim();
if (str.charCodeAt(0) === 123) str = str.slice(1, -1);
const result: [string, string | undefined][] = [];
let toks = str.match(token);
let toks = str.match(token) as RegExpMatchArray;
if (!toks) return result;
let key: string | undefined = undefined;
let values = [];
let depth = 0;
if (toks) {
toks.push(',');
for (let i = 0, tok: string; (tok = toks[i]); ++i) {
const c = tok.charCodeAt(0);
if (c === 44) {
if (depth <= 0) {
if (!key && values.length === 1) {
key = values.pop();
}
result.push([key, values.length ? values.join('') : undefined]);
key = undefined;
values = [];
depth = 0;
continue;
}
} else if (c === 58) {
if (!depth && !key && values.length === 1) {
toks.push(',');
for (let i = 0, tok: string; (tok = toks[i]); ++i) {
const c = tok.charCodeAt(0);
if (c === 44) {
if (depth <= 0) {
if (!key && values.length === 1) {
key = values.pop();
continue;
}
} else if (c === 47 && i && tok.length > 1) {
const match = toks[i - 1].match(divisionLookBehind);
if (match && !keywordRegexLookBehind[match[0]]) {
str = str.substr(str.indexOf(tok) + 1);
toks = str.match(token);
toks.push(',');
i = -1;
tok = '/';
}
} else if (c === 40 || c === 123 || c === 91) {
++depth;
} else if (c === 41 || c === 125 || c === 93) {
--depth;
} else if (!key && !values.length && (c === 34 || c === 39)) {
tok = tok.slice(1, -1);
if (key)
result.push([key, values.length ? values.join('') : undefined]);
key = undefined;
values = [];
depth = 0;
continue;
}
} else if (c === 58) {
if (!depth && !key && values.length === 1) {
key = values.pop();
continue;
}
} else if (c === 47 && i && tok.length > 1) {
const match = toks[i - 1].match(divisionLookBehind);
if (match && !keywordRegexLookBehind[match[0]]) {
str = str.substr(str.indexOf(tok) + 1);
const result = str.match(token);
if (result) toks = result;
toks.push(',');
i = -1;
tok = '/';
}
values.push(tok);
} else if (c === 40 || c === 123 || c === 91) {
++depth;
} else if (c === 41 || c === 125 || c === 93) {
--depth;
} else if (!key && !values.length && (c === 34 || c === 39)) {
tok = tok.slice(1, -1);
}
values.push(tok);
}
return result;
};
10 changes: 5 additions & 5 deletions src/modules/access-tokens/access-tokens.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import {
Query,
} from '@nestjs/common';
import { accessTokens } from '@prisma/client';
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';
import { WherePipe } from 'src/pipes/where.pipe';
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';
import { WherePipe } from '../../../src/pipes/where.pipe';
import { Scopes } from '../auth/scope.decorator';
import {
CreateAccessTokenDto,
Expand Down
10 changes: 5 additions & 5 deletions src/modules/access-tokens/access-tokens.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,24 @@ export class UpdateAccessTokenDto {
export class ReplaceAccessTokenDto {
@IsString()
@IsNotEmpty()
description: string;
description!: string;

@IsString()
@IsNotEmpty()
name: string;
name!: string;

@IsArray()
@IsString({ each: true })
@IsNotEmpty()
scopes: string[];
scopes!: string[];

@IsArray()
@IsString({ each: true })
@IsNotEmpty()
ipRestrictions: string[];
ipRestrictions!: string[];

@IsArray()
@IsString({ each: true })
@IsNotEmpty()
referrerRestrictions: string[];
referrerRestrictions!: string[];
}
6 changes: 3 additions & 3 deletions src/modules/access-tokens/access-tokens.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
accessTokensWhereInput,
accessTokensWhereUniqueInput,
} from '@prisma/client';
import { Expose } from 'src/modules/prisma/prisma.interface';
import { Expose } from '../../../src/modules/prisma/prisma.interface';
import { PrismaService } from '../prisma/prisma.service';

@Injectable()
Expand Down Expand Up @@ -47,13 +47,13 @@ export class AccessTokensService {
where: { ...where, user: { id: userId } },
orderBy,
});
return accessTokens.map(user => this.prisma.expose<accessTokens>(user));
return accessTokens.map((user) => this.prisma.expose<accessTokens>(user));
}

async getAccessToken(
userId: number,
id: number,
): Promise<Expose<accessTokens> | null> {
): Promise<Expose<accessTokens>> {
const accessToken = await this.prisma.accessTokens.findOne({
where: { id },
});
Expand Down
10 changes: 5 additions & 5 deletions src/modules/api-keys/api-keys.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import {
Query,
} from '@nestjs/common';
import { apiKeys } from '@prisma/client';
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';
import { WherePipe } from 'src/pipes/where.pipe';
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';
import { WherePipe } from '../../../src/pipes/where.pipe';
import { Scopes } from '../auth/scope.decorator';
import {
CreateApiKeyDto,
Expand Down
10 changes: 5 additions & 5 deletions src/modules/api-keys/api-keys.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,24 @@ export class UpdateApiKeyDto {
export class ReplaceApiKeyDto {
@IsString()
@IsNotEmpty()
description: string;
description!: string;

@IsString()
@IsNotEmpty()
name: string;
name!: string;

@IsArray()
@IsString({ each: true })
@IsNotEmpty()
scopes: string[];
scopes!: string[];

@IsArray()
@IsString({ each: true })
@IsNotEmpty()
ipRestrictions: string[];
ipRestrictions!: string[];

@IsArray()
@IsString({ each: true })
@IsNotEmpty()
referrerRestrictions: string[];
referrerRestrictions!: string[];
}
9 changes: 3 additions & 6 deletions src/modules/api-keys/api-keys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
apiKeysWhereInput,
apiKeysWhereUniqueInput,
} from '@prisma/client';
import { Expose } from 'src/modules/prisma/prisma.interface';
import { Expose } from '../../../src/modules/prisma/prisma.interface';
import { PrismaService } from '../prisma/prisma.service';

@Injectable()
Expand Down Expand Up @@ -48,13 +48,10 @@ export class ApiKeysService {
where: { ...where, group: { id: groupId } },
orderBy,
});
return apiKeys.map(group => this.prisma.expose<apiKeys>(group));
return apiKeys.map((group) => this.prisma.expose<apiKeys>(group));
}

async getApiKey(
groupId: number,
id: number,
): Promise<Expose<apiKeys> | null> {
async getApiKey(groupId: number, id: number): Promise<Expose<apiKeys>> {
const apiKey = await this.prisma.apiKeys.findOne({
where: { id },
});
Expand Down
10 changes: 5 additions & 5 deletions src/modules/approved-subnets/approved-subnets.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import {
Query,
} from '@nestjs/common';
import { approvedSubnets } from '@prisma/client';
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';
import { WherePipe } from 'src/pipes/where.pipe';
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';
import { WherePipe } from '../../../src/pipes/where.pipe';
import { Scopes } from '../auth/scope.decorator';
import { ApprovedSubnetsService } from './approved-subnets.service';

Expand Down
10 changes: 5 additions & 5 deletions src/modules/approved-subnets/approved-subnets.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
approvedSubnetsWhereInput,
approvedSubnetsWhereUniqueInput,
} from '@prisma/client';
import { Expose } from 'src/modules/prisma/prisma.interface';
import { Expose } from '../../../src/modules/prisma/prisma.interface';
import { PrismaService } from '../prisma/prisma.service';
import anonymize from 'ip-anonymize';
import { compare, hash } from 'bcrypt';
Expand Down Expand Up @@ -43,15 +43,15 @@ export class ApprovedSubnetsService {
where: { ...where, user: { id: userId } },
orderBy,
});
return approvedSubnets.map(user =>
return approvedSubnets.map((user) =>
this.prisma.expose<approvedSubnets>(user),
);
}

async getApprovedSubnet(
userId: number,
id: number,
): Promise<Expose<approvedSubnets> | null> {
): Promise<Expose<approvedSubnets>> {
const approvedSubnet = await this.prisma.approvedSubnets.findOne({
where: { id },
});
Expand Down Expand Up @@ -82,15 +82,15 @@ export class ApprovedSubnetsService {
async approveNewSubnet(userId: number, ipAddress: string) {
const subnet = await hash(
anonymize(ipAddress),
this.configService.get<number>('security.saltRounds'),
this.configService.get<number>('security.saltRounds') ?? 10,
);
const location = await this.geolocationService.getLocation(ipAddress);
const approved = await this.prisma.approvedSubnets.create({
data: {
user: { connect: { id: userId } },
subnet,
city: location?.city?.names?.en,
region: location?.subdivisions.pop()?.names?.en,
region: location?.subdivisions?.pop()?.names?.en,
timezone: location?.location?.time_zone,
countryCode: location?.country?.iso_code,
},
Expand Down
2 changes: 1 addition & 1 deletion src/modules/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '@nestjs/common';
import { users } from '@prisma/client';
import { RateLimit } from 'nestjs-rate-limiter';
import { Expose } from 'src/modules/prisma/prisma.interface';
import { Expose } from '../../../src/modules/prisma/prisma.interface';
import {
ForgotPasswordDto,
LoginDto,
Expand Down
20 changes: 10 additions & 10 deletions src/modules/auth/auth.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ export class RegisterDto {
@IsString()
@IsNotEmpty()
@MinLength(3)
name: string;
name!: string;

@IsEmail()
@IsNotEmpty()
email: string;
email!: string;

@IsBoolean()
@IsOptional()
Expand Down Expand Up @@ -82,24 +82,24 @@ export class RegisterDto {
export class ResendEmailVerificationDto {
@IsEmail()
@IsNotEmpty()
email: string;
email!: string;
}

export class ForgotPasswordDto {
@IsEmail()
@IsNotEmpty()
email: string;
email!: string;
}

export class ResetPasswordDto {
@IsString()
@IsNotEmpty()
token: string;
token!: string;

@IsString()
@MinLength(8)
@IsNotEmpty()
password: string;
password!: string;

@IsBoolean()
@IsOptional()
Expand All @@ -109,7 +109,7 @@ export class ResetPasswordDto {
export class LoginDto {
@IsEmail()
@IsNotEmpty()
email: string;
email!: string;

@IsString()
@MinLength(8)
Expand All @@ -125,16 +125,16 @@ export class LoginDto {
export class TotpLoginDto {
@IsString()
@IsNotEmpty()
token: string;
token!: string;

@IsString()
@Length(6)
@IsNotEmpty()
code: string;
code!: string;
}

export class VerifyEmailDto {
@IsString()
@IsNotEmpty()
token: string;
token!: string;
}
2 changes: 1 addition & 1 deletion src/modules/auth/auth.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface ValidatedUser {
id: number;
name: string;
twoFactorEnabled: boolean;
twoFactorSecret?: string;
twoFactorSecret: string | null;
checkLocationOnLogin: boolean;
prefersEmailAddress: string;
}
Loading

0 comments on commit be293ae

Please sign in to comment.