DEV Community

artydev
artydev

Posted on

1 1

DEV Api a practical use case of streams

For fun here is a simple example using streams, no framework, vanilla JS

You can test it here DEV API

function stream(v) {
    let mappers = []
    let storedvalue = v

    function _stream(v = storedvalue) {
        if (v) storedvalue = v;
        mappers.map((f) => v && f(v))
        return storedvalue;
    }
    _stream.map = function(f) {
        mappers.push(f)
    }
    return _stream
}

let currentusr = "artydev"

const users = ["artydev", "dailydevtips1"]

// search user from input box
window.searchInput = () => {
    user(searchuser.value);
    currentusr = searchuser.value
}

// search user from list
window.searchUser = (usr) => {
    user(usr);
    currentusr = usr
}

// url API
function url(user) {
    return `https://dev.to/api/articles?username=${user}&per_page=1000`
}

function listUsers() {
    return `
    <ul style="text-align:left; margin-left: 10px; margin-top:10px;">
      ${users.map(u => `<li class="user" onclick="searchUser('${u}')">${u}</li>`).join("")}
    </ul>
  `
}

async function getPosts(user) {
    const response = await fetch(url(user))
    const json = await response.json()
    return json
}

function inputSearch() {
    return `<input id="searchuser" />
  `
}

function displayPost(post) {
    return `
    <li class="post">
      <div class="headpost">
        <a href="${post.url}">${post.title}</a>
      </div>
      <div class="description">
        <em>${post.description}</em>
      </div>
    </li>
  `
}

function listPosts(posts) {
    const view = `
    <ul>
      ${posts.map(displayPost).join('')}
    </ul>
  `
    return view
}

function App(posts) {
    document.body.innerHTML = `
    <div class="title">
      <h3>Dev to Posts  -  ${currentusr} ${posts.length}  articles </h3>
    </div>
    <div class="searchbar">
        <div>
          ${inputSearch()}
          <button onclick="searchInput()">search user posts</button>
        </div>
      </div>
    <div class="container">
      <div>${listPosts(posts)}</div>
      <div class="users">
        <h4>Users</h4>
        <div>${listUsers()}</div>
    </div>
  `
}

const user = stream()

user.map((usr) => {
    console.log(usr)
    getPosts(usr).then(App)
})

user(currentusr)

API Trace View

Struggling with slow API calls? πŸ‘€

Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more β†’

Top comments (0)

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay