node-asana icon indicating copy to clipboard operation
node-asana copied to clipboard

typescript definitions - howto

Open simllll opened this issue 4 years ago • 3 comments
trafficstars

this should just be a hint for everyone who is looking for complete and (almost) valid type definitions, currently @types/asana is incomplete and partly wrong, the best and easiert workaround I've found is to generate your own api client via open api files. e.g. you can go to editor.swagger.com and download your client, or by using the following command:

npm install @openapitools/openapi-generator-cli
openapi-generator-cli generate -p apiPackage=apipkg -p modelPackage=modelPkg -p withSeparateModelsAndApi=true -p modelPropertyNaming=original -p supportsES6=true -i https://raw.githubusercontent.com/Asana/developer-docs/master/defs/asana_oas.yaml -g typescript-axios -o src/AsanaClient --skip-validate-spec

after that you have a AsanaClient folder inside of "src" folder, e.g. image

To use the api, here is an example code to get the basic idea:

import { Configuration, ProjectsApi, SectionsApi, TasksApi } from './AsanaClient';

const config = new Configuration({ accessToken: <your personal access token });

const apis = {
tasksApi: new TasksApi(config),
projectsApi: new ProjectsApi(config),
sectionsApi: new SectionsApi(config)
};

// e.g. get sections
const sections = (await apis.sectionsApi.getSectionsForProject(project.gid))?.data?.data;

// e.g. create a task...
const createTaskResult = await apis.tasksApi.createTask({data: ...taskData });

the types are quite good, except for creating a task memberships properties, it assumes that project and section should be an object with a format like { gid: string }, but the correct type is just a gid (string). I guess there are more flaws, but for now I couldn't find anything else.

Hope this helps someone!

simllll avatar Sep 19 '21 19:09 simllll

@simllll - This was super helpful, thank you.

As a note to anyone else coming across this, steps I took to solve the problem:

  1. Run npm install -d @openapitools/openapi-generator-cli
  2. Add a package.json script called generate-asana-apis containing: openapi-generator-cli generate -p apiPackage=apipkg -p modelPackage=modelPkg -p withSeparateModelsAndApi=true -p modelPropertyNaming=original -p supportsES6=true -i https://raw.githubusercontent.com/Asana/developer-docs/master/defs/asana_oas.yaml -g typescript-axios -o src/AsanaClient --skip-validate-spec
  3. Run node run generate-asana-apis

If you run into this error when trying to run node run generate-asana-apis:

Error: The operation couldn’t be completed. Unable to locate a Java Runtime.

you can install a Java Runtime with brew install java. Note the output from brew install java may include something like:

For the system Java wrappers to find this JDK, symlink it with
  sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk

Go ahead and run that sudo ln ... command, and then run node run generate-asana-apis again.

alexnitta avatar Jan 22 '22 14:01 alexnitta

For what it's worth, I also took a crack at improving the official definitions, at least for the issues that were impacting me. The process to improve those is well defined and documented in the DefinitelyTyped repo, if a bit slow on the approval side due to lack of types/asana maintainers: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/57226

apiology avatar Jan 22 '22 14:01 apiology

Another round of improvements is going in soon: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/60589

apiology avatar May 30 '22 20:05 apiology