EasyPost / easypost-node

EasyPost Shipping API Client Library for Node
https://easypost.com/docs/api
MIT License
139 stars 54 forks source link

[Bug]: TypeError: api_1.default is not a constructor in Nestjs #446

Open mustafakendiguzel opened 7 months ago

mustafakendiguzel commented 7 months ago

Software Version

7.3.0

Language Version

21.4.0

Operating System

Windows

What happened?

  1. Imported easypost-node library in nestjs
  2. Attempt to create new EasyPostClient via
    import EasyPost from '@easypost/api';
    export class ShippingMailingService {
    private readonly easyPostClient: EasyPost
    constructor() { 
    this.easyPostClient = new EasyPost('apiKey')
    }
    }
  3. Upon building, error is thrown that . TypeError: api_1.default is not a constructor
  4. Tsconfig.json
    {
    "compilerOptions": {
    "module": "commonjs",
    "declaration": false,
    "removeComments": true,
    "noLib": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "resolveJsonModule": true,
    "target": "es2017",
    "sourceMap": true,
    "allowJs": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "lib": ["ES7", "ES2021", "DOM"],
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false,
    }
    }

What was expected?

To be able to build the app while using EasyPostClient

Sample Code

import EasyPost from '@easypost/api';

export class ShippingMailingService {
  private readonly easyPostClient: EasyPost
  constructor() { 
    this.easyPostClient = new EasyPost('apiKey')
  }
}

Relevant logs

No response

nwithan8 commented 7 months ago

Hello there, thanks for writing in!

I believe there's an issue with your provided code snippet, you should be trying to construct an EasyPostClient object, not an EasyPost object

import EasyPostClient from '@easypost/api'; 

export class ShippingMailingService { 
  private readonly easyPostClient: EasyPostClient constructor() { this.easyPostClient = new EasyPostClient('apiKey') } }
mustafakendiguzel commented 7 months ago

Hello there, thanks for writing in!

I believe there's an issue with your provided code snippet, you should be trying to construct an EasyPostClient object, not an EasyPost object

import EasyPostClient from '@easypost/api'; 

export class ShippingMailingService { 
  private readonly easyPostClient: EasyPostClient constructor() { this.easyPostClient = new EasyPostClient('apiKey') } }

Thanks for the answer @nwithan8. I get the same error that didn't solve this problem.

heindrik commented 3 months ago

has anyone found a workaround for this yet?

AshuImmencer05 commented 1 month ago

Even i tried these solutions but still i am getting the same error

chr0m1ng commented 1 month ago

@AshuImmencer05 @heindrik @mustafakendiguzel Hey folks, I've found a workaround, import under dist/easypost:

import EasyPostClient from '@easypost/api/dist/easypost'

const client = new EasyPostClient(api_key)
AshuImmencer05 commented 1 month ago

@chr0m1ng Thank you so much ,your solution was really helpful it worked out pretty well..

AshuImmencer05 commented 1 month ago

@heindrik @chr0m1ng @nwithan8 @Justintime50 @mustafakendiguzel Guys I just wanted to ask a simple question, as i create shipment using api from the backend response object i get from easypost is very large so basically which has properties that are not required so how to get an ideal response which can help reduce the response time and prevent us from security breaks and other stuff etc?

nwithan8 commented 1 month ago

@heindrik @chr0m1ng @nwithan8 @Justintime50 @mustafakendiguzel Guys I just wanted to ask a simple question, as i create shipment using api from the backend response object i get from easypost is very large so basically which has properties that are not required so how to get an ideal response which can help reduce the response time and prevent us from security breaks and other stuff etc?

The API will always return the complete record for any given object (e.g. a Shipment), so there is no way to necessarily reduce the response payload size. You can reduce your request payloads by sending only the ID of existing server-side records (e.g. the ID of an existing Shipment), as that is all the server requires to locate and interact with an existing record.

This question is unrelated to the original issue. To avoid pinging the other subscribers to this thread, if you have any additional questions, please open a separate ticket or reach out to our support team at support@easypost.com.