XeroAPI / xero-node

Xero Node SDK for OAuth 2.0 generated from XeroAPI/Xero-OpenAPI
http://developer.xero.com/
MIT License
192 stars 155 forks source link

error TS7016: Could not find a declaration file for module 'request'. #657

Closed rdok closed 4 months ago

rdok commented 7 months ago

SDK Version: 4.37.0

Describe the bug

Fails to compile in a TypeScript based project. This issue also includes a temporarily solution.

Actual error

$  npm run build

> acme@1.0.0 prebuild
> rm -rf dist/

> acme@1.0.0 build
> rm -rf dist && tsc -p .

node_modules/xero-node/dist/gen/model/accounting/models.d.ts:134:34 - error TS7016: Could not find a declaration file for module 'request'. '/home/services/acme/node_modules/request/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/request` if it exists or add a new declaration (.d.ts) file containing `declare module 'request';`

134 import localVarRequest = require('request');
                                     ~~~~~~~~~

node_modules/xero-node/dist/gen/model/appstore/models.d.ts:11:34 - error TS7016: Could not find a declaration file for module 'request'. '/home/services/acme/node_modules/request/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/request` if it exists or add a new declaration (.d.ts) file containing `declare module 'request';`

11 import localVarRequest = require('request');
                                    ~~~~~~~~~

node_modules/xero-node/dist/gen/model/assets/models.d.ts:13:34 - error TS7016: Could not find a declaration file for module 'request'. '/home/services/acme/node_modules/request/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/request` if it exists or add a new declaration (.d.ts) file containing `declare module 'request';`

13 import localVarRequest = require('request');
                                    ~~~~~~~~~

node_modules/xero-node/dist/gen/model/bankfeeds/models.d.ts:13:34 - error TS7016: Could not find a declaration file for module 'request'. '/home/services/acme/node_modules/request/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/request` if it exists or add a new declaration (.d.ts) file containing `declare module 'request';`

13 import localVarRequest = require('request');
                                    ~~~~~~~~~

node_modules/xero-node/dist/gen/model/files/models.d.ts:10:34 - error TS7016: Could not find a declaration file for module 'request'. '/home/services/acme/node_modules/request/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/request` if it exists or add a new declaration (.d.ts) file containing `declare module 'request';`

10 import localVarRequest = require('request');
                                    ~~~~~~~~~

node_modules/xero-node/dist/gen/model/finance/models.d.ts:55:34 - error TS7016: Could not find a declaration file for module 'request'. '/home/services/acme/node_modules/request/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/request` if it exists or add a new declaration (.d.ts) file containing `declare module 'request';`

55 import localVarRequest = require('request');
                                    ~~~~~~~~~

node_modules/xero-node/dist/gen/model/payroll-au/models.d.ts:89:34 - error TS7016: Could not find a declaration file for module 'request'. '/home/services/acme/node_modules/request/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/request` if it exists or add a new declaration (.d.ts) file containing `declare module 'request';`

89 import localVarRequest = require('request');
                                    ~~~~~~~~~

node_modules/xero-node/dist/gen/model/payroll-nz/models.d.ts:100:34 - error TS7016: Could not find a declaration file for module 'request'. '/home/services/acme/node_modules/request/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/request` if it exists or add a new declaration (.d.ts) file containing `declare module 'request';`

100 import localVarRequest = require('request');
                                     ~~~~~~~~~

node_modules/xero-node/dist/gen/model/payroll-uk/models.d.ts:91:34 - error TS7016: Could not find a declaration file for module 'request'. '/home/services/acme/node_modules/request/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/request` if it exists or add a new declaration (.d.ts) file containing `declare module 'request';`

91 import localVarRequest = require('request');
                                    ~~~~~~~~~

node_modules/xero-node/dist/gen/model/projects/models.d.ts:19:34 - error TS7016: Could not find a declaration file for module 'request'. '/home/services/acme/node_modules/request/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/request` if it exists or add a new declaration (.d.ts) file containing `declare module 'request';`

19 import localVarRequest = require('request');
                                    ~~~~~~~~~

Found 10 errors in 10 files.

Errors  Files
     1  node_modules/xero-node/dist/gen/model/accounting/models.d.ts:134
     1  node_modules/xero-node/dist/gen/model/appstore/models.d.ts:11
     1  node_modules/xero-node/dist/gen/model/assets/models.d.ts:13
     1  node_modules/xero-node/dist/gen/model/bankfeeds/models.d.ts:13
     1  node_modules/xero-node/dist/gen/model/files/models.d.ts:10
     1  node_modules/xero-node/dist/gen/model/finance/models.d.ts:55
     1  node_modules/xero-node/dist/gen/model/payroll-au/models.d.ts:89
     1  node_modules/xero-node/dist/gen/model/payroll-nz/models.d.ts:100
     1  node_modules/xero-node/dist/gen/model/payroll-uk/models.d.ts:91
     1  node_modules/xero-node/dist/gen/model/projects/models.d.ts:19

Temporarily solution

If you are encountering issues related to the 'request' module and its types, using declare module 'request'; is a way to tell TypeScript that the module will exist at runtime, and it helps TypeScript understand that the types are available externally. This declaration is often used with third-party libraries or modules that don't have TypeScript typings included.

ignore-types.d.ts. Put this file either in the root directory, or inside a directory you're implicitly including.

declare module "request" {
  export interface Options {}
}

To Reproduce

Code

import { logInfo } from "../lib/utils";
import { XeroClient } from "xero-node";

async function handler(): Promise<void> {
  logInfo("log xero connections");
  const xero = new XeroClient({
    clientId: "****************",
    clientSecret: "******************",
    grantType: "client_credentials",
  });

  await xero.getClientCredentialsToken();

  const contacts = await xero.accountingApi.getContacts("");

  logInfo("Contacts: ", contacts);
}

export { handler };
tsconfig.json
{
  "compilerOptions": {
    "outDir": "dist",
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "target": "ES2018",
    "module": "commonjs",
    "lib": ["es2018", "ES6", "dom"],
    "declaration": false,
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": false,
    "inlineSourceMap": true,
    "inlineSources": true,
    "strictPropertyInitialization": false,
    "typeRoots": ["./node_modules/@types"],
    "plugins": [
      {
        "transform": "ts-auto-mock/transformer",
        "cacheBetweenTests": false,
        "features": ["random"]
      }
    ]
  },
  "include": ["handlers", "lib", "dev"],
  "exclude": [
    "node_modules",
    "cicd-authorisation",
    ".aws-sam",
    ".github",
    "dist"
  ],
  "emitDecoratorMetadata": true,
  "experimentalDecorators": true
}
TypeScript version:

At the time of this writing, 5 Dec 2023, the xero-node is using 3rd major version, whereas this use case scenario is using 4th.

"ttypescript": "1.5.15",
"typescript": "4.7.4"

Expected behavior

No compilation errors.

github-actions[bot] commented 7 months ago

PETOSS-372

github-actions[bot] commented 7 months ago

Thanks for raising an issue, a ticket has been created to track your request

sangeet-joy-tw commented 4 months ago

hey @rdok

please use the latest version v5.0.1 we have got rid off request module altogether.

let us know.

sangeet-joy-tw commented 4 months ago

request module has been replaced with axios. please use the latest versions.