Skip to content

Commit

Permalink
feat: udpated exports of index files and changed error code when ther…
Browse files Browse the repository at this point in the history
…e is no user to be deleted
  • Loading branch information
AnthonyLzq committed Dec 19, 2021
1 parent b795b85 commit 28403d8
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 70 deletions.
4 changes: 1 addition & 3 deletions example/src/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import { User } from './user'

export { User }
export * from './user'
2 changes: 1 addition & 1 deletion example/src/controllers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class User {
if (usersDeleted.deletedCount >= 1) return MFU.ALL_USERS_DELETED

if (usersDeleted.deletedCount === 0)
throw new httpErrors.BadRequest(EFU.NOTHING_TO_DELETE)
throw new httpErrors.Conflict(EFU.NOTHING_TO_DELETE)

throw new httpErrors.InternalServerError(GE.INTERNAL_SERVER_ERROR)
} catch (e) {
Expand Down
5 changes: 2 additions & 3 deletions example/src/controllers/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import httpErrors from 'http-errors'

import { EFU, MFU, GE } from './messages'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const errorHandling = (e: any, message?: string): never => {
console.error(e)
Expand All @@ -11,4 +9,5 @@ const errorHandling = (e: any, message?: string): never => {
throw new httpErrors.InternalServerError(message ?? e.message)
}

export { EFU, MFU, GE, errorHandling }
export { errorHandling }
export * from './messages'
5 changes: 2 additions & 3 deletions example/src/controllers/utils/messages/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { EFU, MFU } from './user'

enum GenericErrors {
INTERNAL_SERVER_ERROR = 'Something went wrong'
}

export { EFU, MFU, GenericErrors as GE }
export { GenericErrors as GE }
export * from './user'
4 changes: 1 addition & 3 deletions example/src/dto-interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import { DtoUser } from './user'

export { DtoUser }
export * from './user'
4 changes: 1 addition & 3 deletions example/src/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import { IUser, UserModel } from './user'

export { IUser, UserModel }
export * from './user'
6 changes: 2 additions & 4 deletions example/src/network/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
import { applyRoutes } from './routes'
import { Server } from './server'

export { applyRoutes, Server }
export * from './routes'
export * from './server'
6 changes: 2 additions & 4 deletions example/src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
import { Home } from './home'
import { User } from './user'

export { Home, User }
export * from './home'
export * from './user'
5 changes: 2 additions & 3 deletions example/src/schemas/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Joi from 'joi'

import { userSchema } from './user'

const idSchema = Joi.string().length(24).required()

export { idSchema, userSchema }
export { idSchema }
export * from './user'
4 changes: 1 addition & 3 deletions example/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import docs from './docs.json'

export { docs }
export { default as docs } from './docs.json'
61 changes: 21 additions & 40 deletions lib/src/functions/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,7 @@ export {}
},
controllers: {
index: {
content: `import { User } from './user'
export { User }
`,
content: "export * from './user'\n",
file: `${projectName}/src/controllers/index.ts`
},
user: {
Expand Down Expand Up @@ -165,7 +162,7 @@ class User {
if (usersDeleted.deletedCount >= 1) return MFU.ALL_USERS_DELETED
if (usersDeleted.deletedCount === 0)
throw new httpErrors.BadRequest(EFU.NOTHING_TO_DELETE)
throw new httpErrors.Conflict(EFU.NOTHING_TO_DELETE)
throw new httpErrors.InternalServerError(GE.INTERNAL_SERVER_ERROR)
} catch (e) {
Expand Down Expand Up @@ -229,8 +226,6 @@ export { User }
index: {
content: `import httpErrors from 'http-errors'
import { EFU, MFU, GE } from './messages'
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const errorHandling = (e: any, message?: string): never => {
console.error(e)
Expand All @@ -240,20 +235,20 @@ const errorHandling = (e: any, message?: string): never => {
throw new httpErrors.InternalServerError(message ?? e.message)
}
export { EFU, MFU, GE, errorHandling }
export { errorHandling }
export * from './messages'
`,
file: `${projectName}/src/controllers/utils/index.ts`
}
},
'controllers/utils/messages': {
index: {
content: `import { EFU, MFU } from './user'
enum GenericErrors {
content: `enum GenericErrors {
INTERNAL_SERVER_ERROR = 'Something went wrong'
}
export { EFU, MFU, GenericErrors as GE }
export { GenericErrors as GE }
export * from './user'
`,
file: `${projectName}/src/controllers/utils/messages/index.ts`
},
Expand All @@ -275,10 +270,7 @@ export { ErrorForUser as EFU, MessageForUser as MFU }
},
'dto-interfaces': {
index: {
content: `import { DtoUser } from './user'
export { DtoUser }
`,
content: "export * from './user'\n",
file: `${projectName}/src/dto-interfaces/index.ts`
},
user: {
Expand All @@ -295,10 +287,7 @@ export { DtoUser }
},
models: {
index: {
content: `import { IUser, UserModel } from './user'
export { IUser, UserModel }
`,
content: "export * from './user'\n",
file: `${projectName}/src/models/index.ts`
},
user: {
Expand Down Expand Up @@ -350,6 +339,12 @@ export { IUser, UserModel }
}
},
network: {
index: {
content: `export * from './routes'
export * from './server'
`,
file: `${projectName}/src/network/index.ts`
},
routes: {
content: `import { Application, Response, Request, Router, NextFunction } from 'express'
import swaggerUi from 'swagger-ui-express'
Expand Down Expand Up @@ -495,14 +490,6 @@ const server = new Server()
export { server as Server }
`,
file: `${projectName}/src/network/server.ts`
},
index: {
content: `import { applyRoutes } from './routes'
import { Server } from './server'
export { applyRoutes, Server }
`,
file: `${projectName}/src/network/index.ts`
}
},
routes: {
Expand All @@ -525,10 +512,8 @@ export { Home }
file: `${projectName}/src/routes/home.ts`
},
index: {
content: `import { Home } from './home'
import { User } from './user'
export { Home, User }
content: `export * from './home'
export * from './user'
`,
file: `${projectName}/src/routes/index.ts`
},
Expand Down Expand Up @@ -681,11 +666,10 @@ export { User }
index: {
content: `import Joi from 'joi'
import { userSchema } from './user'
const idSchema = Joi.string().length(24).required()
export { idSchema, userSchema }
export { idSchema }
export * from './user'
`,
file: `${projectName}/src/schemas/index.ts`
},
Expand Down Expand Up @@ -1164,10 +1148,7 @@ DELETE http://localhost:1996/api/user/60e7e3b93b01c1a7aa74cd6b
file: `${projectName}/src/utils/docs.json`
},
index: {
content: `import docs from './docs.json'
export { docs }
`,
content: "export { default as docs } from './docs.json'\n",
file: `${projectName}/src/utils/index.ts`
}
},
Expand Down Expand Up @@ -1236,9 +1217,9 @@ ${projectName}/src/utils
await writeFile(data.models.index.file, data.models.index.content)

// /network
await writeFile(data.network.index.file, data.network.index.content)
await writeFile(data.network.routes.file, data.network.routes.content)
await writeFile(data.network.server.file, data.network.server.content)
await writeFile(data.network.index.file, data.network.index.content)

// /routes
await writeFile(data.routes.home.file, data.routes.home.content)
Expand Down

0 comments on commit 28403d8

Please sign in to comment.