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

Add new Adapter and Action interfaces, add new Next.js handler implementation, remove middlewares #401

Merged
merged 46 commits into from
Feb 3, 2025

Conversation

witoszekdev
Copy link
Member

@witoszekdev witoszekdev commented Jan 28, 2025

This PR adds new interfaces for writing handlers:

  • Adapter
  • Action
  • Validator

It replaces Next.js handlers with these new abstractions.
It removes deprecated process-protected-handler and process-saleor-webhook files, in favor of Validator classes.

It also removes middlewares and retes dependency, since they are no longer used in handlers.

Additional tests for existing usage were added: for example, fetching of JWKS when Saleor webhook validation has failed, webhook error handling.

TODO for next PR:

  • Add handlers for Web API and AWS lambda
  • Make schemaVersion required in SaleorWebhook

Copy link

changeset-bot bot commented Jan 28, 2025

🦋 Changeset detected

Latest commit: 2e0b09d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@saleor/app-sdk Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

codecov bot commented Jan 28, 2025

Codecov Report

Attention: Patch coverage is 98.00420% with 19 lines in your changes missing coverage. Please review.

Project coverage is 81.16%. Comparing base (d339a69) to head (2e0b09d).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/handlers/actions/register-action-handler.ts 97.18% 9 Missing ⚠️
src/test-utils/mock-adapter.ts 85.18% 4 Missing ⚠️
src/handlers/platforms/next/index.ts 0.00% 2 Missing ⚠️
src/handlers/shared/generic-saleor-webhook.ts 98.27% 2 Missing ⚠️
src/handlers/platforms/next/platform-adapter.ts 97.36% 1 Missing ⚠️
src/handlers/shared/saleor-webhook-validator.ts 99.35% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #401      +/-   ##
==========================================
+ Coverage   73.07%   81.16%   +8.09%     
==========================================
  Files          82       78       -4     
  Lines        3034     3143     +109     
  Branches      456      513      +57     
==========================================
+ Hits         2217     2551     +334     
+ Misses        812      587     -225     
  Partials        5        5              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@witoszekdev witoszekdev marked this pull request as ready for review January 31, 2025 16:26
@witoszekdev witoszekdev requested a review from a team as a code owner January 31, 2025 16:26
@witoszekdev witoszekdev marked this pull request as draft January 31, 2025 16:39
@witoszekdev witoszekdev marked this pull request as ready for review February 3, 2025 12:02
@@ -2,4 +2,6 @@
"@saleor/app-sdk": major
---

Breaking change: Remove checking "domain" header from Saleor requests. It should be replaced with the "saleor-api-url" header.
Breaking change: Remove checking "saleor-domain" header from Saleor requests. It should be replaced with the "saleor-api-url" header.
Copy link
Member

Choose a reason for hiding this comment

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

But who should check saleor-api-url? app or sdk? If SDK, it's not a part of the changelog but something we should do

Copy link
Member Author

Choose a reason for hiding this comment

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

Nope, this is something SDK does: we have dropped saleor-domain which was deprecated in favor of saleor-api-url header in our validation, thus making SDK incompatible with versions prior to 3.15. I've improved the description so that it's more clear.

package.json Outdated
Comment on lines 153 to 157
"./handlers/actions": {
"types": "./handlers/actions/index.d.ts",
"import": "./handlers/actions/index.mjs",
"require": "./handlers/actions/index.js"
},
Copy link
Member

Choose a reason for hiding this comment

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

what is the use case of this being exported? Why App would need that?

Copy link
Member Author

Choose a reason for hiding this comment

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

If you're not using any of our adapters, you can re-implement them for your platform without forking app-sdk

Copy link
Member

Choose a reason for hiding this comment

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

I think on 1.x better to not export more than we need. Every time we export something we must be sure it should be there, because its public api and everyone can build on top

Unless someone asks for, I'd prefer to export only platform specific logic, while we control the internals. The we can evolve into exposing this

Copy link
Member Author

Choose a reason for hiding this comment

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

All right, I haven't though about that this way 😅

I'll remove internals export then 👍🏻

Comment on lines 33 to 37
const methodEagerResponse = this.adapterMiddleware.withMethod(["GET"]);

if (methodEagerResponse) {
return methodEagerResponse;
}
Copy link
Member

Choose a reason for hiding this comment

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

not sure if I get that

Copy link
Member Author

Choose a reason for hiding this comment

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

It returns an error response if validation failed, I've updated the name


import { HTTPMethod, PlatformAdapterInterface } from "./generic-adapter-use-case-types";

export class PlatformAdapterMiddleware<T> {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe better name would be to mention what it does (validating requests), not that it uses adapter for it?

Copy link
Member Author

Choose a reason for hiding this comment

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

👍 I've came up with SaleorRequestProcessor since it does both some validation and getting of headers


export class ProtectedActionValidator<I> {
// Name left for backwards compatibility
private debug = createDebug("processProtectedHandler");
Copy link
Member

Choose a reason for hiding this comment

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

We dont need this compatibility, we are introdcucing bc

Breaking change: Remove checking "domain" header from Saleor requests. It should be replaced with the "saleor-api-url" header.
Breaking change: SDK will no longer check `saleor-domain` header when validating Saleor requests, instead it will check `saleor-api-url` header.

This makes SDK incompatible with Saleor versions prior to 3.15.
Copy link
Member

Choose a reason for hiding this comment

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

We don't have to write that, I want SDK 1.x to drop support below 3.20

@witoszekdev witoszekdev enabled auto-merge (squash) February 3, 2025 16:07
@witoszekdev witoszekdev merged commit 51caa77 into main Feb 3, 2025
6 checks passed
@witoszekdev witoszekdev deleted the add-adapter-actions-new-handlers branch February 3, 2025 16:07
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.

2 participants