@ticketmaster/allure-mock
TypeScript icon, indicating that this package has built-in type declarations

5.5.12-alpha.0 • Public • Published

@ticketmaster/allure-mock

Allure common mocking solution library.


This library is a collection of shared mocking middlewares to mock shared services inside an Allure project.

Usage

1. Install dependencies

First, you need to install the dependencies.

yarn add @ticketmaster/allure-mock -D

2. Create the mock server creator

If you don't already have one, you have to create a function to instantiate a mock server. This function will be called later if we need to mock calls.

We recommend to put this file at the root of your src folder and call it middleware.ts.

// src/middleware.ts

import { NextRequest, NextResponse } from "next/server";
import createMiddleware from "next-intl/middleware";
import { AVAILABLE_LOCALES, DEFAULT_LOCALE } from "./config";
import {
  applicationsMockApi,
  identityMockApi,
} from "@ticketmaster/allure-mock";

export function middleware(request: NextRequest) {
const defaultLocale = request.headers.get("NEXT_LOCALE") || DEFAULT_LOCALE;

const handleI18nRouting = createIntlMiddleware({
  locales: AVAILABLE_LOCALES,
  defaultLocale,
  localeDetection: true,
});

const response = handleI18nRouting(request);

response.headers.set("NEXT_LOCALE", defaultLocale);

export const config = {
  matcher: [
    "/((?!_next).*)", // do not localize next.js paths
    `/api/v1/:path*`, // Limit the middleware to paths starting with `/api/v1`
  ],
};

export function middleware(req: NextRequest) {
  if (process.env.NODE_ENV === "development") {
    const { pathname } = request.nextUrl;
    const endpointsMap: {
      [key: string]: (nextRequest: NextRequest) => Promise<NextResponse>;
    } = {
      "/applications": applicationsMockApi,
      "/authenticate/token": identityMockApi,
      "/token/refresh": identityMockApi,
      "/token/verify": identityMockApi,
      "/sessions/logout": identityMockApi,
    };

    if (pathname.startsWith("/api/v1")) {
      const path = pathname.split("/api/v1");
      const endpoint = path.length > 0 ? path[1] : "";

      for (const [key, value] of Object.entries(endpointsMap)) {
        if (endpoint.match(key)) {
          return value(request);
        }
      }
    }
  }

  return NextResponse.next();
}

Dependents (0)

Package Sidebar

Install

npm i @ticketmaster/allure-mock

Weekly Downloads

802

Version

5.5.12-alpha.0

License

MIT

Unpacked Size

47.1 kB

Total Files

14

Last publish

Collaborators

  • traynak
  • sk89q
  • tmtech
  • george.yue
  • tm_platinum
  • chestersound
  • taddei
  • tmsean
  • iccpfrontend
  • tm1-marketing
  • resaleengineering
  • ccpdiscovery
  • tm1salesfrontend
  • lndtfrontend
  • b2bauroraextensionsdevelopers
  • livn.spons.frontend
  • dansel
  • mikeanama
  • mindosilalahi
  • tmlabs.projects
  • secureaccountsnpm
  • edpteam
  • eibfrontend
  • lndigital
  • safetix_ui_tool
  • namme
  • corpwebsites
  • presencefrontend
  • tm1reports
  • afterpay-ticketmaster
  • tm-intl-enterprise
  • wojciech.jawor
  • zbremantm