Socket
Book a DemoInstallSign in
Socket

@deckweiss/cart

Package Overview
Dependencies
Maintainers
3
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@deckweiss/cart

This is a shopping cart implementation for SvelteKit applications at Deckweiss.

latest
Source
npmnpm
Version
2.1.0
Version published
Maintainers
3
Created
Source

Cart for SvelteKit

This is a shopping cart implementation for SvelteKit applications at Deckweiss.

Installation

Step 1: Install package

pnpm i @deckweiss/cart

Step 2: Add hook

// src/hooks.server.ts
import { handle } from '@deckweiss/cart'

export { handle }

Step 3: Initialize useCart()

// +layout.server.ts
export const load: LayoutServerLoad = function (event) {
    return { cart: event.locals.cart.cart };
};
// +layout.svelte
<script lang="ts">
    import { setCartContext } from '@deckweiss/cart';

    let { data, children } = $props();

    setCartContext(data.cart);
</script>

{@render children()}

Step 4: Use cart

<script lang="ts">
import { useCart } from '@deckweiss/cart'
let cart = useCart()
</script>

<button on:click={() => cart.addProduct('id1', 1)}>Add product 1</button>
<button on:click={() => cart.addProduct('id2', 1)}>Add product 2</button>
<button on:click={() => cart.clear()}>Clear cart</button>

{#each cart.cart.products as product}
    <div>
        <h3>{product.id}</h3>
        <p>Amount: {product.amount}</p>
        <button on:click={() => cart.removeProduct(product.id)}>Remove product</button>
    </div>
{:else}
    <p>Cart is empty</p>
{/each}

Custom metadata with TypeScript

The types CartMetaData and CartProductMetaData support to be extended/augmented via TypeScript. This means, your editor will recognize the custom types and give you intellisense.

// src/app.d.ts
declare module '@deckweiss/cart' {
    interface CartMetaData {
        userId?: string
	    note?: string
	    discountCode?: string
    }

    interface CartProductMetaData {
        type?: 'food''tool' | 'book'
    }
}

FAQs

Package last updated on 01 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts