diff --git a/packages/sails-lemonsqueezy/adapter.js b/packages/sails-lemonsqueezy/adapter.js index 2644a97..fd81c89 100644 --- a/packages/sails-lemonsqueezy/adapter.js +++ b/packages/sails-lemonsqueezy/adapter.js @@ -2,5 +2,8 @@ const methods = require('./machines') module.exports = { identity: 'sails-lemonsqueezy', config: {}, - checkout: methods.checkout + checkout: methods.checkout, + subscription: { + get: methods.subscription.get + } } diff --git a/packages/sails-lemonsqueezy/machines/index.js b/packages/sails-lemonsqueezy/machines/index.js index 2c16a3f..366a8c6 100644 --- a/packages/sails-lemonsqueezy/machines/index.js +++ b/packages/sails-lemonsqueezy/machines/index.js @@ -1,3 +1,6 @@ module.exports = { - checkout: require('./checkout') + checkout: require('./checkout'), + subscription: { + get: require('./subscription/get') + } } diff --git a/packages/sails-lemonsqueezy/machines/subscription/get.js b/packages/sails-lemonsqueezy/machines/subscription/get.js new file mode 100644 index 0000000..39a5655 --- /dev/null +++ b/packages/sails-lemonsqueezy/machines/subscription/get.js @@ -0,0 +1,41 @@ +const fetch = require('../../helpers/fetch') + +module.exports = require('machine').build({ + friendly: 'Get Subscription', + description: 'Get a subscription by ID.', + moreInfoUrl: 'https://docs.lemonsqueezy.com/api/subscriptions', + inputs: { + apiKey: require('../../helpers/parameters').LEMON_SQUEEZY_API_KEY, + id: { + type: 'string', + description: 'The ID of the subscription to get.', + allowNull: true + } + }, + exits: { + success: { + outputFriendlyName: 'Subscription', + outputDescription: 'The subscription that was retrieved.' + }, + error: { + outputFriendlyName: 'Error', + outputDescription: 'An unexpected error occurred.' + } + }, + fn: async function ({ apiKey, id }, exits) { + const adapterConfig = require('../../adapter').config + + const subscription = await fetch(`/subscriptions/${id}`, { + method: 'GET', + headers: { + authorization: `Bearer ${apiKey || adapterConfig.apiKey}` + } + }) + if (subscription.errors) { + const errors = subscription.errors + return exits.error(errors) + } + + return exits.success(subscription) + } +})