Generating a Typescript router in Django
(Nerd alert! This post is a small technical snippet for Python and Django programmers. If you don’t know what those proper nouns mean, don’t worry — feel free to skip this post.)
Buttondown has used django-js-reverse since its inception to route backend API urls from the Django backend to the Vue frontend.
This has been (not to damn with faint praise!) better than nothing: some kind of syncing mechanism certainly beats having to manually correlate backend routes with frontend definitions. But there are two nits I’ve had with the package:
- The package isn’t TypeScript-friendly; errors in routing or invocation (such as calling
Urls.email_detail()
instead ofUrls.email_detail("foo")
) are not caught until runtime. - The package does some light metaprogramming to allow you to refer to a route not just as its name (
email-detail
) but via camel or snake case (Urls.emailDetail()
orUrls.email_detail()
). This attempt at ergonomics makes it trickier to actually find all invocations of a route at a glance.
Naturally, I thought to myself: why not try my hand at building a better version? I’m happy to share that it was fairly painless: while I’m still a little bit away from formally open-sourcing this as a package (there are a number of administrative tasks like registering with PEP and settling on a name that I need to take care of), here is the total implementation — 34 lines of Python and 4 lines of templated TypeScript.
I am confident this implementation is missing many, many, many edge cases. It does not handle URL namespacing nor non-string parameters, nor I’m sure many other things. But it is a step change improvement to Buttondown’s codebase, and I’m excited to have it. Please let me know if you find it useful, or if you have any feature requests/suggestions — I promise to update this blog post with a link to the repository once it’s published to PEP.
Previous posts
- All your emails, in one place
- Better logging in RQ
- Attach files to your emails
- A better Events view
- Secure your account with multi-factor authentication
- Get more premium subscribers by sending teasers of premium emails
- How I organize Buttondown's Django applications
- Buttondown's new pricing system
- Send archived emails to new subscribers
- Embed your subscribe form in the middle of your writing
- Granular tracking settings
- Migrating Buttondown to mypy
- Better upselling for premium subscriptions
- Support for Stripe Payment Links
- Pay-what-you-want subscriptions
- Spring 2021
- Support for HTTPS tracking links
- Email tracking is now off by default
- You can now integrate with Plausible!
- A nicer subscriber importing experience
- Referencing subscriber tags in your newsletter
- Accept whatever currency you want for your paid newsletter
- Slightly nicer Design settings
- Webhooks and the Programming page
- Buttondown in 2021
- Undo send
- Navigation updates
- Open Beta: European mail servers
- Set a custom unsubscription URL for your (sadly no-longer-subscribed) subscribers
- Compare analytics from multiple emails
- Saved draft recipients
- Buttondown's funding of open source software
- Why you need to include a physical address in your newsletter
- Adding a Buttondown subscription form to Weebly
- Annual subscriptions for paid newsletters
- Reminding folks to confirm their subscription
- Adding a Buttondown subscription form to Squarespace
- Adding a Buttondown subscription form to Webflow
- Embedding Instagram photos into your emails
- Customizing your subscription form to tag new subscribers
- How to opt out of analytics and email tracking
- Managing your drafts through the API
- Open Beta: Multiple newsletters
- Easy subscriber filters
- Scheduling emails through the API
- Sending newsletters that get delivered
- May Updates
- April Updates
- March Updates
- February Updates
- Winter Update
- June Updates
- May Updates
- April Updates
- March Updates
- Nicer archive pages!
- Markdown is easier than ever
- January Updates
- 2017: In Review
- Introducing subscriber events
- Learning about unsubscribed users
- A new blog
- Learn more about your subscribers!
- Send emails to certain tags!
- Gravatar support
- Subscriber sources
- Embeddable subscription widget
- Creating drafts via email
- Subscriber and archive search