chore(api): create nestjs project

This commit is contained in:
cedoor
2023-02-24 18:28:21 +01:00
parent a38108e065
commit 6630cba60b
17 changed files with 127 additions and 160 deletions

View File

@@ -1,18 +0,0 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}

View File

@@ -1,25 +0,0 @@
/* eslint-disable */
export default {
displayName: "api",
preset: "../../jest.preset.js",
globals: {
"ts-jest": {
tsconfig: "<rootDir>/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
}
}
}

8
apps/api/nest-cli.json Normal file
View File

@@ -0,0 +1,8 @@
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true
}
}

51
apps/api/package.json Normal file
View File

@@ -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"
}
}

View File

@@ -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": []
}

View File

@@ -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"
})
]
})

View File

@@ -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"

View File

@@ -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()

View File

@@ -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)
}
}

View File

@@ -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"

View File

@@ -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<string, CachedGroup>
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<Group[]> {
<<<<<<< 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<Group[]> {
<<<<<<< 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<void> {
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 {

View File

@@ -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"

View File

@@ -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)

View File

@@ -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"]
}

View File

@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "dist", "**/*spec.ts"]
}

View File

@@ -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
}
}

View File

@@ -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"]
}