/ parenscript-utils.lisp
parenscript-utils.lisp
 1  ;;;; parenscript-utils.lisp
 2  ;;;; Utilities for generating JavaScript from ParenScript
 3  
 4  (in-package :asteroid)
 5  
 6  ;;; ParenScript compilation utilities
 7  
 8  (defun compile-ps-to-js (ps-code)
 9    "Compile ParenScript code to JavaScript string"
10    (ps:ps* ps-code))
11  
12  (defmacro define-js-route (name (&rest args) &body parenscript-body)
13    "Define a route that serves compiled ParenScript as JavaScript"
14    `(define-page ,name (,@args)
15       (:content-type "application/javascript")
16       (ps:ps ,@parenscript-body)))
17  
18  ;;; Common ParenScript macros and utilities
19  
20  (defmacro ps-defun (name args &body body)
21    "Define a ParenScript function"
22    `(ps:defun ,name ,args ,@body))
23  
24  (defmacro ps-api-call (endpoint method data success-callback error-callback)
25    "Generate ParenScript for making API calls with fetch"
26    `(ps:ps
27       (fetch ,endpoint
28              (ps:create :method ,method
29                         :headers (ps:create "Content-Type" "application/json")
30                         :body (ps:chain -j-s-o-n (stringify ,data))))
31       (then (lambda (response) (ps:chain response (json))))
32       (then ,success-callback)
33       (catch ,error-callback)))