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

fix(lib): include userid to data response from field plugin #438

Merged

Conversation

ECJ222
Copy link
Contributor

@ECJ222 ECJ222 commented Nov 29, 2024

What?

This PR includes the userID to the data response retrieved on state change and loaded state from a field plugin as documented within our Field plugin API documentation

Why?

JIRA: SHAPE-7741

How to test? (optional)

To test, run the following commands below (on separate terminals):

  • yarn dev:lib.
  • yarn dev:demo.
  • yarn dev:sandbox.

These commands above should get your sandbox, field-plugin demo, and field-plugin library setup and running successfully.

Copy your currently running local sandbox URL, http://localhost:7070/, and place it within the file packages/field-plugin/src/createFieldPlugin/createFieldPlugin.ts.

Screenshot 2024-12-03 at 12 56 39 PM

This facilitates communication between your sandbox and field plugin through the Iframe when interacting with the rendered Iframe window.

fix.SHAPE-7741.mov

You should find the userId property returned and other plugin-related data from your field plugin demo.

Screenshot 2024-12-03 at 1 09 41 AM

Copy link

vercel bot commented Nov 29, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
plugin-sandbox ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 4, 2024 9:09am

@ECJ222 ECJ222 force-pushed the fix/SHAPE-7741-include-userid-in-fieldplugin-response-data branch from 443ef54 to 4439855 Compare November 29, 2024 12:38
@ECJ222 ECJ222 changed the title fix(SHAPE-7741): include userid to data response from field plugin fix(lib): include userid to data response from field plugin Nov 29, 2024
@ECJ222 ECJ222 force-pushed the fix/SHAPE-7741-include-userid-in-fieldplugin-response-data branch from 4439855 to 604a79c Compare December 2, 2024 16:23
Copy link
Collaborator

@johannes-lindgren johannes-lindgren left a comment

Choose a reason for hiding this comment

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

Hi @ECJ222, thank you for your contribution; this will be a nice addition!

This is what we need to to move forward:

  • Update the validation functions. This does not show up as typescript errors because is essentially asserts the type. (And corresponding unit tests)
  • We need the "how to test" section to be filled out (it says that it's optional, but we need it in this instance). We can use the demo and sandbox for this.

After this PR, we will need to follow up with more PRs to perform the release.

@@ -27,7 +27,7 @@ export type LoadedMessage = MessageToPlugin<'loaded'> & {
}

// TODO full implementation of validation
export const isLoadedMessage = (data: unknown): data is LoadedMessage =>
export const isLoadedMessage = (data: unknown): boolean =>
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is going to disable the type narrowing

Copy link
Contributor

Choose a reason for hiding this comment

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

This probably explains my warning below 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure the warnings you got from the visual editor were from this PR 🙏

Copy link
Contributor

Choose a reason for hiding this comment

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

They are no longer appearing after the last commit 🙏

Copy link
Contributor

@Dawntraoz Dawntraoz left a comment

Choose a reason for hiding this comment

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

Something is broken, I tested the demo with the changes you made by building it and adding the dist/index.js to My Plugins using the storyfront branch with the changes, and I got this warning while the plugin never loads:

Screenshot 2024-12-03 at 13 27 44

The not loading wasn't related to the warnings, I managed to make it work on the Visual Editor 🎉 So let's fix those warnings and we are ready to go 🚀
Screenshot 2024-12-03 at 13 46 30

@ECJ222
Copy link
Contributor Author

ECJ222 commented Dec 3, 2024

Awesome! @Dawntraoz 🫶

Was this the error you were talking about?

Uncaught Error: This error can be safely ignored. It is caused by the legacy field plugin API. See issue

@ECJ222 ECJ222 force-pushed the fix/SHAPE-7741-include-userid-in-fieldplugin-response-data branch from 7cb28de to 34df2be Compare December 3, 2024 13:07
@Dawntraoz
Copy link
Contributor

Awesome! @Dawntraoz 🫶

Was this the error you were talking about?

Uncaught Error: This error can be safely ignored. It is caused by the legacy field plugin API. See issue

Oh no, that error is always there on purpose.

I meant the warning messages:
[Vue warn]: Invalid prop: type check failed for prop "spaceId". Expected Number with value X, got String with value "X"

@@ -33,6 +34,8 @@ export const isStateMessage = (data: unknown): data is StateChangedMessage =>
typeof data.language === 'string' &&
hasKey(data, 'schema') &&
isFieldPluginSchema(data.schema) &&
hasKey(data, 'userId') &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for this @ECJ222 🫂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I meant the warning messages:
[Vue warn]: Invalid prop: type check failed for prop "spaceId". Expected Number with value X, got String with value "X"

Oh, I don't think it's related to this PR.

@johannes-lindgren
Copy link
Collaborator

@ECJ222 I think there's a bug in the tests: it's passing the userId and spaceId as strings:

image

It worked before, because the validation function actually does not perform a check on the spaceId property (see the TODO comment)

@ECJ222
Copy link
Contributor Author

ECJ222 commented Dec 3, 2024

Thanks man, I updated it!

@ECJ222
Copy link
Contributor Author

ECJ222 commented Dec 3, 2024

Once this PR gets merged, I will follow up with the release PR. 🙏

Copy link
Contributor

@Dawntraoz Dawntraoz left a comment

Choose a reason for hiding this comment

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

The code looks perfect to me! 🎉

Great job @ECJ222, thanks a lot for helping us with this 💙 And thanks to @johannes-lindgren for encouraging testing and validation and helped us review the PR 🫂

Copy link
Collaborator

@johannes-lindgren johannes-lindgren left a comment

Choose a reason for hiding this comment

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

Perfect, well done!

I checked that it works:

image image

})
})

