pwi-plata-type 0.2.21 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as PlataCluster from './libs/cluster.js'
2
2
  import * as PlataEnv from './libs/env.js'
3
3
  import * as PlataRoutes from './libs/routes.js'
4
- import { PlataRoute } from './libs/routes.js'
4
+ import { PlataRouter } from './libs/routes.js'
5
5
  import * as PlataTools from './libs/tools.js'
6
6
  import * as PlataModels from './libs/model.js'
7
7
  import { PlataSwagger } from './libs/swagger.js'
@@ -12,6 +12,6 @@ export {
12
12
  PlataRoutes,
13
13
  PlataTools,
14
14
  PlataModels,
15
- PlataRoute,
15
+ PlataRouter as PlataRouter,
16
16
  PlataSwagger
17
17
  }
package/libs/model.ts CHANGED
@@ -28,6 +28,7 @@ export class Model {
28
28
  constructor(template: ModelType) {
29
29
  this.template = template
30
30
  this.validacoes = new Map()
31
+ this.tratamentos = new Map()
31
32
  }
32
33
 
33
34
  public addValidacao(nome: string, callback: ModelValicacao) {
package/libs/routes.ts CHANGED
@@ -1,11 +1,92 @@
1
+ import path from 'node:path'
1
2
  import { PlataDirs, PlataRequire, PlataError } from "./tools.js";
2
3
  import express, { Router } from "express";
3
4
 
4
- export type _Router = Router & { swaggerHide?: boolean }
5
+ export const HttpMethodsArray = [
6
+ "all"
7
+ ,"get"
8
+ ,"post"
9
+ ,"put"
10
+ ,"delete"
11
+ ,"patch"
12
+ ,"options"
13
+ ,"head"
14
+ ,"checkout"
15
+ ,"connect"
16
+ ,"copy"
17
+ ,"lock"
18
+ ,"merge"
19
+ ,"mkactivity"
20
+ ,"mkcol"
21
+ ,"move"
22
+ ,"notify"
23
+ ,"propfind"
24
+ ,"proppatch"
25
+ ,"purge"
26
+ ,"report"
27
+ ,"search"
28
+ ,"subscribe"
29
+ ,"trace"
30
+ ,"unlock"
31
+ ,"unsubscribe"
32
+ ,"use"
33
+ ]
34
+
35
+ export type HttpMethods =
36
+ "all"
37
+ | "get"
38
+ | "post"
39
+ | "put"
40
+ | "delete"
41
+ | "patch"
42
+ | "options"
43
+ | "head"
44
+ | "checkout"
45
+ | "connect"
46
+ | "copy"
47
+ | "lock"
48
+ | "merge"
49
+ | "mkactivity"
50
+ | "mkcol"
51
+ | "move"
52
+ | "notify"
53
+ | "propfind"
54
+ | "proppatch"
55
+ | "purge"
56
+ | "report"
57
+ | "search"
58
+ | "subscribe"
59
+ | "trace"
60
+ | "unlock"
61
+ | "unsubscribe"
62
+ | "use"
63
+ ;
64
+
65
+ export type PlataRouterMethods = (
66
+ (path: string | PlataRequestHandler, ...handlers: PlataRequestHandler[]) => void | Promise<void>
67
+ )
68
+
69
+ export interface _PlataRoute {
70
+ path?: string,
71
+ method: HttpMethods
72
+ handlers: PlataRequestHandler[]
73
+ }
74
+ export interface PlataRouterExtras {
75
+ swaggerHide?: boolean
76
+ _routes?: _PlataRoute[]
77
+ }
78
+
79
+ export type PlataRouter = PlataRouterExtras & {
80
+ [method in HttpMethods]: PlataRouterMethods
81
+ }
5
82
 
6
- export type PlataRouter = () => _Router | Promise<_Router>
83
+ export type _Router = Router & PlataRouterExtras
7
84
 
8
- export type PlataRequest = express.Request & { user: any, extras: any }
85
+ export type PlataRouterBuilder = (r: PlataRouter) => PlataRouter | Promise<PlataRouter>
86
+
87
+ export type PlataRequest = express.Request & {
88
+ user: any, extras: any
89
+ }
9
90
 
10
91
  export type PlataOnResponse = (req: PlataRequest, response?: any, error?: PlataError, nodeError?: any) => Promise<any>
11
92
  export interface PlataResponse extends express.Response {
@@ -16,62 +97,36 @@ export interface PlataResponse extends express.Response {
16
97
  addOnResponse(callback: PlataOnResponse): void
17
98
  }
18
99
 
19
- export interface PlataRequiredRoute extends PlataRequire.RequiredInterface<PlataRouter> {
100
+ export interface PlataRequiredRoute extends PlataRequire.RequiredInterface<PlataRouterBuilder> {
20
101
  httpRoute: string
21
102
  }
22
103
 
23
- export async function loadExpressRoutes(): Promise<PlataRequiredRoute[]> {
24
- const routes = await PlataRequire.requireFolderAsync<PlataRouter>(PlataDirs.getProjectRoutesDir())
25
-
26
- if (routes.errorID === undefined) {
27
- if (routes.findIndex(r => r.errorID !== undefined) !== -1) {
28
- routes.forEach(r => {
29
- if (r.errorID !== undefined) {
30
- console.log(r)
31
- }
32
- })
33
-
34
- throw routes
104
+ export function createPlataRouter(): any {
105
+ const router: any = new Object(null)
106
+ router._routes = []
107
+
108
+ for (let i = 0; i < HttpMethodsArray.length; i++) {
109
+ const method = HttpMethodsArray[i];
110
+
111
+ router[method] = (arg, ...args) => {
112
+ if (typeof arg === 'string') {
113
+ router._routes.push(
114
+ {
115
+ path: arg,
116
+ method: method,
117
+ handlers: args
118
+
119
+ } as _PlataRoute
120
+ )
121
+ } else {
122
+ router._routes.push(
123
+ {
124
+ method: method,
125
+ handlers: [].concat(arg, ...args)
126
+ } as _PlataRoute
127
+ )
128
+ }
35
129
  }
36
-
37
- let r = (routes as PlataRequiredRoute[])
38
-
39
- // Cria as rotas http no array
40
- r = r.map(route => {
41
- route.httpRoute = route.filePath
42
- .replace(PlataDirs.getProjectRoutesDir(), '')
43
- .replace(/.(ts|js)$/, '')
44
- .replace(/index/, '')
45
- .replace(/\\/g, '/')
46
- .replace(/\{(.*?)\}/g, ':$1')
47
- .replace(/\[(.*?)\]/g, ':$1?')
48
- ;
49
-
50
- return route
51
- })
52
-
53
- // coloca o index na frente de todas as rotas
54
- r = r.sort((x,y) => {
55
- if (x.name === 'index') return -1
56
-
57
- return +(x.name > y.name)
58
- })
59
-
60
- return r
61
- } else {
62
- console.log(routes)
63
- throw routes
64
- }
65
- }
66
-
67
- export async function buildExpressRouter(): Promise<Router> {
68
- const router = Router()
69
- router.use(express.json())
70
-
71
- const routes = await loadExpressRoutes()
72
-
73
- for (const r of routes) {
74
- router.use(r.httpRoute, await r.exports())
75
130
  }
76
131
 
77
132
  return router
@@ -128,4 +183,82 @@ export function PlataRoute(r: PlataRequestHandler): express.RequestHandler {
128
183
 
129
184
  return r(aReq, aRes, next)
130
185
  }
186
+ }
187
+
188
+ export async function loadExpressRoutes(): Promise<PlataRequiredRoute[]> {
189
+ const routes = await PlataRequire.requireFolderAsync<PlataRouterBuilder>(PlataDirs.getProjectRoutesDir())
190
+
191
+ if (routes.errorID === undefined) {
192
+ if (routes.findIndex(r => r.errorID !== undefined) !== -1) {
193
+ routes.forEach(r => {
194
+ if (r.errorID !== undefined) {
195
+ console.log(r)
196
+ }
197
+ })
198
+
199
+ throw routes
200
+ }
201
+
202
+ let r = (routes as PlataRequiredRoute[])
203
+
204
+ // Cria as rotas http no array
205
+ r = r.map(route => {
206
+ route.httpRoute = route.filePath
207
+ .replace(PlataDirs.getProjectRoutesDir(), '')
208
+ .replace(/.(ts|js)$/, '')
209
+ .replace(/index/, '')
210
+ .replace(/\\/g, '/')
211
+ .replace(/\{(.*?)\}/g, ':$1')
212
+ .replace(/\[(.*?)\]/g, ':$1?')
213
+ ;
214
+
215
+ return route
216
+ })
217
+
218
+ // coloca o index na frente de todas as rotas
219
+ r = r.sort((x,y) => {
220
+ if (x.name === 'index') {
221
+ if (y.name === 'index') {
222
+ return +(x.httpRoute.length > y.httpRoute.length)
223
+ }
224
+
225
+ return -1
226
+ }
227
+
228
+ return +(x.name > y.name)
229
+ })
230
+
231
+ return r
232
+ } else {
233
+ console.log(routes)
234
+ throw routes
235
+ }
236
+ }
237
+
238
+ export async function buildExpressRouter(): Promise<Router> {
239
+ const router = Router()
240
+ router.use(express.json())
241
+
242
+ const routes = await loadExpressRoutes()
243
+
244
+ for (const r of routes) {
245
+ const pr = await r.exports(createPlataRouter())
246
+
247
+ pr._routes = pr._routes ?? []
248
+
249
+ for (let i = 0; i < pr._routes.length; i++) {
250
+ const prr = pr._routes[i];
251
+
252
+ if (prr.path === undefined) {
253
+ (router as any)[prr.method](...prr.handlers.map(PlataRoute))
254
+ } else {
255
+ (router as any)[prr.method](
256
+ path.normalize(`${r.httpRoute}/${prr.path}`),
257
+ ...prr.handlers.map(PlataRoute)
258
+ )
259
+ }
260
+ }
261
+ }
262
+
263
+ return router
131
264
  }
package/libs/swagger.ts CHANGED
@@ -1,19 +1,21 @@
1
1
  import swaggerUi from 'swagger-ui-express'
2
2
  import { Router } from 'express'
3
3
  import path from 'node:path'
4
- import { _Router } from './routes.js';
4
+ import { PlataRouterBuilder, PlataRequestHandler } from './routes.js';
5
5
  import { PlataFiles, PlataDirs } from "./tools.js";
6
6
 
7
7
  export namespace PlataSwagger {
8
- export async function swagger(): Promise<_Router> {
9
- const router = Router() as _Router
8
+ export const swagger: PlataRouterBuilder = async (router) => {
10
9
  router.swaggerHide = true
11
10
 
12
11
  if (process.env._plata_name === '_plata_express') {
13
- router.use(swaggerUi.serve, swaggerUi.setup(
14
- await _loadSwaggerConfig(),
15
- {}
16
- ))
12
+ router.use(
13
+ swaggerUi.serve as unknown as PlataRequestHandler,
14
+ swaggerUi.setup(
15
+ await _loadSwaggerConfig(),
16
+ {}
17
+ ) as unknown as PlataRequestHandler
18
+ )
17
19
  }
18
20
 
19
21
  return router
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pwi-plata-type",
3
- "version": "0.2.21",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "main": "index.ts",
6
6
  "bin": {
@@ -23,7 +23,8 @@
23
23
  "jest": "^28.1.1",
24
24
  "swagger-ui-express": "^4.4.0",
25
25
  "ts-jest": "^28.0.4",
26
- "ts-node": "10.8.2"
26
+ "ts-node": "10.8.2",
27
+ "express": "^4.18.1"
27
28
  },
28
29
  "description": "",
29
30
  "devDependencies": {
@@ -1,13 +1,11 @@
1
1
  //@ts-nocheck
2
- import { Router } from 'express'
3
- import { PlataRoute, PlataRoutes } from 'ç__PlataName__ç'
2
+ import { PlataRouter } from 'ç__PlataName__ç'
4
3
 
5
- export default (): PlataRoutes._Router => {
6
- const router = Router() as PlataRoutes._Router
4
+ export default (router: PlataRouter): PlataRouter => {
7
5
 
8
- router.get('/', PlataRoute(async (req, res) => {
6
+ router.get('/', async (req, res) => {
9
7
  return res.status(200).json({})
10
- }))
8
+ })
11
9
 
12
10
  return router
13
11
  }