Repository navigation
JavaScript SDK
This page is the reference for the globals registered by CapiCommunity.Setup(): window.$community, a small typed
client, and window.CAPI, the community URL builder catalog. Routes are relative to the selldone-capi base URL
(https://capi.selldone.com). For setup, see Getting Started.
| Global | Type | What it is |
|---|---|---|
window.$community |
CapiCommunity |
Typed client: community info, post search, community ID and route names. |
window.CAPI |
CAPI |
URL builders for every community route. See Community API URL Builders. |
The package exports one symbol, CapiCommunity. The link helper CommunityURLs is imported from
@selldone/sdk-community/url/CommunityURLs (see Links and Routes).
Loads a community with its categories and statistics.
| Parameter | Type | Description |
|---|---|---|
community_id |
number | The community ID. The method throws Not valid community id! if it is empty. |
offset |
number, optional | Start of the statistics window (query parameter). |
days |
number, optional | Length of the statistics window in days (query parameter). |
Route: GET /communities/{community_id}
Response
| Field | Type | Description |
|---|---|---|
community |
Partial<Community> |
The community (id, name, title, …). |
categories |
CommunityCategory[] |
Categories, with title, desc, image, total_topics, total_posts, … |
data |
CommunityData[] |
Statistics time series for the requested window. |
const { community, categories, data } = await window.$community.getInfo(communityId, 0, 7);Searches the posts of a community.
| Parameter | Type | Description |
|---|---|---|
community_id |
number | The community ID. |
offset |
number | Pagination offset. |
limit |
number | Page size. |
search |
string or null
|
Search text. |
Route: GET /communities/{community_id}/search
Response: { posts, more }, where posts is an array of Partial<CommunityPost> and more is true when
another page is available.
async function searchAll(communityId, text, limit = 20) {
const results = [];
for (let offset = 0; ; offset += limit) {
const { posts, more } = await window.$community.post.searchPosts(communityId, offset, limit, text);
results.push(...posts);
if (!more) return results;
}
}Returns the community ID to use:
- with a
shopobject, the ID ofshop.community(orundefinedif the shop has no community); - without a shop (
nullorundefined),1, the main Selldone community.
const communityId = window.$community.getCommunityID(shop);
if (!communityId) {
// The shop has not created a community yet
}The route names of the community pages, from CommunityRoutesName in Selldone Core. Use them with Vue Router so your
links match the Storefront and the Components community UI:
| Key | Value |
|---|---|
COMMUNITY_HOME_PAGE |
CommunityHomePage |
COMMUNITY_CATEGORY_PAGE |
CommunityCategoryPage |
COMMUNITY_TOPIC_PAGE |
CommunityTopicPage |
See Links and Routes for an example.
window.$community and window.$community.post extend APIAbstract from Selldone Core, so they support the same
helpers as the other Selldone SDKs:
window.$community.post
.optimize(60) // reuse a cached result younger than 60 seconds
.searchPosts(communityId, 0, 20, text)
.cache(({ posts, more }) => render(posts, more)) // cached result first, if any
.then(({ posts, more }) => render(posts, more))
.catch((error) => console.error(error));| Helper | Effect |
|---|---|
promise.cache(callback) |
Calls callback with the last cached response for the same request, and caches the fresh one. Call it before .then(). |
.optimize(seconds = 60) |
Resolves with the cached response, without a server call, if it is younger than seconds. |
.force() |
Clears optimize(). |
.cancellation(enabled = true) |
A new request to the same URL cancels the pending one; the canceled promise rejects with undefined. |
Promises reject with the API error body ({ error: true, error_msg, code }) when the API reports an error, or with
an Axios error for network and HTTP failures.