Skip to content

Commit

Permalink
Fix ddex webapp client lint (#8127)
Browse files Browse the repository at this point in the history
  • Loading branch information
sliptype authored Apr 16, 2024
1 parent fcf9757 commit 44d4160
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 85 deletions.
29 changes: 2 additions & 27 deletions packages/ddex/webapp/server/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,30 +1,5 @@
module.exports = {
root: true,
env: { node: true, es2020: true },
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
'plugin:prettier/recommended',
],
ignorePatterns: ['dist', '.eslintrc.cjs'],
parser: '@typescript-eslint/parser',
plugins: ['prettier'],
rules: {
'@typescript-eslint/no-unsafe-call': 'off',
'@typescript-eslint/no-unsafe-assignment': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-empty-function': 'off',
'prettier/prettier': ['error', {
singleQuote: true,
semi: false,
useTabs: false,
tabWidth: 2,
trailingComma: 'es5',
printWidth: 80,
bracketSpacing: true,
arrowParens: 'always',
}],
},
};
extends: ['audius']
}
30 changes: 16 additions & 14 deletions packages/ddex/webapp/server/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import express, { Express, NextFunction, Request, Response } from 'express'
import path from 'path'
import session from 'express-session'

import { ProfilePicture } from '@audius/sdk'
import connectMongoDBSession from 'connect-mongodb-session'
import User from './userSchema'
import express, { Express, NextFunction, Request, Response } from 'express'
import session from 'express-session'

import createSdkService from './services/sdkService'
import { ProfilePicture } from '@audius/sdk'
import User from './userSchema'

