You can't create custom wire adapters at this point in time. The errors I got while trying to do so suggest that this is explicitly blocked or otherwise not supported at the moment.
@wire imported function
This results in an error like the following:
Uncaught Error: Assert Violation: @wire on "propName": unknown adapter id: function functionName()
This indicates that the framework is specifically checking to make sure you're only calling Apex methods.
@customDecorator
This results in an error like the following:
Compiler worker caught an error SyntaxError: /root/app/app.js: LWC1100: Invalid decorator usage. Supported decorators (api, wire, track) should be imported from "lwc"
This indicates that api, wire, and track are somehow "blessed" (to borrow from a Perl programming term).
As far as I can tell, decorators are still experimental, which may be why custom decorators are not supported yet. It is likely that this may be supported in the future.
In the meantime, you can still Share JavaScript Code to at least allow a way to import methods that are commonly used to reduce your overall code base size.
As an alternative, we can also use mixins. This allows us to add in functions, properties, etc from another component.
c/mixin/mixin.js
let mixin = (superclass) => class extends superclass {
mixedInFunction() {
return 'Hello World';
}
}
export { mixin };
c/app/app.js
import { LightningElement } from 'lwc';
import { mixin } from 'c/mixin';
export default class App extends mixin(LightningElement) {
connectedCallback() {
console.log(this.mixedInFunction());
}
}