Skip to content

Commit

Permalink
add eslint-config, prettier-config & tsconfig package to uniform code…
Browse files Browse the repository at this point in the history
… style
  • Loading branch information
lukesthl committed Oct 24, 2023
1 parent 09117d3 commit 8b58e65
Show file tree
Hide file tree
Showing 80 changed files with 1,998 additions and 1,433 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ jobs:
run: pnpm install

- name: Run linters
run: 'pnpm run lint'
run: "pnpm run lint && pnpm run format"
7 changes: 0 additions & 7 deletions .prettierrc

This file was deleted.

25 changes: 0 additions & 25 deletions apps/api/.eslintrc.js

This file was deleted.

4 changes: 0 additions & 4 deletions apps/api/.prettierrc

This file was deleted.

23 changes: 14 additions & 9 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
"license": "UNLICENSED",
"scripts": {
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"format": "prettier --write \"**/*.{js,cjs,mjs,ts,tsx,md,json}\"",
"start": "nest start",
"dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"lint": "eslint . --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
Expand All @@ -36,20 +36,18 @@
"supertokens-node": "^16.3.3"
},
"devDependencies": {
"@acme/eslint-config": "^0.2.0",
"@acme/prettier-config": "^0.1.0",
"@acme/tsconfig": "^0.1.0",
"@nestjs/cli": "^10.2.0",
"@nestjs/schematics": "^10.0.2",
"@nestjs/testing": "^10.2.7",
"@types/express": "^4.17.20",
"@types/jest": "^29.5.6",
"@types/node": "^20.8.7",
"@types/supertest": "^2.0.15",
"@typescript-eslint/eslint-plugin": "^6.8.0",
"@typescript-eslint/parser": "^6.8.0",
"eslint": "^8.52.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.1",
"expo": "link:apps/expo",
"jest": "^29.7.0",
"prettier": "^3.0.3",
"source-map-support": "^0.5.21",
"supertest": "^6.3.3",
"ts-jest": "^29.1.1",
Expand All @@ -74,5 +72,12 @@
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
},
"eslintConfig": {
"root": true,
"extends": [
"@acme/eslint-config/base"
]
},
"prettier": "@acme/prettier-config"
}
14 changes: 8 additions & 6 deletions apps/api/src/app.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import type { TestingModule } from "@nestjs/testing";
import { Test } from "@nestjs/testing";

