@rip-lang/server 1.3.7 → 1.3.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/api.rip +26 -4
- package/package.json +1 -1
package/api.rip
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
# Server: start, startHandler, fetch, App
|
|
11
11
|
# Filters: raw, before, after
|
|
12
12
|
# Handlers: onError, notFound
|
|
13
|
+
# Errors: error!, notice!, bail!
|
|
13
14
|
# Validation: read, validators, registerValidator, getValidator
|
|
14
15
|
# Context: ctx, session, env, subrequest
|
|
15
16
|
# Utilities: isBlank, toName, toPhone, mimeType
|
|
@@ -184,7 +185,7 @@ createContext = (req, params = {}) ->
|
|
|
184
185
|
else n
|
|
185
186
|
else parseInt(duration) or 0
|
|
186
187
|
else 0
|
|
187
|
-
out.set 'Cache-Control', "public, max-age=#{seconds}"
|
|
188
|
+
out.set 'Cache-Control', "public, max-age=#{seconds}, immutable"
|
|
188
189
|
return
|
|
189
190
|
|
|
190
191
|
session: {}
|
|
@@ -271,8 +272,12 @@ smart = (fn) ->
|
|
|
271
272
|
catch err
|
|
272
273
|
status = err?.status or 500
|
|
273
274
|
console.error 'Handler error:', err if status >= 500
|
|
274
|
-
|
|
275
|
-
|
|
275
|
+
if err?.notice
|
|
276
|
+
body = JSON.stringify { error: { notice: err.notice } }
|
|
277
|
+
new Response body, { status, headers: { 'Content-Type': 'application/json' } }
|
|
278
|
+
else
|
|
279
|
+
message = err?.message or 'Internal Server Error'
|
|
280
|
+
new Response message, { status, headers: { 'Content-Type': 'text/plain' } }
|
|
276
281
|
|
|
277
282
|
# ==============================================================================
|
|
278
283
|
# DSL: Route Registration
|
|
@@ -449,6 +454,23 @@ export session = new Proxy {},
|
|
|
449
454
|
# Env proxy — access env.FOO anywhere (shortcut for process.env)
|
|
450
455
|
export env = new Proxy {}, get: (_, key) -> process.env[key]
|
|
451
456
|
|
|
457
|
+
# ==============================================================================
|
|
458
|
+
# Error Helpers — Halt request with an HTTP error
|
|
459
|
+
# ==============================================================================
|
|
460
|
+
|
|
461
|
+
export error = (message = 'Bad request', status = 400) ->
|
|
462
|
+
throw Object.assign(new Error(message), { status })
|
|
463
|
+
|
|
464
|
+
export notice = (message = 'Something went wrong', status = 400) ->
|
|
465
|
+
throw Object.assign(new Error(message), { status, notice: message })
|
|
466
|
+
|
|
467
|
+
export bail = ->
|
|
468
|
+
store = requestContext.getStore()
|
|
469
|
+
if store?.env?.session?
|
|
470
|
+
for key of store.env.session
|
|
471
|
+
delete store.env.session[key]
|
|
472
|
+
throw Object.assign(new Error('Forbidden'), { status: 403 })
|
|
473
|
+
|
|
452
474
|
# ==============================================================================
|
|
453
475
|
# Utility Functions
|
|
454
476
|
# ==============================================================================
|
|
@@ -607,7 +629,7 @@ export read = (name = null, type = null, miss = null) ->
|
|
|
607
629
|
# missing value helper
|
|
608
630
|
done = (must = false) ->
|
|
609
631
|
return miss() if typeof miss is 'function'
|
|
610
|
-
throw new Error
|
|
632
|
+
throw Object.assign(new Error("Missing required field: #{name}"), { status: 400 }) if must
|
|
611
633
|
return miss ?? null
|
|
612
634
|
|
|
613
635
|
# get value from store
|