Skip to content

Commit ac19ee9

Browse files
committed
test(@sirutils/wizard): middleware
1 parent 6f678d7 commit ac19ee9

File tree

7 files changed

+161
-0
lines changed

7 files changed

+161
-0
lines changed
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import './handler'
2+
import './middlewares/auth'
3+
import './services/users'
4+
import './wizard'
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { extractEnvs } from '@sirutils/core'
2+
3+
export const ENV = extractEnvs<
4+
Sirutils.DriverScylla.Env &
5+
Sirutils.DriverRedis.Env & {
6+
mode: Exclude<Sirutils.Wizard.Options['environment'], undefined>
7+
}
8+
>(env => ({
9+
scyllaContactPoints: env.SCYLLA_CONTACT_POINTS?.split(','),
10+
scyllaLocalDataCenter: env.SCYLLA_LOCAL_DATA_CENTER,
11+
scyllaPassword: env.SCYLLA_PASSWORD,
12+
scyllaUsername: env.SCYLLA_USERNAME,
13+
14+
redisHost: env.REDIS_HOST,
15+
redisPort: env.REDIS_PORT,
16+
redisPassword: env.REDIS_PASSWORD,
17+
redisUsername: env.REDIS_USERNAME,
18+
redisDatabase: env.REDIS_DATABASE || '0',
19+
20+
mode: env.NODE_ENV || 'development',
21+
}))
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { createRedisDriver } from '@sirutils/driver-redis'
2+
import { createScyllaDriver } from '@sirutils/driver-scylla'
3+
4+
import { ENV } from './consts'
5+
6+
export const redis = await createRedisDriver({
7+
client: {
8+
host: ENV.redisHost,
9+
port: ENV.redisPort,
10+
db: ENV.redisDatabase,
11+
password: ENV.redisPassword,
12+
},
13+
})
14+
15+
export const scylla = await createScyllaDriver(
16+
{
17+
client: {
18+
contactPoints: ENV.scyllaContactPoints,
19+
localDataCenter: ENV.scyllaLocalDataCenter,
20+
credentials: { username: ENV.scyllaUsername, password: ENV.scyllaPassword },
21+
keyspace: 'sirius',
22+
},
23+
},
24+
redis
25+
)
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ProjectError } from '@sirutils/core'
2+
3+
import { logger } from '../src/internal/logger'
4+
5+
process
6+
.on('unhandledRejection', (_reason, p) => {
7+
if (p instanceof ProjectError) {
8+
p.appendCause('?handler')
9+
logger.error(p.stringify())
10+
} else {
11+
logger.error(p)
12+
}
13+
})
14+
.on('uncaughtException', err => {
15+
if (err instanceof ProjectError) {
16+
err.appendCause('?handler')
17+
logger.error(err.stringify())
18+
} else {
19+
logger.error(err)
20+
}
21+
22+
process.exit(1)
23+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { wizard } from '../wizard'
2+
3+
export const auth = wizard.api.createMiddleware(
4+
{
5+
name: 'auth',
6+
share: ['user']
7+
},
8+
(ctx, next) => {
9+
if(ctx.req && ctx.res) {
10+
const authHeader = ctx.req.headers.authorization
11+
//biome-ignore lint/complexity/useOptionalChain: <explanation>
12+
const token = authHeader && authHeader.split(" ")[1]
13+
14+
if (token !== undefined) {
15+
ctx.share.user = {
16+
name: "siaeyy"
17+
}
18+
return next
19+
}
20+
21+
ctx.res.statusCode = 404
22+
ctx.res.end("Authorization is not found!")
23+
return
24+
}
25+
26+
return undefined
27+
}
28+
)
29+
30+
declare global {
31+
//biome-ignore lint/style/noNamespace: <explanation>
32+
namespace Sirutils {
33+
//biome-ignore lint/style/noNamespace: <explanation>
34+
namespace Wizard {
35+
interface ContextShare {
36+
'user': {
37+
name: string
38+
}
39+
}
40+
}
41+
42+
interface WizardMiddlewares {
43+
'auth': typeof auth
44+
}
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { wizard } from '../wizard'
2+
3+
const usersService = await wizard.api.service({
4+
name: 'users',
5+
version: '0.0.1',
6+
description: 'users api',
7+
8+
actions: {
9+
get: wizard.api.createAction(
10+
{
11+
rest: 'GET /',
12+
middlewares: ['auth']
13+
},
14+
ctx => {
15+
//biome-ignore lint/suspicious/noConsole: <explanation>
16+
console.log(ctx.share?.user)
17+
18+
return JSON.stringify(ctx.share?.user)
19+
}
20+
),
21+
},
22+
})
23+
24+
declare global {
25+
// biome-ignore lint/style/noNamespace: <explanation>
26+
namespace Sirutils {
27+
interface WizardServices {
28+
'users@0.1.1': typeof usersService
29+
}
30+
}
31+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { createWizard } from '../src'
2+
import { ENV } from './consts'
3+
import { redis, scylla } from './drivers'
4+
5+
export const wizard = await createWizard(
6+
{
7+
environment: ENV.mode,
8+
},
9+
redis,
10+
scylla
11+
)

0 commit comments

Comments
 (0)