Skip to content

Commit

Permalink
feat(store-api): Add cart to store-api (#963)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlgimenes authored Oct 4, 2021
1 parent 0f63589 commit 0607d82
Show file tree
Hide file tree
Showing 28 changed files with 932 additions and 92 deletions.
6 changes: 5 additions & 1 deletion packages/store-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
},
"dependencies": {
"@graphql-tools/schema": "^8.2.0",
"graphql": "^15.5.3",
"dataloader": "^2.0.0",
"fast-deep-equal": "^3.1.3",
"isomorphic-unfetch": "^3.1.0",
"rollup-plugin-graphql": "^0.1.0",
"slugify": "^1.6.0"
Expand All @@ -36,5 +37,8 @@
"tsdx": "^0.14.1",
"tslib": "^2.3.1",
"typescript": "^4.4.2"
},
"peerDependencies": {
"graphql": "^15.6.0"
}
}
68 changes: 68 additions & 0 deletions packages/store-api/src/__generated__/schema.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 61 additions & 9 deletions packages/store-api/src/platforms/vtex/clients/commerce/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@ import type {
Simulation,
SimulationArgs,
SimulationOptions,
} from './types/Checkout'
} from './types/Simulation'
import type { CategoryTree } from './types/CategoryTree'
import type { Options } from '../..'
import type { Brand } from './types/Brand'
import type { OrderForm, OrderFormInputItem } from './types/OrderForm'

const BASE_INIT = {
method: 'POST',
headers: {
'content-type': 'application/json',
},
}

const getBase = ({ account, environment }: Options) =>
`http://${account}.${environment}.com.br`

export const VtexCommerce = (opts: Options) => {
const base = getBase(opts)
export const VtexCommerce = (options: Options) => {
const { channel } = options
const base = getBase(options)

return {
catalog: {
Expand All @@ -28,18 +37,61 @@ export const VtexCommerce = (opts: Options) => {
checkout: {
simulation: (
args: SimulationArgs,
options: SimulationOptions = { sc: '1' }
{ salesChannel }: SimulationOptions = { salesChannel: channel }
): Promise<Simulation> => {
const params = new URLSearchParams({ ...options })
const params = new URLSearchParams({
sc: salesChannel,
})

return fetchAPI(
`${base}/api/checkout/pub/orderForms/simulation?${params.toString()}`,
{
method: 'POST',
...BASE_INIT,
body: JSON.stringify(args),
headers: {
'content-type': 'application/json',
},
}
)
},
orderForm: ({
id,
refreshOutdatedData = true,
salesChannel = channel,
}: {
id: string
refreshOutdatedData?: boolean
salesChannel?: string
}): Promise<OrderForm> => {
const params = new URLSearchParams({
refreshOutdatedData: refreshOutdatedData.toString(),
sc: salesChannel,
})

return fetchAPI(
`${base}/api/checkout/pub/orderForm/${id}?${params.toString()}`,
BASE_INIT
)
},
updateOrderFormItems: ({
id,
orderItems,
allowOutdatedData = 'paymentData',
salesChannel = channel,
}: {
id: string
orderItems: OrderFormInputItem[]
allowOutdatedData?: 'paymentData'
salesChannel?: string
}): Promise<OrderForm> => {
const params = new URLSearchParams({
allowOutdatedData,
sc: salesChannel,
})

return fetchAPI(
`${base}/api/checkout/pub/orderForm/${id}/items?${params}`,
{
...BASE_INIT,
body: JSON.stringify({ orderItems }),
method: 'PATCH',
}
)
},
Expand Down
Loading

0 comments on commit 0607d82

Please sign in to comment.