generated from drivly/worker.templates.do
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
99 lines (84 loc) · 2.77 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
export const api = {
icon: '🚀',
name: 'sort.do',
description: 'Cloudflare Worker Template',
url: 'https://sort.do/api',
type: 'https://apis.do/data',
endpoints: {
listCategories: 'https://sort.do/api',
getCategory: 'https://sort.do/:type',
},
site: 'https://sort.do',
login: 'https://sort.do/login',
signup: 'https://sort.do/signup',
subscribe: 'https://sort.do/subscribe',
repo: 'https://github.com/drivly/sort.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://sort.do/worker',
}
export default {
fetch: async (req, env) => {
const search = req.url.split('?')[1]
const { user, hostname, pathname, rootPath, pathSegments, query } = await env.CTX.fetch(req).then(res => res.json())
if (rootPath) return json({ api, gettingStarted, examples, user })
if (pathname.includes('favicon')) return new Response(null, { status: 302, headers: { location: 'https://uploads-ssl.webflow.com/60bee04bdb1a7a33432ce295/60ca2dd82fe6f273c60220ae_favicon_drivly.png' } })
const [opt, ...url] = pathSegments
const options = {
field: '',
direction: 'dsc',
}
// opt will be the field name we need to sort by (e.g. "name")
// and optionally the direction. (e.g. "name&asc")
if (opt) {
opt.split('&').forEach(p => {
if (p === 'asc' || p === 'desc' || p === 'dsc') options.direction = p === 'asc' ? 'asc' : 'dsc' // Normalize to dsc
else options.field = p
})
}
let resp
try {
resp = await fetch(
`https://${url.join('/')}?${search}`
)
} catch (e) {
// If this is a fetcherror, we may have consumed the domain via the options.
resp = await fetch(
`https://${options.field}/${url.join('/')}?${search}`
)
options.field = ''
}
const data = await resp.json()
// check if the data is an array
if (!Array.isArray(data)) {
return json({
api,
data: {
success: false,
error: 'Data is not an array',
},
user
}, {
status: 400
})
}
// sort the data
const sorted = data.sort((a, b) => {
const aVal = options.field ? a[options.field] : a // if no field is specified, sort by the entire object
const bVal = options.field ? b[options.field] : b
if (aVal < bVal) return options.direction === 'asc' ? -1 : 1
if (aVal > bVal) return options.direction === 'asc' ? 1 : -1
return 0
})
return json({
api,
data: sorted,
user
})
}
}
const json = (obj,opt) => new Response(JSON.stringify(obj, null, 2), { headers: { 'content-type': 'application/json; charset=utf-8' }, ...opt })