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

feat: generate types from OpenAPI schemas #33

Merged
merged 21 commits into from
Aug 18, 2023

Conversation

mattmess1221
Copy link
Contributor

πŸ”— Linked Issue

#32

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme or JSDoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

Types can now be generated for an API if it defines an OpenAPI schema. By specifying the schema on the endpoint as a url, file, schema object, or function returning the schema object, types for the paths and components will be generated in .nuxt/types/nuxt-api-party-types.d.ts.

Types can be imported directly via

import { components, paths } from '#nuxt-api-party/myApi'

Resolves #32

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@netlify
Copy link

netlify bot commented Aug 16, 2023

βœ… Deploy Preview for nuxt-api-party ready!

Name Link
πŸ”¨ Latest commit b2f1e55
πŸ” Latest deploy log https://app.netlify.com/sites/nuxt-api-party/deploys/64df17175768c0000884b8c5
😎 Deploy Preview https://deploy-preview-33--nuxt-api-party.netlify.app
πŸ“± Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@johannschopplich
Copy link
Owner

Dang, that was fast. πŸ‘ Will take me some time to review. But thanks in advance!

body: string | Blob
headers: { 'content-type': 'application/octet-stream' }
}
: {})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick question: What do you mean by {}?

We shouldn't use {} as a type. {} actually means "any non-nullish value", according to ESLint.

  • If you want a type meaning "any object", you probably want object instead.
  • If you want a type meaning "any value", you probably want unknown instead.
  • If you want a type meaning "empty object", you probably want Record<string, never> instead.
    ...

See the eslint@typescript-eslint/ban-types.

@johannschopplich
Copy link
Owner

Hey @killjoy1221 I just reviewed most of your code. Looks quite promising. I have refactored some code, to bring it closer to Nuxt standards. For example, instead of relying on require.resolve, we can use a Nuxt kit util:

const hasOpenAPIPkg = await tryResolveModule('openapi-typescript', [nuxt.options.rootDir])

But in the end, minor optimizations. I haven't touched your logic a lot. The OpenAPI stuff is complicated and I might get 75 % of it. Looking good tho. I have one question left – see review tab. Then, I will have to take a look at why the tests are failing.
Looks like we can release soon. Thanks a lot for your efforts!

@mattmess1221
Copy link
Contributor Author

No problem. I always expect a bit of nitpicking when it comes to coding standards.

I agree that the OpenAPI types are complex. I tried to support path parameters via template strings `pet/${petId}`, but I could never get the type to resolve properly. Maybe I'll get it later and create a new PR.

@johannschopplich
Copy link
Owner

Yeah, maybe TS can't infer the type from a concatenated string. There is the Split utility from type-fest, which might help?

Anyway, thanks for your contribution. It's a hell of a lot of work! I will release a new package version later today. Glad you helped. :)

@johannschopplich johannschopplich changed the title Generate types from OpenAPI schemas feat: generate types from OpenAPI schemas Aug 18, 2023
@johannschopplich johannschopplich merged commit 6876f40 into johannschopplich:main Aug 18, 2023
7 checks passed
@johannschopplich
Copy link
Owner

@killjoy1221 Nuxt API Party v0.14 just released! 🏁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generate types from Swagger/OpenAPI schemas
2 participants