magento2-api-wrapper
Version:
Minimal Magento 2 API library. Both node and browser compatible
109 lines (108 loc) • 4.51 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MagentoOAuth = exports.Magento2Api = void 0;
const OAuth_js_1 = require("./OAuth.js");
const hmacSha256_js_1 = require("./hash/hmacSha256.js");
const flatten_js_1 = require("./util/flatten.js");
class Magento2Api {
constructor(options) {
Object.defineProperty(this, "oauth", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "options", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "requestMiddleware", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "responseMiddleware", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.options = options;
this.requestMiddleware = [];
this.responseMiddleware = [];
if (options.url.endsWith("/")) {
options.url = options.url.replace(/\/+$/, '');
}
if (options.consumerKey && options.consumerSecret) {
this.oauth = new MagentoOAuth({
consumer: { key: options.consumerKey, secret: options.consumerSecret },
signatureMethod: 'HMAC-SHA256',
hashMethods: {
"HMAC-SHA256": hmacSha256_js_1.hmacSha256
}
});
}
}
async request(method, path, data, options) {
const request = await this.buildRequest(method, path, data, options);
let response = this.fetch(request, options?.fetchOptions || {});
for (const middleware of this.responseMiddleware) {
response = response.then(middleware, middleware.bind(this, null));
}
return await response;
}
/**
* Internal method that could be overriden to decorate more response / reqest / add exception etc.
*/
fetch(request, options) {
return fetch(request);
}
async buildRequest(method, path, data, options) {
if (path[0] !== '/') {
const version = this.options.apiVersion || 1;
let storeCode = options?.storeCode || this.options.storeCode || '';
if (storeCode) {
storeCode += "/";
}
path = `/rest/${storeCode}V${version}/${path}`;
}
const query = options?.params
? Object.entries((0, flatten_js_1.flatten)(options.params)).map(p => `${encodeURIComponent(p[0])}=${encodeURIComponent(p[1])}`).join('&')
: null;
const url = this.options.url + path + (query ? `?${query}` : '');
const body = data ? JSON.stringify(data) : null;
const request = new Request(url, {
method,
...options,
headers: {
"Content-Type": "application/json",
...(options?.headers ? options.headers : {})
},
body
});
if (this.oauth && this.options.accessToken && this.options.tokenSecret) {
await this.oauth.authRequest(request, { key: this.options.accessToken, secret: this.options.tokenSecret });
}
for (const middleware of this.requestMiddleware) {
await middleware(request);
}
return request;
}
async $(method, path, data, options) { return (await this.request(method, path, data, options)).json(); }
async $get(path, options) { return (await this.request('get', path, null, options)).json(); }
async $post(path, data, options) { return (await this.request('post', path, data, options)).json(); }
async $patch(path, data, options) { return (await this.request('patch', path, data, options)).json(); }
async $put(path, data, options) { return (await this.request('put', path, data, options)).json(); }
async $delete(path, options) { return (await this.request('delete', path, null, options)).json(); }
}
exports.Magento2Api = Magento2Api;
class MagentoOAuth extends OAuth_js_1.OAuth {
// avoids sign requests issues when SKU for example contains special characters
constructRequestUrl(request) {
return super.constructRequestUrl(request).replace(/%252F/gi, '%2F');
}
}
exports.MagentoOAuth = MagentoOAuth;