Skip to content

feat: add createMapper convenience utility #264

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

Merged
merged 3 commits into from
Feb 19, 2022
Merged

feat: add createMapper convenience utility #264

merged 3 commits into from
Feb 19, 2022

Conversation

danielroe
Copy link
Owner

@danielroe danielroe commented Feb 17, 2022

resolves #260

🔥 Notable changes

This PR adds a convenience helper function to allow you to easily map the accessor state/methods into your component.

import Vue from 'vue'

// Nuxt
import { accessorType } from '~/store'
const mapper = createMapper(accessorType)
// Vue
import { accessor } from './src/store'
const mapper = createMapper(accessor)

export default Vue.extend({
  computed: {
    // Direct mapping to a property
    ...mapper('name'),
    // or array of properties
    ...mapper(['name', 'email']),
    // or submodules
    ...mapper('submodule', ['name', 'email']),
  },
  methods: {
    // All your methods should be included in the methods object,
    // whether they are mutations, actions (or even a method returned
    // by a getter or stored in state)
    ...mapper('resetName'),
    resetEmail() {
      // You can access directly off your component instance
      console.log(this.name, this.email)
      this.resetName()
    },
  },
})

@danielroe danielroe self-assigned this Feb 17, 2022
@vercel
Copy link

vercel bot commented Feb 17, 2022

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/danielroe/nuxt-typed-vuex/2pbMXckQsBB4CYnDJX6imWLExqi4
✅ Preview: https://nuxt-typed-vuex-git-feat-mapper-danielroe.vercel.app

@codecov
Copy link

codecov bot commented Feb 17, 2022

Codecov Report

Merging #264 (ecdd99f) into main (0572a30) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##              main      #264   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            9        10    +1     
  Lines           99       117   +18     
  Branches        20        28    +8     
=========================================
+ Hits            99       117   +18     
Impacted Files Coverage Δ
packages/typed-vuex/src/accessor.ts 100.00% <100.00%> (ø)
packages/typed-vuex/src/utils.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0572a30...ecdd99f. Read the comment docs.

@RomainBillot
Copy link

Freaking insane ! Thanks ❤️

@MateiNenciu
Copy link

Great feature, but can this be extend to allow us to rename the props names? Like vuex does with mapGetters, etc ?

@danielroe
Copy link
Owner Author

danielroe commented Mar 7, 2022

Go for it - raise an issue with an example of what you mean 😊

@omnichronous
Copy link

This looks very cool, but I don't understand where to import createMapper from - it's not exported in node_modules/typed-vuex/lib/index.d.ts?

@danielroe
Copy link
Owner Author

I think it seems to be: https://unpkg.com/browse/[email protected]/dist/index.d.ts.

Are you on v0.3.0+?

@omnichronous
Copy link

My bad, I was using an older version, thank you!

I also want to second MateiNenciu's feature request, but I'll follow your suggestion to open a new issue for it.

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.

feat: is there a replacement for the mapState utility?
4 participants