Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middleware For Wizard Actions #27

Closed
wants to merge 15 commits into from

Conversation

siaeyy
Copy link
Contributor

@siaeyy siaeyy commented Oct 17, 2024

Created 2 wizard API actions (createMiddleware and processMiddleware) and made minor changes on other actions.

By this definition, we have a global middleware named example :

export const example = wizard.api.createMiddleware(
    {
        name: 'example',
    },
    (ctx, next) => {
        return next
    }
)

If next is not returned back, it dont pass to next middleware,
If a data expect next is returned , the data returns as action return

We can use example middleware by stating in action creation:

const service = await wizard.api.service({
  name: 'service',
  version: '0.0.1',
  description: 'description',

  actions: {
    hello: wizard.api.createAction(
      {
        rest: 'GET /',
        middlewares: ['example']
      },
      ctx => {
        return "hello"
      }
    ),
  },
})

We can also state middlewares with MiddlewareSchemas (return type of createMiddleware)
This usage is mostly for nameless middlewares that created by factory functions

{
  middlewares: [example]
}

With middlewares, we need a structure to store all data created by middlewares, so after we can use these datas
Data structure that standing for this purpose is share

After define props like that:

declare global {
    namespace Sirutils {
        namespace Wizard {
            interface ContextShare {
                'user': {
                    name: string
                }
            }
        }
    }
}

And of course we must define middleware types:

declare global {
    namespace Sirutils {
        interface WizardMiddlewares {
            'example': typeof example
        }
    }
}

We can use it in middleware by stating:

export const example = wizard.api.createMiddleware(
    {
        name: 'example',
        share: ['user']
    },
    (ctx, next) => {
        ctx.share?.user = {
            name: "siaeyy"
        }
        return next
    }
)

When you use a middleware that access any share property for service action, we have types of that share props for handler context's share object:

{
   rest: 'GET /',
   middlewares: ['example']
},
   ctx => {
   return ctx.share?.user
   // undefined | { name: string }
}

The processMiddlewares method is for using it inside the package, not for user usage but it works like that:

wizard.api.processMiddlewares(ctx /* ActionContext */, [/* keyof WizardMiddlewares | MiddlewareSchema */])
// { continue: true} | { continue: false, returnedData: BlobType }

I have made some minor changes on important and fundamental types, these need to be reviewed
And new types that comes with this pr may need reworking

I had wrote comment lines in the code with care but they had to be gone with linter for deployments :')
@giveerr

@siaeyy siaeyy self-assigned this Oct 17, 2024
@siaeyy siaeyy added the enhancement New feature or request label Oct 17, 2024
@giveerr
Copy link
Contributor

giveerr commented Oct 17, 2024

can you fix the failing ci tasks and please don't create another test folder

@siaeyy
Copy link
Contributor Author

siaeyy commented Oct 18, 2024

I made the changes you mentioned but there is a problem with CI task, can you check it?
It's not successfull because of './test/services/mails.ts file is not found' and potantial secret errors
@giveerr

@giveerr
Copy link
Contributor

giveerr commented Nov 1, 2024

We're planning to move to our own microservice library instead of moleculer so wizard is not open to any new feature until then.

Thanks for your great work

@giveerr giveerr closed this Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants