Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typescript definitions - howto #244

Open
simllll opened this issue Sep 19, 2021 · 3 comments
Open

typescript definitions - howto #244

simllll opened this issue Sep 19, 2021 · 3 comments

Comments

@simllll
Copy link

simllll commented Sep 19, 2021

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!

@alexnitta
Copy link

@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.

@apiology
Copy link

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: DefinitelyTyped/DefinitelyTyped#57226

@apiology
Copy link

Another round of improvements is going in soon: DefinitelyTyped/DefinitelyTyped#60589

@goodwin64 goodwin64 mentioned this issue Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants