Skip to content

Commit

Permalink
test(@sirutils/wizard): middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
siaeyy committed Oct 19, 2024
1 parent baac022 commit d050514
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
19 changes: 3 additions & 16 deletions packages/wizard/test/app.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
import './handler'
import './middlewares/auth'
import './services/users'
import './services/mails'
import './wizard'
import { wizard } from './wizard'

// import fs from 'node:fs'
// import { logger } from '../src/internal/logger'
// import { wizard } from './wizard'

// const result = await wizard.api.call(
// '[email protected]#create',
// {
// name: 'alice',
// },
// {
// stream: [fs.createReadStream('.env'), fs.createReadStream('tsconfig.json')],
// }
// )

// logger.log(result)
const result = await wizard.api.call('[email protected]#signOut', {}, {})
28 changes: 28 additions & 0 deletions packages/wizard/test/middlewares/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { wizard } from '../wizard'

export const auth = wizard.api.createMiddleware(
{
name: 'auth',
share: ['token'],
},
(ctx, next) => {
ctx.share.token = 'qwerty1234567890'
return next
}
)

declare global {
// biome-ignore lint/style/noNamespace: <explanatiion>
namespace Sirutils {
// biome-ignore lint/style/noNamespace: <explanatiion>
namespace Wizard {
interface ContextShare {
token: string
}
}

interface WizardMiddlewares {
auth: typeof auth
}
}
}
15 changes: 4 additions & 11 deletions packages/wizard/test/services/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,13 @@ const usersService = await wizard.api.service({
description: 'users api',

actions: {
create: wizard.api.createAction(
signOut: wizard.api.createAction(
{
body: {
name: 'string',
},
rest: true,
stream: true,
multipart: true,
middlewares: ['auth'],
},
ctx => {
if (ctx.streams) {
// biome-ignore lint/suspicious/noConsole: <explanation>
console.log(ctx.streams)
}
// biome-ignore lint/suspicious/noConsole: <explanation>
console.log(ctx.share?.token)

return 'sa' as const
}
Expand Down

0 comments on commit d050514

Please sign in to comment.