question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hello,

I am having an issue when trying to use the library with typescript in NestJS. I have the following code :

import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import Mailjet, { SendEmailV3_1 } from 'node-mailjet';

@Injectable()
export class MailjetService {
  private mailjet: Mailjet;

  constructor(private readonly configService: ConfigService) {
    this.mailjet = new Mailjet({
      apiKey: this.configService.get('MJ_APIKEY_PUBLIC'),
      apiSecret: this.configService.get('MJ_APIKEY_PRIVATE'),
    });
  }

  async sendForgottenPasswordMail(token: string, recipientMail: string) {
    const data: SendEmailV3_1.IBody = {
      Messages: [
        {
          From: {
            Email: 'some@email.com',
            Name: 'SomeName',
          },
          To: [{ Email: recipientMail }],
          TemplateID: SOME_TEMPLATE_ID,
        },
      ],
    };

    const res = await this.mailjet
      .post('send', { version: 'v3.1' })
      .request<SendEmailV3_1.IResponse>(data);

    const { Status } = res.body.Messages[0];
  }
}

Typescript says :

Type 'IResponse' does not satisfy the constraint 'TRequestData'.
Type 'IResponse' is not assignable to type 'TUnknownRec'.
Index signature for type 'string' is missing in type 'IResponse'.ts(2344)

I can’t see any difference between my current code here and the documentation example, so any help is welcome. I think I’m probably missing something here.

package.json version : 5.1.1

Bests, Max

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:21
  • Comments:21

github_iconTop GitHub Comments

4reactions
woodinteractivecommented, Aug 31, 2022

I’m having an issue just trying to get it instantiated.

export class MailjetService {
	private _transporter: nodemailer.Transporter
	private mailjet: Mailjet
	constructor(private readonly configService: ConfigService) {
		this.mailjet = new Mailjet({
			apiKey: process.env.MJ_APIKEY_PUBLIC,
			apiSecret: process.env.MJ_APIKEY_PRIVATE
		})
...

Error output says: TypeError: node_mailjet_1.default is not a constructor

Am I missing something?

2reactions
Codesleuthcommented, Oct 14, 2022

I think that the following code is fundamentally flawed:

const result = await mailjet
  .post('send', { version: 'v3.1' })
  .request({ ...data });

// result is ILibraryResponse<TRequestData>

The type of result should not represent TRequestData - that’s a request shape, not a response shape. You actually want it to be ILibraryResponse<SendEmailV3_1.IResponse> but it isn’t compatible.

A workaround is to use a type predicate:

function isSendResponse(r: unknown): r is ILibraryResponse<SendEmailV3_1.IResponse> {
  if (r == null || typeof r !== 'object') return false;
  const o = r as { body?: { Messages: unknown } };
  return o.body?.Messages != null && Array.isArray(o.body.Messages);
}

const result = await mailjet
  .post('send', { version: 'v3.1' })
  .request({ ...data });

if (!isSendResponse(result)) {
  // probably never hit, but you just don't know
  throw new Error('Unexpected response body');
}

const { Status } = result.body.Messages[0];
// ...

Arguably, this is more reliable than if the type system worked today. At least in this code, there’s runtime checking of the response to give you some level of confidence that the API response is what you expect. Feel free to extend isSendResponse with further checks for what you need (maybe a json schema check?)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · microsoft/TypeScript - GitHub
TypeScript is a superset of JavaScript that compiles to clean JavaScript output. - Issues · microsoft/TypeScript.
Read more >
Documentation - TypeScript 3.9
TypeScript 3.9 addresses this issue by changing the internals of how the compiler and language service caches file lookups.
Read more >
7 really good reasons not to use TypeScript - everyday.codes
Everyone loves TypeScript. It “solves” many problems JS has, it is a “superset” of JS, it will make your code error-prone and pleasant...
Read more >
Common TypeScript module problems and how to solve them
Common TypeScript module problems and how to solve them · Introduction · Problem 1: Irregular location of dependencies · Solution 1: Locate the ......
Read more >
Pros and Cons of TypeScript - AltexSoft
TypeScript cons: what problems it creates · Not true static typing · One more JavaScript to learn · Bloated code · Adding extra...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found