Last active
May 14, 2020 03:49
-
-
Save tonyspiro/70e176a4c314757db96a2f165252e305 to your computer and use it in GitHub Desktop.
Get Cosmic content using Deno and fetch https://cosmicjs.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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