Skip to content

Commit

Permalink
feat: #56 (api) getPaymentMethods
Browse files Browse the repository at this point in the history
  • Loading branch information
Baroshem committed Aug 22, 2021
1 parent d9d6837 commit e4b4744
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
26 changes: 26 additions & 0 deletions packages/api-client/__tests__/api/getPaymentMethods.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import getPaymentMethods from '../../src/api/getPaymentMethods';
import eligiblePaymentMethodsQuery from '../../src/api/getPaymentMethods/eligiblePaymentMethodsQuery';
import { Context } from '../../src/types';

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

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

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

const { data } = await getPaymentMethods(context);

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

export default gql`
query eligiblePaymentMethods {
eligiblePaymentMethods {
id
code
name
description
isEligible
eligibilityMessage
}
}
`;
22 changes: 22 additions & 0 deletions packages/api-client/src/api/getPaymentMethods/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import gql from 'graphql-tag';
import eligiblePaymentMethodsQuery from './eligiblePaymentMethodsQuery';
import { CustomQuery } from '@vue-storefront/core';
import { Context, RequestDataStructure, PaymentMethodQuote, GetPaymentMethodsResponse } from '../../types';

const getPaymentMethods = async (context: Context, customQuery?: CustomQuery): Promise<GetPaymentMethodsResponse> => {
const getPaymentMethodsVariables = {};

const { eligiblePaymentMethods } = context.extendQuery(
customQuery, { eligiblePaymentMethods: { query: eligiblePaymentMethodsQuery, variables: getPaymentMethodsVariables } }
);

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

};

export default getPaymentMethods;
4 changes: 3 additions & 1 deletion 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, CreateCustomerInput, Customer, Order, PaymentInput, Product, RemoveOrderItemsResult, SearchResponse, SetCustomerForOrderResult, ShippingMethodQuote, UpdateOrderItemsResult } from './GraphQL';
import { ActiveOrderResult, ApplyCouponCodeResult, CollectionList, CreateCustomerInput, Customer, Order, PaymentInput, PaymentMethodQuote, Product, RemoveOrderItemsResult, SearchResponse, SetCustomerForOrderResult, ShippingMethodQuote, UpdateOrderItemsResult } from './GraphQL';
import { AddToCartParams, CartCouponParams, CollectionParams, ProductParams, RemoveFromCartParams, SearchParams, SetShippingMethodParams, TransitionOrderToStateParams, UpdateAddressDetailsParams, UpdateCartParams } from './types';

export type QueryResponse<K extends string, V> = ApolloQueryResult<Record<K, V>>;
Expand All @@ -14,6 +14,7 @@ export type GetFacetResponse = QueryResponse<'search', SearchResponse>;
export type GetCartResponse = QueryResponse<'activeOrder', Order>;
export type GetMeResponse = QueryResponse<'activeCustomer', Customer>;
export type GetShippingMethodsResponse = QueryResponse<'eligibleShippingMethods', ShippingMethodQuote[]>;
export type GetPaymentMethodsResponse = QueryResponse<'eligiblePaymentMethods', PaymentMethodQuote[]>;
export type AddToCartResponse = MutationResponse<'addItemToOrder', UpdateOrderItemsResult>;
export type RemoveFromCartResponse = MutationResponse<'removeOrderLine', RemoveOrderItemsResult>;
export type UpdateCartQuantityResponse = MutationResponse<'adjustOrderLine', UpdateOrderItemsResult>;
Expand All @@ -32,6 +33,7 @@ export interface VendureApiMethods {
getCart(customQuery?: CustomQuery): Promise<GetCartResponse>;
getsMe(customQuery?: CustomQuery): Promise<GetMeResponse>;
getShippingMethods(customQuery?: CustomQuery): Promise<GetShippingMethodsResponse>;
getPaymentMethods(customQuery?: CustomQuery): Promise<GetPaymentMethodsResponse>;
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 e4b4744

Please sign in to comment.