DA App SDK Recipes - adobe/da-live GitHub Wiki
import DA_SDK from 'https://da.live/nx/utils/sdk.js';
const MOCK_PAGE = `
<body>
<header></header>
<main><div><h1>Hello World</h1></div></main>
<footer></footer>
</body>`;
(async function init() {
const { context, token } = await DA_SDK;
const { org, repo } = context;
const blob = new Blob([MOCK_PAGE], { type: 'text/html' });
const body = new FormData();
body.append('data', blob);
const opts = {
headers: { Authorization: `Bearer ${token}` },
method: 'POST',
body,
};
const fullpath = `https://admin.da.live/source/${org}/${repo}/drafts/cmillar/hello-world.html`;
const resp = await fetch(fullpath, opts);
console.log(resp.status);
}());
Note: You will need to obtain and provide your own authorization token. We have provided an anonymous implementation below.
const DA_API = 'https://admin.da.live/source';
const ORG = 'aemsites';
const REPO = 'da-block-collection';
const MOCK_PAGE = `
<body>
<header></header>
<main><div><h1>Hello World</h1></div></main>
<footer></footer>
</body>`;
(async () => {
const body = new FormData();
const data = new Blob([MOCK_PAGE], { type: 'text/html' });
body.set('data', data);
// const headers = { Authorization: `Bearer {{YOUR_TOKEN_HERE}}` };
const opts = { method: 'POST', body, headers };
const path = `${DA_API}/${ORG}/${REPO}/drafts/cmillar/node-demo.html`;
const resp = await fetch(path, opts);
console.log(resp.status);
})();
node index.js
- Use the crawl function to traverse a tree.
- Optional: Pass a callback to execute against the files as the are discovered.
- Optional: set a throttle limit. Increase this value if you are performing other network requests in your callback.
- Crawl returns
results<Promise>
,getDuration
, andcancelCrawl
. - Optional: await the final results.
- Optional: log the duration of the traversal.
import { crawl } from 'https://da.live/nx/public/utils/tree.js';
const PATH = '/da-sites/bacom';
(async function init() {
// Create cancel button
const button = document.createElement('button');
button.innerText = 'Cancel';
document.body.append(button);
// Create the callback to fire when a file is returned
const callback = (file) => {
button.insertAdjacentHTML('afterend', `<p>${file.path}</p>`);
}
// Start the crawl
const { results, getDuration, cancelCrawl } = crawl({ path: PATH, callback, throttle: 10 });
// Asign the cancel button the cancel event
button.addEventListener('click', cancelCrawl);
// Await the results to finish
await results;
// Add the duration after the results are finished
button.insertAdjacentHTML('beforebegin', `<p>${getDuration()}</p>`);
}());