lightspeed-retail-sdk 3.0.0 → 3.0.2
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 +234 -49
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
A JavaScript SDK for interacting with the Lightspeed Retail API. This SDK provides a convenient way to access Lightspeed Retail's functionalities, including customer, item, order management, and more.
|
|
4
4
|
|
|
5
|
-
**Current Version: 3.0.
|
|
5
|
+
**Current Version: 3.0.2** - Updated with new OAuth system support and enhanced token management.
|
|
6
6
|
|
|
7
7
|
## 🚨 Important Update - New OAuth System
|
|
8
8
|
|
|
@@ -104,85 +104,216 @@ const api = new LightspeedRetailSDK({
|
|
|
104
104
|
export default api;
|
|
105
105
|
```
|
|
106
106
|
|
|
107
|
-
####
|
|
107
|
+
#### Database Storage (Built-in Base Class)
|
|
108
108
|
|
|
109
|
-
|
|
110
|
-
import LightspeedRetailSDK from "lightspeed-retail-sdk";
|
|
109
|
+
The SDK provides a `DatabaseTokenStorage` base class that you can extend:
|
|
111
110
|
|
|
112
|
-
|
|
113
|
-
|
|
111
|
+
```javascript
|
|
112
|
+
import LightspeedRetailSDK, {
|
|
113
|
+
DatabaseTokenStorage,
|
|
114
|
+
} from "lightspeed-retail-sdk";
|
|
115
|
+
import mysql from "mysql2/promise";
|
|
116
|
+
|
|
117
|
+
class MySQLTokenStorage extends DatabaseTokenStorage {
|
|
118
|
+
constructor(connectionConfig, userId) {
|
|
119
|
+
super();
|
|
120
|
+
this.config = connectionConfig;
|
|
114
121
|
this.userId = userId;
|
|
115
122
|
}
|
|
116
123
|
|
|
117
124
|
async getTokens() {
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
125
|
+
const connection = await mysql.createConnection(this.config);
|
|
126
|
+
try {
|
|
127
|
+
const [rows] = await connection.execute(
|
|
128
|
+
"SELECT access_token, refresh_token, expires_at FROM user_tokens WHERE user_id = ?",
|
|
129
|
+
[this.userId]
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
if (rows.length === 0) {
|
|
133
|
+
return { access_token: null, refresh_token: null, expires_at: null };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return {
|
|
137
|
+
access_token: rows[0].access_token,
|
|
138
|
+
refresh_token: rows[0].refresh_token,
|
|
139
|
+
expires_at: rows[0].expires_at,
|
|
140
|
+
};
|
|
141
|
+
} finally {
|
|
142
|
+
await connection.end();
|
|
143
|
+
}
|
|
124
144
|
}
|
|
125
145
|
|
|
126
146
|
async setTokens(tokens) {
|
|
127
|
-
await
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
147
|
+
const connection = await mysql.createConnection(this.config);
|
|
148
|
+
try {
|
|
149
|
+
await connection.execute(
|
|
150
|
+
`INSERT INTO user_tokens (user_id, access_token, refresh_token, expires_at, updated_at)
|
|
151
|
+
VALUES (?, ?, ?, ?, NOW())
|
|
152
|
+
ON DUPLICATE KEY UPDATE
|
|
153
|
+
access_token = VALUES(access_token),
|
|
154
|
+
refresh_token = VALUES(refresh_token),
|
|
155
|
+
expires_at = VALUES(expires_at),
|
|
156
|
+
updated_at = NOW()`,
|
|
157
|
+
[
|
|
158
|
+
this.userId,
|
|
159
|
+
tokens.access_token,
|
|
160
|
+
tokens.refresh_token,
|
|
161
|
+
tokens.expires_at,
|
|
162
|
+
]
|
|
163
|
+
);
|
|
164
|
+
} finally {
|
|
165
|
+
await connection.end();
|
|
166
|
+
}
|
|
132
167
|
}
|
|
133
168
|
}
|
|
134
169
|
|
|
170
|
+
// Usage
|
|
171
|
+
const dbConfig = {
|
|
172
|
+
host: "localhost",
|
|
173
|
+
user: "your_user",
|
|
174
|
+
password: "your_password",
|
|
175
|
+
database: "your_database",
|
|
176
|
+
};
|
|
177
|
+
|
|
135
178
|
const api = new LightspeedRetailSDK({
|
|
136
179
|
accountID: "Your Account No.",
|
|
137
180
|
clientID: "Your client ID.",
|
|
138
181
|
clientSecret: "Your client secret.",
|
|
139
182
|
refreshToken: "Your initial refresh token.",
|
|
140
|
-
tokenStorage: new
|
|
183
|
+
tokenStorage: new MySQLTokenStorage(dbConfig, "user123"),
|
|
141
184
|
});
|
|
142
185
|
```
|
|
143
186
|
|
|
144
|
-
|
|
187
|
+
#### PostgreSQL Example
|
|
145
188
|
|
|
146
189
|
```javascript
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
console.log(item);
|
|
190
|
+
import { DatabaseTokenStorage } from "lightspeed-retail-sdk";
|
|
191
|
+
import pg from "pg";
|
|
150
192
|
|
|
151
|
-
|
|
152
|
-
|
|
193
|
+
class PostgreSQLTokenStorage extends DatabaseTokenStorage {
|
|
194
|
+
constructor(connectionString, userId) {
|
|
195
|
+
super();
|
|
196
|
+
this.connectionString = connectionString;
|
|
197
|
+
this.userId = userId;
|
|
198
|
+
}
|
|
153
199
|
|
|
154
|
-
|
|
155
|
-
const
|
|
200
|
+
async getTokens() {
|
|
201
|
+
const client = new pg.Client(this.connectionString);
|
|
202
|
+
await client.connect();
|
|
203
|
+
|
|
204
|
+
try {
|
|
205
|
+
const result = await client.query(
|
|
206
|
+
"SELECT access_token, refresh_token, expires_at FROM user_tokens WHERE user_id = $1",
|
|
207
|
+
[this.userId]
|
|
208
|
+
);
|
|
209
|
+
|
|
210
|
+
if (result.rows.length === 0) {
|
|
211
|
+
return { access_token: null, refresh_token: null, expires_at: null };
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const row = result.rows[0];
|
|
215
|
+
return {
|
|
216
|
+
access_token: row.access_token,
|
|
217
|
+
refresh_token: row.refresh_token,
|
|
218
|
+
expires_at: row.expires_at,
|
|
219
|
+
};
|
|
220
|
+
} finally {
|
|
221
|
+
await client.end();
|
|
222
|
+
}
|
|
223
|
+
}
|
|
156
224
|
|
|
157
|
-
|
|
158
|
-
const
|
|
225
|
+
async setTokens(tokens) {
|
|
226
|
+
const client = new pg.Client(this.connectionString);
|
|
227
|
+
await client.connect();
|
|
228
|
+
|
|
229
|
+
try {
|
|
230
|
+
await client.query(
|
|
231
|
+
`INSERT INTO user_tokens (user_id, access_token, refresh_token, expires_at, updated_at)
|
|
232
|
+
VALUES ($1, $2, $3, $4, NOW())
|
|
233
|
+
ON CONFLICT (user_id) DO UPDATE SET
|
|
234
|
+
access_token = EXCLUDED.access_token,
|
|
235
|
+
refresh_token = EXCLUDED.refresh_token,
|
|
236
|
+
expires_at = EXCLUDED.expires_at,
|
|
237
|
+
updated_at = NOW()`,
|
|
238
|
+
[
|
|
239
|
+
this.userId,
|
|
240
|
+
tokens.access_token,
|
|
241
|
+
tokens.refresh_token,
|
|
242
|
+
tokens.expires_at,
|
|
243
|
+
]
|
|
244
|
+
);
|
|
245
|
+
} finally {
|
|
246
|
+
await client.end();
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
```
|
|
159
251
|
|
|
160
|
-
|
|
161
|
-
const lowStockItems = await api.getItemsWithLowStock(10, '["Category"]');
|
|
252
|
+
#### MongoDB Example
|
|
162
253
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
254
|
+
```javascript
|
|
255
|
+
import { DatabaseTokenStorage } from "lightspeed-retail-sdk";
|
|
256
|
+
import { MongoClient } from "mongodb";
|
|
257
|
+
|
|
258
|
+
class MongoTokenStorage extends DatabaseTokenStorage {
|
|
259
|
+
constructor(connectionString, databaseName, userId) {
|
|
260
|
+
super();
|
|
261
|
+
this.connectionString = connectionString;
|
|
262
|
+
this.databaseName = databaseName;
|
|
263
|
+
this.userId = userId;
|
|
264
|
+
}
|
|
169
265
|
|
|
170
|
-
|
|
171
|
-
const
|
|
172
|
-
|
|
266
|
+
async getTokens() {
|
|
267
|
+
const client = new MongoClient(this.connectionString);
|
|
268
|
+
await client.connect();
|
|
269
|
+
|
|
270
|
+
try {
|
|
271
|
+
const db = client.db(this.databaseName);
|
|
272
|
+
const collection = db.collection("user_tokens");
|
|
273
|
+
|
|
274
|
+
const doc = await collection.findOne({ userId: this.userId });
|
|
275
|
+
|
|
276
|
+
if (!doc) {
|
|
277
|
+
return { access_token: null, refresh_token: null, expires_at: null };
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
return {
|
|
281
|
+
access_token: doc.access_token,
|
|
282
|
+
refresh_token: doc.refresh_token,
|
|
283
|
+
expires_at: doc.expires_at,
|
|
284
|
+
};
|
|
285
|
+
} finally {
|
|
286
|
+
await client.close();
|
|
287
|
+
}
|
|
288
|
+
}
|
|
173
289
|
|
|
174
|
-
|
|
175
|
-
const
|
|
176
|
-
|
|
290
|
+
async setTokens(tokens) {
|
|
291
|
+
const client = new MongoClient(this.connectionString);
|
|
292
|
+
await client.connect();
|
|
293
|
+
|
|
294
|
+
try {
|
|
295
|
+
const db = client.db(this.databaseName);
|
|
296
|
+
const collection = db.collection("user_tokens");
|
|
297
|
+
|
|
298
|
+
await collection.updateOne(
|
|
299
|
+
{ userId: this.userId },
|
|
300
|
+
{
|
|
301
|
+
$set: {
|
|
302
|
+
access_token: tokens.access_token,
|
|
303
|
+
refresh_token: tokens.refresh_token,
|
|
304
|
+
expires_at: tokens.expires_at,
|
|
305
|
+
updated_at: new Date(),
|
|
306
|
+
},
|
|
307
|
+
},
|
|
308
|
+
{ upsert: true }
|
|
309
|
+
);
|
|
310
|
+
} finally {
|
|
311
|
+
await client.close();
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
177
315
|
```
|
|
178
316
|
|
|
179
|
-
## Token Storage Options
|
|
180
|
-
|
|
181
|
-
### Built-in Storage Classes
|
|
182
|
-
|
|
183
|
-
1. **InMemoryTokenStorage** (default) - Stores tokens in memory only
|
|
184
|
-
2. **FileTokenStorage** - Stores tokens in a JSON file
|
|
185
|
-
|
|
186
317
|
### Custom Storage Interface
|
|
187
318
|
|
|
188
319
|
Implement your own storage by creating a class with these methods:
|
|
@@ -191,6 +322,7 @@ Implement your own storage by creating a class with these methods:
|
|
|
191
322
|
class CustomTokenStorage {
|
|
192
323
|
async getTokens() {
|
|
193
324
|
// Return an object with: { access_token, refresh_token, expires_at }
|
|
325
|
+
// Return null values if no tokens are stored
|
|
194
326
|
}
|
|
195
327
|
|
|
196
328
|
async setTokens(tokens) {
|
|
@@ -199,6 +331,59 @@ class CustomTokenStorage {
|
|
|
199
331
|
}
|
|
200
332
|
```
|
|
201
333
|
|
|
334
|
+
### Database Schema Examples
|
|
335
|
+
|
|
336
|
+
#### MySQL/PostgreSQL Schema
|
|
337
|
+
|
|
338
|
+
```sql
|
|
339
|
+
CREATE TABLE user_tokens (
|
|
340
|
+
user_id VARCHAR(255) PRIMARY KEY,
|
|
341
|
+
access_token TEXT NOT NULL,
|
|
342
|
+
refresh_token TEXT NOT NULL,
|
|
343
|
+
expires_at TIMESTAMP NOT NULL,
|
|
344
|
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
345
|
+
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
346
|
+
);
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
#### MongoDB Schema
|
|
350
|
+
|
|
351
|
+
```javascript
|
|
352
|
+
// No strict schema required, but documents will look like:
|
|
353
|
+
{
|
|
354
|
+
_id: ObjectId("..."),
|
|
355
|
+
userId: "user123",
|
|
356
|
+
access_token: "eyJ0eXAiOiJKV1Q...",
|
|
357
|
+
refresh_token: "def5020058aac34d...",
|
|
358
|
+
expires_at: "2025-07-02T16:09:42.069Z",
|
|
359
|
+
created_at: ISODate("2025-07-02T15:09:42.069Z"),
|
|
360
|
+
updated_at: ISODate("2025-07-02T15:09:42.069Z")
|
|
361
|
+
}
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
## CommonJS Usage
|
|
365
|
+
|
|
366
|
+
The SDK supports both ES modules and CommonJS:
|
|
367
|
+
|
|
368
|
+
### ES Modules (Recommended)
|
|
369
|
+
|
|
370
|
+
```javascript
|
|
371
|
+
import LightspeedRetailSDK, {
|
|
372
|
+
FileTokenStorage,
|
|
373
|
+
DatabaseTokenStorage,
|
|
374
|
+
} from "lightspeed-retail-sdk";
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
### CommonJS
|
|
378
|
+
|
|
379
|
+
```javascript
|
|
380
|
+
const LightspeedRetailSDK = require("lightspeed-retail-sdk");
|
|
381
|
+
const {
|
|
382
|
+
FileTokenStorage,
|
|
383
|
+
DatabaseTokenStorage,
|
|
384
|
+
} = require("lightspeed-retail-sdk");
|
|
385
|
+
```
|
|
386
|
+
|
|
202
387
|
## Migration from Previous Versions
|
|
203
388
|
|
|
204
389
|
If you're upgrading from a previous version:
|