Skip to content

Commit

Permalink
feat(api): ensure api is always initialized before first request
Browse files Browse the repository at this point in the history
  • Loading branch information
stfsy committed Nov 19, 2022
1 parent b638156 commit d96ec94
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions lib/paddle/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,21 @@ module.exports = class {
}
}

async _request(path, payload) {
if (!this._got) {
await this.init()
}

return this._got(path, payload)
}

/**
*
* @param {Object} subscription the target subscription object
* @returns
*/
async refundFullPayment({ order_id }) {
return this._returnResponseIf200(this._got(PATH_LIST_USERS, {
return this._returnResponseIf200(this._request(PATH_LIST_USERS, {
form: {
vendor_id: this._vendorId,
vendor_auth_code: this._authCode,
Expand Down Expand Up @@ -72,7 +80,7 @@ module.exports = class {
if (perPage) {
form.results_per_page = perPage
}
return this._returnResponseIf200(this._got(PATH_LIST_USERS, {
return this._returnResponseIf200(this._request(PATH_LIST_USERS, {
form
}))
}
Expand All @@ -89,13 +97,13 @@ module.exports = class {
subscription_id
}

return this._returnResponseIf200(this._got(PATH_LIST_USERS, {
return this._returnResponseIf200(this._request(PATH_LIST_USERS, {
form
}))
.catch((e) => {
if (e.message.includes('119')) {
form.state = 'deleted'
return this._returnResponseIf200(this._got(PATH_LIST_USERS, {
return this._returnResponseIf200(this._request(PATH_LIST_USERS, {
form
}))
} else {
Expand All @@ -117,7 +125,7 @@ module.exports = class {
if (plan) {
form.plan = plan
}
return this._returnResponseIf200(this._got(PATH_LIST_PLANS, {
return this._returnResponseIf200(this._request(PATH_LIST_PLANS, {
form
}))
}
Expand All @@ -127,7 +135,7 @@ module.exports = class {
* @returns
*/
async listProducts() {
return this._returnResponseIf200(this._got(PATH_LIST_PRODUCTS, {
return this._returnResponseIf200(this._request(PATH_LIST_PRODUCTS, {
form: {
vendor_id: this._vendorId,
vendor_auth_code: this._authCode
Expand All @@ -142,7 +150,7 @@ module.exports = class {
* @returns
*/
async updateSubscriptionPlan({ subscription_id }, planId) {
return this._returnResponseIf200(this._got(PATH_UPDATE_USERS, {
return this._returnResponseIf200(this._request(PATH_UPDATE_USERS, {
form: {
vendor_id: this._vendorId,
vendor_auth_code: this._authCode,
Expand All @@ -161,7 +169,7 @@ module.exports = class {
* @returns
*/
async updatePostcode({ subscription_id }, postcode) {
return this._returnResponseIf200(this._got(this._baseUrl, {
return this._returnResponseIf200(this._request(this._baseUrl, {
form: {
vendor_id: this._vendorId,
vendor_auth_code: this._authCode,
Expand All @@ -177,7 +185,7 @@ module.exports = class {
* @returns
*/
async cancelSubscription({ subscription_id }) {
return this._returnResponseIf200(this._got(PATH_CANCEL_SUBSCRIPTION, {
return this._returnResponseIf200(this._request(PATH_CANCEL_SUBSCRIPTION, {
form: {
vendor_id: this._vendorId,
vendor_auth_code: this._authCode,
Expand Down

0 comments on commit d96ec94

Please sign in to comment.