generated from drivly/worker.templates.do
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
43 lines (36 loc) · 1.34 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
export const api = {
icon: '🚀',
name: 'jsn.do',
description: 'JSON URL to JSON body converter.',
url: 'https://jsn.do/api',
type: 'https://apis.do/templates',
endpoints: {
convert: 'https://jsn.do/:jsonBody'
},
site: 'https://jsn.do',
login: 'https://jsn.do/login',
signup: 'https://jsn.do/signup',
subscribe: 'https://jsn.do/subscribe',
repo: 'https://github.com/drivly/jsn.do',
}
export const gettingStarted = [
`If you don't already have a JSON Viewer Browser Extension, get that first:`,
`https://extensions.do`,
]
export const examples = {
listItems: 'https://jsn.do/{ "hello": "world", "true": false }',
}
export default {
fetch: async (req, env) => {
const { user, hostname, pathname, rootPath, pathSegments, query } = await env.CTX.fetch(req).then(res => res.json())
if (rootPath) return json({ api, gettingStarted, examples, user })
let data
try {
data = JSON.parse(pathSegments.join('/')) // Converts all path segments ignoring slashes
} catch (e) {
return new Response('{ "success": false, "error": "Failed to parse JSON." }', { headers: { ContentType: 'application/json; charset=utf-8' }, status: 400 })
}
return json(data)
}
}
const json = obj => new Response(JSON.stringify(obj, null, 2), { headers: { 'content-type': 'application/json; charset=utf-8' }})