mirror of
https://github.com/hanzoai/papers.git
synced 2026-10-22 17:20:17 +00:00
97 lines
1.9 KiB
TeX
97 lines
1.9 KiB
TeX
\section{JavaScript SDK}
|
|
|
|
|
|
\subsection{Package Structure}
|
|
|
|
\begin{lstlisting}[caption=JavaScript SDK Structure]
|
|
hanzo-js/
|
|
src/
|
|
index.ts
|
|
client.ts
|
|
resources/
|
|
products.ts
|
|
orders.ts
|
|
customers.ts
|
|
models/
|
|
product.ts
|
|
order.ts
|
|
errors.ts
|
|
utils/
|
|
pagination.ts
|
|
webhooks.ts
|
|
package.json
|
|
tsconfig.json
|
|
\end{lstlisting}
|
|
|
|
\subsection{Client Implementation}
|
|
|
|
\begin{lstlisting}[language=JavaScript, caption=JavaScript Client]
|
|
import Hanzo from 'hanzo';
|
|
|
|
const hanzo = new Hanzo('sk_live_...');
|
|
|
|
// List products with pagination
|
|
const products = await hanzo.products.list({ limit: 10 });
|
|
for await (const product of products) {
|
|
console.log(product.name);
|
|
}
|
|
|
|
// Create a product
|
|
const product = await hanzo.products.create({
|
|
name: 'Widget',
|
|
price: 1999,
|
|
description: 'A useful widget',
|
|
});
|
|
|
|
// Update with idempotency
|
|
const updated = await hanzo.products.update(product.id, {
|
|
price: 2499,
|
|
}, {
|
|
idempotencyKey: 'update-123',
|
|
});
|
|
\end{lstlisting}
|
|
|
|
\subsection{TypeScript Types}
|
|
|
|
\begin{lstlisting}[language=JavaScript, caption=Generated Types]
|
|
interface Product {
|
|
id: string;
|
|
name: string;
|
|
price: number;
|
|
description: string | null;
|
|
variants: Variant[];
|
|
createdAt: Date;
|
|
updatedAt: Date;
|
|
}
|
|
|
|
interface ProductCreateParams {
|
|
name: string;
|
|
price: number;
|
|
description?: string;
|
|
}
|
|
|
|
interface ProductListParams {
|
|
limit?: number;
|
|
startingAfter?: string;
|
|
endingBefore?: string;
|
|
}
|
|
\end{lstlisting}
|
|
|
|
\subsection{Error Handling}
|
|
|
|
\begin{lstlisting}[language=JavaScript, caption=JavaScript Error Handling]
|
|
import { HanzoError, InvalidRequestError } from 'hanzo';
|
|
|
|
try {
|
|
await hanzo.products.create({ price: -100 });
|
|
} catch (error) {
|
|
if (error instanceof InvalidRequestError) {
|
|
console.error(`Invalid parameter: ${error.param}`);
|
|
console.error(`Message: ${error.message}`);
|
|
} else if (error instanceof HanzoError) {
|
|
console.error(`API error: ${error.code}`);
|
|
}
|
|
}
|
|
\end{lstlisting}
|
|
|