:rocket: While converting a pair of JavaScript rep...
# pact-js
m
🚀 While converting a pair of JavaScript repos to TypeScript, I saw an opportunity to improve the API by introducing an abstraction on the consumer side and a pattern for stateHandlers on the provider side. I’ve documented the approach in a blog post—would love to hear your thoughts! 💬 📝 Blog Post: Strengthening Pact Contract Testing with TypeScript and Data Abstraction 💻 Check out the PRs: • Consumer PRProvider PR
🚀 1
🎉 1
m
Thanks - looking forward to reading in detail. It’s definitely an area that confuses people and any abstractions that improve type safety/comprehension is a 👍 for me
One thing to note, is that all Pact really needs is a JSON serialisable object. So the simplest implementation of
toJsonMap
would be something like this:
Copy code
const toJsonMap = (obj: any) => JSON.parse(JSON.stringify(obj))
m
if we use JSON.parse(JSON.stringify(..), we might run into trouble when we need to: • convert
null
and
undefined
to a string
"null"
• serialize objects (except for dates and arrays) into JSON strings • convert dates to ISO strings • preserve numbers, booleans, and arrays
m
yep, I appreciate it wouldn’t solve those problems but was just trying to make the general point. Some of those decisions would be use-case specific. For example, serialising
null
as
"null"
is not something I would want represented in my API. Date formats are another good example - some may prefer a timestamp serialisation rather than ISO.
👍 1