Skip to content

Commit

Permalink
refactor: fix more types
Browse files Browse the repository at this point in the history
  • Loading branch information
patzick committed Nov 21, 2023
1 parent 25a6816 commit 264ef09
Show file tree
Hide file tree
Showing 20 changed files with 290 additions and 232 deletions.
117 changes: 95 additions & 22 deletions packages/api-client-next/api-types/apiTypes-6.5.3.0.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -640,13 +640,35 @@ export type components = {
ArrayStruct: components["schemas"]["Struct"];
/** Members of the attributes object ("attributes") represent information about the resource object in which it's defined. */
attributes: GenericRecord;
// TODO: [OpenAPI][CalculatedPrice] - define CalculatedPrice with proper fields + define calculated_cheapest_price entity as well
CalculatedPrice: {
apiAlias: "calculated_price";
unitPrice: number;
quantity: number;
totalPrice: number;
calculatedTaxes: CalculatedTax[];
taxRules: TaxRule[];
referencePrice: ReferencePrice;
hasRange?: boolean;
listPrice: {
price: number;
discount: number;
percentage: number;
apiAlias: string;
} | null;
regulationPrice: null | {
price: number;
};
variantId?: string;
};
Cart: components["schemas"]["ArrayStruct"] & {
/** An affiliate tracking code */
affiliateCode?: string;
/** A campaign tracking code */
campaignCode?: string;
/** A comment that can be added to the cart. */
customerComment?: string;
deliveries?: Array<components["schemas"]["OrderDelivery"]>; // TODO: [OpenAPI][Cart] - `deliveries` is missing in schema
/** A list of all cart errors, such as insufficient stocks, invalid addresses or vouchers. */
errors: // TODO: [OpenAPI][Cart] - define errors properly, `key` and `message` should be required fields. `Errors` should be required field as well. Problem is that sometimes it's an array, and sometimes map object
| []
Expand Down Expand Up @@ -2012,15 +2034,54 @@ export type components = {
LineItem: {
description?: string;
good?: boolean;
id?: string;
id: string; // TODO: [OpenAPI][LineItem] - make `id` required
cover?: components["schemas"]["ProductMedia"]; // TODO: [OpenAPI][LineItem] - add `cover` definition to schema
deliveryInformation: {
// TODO: [OpenAPI][LineItem] - define `deliveryInformation` object and find out what's inside (`cart_delivery_information` entity)
stock: number;
};
label?: string;
modified?: boolean;
/** Format: int32 */
quantity?: number;
quantity: number; // TODO: [OpenAPI][LineItem] - make `quantity` required
payload: {
// TODO: [OpenAPI][LineItem] - add `payload` definition to schema (find out what's inside)
options: Array<{
group: string;
option: string;
translated: {
[key: string]: string;
};
}>;
};
price: {
// TODO: [OpenAPI][LineItem] - define price object, also UNIFY price objects across responses
listPrice?: {
/** Format: float */
discount?: number;
/** Format: float */
percentage?: number;
/** Format: float */
price?: number;
};
/** Format: int64 */
quantity: number;
referencePrice?: GenericRecord;
regulationPrice?: {
/** Format: float */
price?: number;
};
taxRules?: GenericRecord;
/** Format: float */
totalPrice: number;
/** Format: float */
unitPrice: number;
};
referencedId?: string;
removable?: boolean;
stackable?: boolean;
type?: string;
states: string[]; // TODO: [OpenAPI][LineItem] - add definition of `states` array, also union type of possible states
type: "product" | "promotion" | "custom" | "credit"; // TODO: [OpenAPI][LineItem] - define type as required and string union type
};
/** A link **MUST** be represented as either: a string containing the link's URL or a link object. */
link: OneOf<
Expand Down Expand Up @@ -2355,7 +2416,7 @@ export type components = {
};
/** Added since version: 6.0.0.0 */
Order: {
addresses?: components["schemas"]["OrderAddress"];
addresses?: Array<components["schemas"]["OrderAddress"]>; // TODO: [OpenAPI][Order] addresses field should be defined as an array
affiliateCode?: string;
/** Format: float */
amountNet?: number;
Expand All @@ -2375,8 +2436,8 @@ export type components = {
customerComment?: string;
customFields?: GenericRecord;
deepLinkCode?: string;
deliveries?: components["schemas"]["OrderDelivery"];
documents?: components["schemas"]["Document"];
deliveries?: Array<components["schemas"]["OrderDelivery"]>; // TODO: [OpenAPI][Order] deliveries field should be defined as an array
documents: Array<components["schemas"]["Document"]>; // TODO: [OpenAPI][Order] documents field should be defined as an array and required field
extensions?: {
returns?: {
data?: {
Expand Down Expand Up @@ -2468,7 +2529,7 @@ export type components = {
customFields?: GenericRecord;
department?: string;
firstName: string;
id?: string;
id: string; // TODO: [OpenAPI][OrderAddress] id field should be defined as required
lastName: string;
phoneNumber?: string;
salutation?: components["schemas"]["Salutation"];
Expand Down Expand Up @@ -3094,21 +3155,15 @@ export type components = {
available?: boolean;
/** Format: int64 */
availableStock?: number;
calculatedCheapestPrice?: GenericRecord & { hasRange: boolean }; // TODO: [OpenAPI][Product] calculatedCheapestPrice field should be defined exactly what it is
calculatedCheapestPrice?: components["schemas"]["CalculatedPrice"]; // TODO: [OpenAPI][Product] calculatedCheapestPrice field should be defined exactly what it is
/**
* Format: int64
* Runtime field, cannot be used as part of the criteria.
*/
calculatedMaxPurchase?: number;
calculatedPrice?: {
// TODO: [OpenAPI][Product] calculatedPrice field should be defined properly
referencePrice: {
price: number;
referenceUnit: number;
unitName: string;
};
};
calculatedPrices?: unknown[];
// TODO: [OpenAPI][Product] calculatedPrice field should be defined properly
calculatedPrice?: components["schemas"]["CalculatedPrice"];
calculatedPrices?: components["schemas"]["CalculatedPrice"][]; // TODO: [OpenAPI][Product] calculatedPrices field should be defined as an array and required!
canonicalProduct?: components["schemas"]["Product"];
canonicalProductId?: string;
categories?: components["schemas"]["Category"];
Expand Down Expand Up @@ -3257,6 +3312,10 @@ export type components = {
unitId?: string;
/** Format: date-time */
updatedAt?: string;
// TODO: [OpenAPI][Product] variantListingConfig field should be defined in schema
variantListingConfig?: {
displayParent: boolean;
};
versionId?: string;
/** Format: float */
weight?: number;
Expand Down Expand Up @@ -3930,7 +3989,7 @@ export type components = {
"shipping-free"?: boolean;
search: string; // TODO: [OpenAPI][ProductListingResult] search field should be defined properly
};
elements?: components["schemas"]["Product"][];
elements: components["schemas"]["Product"][]; // TODO: [OpenAPI][ProductListingResult] elements field should be defined as required
sorting?: string;
};
/** Added since version: 6.0.0.0 */
Expand Down Expand Up @@ -6010,7 +6069,7 @@ export type components = {
updatedAt?: string;
};
WishlistLoadRouteResponse: {
products?: components["schemas"]["ProductListingResult"][];
products: components["schemas"]["ProductListingResult"]; // TODO: [OpenAPI][WishlistLoadRouteResponse] - products is listing result, not array
wishlist?: {
customerId?: string;
salesChannelId?: string;
Expand Down Expand Up @@ -6662,7 +6721,15 @@ export type operations<components = components> = {
addLineItem: {
requestBody?: {
content: {
"application/json": components["schemas"]["CartItems"];
"application/json": {
// TODO: [OpenAPI][addLineItem] - add proper request body type with required fields
items: Array<{
id?: string; // TODO: check if this is used at all?
referencedId: string;
quantity?: number;
type: "product" | "promotion" | "custom" | "credit";
}>;
};
};
};
responses: {
Expand Down Expand Up @@ -6705,7 +6772,13 @@ export type operations<components = components> = {
updateLineItem: {
requestBody?: {
content: {
"application/json": components["schemas"]["CartItems"];
"application/json": {
// TODO: [OpenAPI][updateLineItem] - add proper request body type with required fields
items: Array<{
id: string;
quantity: number;
}>;
};
};
};
responses: {
Expand Down Expand Up @@ -7377,7 +7450,7 @@ export type operations<components = components> = {
content: {
"application/json": {
/** The identifier of the order to be canceled. */
orderId?: string;
orderId: string; // TODO: [OpenAPI][cancelOrder] add orderId as required field
};
};
};
Expand Down
2 changes: 1 addition & 1 deletion packages/composables/src/useAddToCart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function useAddToCart(product: Ref<Product>): UseAddToCartReturn {
async function addToCart(): Promise<Cart> {
if (!quantity.value) quantity.value = 1;
const addToCartResponse = await addProduct({
id: _product.value.id as string, // TODO: [OpenAPI][Product] - `id` should be required field in Product schema
id: _product.value.id,
quantity: quantity.value,
});
quantity.value = 1;
Expand Down
26 changes: 11 additions & 15 deletions packages/composables/src/useCart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export function useCartFunction(): UseCartReturn {
const result = await apiClient.invoke(
"removeLineItem delete /checkout/cart/line-item?ids",
{
ids: [lineItem.id as string], // TODO: [OpenAPI] - change lineitem id to mandatory
ids: [lineItem.id],
},
);
_storeCart.value = result;
Expand Down Expand Up @@ -183,14 +183,11 @@ export function useCartFunction(): UseCartReturn {
return [];
}

const result = await apiClient.invoke(
"readProduct post /product",
{
ids: cartItems.value
.map(({ referencedId }) => referencedId)
.filter(String) as string[],
} as any, // TODO: [OpenAPI] - `ids` is missing in schema
);
const result = await apiClient.invoke("readProduct post /product", {
ids: cartItems.value
.map(({ referencedId }) => referencedId)
.filter(String) as string[],
});
return result?.elements || [];
}

Expand All @@ -209,8 +206,8 @@ export function useCartFunction(): UseCartReturn {
const count = computed(() => {
return cartItems.value.reduce(
(accumulator: number, lineItem: Schemas["LineItem"]) =>
lineItem.type === "product" // TODO: [OpenAPI][Cart] - LineItem `type` should be defined as union type not string -> "product" | "promotion" | "custom" | "credit";
? (lineItem.quantity as number) + accumulator // TODO: [OpenAPI][Cart] - LineItem `quantity` should be defined
lineItem.type === "product"
? lineItem.quantity + accumulator
: accumulator,
0,
);
Expand All @@ -225,9 +222,8 @@ export function useCartFunction(): UseCartReturn {
});

const shippingTotal = computed(() => {
// TODO: [OpenAPI][Cart] - `deliveries` is missing in schema
const shippingTotal = (cart.value as any)?.deliveries?.[0]?.shippingCosts
?.totalPrice as number;
const shippingTotal =
cart.value?.deliveries?.[0]?.shippingCosts?.totalPrice;
return shippingTotal || 0;
});

Expand All @@ -241,7 +237,7 @@ export function useCartFunction(): UseCartReturn {
cartItems.value.length > 0 &&
cartItems.value
.filter((element) => element.type !== "promotion")
.every((item) => (item as any).states.includes("is-download")) // TODO: [OpenAPI][Cart] - LineItem `states` should be defined
.every((item) => item.states.includes("is-download"))
);
});

Expand Down
36 changes: 13 additions & 23 deletions packages/composables/src/useCartItem.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { computed } from "vue";
import type { ComputedRef, Ref } from "vue";
import type {
PropertyGroupOptionCart,
CartProductItem,
} from "@shopware-pwa/types";

import { getMainImageUrl } from "@shopware-pwa/helpers-next";
import { useShopwareContext, useCart } from "#imports";
import type { Cart, Schemas } from "#shopware";
Expand All @@ -29,11 +24,11 @@ export type UseCartItemReturn = {
/**
* Options (of variation) for the current item
*/
itemOptions: ComputedRef<PropertyGroupOptionCart[]>;
itemOptions: ComputedRef<Schemas["LineItem"]["payload"]["options"]>;
/**
* Type of the current item: "product" or "promotion"
*/
itemType: ComputedRef<Schemas["LineItem"]["type"] | undefined>; // TODO: [OpenAPI][LineItem] - LineItem type should be required, temporal regression to `string` istead of union type
itemType: ComputedRef<Schemas["LineItem"]["type"] | undefined>;
/**
* Determines if the current item is a product
*/
Expand Down Expand Up @@ -84,36 +79,31 @@ export function useCartItem(
const { refreshCart, changeProductQuantity } = useCart();

const itemQuantity = computed(() => cartItem.value.quantity);
const itemImageThumbnailUrl = computed(() =>
getMainImageUrl(cartItem.value as any),
); // TODO: [OpenAPI][LineItem] - `cover` should be defined in LineItem schema
const itemImageThumbnailUrl = computed(() => getMainImageUrl(cartItem.value));

const itemRegularPrice = computed<number | undefined>(
() =>
(cartItem.value as any)?.price?.listPrice?.price || // TODO: [OpenAPI][LineItem] - LineItem price should be required and defined
(cartItem.value as any)?.price?.unitPrice,
cartItem.value?.price?.listPrice?.price ||
cartItem.value?.price?.unitPrice,
);

const itemSpecialPrice = computed(
() =>
(cartItem.value as any)?.price?.listPrice?.price && // TODO: [OpenAPI][LineItem] - LineItem price should be required and defined
(cartItem.value as any)?.price?.unitPrice,
cartItem.value?.price?.listPrice?.price &&
cartItem.value?.price?.unitPrice,
);

const itemTotalPrice = computed(
() => (cartItem.value as any).price?.totalPrice, // TODO: [OpenAPI][LineItem] - LineItem price should be required and defined
);
const itemTotalPrice = computed(() => cartItem.value.price?.totalPrice);

const itemOptions = computed(
() =>
(cartItem.value.type === "product" &&
((cartItem.value as any).payload as CartProductItem)?.options) || // TODO: [OpenAPI][LineItem] - LineItem payload should be required and defined in type `product`
(cartItem.value.type === "product" && cartItem.value.payload?.options) ||
[],
);

const itemStock = computed<number>(
() => (cartItem.value as any).deliveryInformation?.stock,
); // TODO: [OpenAPI][LineItem] - LineItem deliveryInformation should be required and defined
() => cartItem.value.deliveryInformation.stock,
);

const itemType = computed(() => cartItem.value.type);

Expand All @@ -125,15 +115,15 @@ export function useCartItem(
const newCart = await apiClient.invoke(
"removeLineItem delete /checkout/cart/line-item?ids",
{
ids: [cartItem.value.id as string], // TODO: [OpenAPI][LineItem] - change lineitem id to mandatory
ids: [cartItem.value.id],
},
);
await refreshCart(newCart);
}

async function changeItemQuantity(quantity: number): Promise<Cart> {
const result = changeProductQuantity({
id: cartItem.value.id as string, // TODO: [OpenAPI][updateLIneItem] - change id field to mandatory
id: cartItem.value.id,
quantity: +quantity,
});

Expand Down
Loading

0 comments on commit 264ef09

Please sign in to comment.