Skip to content

Commit

Permalink
feat(composables): add onlyAvailable parameter (#1378)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkucmus authored Mar 5, 2021
1 parent 4ec6ecf commit f0b8016
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 20 deletions.
8 changes: 6 additions & 2 deletions api/shopware-6-client.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,17 @@ export function getAvailableCurrencies(contextInstance?: ShopwareApiInstance): P
export function getAvailableLanguages(contextInstance?: ShopwareApiInstance): Promise<Language[]>;

// @beta (undocumented)
export function getAvailablePaymentMethods(contextInstance?: ShopwareApiInstance): Promise<PaymentMethod[]>;
export function getAvailablePaymentMethods(contextInstance?: ShopwareApiInstance, params?: {
onlyAvailable?: boolean;
}): Promise<PaymentMethod[]>;

// @beta
export function getAvailableSalutations(contextInstance?: ShopwareApiInstance): Promise<SearchResult<Salutation[]>>;

// @beta (undocumented)
export function getAvailableShippingMethods(contextInstance?: ShopwareApiInstance): Promise<ShippingMethod[]>;
export function getAvailableShippingMethods(contextInstance?: ShopwareApiInstance, params?: {
onlyAvailable?: boolean;
}): Promise<ShippingMethod[]>;

// @beta
export function getCart(contextInstance?: ShopwareApiInstance): Promise<Cart>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@
<b>Signature:</b>

```typescript
export declare function getAvailablePaymentMethods(contextInstance?: ShopwareApiInstance): Promise<PaymentMethod[]>;
export declare function getAvailablePaymentMethods(contextInstance?: ShopwareApiInstance, params?: {
onlyAvailable?: boolean;
}): Promise<PaymentMethod[]>;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| contextInstance | [ShopwareApiInstance](./shopware-6-client.shopwareapiinstance.md) | |
| params | { onlyAvailable?: boolean; } | |

<b>Returns:</b>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@
<b>Signature:</b>

```typescript
export declare function getAvailableShippingMethods(contextInstance?: ShopwareApiInstance): Promise<ShippingMethod[]>;
export declare function getAvailableShippingMethods(contextInstance?: ShopwareApiInstance, params?: {
onlyAvailable?: boolean;
}): Promise<ShippingMethod[]>;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| contextInstance | [ShopwareApiInstance](./shopware-6-client.shopwareapiinstance.md) | |
| params | { onlyAvailable?: boolean; } | |

<b>Returns:</b>

Expand Down
4 changes: 2 additions & 2 deletions docs/landing/resources/api/shopware-6-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
| [getAvailableCountries(contextInstance)](./shopware-6-client.getavailablecountries.md) | <b><i>(BETA)</i></b> Get all available countries |
| [getAvailableCurrencies(contextInstance)](./shopware-6-client.getavailablecurrencies.md) | <b><i>(BETA)</i></b> |
| [getAvailableLanguages(contextInstance)](./shopware-6-client.getavailablelanguages.md) | <b><i>(BETA)</i></b> |
| [getAvailablePaymentMethods(contextInstance)](./shopware-6-client.getavailablepaymentmethods.md) | <b><i>(BETA)</i></b> |
| [getAvailablePaymentMethods(contextInstance, params)](./shopware-6-client.getavailablepaymentmethods.md) | <b><i>(BETA)</i></b> |
| [getAvailableSalutations(contextInstance)](./shopware-6-client.getavailablesalutations.md) | <b><i>(BETA)</i></b> Get all available salutations |
| [getAvailableShippingMethods(contextInstance)](./shopware-6-client.getavailableshippingmethods.md) | <b><i>(BETA)</i></b> |
| [getAvailableShippingMethods(contextInstance, params)](./shopware-6-client.getavailableshippingmethods.md) | <b><i>(BETA)</i></b> |
| [getCart(contextInstance)](./shopware-6-client.getcart.md) | <b><i>(BETA)</i></b> Gets the current cart for the sw-context-token. |
| [getCmsPage(path, criteria, contextInstance)](./shopware-6-client.getcmspage.md) | <b><i>(BETA)</i></b> |
| [getCustomer(contextInstance)](./shopware-6-client.getcustomer.md) | <b><i>(BETA)</i></b> Get customer's object |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ export interface CustomerRegistrationParams {
birthdayYear?: number;
birthdayMonth?: number;
birthdayDay?: number;
billingAddress: Partial<
BillingAddress
> /** TODO: replace Partial with correct optional fields in BillingAddress */;
billingAddress: Partial<BillingAddress> /** TODO: replace Partial with correct optional fields in BillingAddress */;
shippingAddress?: ShippingAddress;
/**
* storefrontUrl - should be one of the domains that are assigned to the corresponding sales channel (admin panel)
Expand Down
6 changes: 3 additions & 3 deletions packages/composables/src/hooks/useCms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ export const useCms = (rootContext: ApplicationVueContext): any => {
const { getDefaults } = useDefaults(rootContext, "useCms");
const error: Ref<any> = ref(null);
const loading: Ref<boolean> = ref(false);
const page: Ref<Readonly<
PageResolverProductResult | PageResolverResult<CmsPage>
>> = computed(() => {
const page: Ref<
Readonly<PageResolverProductResult | PageResolverResult<CmsPage>>
> = computed(() => {
return vuexStore.getters.getPage;
});
// TODO: rename it to something more obvious, or just leav as resourceIdentifier
Expand Down
10 changes: 8 additions & 2 deletions packages/composables/src/logic/useCheckout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ export const useCheckout = (
) => {
if (shippingMethods.value.length && !forceReload) return shippingMethods;
const shippingMethodsResponse = await getAvailableShippingMethods(
apiInstance
apiInstance,
{
onlyAvailable: true, // depending on the context, some of them can be hidden due to applied rules describing whether a method can be available
}
);
orderData.shippingMethods = shippingMethodsResponse || [];
return shippingMethods;
Expand All @@ -105,7 +108,10 @@ export const useCheckout = (
) => {
if (paymentMethods.value.length && !forceReload) return paymentMethods;
const paymentMethodsResponse = await getAvailablePaymentMethods(
apiInstance
apiInstance,
{
onlyAvailable: true, // depending on the context, some of them can be hidden due to applied rules describing whether a method can be available
}
);
orderData.paymentMethods = paymentMethodsResponse || [];
return paymentMethods;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ describe("ContextService - getAvailablePaymentMethods", () => {

const result = await getAvailablePaymentMethods();
expect(mockedGet).toBeCalledTimes(1);
expect(mockedGet).toBeCalledWith("/store-api/v3/payment-method");
expect(mockedGet).toBeCalledWith("/store-api/v3/payment-method", {
params: {},
});
expect(result).toHaveLength(2);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ describe("ContextService - getAvailableShippingMethods", () => {

const result = await getAvailableShippingMethods();
expect(mockedGet).toBeCalledTimes(1);
expect(mockedGet).toBeCalledWith("/store-api/v3/shipping-method");
expect(mockedGet).toBeCalledWith("/store-api/v3/shipping-method", {
params: {},
});
expect(result).toHaveLength(2);
});
});
16 changes: 12 additions & 4 deletions packages/shopware-6-client/src/services/contextService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,14 @@ export async function getAvailableSalutations(
* @beta
*/
export async function getAvailablePaymentMethods(
contextInstance: ShopwareApiInstance = defaultInstance
contextInstance: ShopwareApiInstance = defaultInstance,
params: { onlyAvailable?: boolean } = {}
): Promise<PaymentMethod[]> {
const resp = await contextInstance.invoke.get(
getContextPaymentMethodEndpoint()
getContextPaymentMethodEndpoint(),
{
params,
}
);

return resp.data;
Expand Down Expand Up @@ -202,10 +206,14 @@ export async function setCurrentPaymentMethod(
* @beta
*/
export async function getAvailableShippingMethods(
contextInstance: ShopwareApiInstance = defaultInstance
contextInstance: ShopwareApiInstance = defaultInstance,
params: { onlyAvailable?: boolean } = {}
): Promise<ShippingMethod[]> {
const resp = await contextInstance.invoke.get(
getContextShippingMethodEndpoint()
getContextShippingMethodEndpoint(),
{
params,
}
);

return resp.data;
Expand Down

1 comment on commit f0b8016

@vercel
Copy link

@vercel vercel bot commented on f0b8016 Mar 5, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.