From 6630cba60b7e435bf0766014733405d7c93d38e8 Mon Sep 17 00:00:00 2001 From: cedoor Date: Fri, 24 Feb 2023 18:28:21 +0100 Subject: [PATCH] chore(api): create nestjs project --- apps/api/.eslintrc.json | 18 ------ apps/api/jest.config.ts | 25 --------- apps/api/nest-cli.json | 8 +++ apps/api/package.json | 51 +++++++++++++++++ apps/api/project.json | 47 ---------------- apps/api/src/app/app.module.ts | 3 +- apps/api/src/app/auth/auth.module.ts | 2 +- .../app/auth/strategies/reddit.strategy.ts | 2 +- apps/api/src/app/common/cookie.serializer.ts | 13 ----- apps/api/src/app/groups/groups.controller.ts | 2 +- apps/api/src/app/groups/groups.service.ts | 56 +++++++++++++------ .../api/src/app/invites/invites.controller.ts | 2 +- apps/api/src/main.ts | 2 +- apps/api/tsconfig.app.json | 12 ---- apps/api/tsconfig.build.json | 4 ++ apps/api/tsconfig.json | 31 ++++++---- apps/api/tsconfig.spec.json | 9 --- 17 files changed, 127 insertions(+), 160 deletions(-) delete mode 100644 apps/api/.eslintrc.json delete mode 100644 apps/api/jest.config.ts create mode 100644 apps/api/nest-cli.json create mode 100644 apps/api/package.json delete mode 100644 apps/api/project.json delete mode 100644 apps/api/src/app/common/cookie.serializer.ts delete mode 100644 apps/api/tsconfig.app.json create mode 100644 apps/api/tsconfig.build.json delete mode 100644 apps/api/tsconfig.spec.json diff --git a/apps/api/.eslintrc.json b/apps/api/.eslintrc.json deleted file mode 100644 index 5626944..0000000 --- a/apps/api/.eslintrc.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "extends": ["../../.eslintrc.json"], - "ignorePatterns": ["!**/*"], - "overrides": [ - { - "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], - "rules": {} - }, - { - "files": ["*.ts", "*.tsx"], - "rules": {} - }, - { - "files": ["*.js", "*.jsx"], - "rules": {} - } - ] -} diff --git a/apps/api/jest.config.ts b/apps/api/jest.config.ts deleted file mode 100644 index 0a4b7b5..0000000 --- a/apps/api/jest.config.ts +++ /dev/null @@ -1,25 +0,0 @@ -/* eslint-disable */ -export default { - displayName: "api", - preset: "../../jest.preset.js", - globals: { - "ts-jest": { - tsconfig: "/tsconfig.spec.json" - } - }, - testEnvironment: "node", - transform: { - "^.+\\.[tj]s$": "ts-jest" - }, - moduleFileExtensions: ["ts", "js", "html"], - coverageDirectory: "../../coverage/api", - coverageReporters: ["lcov"], - coverageThreshold: { - global: { - branches: 90, - functions: 95, - lines: 95, - statements: 95 - } - } -} diff --git a/apps/api/nest-cli.json b/apps/api/nest-cli.json new file mode 100644 index 0000000..363961e --- /dev/null +++ b/apps/api/nest-cli.json @@ -0,0 +1,8 @@ +{ + "$schema": "https://json.schemastore.org/nest-cli", + "collection": "@nestjs/schematics", + "sourceRoot": "src", + "compilerOptions": { + "deleteOutDir": true + } +} diff --git a/apps/api/package.json b/apps/api/package.json new file mode 100644 index 0000000..8f0cab4 --- /dev/null +++ b/apps/api/package.json @@ -0,0 +1,51 @@ +{ + "name": "api", + "private": true, + "license": "MIT", + "scripts": { + "start": "nest start", + "build": "nest build", + "start:dev": "nest start --watch", + "start:debug": "nest start --debug --watch", + "start:prod": "node dist/main" + }, + "dependencies": { + "@ethersproject/hash": "^5.7.0", + "@nestjs/common": "^9.0.0", + "@nestjs/core": "^9.0.0", + "@nestjs/jwt": "^10.0.2", + "@nestjs/mapped-types": "^1.2.2", + "@nestjs/passport": "^9.0.3", + "@nestjs/platform-express": "^9.0.0", + "@nestjs/schedule": "^2.2.0", + "@nestjs/typeorm": "^9.0.1", + "@semaphore-protocol/group": "^3.1.0", + "cookie-parser": "^1.4.6", + "passport": "^0.6.0", + "passport-github": "^1.1.0", + "passport-jwt": "^4.0.1", + "passport-oauth2": "^1.6.1", + "passport-twitter": "^1.0.4", + "querystring": "^0.2.1", + "reflect-metadata": "^0.1.13", + "rxjs": "^7.2.0", + "sqlite3": "^5.1.4", + "typeorm": "^0.3.12" + }, + "devDependencies": { + "@nestjs/cli": "^9.0.0", + "@nestjs/schematics": "^9.0.0", + "@nestjs/testing": "^9.0.0", + "@types/express": "^4.17.13", + "@types/passport": "^1.0.12", + "@types/passport-github": "^1.1.7", + "@types/passport-twitter": "^1.0.37", + "@types/supertest": "^2.0.11", + "source-map-support": "^0.5.20", + "supertest": "^6.1.3", + "ts-loader": "^9.2.3", + "ts-node": "^10.0.0", + "tsconfig-paths": "4.1.1", + "typescript": "^4.7.4" + } +} diff --git a/apps/api/project.json b/apps/api/project.json deleted file mode 100644 index 4c3e851..0000000 --- a/apps/api/project.json +++ /dev/null @@ -1,47 +0,0 @@ -{ - "$schema": "../../node_modules/nx/schemas/project-schema.json", - "sourceRoot": "apps/api/src", - "projectType": "application", - "targets": { - "build": { - "executor": "@nrwl/node:webpack", - "outputs": ["{options.outputPath}"], - "options": { - "outputPath": "dist/api", - "main": "apps/api/src/main.ts", - "tsConfig": "apps/api/tsconfig.app.json", - "assets": ["apps/api/src/assets"], - "generatePackageJson": true - }, - "configurations": { - "production": { - "optimization": true, - "extractLicenses": true, - "inspect": false - } - } - }, - "serve": { - "executor": "@nrwl/node:node", - "options": { - "buildTarget": "api:build" - } - }, - "lint": { - "executor": "@nrwl/linter:eslint", - "outputs": ["{options.outputFile}"], - "options": { - "lintFilePatterns": ["apps/api/**/*.ts"] - } - }, - "test": { - "executor": "@nrwl/jest:jest", - "outputs": ["coverage/api"], - "options": { - "jestConfig": "apps/api/jest.config.ts", - "passWithNoTests": true - } - } - }, - "tags": [] -} diff --git a/apps/api/src/app/app.module.ts b/apps/api/src/app/app.module.ts index 25b49c2..6e01577 100644 --- a/apps/api/src/app/app.module.ts +++ b/apps/api/src/app/app.module.ts @@ -1,6 +1,5 @@ import { Module } from "@nestjs/common" import { TypeOrmModule } from "@nestjs/typeorm" -import "pg" // This is required for NX to include pg in api package.json during build import { AccountModule } from "./accounts/account.module" import { AuthModule } from "./auth/auth.module" import { GroupsModule } from "./groups/groups.module" @@ -21,7 +20,7 @@ type DB_TYPE = "mysql" | "sqlite" | "postgres" database: process.env.DB_URL }), autoLoadEntities: true, - synchronize: process.env.NODE_ENV === "production" ? false : true + synchronize: process.env.NODE_ENV !== "production" }) ] }) diff --git a/apps/api/src/app/auth/auth.module.ts b/apps/api/src/app/auth/auth.module.ts index e3f2271..58b0fc0 100644 --- a/apps/api/src/app/auth/auth.module.ts +++ b/apps/api/src/app/auth/auth.module.ts @@ -2,7 +2,7 @@ import { Module } from "@nestjs/common" import { JwtModule } from "@nestjs/jwt" import { PassportModule } from "@nestjs/passport" import { AccountModule } from "../accounts/account.module" -import { CookieSerializer } from "../common/cookie.serializer" +import CookieSerializer from "../utils/cookie.serializer" import { AuthController } from "./auth.controller" import { AuthService } from "./auth.service" import { GithubStrategy } from "./strategies/github.strategy" diff --git a/apps/api/src/app/auth/strategies/reddit.strategy.ts b/apps/api/src/app/auth/strategies/reddit.strategy.ts index 0ec324d..40fbf65 100644 --- a/apps/api/src/app/auth/strategies/reddit.strategy.ts +++ b/apps/api/src/app/auth/strategies/reddit.strategy.ts @@ -1,6 +1,6 @@ import { Injectable, Logger } from "@nestjs/common" import { PassportStrategy } from "@nestjs/passport" -import { Profile, Strategy } from "@zk-groups/passport-reddit" +import Strategy, { Profile } from "../../utils/passport-reddit" import { AuthService } from "../auth.service" @Injectable() diff --git a/apps/api/src/app/common/cookie.serializer.ts b/apps/api/src/app/common/cookie.serializer.ts deleted file mode 100644 index 73dc98d..0000000 --- a/apps/api/src/app/common/cookie.serializer.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { Injectable } from "@nestjs/common" -import { PassportSerializer } from "@nestjs/passport" - -@Injectable() -export class CookieSerializer extends PassportSerializer { - serializeUser(user, done: (a, b) => void) { - done(null, user) - } - - deserializeUser(payload, done: (a, b) => void) { - done(null, payload) - } -} diff --git a/apps/api/src/app/groups/groups.controller.ts b/apps/api/src/app/groups/groups.controller.ts index e6ba47c..b6ce256 100644 --- a/apps/api/src/app/groups/groups.controller.ts +++ b/apps/api/src/app/groups/groups.controller.ts @@ -9,7 +9,7 @@ import { UseGuards } from "@nestjs/common" import { AuthGuard } from "@nestjs/passport" -import { stringifyJSON } from "@zk-groups/utils" +import stringifyJSON from "../utils/stringifyJSON" import { AddMemberDto } from "./dto/add-member.dto" import { CreateGroupDto } from "./dto/create-group.dto" import { UpdateGroupDto } from "./dto/update-group.dto" diff --git a/apps/api/src/app/groups/groups.service.ts b/apps/api/src/app/groups/groups.service.ts index 68baa1f..08baa95 100644 --- a/apps/api/src/app/groups/groups.service.ts +++ b/apps/api/src/app/groups/groups.service.ts @@ -1,4 +1,4 @@ -import { id } from "@ethersproject/hash" +import {id} from "@ethersproject/hash" import { BadRequestException, forwardRef, @@ -8,23 +8,29 @@ import { NotFoundException, UnauthorizedException } from "@nestjs/common" -import { SchedulerRegistry } from "@nestjs/schedule" -import { InjectRepository } from "@nestjs/typeorm" -import { Group as CachedGroup } from "@semaphore-protocol/group" -import { zkGroups } from "@zk-groups/contract-utils" -import { Repository } from "typeorm" -import { InvitesService } from "../invites/invites.service" -import { AddMemberDto } from "./dto/add-member.dto" -import { CreateGroupDto } from "./dto/create-group.dto" -import { UpdateGroupDto } from "./dto/update-group.dto" -import { Group } from "./entities/group.entity" -import { Member } from "./entities/member.entity" -import { MerkleProof } from "./types" +import {SchedulerRegistry} from "@nestjs/schedule" +import {InjectRepository} from "@nestjs/typeorm" +import {Group as CachedGroup} from "@semaphore-protocol/group" +import { + getZKGroupsContract, + Network, + OnchainZKGroup, + ZKGroupsContract +} from "@zk-groups/utils" +import {Repository} from "typeorm" +import {InvitesService} from "../invites/invites.service" +import {AddMemberDto} from "./dto/add-member.dto" +import {CreateGroupDto} from "./dto/create-group.dto" +import {UpdateGroupDto} from "./dto/update-group.dto" +import {Group} from "./entities/group.entity" +import {Member} from "./entities/member.entity" +import {MerkleProof} from "./types" @Injectable() export class GroupsService { private cachedGroups: Map - private updatedGroups: zkGroups.Group[] + private updatedGroups: OnchainZKGroup[] + private zkGroupsContract: ZKGroupsContract constructor( @InjectRepository(Group) @@ -36,6 +42,10 @@ export class GroupsService { ;(async () => { this.cachedGroups = new Map() this.updatedGroups = [] + this.zkGroupsContract = getZKGroupsContract( + process.env.DEFAULT_NETWORK as Network, + process.env.BACKEND_PRIVATE_KEY as string + ) const groups = await this.getAllGroups() @@ -76,7 +86,7 @@ export class GroupsService { description, treeDepth, tag, - admin: admin, + admin, members: [] }) @@ -174,9 +184,13 @@ export class GroupsService { * @returns List of existing groups. */ async getAllGroups(): Promise { +<<<<<<< HEAD return await this.groupRepository.find({ relations: { members: true } }) +======= + return this.groupRepository.find() +>>>>>>> 6c40be3 (chore(api): create nestjs project) } /** @@ -185,10 +199,14 @@ export class GroupsService { * @returns List of admin's existing groups. */ async getGroupsByAdmin(admin: string): Promise { +<<<<<<< HEAD return await this.groupRepository.find({ relations: { members: true }, where: { admin } }) +======= + return this.groupRepository.findBy({ admin }) +>>>>>>> 6c40be3 (chore(api): create nestjs project) } /** @@ -248,14 +266,16 @@ export class GroupsService { private async updateContractGroups(period = 60): Promise { if (this.schedulerRegistry.getTimeouts().length === 0) { const callback = async () => { - const tx = await zkGroups.updateGroups(this.updatedGroups) + const tx = await this.zkGroupsContract.updateGroups( + this.updatedGroups + ) this.updatedGroups = [] if (tx.status) { Logger.log( - `GroupsService: ${tx.events.length} ${ - tx.events.length === 1 ? "group" : "groups" + `GroupsService: ${tx.logs.length} ${ + tx.logs.length === 1 ? "group" : "groups" } have been updated in the contract` ) } else { diff --git a/apps/api/src/app/invites/invites.controller.ts b/apps/api/src/app/invites/invites.controller.ts index 030e35c..9d54779 100644 --- a/apps/api/src/app/invites/invites.controller.ts +++ b/apps/api/src/app/invites/invites.controller.ts @@ -8,7 +8,7 @@ import { UseGuards } from "@nestjs/common" import { AuthGuard } from "@nestjs/passport" -import { mapEntity } from "@zk-groups/utils" +import mapEntity from "../utils/mapEntity" import { CreateInviteDto } from "./dto/create-invite.dto" import { Invite } from "./entities/invite.entity" import { InvitesService } from "./invites.service" diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index e2969bc..5c57a30 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -1,7 +1,7 @@ import { Logger, ValidationPipe } from "@nestjs/common" import { NestFactory } from "@nestjs/core" -import { AppModule } from "./app/app.module" import cookieParser from "cookie-parser" +import { AppModule } from "./app/app.module" async function bootstrap() { const app = await NestFactory.create(AppModule) diff --git a/apps/api/tsconfig.app.json b/apps/api/tsconfig.app.json deleted file mode 100644 index 57d119b..0000000 --- a/apps/api/tsconfig.app.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "extends": "./tsconfig.json", - "compilerOptions": { - "outDir": "../../dist/out-tsc", - "module": "commonjs", - "types": ["node"], - "emitDecoratorMetadata": true, - "target": "es2015" - }, - "exclude": ["jest.config.ts", "**/*.spec.ts", "**/*.test.ts"], - "include": ["**/*.ts"] -} diff --git a/apps/api/tsconfig.build.json b/apps/api/tsconfig.build.json new file mode 100644 index 0000000..1430cfc --- /dev/null +++ b/apps/api/tsconfig.build.json @@ -0,0 +1,4 @@ +{ + "extends": "./tsconfig.json", + "exclude": ["node_modules", "dist", "**/*spec.ts"] +} diff --git a/apps/api/tsconfig.json b/apps/api/tsconfig.json index e23d75a..373be19 100644 --- a/apps/api/tsconfig.json +++ b/apps/api/tsconfig.json @@ -1,13 +1,22 @@ { - "extends": "../../tsconfig.base.json", - "files": [], - "include": [], - "references": [ - { - "path": "./tsconfig.app.json" - }, - { - "path": "./tsconfig.spec.json" - } - ] + "compilerOptions": { + "baseUrl": ".", + "target": "es2017", + "module": "commonjs", + "declaration": true, + "resolveJsonModule": true, + "removeComments": true, + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "allowSyntheticDefaultImports": true, + "sourceMap": true, + "outDir": "./dist", + "incremental": true, + "skipLibCheck": true, + "strictNullChecks": false, + "noImplicitAny": false, + "strictBindCallApply": false, + "forceConsistentCasingInFileNames": false, + "noFallthroughCasesInSwitch": false + } } diff --git a/apps/api/tsconfig.spec.json b/apps/api/tsconfig.spec.json deleted file mode 100644 index 5591a03..0000000 --- a/apps/api/tsconfig.spec.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "extends": "./tsconfig.json", - "compilerOptions": { - "outDir": "../../dist/out-tsc", - "module": "commonjs", - "types": ["jest", "node"] - }, - "include": ["jest.config.ts", "**/*.test.ts", "**/*.spec.ts", "**/*.d.ts"] -}