Newsletter Form API¶
Feature name: newsletter-form
Api name: NewsletterFormApi
API used to send a newsletter form without the need to use a newsletter-form webcomponent.
classDiagram
direction LR
NewsletterFormApi --> subscribe
class NewsletterFormApi {
subscribe(email: string) Promise~TResponseStatus|undefined~
}
NewsletterFormApi : TResponseStatus 'success' | 'failed' | undefined
link subscribe "../methods/subscribe/"
Get API¶
To get the NewsletterFormApi
use its name NewsletterFormApi
with the getApiSync
method.
useStorefront(async (storefront) => {
const newsletterFormApi = storefront.getApiSync('NewsletterFormApi');
});
This API is initialized only after newsletter-form webcomponent has been added to the page. Unless you add the webcomponent, you won't be able to fetch the API. If you wish to use the API alone without the webcomponent you can use the registerDynamic method from the Feature System API. Here is an example on how to do it:
useStorefront(async (storefront) => {
const featureSystemApi = this.getApiSync('FeatureSystemApi');
await featureSystemApi.registerDynamic('newsletter-form');
const newsletterFormApi = storefront.getApiSync('NewsletterFormApi');
});
Methods¶
- subscribe - subscribe to a newsletter via api
Event Bus events¶
Methods of this API dispatch the following events with the Event Bus:
Example¶
In this example we make a NewsletterFormApi
call to subscribe to a newsletter.
useStorefront(async (storefront) => {
const newsletterFormApi = storefront.getApiSync('NewsletterFormApi');
await newsletterFormApi.subscribe('email@example.com');
});