Skip to content

Commit

Permalink
fix(core): fix the middleware apply bug
Browse files Browse the repository at this point in the history
fix the koaExperienceInteraction middleware apply bug
  • Loading branch information
simeng-li committed Jul 31, 2024
1 parent 4de522f commit 7d237c5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
19 changes: 8 additions & 11 deletions packages/core/src/routes/experience/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@ export default function experienceApiRoutes<T extends AnonymousRouter>(
) {
const { queries } = tenant;

const routerWithLogs = anonymousRouter.use(koaAuditLog(queries));
const experienceRouter =
// @ts-expect-error for good koa types
// eslint-disable-next-line no-restricted-syntax
(anonymousRouter as Router<unknown, WithExperienceInteractionContext<RouterContext<T>>>).use(
koaAuditLog(queries),
koaExperienceInteraction(tenant)
);

// Should not apply the koaExperienceInteraction middleware to this route.
// New ExperienceInteraction instance will be created.
routerWithLogs.put(
experienceRouter.put(
experienceRoutes.prefix,
koaGuard({
body: z.object({
Expand All @@ -71,13 +75,6 @@ export default function experienceApiRoutes<T extends AnonymousRouter>(
}
);

const experienceRouter =
// @ts-expect-error for good koa types
// eslint-disable-next-line no-restricted-syntax
(routerWithLogs as Router<unknown, WithExperienceInteractionContext<RouterContext<T>>>).use(
koaExperienceInteraction(tenant)
);

experienceRouter.put(
`${experienceRoutes.prefix}/interaction-event`,
koaGuard({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { type WithLogContext } from '#src/middleware/koa-audit-log.js';
import type TenantContext from '#src/tenants/TenantContext.js';

import ExperienceInteraction from '../classes/experience-interaction.js';
import { experienceRoutes } from '../const.js';

export type WithExperienceInteractionContext<ContextT extends WithLogContext = WithLogContext> =
ContextT & {
Expand All @@ -25,6 +26,14 @@ export default function koaExperienceInteraction<
tenant: TenantContext
): MiddlewareType<StateT, WithExperienceInteractionContext<ContextT>, ResponseT> {
return async (ctx, next) => {
const { method, path } = ctx.request;

// Should not apply the koaExperienceInteraction middleware to the PUT /experience route.
// New ExperienceInteraction instance are supposed to be created in the PUT /experience route.
if (method === 'PUT' && path === `${experienceRoutes.prefix}`) {
return next();
}

Check warning on line 36 in packages/core/src/routes/experience/middleware/koa-experience-interaction.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes/experience/middleware/koa-experience-interaction.ts#L29-L36

Added lines #L29 - L36 were not covered by tests
const { provider } = tenant;
const interactionDetails = await provider.interactionDetails(ctx.req, ctx.res);

Expand Down

0 comments on commit 7d237c5

Please sign in to comment.