-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
29 lines (27 loc) · 1.12 KB
/
worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { get } from 'lodash'
export const api = {
icon: '👌',
name: 'pluck.do',
description: 'Pluck a Property from a JSON Document',
url: 'https://pluck.do/api',
type: 'https://apis.do/data',
endpoints: {
pluckFromURL: 'https://pluck.do/:property/:url',
pluckFromPOST: 'https://pluck.do/:property',
},
site: 'https://pluck.do',
login: 'https://pluck.do/login',
signup: 'https://pluck.do/signup',
repo: 'https://github.com/drivly/pluck.do',
}
export default {
fetch: async (req, env) => {
const { hostname, pathname, search } = new URL(req.url)
if (pathname == '/api') return new Response(JSON.stringify({api}, null, 2), { headers: { 'content-type': 'application/json; charset=utf-8' }})
const [_,prop, ...rest] = pathname.split('/')
const url = 'https://' + rest.join('/') + search
const data = await fetch(url, req).then(res => res.json()).catch(({ name, message, stack }) => ({ error: { name, message, stack }}))
const pluckedData = get(data, prop)
return new Response(JSON.stringify(pluckedData, null, 2), { headers: { 'content-type': 'application/json; charset=utf-8' }})
},
}