aboutsummaryrefslogtreecommitdiff
path: root/src/client/parenscript.lisp
blob: 141c9cd82a19471ae2b91e0858477559484370c2 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
(defpackage #:lazybones/client.parenscript
  (:use #:cl)
  (:local-nicknames (#:lzb #:lazybones)
                    (#:a #:alexandria))
  (:export #:generate
           #:generate-js))

(in-package :lazybones/client.parenscript)

(defun lispify (str)
  #+testiere
  (:tests
   (equalp (lispify "a.b") "a-b")
   (equalp (lispify "a/b") "a-b"))
  (with-output-to-string (out)
    (loop :for c :across str :do
      (write-char
       (if (find c "./") #\- c)
       out))))

(defun client-function-name (method pattern)
  "Return a symbol; the name of the parenscript function that will make
an HTTP request of type METHOD at the path implied by PATTERN. PATTERN
is the value of the endpoint's DISPATCH-PATTERN slot."
  #+testiere
  (:tests
   (string-equal
    (client-function-name
     :get '("" "session" ("doesn't" :matter) "notifications"))
    'get-session-notifications)
   (string-equal
    (client-function-name
     :post '("" "foo.bar" "moo"))
    'post-foo-bar-moo))

  (intern
   (lispify
    (string-upcase
     (format nil "~a-~{~a~^-~}"
             method
             (remove-if-not (a:conjoin #'stringp (complement #'a:emptyp))
                            pattern))))))

(defun client-function-lambda-list (variables &optional body-vars)
  #+testiere
  (:tests
   (equalp
    (client-function-lambda-list '("*SESSION*"))
    '(*session*))

   (equalp
    (client-function-lambda-list '("a.x" "b/y" "c") '(q r))
    '(a-x b-y c &key q r))

   (equalp
    (client-function-lambda-list '("pl" "foo") '(title url))
    '(pl foo &key title url)))

  (nconc (mapcar (a:compose #'intern #'string-upcase #'lispify) variables)
         (when body-vars (cons '&key body-vars))))

(defun client-function-endpoint-path (prefix pattern)
  #+testiere
  (:tests
   (equalp
    (client-function-endpoint-path
     "/api" '("" "session" ("id" :blahblah) "notifications"))
    '(+ "/api" "/" "session" "/"  id "/" "notifications")))
  (list* '+ prefix
         (loop :for x :in pattern
               :unless (zerop (length x))
                 :collect "/"
                 :and :collect (if (consp x)
                                   (intern (string-upcase (first x)))
                                   x))))

(defun method-accepts-body? (m)
  (member m '(:post :put :patch)))

(defun generate-endpoint-form (app ep)
  (let* ((method
           (lzb:endpoint-method ep))

         (method-name
           (symbol-name method))

         (content-type
           (or (lzb:endpoint-content-type ep)
               (lzb::default-content-type app)))

         (pattern
           (lzb:endpoint-dispatch-pattern ep))

         (variables
           (lzb:endpoint-route-variables ep))

         (function-name
           (client-function-name method pattern))

         (body-vars
           (lzb:endpoint-body-variables ep))

         (lambda-list
           (client-function-lambda-list variables body-vars))

         (body-serializtion-code
           (when body-vars
             (cond ((equal "application/json" content-type)
                    `(ps:chain
                      -j-s-o-n
                      (stringify (ps:create
                                  ,@(loop :for var :in body-vars
                                          :collect var
                                          :collect var)))))

                   ((equal "multipart/form-data" content-type)
                    (let ((fd (gensym "FORMDATA")))
                      `((lambda ()
                          (let ((,fd (new -form-data)))
                            ,@(loop :for v :in body-vars
                                    :for s := (string-downcase (symbol-name v))
                                    :collect `(ps:chain ,fd (append ,s ,v)))
                            ,fd)))))

                   (t (error "Cannot automatically encode bodies for ~s"
                             content-type)))))

         (fetch-path
           (client-function-endpoint-path (lzb::app-prefix app) pattern)))
    
    `(defun ,function-name ,lambda-list
       ,(if body-vars
            `(fetch ,fetch-path
                    (ps:create
                     method ,method-name
                     headers  (ps:create "Content-Type" ,content-type)
                     redirect "follow"
                     body ,body-serializtion-code))
            `(fetch ,fetch-path)))))

(defun generate (app)
  (let* ((module
           (gensym "MODULE"))

         (module-name
           (intern (lispify (concatenate 'string "-" (symbol-name (lzb::app-name app))))))

         (defuns
           (loop :for ep :in (lzb:app-endpoints app)
                 :collect (generate-endpoint-form app ep)))

         (exports
           (loop :for defun :in defuns
                 :for name := (second defun)
                 :collect `(ps:@ ,module ,name)
                 :collect name)))
    
    `(defvar ,module-name
       ((lambda (,module)
          ,@defuns
          (setf ,@exports)
          ,module)
        (ps:create)))))

(defun generate-js (app)
  (eval `(ps:ps ,(generate app))))