Created
July 8, 2021 05:06
-
-
Save kentcdodds/815c2fad013740830946c17468e3bbb7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Menu: ConvertKit > Lookup | |
// Description: Query convertkit | |
// Author: Kent C. Dodds | |
// Twitter: @kentcdodds | |
const CONVERT_KIT_API_SECRET = await env('CONVERT_KIT_API_SECRET') | |
const CONVERT_KIT_API_KEY = await env('CONVERT_KIT_API_KEY') | |
const query = await arg('query') | |
let url | |
if (query.includes('@')) { | |
const sub = await getConvertKitSubscriber(query) | |
if (sub?.id) { | |
url = `https://app.convertkit.com/subscribers/${sub.id}` | |
} | |
} | |
if (!url) { | |
url = `https://app.convertkit.com/subscribers?utf8=%E2%9C%93&q=${query}&status=all` | |
} | |
exec(`open "${url}"`) | |
async function getConvertKitSubscriber(email) { | |
const url = new URL('https://api.convertkit.com/v3/subscribers') | |
url.searchParams.set('api_secret', CONVERT_KIT_API_SECRET) | |
url.searchParams.set('email_address', email) | |
const resp = await fetch(url.toString()) | |
const json = await resp.json() | |
const {subscribers: [subscriber] = []} = json | |
return subscriber | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment