GIScience / openrouteservice-js

:pushpin: The JavaScript API to consume openrouteservice(s) painlessly!
https://openrouteservice.org
Apache License 2.0
191 stars 35 forks source link

TypeScript support #55

Open Splicer97 opened 1 year ago

Splicer97 commented 1 year ago

How about implement typescript support for your package?

TheGreatRefrigerator commented 1 year ago

Would be awesome, but currently not in our scope, as i don't have any experience with it, yet... Feel free to contribute with a PR though!

Basile001 commented 1 year ago

Hello,

I use a AWS lambda with a runtime Node.js 14. I tried to import openrouteservice-js.

But when I try to run my code the following error appears :

{
    "errorType": "TypeError",
    "errorMessage": "o is not a function",
    "stack": [
        "TypeError: o is not a function",....
}

Here what I've done:

mylambda.ts :

import Openrouteservice from "openrouteservice-js";

.....
exports.generateGpxHandler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
    const { httpMethod, body, headers } = event;
....

// the line causing error
    const directionsService = new Openrouteservice.Directions({ api_key: OPENROUTE_API_KEY });
.............

}

I declare a types.d.ts file :

declare module "openrouteservice-js";

in my package.json :


"dependencies": {
        "aws-sdk": "^2.1238.0",
        "@aws-sdk/client-dynamodb": "3.194.0",
        "@aws-sdk/lib-dynamodb": "3.194.0",
        "jwt-decode": "3.1.2",
        "uuid": "9.0.0",
        "openrouteservice-js": "0.2.0"
    },

my tsconfig.json:


{
  "compilerOptions": {
    "target": "ES6",
    "moduleResolution": "node",
    "noImplicitAny": true,
    "preserveConstEnums": true,
    "outDir": "./bin",
    "sourceMap": true
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "node_modules",
    "**/*.spec.ts"
  ]
}

I also use a webpack.conf.js file similar to my open source project:

https://github.com/Basile001/another-aws-example/blob/master/back/webpack.config.js

I don't understand why my lambda return this error, I miss something but I don't know what it is ?

I tried the require way instead of the import way but the result is the same, or to mess up with my tsconfig file but nothing change.

Thank you for the help in advance.

Kind regards,

Basile

didierdemaeyer commented 8 months ago

Perhaps this can be used to generate the TypeScript declaration file(s).

https://www.typescriptlang.org/docs/handbook/declaration-files/dts-from-js.html