platform-sdk-node
v0.10.8
Published
A node sdk for Platform API
Readme
Platform SDK for NodeJs
Install
npm i --save git+ssh://[email protected]:chaordic/platform-sdk-node.gitUsage Examples
Constructor
const Platform = require('platform-sdk-node');
const platformClient = new Platform({
apis: { // overrides default client configs
clients: {
// overrides host for clients only
host: 'https://clients.chaordicsystems.com',
},
},
// default host
host: 'https://platform.chaordicsystems.com',
// default authorization, can be a string or an object, with usermame/pass
authorization: '<platform basic auth>',
// default timeout, can be overridden, both within api configs, and in
// specific methods
requestTimeout: 10000,
// an optional interceptor function for endpoint calls, you can then
// manipulate the promise. This function should return the promise's result
// interceptor: (promise, clientName) => promise,
interceptor: async (promise, clientName) => {
// at this point the promise for clientName just begun
// we could for example measure the RT of the request here
const start = process.hrtime();
try {
const result = await promise;
// at this point, the promise resolved
// it's important to return the promises result
return result;
} catch (err) {
// the promise rejected
console.log('err', err);
// we want the method caller itself to handle the errors, so we throw here
throw err;
} finally {
// this code will run regardless of the promise resolving or rejecting
// so we could use it to meaure the request duration
const end = process.hrtime(start);
const elapsedTime = end[0] + end[1] / 1e9;
console.log(`request for ${clientName} took ${elapsedTime} seconds`);
}
},
});Clients
// get all clients
platformClient.v1.clients.getClients()
.then((resp) => {
// log only the first
console.log('getClients: resp', JSON.stringify(resp[0], null, ' '));
}).catch((err) => {
console.log('getClients: err', err);
});
// get single client by apiKey
platformClient.v1.clients.getClients({ apiKey: 'some-apikey' })
.then((resp) => {
console.log('getClient: resp', JSON.stringify(resp, null, ' '));
}).catch((err) => {
console.log('getClient: err', err);
});
// get clients with a query
platformClient.v1.clients.getClients({ query: '?info.status=integration||active' })
.then((resp) => {
console.log('getClient: resp', JSON.stringify(resp, null, ' '));
}).catch((err) => {
console.log('getClient: err', err);
});Products
// get product of a client by id
platformClient.v2.products.getProduct({
apiKey: 'some-apikey', productId: '1771896',
})
.then((resp) => {
console.log('getProduct: resp', JSON.stringify(resp, null, ' '));
}).catch((err) => {
console.log('getProduct: err', err);
});
// get product of a client by id and sales channel
platformClient.v2.products.getProduct({
apiKey: 'some-apikey', productId: '1771896', salesChannel: '18',
})
.then((resp) => {
console.log('getProduct: resp', JSON.stringify(resp, null, ' '));
}).catch((err) => {
console.log('getProduct: err', err);
});Users
// get a list of anonymous ids from a real user id
platformClient.v2.users.getAnonymous({ apiKey: 'some-apikey', userId: 'some-id' })
.then((resp) => {
console.log('v2.users.getAnonymous: resp', JSON.stringify(resp, null, ' '));
}).catch((err) => {
console.log('v2.users.getAnonymous: err', err);
});
// get a list of real user ids from an anonymous user id
platformClient.v2.users.getReals({ apiKey: 'some-apikey', anonymousId: 'some-id' })
.then((resp) => {
console.log('v2.users.getReals: resp', JSON.stringify(resp, null, ' '));
}).catch((err) => {
console.log('v2.users.getReals: err', err);
});Logs
// get a list of user transactions from a real user id
platformClient.v2.logs.getUserTransactions({ apiKey: 'some-apikey', userId: 'some-id' })
.then((resp) => {
console.log('v2.logs.getUserTransactions: resp', JSON.stringify(resp, null, ' '));
}).catch((err) => {
console.log('v2.logs.getUserTransactions: err', err);
});
// get a list of user views from a real/anonymous user id
platformClient.v2.logs.getUserViews({ apiKey: 'some-apikey', userId: 'some-id' })
.then((resp) => {
console.log('v2.logs.getUserViews: resp', JSON.stringify(resp, null, ' '));
}).catch((err) => {
console.log('v2.logs.getUserViews: err', err);
});
// get a list of cart views from a real/anonymous user id
platformClient.v2.logs.getCartViews({ apiKey: 'some-apikey', userId: 'some-id' })
.then((resp) => {
console.log('v2.logs.getCartViews: resp', JSON.stringify(resp, null, ' '));
}).catch((err) => {
console.log('v2.logs.getCartViews: err', err);
});LGPD Logs
If optout is set in params, the platform team validates that personalization is enabled for this touchpoint before returning any results.
// get a list of user transactions from a real user id
platformClient.v2.logs.getUserTransactions({ apiKey: 'some-apikey', userId: 'some-id', optout: '<touchpoint>' })
.then((resp) => {
console.log('v2.logs.getUserTransactions: resp', JSON.stringify(resp, null, ' '));
}).catch((err) => {
console.log('v2.logs.getUserTransactions: err', err);
});
// get a list of user views from a real/anonymous user id
platformClient.v2.logs.getUserViews({ apiKey: 'some-apikey', userId: 'some-id', optout: '<touchpoint>' })
.then((resp) => {
console.log('v2.logs.getUserViews: resp', JSON.stringify(resp, null, ' '));
}).catch((err) => {
console.log('v2.logs.getUserViews: err', err);
});
// get a list of cart views from a real/anonymous user id
platformClient.v2.logs.getCartViews({ apiKey: 'some-apikey', userId: 'some-id', optout: '<touchpoint>' })
.then((resp) => {
console.log('v2.logs.getCartViews: resp', JSON.stringify(resp, null, ' '));
}).catch((err) => {
console.log('v2.logs.getCartViews: err', err);
});touchpoint can be equals to general/onsite/email/search/banner/wishlist, etc. If it's equal to general, the personalization will be disabled for all products
Develop
Dependencies
Setup
To build the project:
Installs dependencies.
make buildStart the system
Runs the tests in watch mode.
make startStop the system
make stopRemove the system
make removeUpdate this README ToC
Needs markdown-toc.
markdown-toc -i README.mdGenerate this documentation
Documentation will be in ./docs directory
make docsTest
To run lint + unit tests
make testlint
make lintUnit tests
make mocha