Skip to content

Instantly share code, notes, and snippets.

@tonyspiro
Last active May 14, 2020 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonyspiro/70e176a4c314757db96a2f165252e305 to your computer and use it in GitHub Desktop.
Save tonyspiro/70e176a4c314757db96a2f165252e305 to your computer and use it in GitHub Desktop.
Get Cosmic content using Deno and fetch https://cosmicjs.com
/* To run
1. Follow Deno Guide to install https://deno.land/#installation
2. Add the following code to a file called cosmic-deno-test.ts
3. run the following command: deno run --allow-net=127.0.0.1:8000,api.cosmicjs.com cosmic-deno-test.ts
*/
import { serve } from "https://deno.land/std@0.50.0/http/server.ts";
const s = serve({ port: 8000 });
const bucket_slug = `cosmic-js`;
const object_type = `articles`;
const limit = 10;
const url = `https://api.cosmicjs.com/v1/${bucket_slug}/objects?type=${object_type}&limit=${limit}&props=title`
const data = await fetch(url).then(response => response.json())
for await (const req of s) {
req.respond({ body: JSON.stringify(data, null, 2) });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment