Skip to content

Custom middleware not called #671

@dbartholomae

Description

@dbartholomae

Hi there,

I've tried adding a custom logging middleware, but it never gets called. I've debugged a bit into the library and the middleware is correctly added to the config, but once in the codegen, I've lost the trail. From what I can see, no middleware is ever called in the codegen code, though.

Here's my code:
LoggerMiddleware.ts

export class LoggerMiddleware {
  private readonly logger: Logger;

  constructor(logger: Logger) {
    this.logger = logger;
  }

  // Use arrow functions to preserve `this` when methods are extracted by the HubSpot SDK
  pre = (context: RequestContext) => {
    this.logger.debug(
      {
        method: context.getHttpMethod(),
        url: context.getUrl(),
        body: context.getBody(),
      },
      "Sending Hubspot request"
    );
    return context;
  };

  post = async (context: ResponseContext) => {
    this.logger.debug(
      {
        statusCode: context.httpStatusCode,
        body: (await context.getBodyAsAny())?.toString(),
      },
      "Receiving Hubspot response"
    );
    return context;
  };
}

LoggerMiddleware.test.ts

    const middleware = new LoggerMiddleware(logger);

    const hubspotClient = new Hubspot.Client({
      accessToken: secret.hubspotAccessToken,
      middleware: [middleware],
    });

    // Perform a minimal API call to trigger the middleware.
    // We create and immediately delete a contact to avoid leaving state behind.
    const email = `logger-mw-test+${Date.now()}@example.com`;
    const created = await hubspotClient.crm.contacts.basicApi.create({
      properties: {
        email,
        firstname: "Logger",
        lastname: "MiddlewareTest",
      },
    });

The logger is never called.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions