Make your own
Making your own adapter is straightforward. An adapter must implement the following interface:
interface IAdapter<JP> {
/**
* Verifies the signature of the incoming request
*/
verify: IAdapterVerify;
/**
* Sends the payload to the backend
*/
send: IAdapterSend<JP>;
}
Here, JP
is a generic which refers to the type of the job payload. You can use the existing adapters on GitHub as a guide. Otherwise, here is a minimal reference:
import type { IAdapter } from "bunnygram";
const MyAdapter = <JP>(): IAdapter<JP> => {
return {
verify: async (verifyProps) => {
const { rawBody, req, runtime } = verifyProps;
return {
verified: true,
message: "LGTM 👍",
};
},
send: async (sendProps) => {
const { payload, url, runtime } = sendProps;
console.log(payload, url);
},
};
};
Runtimes
Your adapter should be resilient to running in different JavaScript runtimes e.g. browser, Edge or Node.js.
Here are some tips you can use:
- Bunnygram exposes the current
runtime
as part of thesendProps
on thesend
function andverifyProps
on theverify
function. You can use that for conditional logic. - Even if you have conditional guards, you can still run into import errors if, for example, you have a Node.js-specific dependency and the import statement is read by the browser (or any other incompatible import combination between the runtimes). To prevent this, you can use runtime dynamic imports (opens in a new tab).