@faststore/api 2.0.156-alpha.0 → 2.0.157-alpha.0

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.
@@ -13,8 +13,9 @@ import type { SalesChannel } from './types/SalesChannel';
13
13
  import { MasterDataResponse } from './types/Newsletter';
14
14
  import type { Address, AddressInput } from './types/Address';
15
15
  import { ShippingDataBody } from './types/ShippingData';
16
+ import { IncrementedAddress } from './types/IncrementedAddress';
16
17
  declare type ValueOf<T> = T extends Record<string, infer K> ? K : never;
17
- export declare const VtexCommerce: ({ account, environment }: Options, ctx: Context) => {
18
+ export declare const VtexCommerce: ({ account, environment, incrementAddress }: Options, ctx: Context) => {
18
19
  catalog: {
19
20
  salesChannel: (sc: string) => Promise<SalesChannel>;
20
21
  brand: {
@@ -36,10 +37,11 @@ export declare const VtexCommerce: ({ account, environment }: Options, ctx: Cont
36
37
  };
37
38
  checkout: {
38
39
  simulation: (args: SimulationArgs, { salesChannel }?: SimulationOptions) => Promise<Simulation>;
40
+ incrementAddress: (country: string, postalCode: string) => Promise<IncrementedAddress>;
39
41
  shippingData: ({ id, body, }: {
40
42
  id: string;
41
43
  body: ShippingDataBody;
42
- }) => Promise<OrderForm>;
44
+ }, incrementedAddress?: IncrementedAddress | undefined) => Promise<OrderForm>;
43
45
  orderForm: ({ id, refreshOutdatedData, channel, }: {
44
46
  id: string;
45
47
  refreshOutdatedData?: boolean | undefined;
@@ -0,0 +1,16 @@
1
+ export interface IncrementedAddress {
2
+ postalCode?: string | null;
3
+ city?: string | null;
4
+ state?: string | null;
5
+ country?: string | null;
6
+ street?: string | null;
7
+ number?: string | null;
8
+ neighborhood?: string | null;
9
+ complement?: string | null;
10
+ reference?: string | null;
11
+ geoCoordinates?: GeoCoordinates | null | [];
12
+ }
13
+ export interface GeoCoordinates {
14
+ latitude: GLfloat;
15
+ longitude: GLfloat;
16
+ }
@@ -29,10 +29,11 @@ export declare const getClients: (options: Options, ctx: Context) => {
29
29
  };
30
30
  checkout: {
31
31
  simulation: (args: import("./commerce/types/Simulation").SimulationArgs, { salesChannel }?: import("./commerce/types/Simulation").SimulationOptions) => Promise<import("./commerce/types/Simulation").Simulation>;
32
+ incrementAddress: (country: string, postalCode: string) => Promise<import("./commerce/types/IncrementedAddress").IncrementedAddress>;
32
33
  shippingData: ({ id, body, }: {
33
34
  id: string;
34
35
  body: import("./commerce/types/ShippingData").ShippingDataBody;
35
- }) => Promise<import("./commerce/types/OrderForm").OrderForm>;
36
+ }, incrementedAddress?: import("./commerce/types/IncrementedAddress").IncrementedAddress | undefined) => Promise<import("./commerce/types/OrderForm").OrderForm>;
36
37
  orderForm: ({ id, refreshOutdatedData, channel, }: {
37
38
  id: string;
38
39
  refreshOutdatedData?: boolean | undefined;
@@ -9,6 +9,7 @@ export interface Options {
9
9
  channel: string;
10
10
  locale: string;
11
11
  hideUnavailableItems: boolean;
12
+ incrementAddress: boolean;
12
13
  flags?: FeatureFlags;
13
14
  }
14
15
  interface FeatureFlags {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faststore/api",
3
- "version": "2.0.156-alpha.0",
3
+ "version": "2.0.157-alpha.0",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -49,5 +49,5 @@
49
49
  "peerDependencies": {
50
50
  "graphql": "^15.6.0"
51
51
  },
52
- "gitHead": "d24d06c4907a19c8f33cae1812175e393f223340"
52
+ "gitHead": "2fe3d9c5452955ce2444486eefb015a9b9921ccf"
53
53
  }
@@ -20,6 +20,7 @@ import type { SalesChannel } from './types/SalesChannel'
20
20
  import { MasterDataResponse } from './types/Newsletter'
21
21
  import type { Address, AddressInput } from './types/Address'
22
22
  import { ShippingDataBody } from './types/ShippingData'
23
+ import { IncrementedAddress } from './types/IncrementedAddress'
23
24
 
24
25
 
25
26
  type ValueOf<T> = T extends Record<string, infer K> ? K : never
@@ -32,7 +33,7 @@ const BASE_INIT = {
32
33
  }
33
34
 
34
35
  export const VtexCommerce = (
35
- { account, environment }: Options,
36
+ { account, environment, incrementAddress }: Options,
36
37
  ctx: Context
37
38
  ) => {
38
39
  const base = `https://${account}.${environment}.com.br`
@@ -91,28 +92,48 @@ export const VtexCommerce = (
91
92
  }
92
93
  )
93
94
  },
95
+
96
+ incrementAddress: (
97
+ country: string,
98
+ postalCode: string
99
+ ): Promise<IncrementedAddress> => {
100
+
101
+ return incrementAddress
102
+ ? fetchAPI(`${base}/api/checkout/pub/postal-code/${country}/${postalCode}`, {
103
+ method: 'GET',
104
+ headers: {
105
+ 'content-type': 'application/json'
106
+ },
107
+ })
108
+ : Promise.resolve(undefined);
109
+ },
110
+
94
111
  shippingData: ({
112
+
95
113
  id,
96
114
  body,
97
115
  }: {
98
116
  id: string
99
117
  body: ShippingDataBody
100
- }): Promise<OrderForm> => {
118
+ },
119
+ incrementedAddress?: IncrementedAddress): Promise<OrderForm> => {
120
+
101
121
 
102
122
  const mappedBody = {
103
123
  "selectedAddresses": body?.selectedAddresses?.map(address => ({
104
124
  "addressType": address.addressType || null,
105
125
  "receiverName": address.receiverName || null,
106
- "postalCode": address.postalCode || null,
107
- "city": address.city || null,
108
- "state": address.state || null,
109
- "country": address.country || null,
110
- "street": address.street || null,
111
- "number": address.number || null,
112
- "neighborhood": address.neighborhood || null,
113
- "complement": address.complement || null,
114
- "reference": address.reference || null,
115
- "geoCoordinates": address.geoCoordinates || []
126
+ "postalCode": address.postalCode || incrementedAddress?.postalCode || null,
127
+ "city": incrementedAddress?.city || null,
128
+ "state": incrementedAddress?.state || null,
129
+ "country": address.country || incrementedAddress?.country || null,
130
+ "street": incrementedAddress?.street || null,
131
+ "number": incrementedAddress?.number || null,
132
+ "neighborhood": incrementedAddress?.neighborhood || null,
133
+ "complement": incrementedAddress?.complement || null,
134
+ "reference": incrementedAddress?.reference || null,
135
+ "geoCoordinates": address.geoCoordinates || incrementedAddress?.geoCoordinates || []
136
+
116
137
  }))
117
138
  };
118
139
 
@@ -0,0 +1,17 @@
1
+ export interface IncrementedAddress {
2
+ postalCode?: string | null;
3
+ city?: string | null;
4
+ state?: string | null;
5
+ country?: string | null;
6
+ street?: string | null;
7
+ number?: string | null;
8
+ neighborhood?: string | null;
9
+ complement?: string | null;
10
+ reference?: string | null;
11
+ geoCoordinates?: GeoCoordinates | null | [];
12
+ }
13
+
14
+ export interface GeoCoordinates {
15
+ latitude: GLfloat;
16
+ longitude: GLfloat;
17
+ }
@@ -35,6 +35,7 @@ export interface Options {
35
35
  channel: string
36
36
  locale: string
37
37
  hideUnavailableItems: boolean
38
+ incrementAddress: boolean,
38
39
  flags?: FeatureFlags
39
40
  }
40
41
 
@@ -91,16 +92,16 @@ const Resolvers = {
91
92
 
92
93
  export const getContextFactory =
93
94
  (options: Options) =>
94
- (ctx: any): Context => {
95
- ctx.storage = {
96
- channel: ChannelMarshal.parse(options.channel),
97
- flags: options.flags ?? {},
98
- locale: options.locale,
99
- }
100
- ctx.clients = getClients(options, ctx)
101
- ctx.loaders = getLoaders(options, ctx)
95
+ (ctx: any): Context => {
96
+ ctx.storage = {
97
+ channel: ChannelMarshal.parse(options.channel),
98
+ flags: options.flags ?? {},
99
+ locale: options.locale,
100
+ }
101
+ ctx.clients = getClients(options, ctx)
102
+ ctx.loaders = getLoaders(options, ctx)
102
103
 
103
- return ctx
104
- }
104
+ return ctx
105
+ }
105
106
 
106
107
  export const getResolvers = (_: Options) => Resolvers
@@ -21,6 +21,7 @@ import type {
21
21
  OrderFormInputItem,
22
22
  OrderFormItem
23
23
  } from '../clients/commerce/types/OrderForm'
24
+ import { IncrementedAddress } from '../clients/commerce/types/IncrementedAddress'
24
25
 
25
26
  type Indexed<T> = T & { index?: number }
26
27
 
@@ -210,14 +211,14 @@ async function getOrderNumberFromSession(
210
211
  const { namespaces } = await commerce.getSessionOrder()
211
212
  return namespaces.checkout?.orderFormId?.value
212
213
  }
213
- return ;
214
+ return;
214
215
  }
215
216
 
216
217
  // Returns the regionalized orderForm
217
218
  const getOrderForm = async (
218
219
  id: string,
219
220
  session: Maybe<IStoreSession> | undefined,
220
- { clients: { commerce } }: Context
221
+ { clients: { commerce } }: Context,
221
222
  ) => {
222
223
  const orderForm = await commerce.checkout.orderForm({
223
224
  id,
@@ -234,15 +235,27 @@ const getOrderForm = async (
234
235
 
235
236
  const shouldUpdateShippingData =
236
237
  typeof session.postalCode === 'string' &&
237
- orderForm.shippingData?.address?.postalCode != session.postalCode
238
+ orderForm.shippingData?.address?.postalCode !== session.postalCode ||
239
+ (
240
+ typeof session.geoCoordinates === 'object' &&
241
+ typeof session.geoCoordinates?.latitude === 'number' &&
242
+ typeof session.geoCoordinates.longitude === 'number' &&
243
+ (orderForm.shippingData?.address?.geoCoordinates[0] !== session.geoCoordinates.longitude ||
244
+ orderForm.shippingData?.address?.geoCoordinates[1] !== session.geoCoordinates.latitude)
245
+ );
238
246
 
239
247
  if (shouldUpdateShippingData) {
248
+ let incrementedAddress: IncrementedAddress | undefined;
249
+ if (session.postalCode) {
250
+ incrementedAddress = await commerce.checkout.incrementAddress(session.country, session.postalCode)
251
+
252
+ }
240
253
  return commerce.checkout.shippingData({
241
254
  id: orderForm.orderFormId,
242
255
  body: {
243
256
  selectedAddresses: [session],
244
257
  },
245
- })
258
+ }, incrementedAddress)
246
259
  }
247
260
 
248
261
  return orderForm
@@ -298,11 +311,11 @@ export const validateCart = async (
298
311
  // If so, this means the user interacted with this cart elsewhere and expects
299
312
  // to see this new cart state instead of what's stored on the user's browser.
300
313
  const isStale = isOrderFormStale(orderForm)
301
-
314
+
302
315
  if (isStale && orderNumber) {
303
316
  const newOrderForm = await setOrderFormEtag(orderForm, commerce).then(
304
317
  joinItems
305
- )
318
+ )
306
319
  return orderFormToCart(newOrderForm, skuLoader)
307
320
  }
308
321