SHADE is young: 0.1.0-next.2 is a pre-release, and 0.1.0 isn't out yet.

SHADE
Writing a plugin

Calls between modules

A module that builds on another can call it. The module that offers the calls lists them in its manifest part provides; the module that calls them says it requires the first and calls with callModule. Calls between modules are experimental in 0.1 (see Experimental manifest parts): provides, defineModuleCalls, callModule, ModuleCallContract, ModuleCallSpec, ModuleProvides, ModuleCall, ModuleCallHandler and ModuleCaller may change in a minor version. Both modules must be trusted.

The payments module is the example: the shop starts, reads, cancels and refunds payments by calling it, and hears what happens to them as events. Core has no payments of its own.

The contract

The offering module exports its calls from a shared entry (@shadestack/module-payments/shared), written with zod, so the callers can import them without importing its server code:

import type { ModuleCallContract } from '@shadestack/module-sdk/shared'
import * as z from 'zod'

export const PAYMENTS_CALLS = {
  start: {
    description: 'Starts a hosted checkout for the caller: the payment\'s id and where to send the visitor',
    input: StartPaymentInput,
    answer: z.object({ paymentId: z.string(), checkoutUrl: z.string() })
  },
  get: { description: 'One of the caller\'s payments', input: z.object({ id: z.string() }), answer: Payment },
  // getMany, cancel, refund …
} satisfies ModuleCallContract

Each call has a description, an input schema and an answer schema. Amounts are SHADE's Money (whole minor units and a currency), never floats. A caller imports the contract as a peer dependency of the offering module's package, never its server code.

Offering calls: provides

import { defineModuleCalls } from '@shadestack/module-sdk/server'

export const provides = defineModuleCalls(PAYMENTS_CALLS, {
  start: (ctx, input, { caller }) => startPayment(ctx, caller, input),
  get: (ctx, { id }, { caller }) => getPayment(ctx, caller, id),
  // …
})

export default defineModule({ name: 'payments', provides, /* … */ })

A handler gets the context made for the offering module, the checked input and who called (caller, the calling module's name). It must keep each caller to its own records: the payments module answers NOT_FOUND for another module's payment. The handler does not check the caller's permissions: the caller does, before it calls (the shop checks commerce.orders.refund before it refunds). The handler runs in the caller's transaction, for whoever caused the call, so its writes and events commit or roll back with the caller's.

Calling: callModule

import { callModule } from '@shadestack/module-sdk/server'
import { PAYMENTS_CALLS } from '@shadestack/module-payments/shared'

const { paymentId, checkoutUrl } = await callModule(ctx, 'payments', PAYMENTS_CALLS, 'start', {
  reference: order.id, amount: { amount: 2550, currency: 'EUR' }, description: 'Order 1001',
  redirectUrl, locale: 'nl', idempotencyKey: `order:${order.id}`
})
export default defineModule({ name: 'commerce', requires: { payments: '^0.1.0' }, /* … */ })
  • The caller must require the offering module, and the call must come from trusted module code (the context SHADE gave it). Otherwise it is refused (FORBIDDEN). A call to a name the module does not offer is NOT_FOUND; a call while the offering module is off is UNAVAILABLE.
  • The input is checked by the caller's contract and then by the offering module's own schema, and the answer by both again, so neither side trusts the other's types.
  • Because the caller requires the offering module, it cannot be switched on before it, and the offering module cannot be switched off or uninstalled while the caller is on (Modules that build on other modules). The shop therefore waits for the payments module and its Mollie key.

Telling the caller what happened: events

Calls go one way. What happens later comes back as events the offering module emits, which the calling module hears in on (Hearing events): the payments module emits payments.payment.paid, .failed, .canceled, .expired, .pending and .refunded, each with the payment as its data (event.data.source names the module that started it, so a module keeps to its own). The events are durable: they are retried when the handler throws and kept while the hearing module is off. Read the payment again with get in the handler, so the latest state counts whatever order the events arrive in.

on: {
  'payments.payment.paid': async (ctx, event) => {
    if (event.data.source !== 'commerce') return
    const payment = await callModule(ctx, 'payments', PAYMENTS_CALLS, 'get', { id: event.data.id })
    await markOrderPaid(ctx, payment)
  }
}

Testing

The payments module's @shadestack/module-payments/testing entry (tests only) gives fakeMollie(), which answers Mollie's API inside the test, storeMollieKey and callMollieWebhook. The shop's tests use it, with SHADE's testInstance, to run a whole checkout without a network.