@slotchain/sdk 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +167 -0
- package/dist/index.cjs.js +1463 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.d.mts +1149 -0
- package/dist/index.d.ts +1149 -0
- package/dist/index.esm.js +1422 -0
- package/dist/index.esm.js.map +1 -0
- package/package.json +52 -0
- package/src/client.ts +161 -0
- package/src/clients/__tests__/tenant-client.spec.ts +236 -0
- package/src/clients/booking-client.ts +98 -0
- package/src/clients/customer-client.ts +165 -0
- package/src/clients/data-quality-client.ts +93 -0
- package/src/clients/flow-client.ts +107 -0
- package/src/clients/notification-client.ts +108 -0
- package/src/clients/register-client.ts +19 -0
- package/src/clients/service-client.ts +215 -0
- package/src/clients/service-item-client.ts +103 -0
- package/src/clients/slot-client.ts +140 -0
- package/src/clients/studio-client.ts +128 -0
- package/src/clients/tenant-client.ts +148 -0
- package/src/errors.ts +41 -0
- package/src/index.ts +296 -0
- package/src/interceptors.ts +81 -0
- package/src/middleware/validateSlotlyRequest.ts +337 -0
- package/src/mocks/mockAdapter.ts +32 -0
- package/src/retry.ts +82 -0
- package/src/types/api.ts +192 -0
package/README.md
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
# @slotly/sdk
|
|
2
|
+
|
|
3
|
+
🧩 **Slotly SDK** — A typed, developer‑friendly client for interacting with the Slotly platform API.
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
- [Overview](#overview)
|
|
7
|
+
- [Installation](#installation)
|
|
8
|
+
- [Quick Start](#quick‑start)
|
|
9
|
+
- [API Client Architecture](#api‑client‑architecture)
|
|
10
|
+
- [Key Concepts](#key‑concepts)
|
|
11
|
+
- [Usage Examples](#usage‑examples)
|
|
12
|
+
- [Middleware & Auth](#middleware‑auth)
|
|
13
|
+
- [Mocking & Testing](#mocking‑testing)
|
|
14
|
+
- [Contributing](#contributing)
|
|
15
|
+
- [License](#license)
|
|
16
|
+
|
|
17
|
+
## Overview
|
|
18
|
+
The Slotly SDK abstracts your HTTP API into typed domain‑clients, handling authentication, retries, error handling, and client‑scoped access. Whether you're building internal admin tools, public booking flows, or partner integrations, the SDK provides a consistent interface.
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
```bash
|
|
22
|
+
npm install @slotly/sdk
|
|
23
|
+
# or
|
|
24
|
+
yarn add @slotly/sdk
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
**Dependencies:**
|
|
28
|
+
- `jwt-decode` (for decoding user token) - included in dependencies
|
|
29
|
+
- `axios` (HTTP client with custom retry logic)
|
|
30
|
+
- Custom exponential backoff retry with jitter
|
|
31
|
+
|
|
32
|
+
**Peer Dependencies** (install at least one based on your environment):
|
|
33
|
+
- `@clerk/clerk-sdk-node` - For server-side usage
|
|
34
|
+
- `@clerk/nextjs` - For Next.js client-side usage
|
|
35
|
+
- `@clerk/clerk-react` - For React client-side usage
|
|
36
|
+
- `next` - For Next.js middleware (required if using middleware)
|
|
37
|
+
|
|
38
|
+
## Quick Start
|
|
39
|
+
```ts
|
|
40
|
+
import { useSlotly } from '@slotly/sdk';
|
|
41
|
+
|
|
42
|
+
const slotly = useSlotly({
|
|
43
|
+
getClientKey: async () => process.env.SLOTLY_API_KEY!,
|
|
44
|
+
getUserToken: async () => /* optional: return user token or null */
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const tenant = await slotly.tenant.getBySlug('example‑tenant');
|
|
48
|
+
console.log(tenant.data);
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## API Client Architecture
|
|
52
|
+
- Single entry point → `useSlotly()`
|
|
53
|
+
- Axios instance under the hood with:
|
|
54
|
+
- Base URL config
|
|
55
|
+
- Request/response interceptors (logging, error shaping)
|
|
56
|
+
- Exponential back‑off retry logic with jitter
|
|
57
|
+
- Domain‑specific clients exposed via `slotly.tenant`, `slotly.booking`, etc.
|
|
58
|
+
- Strong types for all requests and responses (`ApiResponse<T>`, domain types)
|
|
59
|
+
- Client isolation per instantiation ensures SSR‑safety (supports both client‑side and server‑side usage)
|
|
60
|
+
|
|
61
|
+
## Domain‑Specific Clients
|
|
62
|
+
The SDK provides typed clients for each domain:
|
|
63
|
+
|
|
64
|
+
- **TenantClient** - Tenant configuration and management
|
|
65
|
+
- **BookingClient** - Booking CRUD operations
|
|
66
|
+
- **ServiceClient** - Service management (listServices, create, update, delete, bulkCreate)
|
|
67
|
+
- **SlotClient** - Time slot management and availability
|
|
68
|
+
- **CustomerClient** - Customer management and lookup
|
|
69
|
+
- **FlowClient** - Booking flow configuration and execution
|
|
70
|
+
- **StudioClient** - Studio CRUD and data quality operations
|
|
71
|
+
- **NotificationClient** - Notification management and sending
|
|
72
|
+
- **DataQualityClient** - Data quality issue tracking and resolution
|
|
73
|
+
|
|
74
|
+
**Note:** RegisterClient (studio‑specific booking register operations) will be available in the separate `@slotly/studio-sdk`.
|
|
75
|
+
|
|
76
|
+
## Key Concepts
|
|
77
|
+
### **Client Key (x‑slotly‑api‑key)**
|
|
78
|
+
Scope‑based key used to identify the _calling client_ or application (e.g., booking site, admin UI). Carries permissions that scope API access to specific tenants and operations.
|
|
79
|
+
### **User Token (Authorization: Bearer …)**
|
|
80
|
+
Optional token that identifies the _end‑user_ (e.g., a clerk user).
|
|
81
|
+
### **Tenant Context**
|
|
82
|
+
Many operations are tenant‑scoped. The SDK enables context propagation so your calls act within the right tenant.
|
|
83
|
+
|
|
84
|
+
## Usage Examples
|
|
85
|
+
### Domain Client Example – Tenant
|
|
86
|
+
```ts
|
|
87
|
+
const result = await slotly.tenant.getBySlug('bright‑accountants');
|
|
88
|
+
if (result.success) {
|
|
89
|
+
console.log('Tenant data:', result.data);
|
|
90
|
+
} else {
|
|
91
|
+
console.error('Error:', result.error);
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Booking Flow for Public Site
|
|
96
|
+
```ts
|
|
97
|
+
const slotly = useSlotly({
|
|
98
|
+
getClientKey: async () => process.env.SLOTLY_PAGES_KEY!,
|
|
99
|
+
getUserToken: async () => null
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
const booking = await slotly.booking.create({
|
|
103
|
+
slot_id: 'xyz123',
|
|
104
|
+
customer_info: { name: 'Jane Doe', email: 'jane@example.com' },
|
|
105
|
+
booking_data: { services: ['s1'], total: 100 }
|
|
106
|
+
});
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Service Management
|
|
110
|
+
```ts
|
|
111
|
+
const services = await slotly.service.listServices('bright-accountants');
|
|
112
|
+
if (services.success) {
|
|
113
|
+
console.log('Available services:', services.data);
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## Middleware & Auth
|
|
118
|
+
For your API routes, use the `validateSlotlyRequest()` middleware to enforce both client and optional user auth:
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
import { validateSlotlyRequest } from '@slotly/sdk/middleware/validateSlotlyRequest';
|
|
122
|
+
|
|
123
|
+
export default validateSlotlyRequest(async (req, res) => {
|
|
124
|
+
const { clientKey, userId, tenantId } = req.slotlyContext;
|
|
125
|
+
// … your logic here …
|
|
126
|
+
});
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
**Features of middleware:**
|
|
130
|
+
- Validates `x‑slotly‑api‑key` (required)
|
|
131
|
+
- Decodes optional `Authorization` bearer token (JWT)
|
|
132
|
+
- Throws `SlotlyAuthError` if validation fails
|
|
133
|
+
- Injects `slotlyContext` into request object (clientKey, userId, tenantId, permissions)
|
|
134
|
+
- Mock allowlist for development – replace with production key store
|
|
135
|
+
|
|
136
|
+
## Mocking & Testing
|
|
137
|
+
- A `mockAdapter.ts` is included to enable mocking Axios responses in tests
|
|
138
|
+
- Configure mocks to simulate API behaviour without needing live backend
|
|
139
|
+
- Use domain‑clients in unit tests with predictable responses
|
|
140
|
+
|
|
141
|
+
## Bundle & Publishing
|
|
142
|
+
The SDK is bundled using `tsup` for optimal performance and compatibility:
|
|
143
|
+
- Generates both ESM (`dist/index.esm.js`) and CJS (`dist/index.cjs.js`) formats
|
|
144
|
+
- Includes TypeScript declaration files (`.d.ts`) for type support
|
|
145
|
+
- Source maps included for debugging
|
|
146
|
+
- Dependencies are marked as external to minimize bundle size
|
|
147
|
+
- Target: ES2020 for broad runtime compatibility
|
|
148
|
+
|
|
149
|
+
**Known Limitations:**
|
|
150
|
+
- No asset bundling currently supported (e.g., CSS, images)
|
|
151
|
+
- If you require custom assets or legacy browser support, switching to Rollup with plugins is supported
|
|
152
|
+
|
|
153
|
+
**Build Commands:**
|
|
154
|
+
```bash
|
|
155
|
+
npm run build # Production build
|
|
156
|
+
npm run build:watch # Watch mode for development
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## Contributing
|
|
160
|
+
We welcome contributions!
|
|
161
|
+
- Fork the repo
|
|
162
|
+
- Run `npm install` and tests (`npm test`)
|
|
163
|
+
- Submit PRs with clear changelog entries
|
|
164
|
+
- Please keep README, JSDoc, and type definitions up to date
|
|
165
|
+
|
|
166
|
+
## License
|
|
167
|
+
[MIT License](./LICENSE)
|