declare module 'express-session' {
interface SessionData {
Expand Down Expand Up @@ -39,10 +41,10 @@ export default function createApp(
const MongoDBStore = connectMongoDBSession(session)
const store = new MongoDBStore({
uri: dbUrl,
collection: 'authSessions',
collection: 'authSessions'
})
store.on('error', function (error: any) {
console.log(error)
console.error(error)
})

if (!process.env.SESSION_SECRET) {
Expand All @@ -52,12 +54,12 @@ export default function createApp(
const sess: session.SessionOptions = {
secret: process.env.SESSION_SECRET,
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 7 * 3, // 3 weeks
maxAge: 1000 * 60 * 60 * 24 * 7 * 3 // 3 weeks
},
store,
resave: false,
saveUninitialized: false,
rolling: true, // Reset maxAge on every response
rolling: true // Reset maxAge on every response
}

if (app.get('env') === 'production') {
Expand All @@ -79,7 +81,7 @@ export default function createApp(
// The frontend does this for the user on OAuth (there's no actual login button UI) and re-checks the session via /auth/session on page load.
app.post('/auth/login', async (req: Request, res: Response) => {
const decodedJwt = await sdkService.users.verifyIDToken({
token: req.body.token,
token: req.body.token
})
if (decodedJwt?.data) {
const { userId, handle, email, name, verified, profilePicture } =
Expand All @@ -90,7 +92,7 @@ export default function createApp(
email,
name,
verified,
profilePicture,
profilePicture
})

req.session.user = {
Expand All @@ -100,7 +102,7 @@ export default function createApp(
name: user.name,
verified: user.verified,
profilePicture: user.profilePicture,
isAdmin: user.isAdmin,
isAdmin: user.isAdmin
}

req.session.save((err) => {
Expand Down Expand Up @@ -136,7 +138,7 @@ export default function createApp(
name: updatedUser.name,
verified: updatedUser.verified,
profilePicture: updatedUser.profilePicture,
isAdmin: updatedUser.isAdmin,
isAdmin: updatedUser.isAdmin
}
req.session.save((err) => {
if (err) {
Expand Down Expand Up @@ -175,8 +177,8 @@ export default function createApp(
data: {
env: process.env.NODE_ENV,
ddexKey: process.env.DDEX_KEY,
ddexChoreography: process.env.DDEX_CHOREOGRAPHY,
},
ddexChoreography: process.env.DDEX_CHOREOGRAPHY
}
}
res.json(envData)
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Request, Response } from 'express'
import mongoose from 'mongoose'
import { Sort } from 'mongodb'
import mongoose from 'mongoose'

export const collections = [
'deliveries',
'pending_releases',
'published_releases',
'published_releases'
]

export const getCollection = (collection: string) => {
Expand Down
31 changes: 16 additions & 15 deletions packages/ddex/webapp/server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import dotenv from 'dotenv'
import path from 'path'

import * as trpcExpress from '@trpc/server/adapters/express'
import dotenv from 'dotenv'

import createApp from './app'
import createAppRouter from './routers'
import collectionRouters from './routers/collectionRouters'
import makeUploadRouter from './routers/uploadRouter'
import { dialDb } from './services/dbService'
import createS3 from './services/s3'
import createSdkService from './services/sdkService'
import { createContext, router } from './trpc'

// Load env vars from ddex package root
dotenv.config({ path: path.join(__dirname, '..', '..', '..', '.env') })
Expand All @@ -17,15 +27,6 @@ switch (process.env.NETWORK) {
process.env.NODE_ENV = 'development'
}

import createApp from './app'
import { dialDb } from './services/dbService'
import { createContext, router } from './trpc'
import * as trpcExpress from '@trpc/server/adapters/express'
import collectionRouters from './routers/collectionRouters'
import makeUploadRouter from './routers/uploadRouter'
import createAppRouter from './routers'
import createSdkService from './services/sdkService'

// TODO: Use superjson

export type AppRouter = ReturnType<typeof createAppRouter>
Expand All @@ -45,22 +46,22 @@ const port = process.env.DDEX_PORT || 9000
const s3 = createS3()
const appRouter = router({
upload: makeUploadRouter(s3),
deliveries: collectionRouters['deliveries'],
pendingReleases: collectionRouters['pending_releases'],
publishedReleases: collectionRouters['published_releases'],
deliveries: collectionRouters.deliveries,
pendingReleases: collectionRouters.pending_releases,
publishedReleases: collectionRouters.published_releases
})

app.use(
'/api/trpc',
isAuthedAsAdmin,
trpcExpress.createExpressMiddleware({
router: appRouter,
createContext: (opts) => createContext(opts, {}),
createContext: (opts) => createContext(opts, {})
})
)

app.listen(port, () => {
console.log(`[server]: Server is running at http://localhost:${port}`)
console.info(`[server]: Server is running at http://localhost:${port}`)
})
} catch (error) {
console.error('Failed to initialize:', error)
Expand Down
6 changes: 3 additions & 3 deletions packages/ddex/webapp/server/src/routers/collectionRouters.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Sort } from 'mongodb'
import mongoose from 'mongoose'
import { z } from 'zod'

import { router, publicProcedure } from '../trpc'
import { collections } from '../controllers/collectionController'
import mongoose from 'mongoose'
import { Sort } from 'mongodb'
import { router, publicProcedure } from '../trpc'

const getCollectionInput = z.object({
nextId: z.string().optional(),
Expand Down
13 changes: 7 additions & 6 deletions packages/ddex/webapp/server/src/routers/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { router } from '../trpc'
import createS3 from '../services/s3'
import makeUploadRouter from './uploadRouter'
import { router } from '../trpc'

import collectionRouters from './collectionRouters'
import makeUploadRouter from './uploadRouter'

export default function createAppRouter(s3: ReturnType<typeof createS3>) {
return router({
upload: makeUploadRouter(s3),
uploads: collectionRouters['uploads'],
deliveries: collectionRouters['deliveries'],
pendingReleases: collectionRouters['pending_releases'],
publishedReleases: collectionRouters['published_releases'],
uploads: collectionRouters.uploads,
deliveries: collectionRouters.deliveries,
pendingReleases: collectionRouters.pending_releases,
publishedReleases: collectionRouters.published_releases
})
}
5 changes: 3 additions & 2 deletions packages/ddex/webapp/server/src/routers/uploadRouter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { z } from 'zod'

import createS3 from '../services/s3'
import { router, publicProcedure } from '../trpc'

Expand All @@ -7,12 +8,12 @@ export default function makeUploadRouter(s3: ReturnType<typeof createS3>) {
generateSignedUrl: publicProcedure
.input(
z.object({
fileName: z.string(),
fileName: z.string()
})
)
.mutation(async ({ input }) => {
const { fileName } = input
return await s3.getSignedFileUrl(fileName)
}),
})
})
}
2 changes: 1 addition & 1 deletion packages/ddex/webapp/server/src/services/dbService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import mongoose from 'mongoose'

export const dialDb = async (dbUrl: string) => {
await mongoose.connect(dbUrl)
console.log('MongoDB connected...')
console.info('MongoDB connected...')
}
8 changes: 4 additions & 4 deletions packages/ddex/webapp/server/src/services/s3.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getSignedUrl } from '@aws-sdk/s3-request-presigner'
import { S3Client, PutObjectCommand, S3ClientConfig } from '@aws-sdk/client-s3'
import { getSignedUrl } from '@aws-sdk/s3-request-presigner'

export default function createS3() {
if (!process.env.AWS_ACCESS_KEY_ID || !process.env.AWS_SECRET_ACCESS_KEY) {
Expand All @@ -9,9 +9,9 @@ export default function createS3() {
const config: S3ClientConfig = {
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
},
region: process.env.AWS_REGION,
region: process.env.AWS_REGION
}
const bucket = process.env.AWS_BUCKET_RAW
const client = new S3Client(config)
Expand All @@ -22,7 +22,7 @@ export default function createS3() {
) {
const command = new PutObjectCommand({
Bucket: bucket,
Key: fileName,
Key: fileName
})

return await getSignedUrl(client, command, { expiresIn: expiresInSeconds })
Expand Down
16 changes: 8 additions & 8 deletions packages/ddex/webapp/server/src/services/sdkService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
developmentConfig,
stagingConfig,
productionConfig,
sdk,
sdk
} from '@audius/sdk'

const createSdkService = async (): Promise<AudiusSdk> => {
Expand Down Expand Up @@ -52,13 +52,13 @@ const createSdkService = async (): Promise<AudiusSdk> => {

// Init SDK
const discoveryNodeSelector = new DiscoveryNodeSelector({
initialSelectedNode,
initialSelectedNode
})
const storageNodeSelector = new StorageNodeSelector({
auth: new AppAuth(ddexKey, ddexSecret),
discoveryNodeSelector: discoveryNodeSelector,
discoveryNodeSelector,
bootstrapNodes: config.storageNodes,
logger,
logger
})
const sdkInstance = sdk({
services: {
Expand All @@ -69,16 +69,16 @@ const createSdkService = async (): Promise<AudiusSdk> => {
contractAddress: config.entityManagerContractAddress,
identityServiceUrl: config.identityServiceUrl,
useDiscoveryRelay: true,
logger,
logger
}),
storageNodeSelector,
logger,
logger
},
apiKey: ddexKey,
apiSecret: ddexSecret,
appName: 'DDEX Demo',
appName: 'DDEX Demo'
})
console.log(`SDK initialized for ${env}`)
console.info(`SDK initialized for ${env}`)
return sdkInstance
} catch (error) {
throw new Error(`SDK failed to initialize for ${env}: ${error}`)
Expand Down
2 changes: 1 addition & 1 deletion packages/ddex/webapp/server/src/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function createContext(
return {
req: options.req,
res: options.res,
...customContext,
...customContext
}
}

Expand Down
5 changes: 3 additions & 2 deletions packages/ddex/webapp/server/src/userSchema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ProfilePicture } from '@audius/sdk'
import mongoose from 'mongoose'

import { decodeHashedId, isUserAdmin } from './utils'

export interface IUser {
Expand All @@ -16,7 +17,7 @@ export interface IUser {
const profilePictureSchema = new mongoose.Schema({
'150x150': String,
'480x480': String,
'1000x1000': String,
'1000x1000': String
})

interface IUserMethods {
Expand All @@ -38,7 +39,7 @@ const userSchema = new mongoose.Schema<IUser, UserModel, IUserMethods>({
name: String,
verified: { type: Boolean, default: false },
profilePicture: { type: profilePictureSchema, default: null },
isAdmin: { type: Boolean, default: false },
isAdmin: { type: Boolean, default: false }
})

userSchema.method('updateRoles', async function updateRoles() {
Expand Down

0 comments on commit 44d4160

Please sign in to comment.