Skip to content

Commit

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

describe('[vendure-api-client] getShippingMethods', () => {
it('returns shipping methods', async () => {
const givenVariables = {};

const context = {
config: {},
client: {
query: ({ variables, query }) => {
expect(variables).toEqual(givenVariables);
expect(query).toEqual(eligibleShippingMethodsQuery);

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

const { data } = await getShippingMethods(context);

expect(data).toBe('get shipping methods response');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import gql from 'graphql-tag';

export default gql`
query eligibleShippingMethods {
eligibleShippingMethods {
id
price
priceWithTax
code
name
description
metadata
}
}
`;
22 changes: 22 additions & 0 deletions packages/api-client/src/api/getShippingMethods/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import gql from 'graphql-tag';
import eligibleShippingMethodsQuery from './eligibleShippingMethodsQuery';
import { CustomQuery } from '@vue-storefront/core';
import { Context, RequestDataStructure, GetShippingMethodsResponse, PaymentMethodQuote } from '../../types';

const getShippingMethods = async (context: Context, customQuery?: CustomQuery): Promise<GetShippingMethodsResponse> => {
const getShippingMethods = {};

const { eligibleShippingMethods } = context.extendQuery(
customQuery, { eligibleShippingMethods: { query: eligibleShippingMethodsQuery, variables: getShippingMethods } }
);

const request = await context.client.query<RequestDataStructure<'eligiblePaymentMethods', PaymentMethodQuote[]>>({
query: gql`${eligibleShippingMethods.query}`,
variables: eligibleShippingMethods.variables,
fetchPolicy: 'no-cache'
});
return request;

};

export default getShippingMethods;
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 @@ -9,3 +9,4 @@ export { default as applyCartCoupon } from './applyCartCoupon';
export { default as removeCartCoupon } from './removeCartCoupon';
export { default as getMe } from './getMe';
export { default as updateAddressDetails } from './updateAddressDetails';
export { default as getShippingMethods } from './getShippingMethods';
6 changes: 4 additions & 2 deletions packages/api-client/src/types/API.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CustomQuery } from '@vue-storefront/core';
import { ApolloQueryResult } from 'apollo-client';
import { FetchResult } from 'apollo-link';
import { ActiveOrderResult, ApplyCouponCodeResult, CollectionList, Customer, Order, Product, RemoveOrderItemsResult, SearchResponse, UpdateOrderItemsResult } from './GraphQL';
import { ActiveOrderResult, ApplyCouponCodeResult, CollectionList, Customer, Order, PaymentMethodQuote, Product, RemoveOrderItemsResult, SearchResponse, UpdateOrderItemsResult } from './GraphQL';
import { AddToCartParams, CartCouponParams, CollectionParams, ProductParams, RemoveFromCartParams, SearchParams, UpdateAddressDetailsParams, UpdateCartParams } from './types';

export type QueryResponse<K extends string, V> = ApolloQueryResult<Record<K, V>>;
Expand All @@ -13,6 +13,7 @@ export type GetCategoryResponse = QueryResponse<'collections', CollectionList>;
export type GetFacetResponse = QueryResponse<'search', SearchResponse>;
export type GetCartResponse = QueryResponse<'activeOrder', Order>;
export type GetMeResponse = QueryResponse<'activeCustomer', Customer>;
export type GetShippingMethodsResponse = QueryResponse<'eligiblePaymentMethods', PaymentMethodQuote[]>;
export type AddToCartResponse = MutationResponse<'addItemToOrder', UpdateOrderItemsResult>;
export type RemoveFromCartResponse = MutationResponse<'removeOrderLine', RemoveOrderItemsResult>;
export type UpdateCartQuantityResponse = MutationResponse<'adjustOrderLine', UpdateOrderItemsResult>;
Expand All @@ -25,7 +26,8 @@ export interface VendureApiMethods {
getFacet(params: SearchParams, customQuery?: CustomQuery): Promise<GetFacetResponse>;
getCategory(params: CollectionParams, customQuery?: CustomQuery): Promise<GetCategoryResponse>;
getCart(customQuery?: CustomQuery): Promise<GetCartResponse>;
getMe(customQuery?: CustomQuery): Promise<GetMeResponse>;
getsMe(customQuery?: CustomQuery): Promise<GetMeResponse>;
getShippingMethods(customQuery?: CustomQuery): Promise<GetShippingMethodsResponse>;
addToCart(params: AddToCartParams, customQuery?: CustomQuery): Promise<AddToCartResponse>;
removeFromCart(params: RemoveFromCartParams, customQuery?: CustomQuery): Promise<RemoveFromCartResponse>;
updateCartQuantity(params: UpdateCartParams, customQuery?: CustomQuery): Promise<UpdateCartQuantityResponse>;
Expand Down

0 comments on commit 1cd437b

Please sign in to comment.