### Install create-nube-app with NPM
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/create-nube-app/README.md
Use this command to start a new NubeSDK project using NPM.
```sh
npm create nube-app@latest
```
--------------------------------
### Install create-nube-app with Yarn
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/create-nube-app/README.md
Use this command to start a new NubeSDK project using Yarn.
```sh
yarn create nube-app
```
--------------------------------
### Install create-nube-app with PNPM
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/create-nube-app/README.md
Use this command to start a new NubeSDK project using PNPM.
```sh
pnpm create nube-app
```
--------------------------------
### Install @tiendanube/nube-sdk-ui
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/ui/README.md
Install the @tiendanube/nube-sdk-ui package and its peer dependency @tiendanube/nube-sdk-types.
```sh
npm install @tiendanube/nube-sdk-ui @tiendanube/nube-sdk-types
```
--------------------------------
### Interactive create-nube-app Usage Example
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/create-nube-app/README.md
This example demonstrates the interactive prompts when creating a new NubeSDK application, including project naming, template selection, and Git initialization.
```sh
$ npm create nube-app@latest
◆ What is the project's name?
│ my-nube-app
│
◆ Select a template:
│ ● Minimal
│ ○ Minimal with UI
│ ○ Minimal with UI in JSX
│
◇ Initialize git repository?
│ Yes
```
--------------------------------
### Install dependencies
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/nube-devtools/README.md
Install the required project dependencies using npm.
```bash
npm install
```
--------------------------------
### Example Project Structure
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/create-nube-app/README.md
This outlines the typical directory and file structure generated by `create-nube-app` for a new NubeSDK project.
```sh
my-nube-app/
├── .vscode/
│ ├── extensions.json
│ └── settings.json
├── src/
│ ├── main.ts
│ └── main.test.ts
├── .gitignore
├── biome.json
├── package.json
├── README.md
├── tsconfig.json
├── tsup.config.js
└── vitest.config.ts
```
--------------------------------
### Run NubeSDK in development mode
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/examples/age-confirmation.md
Starts a live server for local development of your application.
```bash
npm run dev
```
--------------------------------
### Install @tiendanube/nube-sdk-jsx dependencies
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/jsx/README.md
Install the package along with its required peer dependencies.
```sh
npm install @tiendanube/nube-sdk-jsx @tiendanube/nube-sdk-ui @tiendanube/nube-sdk-types
```
--------------------------------
### Install NubeSDK Types
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/types/README.md
Install the @tiendanube/nube-sdk-types package as a development dependency.
```sh
npm install -D @tiendanube/nube-sdk-types
```
--------------------------------
### Creating a Complete Styled Component
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/styling.md
Example demonstrating the integration of StyleSheet, styled components, and theme tokens in a single component.
```tsx
import { styled, StyleSheet, theme } from "@tiendanube/nube-sdk-ui";
import { Box, Button, Text } from "@tiendanube/nube-sdk-jsx";
// Reusable styles
const styles = StyleSheet.create({
container: {
backgroundColor: theme.color.main.background,
borderRadius: theme.border.radius,
padding: "24px",
},
title: {
color: theme.color.main.foreground,
fontSize: "24px",
fontWeight: "bold",
marginBottom: "16px",
},
});
// Static styled component
const StyledButton = styled(Button)`
background-color: ${theme.color.accent};
color: white;
padding: 12px 24px;
border-radius: ${theme.border.radius};
border: 1px solid ${theme.border.color};
transition: all 0.2s;
&:hover {
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
`;
// Usage
Welcome to NubeSDKGet Started
```
--------------------------------
### Install Nube SDK UI Package
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/getting-started.md
Install the Nube SDK UI package if your application has a user interface. This package provides UI components and utilities.
```bash
npm install @tiendanube/nube-sdk-ui
```
```bash
yarn add @tiendanube/nube-sdk-ui
```
```bash
pnpm add @tiendanube/nube-sdk-ui
```
```bash
bun add @tiendanube/nube-sdk-ui
```
--------------------------------
### Run Development Server with npm
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/getting-started.md
Start the local development server using npm. This server is configured for easy debugging with embedded sourcemaps.
```bash
npm run dev
```
```bash
yarn dev
```
```bash
pnpm dev
```
```bash
bun run dev
```
--------------------------------
### Install Tsup Bundler
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/getting-started.md
Install tsup as a development dependency for bundling your application. Tsup is a simple and fast bundler recommended for Nube SDK projects.
```bash
npm install -D tsup
```
```bash
yarn add -D tsup
```
```bash
pnpm add -D tsup
```
```bash
bun add -D tsup
```
--------------------------------
### Create Nube App with CLI
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/examples/age-confirmation.md
Use the NubeSDK CLI to create a new application. Ensure Node.js version 16 or higher is installed.
```bash
npm create nube-app@latest
```
--------------------------------
### Install NubeSDK Snippet Package
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/snippet/README.md
Add the package as a development dependency to your project.
```bash
npm install --save-dev @tiendanube/nube-sdk-snippet
```
--------------------------------
### Create a UI Component Tree
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/ui/README.md
Import and use various UI component functions to construct a declarative component tree. This example demonstrates creating a layout with text and an input field.
```ts
import { box, col, row, field, img, txt, fragment } from "@tiendanube/nube-sdk-ui";
const componentTree = box({
children: [
row({
children: [
col({ children: txt({ children: "Hello, NubeSDK!" }) }),
],
}),
field({ name: "email", label: "Email" })
]
});
```
--------------------------------
### Install Nube SDK Types
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/getting-started.md
Install the Nube SDK types package as a development dependency. This is required to access NubeSDK types in your project.
```bash
npm install -D typescript @tiendanube/nube-sdk-types
```
```bash
yarn add -D typescript @tiendanube/nube-sdk-types
```
```bash
pnpm add -D typescript @tiendanube/nube-sdk-types
```
```bash
bun add -D typescript @tiendanube/nube-sdk-types
```
--------------------------------
### Install Nube SDK JSX Package
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/getting-started.md
Install the Nube SDK JSX package if you intend to use JSX syntax in your project. This enables JSX transformations and runtime.
```bash
npm install @tiendanube/nube-sdk-jsx
```
```bash
yarn add @tiendanube/nube-sdk-jsx
```
```bash
pnpm add @tiendanube/nube-sdk-jsx
```
```bash
bun add @tiendanube/nube-sdk-jsx
```
--------------------------------
### Listen to Cart Updates with NubeSDK
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/types/README.md
Example of how to use NubeSDK types to listen for cart updates and access the cart state. Requires importing NubeSDK and NubeSDKState types.
```ts
import type { NubeSDK, NubeSDKState } from "@tiendanube/nube-sdk-types";
function App(nube: NubeSDK) {
nube.on("cart:update", (state: NubeSDKState) => {
console.log("Cart updated:", state.cart);
});
}
```
--------------------------------
### Implement Textarea Event Handlers
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/components/textarea.md
Example of handling events using the NubeComponentTextareaEventHandler type.
```tsx
import { Textarea } from "@tiendanube/nube-sdk-jsx";
import type { NubeComponentTextareaEventHandler } from "@tiendanube/nube-sdk-types";
const handleEvents: NubeComponentTextareaEventHandler = (event) => {
const { type, value, state } = event;
// Perform some action
};
;
```
```typescript
import { textarea } from "@tiendanube/nube-sdk-ui";
import type { NubeComponentTextareaEventHandler } from "@tiendanube/nube-sdk-types";
const handleEvents: NubeComponentTextareaEventHandler = (event) => {
const { type, value, state } = event;
// Perform some action
};
textarea({
name: "description",
label: "Description",
value: "example@example.com",
maxLength: 500,
rows: 4,
onChange: handleEvents,
onBlur: handleEvents,
onFocus: handleEvents,
});
```
--------------------------------
### Accessing NubeSDKState
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/state.md
Demonstrates how to get the current NubeSDKState using the `getState` function and access its properties.
```APIDOC
## Accessing NubeSDKState
### Description
This section shows how to retrieve the current state of the Nube SDK using the `getState` function. This is useful for accessing information when your application starts.
### Method
`getState()`
### Endpoint
N/A (Client-side SDK function)
### Parameters
None
### Request Example
```typescript
import type { NubeSDK, NubeSDKState } from "@tiendanube/nube-sdk-types";
export function App(nube: NubeSDK) {
// Get current state
const currentState: Readonly = nube.getState();
// Access state properties
const cartTotal = currentState.cart.total;
const storeCurrency = currentState.store.currency;
const currentPage = currentState.location.page.type;
}
```
### Response
#### Success Response (NubeSDKState)
- **currentState** (Readonly) - An immutable object representing the current state of the Nube SDK.
#### Response Example
```json
{
"cart": {
"total": 150.75,
"items": [...],
"validation": {}
},
"config": {
"validationRules": {}
},
"location": {
"page": {
"type": "product",
"params": {}
},
"pathname": "/products/123"
},
"store": {
"currency": "USD",
"name": "My Store"
},
"ui": {
"slots": {}
},
"shipping": null,
"customer": null,
"payment": null
}
```
```
--------------------------------
### Button with onClick Handler Accessing State
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/components/button.md
This example demonstrates how to use the onClick handler to access the current SDK state. The handler receives an object containing the event type and the SDK state.
```tsx
import { Button } from "@tiendanube/nube-sdk-jsx";
;
```
```typescript
import { button } from "@tiendanube/nube-sdk-ui";
button({
children: "Click here",
onClick: ({ state }) => {
// Access the current state
console.log(state);
// Perform some action
}
});
```
--------------------------------
### Get Current App State
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/state.md
Retrieve the current state of the Nube SDK using the `getState` function. This is useful for accessing information when the application starts.
```typescript
import type { NubeSDK, NubeSDKState } from "@tiendanube/nube-sdk-types";
export function App(nube: NubeSDK) {
// Get current state
const currentState: Readonly = nube.getState();
// Access state properties
const cartTotal = currentState.cart.total;
const storeCurrency = currentState.store.currency;
const currentPage = currentState.location.page.type;
}
```
--------------------------------
### Scaffold a new Nube application
Source: https://github.com/tiendanube/nube-sdk/blob/main/README.md
Use the CLI tool to initialize a project with all necessary dependencies.
```sh
npm create nube-app@latest
```
```sh
yarn create nube-app
```
```sh
pnpm create nube-app
```
```sh
bun create nube-app
```
--------------------------------
### Build Production App
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/getting-started.md
Execute the build command to create the production-ready JavaScript file. The output will be located in the `./dist/` directory.
```bash
npx tsup
```
```bash
yarn tsup
```
```bash
pnpm tsup
```
```bash
bunx tsup
```
--------------------------------
### Build NubeSDK for production
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/examples/age-confirmation.md
Creates an optimized version of your script in the dist directory for publication.
```bash
npm run build
```
--------------------------------
### Navigate to project directory
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/nube-devtools/README.md
Change the current working directory to the devtools package folder.
```bash
cd packages/nube-devtools
```
--------------------------------
### Build Production Bundle with npm
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/getting-started.md
Generate an optimized production bundle for your application using npm. The output script is located in `./dist/main.min.js`.
```bash
npm run build
```
```bash
yarn build
```
```bash
pnpm build
```
```bash
bun run build
```
--------------------------------
### Page Type Validation Utility
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/examples/age-confirmation.md
A utility function to check if the current location in the app is the starting page of the checkout process.
```tsx
import type { AppLocation } from "@tiendanube/nube-sdk-types";
export function isStartPage(location: AppLocation) {
return location.page.type === "checkout" && location.page.data.step === "start";
}
```
--------------------------------
### Create App Entrypoint
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/getting-started.md
Define the main application function that accepts the NubeSDK instance. This serves as the entry point for your Nube SDK application.
```typescript
// src/main.ts
import type { NubeSDK } from "@tiendanube/nube-sdk-types";
export function App(nube: NubeSDK) {
// your code here!
}
```
--------------------------------
### App Entry Point with NubeSDK
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/getting-started.md
The `App` function in `main.ts` or `main.tsx` is the entry point for your application, providing access to the NubeSDK instance.
```typescript
import type { NubeSDK } from "@tiendanube/nube-sdk-types";
export function App(nube: NubeSDK) {
// your code here!
}
```
--------------------------------
### Field with Event Handlers in JSX
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/components/field.md
Example of using a Field component in JSX with a shared event handler function for onChange, onBlur, and onFocus.
```tsx
import { Field } from "@tiendanube/nube-sdk-jsx";
import type { NubeComponentFieldEventHandler } from "@tiendanube/nube-sdk-types";
const handleEvents: NubeComponentFieldEventHandler = (event) => {
const { type, value, state } = event;
// Perform some action
};
;
```
--------------------------------
### Define the App entry point in NubeSDK
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/secure-by-design.md
Every application must export an App function which receives the SDK instance for secure communication.
```javascript
export function App(nube) {
// Your code start here!
}
```
--------------------------------
### Get Item from Session Storage with NubeSDK
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/browser-apis.md
Retrieve a stored value using `asyncSessionStorage.getItem`. The retrieved value is available in the promise's resolution.
```typescript
import type { NubeSDK } from "@tiendanube/nube-sdk-types";
export function App(nube: NubeSDK) {
const browser = nube.getBrowserAPIs();
browser.asyncSessionStorage.getItem("my_custom_field").then((value) => {
console.log("my_custom_field", value);
});
}
```
--------------------------------
### Run Unit Tests with npm
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/getting-started.md
Use this command to run unit tests for your application using npm. Vitest is preconfigured for testing.
```bash
npm test
```
```bash
yarn test
```
```bash
pnpm test
```
```bash
bun test
```
--------------------------------
### Listen for Cart Updates
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/events.md
The `cart:update` event is dispatched by the store when the cart's content changes. This example logs a message if more than 5 different items are in the cart.
```typescript
nube.on("cart:update", ({ cart }) => {
if (cart.items.length > 5) {
console.log("Purchased more than 5 different items");
}
});
```
--------------------------------
### Initialize App Configuration
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/examples/age-confirmation.md
Configure the application to indicate it performs cart validation. This is done by sending a 'config:set' event.
```tsx
import type { NubeSDK } from "@tiendanube/nube-sdk-types";
export function App(nube: NubeSDK) {
nube.send("config:set", () => ({
config: {
has_cart_validation: true,
},
}));
}
```
--------------------------------
### Available Scripts in Created Project
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/create-nube-app/README.md
Lists the common npm scripts available in a project generated by `create-nube-app`, covering build, development, testing, linting, and formatting tasks.
```sh
- npm run build
- npm run dev
- npm run test
- npm run test:watch
- npm run test:coverage
- npm run lint
- npm run format
```
--------------------------------
### Validate Cart Content
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/events.md
The `cart:validate` event is dispatched by an app to signal cart validity. It requires `has_cart_validation: true` in the script configuration. This example shows how to reject the cart if it has fewer than 5 items, dispatching a `fail` event, or a `success` event otherwise.
```typescript
// Tell NubeSDK that this script wants to validate the content of the cart
nube.send("config:set", () => ({
config: {
has_cart_validation: true
},
}));
nube.on("cart:update", ({ cart }) => {
// Reject the cart if it has fewer than 5 items
if (cart.items.length < 5) {
// Dispatch a failed `cart:validate` event with the reason why it failed to validate
nube.send("cart:validate", () => ({
cart: {
validation: {
status: "fail",
reason: "Cart must have at least 5 items!",
},
},
}));
return;
}
// Dispatch a successful `cart:validate` event
nube.send("cart:validate", () => ({
cart: { validation: { status: "success" } },
}));
});
```
--------------------------------
### Format Code with npm
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/getting-started.md
Execute this command to format all source code according to project standards using npm. This ensures consistent code style.
```bash
npm run format
```
```bash
yarn format
```
```bash
pnpm format
```
```bash
bun run format
```
--------------------------------
### Implementing Responsive Design
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/styling.md
Use standard CSS media queries within styled components to handle responsive layouts.
```typescript
const ResponsiveContainer = styled(box)`
padding: 16px;
flex-direction: column;
@media (min-width: 768px) {
padding: 24px;
flex-direction: row;
}
`;
```
--------------------------------
### Tsup Bundler Configuration
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/getting-started.md
Configure `tsup.config.js` for your build process. This configuration specifies entry points, output formats, and optimization settings. Adjust `esbuildOptions.alias` if using JSX.
```javascript
// tsup.config.js
import { defineConfig } from "tsup";
export default defineConfig({
entry: ["src/main.tsx"],
format: ["esm"],
target: "esnext",
clean: true,
minify: true,
bundle: true,
sourcemap: false,
splitting: false,
skipNodeModulesBundle: false,
esbuildOptions(options) {
// only if you use JSX
options.alias = {
"@tiendanube/nube-sdk-jsx/dist/jsx-runtime": "@tiendanube/nube-sdk-jsx/jsx-runtime"
};
},
outExtension: ({ options }) => ({
js: options.minify ? ".min.js" : ".js"
})
});
```
--------------------------------
### config:set
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/events.md
Dispatched by the app to set initial configuration for the integration.
```APIDOC
## EVENT config:set
### Description
Dispatched by the app to setup initial configuration.
### Request Body
- **config** (object) - Required - Configuration object
- **has_cart_validation** (boolean) - Optional - Enables cart validation feature
- **disable_shipping_more_options** (boolean) - Optional - Determines whether the user can select a shipping option
### Request Example
```typescript
nube.send("config:set", () => ({
config: {
has_cart_validation: true
},
}));
```
```
--------------------------------
### Set Initial Configuration
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/events.md
Use `config:set` to establish initial configuration for the app. Requires `has_cart_validation` to be set to `true` for cart validation events to function.
```typescript
nube.send("config:set", () => ({
config: {
has_cart_validation: true
},
}));
```
--------------------------------
### Configure and Render a NubeSDK Snippet
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/snippet/README.md
Include the triple-slash reference at the top of the file to enable TypeScript support for injected globals. Use the 'nube.render' method to mount components to the platform.
```tsx
///
const translations: Readonly> = {
pt: "Olá, bem-vindo!",
es: "Hola, ¡bienvenido!",
en: "Hi, welcome!"
};
function Greeting({ store }: NubeSDKState) {
const lang = store.language
const message = translations[lang]
return {message}
}
nube.render("before_main_content", Greeting)
```
--------------------------------
### Implement media queries
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/styling.md
Define responsive styles using standard CSS media queries within styled components.
```typescript
const ResponsiveBox = styled(box)`
background-color: red;
padding: 16px;
@media (max-width: 768px) {
padding: 8px;
background-color: blue;
}
@media (min-width: 1024px) {
padding: 24px;
background-color: green;
}
`;
```
--------------------------------
### Button Component Usage
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/components/button.md
Demonstrates the basic usage of the Button component in both JSX and declarative formats.
```APIDOC
## Button Component Usage
### Description
This section shows how to import and use the Button component.
### Usage Examples
#### JSX
```tsx
import { Button } from "@tiendanube/nube-sdk-jsx";
;
```
#### Declarative
```typescript
import { button } from "@tiendanube/nube-sdk-ui";
button({
children: "Click here",
variant: "primary",
onClick: () => {},
});
```
```
--------------------------------
### Initialize Textarea Component
Source: https://github.com/tiendanube/nube-sdk/blob/main/packages/doc/docs/components/textarea.md
Basic implementation of the Textarea component using JSX or declarative syntax.
```tsx
import { Textarea } from "@tiendanube/nube-sdk-jsx";