Skip to content

Commit

Permalink
fix: add error response types to account, cart and subscriptions api (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
awwit authored Jun 12, 2024
1 parent a1c0263 commit 59107f8
Showing 1 changed file with 69 additions and 23 deletions.
92 changes: 69 additions & 23 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
import { Settings } from './settings';
import { Subscription } from './subscription';
import { Invoice } from './invoice';
import { ShipmentRating } from './shipment_rating';

export * from './account';
export * from './attribute';
Expand Down Expand Up @@ -128,6 +127,19 @@ export interface ItemDiscount {
amount?: number;
}

export interface SwellError {
code: string;
message: string;
}

export interface ServerError {
error: SwellError;
}

export interface ErrorResponse {
errors: Record<string, SwellError | undefined>;
}

export const version: string;

export function init(
Expand All @@ -137,16 +149,16 @@ export function init(
): void;

export namespace account {
export function create(input: Account): Promise<Account>;
export function create(input: Account): Promise<Account | ErrorResponse>;

export function login(
user: string,
password: string | PasswordTokenInput,
): Promise<Account | null>;

export function logout(): Promise<unknown>;
export function recover(input: object): Promise<Account>;
export function update(input: Account): Promise<Account>;
export function recover(input: object): Promise<Account | ErrorResponse>;
export function update(input: Account): Promise<Account | ErrorResponse>;
export function get(query?: object): Promise<Account | null>;

export function listAddresses(
Expand All @@ -158,16 +170,28 @@ export namespace account {
input?: object,
): Promise<ResultsResponse<Address>>;

export function createAddress(input: Address): Promise<Address>;
export function updateAddress(id: string, input: Address): Promise<Address>;
export function deleteAddress(id: string): Promise<Address>;
export function createAddress(
input: Address,
): Promise<Address | ErrorResponse>;

export function updateAddress(
id: string,
input: Address,
): Promise<Address | ErrorResponse>;

export function deleteAddress(id: string): Promise<Address | null>;

export function listCards(query?: object): Promise<ResultsResponse<Card>>;
/** @deprecated use `listCards` instead */
export function getCards(query?: object): Promise<ResultsResponse<Card>>;
export function createCard(input: Card): Promise<Card>;
export function updateCard(id: string, input: Card): Promise<Card>;
export function deleteCard(id: string): Promise<Card>;
export function createCard(input: Card): Promise<Card | ErrorResponse>;

export function updateCard(
id: string,
input: Card,
): Promise<Card | ErrorResponse>;

export function deleteCard(id: string): Promise<Card | null>;

export function listOrders(query?: object): Promise<ResultsResponse<Order>>;
/** @deprecated use `listOrders` instead */
Expand All @@ -189,20 +213,26 @@ export namespace card {

export namespace cart {
export function get(): Promise<Cart | null>;
export function update(input: object): Promise<Cart>;
export function update(input: object): Promise<Cart | null>;
export function recover(input: string): Promise<Cart>;

export function getSettings(): Promise<Settings>;
export function getShippingRates(): Promise<ShipmentRating>;
export function getShippingRates(): Promise<Cart>;

export function addItem(input: Partial<CartItem>): Promise<Cart>;
export function setItems(input: Partial<CartItem>[]): Promise<Cart>;
export function removeItem(id: string): Promise<Cart>;
export function addItem(
input: Partial<CartItem>,
): Promise<Cart | ErrorResponse>;

export function setItems(
input: Partial<CartItem>[],
): Promise<Cart | ErrorResponse>;

export function removeItem(id: string): Promise<Cart | ErrorResponse>;

export function updateItem(
id: string,
input: Partial<CartItem>,
): Promise<Cart>;
): Promise<Cart | ErrorResponse>;

export function applyCoupon(code: string): Promise<Cart>;
export function removeCoupon(): Promise<Cart>;
Expand All @@ -211,7 +241,7 @@ export namespace cart {
export function removeGiftcard(id: string): Promise<Cart>;

export function submitOrder(): Promise<Order>;
export function getOrder(checkoutId?: string): Promise<Order | null>;
export function getOrder(checkoutId?: string): Promise<Order>;
}

export namespace categories {
Expand Down Expand Up @@ -276,7 +306,7 @@ export namespace payment {
klarna?: InputPaymentRedirect;
}): Promise<void>;

export function authenticate(id: string): Promise<object>;
export function authenticate(id: string): Promise<object | { error: Error }>;
export function resetAsyncPayment(id: string): Promise<object>;

export function createIntent(input: {
Expand Down Expand Up @@ -361,20 +391,36 @@ export namespace settings {
}

export namespace subscriptions {
export function create(input: object): Promise<Subscription>;
export function update(id: string, input: object): Promise<Subscription>;
export function create(input: object): Promise<Subscription | ErrorResponse>;

export function update(
id: string,
input: object,
): Promise<Subscription | ErrorResponse>;

export function list(query?: object): Promise<ResultsResponse<Subscription>>;
export function get(id: string, query?: object): Promise<Subscription | null>;

export function addItem(id: string, input: object): Promise<Subscription>;
export function addItem(
id: string,
input: object,
): Promise<Subscription | ErrorResponse>;

export function setItems(
id: string,
input: object[],
): Promise<Subscription | ErrorResponse>;

export function updateItem(
id: string,
itemId: string,
input: object,
): Promise<Subscription>;
): Promise<Subscription | ErrorResponse>;

export function removeItem(id: string, itemId: string): Promise<unknown>;
export function removeItem(
id: string,
itemId: string,
): Promise<Subscription | ErrorResponse>;
}

export namespace invoices {
Expand Down

0 comments on commit 59107f8

Please sign in to comment.