paxton-front/api/instance.ts

40 lines
1.1 KiB
TypeScript

import { Api } from '~/api/Api'
const requestUrl = 'https://wp.koptilnya.xyz/wp-json'
const consumerKey = 'ck_8b5477a1573ce6038ef1367f25d95cede1de4559'
const consumerSecret = 'cs_d0ccaa93e8efe4f76eef0b70c9828a58fc53459f'
const authString = `${consumerKey}:${consumerSecret}`
const encodedAuth = btoa(unescape(encodeURIComponent(authString)))
const api = new Api({
baseUrl: String(requestUrl),
})
Object.keys(api.wc).forEach((methodName) => {
const nativeMethod = api.wc[methodName] as typeof Function
const argsNumber = nativeMethod.length
api.wc[methodName] = function (...args) {
let params: Record<string, unknown> = {}
if (args.length > argsNumber) {
params = args.pop()
}
return nativeMethod(...args, {
...params,
headers: {
...(params.headers as Record<string, string> ?? {}),
Authorization: `Basic ${encodedAuth}`,
},
} as unknown as string)
}
})
const nativeRequest = api.request
api.request = async function (...args) {
const response = await nativeRequest.call(api, ...args)
return await response.json()
}
export default api