Developer guide
Developer guide

Call a server module from a component

Use this.serverCall(module, method, data) inside a WEngineElement or WEngineModal class. It posts JSON and resolves to the parsed JSON response. A non-success HTTP response rejects with the server's error message when one is supplied.

async loadItems() {
  try {
    const result = await this.serverCall('c__inventory', 'listItems', { limit: 20 });
    this.items = result?.items || [];
  } catch (error) {
    this.showToast({
      title: 'Could not load items',
      type: 'error',
      message: error?.message || 'Please try again.'
    });
  }
}

An unqualified standard module name such as we_record_picker maps to /server/std/we_record_picker/<method>. An explicitly qualified package module name such as c__inventory maps to /server/qualified/c/inventory/<method>. Use an unqualified name for standard modules. Existing custom code uses c__we-test. Component imports and browser tags use a colon form such as c:inventory; server-call names use __.

Export the named method from the custom module's index.js. The handler receives data from the request body and weSystem for platform operations:

async function listItems({ data, weSystem }) {
  const limit = Math.max(1, Math.min(Number(data?.limit) || 20, 100));
  // Query allowed records with weSystem, then return JSON-safe data.
  return { items: [], limit };
}

module.exports = { listItems };

The router resolves a custom module function by export name and runs it in the module loader. Handle expected errors in the component, and enforce data access on the server through the weSystem query and record APIs. The call transport is in working/public/js/server.js; routing is in working/routes/rpc.js and working/controllers/rpcController.js.

Next: server modules and API v1.0.