describe('AppController', () => {
import { AppController } from "./app.controller";
import { AppService } from "./app.service";

describe("AppController", () => {
let appController: AppController;

beforeEach(async () => {
Expand All @@ -14,9 +16,9 @@ describe('AppController', () => {
appController = app.get<AppController>(AppController);
});

describe('root', () => {
describe("root", () => {
it('should return "Hello World!"', () => {
expect(appController.getTest(undefined)).toBe('Unauthorised');
expect(appController.getTest(undefined)).toBe("Unauthorised");
});
});
});
79 changes: 26 additions & 53 deletions apps/api/src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,97 +1,70 @@
import {
Controller,
Delete,
Get,
Post,
Session,
UseGuards,
} from '@nestjs/common';
import { EmailVerificationClaim } from 'supertokens-node/recipe/emailverification';
import {
SessionClaimValidator,
SessionContainer,
} from 'supertokens-node/recipe/session';
import UserRoles, {
PermissionClaim,
UserRoleClaim,
} from 'supertokens-node/recipe/userroles';
import { AuthGuard } from './auth/filter/auth.guard';
import { Controller, Delete, Get, Post, Session, UseGuards } from "@nestjs/common";
import { EmailVerificationClaim } from "supertokens-node/recipe/emailverification";
import type { SessionClaimValidator } from "supertokens-node/recipe/session";
import { SessionContainer } from "supertokens-node/recipe/session";
import UserRoles, { PermissionClaim, UserRoleClaim } from "supertokens-node/recipe/userroles";

import { AuthGuard } from "./auth/filter/auth.guard";

@Controller()
export class AppController {
@Get('test')
@Get("test")
@UseGuards(
new AuthGuard({
overrideGlobalClaimValidators: async (
globalValidators: SessionClaimValidator[],
) => [
overrideGlobalClaimValidators: (globalValidators: SessionClaimValidator[]) => [
...globalValidators,
EmailVerificationClaim.validators.isVerified(),
],
}),
})
)
async getTest(@Session() session: SessionContainer): Promise<string> {
getTest(@Session() session: SessionContainer): string {
return session.getUserId();
}

@Get('admin')
@Get("admin")
@UseGuards(
new AuthGuard({
overrideGlobalClaimValidators: async (
globalValidators: SessionClaimValidator[],
) => [
overrideGlobalClaimValidators: (globalValidators: SessionClaimValidator[]) => [
...globalValidators,
UserRoles.UserRoleClaim.validators.includes('admin'),
UserRoles.UserRoleClaim.validators.includes("admin"),
],
}),
})
)
async admin(): Promise<string> {
admin(): string {
return "you're an admin";
}

// totally insecure route, just for demo purposes
@Post('make-me-an-admin')
@Post("make-me-an-admin")
@UseGuards(
new AuthGuard({
overrideGlobalClaimValidators: async (
globalValidators: SessionClaimValidator[],
) => [
overrideGlobalClaimValidators: (globalValidators: SessionClaimValidator[]) => [
...globalValidators,
EmailVerificationClaim.validators.isVerified(),
],
}),
})
)
async setRole(@Session() session: SessionContainer) {
await UserRoles.createNewRoleOrAddPermissions('admin', ['read', 'write']);
const response = await UserRoles.addRoleToUser(
session.getTenantId(),
session.getUserId(),
'admin',
);
await UserRoles.createNewRoleOrAddPermissions("admin", ["read", "write"]);
const response = await UserRoles.addRoleToUser(session.getTenantId(), session.getUserId(), "admin");

await session.fetchAndSetClaim(UserRoleClaim);
await session.fetchAndSetClaim(PermissionClaim);
return response;
}

// totally insecure route, just for demo purposes
@Delete('remove-admin')
@Delete("remove-admin")
@UseGuards(
new AuthGuard({
overrideGlobalClaimValidators: async (
globalValidators: SessionClaimValidator[],
) => [
overrideGlobalClaimValidators: (globalValidators: SessionClaimValidator[]) => [
...globalValidators,
UserRoles.UserRoleClaim.validators.includes('admin'),
UserRoles.UserRoleClaim.validators.includes("admin"),
],
}),
})
)
async removeAdmin(@Session() session: SessionContainer) {
const response = await UserRoles.removeUserRole(
session.getTenantId(),
session.getUserId(),
'admin',
);
const response = await UserRoles.removeUserRole(session.getTenantId(), session.getUserId(), "admin");
await session.fetchAndSetClaim(UserRoleClaim);
await session.fetchAndSetClaim(PermissionClaim);
return response;
Expand Down
28 changes: 15 additions & 13 deletions apps/api/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthModule } from './auth/auth.module';
import { LoggerMiddleware } from './logger/logger.middleware';
import { UserModule } from './user/user.module';
import type { MiddlewareConsumer, NestModule } from "@nestjs/common";
import { Module } from "@nestjs/common";
import { ConfigModule } from "@nestjs/config";

import { AppController } from "./app.controller";
import { AppService } from "./app.service";
import { AuthModule } from "./auth/auth.module";
import { LoggerMiddleware } from "./logger/logger.middleware";
import { UserModule } from "./user/user.module";

@Module({
imports: [
Expand All @@ -13,15 +15,15 @@ import { UserModule } from './user/user.module';
isGlobal: true,
}),
AuthModule.forRoot({
connectionURI: process.env.SUPERTOKENS_CONNECTION_URI,
connectionURI: process.env.SUPERTOKENS_CONNECTION_URI ?? "",
apiKey: process.env.SUPERTOKENS_API_KEY,
appInfo: {
appName: 'SuperTokens Managed Service',
apiDomain: process.env.API_DOMAIN,
appName: "SuperTokens Managed Service",
apiDomain: process.env.API_DOMAIN ?? "",
// only used for email links
websiteDomain: process.env.API_DOMAIN,
apiBasePath: '/auth',
websiteBasePath: '/auth',
apiBasePath: "/auth",
websiteBasePath: "/auth",
},
}),
UserModule,
Expand All @@ -31,6 +33,6 @@ import { UserModule } from './user/user.module';
})
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(LoggerMiddleware).forRoutes('*');
consumer.apply(LoggerMiddleware).forRoutes("*");
}
}
4 changes: 2 additions & 2 deletions apps/api/src/app.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Injectable } from '@nestjs/common';
import { Injectable } from "@nestjs/common";

@Injectable()
export class AppService {
getHello(): string {
return 'Hello World!';
return "Hello World!";
}
}
Loading

0 comments on commit 8b58e65

Please sign in to comment.