🔌 adapter
write I/O agnostic utilities
What is it?
adapter
is a tiny TypeScript helper for writing I/O agnostic utilities in a standardized way. Your code could run in a headless process, an interactive commandline tool, or in a graphical program, but either way your code remains the same.
The idea is difficult to appreciate without an example. If you're comfortable with writing your own Promises and async
/await
patterns, the following example should be fairly intuitive.
Example
Let's say that you have some code that creates a remote repository via the GitHub API. It might look roughly like this:
; // http library ;;; await axios.posturl, data, ;
adapter
can be used to improve this code by injecting (or "plugging in") the means of handling I/O (input
/output
), just as Promises inject the means of handling success/failure (resolve
/reject
). Let's rewrite the above code leveraging these injections:
// github-service.ts ;; ;;; ;
Now that our input and output functions are injected, we can use this service anywhere. Say we want to use it as part of a CLI application:
// cli-app.ts ; // inquirer is tool for getting cli input; ;;; createRepo // calling our service gives us an `Adapter` .attach // here we "plug in" our attachments .exec; // and this executes our code
We could also utilize the same service in a web application:
// browser-app.ts ; service // you can also attach handlers independently, .thenconsole.info // like so: .catchconsole.error .outputconsole.log .input .exec;
Hopefully seeing these two applications side by side illustrates the advantages of using adapter
.
Installation
npm install adapter
or
yarn add adapter
Documentation
More complete documentation is coming soon!
Author
Tanner Nielsen tannerntannern@gmail.com