-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
feat(oas): declare x-expanded-relations - Store #3482
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
🦋 Changeset detectedLatest commit: 1620375 The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self-review: highlighting noteworthy code changes
@@ -91,7 +92,7 @@ export default async (req, res) => { | |||
|
|||
const customerService: CustomerService = req.scope.resolve("customerService") | |||
const customer = await customerService.retrieve(result.customer?.id || "", { | |||
relations: ["orders", "orders.items"], | |||
relations: defaultRelations, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will also include shipping_addresses
in order to harmonize the responses shaped with get-session
.
@@ -22,11 +22,12 @@ export default (app) => { | |||
return app | |||
} | |||
|
|||
export const defaultStoreCollectionRelations = ["products"] | |||
export const defaultStoreCollectionRelations = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
defaultStoreCollectionRelations is not used and is not passed to services in controllers. To avoid confusion, product
has been moves to allowedFields. This change should have no effect.
* name: handle | ||
* style: form | ||
* explode: false | ||
* description: Filter by the collection handle | ||
* schema: | ||
* type: array | ||
* items: | ||
* type: string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing OAS for query param.
medusa/packages/medusa/src/api/routes/store/collections/list-collections.ts
Lines 121 to 124 in d9d046a
export class StoreGetCollectionsParams { | |
@IsOptional() | |
@IsArray() | |
handle?: string[] |
* implicit: | ||
* - items | ||
* - items.variant | ||
* - items.variant.product | ||
* - items.tax_lines | ||
* - items.adjustments | ||
* - gift_cards | ||
* - discounts | ||
* - discounts.rule | ||
* - shipping_methods | ||
* - shipping_methods.tax_lines | ||
* - shipping_address | ||
* - region | ||
* - region.tax_rates |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Relations from decorateTotals subprocess.
medusa/packages/medusa/src/services/cart.ts
Lines 2771 to 2789 in d9d046a
private getTotalsRelations(config: FindConfig<Cart>): string[] { | |
const relationSet = new Set(config.relations) | |
relationSet.add("items") | |
relationSet.add("items.variant") | |
relationSet.add("items.variant.product") | |
relationSet.add("items.tax_lines") | |
relationSet.add("items.adjustments") | |
relationSet.add("gift_cards") | |
relationSet.add("discounts") | |
relationSet.add("discounts.rule") | |
relationSet.add("shipping_methods") | |
relationSet.add("shipping_methods.tax_lines") | |
relationSet.add("shipping_address") | |
relationSet.add("region") | |
relationSet.add("region.tax_rates") | |
return Array.from(relationSet.values()) | |
} |
@@ -124,9 +129,103 @@ export type StoreCustomersRes = { | |||
customer: Omit<Customer, "password_hash"> | |||
} | |||
|
|||
/** | |||
* @schema StoreCustomersResetPasswordRes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
customer model in reset password response will not include expanded-relations from StoreCustomersRes
. Creating a separate response for reset password endpoint.
* relations: | ||
* - prices | ||
* - options | ||
* - product |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export const defaultStoreVariantRelations = ["prices", "options", "product"] |
* relations: | ||
* - prices | ||
* - options | ||
* - product |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export const defaultStoreVariantRelations = ["prices", "options", "product"] |
* required: | ||
* - variant | ||
* properties: | ||
* variant: | ||
* $ref: "#/components/schemas/PricedVariant" | ||
*/ | ||
export type StoreVariantsRes = { | ||
variant: ProductVariant | ||
variant: PricedVariant |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Controller calls PricingService.setVariantPrices
@@ -46,7 +57,7 @@ export type StoreVariantsRes = { | |||
* $ref: "#/components/schemas/PricedVariant" | |||
*/ | |||
export type StoreVariantsListRes = { | |||
variants: ProductVariant[] | |||
variants: PricedVariant[] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Controller calls PricingService.setVariantPrices
@@ -31,6 +31,35 @@ export type ShippingOptionPricing = { | |||
tax_amount: number | |||
} | |||
|
|||
/** @schema PricedShippingOption |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing OAS
medusa/packages/medusa/src/types/pricing.ts
Lines 28 to 32 in d41c3d4
export type ShippingOptionPricing = { | |
price_incl_tax: number | null | |
tax_rates: TaxServiceRate[] | null | |
tax_amount: number | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Not sure if you already considered this, so feel free to ignore if you did: maybe we should consider creating something like a schema in OAS for relations (for example, defaultRelations
). The only reason I'm thinking about that is ensuring the solution is maintainable in the long run.
* - type: object | ||
* properties: | ||
* price_incl_tax: | ||
* type: number | ||
* description: Price including taxes | ||
* tax_rates: | ||
* type: array | ||
* description: An array of applied tax rates | ||
* items: | ||
* type: object | ||
* properties: | ||
* rate: | ||
* type: number | ||
* description: The tax rate value | ||
* name: | ||
* type: string | ||
* description: The name of the tax rate | ||
* code: | ||
* type: string | ||
* description: The code of the tax rate | ||
* tax_amount: | ||
* type: number | ||
* description: The taxes applied. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: should we specify anything as required here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't believe so. I followed the same pattern as PriceVariant.
I hear you. Maintainability was top of mind when designing this solution. OAS not as a good as TypeScript when it comes to mutating definitions using composition. In the end, I settled on using the same definition pattern as the one used by the controllers, services, and repositories. I believe we will be able to improvement maintainability by fully deprecating |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work @patrick-medusajs!
export const allowedFields = [ | ||
"id", | ||
"title", | ||
"handle", | ||
"product", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"product", | |
"products", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😅 Fixed in 9945163
@@ -37,7 +37,7 @@ import { PaymentCollectionService } from "../../../../services" | |||
* tags: | |||
* - Payment Collections | |||
* responses: | |||
* 200: | |||
* 207: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
todo: This must have slipped through at some point. We don't use any other success codes than 200 and 201. I'll create a follow-up ticket.
Unless you want to tackle it now? I believe its a minor change.
@@ -72,7 +72,7 @@ export default async (req, res) => { | |||
req.request_context | |||
) | |||
|
|||
res.status(207).json({ payment_collection }) | |||
res.status(200).json({ payment_collection }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@olivermrbl Fixed
We can see the result of the these changes on the generated types on this commit (0bee564) from a future PR. |
What
Declare
x-expanded-relations
on store responsesWhy
Allows our codegen to alter generated types to better represent the shape of models included in responses when the backend augments the models with relations and/or calculated totals.
How
relations
.relations
.implicit
.eager
.totals
Extra scope:
Tests
yarn install
yarn build
packages/generated/client-types/src/lib/models
SetRelation
.Merge
for be used for deeply nested relations.