vtex-masterdata v0.4.1
VtexMasterdata.js
Helpers to manipulate data from Vtex Masterdata
Table of contents
Main
dist/
├── vtex-masterdata.js (UMD)
├── vtex-masterdata.min.js (UMD, compressed)
├── vtex-masterdata.common.js (CommonJS, default)
└── vtex-masterdata.esm.js (ES Module)
Getting started
Direct download
Download the script here and include it.
You will need VtexUtils.js
<script type="text/javascript" src="/arquivos/vtex-utils.min.js"></script>
<script type="text/javascript" src="/arquivos/vtex-masterdata.min.js"></script>
Package Managers
VtexMasterdata.js supports npm under the name vtex-masterdata
.
npm install vtex-masterdata --save
Module Loaders
VtexMasterdata.js can also be loaded as an CommonJS or ES6 module (recomended).
// CommomJS
var VtexMasterdata = require('vtex-masterdata');
// ES6 module
import VtexMasterdata from 'vtex-masterdata';
Usage
With UMD (Universal Module Definition), the package is available on VTEX
namespace.
// First, initialize VtexUtils.js
var vtexUtils = new VTEX.VtexUtils();
// Initialize constructor passing VtexUtils.js as a param
var vtexMasterdata = new VTEX.VtexMasterdata(vtexUtils);
// Set store name (https://store-name.vtexcommercestable.com.br)
vtexMasterdata.setStore('store-name');
Methods
vtexMasterdata.newsletter(email[, newsletter, entity])
Newsletter opt-in / opt-out
email:
- Type:
String
- The email of the user to opt-in/out
- Type:
newsletter (optional):
- Type:
Boolean
- Default:
true
- Whether to opt-in/out
- Type:
entity (optional):
- Type:
String
- Default:
'CL'
- The Entity of the user
- Type:
Example
Remove user from newsletter
vtexMasterdata.newsletter('email@email.com', false)
.done(function(res) {
if ( res.result.dataInsert.isNewsletterOptIn ) {
window.console.log('Subscribed');
} else {
window.console.log('Not subscribed');
}
})
.fail(function(err) {
window.console.log(err);
});
vtexMasterdata.getUser(email, fields, entity)
Get User by mail
email:
- Type:
String
- The email of the user
- Type:
fields
- Type:
Array
- A list of fields to retrieve
- Type:
entity (optional):
- Type:
String
- Default:
'CL'
- The Entity of the user
- Type:
Example
Get email, firstName and lastName by user e-mail (needs read properties set to public on Masterdata)
vtexMasterdata.getUser('email@email.com', ['email', 'firstName', 'lastName'])
.done(function(res) {
// Response with an object that contains id, email, firstName and lastName of user
window.console.log(res.result.dataResponse);
})
.fail(function(err) {
window.console.log(err);
});
vtexMasterdata.updateUser(email, data, entity)
Update User by email
email:
- Type:
String
- The email of the user
- Type:
data:
- Type:
Object
- The data that will be updated.
- Type:
entity (optional):
- Type:
String
- Default:
'CL'
- The Entity of the user
- Type:
Example
Update user newsletter opt in, firstName and lastName (needs update properties set to public on Masterdata)
vtexMasterData.updateUser('email@email.com', {isNewsletterOptIn: true, firstName: 'New firstname', lastName: 'new lastname'})
.done(function(res) {
if ( res.isUpdate() ) {
window.console.log(res.result);
}
})
.fail(function(err) {
window.console.log(err);
});
vtexMasterdata.insertUpdateUser(email[, data, entity])
Update a user if the email exists, or insert a new one if it doesn't
email:
- Type:
String
- The email of the user
- Type:
data (optional):
- Type:
Object
- The data that will be updated.
- Type:
entity (optional):
- Type:
String
- Default:
'CL'
- The Entity of the user
- Type:
Example
Create new user by email
vtexMasterdata.insertUpdateUser('new-user@email.com')
.then(function(res) {
window.console.log(res);
})
.fail(function(err) {
window.console.log(err);
});
Update newsletter opt in user by email (needs update properties set to public on Masterdata)
vtexMasterdata.insertUpdateUser('new-user@email.com', {isNewsletterOptIn: true})
.then(function(res) {
window.console.log(res);
})
.fail(function(err) {
window.console.log(err);
});
vtexMasterdata.insert(data, entity)
Insert a document
data:
- Type:
Object
- The data that will be inserted
- Type:
entity (optional):
- Type:
String
- Default:
'CL'
- The entity of the document to insert
- Type:
vtexMasterdata.insertUpdate(id, data, entity)
vtexMasterdata.search(params, fields[, entity[, limit, offset]])
Performs a search
params
- Type:
Object
- The search parameters
- Type:
fields
- Type:
Array
- A list of fields to retrieve
- Type:
entity (optional):
- Type:
String
- Default:
'CL'
- The entity where the search will be performed
- Type:
limit (optional):
- Type:
Integer
- Default:
'49'
- The search limit
- Type:
offset (optional):
- Type:
Integer
- Default:
'0'
- The search offset
- Type:
Example
Search only stores with state = SP
vtexMasterdata.search({state: 'SP'}, ['latitute', 'longitude'], 'SO')
.done(function(res) {
// Response with an object that contains only stores from 'SP' and latitude/latitude properties
window.console.log(res.result.dataResponse);
})
.fail(function(err) {
window.console.log(err)
});
To search all results from entity, pass params as an empty object
vtexMasterdata.search({}, ['latitute', 'longitude'], 'SO')
.done(function(res) {
// Response all data from 'SO' entity
window.console.log(res.result.dataResponse);
})
.fail(function(err) {
window.console.log(err)
});
vtexMasterdata.fullSearch(params, fields, [filters[, entity[, limit, offset]]])
vtexMasterdata.get(id, fields, entity)
vtexMasterdata.exists(id, entity)
vtexMasterdata.uploadFile(id, entity, field, file)
License
VtexMasterdata.js is open-sourced software licensed under the MIT license.
Dependencies
jQuery 1.8.3+
VtexUtils.js
Todo
- Docs
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago