Skip to content

Commit

Permalink
feat: #49 (api) setShippingMethod function
Browse files Browse the repository at this point in the history
  • Loading branch information
Baroshem committed Aug 21, 2021
1 parent 1cd437b commit 088f0d4
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 1 deletion.
26 changes: 26 additions & 0 deletions packages/api-client/__tests__/api/setShippingMethod.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import setShippingMethod from '../../src/api/setShippingMethod';
import setOrderShippingMethodMutation from '../../src/api/setShippingMethod/setOrderShippingMethodMutation';
import { Context } from '../../src/types';

describe('[vendure-api-client] setShippingMethod', () => {
it('sets shipping method for certain order', async () => {
const givenVariables = { shippingMethodId: '1'};

const context = {
config: {},
client: {
mutate: ({ variables, mutation }) => {
expect(variables).toEqual(givenVariables);
expect(mutation).toEqual(setOrderShippingMethodMutation);

return { data: 'set shipping method response' };
}
},
extendQuery: (customQuery, args) => args
} as unknown as Context;

const { data } = await setShippingMethod(context, { shippingMethodId: '1' });

expect(data).toBe('set shipping method response');
});
});
1 change: 1 addition & 0 deletions packages/api-client/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export { default as removeCartCoupon } from './removeCartCoupon';
export { default as getMe } from './getMe';
export { default as updateAddressDetails } from './updateAddressDetails';
export { default as getShippingMethods } from './getShippingMethods';
export { default as setShippingMethod } from './setShippingMethod';
24 changes: 24 additions & 0 deletions packages/api-client/src/api/setShippingMethod/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import setOrderShippingMethodMutation from './setOrderShippingMethodMutation';
import { CustomQuery } from '@vue-storefront/core';
import gql from 'graphql-tag';
import { Context, SetShippingMethodParams, SetShippingMethodResponse } from '../../types';

const setShippingMethod = async (context: Context, params: SetShippingMethodParams, customQuery?: CustomQuery): Promise<SetShippingMethodResponse> => {
const setShippingMethodVariables = {
...params
};

const { setOrderShippingMethod } = context.extendQuery(
customQuery, { setOrderShippingMethod: { query: setOrderShippingMethodMutation, variables: setShippingMethodVariables } }
);

const request = await context.client.mutate({
mutation: gql`${setOrderShippingMethod.query}`,
variables: setOrderShippingMethod.variables,
fetchPolicy: 'no-cache'
}) as SetShippingMethodResponse;

return request;
};

export default setShippingMethod;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import gql from 'graphql-tag';
import { CartFragment, ErrorResultFragment } from '../../fragments';

export default gql`
${CartFragment}
${ErrorResultFragment}
mutation setOrderShippingMethod($shippingMethodId: ID!) {
setOrderShippingMethod(shippingMethodId: $shippingMethodId) {
...Cart
...ErrorResult
}
}
`;
4 changes: 3 additions & 1 deletion packages/api-client/src/types/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CustomQuery } from '@vue-storefront/core';
import { ApolloQueryResult } from 'apollo-client';
import { FetchResult } from 'apollo-link';
import { ActiveOrderResult, ApplyCouponCodeResult, CollectionList, Customer, Order, PaymentMethodQuote, Product, RemoveOrderItemsResult, SearchResponse, UpdateOrderItemsResult } from './GraphQL';
import { AddToCartParams, CartCouponParams, CollectionParams, ProductParams, RemoveFromCartParams, SearchParams, UpdateAddressDetailsParams, UpdateCartParams } from './types';
import { AddToCartParams, CartCouponParams, CollectionParams, ProductParams, RemoveFromCartParams, SearchParams, SetShippingMethodParams, UpdateAddressDetailsParams, UpdateCartParams } from './types';

export type QueryResponse<K extends string, V> = ApolloQueryResult<Record<K, V>>;
export type MutationResponse<K extends string, V> = FetchResult<Record<K, V>>;
Expand All @@ -20,6 +20,7 @@ export type UpdateCartQuantityResponse = MutationResponse<'adjustOrderLine', Upd
export type ApplyCouponCodeResponse = MutationResponse<'applyCouponCode', ApplyCouponCodeResult>;
export type RemoveCouponCodeResponse = MutationResponse<'removeCouponCode', Order>;
export type UpdateAddressDetailsResponse = MutationResponse<'setOrderShippingAddress' | 'setOrderBillingAddress', ActiveOrderResult>;
export type SetShippingMethodResponse = MutationResponse<'setOrderShippingMethod', Order>;

export interface VendureApiMethods {
getProduct(params: ProductParams, customQuery?: CustomQuery): Promise<GetProductResponse>;
Expand All @@ -34,4 +35,5 @@ export interface VendureApiMethods {
applyCouponCode(params: CartCouponParams, customQuery?: CustomQuery): Promise<ApplyCouponCodeResponse>;
removeCouponCode(params: CartCouponParams, customQuery?: CustomQuery): Promise<RemoveCouponCodeResponse>;
updateAddressDetails(params: UpdateAddressDetailsParams, customQuery?: CustomQuery): Promise<UpdateAddressDetailsResponse>;
setShippingMethod(params: SetShippingMethodParams, customQuery?: CustomQuery): Promise<SetShippingMethodResponse>;
}
4 changes: 4 additions & 0 deletions packages/api-client/src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,7 @@ export type UpdateAddressDetailsParams = {
input: CreateAddressInput;
type?: string;
};

export type SetShippingMethodParams = {
shippingMethodId: string;
}

0 comments on commit 088f0d4

Please sign in to comment.