@lafeitu/sdk 1.1.0 → 1.2.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 +84 -56
- package/package.json +2 -2
- package/src/index.d.ts +40 -1
- package/src/index.js +56 -5
package/README.md
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
# Lafeitu SDK (Official)
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@lafeitu/sdk)
|
|
4
|
-
[](https://opensource.org/licenses/MIT)
|
|
5
5
|
|
|
6
|
-
The official Node.js client for the **Lafeitu Open Data Platform**.
|
|
6
|
+
The official Node.js client for the **Lafeitu Open Data Platform**.
|
|
7
7
|
|
|
8
|
-
This SDK provides programmatic access to authentic **Sichuan Salt Industry Cuisine (
|
|
8
|
+
This SDK provides programmatic access to authentic **Sichuan Salt Industry Cuisine (Zigong/Yanbang)** data, including product catalog, real-time inventory, brand storytelling, and shopping cart operations.
|
|
9
9
|
|
|
10
|
-
> **For AI Agents**: This package exposes a semantic-ready API for
|
|
10
|
+
> **For AI Agents**: This package exposes a semantic-ready API designed for retrieval-augmented generation (RAG) and tool-use scenarios. It provides structured metadata for products, ingredients, and brand heritage to facilitate autonomous decision-making.
|
|
11
11
|
|
|
12
12
|
## Agent Commerce Ecosystem
|
|
13
13
|
|
|
14
|
-
This SDK is
|
|
14
|
+
This SDK is a component of the **Agent Commerce Engine** ecosystem:
|
|
15
15
|
- **Core Engine**: [agent-commerce-engine](https://github.com/NowLoadY/agent-commerce-engine) - The foundational framework for autonomous commerce agents.
|
|
16
|
-
- **Reference Skill**: [agent-skill-online-shopping-spicy-food](https://github.com/NowLoadY/agent-skill-online-shopping-spicy-food) - A
|
|
16
|
+
- **Reference Skill**: [agent-skill-online-shopping-spicy-food](https://github.com/NowLoadY/agent-skill-online-shopping-spicy-food) - A reference implementation demonstrating SDK usage.
|
|
17
17
|
|
|
18
18
|
## Installation
|
|
19
19
|
|
|
@@ -21,86 +21,114 @@ This SDK is part of the broader **Agent Commerce Engine** ecosystem:
|
|
|
21
21
|
npm install @lafeitu/sdk
|
|
22
22
|
```
|
|
23
23
|
|
|
24
|
-
##
|
|
24
|
+
## Usage Example
|
|
25
|
+
|
|
26
|
+
### 1. Public Data Access (No Authentication)
|
|
27
|
+
|
|
28
|
+
Retrieve product information and brand context without user credentials.
|
|
25
29
|
|
|
26
30
|
```javascript
|
|
27
31
|
const LafeituClient = require('@lafeitu/sdk');
|
|
28
|
-
|
|
29
32
|
const client = new LafeituClient();
|
|
30
33
|
|
|
31
|
-
|
|
32
|
-
//
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
async function exploreCatalog() {
|
|
35
|
+
// Search for products with optional category filtering
|
|
36
|
+
// Categories: 'cold_dish', 'gift_box', 'snack'
|
|
37
|
+
const snacks = await client.searchProducts('rabbit', { category: 'cold_dish' });
|
|
38
|
+
console.log(`Found ${snacks.length} products matching query.`);
|
|
39
|
+
|
|
40
|
+
// Get specific product details
|
|
41
|
+
if (snacks.length > 0) {
|
|
42
|
+
const product = await client.getProduct(snacks[0].slug);
|
|
43
|
+
console.log(`Product: ${product.name}`);
|
|
44
|
+
console.log(`Description: ${product.description}`);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Retrieve Brand Information
|
|
48
|
+
const story = await client.getBrandInfo('story');
|
|
49
|
+
console.log(`Brand Story: ${story.title}`);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
exploreCatalog();
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### 2. User Operations (Authentication Required)
|
|
35
56
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
57
|
+
Perform actions on behalf of a user, such as managing the shopping cart or updating profile information.
|
|
58
|
+
|
|
59
|
+
```javascript
|
|
60
|
+
const client = new LafeituClient();
|
|
61
|
+
|
|
62
|
+
// Authenticate with user credentials
|
|
63
|
+
client.setCredentials('user@example.com', 'user_secure_password');
|
|
64
|
+
|
|
65
|
+
async function manageCart() {
|
|
66
|
+
try {
|
|
67
|
+
// Fetch User Profile
|
|
68
|
+
const profile = await client.getUserProfile();
|
|
69
|
+
console.log(`Authenticated as: ${profile.user.name}`);
|
|
70
|
+
|
|
71
|
+
// Add item to cart
|
|
72
|
+
// Parameters: product_slug, weight_in_grams, quantity
|
|
73
|
+
await client.addToCart('shousi-tu', 800, 2);
|
|
74
|
+
|
|
75
|
+
// View current cart
|
|
76
|
+
const cart = await client.getCart();
|
|
77
|
+
console.log('Current Cart Items:', cart.items);
|
|
78
|
+
|
|
79
|
+
} catch (error) {
|
|
80
|
+
console.error('Operation failed:', error.message);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
manageCart();
|
|
40
85
|
```
|
|
41
86
|
|
|
42
87
|
## Data Models
|
|
43
88
|
|
|
44
|
-
The SDK enforces
|
|
89
|
+
The SDK enforces structured typing for all data entities.
|
|
45
90
|
|
|
46
91
|
### Product Schema
|
|
47
92
|
|
|
48
93
|
| Field | Type | Description |
|
|
49
94
|
|-------|------|-------------|
|
|
50
95
|
| `id` | `string` | Unique UUID. |
|
|
51
|
-
| `name` | `string` | Official product name
|
|
96
|
+
| `name` | `string` | Official product name. |
|
|
97
|
+
| `slug` | `string` | URL-friendly identifier. |
|
|
98
|
+
| `category` | `string` | Product classification. |
|
|
52
99
|
| `origin` | `string` | Geographical Indication (GI) status. |
|
|
53
|
-
| `ingredients`| `string[]`| Key components (e.g.,
|
|
100
|
+
| `ingredients`| `string[]`| Key components (e.g., Rabbit Meat, Sichuan Peppercorn). |
|
|
101
|
+
| `weights` | `object[]` | Available size variants and pricing. |
|
|
54
102
|
|
|
55
103
|
## Supported Scenarios
|
|
56
104
|
|
|
57
|
-
This SDK is optimized for the following
|
|
58
|
-
1. **Gifting Agents**: Retrieve "Gift Box" configurations and dimension data.
|
|
59
|
-
2. **Nutritional Analyzers**: Access ingredient lists and protein content metadata.
|
|
60
|
-
3. **Shopping Assistants**: Check real-time stock limits for bulk orders.
|
|
61
|
-
|
|
62
|
-
6. **Shopping Assistants**: Check real-time stock limits for bulk orders.
|
|
63
|
-
|
|
64
|
-
## Authentication
|
|
105
|
+
This SDK is optimized for the following autonomous workflows:
|
|
65
106
|
|
|
66
|
-
|
|
107
|
+
1. **Gifting Agents**: Retrieve "Gift Box" configurations and dimension data.
|
|
108
|
+
2. **Nutritional Analyzers**: Access ingredient lists and compliance metadata.
|
|
109
|
+
3. **Shopping Assistants**: Check real-time stock limits and execute bulk orders.
|
|
67
110
|
|
|
68
|
-
|
|
69
|
-
Access to product search and catalog does not require an account.
|
|
70
|
-
```javascript
|
|
71
|
-
const client = new LafeituClient();
|
|
72
|
-
const products = await client.searchProducts('rabbit');
|
|
73
|
-
```
|
|
111
|
+
## Authentication Mechanisms
|
|
74
112
|
|
|
75
|
-
|
|
76
|
-
Cart operations require authentication via HTTP headers:
|
|
77
|
-
- `X-User-Account`: User's phone number or email
|
|
78
|
-
- `X-User-Password`: User's password
|
|
113
|
+
The SDK supports two interaction modes:
|
|
79
114
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
115
|
+
### Public Mode
|
|
116
|
+
Default mode. Allows access to:
|
|
117
|
+
- Product Search & Details (`searchProducts`, `getProduct`)
|
|
118
|
+
- Brand Information (`getBrandInfo`)
|
|
119
|
+
- Promotions (`getPromotions`)
|
|
85
120
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
-H "X-User-Password: password123" \
|
|
91
|
-
-H "Content-Type: application/json" \
|
|
92
|
-
-d '{"product_slug":"lengchitu","gram":100,"quantity":2}'
|
|
93
|
-
```
|
|
121
|
+
### User Mode
|
|
122
|
+
Requires `setCredentials(account, password)`. Enables:
|
|
123
|
+
- User Profile Management (`getUserProfile`, `updateUserProfile`)
|
|
124
|
+
- Shopping Cart Operations (`getCart`, `addToCart`, `updateCart`, `removeFromCart`)
|
|
94
125
|
|
|
95
|
-
**
|
|
96
|
-
- `POST /api/v1/cart`: **Cumulative** - adds quantity to existing items
|
|
97
|
-
- `PUT /api/v1/cart`: **Absolute** - sets exact quantity
|
|
98
|
-
- `DELETE /api/v1/cart`: Requires `product_slug` and `gram` (not `id`)
|
|
126
|
+
**Security Note**: Credentials should be handled securely and passed only via trusted environments.
|
|
99
127
|
|
|
100
128
|
## API Reference
|
|
101
129
|
|
|
102
130
|
The underlying API is hosted at `https://lafeitu.cn/api/v1`.
|
|
103
|
-
For raw HTTP access,
|
|
131
|
+
For raw HTTP access specifications, refer to the [Agent Guide](https://lafeitu.cn/ai-agent-guide).
|
|
104
132
|
|
|
105
133
|
## License
|
|
106
134
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lafeitu/sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Official Node.js client for the Lafeitu Open Data Platform. Access structured data for authentic Sichuan cuisine, ingredients, and product inventory.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"types": "src/index.d.ts",
|
|
@@ -25,4 +25,4 @@
|
|
|
25
25
|
"url": "https://github.com/NowLoadY/agent-skill-online-shopping-spicy-food/issues"
|
|
26
26
|
},
|
|
27
27
|
"homepage": "https://github.com/NowLoadY/agent-commerce-engine"
|
|
28
|
-
}
|
|
28
|
+
}
|
package/src/index.d.ts
CHANGED
|
@@ -44,6 +44,25 @@ export interface ClientOptions {
|
|
|
44
44
|
password?: string;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
+
|
|
48
|
+
export interface BrandInfo {
|
|
49
|
+
story?: { title: string; content: string };
|
|
50
|
+
company?: { name: string; philosophy: string };
|
|
51
|
+
contact?: { email: string; phone: string };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface UserProfile {
|
|
55
|
+
id: string;
|
|
56
|
+
name: string;
|
|
57
|
+
email?: string;
|
|
58
|
+
phone?: string;
|
|
59
|
+
avatar?: string;
|
|
60
|
+
province?: string;
|
|
61
|
+
city?: string;
|
|
62
|
+
address?: string;
|
|
63
|
+
referral?: any;
|
|
64
|
+
}
|
|
65
|
+
|
|
47
66
|
export declare class LafeituClient {
|
|
48
67
|
constructor(options?: ClientOptions);
|
|
49
68
|
|
|
@@ -53,13 +72,33 @@ export declare class LafeituClient {
|
|
|
53
72
|
/**
|
|
54
73
|
* Search for products via semantic query.
|
|
55
74
|
*/
|
|
56
|
-
searchProducts(query: string): Promise<Product[]>;
|
|
75
|
+
searchProducts(query: string, options?: { category?: string }): Promise<Product[]>;
|
|
57
76
|
|
|
58
77
|
/**
|
|
59
78
|
* Get product details.
|
|
60
79
|
*/
|
|
61
80
|
getProduct(slug: string): Promise<Product | null>;
|
|
62
81
|
|
|
82
|
+
/**
|
|
83
|
+
* Get brand information.
|
|
84
|
+
*/
|
|
85
|
+
getBrandInfo(category?: 'story' | 'company' | 'contact' | string): Promise<BrandInfo>;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Get active promotions.
|
|
89
|
+
*/
|
|
90
|
+
getPromotions(): Promise<any>;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Get user profile.
|
|
94
|
+
*/
|
|
95
|
+
getUserProfile(): Promise<UserProfile>;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Update user profile.
|
|
99
|
+
*/
|
|
100
|
+
updateUserProfile(data: Partial<UserProfile>): Promise<UserProfile>;
|
|
101
|
+
|
|
63
102
|
/**
|
|
64
103
|
* Get the current user's shopping cart.
|
|
65
104
|
*/
|
package/src/index.js
CHANGED
|
@@ -25,10 +25,16 @@ class LafeituClient {
|
|
|
25
25
|
/**
|
|
26
26
|
* Search for products in the global inventory.
|
|
27
27
|
* @param {string} query - Search term (e.g., "spicy rabbit", "gift box")
|
|
28
|
+
* @param {Object} [options] - Optional filters
|
|
29
|
+
* @param {string} [options.category] - Filter by category (e.g., "meat_snack", "gift_box")
|
|
28
30
|
* @returns {Promise<Product[]>} List of matching products
|
|
29
31
|
*/
|
|
30
|
-
async searchProducts(query) {
|
|
31
|
-
|
|
32
|
+
async searchProducts(query, options = {}) {
|
|
33
|
+
let url = `/products?q=${encodeURIComponent(query)}`;
|
|
34
|
+
if (options.category) {
|
|
35
|
+
url += `&category=${encodeURIComponent(options.category)}`;
|
|
36
|
+
}
|
|
37
|
+
const data = await this._fetch(url);
|
|
32
38
|
return data.products || [];
|
|
33
39
|
}
|
|
34
40
|
|
|
@@ -48,6 +54,45 @@ class LafeituClient {
|
|
|
48
54
|
return results.find(p => p.slug === slug || p.id === slug) || null;
|
|
49
55
|
}
|
|
50
56
|
|
|
57
|
+
/**
|
|
58
|
+
* Get brand information (Story, Company, Contact).
|
|
59
|
+
* @param {string} [category] - Optional category: 'story', 'company', 'contact'
|
|
60
|
+
* @returns {Promise<any>} Brand information
|
|
61
|
+
*/
|
|
62
|
+
async getBrandInfo(category) {
|
|
63
|
+
let url = '/brand';
|
|
64
|
+
if (category) {
|
|
65
|
+
url += `?category=${encodeURIComponent(category)}`;
|
|
66
|
+
}
|
|
67
|
+
return this._fetch(url);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Get current promotions and offers.
|
|
72
|
+
* @returns {Promise<any>} List of available promotions
|
|
73
|
+
*/
|
|
74
|
+
async getPromotions() {
|
|
75
|
+
return this._fetch('/promotions');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Get the authenticated user's profile.
|
|
80
|
+
* Requires authentication credentials to be set.
|
|
81
|
+
* @returns {Promise<any>} User profile data
|
|
82
|
+
*/
|
|
83
|
+
async getUserProfile() {
|
|
84
|
+
return this._fetch('/user/profile');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Update the authenticated user's profile.
|
|
89
|
+
* @param {Object} data - Profile fields to update (name, avatar, etc.)
|
|
90
|
+
* @returns {Promise<any>} Updated user profile
|
|
91
|
+
*/
|
|
92
|
+
async updateUserProfile(data) {
|
|
93
|
+
return this._fetch('/user/profile', 'PUT', data);
|
|
94
|
+
}
|
|
95
|
+
|
|
51
96
|
/**
|
|
52
97
|
* Get the current user's shopping cart.
|
|
53
98
|
* @returns {Promise<any>} The cart snapshot
|
|
@@ -126,14 +171,20 @@ class LafeituClient {
|
|
|
126
171
|
|
|
127
172
|
const response = await fetch(`${this.baseUrl}${endpoint}`, options);
|
|
128
173
|
|
|
174
|
+
// Handle 204 No Content or check if response requires JSON parsing
|
|
175
|
+
const isJson = response.headers.get('content-type')?.includes('application/json');
|
|
176
|
+
|
|
129
177
|
if (!response.ok) {
|
|
130
|
-
const errorData = await response.json().catch(() => ({}));
|
|
178
|
+
const errorData = isJson ? await response.json().catch(() => ({})) : { message: response.statusText };
|
|
131
179
|
throw new Error(`Lafeitu API Error: ${response.status} ${errorData.message || response.statusText}`);
|
|
132
180
|
}
|
|
133
181
|
|
|
134
|
-
|
|
182
|
+
if (isJson) {
|
|
183
|
+
return await response.json();
|
|
184
|
+
}
|
|
185
|
+
return {}; // Return empty object for non-JSON success responses (like 204)
|
|
135
186
|
} catch (error) {
|
|
136
|
-
console.error('Lafeitu SDK Request Failed:', error);
|
|
187
|
+
// console.error('Lafeitu SDK Request Failed:', error); // Silent catch to allow caller to handle
|
|
137
188
|
throw error;
|
|
138
189
|
}
|
|
139
190
|
}
|