6

We are trying to normalize our LWC code. Part of that is looking at moving our third-party api's into wire services. I've seen some example of how to setup a custom wire service of OSS lwc but have been unable to get it working on platform. An example would be using the Google geocoding API or autocomplete as part of a wire service. I understand that both of these can be handled by calling an apex class that utilizes a @wire decorator. However, we don't want to be required to make server calls to salesforce when direct third-party calls are possible. (I understand the any third-party source would require CSP trust sites.)

I am happy to provide code for oss lwc, but really looking for reference or documentation sources. Any pointers or direction would be appreciated.

1 Answer 1

3

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());
    }
}
3
  • Thanks. Something like this is hard to tell if its explicitly prevented/disallowed on platform. We make heavy use of mixin/inheritence of lwc in our code base. Part of it is that sfdc is trying to rely on a web-standard but do it in house and oss at the same time. Which makes parsing the docs difficult. I'm somewhat surprised that they limited access to the wire-service, concerning they've released a rfc. It seem like a very good framework for handling data in the client driven context (ie, a content where returned data may change and the single return aspect of a promise isn't perfect)
    – John
    Nov 9, 2019 at 14:07
  • With all due respect, I fail to see how anything in your answer addresses the OP's issue, which is essentially the ability to write custom wire adapters. This would require exposing the interfaces and definitions for wire adapters--one can see parts of what would be required by looking at the source code for any built-in wire adapter--as well as server-side machinery to be able to dynamically trigger the client-side wire when something changed; I assume this machinery is already in existence because it must hva been used in implementing the server-side code for the built-in wire adapters.
    – kamezaburo
    Jun 6, 2020 at 6:24
  • 1
    @kamezaburo Thanks for the feedback. I agree, I was disappointed with the results of my research for this question. As far as I could tell then (and still applies now), we can't register custom wire adapters, we can't write custom decorators, and we can't use anything other than imported Apex functions, which appears to be enforced by the compiler. If you happen to stumble across a solution, I'd love to see it, because I have tons of use cases for this myself.
    – sfdcfox
    Jun 6, 2020 at 11:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .