UNPKG

lazada-iop-client

Version:

Lazada IOP Client SDK for Node JS

132 lines (90 loc) 3.66 kB
# Lazada IOP SDK for Node JS Lazada SDK for JavaScript allows you to interact with the Lazada API easily. This SDK supports features such as authentication, sending API requests, and file handling. ## Features - **OAuth2 Authentication**: Generate an authentication URL for users to authorize your application. - **API Requests**: Send API requests to the Lazada server with the provided parameters. - **API Signature**: Sign requests for security and data integrity. - **File Support**: Upload files to the Lazada API using FormData. ## Installation To get started, install the following dependencies: ```bash pnpm add axios form-data crypto ``` ## Usage ### 1. Create an Instance of `LazadaClient` To begin using this SDK, create an instance of `LazadaClient` and provide the required parameters. ```javascript import LazadaClient from "./LazadaClient"; // Create an instance of LazadaClient const client = new LazadaClient({ apiKey: "YOUR_API_KEY", apiSecret: "YOUR_API_SECRET", region: "SG", // Example: 'SG', 'MY', 'VN', etc. callbackUrl: "YOUR_REDIRECT_URL", }); ``` ### 2. Authentication To get the authentication URL, use `makeAuthURL`. The user will be directed to this URL to grant access to your app. ```javascript const authURL = client.makeAuthURL(); console.log(authURL); // Direct the user to this URL to login ``` ### 3. Set Access Token After the user grants permission and your app receives the authorization code, you can set the access token to the `LazadaClient` instance. ```javascript client.setAccessToken("YOUR_ACCESS_TOKEN"); ``` ### 4. Add API Parameters You can add API parameters that will be used in the API request. ```javascript client.addAPIParam("key", "value"); client.addFileParam("file_key", fileBuffer); ``` ### 5. Execute API Requests To send an API request to Lazada, use the `execute` method with the API path and HTTP method. ```javascript const response = await client.execute("/path/api", "GET", { key: "value" }); console.log(response); ``` ### 6. Change Region If you need to change the region where you want to access the Lazada API, you can do so with the `changeRegion` method. ```javascript client.changeRegion("MY"); // Change to Malaysia ``` ## Full Example ```javascript import LazadaClient from "./LazadaClient"; const client = new LazadaClient({ apiKey: "YOUR_API_KEY", apiSecret: "YOUR_API_SECRET", region: "SG", callbackUrl: "YOUR_REDIRECT_URL", }); // 1. Get the authentication URL const authURL = client.makeAuthURL(); console.log("Redirect the user to the following URL:", authURL); // 2. After getting the access token, set the token client.setAccessToken("YOUR_ACCESS_TOKEN"); // 3. Add API parameters client.addAPIParam("someKey", "someValue"); // 4. Execute the API request client.execute("/some/api/path", "GET").then((response) => { console.log("Response from the API:", response); }); ``` ## Directory Structure ``` project/ │ ├── src/ │ └── index.ts # Main SDK code ├── example.ts # Example usage of the SDK ├── package.json # Project and dependencies information └── tsconfig.json # TypeScript configuration ``` ## Contribution If you want to contribute, please open a _pull request_ or create an _issue_ for any problems or suggestions. ## License This SDK is licensed under the [MIT License](LICENSE). --- Make sure to replace placeholders like `YOUR_API_KEY`, `YOUR_API_SECRET`, and `YOUR_REDIRECT_URL` with your actual project information.