exposing custom options and injecting new actions in TS #490
-
I'm trying to port over some tools I had previous built with vuex (without Typescript) over to pinia. Specifically, this tool helps simplify all the boilerplate involved with making API requests and tracking the state of those requests in the store. Basically I define a set of "api actions" which are functions that take in a payload and returns a description of how to make the API request and what to do if successful. The tooling then handles marking the making the request, all the error handling, and tracking the status (pending, success, error, etc) along the way. There are obviously many ways to do this, but my first inclination was to use a plugin to add a new "option" for my special api actions. For example:
Now to actually trigger these actions, I see two options
I don't think there will be any problems on the actual functionality side, however I'm having issues getting the TS types to work. As far as I understand, I need access to the data passed to my new custom option in the type generics the same way that the state, getters, and actions are exposed. For option 1, I would try to inject new methods for all of the keys defined under For option 2, I would try to add typing to the parameters passed to Additionally, I'd want to use those "action names" again in typing the parameter of a "getRequestStatus" function, which uses that same request name. In this regard, using regular strings may actually be helpful, so I dont have to define and import an extra enum all over the place. I don't have much experience with these more advanced typescript definitions, so unclear if it's possible. Also open to other suggestions of how to accomplish this, although I'm trying to avoid a setup like the debounce example in the docs, as I'd like to keep the full definition of the api request action all together. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Do you have a partially working example on codesandbox or GitHub? |
Beta Was this translation helpful? Give feedback.
-
This is an example of extending with custom types: https://codesandbox.io/s/pinia-example-forked-cuj8g?file=/src/pinia-plugin.ts:1237-1785 check the store.ts file for usage and you will see things are correctly typed |
Beta Was this translation helpful? Give feedback.
This is an example of extending with custom types: https://codesandbox.io/s/pinia-example-forked-cuj8g?file=/src/pinia-plugin.ts:1237-1785 check the store.ts file for usage and you will see things are correctly typed