Skip to content

Instantly share code, notes, and snippets.

@iksena
Created May 25, 2020 18:41
Show Gist options
  • Save iksena/faf1440663451c74dae96f76aaf76076 to your computer and use it in GitHub Desktop.
Save iksena/faf1440663451c74dae96f76aaf76076 to your computer and use it in GitHub Desktop.
import axios, { AxiosResponse } from 'axios';
import OAuth from 'oauth-1.0a';
import hmacSHA1 from 'crypto-js/hmac-sha1';
import Base64 from 'crypto-js/enc-base64';
export const config = {
WC_BASE_URL: 'http://localhost:8888',
WC_API_URL: '/wp-json/wc/v3',
WC_CONSUMER_KEY: 'ck_123xxx',
WC_CONSUMER_SECRET: 'cs_123xxx'
};
const _getOAuth = (): OAuth => new OAuth({
consumer: {
key: config.WC_CONSUMER_KEY,
secret: config.WC_CONSUMER_SECRET
},
signature_method: 'HMAC-SHA1',
hash_function: (baseString: string, key: string) => Base64.stringify(
hmacSHA1(baseString, key)
)
});
const get = async (path: string): Promise<AxiosResponse<object>> => {
const request = {
url: `${config.WC_BASE_URL}${config.WC_API_URL}${path}`,
method: 'GET'
};
const oauth = _getOAuth().authorize(request);
return axios.get(request.url, { params: oauth });
};
const post = async (path: string, body: object): Promise<AxiosResponse<object>> => {
const request = {
url: `${config.WC_BASE_URL}${config.WC_API_URL}${path}`,
method: 'POST'
};
const oauth = _getOAuth().authorize(request);
return axios.post(request.url, body, { params: oauth });
};
export default {
get,
post
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment