@faststore/api 1.6.15 → 1.6.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +30 -0
- package/dist/__generated__/schema.d.ts +15 -0
- package/dist/api.cjs.development.js +76 -8
- package/dist/api.cjs.development.js.map +1 -1
- package/dist/api.cjs.production.min.js +1 -1
- package/dist/api.cjs.production.min.js.map +1 -1
- package/dist/api.esm.js +76 -8
- package/dist/api.esm.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/platforms/vtex/clients/commerce/index.d.ts +2 -0
- package/dist/platforms/vtex/clients/commerce/types/Region.d.ts +8 -0
- package/dist/platforms/vtex/clients/index.d.ts +1 -0
- package/dist/platforms/vtex/index.d.ts +3 -2
- package/dist/platforms/vtex/resolvers/aggregateOffer.d.ts +1 -0
- package/dist/platforms/vtex/resolvers/mutation.d.ts +1 -0
- package/dist/platforms/vtex/resolvers/updateSession.d.ts +3 -0
- package/dist/platforms/vtex/utils/channel.d.ts +8 -0
- package/dist/typings/index.d.ts +1 -0
- package/package.json +2 -2
- package/src/__generated__/schema.ts +19 -0
- package/src/index.ts +1 -1
- package/src/platforms/vtex/clients/commerce/index.ts +12 -0
- package/src/platforms/vtex/clients/commerce/types/Region.ts +7 -0
- package/src/platforms/vtex/resolvers/aggregateOffer.ts +31 -10
- package/src/platforms/vtex/resolvers/mutation.ts +2 -0
- package/src/platforms/vtex/resolvers/product.ts +7 -2
- package/src/platforms/vtex/resolvers/updateSession.ts +26 -0
- package/src/platforms/vtex/utils/channel.ts +25 -0
- package/src/typeDefs/mutation.graphql +14 -0
- package/src/typings/index.ts +1 -0
|
@@ -3,17 +3,38 @@ import type { Simulation } from '../clients/commerce/types/Simulation'
|
|
|
3
3
|
|
|
4
4
|
type Resolvers = (root: Simulation & { product: EnhancedSku }) => unknown
|
|
5
5
|
|
|
6
|
+
const inStock = (item: Simulation['items'][0]) =>
|
|
7
|
+
item.availability === 'available'
|
|
8
|
+
|
|
9
|
+
// Smallest Available Selling Price First
|
|
10
|
+
export const sortOfferByPrice = (
|
|
11
|
+
items: Simulation['items']
|
|
12
|
+
): Simulation['items'] =>
|
|
13
|
+
items.sort((a, b) => {
|
|
14
|
+
if (inStock(a) && !inStock(b)) {
|
|
15
|
+
return -1
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (!inStock(a) && inStock(b)) {
|
|
19
|
+
return 1
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return a.sellingPrice - b.sellingPrice
|
|
23
|
+
})
|
|
24
|
+
|
|
6
25
|
export const StoreAggregateOffer: Record<string, Resolvers> = {
|
|
7
|
-
highPrice: ({ items }) =>
|
|
8
|
-
items.
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
) / 1e2
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
26
|
+
highPrice: ({ items }) => {
|
|
27
|
+
const availableItems = items.filter(inStock)
|
|
28
|
+
const highPrice = availableItems.pop()?.sellingPrice
|
|
29
|
+
|
|
30
|
+
return (highPrice ?? 0) / 1e2
|
|
31
|
+
},
|
|
32
|
+
lowPrice: ({ items }) => {
|
|
33
|
+
const availableItems = items.filter(inStock)
|
|
34
|
+
const lowPrice = availableItems[0]?.sellingPrice
|
|
35
|
+
|
|
36
|
+
return (lowPrice ?? 0) / 1e2
|
|
37
|
+
},
|
|
17
38
|
offerCount: ({ items }) => items.length,
|
|
18
39
|
priceCurrency: () => '',
|
|
19
40
|
offers: ({ items, product }) => items.map((item) => ({ ...item, product })),
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { slugify } from '../utils/slugify'
|
|
1
2
|
import type { Resolver } from '..'
|
|
2
3
|
import type { EnhancedSku } from '../utils/enhanceSku'
|
|
3
|
-
import {
|
|
4
|
+
import { sortOfferByPrice } from './aggregateOffer'
|
|
4
5
|
|
|
5
6
|
type Root = EnhancedSku
|
|
6
7
|
|
|
@@ -81,7 +82,11 @@ export const StoreProduct: Record<string, Resolver<Root>> = {
|
|
|
81
82
|
|
|
82
83
|
const simulation = await simulationLoader.load(items)
|
|
83
84
|
|
|
84
|
-
return {
|
|
85
|
+
return {
|
|
86
|
+
...simulation,
|
|
87
|
+
items: sortOfferByPrice(simulation.items),
|
|
88
|
+
product,
|
|
89
|
+
}
|
|
85
90
|
},
|
|
86
91
|
isVariantOf: ({ isVariantOf }) => isVariantOf,
|
|
87
92
|
additionalProperty: ({ attributes = [] }) =>
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Context } from '..'
|
|
2
|
+
import type {
|
|
3
|
+
MutationUpdateSessionArgs,
|
|
4
|
+
StoreSession,
|
|
5
|
+
} from '../../../__generated__/schema'
|
|
6
|
+
import ChannelMarshal from '../utils/channel'
|
|
7
|
+
|
|
8
|
+
export const updateSession = async (
|
|
9
|
+
_: any,
|
|
10
|
+
{ session }: MutationUpdateSessionArgs,
|
|
11
|
+
{ clients }: Context
|
|
12
|
+
): Promise<StoreSession> => {
|
|
13
|
+
const channel = ChannelMarshal.parse(session.channel ?? '')
|
|
14
|
+
const regionData = await clients.commerce.checkout.region({
|
|
15
|
+
postalCode: String(session.postalCode ?? '').replace(/\D/g, ''),
|
|
16
|
+
country: session.country ?? '',
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
return {
|
|
20
|
+
...session,
|
|
21
|
+
channel: ChannelMarshal.stringify({
|
|
22
|
+
...channel,
|
|
23
|
+
regionId: regionData?.[0]?.id,
|
|
24
|
+
}),
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export interface Channel {
|
|
2
|
+
regionId?: string
|
|
3
|
+
salesChannel?: string
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export default class ChannelMarshal {
|
|
7
|
+
public static parse(channelString: string): Channel {
|
|
8
|
+
try {
|
|
9
|
+
const parsedChannel = JSON.parse(channelString) as Channel
|
|
10
|
+
|
|
11
|
+
return {
|
|
12
|
+
regionId: parsedChannel.regionId ?? '',
|
|
13
|
+
salesChannel: parsedChannel.salesChannel ?? '',
|
|
14
|
+
}
|
|
15
|
+
} catch (error) {
|
|
16
|
+
console.error(error)
|
|
17
|
+
|
|
18
|
+
throw new Error('Malformed channel string')
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public static stringify(channel: Channel): string {
|
|
23
|
+
return JSON.stringify(channel)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -1,4 +1,18 @@
|
|
|
1
|
+
type StoreSession {
|
|
2
|
+
channel: String
|
|
3
|
+
country: String
|
|
4
|
+
postalCode: String
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
input IStoreSession {
|
|
8
|
+
channel: String
|
|
9
|
+
country: String
|
|
10
|
+
postalCode: String
|
|
11
|
+
}
|
|
12
|
+
|
|
1
13
|
type Mutation {
|
|
2
14
|
# Returns the order if anything changed with the order. Null if the order is valid
|
|
3
15
|
validateCart(cart: IStoreCart!): StoreCart
|
|
16
|
+
|
|
17
|
+
updateSession(session: IStoreSession!): StoreSession!
|
|
4
18
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type Platform = 'vtex'
|