# Safe Invoice Serial Counter API - TypeScript Examples

This document provides TypeScript usage examples for the Safe Invoice Serial Counter API API using the Repzo SDK.

## Installation

```bash
npm install repzo
```

## Basic Setup

```typescript
import { Service } from "repzo";

// Initialize with your API key
const apiKey = "your-api-key";
const baseURL = "https://api.repzo.me/v1";
```

## API Usage Examples

### Get all safe invoice serial counters

Retrieve a list of all safe invoice serial counters with optional filtering and pagination

```typescript
// GET /safe-invoice-serial-counter

const params = {
  page: 1, // Page number for pagination

  per_page: 1, // Number of items per page

  sort: "example-value", // Sort field

  company_namespace: null, // Company namespace for filtering

  rep: "example-value", // Filter by representative ID

  warehouse: "example-value", // Filter by warehouse ID

  _id: null, // Filter by ID
};

const result = await fetch(
  `${baseURL}/safe-invoice-serial-counter?${new URLSearchParams(params)}`,
  {
    method: "GET",
    headers: {
      Authorization: `Bearer ${apiKey}`,
      "Content-Type": "application/json",
    },
  },
);

const data = await result.json();
console.log(data);
```

**Expected Response:**

```json
{
  "success": true,
  "data": {
    // Response data structure based on API specification
  }
}
```

---

### Create a new safe invoice serial counter

Create a new safe invoice serial counter entry

```typescript
// POST /safe-invoice-serial-counter

const requestBody = {
  // Add your request body properties here
  // Based on the API schema
};

const result = await fetch(`${baseURL}/safe-invoice-serial-counter`, {
  method: "POST",
  headers: {
    Authorization: `Bearer ${apiKey}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify(requestBody),
});

const data = await result.json();
console.log(data);
```

---

### Get safe invoice serial counter by ID

Retrieve a specific safe invoice serial counter by its ID

```typescript
// GET /safe-invoice-serial-counter/{id}

const params = {
  id: "example-value", // Safe invoice serial counter ID
};

const result = await fetch(
  `${baseURL}/safe-invoice-serial-counter/{id}?${new URLSearchParams(params)}`,
  {
    method: "GET",
    headers: {
      Authorization: `Bearer ${apiKey}`,
      "Content-Type": "application/json",
    },
  },
);

const data = await result.json();
console.log(data);
```

**Expected Response:**

```json
{
  "success": true,
  "data": {
    // Response data structure based on API specification
  }
}
```

---

### Update safe invoice serial counter

Update an existing safe invoice serial counter

```typescript
// PATCH /safe-invoice-serial-counter/{id}

const id = "your-resource-id";
const updatePath = "/safe-invoice-serial-counter/{id}".replace("{id}", id);

const updateData = {
  // Add your update properties here
};

const result = await fetch(`${baseURL}${updatePath}`, {
  method: "PATCH",
  headers: {
    Authorization: `Bearer ${apiKey}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify(updateData),
});

const data = await result.json();
console.log(data);
```

**Expected Response:**

```json
{
  "success": true,
  "data": {
    // Response data structure based on API specification
  }
}
```

---

### Delete safe invoice serial counter

Delete a safe invoice serial counter by ID

```typescript
// DELETE /safe-invoice-serial-counter/{id}

const id = "your-resource-id";
const deletePath = "/safe-invoice-serial-counter/{id}".replace("{id}", id);

const result = await fetch(`${baseURL}${deletePath}`, {
  method: "DELETE",
  headers: {
    Authorization: `Bearer ${apiKey}`,
    "Content-Type": "application/json",
  },
});

const data = await result.json();
console.log(data);
```

**Expected Response:**

```json
{
  "success": true,
  "data": {
    // Response data structure based on API specification
  }
}
```

---

### Get next serial number

Get the next available serial number for invoice generation

```typescript
// POST /safe-invoice-serial-counter/next

const requestBody = {
  // Add your request body properties here
  // Based on the API schema
};

const result = await fetch(`${baseURL}/safe-invoice-serial-counter/next`, {
  method: "POST",
  headers: {
    Authorization: `Bearer ${apiKey}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify(requestBody),
});

const data = await result.json();
console.log(data);
```

**Expected Response:**

```json
{
  "success": true,
  "data": {
    // Response data structure based on API specification
  }
}
```

---

## Using with Repzo SDK Service Types

The Repzo SDK provides TypeScript interfaces for type safety:

```typescript
import { Service } from "repzo";

// Example using SDK types

async function example() {
  // Use the Service.Safeinvoiceserialcounter types for better type safety
  // This provides intellisense and compile-time type checking
}
```

## Error Handling

```typescript
try {
  const result = await fetch(`${baseURL}/endpoint`, {
    method: "GET",
    headers: {
      Authorization: `Bearer ${apiKey}`,
      "Content-Type": "application/json",
    },
  });

  if (!result.ok) {
    throw new Error(`HTTP error! status: ${result.status}`);
  }

  const data = await result.json();

  if (!data.success) {
    throw new Error(data.error || "API request failed");
  }

  return data.data;
} catch (error) {
  console.error("API request failed:", error);
  throw error;
}
```

## Pagination

Many API endpoints support pagination:

```typescript
const params = {
  page: 1,
  per_page: 25,
  // other filters...
};

const result = await fetch(
  `${baseURL}/endpoint?${new URLSearchParams(params)}`,
  {
    method: "GET",
    headers: {
      Authorization: `Bearer ${apiKey}`,
      "Content-Type": "application/json",
    },
  },
);

const data = await result.json();

// Access pagination info
console.log("Total items:", data.paging.total);
console.log("Current page:", data.paging.page);
console.log("Total pages:", data.paging.pages);
```
