Created
May 25, 2020 18:41
-
-
Save iksena/faf1440663451c74dae96f76aaf76076 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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