-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: #49 (api) create api-client function
- Loading branch information
Showing
8 changed files
with
146 additions
and
5 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
packages/api-client/__tests__/api/updateAddressDetails.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import updateAddressDetails from '../../src/api/updateAddressDetails'; | ||
import setOrderBillingAddressMutation from './../../src/api/updateAddressDetails/setOrderBillingAddressMutation'; | ||
import setOrderShippingAddressMutation from './../../src/api/updateAddressDetails/setOrderShippingAddressMutation'; | ||
import { Context } from '../../src/types'; | ||
import { BILLING_TYPE } from '../../src/helpers/constants'; | ||
|
||
describe('[vendure-api-client] updateAddressDetails', () => { | ||
it('updates shipping details', async () => { | ||
const givenVariables = { | ||
input: { | ||
fullName: 'Eliot Anderson', | ||
company: 'F Society', | ||
streetLine1: '1st Avenue', | ||
streetLine2: '1st Avenue', | ||
city: 'New York', | ||
province: 'New York', | ||
postalCode: '34-234', | ||
countryCode: 'US', | ||
phoneNumber: '123123123', | ||
defaultShippingAddress: true, | ||
defaultBillingAddress: false | ||
} | ||
}; | ||
|
||
const context = ({ | ||
config: {}, | ||
client: { | ||
mutate: ({ variables, mutation }) => { | ||
expect(variables).toEqual(givenVariables); | ||
expect(mutation).toEqual(setOrderShippingAddressMutation); | ||
|
||
return { data: 'update shipping details response' }; | ||
} | ||
}, | ||
extendQuery: (customQuery, args) => args | ||
} as unknown) as Context; | ||
|
||
const { data } = await updateAddressDetails(context, { input: givenVariables.input }); | ||
|
||
expect(data).toBe('update shipping details response'); | ||
}); | ||
|
||
it('updates billing details', async () => { | ||
const givenVariables = { | ||
input: { | ||
fullName: 'Eliot Anderson', | ||
company: 'F Society', | ||
streetLine1: '1st Avenue', | ||
streetLine2: '1st Avenue', | ||
city: 'New York', | ||
province: 'New York', | ||
postalCode: '34-234', | ||
countryCode: 'US', | ||
phoneNumber: '123123123', | ||
defaultShippingAddress: false, | ||
defaultBillingAddress: true | ||
} | ||
}; | ||
|
||
const context = ({ | ||
config: {}, | ||
client: { | ||
mutate: ({ variables, mutation }) => { | ||
expect(variables).toEqual(givenVariables); | ||
expect(mutation).toEqual(setOrderBillingAddressMutation); | ||
|
||
return { data: 'update billing details response' }; | ||
} | ||
}, | ||
extendQuery: (customQuery, args) => args | ||
} as unknown) as Context; | ||
|
||
const { data } = await updateAddressDetails(context, { input: givenVariables.input, type: BILLING_TYPE }); | ||
|
||
expect(data).toBe('update billing details response'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import setOrderBillingAddressMutation from './setOrderBillingAddressMutation'; | ||
import setOrderShippingAddressMutation from './setOrderShippingAddressMutation'; | ||
import { CustomQuery } from '@vue-storefront/core'; | ||
import gql from 'graphql-tag'; | ||
import { Context, UpdateAddressDetailsParams, UpdateAddressDetailsResponse } from '../../types'; | ||
import { BILLING_TYPE } from '../../helpers/constants'; | ||
|
||
const updateAddressDetails = async (context: Context, params: UpdateAddressDetailsParams, customQuery?: CustomQuery): Promise<UpdateAddressDetailsResponse> => { | ||
const { type, input } = params; | ||
const updateAddressDetailsVariables = { input }; | ||
|
||
const updateAddressDetailsQuery = type === BILLING_TYPE ? setOrderBillingAddressMutation : setOrderShippingAddressMutation; | ||
|
||
const { updateAddressDetails } = context.extendQuery( | ||
customQuery, { updateAddressDetails: { query: updateAddressDetailsQuery, variables: updateAddressDetailsVariables } } | ||
); | ||
|
||
const request = await context.client.mutate({ | ||
mutation: gql`${updateAddressDetails.query}`, | ||
variables: updateAddressDetails.variables, | ||
fetchPolicy: 'no-cache' | ||
}) as UpdateAddressDetailsResponse; | ||
|
||
return request; | ||
}; | ||
|
||
export default updateAddressDetails; |
14 changes: 14 additions & 0 deletions
14
packages/api-client/src/api/updateAddressDetails/setOrderBillingAddressMutation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 setOrderBillingAddress($input: CreateAddressInput!) { | ||
setOrderBillingAddress(input: $input) { | ||
...Cart | ||
...ErrorResult | ||
} | ||
} | ||
`; |
14 changes: 14 additions & 0 deletions
14
packages/api-client/src/api/updateAddressDetails/setOrderShippingAddressMutation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 setOrderShippingAddress($input: CreateAddressInput!) { | ||
setOrderShippingAddress(input: $input) { | ||
...Cart | ||
...ErrorResult | ||
} | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export const VENDURE_AUTH_TOKEN_NAME = 'vendure-auth-token'; | ||
export const BILLING_TYPE = 'billing'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters