node-allegro-client
v1.1.2
Published
Node.js Allegro REST API client
Downloads
7
Maintainers
Readme
node-allegro-client
Node.js Allegro REST API client - resource management wrapper
Getting Started
This package includes a simple resource management wrapper for Allegro REST Api.
See Allegro docs for more details.
Installation
npm i node-allegro-client
# or
yarn add node-allegro-client
Authorization
First of all, you need to create an application on Allegro.pl. Both Authorization Code and Device flows are supported.
Authorization Code flow:
import AllegroClient, { Config } from 'node-allegro-client';
const config: Config = {
appName: "APP_NAME", // Your App Name
clientId: "CLIENT_ID", // App Client ID
clientSecret: "SECRET_KEY", // App Secret Key
redirectUri: "REDIRECT_URI", // Redirect Uri (the same as specified on the Allegro)
env: "dev", // "dev" for development, "prod" for production
};
const allegroClient = new AllegroClient(config);
const oAuthLink = allegroClient.getAuthorizationCodeLink();
Now, you should redirect user to oAuthLink to get oAuth code.
Allegro will redirect you to redirectUri with oAuth code, eg. http://localhost:3000/allegro?code=YOUR_OAUTH_CODE
Authorize the application using the following method:
allegroClient.authorizeWithCode("YOUR_OAUTH_CODE");
Device flow:
import AllegroClient, { Config } from 'node-allegro-client';
const config: Config = {
appName: "APP_NAME", // Your App Name
clientId: "CLIENT_ID", // App Client ID
clientSecret: "SECRET_KEY", // App Secret Key
env: "dev", // "dev" for development, "prod" for production
};
const allegroClient = new AllegroClient(config);
const verificationLink = allegroClient.getDeviceVerificationLink();
Now, you should ask user to open verificationLink and allow application to access his data.
In parallel, the application can make attempts to authorize:
allegroClient.waitForDeviceVerification();
Token validity
After correct authorization, access_token (valid for 12 hours) and refresh_token (valid for 3 months) will be generated.
If you do not use this packege for 3 months, you will have to re-authorize app.
Usage
Now you can ask the Allegro for any resource:
// ...
allegroClient.request({
method: "get",
endpoint: "/sale/categories",
})
.then((r) => {
// do something with your data
})
.catch((e) => {
console.log(e);
});
allegroClient.request({
method: "post",
endpoint: "/sale/offers",
data: {
// your data
},
headers: {
// your custom headers, default content type: application/vnd.allegro.public.v1+json
}
})
.then((r) => {
// do something with your data
})
.catch((e) => {
console.log(e);
});
All possible resources, http methods and required data can be found in the docs.
License
This project is licensed under the MIT License.