Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(js): implement fetch api for js runtime #69

Merged
merged 15 commits into from
Mar 6, 2024
Merged

Conversation

shouya
Copy link
Owner

@shouya shouya commented Mar 6, 2024

This PR is to implement a simple fetch api for the JS runtime. It enables the user to request data from servers within any JavaScript code.

Here's the API:

type RequestParam = {
  method: "GET" | "POST" | "PUT" | "DELETE",
  headers: { [key: string]: string },
  body: string?
}

fetch(url: string, params?: RequestParam): Promise<Response>;

class Response {
  status: number,
  headers: { [key: string]: string },
  body: string | null

  json(): Object
}

Here's a sample endpoint that returns de-clickbait-ed youtube feed using DeArrow service:

  - path: /kurtzgesagt-dearrow.xml
    source: https://www.youtube.com/feeds/videos.xml?channel_id=UCsXVk37bltHxD1rDPwtNM8Q
    filters:
      - modify_post: |
          const video_id = post.id.split(':').pop();
          let resp = await fetch(`https://sponsor.ajay.app/api/branding?videoID=${video_id}`);
          if (resp.status == 200) {
            let [title] = resp.json().titles;
            post.title.value = title.title;
          }
          let thumbnail = post.extensions.media.group.filter(g => g.name == 'media:group')[0].children.thumbnail[0];
          thumbnail.attrs.url = `https://dearrow-thumb.ajay.app/api/v1/getThumbnail?videoID=${video_id}`;

(Fixes #41)

A few more subtle changes:

  • errors are handled and displayed more nicely: instead of throwing the error's internal struct at the user, now we display the error message instead
  • js syntax errors will now show the code context
  • exceptions raised within modify_post are now reported and ignored, so they won't break the feed
  • both modify_post and modify_feed can be defined as async functions
  • the modify_post function are now called concurrently for each post, this is especially useful when the function contains long-time async operations.

@shouya shouya merged commit a4b3fe9 into master Mar 6, 2024
2 checks passed
@shouya shouya deleted the fetch-api-in-js branch March 6, 2024 11:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support DeArrow for YouTube
1 participant