describe('the "language" property', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

💯

@johannes-lindgren
Copy link
Collaborator

johannes-lindgren commented Dec 4, 2024

@Dawntraoz, @demetriusfeijoo, and @ECJ222: regarding the validation: when I wrote the library ~1.5 years ago, I did not include a validation library for two reasons:

  1. I could not find a library of sufficiently small size
  2. I could not find a library that did not infer the types from the schema. Zod (and other libraries) are nice and all, but if you want typescript support, you need to infer the types from the validation libraries. But this means that the users of the @storyblok/field-plugin will get Zod types when they're using the library, and the types will not as readable as when you type them yourself.

However, with a validation library, the problem that I requested changes for earlier would not have happened.

We also have this dilemma in the app, which is why I created a new validation library to fit these needs (amongst others): PureParse. Check it out if you're interested 🙂

@Dawntraoz
Copy link
Contributor

@Dawntraoz, @demetriusfeijoo, and @ECJ222: regarding the validation: when I wrote the library ~1.5 years ago, I did not include a validation library for two reasons:

  1. I could not find a library of sufficiently small size
  2. I could not find a library that did not infer the types from the schema. Zod (and other libraries) are nice and all, but if you want typescript support, you need to infer the types from the validation libraries. But this means that the users of the @storyblok/field-plugin will get Zod types when they're using the library, and the types will not as readable as when you type them yourself.

But without a validation library, the problem that I requested changes for earlier would not have happened.

We also have this dilemma in the app, which is why I created a new validation library to fit these needs (amongst others): PureParse. Check it out if you're interested 🙂

This conversation is interesting 🤔 Maybe @demetriusfeijoo has more insights here, but since I joined the Plugins Team, we have been using https://valibot.dev/ in all our plugins since it seemed perfect for our use cases at the moment. So maybe we can use it here, too, to keep it consistent between all the projects we have and not use a different one per project 🤔

@ECJ222 ECJ222 merged commit 629e6f4 into main Dec 4, 2024
4 checks passed
@ECJ222 ECJ222 deleted the fix/SHAPE-7741-include-userid-in-fieldplugin-response-data branch December 4, 2024 14:18
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.

3 participants