UNPKG

@koempf/shopgate-utility

Version:

Shopgate WebCheckout utility for Kömpf

88 lines (74 loc) 1.95 kB
'use strict' const { promisify } = require('util') const login = async ({ username, password }, config) => { const params = { url: config.endpoint + '/token', method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, json: true, body: new URLSearchParams({ grant_type: 'password', client_id: config.clientId, client_secret: config.clientSecret, username: username.toLowerCase(), password }).toString() } return promisify(config.tracedRequest)(params) } const refreshLogin = async (config) => { const params = { url: config.endpoint + '/token', method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, json: true, body: new URLSearchParams({ grant_type: 'refresh_token', client_id: config.clientId, client_secret: config.clientSecret, refresh_token: config.context.refreshToken }).toString() } return promisify(config.tracedRequest)(params) } const apiClient = async (method = 'GET', path, body, config) => { const params = { url: config.endpoint + path, method, json: true, headers: { 'Content-Type': 'application/json' }, body } if (config.context.accessToken) { params.headers.Authorization = 'Bearer ' + config.context.accessToken } else { params.headers['X-Anonymous-Customer-Unique-Id'] = config.context.guestToken } return promisify(config.tracedRequest)(params) } const get = async (path, config) => { return apiClient('GET', path, {}, config) } const post = async (path, body, config) => { return apiClient('POST', path, body, config) } const patch = async (path, body, config) => { return apiClient('PATCH', path, body, config) } const del = async (path, config) => { return apiClient('DELETE', path, {}, config) } module.exports = { login, refreshLogin, get, post, patch, del }