feat: fix up bulk upload
This commit is contained in:
parent
b7e8a4246b
commit
9f974d2a45
3 changed files with 36 additions and 23 deletions
|
|
@ -8,10 +8,12 @@
|
||||||
"format": "prettier --write '**/*.{ts,js,css,json,md}'",
|
"format": "prettier --write '**/*.{ts,js,css,json,md}'",
|
||||||
"deploy": "wrangler publish",
|
"deploy": "wrangler publish",
|
||||||
"dev": "wrangler dev",
|
"dev": "wrangler dev",
|
||||||
"addsecret": "wrangler secret put WORKERLINKS_SECRET"
|
"addsecret": "wrangler secret put WORKERLINKS_SECRET",
|
||||||
|
"build": "wrangler publish --dry-run --outdir=./dist"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"hono": "^3.0.2"
|
"hono": "^3.0.2",
|
||||||
|
"simple-runtypes": "^7.1.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@cloudflare/workers-types": "^4.20230228.0",
|
"@cloudflare/workers-types": "^4.20230228.0",
|
||||||
|
|
|
||||||
48
src/index.ts
48
src/index.ts
|
|
@ -1,4 +1,5 @@
|
||||||
import { Context, Hono } from 'hono'
|
import { Context, Hono } from 'hono'
|
||||||
|
import * as st from 'simple-runtypes'
|
||||||
|
|
||||||
type Variables = {
|
type Variables = {
|
||||||
path: string
|
path: string
|
||||||
|
|
@ -12,9 +13,23 @@ type Bindings = {
|
||||||
kv: KVNamespace
|
kv: KVNamespace
|
||||||
}
|
}
|
||||||
|
|
||||||
type BulkUpload = {
|
const url = st.runtype((v) => {
|
||||||
[id: string]: string
|
const stringCheck = st.use(st.string(), v)
|
||||||
}
|
if (!stringCheck.ok) return stringCheck.error
|
||||||
|
|
||||||
|
try {
|
||||||
|
new URL(stringCheck.result)
|
||||||
|
return stringCheck.result
|
||||||
|
} catch {
|
||||||
|
return st.createError('Must be a valid URL')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const bulkValidator = st.dictionary(
|
||||||
|
// Starting `/` and no ending `/`
|
||||||
|
st.string({ match: /^\/.*?[^\/]$/ }),
|
||||||
|
url,
|
||||||
|
)
|
||||||
|
|
||||||
const app = new Hono<{ Variables: Variables; Bindings: Bindings }>()
|
const app = new Hono<{ Variables: Variables; Bindings: Bindings }>()
|
||||||
|
|
||||||
|
|
@ -112,26 +127,18 @@ app.put('*', createLink)
|
||||||
|
|
||||||
// add random key
|
// add random key
|
||||||
app.post('/', async (c) => {
|
app.post('/', async (c) => {
|
||||||
const body = await c.req.text()
|
const rawBody = await c.req.text()
|
||||||
|
|
||||||
if (!body) {
|
if (!rawBody) {
|
||||||
c.set('key', '/' + Math.random().toString(36).slice(5))
|
c.set('key', '/' + Math.random().toString(36).slice(5))
|
||||||
c.set('shortUrl', new URL(c.get('key'), c.req.url).toString())
|
c.set('shortUrl', new URL(c.get('key'), c.req.url).toString())
|
||||||
return await createLink(c)
|
return await createLink(c)
|
||||||
} else {
|
} else {
|
||||||
// bulk upload from body
|
// Bulk upload from body
|
||||||
|
|
||||||
// REMOVE THIS when bulk upload has been migrated
|
let json: unknown
|
||||||
|
|
||||||
return c.json({
|
|
||||||
code: '501 Not Implemented',
|
|
||||||
message: 'Bulk upload has not been reimplemented yet.',
|
|
||||||
})
|
|
||||||
|
|
||||||
// what type should this be???
|
|
||||||
let json: any
|
|
||||||
try {
|
try {
|
||||||
json = JSON.parse(body)
|
json = JSON.parse(rawBody)
|
||||||
} catch {
|
} catch {
|
||||||
return c.json(
|
return c.json(
|
||||||
{
|
{
|
||||||
|
|
@ -142,9 +149,8 @@ app.post('/', async (c) => {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// change this
|
const result = st.use(bulkValidator, json)
|
||||||
const valid = true //validateBulkBody(json);
|
if (!result.ok) {
|
||||||
if (!valid) {
|
|
||||||
return c.json(
|
return c.json(
|
||||||
{
|
{
|
||||||
code: '400 Bad Request',
|
code: '400 Bad Request',
|
||||||
|
|
@ -158,14 +164,14 @@ app.post('/', async (c) => {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const [key, url] of json.entries) {
|
for (const [key, url] of Object.entries(result.result)) {
|
||||||
await c.env.KV.put(key, url)
|
await c.env.KV.put(key, url)
|
||||||
}
|
}
|
||||||
|
|
||||||
return Response.json(
|
return Response.json(
|
||||||
{
|
{
|
||||||
message: 'URLs created successfully',
|
message: 'URLs created successfully',
|
||||||
entries: Object.entries(json).map(([key, longurl]) => ({
|
entries: Object.entries(result.result).map(([key, longurl]) => ({
|
||||||
key: key.slice(1),
|
key: key.slice(1),
|
||||||
shorturl: new URL(key, c.req.url),
|
shorturl: new URL(key, c.req.url),
|
||||||
longurl,
|
longurl,
|
||||||
|
|
|
||||||
|
|
@ -744,6 +744,11 @@ signal-exit@^3.0.7:
|
||||||
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.7.tgz#a9a1767f8af84155114eaabd73f99273c8f59ad9"
|
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.7.tgz#a9a1767f8af84155114eaabd73f99273c8f59ad9"
|
||||||
integrity sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==
|
integrity sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==
|
||||||
|
|
||||||
|
simple-runtypes@^7.1.3:
|
||||||
|
version "7.1.3"
|
||||||
|
resolved "https://registry.yarnpkg.com/simple-runtypes/-/simple-runtypes-7.1.3.tgz#0c9362016ae385995499e85c00dbd4fef3af9769"
|
||||||
|
integrity sha512-NDxAtKPHQplTyfdt2nAUuTV7RVwEBIRFZT7Z+g4eso5JGI5AwllpRcU9ibmxs+8vKKNrhebgK5zvCX3j8DoHhw==
|
||||||
|
|
||||||
source-map-support@^0.5.20:
|
source-map-support@^0.5.20:
|
||||||
version "0.5.21"
|
version "0.5.21"
|
||||||
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.21.tgz#04fe7c7f9e1ed2d662233c28cb2b35b9f63f6e4f"
|
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.21.tgz#04fe7c7f9e1ed2d662233c28cb2b35b9f63f6e4f"
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue