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

BREAKING CHANGE: rewrite the package using Typescript #144

Closed
wants to merge 134 commits into from

Conversation

JoaoPedroAS51
Copy link
Collaborator

@JoaoPedroAS51 JoaoPedroAS51 commented Nov 6, 2020

The package was rewritten using Typescript, with full types support and conditional types for responses. There are no breaking changes for JavaScript applications.

One of my goals when doing this rewrite was to have a precise type checking for responses.
As some responses might be wrapped in "data", I made the Model class a
Generic Class, which accepts two boolean arguments in order to conditionally change
the returned type from responses.

See Conditional Types for more information.

The first argument should be set to true if the collection is wrapped in "data".
The second argument should be set to true if the model is wrapped in "data".

For example, we can have a Post model that has a wrapped collection, but an unwrapped model:

import Model from './Model'

export default class Post extends Model<true, false> {/*...*/}

The backend response like this:

{
  data: [
    /* ... */
    {
      title: 'My awesome post',
      text: 'Some text here...'
    },
    {
      title: 'Another post',
      text: 'Some text here...'
    }
    /* ... */
  ]
}

Then we have the correct type checking:

const posts = new Post().get()

posts[0] // Error
posts.data[0] // Success

If all of your models follows the same structure, you can define the arguments in your Base model class, abstracting this configuration from your models.

import { Model as BaseModel } from 'vue-api-query'

export default class Model extends BaseModel <true, false> {/*...*/}

Note that I wasn't able to make precise type checkings for Static methods due to a limitation on Generic Classes:

Static members cannot reference class type parameters.

See Type Parameters in Static Members for more information.

Based on #104 by @alvaro-canepa

Closes #108

TODO

  • Merge branch dev
  • Add documentation about typescript configuration and usage

# Conflicts:
#	package.json
# Conflicts:
#	src/Model.js
#	tests/dummy/data/post.js
#	tests/dummy/data/postEmbed.ts
#	tests/dummy/data/postsEmbed.js
#	tests/dummy/models/Post.js
#	tests/model.test.ts
#	yarn.lock
This commit remove conditional types feature from static methods. Due to limitations in typescript Generic Classes, this change was needed in order to fix compilation of declaration files.
# Conflicts:
#	src/utils.js
@JoaoPedroAS51 JoaoPedroAS51 changed the base branch from dev to releases/2.0.0 November 7, 2020 05:56
@JoaoPedroAS51
Copy link
Collaborator Author

Outdated. Work continue on branch/next-major

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.

Need typescript support for this lib
1 participant