DA App SDK Recipes - adobe/da-live GitHub Wiki

Create a page

Browser

my-upload.js

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);
}());

Node (20+)

Note: You will need to obtain and provide your own authorization token. We have provided an anonymous implementation below.

index.js

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

Get a streaming list of pages under a path

  1. Use the crawl function to traverse a tree.
  2. Optional: Pass a callback to execute against the files as the are discovered.
  3. Optional: set a throttle limit. Increase this value if you are performing other network requests in your callback.
  4. Crawl returns results<Promise>, getDuration, and cancelCrawl.
  5. Optional: await the final results.
  6. 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>`);
}());
⚠️ **GitHub.com Fallback** ⚠️