Skip to content

Commit

Permalink
feat: add cart_errors (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinlee11 authored Jul 19, 2024
1 parent b9f5859 commit 3cd768b
Show file tree
Hide file tree
Showing 11 changed files with 244 additions and 39 deletions.
3 changes: 3 additions & 0 deletions src/api/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ const getCartError = (response: Response, responseJson: CartBaseErrorResponse) =
cartError.status = 500;
}
}
if (responseJson.cart_errors) {
cartError.cart_errors = responseJson.cart_errors;
}
return cartError;
};

Expand Down
62 changes: 54 additions & 8 deletions src/types/api/cart/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/naming-convention */

export const FulfillmentType = {
SHIPMENT: 'SHIPMENT',
PICKUP: 'PICKUP',
Expand Down Expand Up @@ -102,6 +104,9 @@ export interface CartError extends Error {
* ```
*/
errors?: CartValidationErrors;

/** Errors applicable to the cart, can be used for buying facing error handling. */
cart_errors: CartError[];
}

export interface BaseModifier {
Expand Down Expand Up @@ -431,18 +436,59 @@ export interface CartBaseResponse {
/** The cart object. See https://square.github.io/custom-sites-docs/resources/cart#cart */
// eslint-disable-next-line @typescript-eslint/naming-convention
cart_data: unknown;
/** Validation for scheduling. Empty if not pickup or delivery fulfillment. */
validation: CartValidation;
/** Errors applicable to the cart. Empty array if there are none. */
cart_errors: CartError[];
};
}

export interface CartValidation {
scheduled?: {
/** Whether or not the pickup or delivery schedule update was valid. */
valid: boolean;
/** If the schedule update was invalid, the error reason why. */
error: string;
export interface CartError {
/** Translated error string based on the site locale you can use to display to the buyer. */
translated: string;
/** The next valid time if the error is related to scheduling, otherwise excluded. */
next?: CartErrorNext;
}

export interface CartErrorNext {
/** RFC3339 formatted date-time. */
time: string;
/** Human-readable label for time. Dependent on fulfillment type. */
time_formatted: string;
/** UNIX timestamp of pickup/delivery time. */
time_unix: number;
/** RFC3339 date based on the store location's timezone. */
date: string;
/** Prescribed localized time-only label for use in buyer-facing time selectors. */
label: string;
/** Details about Prep time duration. */
prep_time_duration: {
/** Duration in minutes. */
in_minutes: number;
/** Duration in RFC3339 format. */
rfc3339_interval: string;
};
/** Included if using a window for pickup hours. */
pickup_window?: {
/** Start of the window for the pickup hours. */
start: CartErrorNextFulfillmentWindow;
/** End of the window for the pickup hours. */
end: CartErrorNextFulfillmentWindow;
};
/** Included if the fulfillment is delivery. */
delivery_window?: {
/** Start of the window for the delivery hours. */
start: CartErrorNextFulfillmentWindow;
/** End of the window for the delivery hours. */
end: CartErrorNextFulfillmentWindow;
};
}

export interface CartErrorNextFulfillmentWindow {
/** RFC3339 formatted date-time. */
time: string;
/** Human-readable label for time. */
time_formatted: string;
/** UNIX timestamp of pickup/delivery time. */
time_unix: number;
}

export interface CartResponse extends CartBaseResponse {
Expand Down
5 changes: 3 additions & 2 deletions src/types/api/cart/private.types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
CartValidationErrors,
CartValidation
CartError
} from '.';

import type LooseObject from '../../looseobject';
Expand All @@ -13,13 +13,14 @@ export interface CartBaseErrorResponse {
fields?: string[];
message?: string;
errors?: CartValidationErrors;
cart_errors: CartError[];
}

export interface CartFetchResponse extends CartBaseErrorResponse {
data?: {
cart: string;
cart_data: unknown;
validation: CartValidation;
cart_errors: CartError[];
};
}

Expand Down
15 changes: 13 additions & 2 deletions test/cart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,13 @@ describe('Add item', () => {
});

it('should throw error', async () => {
const fetchResponse = createFetchResponse({}, false, 500);
const fetchResponse = createFetchResponse({
'cart_errors': [
{
translation: 'error message',
}
]
}, false, 500);
vi.mocked(fetch).mockResolvedValue(fetchResponse);

const addItemFn = sdk.cart.addItem({
Expand All @@ -771,7 +777,12 @@ describe('Add item', () => {

await expect(addItemFn).rejects.toThrowError(STATUS_TEXT);
await expect(addItemFn).rejects.toMatchObject({
status: 500
status: 500,
cart_errors: [
{
translation: 'error message',
}
]
});
});

Expand Down
2 changes: 1 addition & 1 deletion typedocs/interfaces/types_api_cart.CartBaseResponse.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@
| :------ | :------ | :------ |
| `cart` | `string` | The `orderId` for the order. Only needed if you're doing very custom order management. |
| `cart_data` | `unknown` | The cart object. See https://square.github.io/custom-sites-docs/resources/cart#cart |
| `validation` | [`CartValidation`](types_api_cart.CartValidation.md) | Validation for scheduling. Empty if not pickup or delivery fulfillment. |
| `cart_errors` | [`CartError`](types_api_cart.CartError.md)[] | Errors applicable to the cart. Empty array if there are none. |
27 changes: 27 additions & 0 deletions typedocs/interfaces/types_api_cart.CartError.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ Depending on the error encountered, the `CartError` may contain any of the prope
- [status](types_api_cart.CartError.md#status)
- [fields](types_api_cart.CartError.md#fields)
- [errors](types_api_cart.CartError.md#errors)
- [cart\_errors](types_api_cart.CartError.md#cart_errors)
- [translated](types_api_cart.CartError.md#translated)
- [next](types_api_cart.CartError.md#next)

## Properties

Expand Down Expand Up @@ -65,3 +68,27 @@ Populated if there are any validation errors
]
}
```

___

### cart\_errors

**cart\_errors**: [`CartError`](types_api_cart.CartError.md)[]

Errors applicable to the cart, can be used for buying facing error handling.

___

### translated

**translated**: `string`

Translated error string based on the site locale you can use to display to the buyer.

___

### next

`Optional` **next**: [`CartErrorNext`](types_api_cart.CartErrorNext.md)

The next valid time if the error is related to scheduling, otherwise excluded.
103 changes: 103 additions & 0 deletions typedocs/interfaces/types_api_cart.CartErrorNext.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
[@square/site-theme-sdk](../GettingStarted.md) / [Modules](../modules.md) / [types/api/cart](../modules/types_api_cart.md) / CartErrorNext

# Interface: CartErrorNext

[types/api/cart](../modules/types_api_cart.md).CartErrorNext

## Table of contents

### Properties

- [time](types_api_cart.CartErrorNext.md#time)
- [time\_formatted](types_api_cart.CartErrorNext.md#time_formatted)
- [time\_unix](types_api_cart.CartErrorNext.md#time_unix)
- [date](types_api_cart.CartErrorNext.md#date)
- [label](types_api_cart.CartErrorNext.md#label)
- [prep\_time\_duration](types_api_cart.CartErrorNext.md#prep_time_duration)
- [pickup\_window](types_api_cart.CartErrorNext.md#pickup_window)
- [delivery\_window](types_api_cart.CartErrorNext.md#delivery_window)

## Properties

### time

**time**: `string`

RFC3339 formatted date-time.

___

### time\_formatted

**time\_formatted**: `string`

Human-readable label for time. Dependent on fulfillment type.

___

### time\_unix

**time\_unix**: `number`

UNIX timestamp of pickup/delivery time.

___

### date

**date**: `string`

RFC3339 date based on the store location's timezone.

___

### label

**label**: `string`

Prescribed localized time-only label for use in buyer-facing time selectors.

___

### prep\_time\_duration

**prep\_time\_duration**: `Object`

Details about Prep time duration.

#### Type declaration

| Name | Type | Description |
| :------ | :------ | :------ |
| `in_minutes` | `number` | Duration in minutes. |
| `rfc3339_interval` | `string` | Duration in RFC3339 format. |

___

### pickup\_window

`Optional` **pickup\_window**: `Object`

Included if using a window for pickup hours.

#### Type declaration

| Name | Type | Description |
| :------ | :------ | :------ |
| `start` | [`CartErrorNextFulfillmentWindow`](types_api_cart.CartErrorNextFulfillmentWindow.md) | Start of the window for the pickup hours. |
| `end` | [`CartErrorNextFulfillmentWindow`](types_api_cart.CartErrorNextFulfillmentWindow.md) | End of the window for the pickup hours. |

___

### delivery\_window

`Optional` **delivery\_window**: `Object`

Included if the fulfillment is delivery.

#### Type declaration

| Name | Type | Description |
| :------ | :------ | :------ |
| `start` | [`CartErrorNextFulfillmentWindow`](types_api_cart.CartErrorNextFulfillmentWindow.md) | Start of the window for the delivery hours. |
| `end` | [`CartErrorNextFulfillmentWindow`](types_api_cart.CartErrorNextFulfillmentWindow.md) | End of the window for the delivery hours. |
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[@square/site-theme-sdk](../GettingStarted.md) / [Modules](../modules.md) / [types/api/cart](../modules/types_api_cart.md) / CartErrorNextFulfillmentWindow

# Interface: CartErrorNextFulfillmentWindow

[types/api/cart](../modules/types_api_cart.md).CartErrorNextFulfillmentWindow

## Table of contents

### Properties

- [time](types_api_cart.CartErrorNextFulfillmentWindow.md#time)
- [time\_formatted](types_api_cart.CartErrorNextFulfillmentWindow.md#time_formatted)
- [time\_unix](types_api_cart.CartErrorNextFulfillmentWindow.md#time_unix)

## Properties

### time

**time**: `string`

RFC3339 formatted date-time.

___

### time\_formatted

**time\_formatted**: `string`

Human-readable label for time.

___

### time\_unix

**time\_unix**: `number`

UNIX timestamp of pickup/delivery time.
2 changes: 1 addition & 1 deletion typedocs/interfaces/types_api_cart.CartResponse.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
| :------ | :------ | :------ |
| `cart` | `string` | The `orderId` for the order. Only needed if you're doing very custom order management. |
| `cart_data` | `unknown` | The cart object. See https://square.github.io/custom-sites-docs/resources/cart#cart |
| `validation` | [`CartValidation`](types_api_cart.CartValidation.md) | Validation for scheduling. Empty if not pickup or delivery fulfillment. |
| `cart_errors` | [`CartError`](types_api_cart.CartError.md)[] | Errors applicable to the cart. Empty array if there are none. |

#### Inherited from

Expand Down
24 changes: 0 additions & 24 deletions typedocs/interfaces/types_api_cart.CartValidation.md

This file was deleted.

3 changes: 2 additions & 1 deletion typedocs/modules/types_api_cart.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
- [RemoveItemRequest](../interfaces/types_api_cart.RemoveItemRequest.md)
- [PutFulfillmentRequest](../interfaces/types_api_cart.PutFulfillmentRequest.md)
- [CartBaseResponse](../interfaces/types_api_cart.CartBaseResponse.md)
- [CartValidation](../interfaces/types_api_cart.CartValidation.md)
- [CartErrorNext](../interfaces/types_api_cart.CartErrorNext.md)
- [CartErrorNextFulfillmentWindow](../interfaces/types_api_cart.CartErrorNextFulfillmentWindow.md)
- [CartResponse](../interfaces/types_api_cart.CartResponse.md)

### Type Aliases
Expand Down

0 comments on commit 3cd768b

Please sign in to comment.