I want to have one file that contains the Moralis.start() method so I don’t need to duplicate code across handlers.
Here’s my current code:
let moralisInstance: typeof Moralis;
export const getMoralisInstance = (
config?: Partial<MoralisConfigValues>
): typeof Moralis => {
if (!moralisInstance) {
Moralis.start({
apiKey: process.env.NEXT_PUBLIC_MORALIS_API_KEY,
...config,
});
moralisInstance = Moralis;
}
return moralisInstance;
};
This works but I will occasionally get a Moralis error stating that the module has already been started.
error - unhandledRejection: MoralisError [Moralis SDK Error]: [C0009] Modules are started already. This method should be called only one time.
Any better ideas?