Feat(medusa): Convert middleware service to typescript (#1936)

Fixes CORE-351
This commit is contained in:
Philip Korsholm authored and GitHub committed 2022-08-02 12:43:55 +00:00
1 parent 6251aecdaa
commit 87fc18137d
2 files changed
+37 -16

No files matched your search

@@ -41,7 +41,7 @@ describe("MiddlewareService", () => {
it("calls middleware", () => { it("calls middleware", () => {
// This doesn't reflect how middleware works but does suffice in our // This doesn't reflect how middleware works but does suffice in our
// testing situation // testing situation
const mid = args => args const mid = (args) => args
middlewareService.addPostAuthentication(mid, { data: "yes" }) middlewareService.addPostAuthentication(mid, { data: "yes" })
@@ -62,7 +62,7 @@ describe("MiddlewareService", () => {
it("calls middleware", () => { it("calls middleware", () => {
// This doesn't reflect how middleware works but does suffice in our // This doesn't reflect how middleware works but does suffice in our
// testing situation // testing situation
const mid = args => args const mid = (args) => args
middlewareService.addPreAuthentication(mid, { data: "yes" }) middlewareService.addPreAuthentication(mid, { data: "yes" })
@@ -1,31 +1,46 @@
import { RequestHandler, Router } from "express"
import { MedusaError } from "medusa-core-utils" import { MedusaError } from "medusa-core-utils"
type middlewareHandlerType = (
options: Record<string, unknown>
) => RequestHandler
type middlewareType = {
middleware: middlewareHandlerType
options: Record<string, unknown>
}
/** /**
* Orchestrates dynamic middleware registered through the Medusa Middleware API * Orchestrates dynamic middleware registered through the Medusa Middleware API
*/ */
class MiddlewareService { class MiddlewareService {
constructor(container) { protected readonly postAuthentication_: middlewareType[]
protected readonly preAuthentication_: middlewareType[]
protected readonly preCartCreation_: RequestHandler[]
protected readonly routers: Record<string, Router[]>
constructor() {
this.postAuthentication_ = [] this.postAuthentication_ = []
this.preAuthentication_ = [] this.preAuthentication_ = []
this.preCartCreation_ = [] this.preCartCreation_ = []
this.routers = {} this.routers = {}
} }
addRouter(path, router) { addRouter(path: string, router: Router): void {
const existing = this.routers[path] || [] const existing = this.routers[path] || []
this.routers[path] = [...existing, router] this.routers[path] = [...existing, router]
} }
getRouters(path) { getRouters(path: string): Router[] {
const routers = this.routers[path] || [] return this.routers[path] || []
return routers
} }
/** /**
* Validates a middleware function, throws if fn is not of type function. * Validates a middleware function, throws if fn is not of type function.
* @param {function} fn - the middleware function to validate. * @param {function} fn - the middleware function to validate.
* @returns nothing if the middleware is a function
*/ */
validateMiddleware_(fn) { validateMiddleware_(fn: unknown): void {
if (typeof fn !== "function") { if (typeof fn !== "function") {
throw new MedusaError( throw new MedusaError(
MedusaError.Types.NOT_ALLOWED, MedusaError.Types.NOT_ALLOWED,
@@ -40,9 +55,12 @@ class MiddlewareService {
* middleware function. * middleware function.
* @param {object} options - the arguments that will be passed to the * @param {object} options - the arguments that will be passed to the
* middleware * middleware
* @return {void} * @return void
*/ */
addPostAuthentication(middleware, options) { addPostAuthentication(
middleware: middlewareHandlerType,
options: Record<string, unknown>
): void {
this.validateMiddleware_(middleware) this.validateMiddleware_(middleware)
this.postAuthentication_.push({ this.postAuthentication_.push({
middleware, middleware,
@@ -56,9 +74,12 @@ class MiddlewareService {
* middleware function. * middleware function.
* @param {object} options - the arguments that will be passed to the * @param {object} options - the arguments that will be passed to the
* middleware * middleware
* @return {void} * @return void
*/ */
addPreAuthentication(middleware, options) { addPreAuthentication(
middleware: middlewareHandlerType,
options: Record<string, unknown>
): void {
this.validateMiddleware_(middleware) this.validateMiddleware_(middleware)
this.preAuthentication_.push({ this.preAuthentication_.push({
middleware, middleware,
@@ -72,7 +93,7 @@ class MiddlewareService {
* middleware function. * middleware function.
* @return {void} * @return {void}
*/ */
addPreCartCreation(middleware) { addPreCartCreation(middleware: RequestHandler): void {
this.validateMiddleware_(middleware) this.validateMiddleware_(middleware)
this.preCartCreation_.push(middleware) this.preCartCreation_.push(middleware)
} }
@@ -82,7 +103,7 @@ class MiddlewareService {
* @param {ExpressApp} app - the express app to add the middleware to * @param {ExpressApp} app - the express app to add the middleware to
* @return {void} * @return {void}
*/ */
usePostAuthentication(app) { usePostAuthentication(app: Router): void {
for (const object of this.postAuthentication_) { for (const object of this.postAuthentication_) {
app.use(object.middleware(object.options)) app.use(object.middleware(object.options))
} }
@@ -93,13 +114,13 @@ class MiddlewareService {
* @param {ExpressApp} app - the express app to add the middleware to * @param {ExpressApp} app - the express app to add the middleware to
* @return {void} * @return {void}
*/ */
usePreAuthentication(app) { usePreAuthentication(app: Router): void {
for (const object of this.preAuthentication_) { for (const object of this.preAuthentication_) {
app.use(object.middleware(object.options)) app.use(object.middleware(object.options))
} }
} }
usePreCartCreation() { usePreCartCreation(): RequestHandler[] {
return this.preCartCreation_ return this.preCartCreation_
} }
} }