cohost

Unofficial Common Lisp client library for Cohost
git clone https://todayiwilllaunchmyinfantsonintoorbit.com/cohost.git
Log | Files | Refs | LICENSE

commit ac1d1528e797d7e371f180365301042c8d77fbd1
parent 82a025dde0d646d497746d9ef2267538ea89bc22
Author: Decay <decay@todayiwilllaunchmyinfantsonintoorbit.com>
Date:   Mon, 21 Nov 2022 21:03:28 -0800

Alter COHOST-TRPC-BATCH to accomodate a method of specifying JS false

This is required for tRPC requests that have required booleans like
posts.profilePosts

Diffstat:
Msrc/client-v1-impl.lisp | 1+
Msrc/rpc.lisp | 18+++++++++++++-----
2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/src/client-v1-impl.lisp b/src/client-v1-impl.lisp @@ -148,5 +148,6 @@ (define-trpc-request "users.displayPrefs" ()) (define-trpc-request "posts.isLiked" (:atom)) (define-trpc-request "posts.getPost" (:atom)) +(define-trpc-request "posts.profilePosts" ("projectHandle" "page" "options")) (define-trpc-request "posts.getPostsTagged" ("projectHandle" "tagSlug")) (define-trpc-request "posts.byProject" ("projectHandle" "page")) diff --git a/src/rpc.lisp b/src/rpc.lisp @@ -50,16 +50,24 @@ Each batch item is a CONS structured as ([request] . [input data]). For requests if (cdr pair) collect (cons idx (cdr pair)) into input-items finally (return (values batch-items input-items))) - (let ((batch (apply #'concatenate (append (list 'string "trpc/") batch-items))) - (batch-input (encode-json-alist-to-string input-items))) - (cohost-rpc client batch :get :batch "1" :input batch-input)))) + (let* ((batch (apply #'concatenate (append (list 'string "trpc/") batch-items))) + (json::+json-lisp-symbol-tokens+ '(("true" . T) + ("null" . nil) + ("false" . nil) + ("false" . :false))) + (batch-input (encode-json-alist-to-string input-items))) + (cohost-rpc client batch :get :batch "1" :input batch-input)))) (defmacro define-trpc-request (name (&rest lambda-list)) "Macro for defining tRPC request objects. Usage: (DEFINE-TRPC-REQUEST [String name of request, eg \"login.loggedIn\"] ((NIL for requests with no parameters, :atom for requests that take a single atomic value, eg a post ID, or a list of string names of parameters for requests that take JSON input)) -As an example (DEFINE-TRPC-REQUEST \"foo.wibbleFrotz\" ()) would create a function named FOO.WIBBLE-FROTZ that takes no parameters and returns a CONS (\"foo.wibbleFrotz\" . NIL). " +As an example (DEFINE-TRPC-REQUEST \"foo.wibbleFrotz\" ()) would create a function named FOO.WIBBLE-FROTZ that takes no parameters and returns a CONS (\"foo.wibbleFrotz\" . NIL). + +Functions created this way that construct a JSON input can take keyword :FALSE to express JSON false unambiguously. For instance: +(DEFINE-TRPC-REQUEST \"test.bool\" (\"booleanParam\")) +would create a function TEST.BOOL that takes a single parameter. (TEST.BOOL NIL) will result in a null value, {\"booleanParam\":null}, while (TEST.BOOL :FALSE) will emit a Javascript false, {\"booleanParam\":false}. This is clunky but unfortunately necessary as Javascript has different ideas of falsity and nullity than CL." (labels ((bomb () (error "Invalid lambda list for tRPC request (~A)" lambda-list)) (create-ll-and-datum (raw-ll) @@ -69,7 +77,7 @@ As an example (DEFINE-TRPC-REQUEST \"foo.wibbleFrotz\" ()) would create a functi collect `(cons ,param ,sym) into a-list if (not (stringp param)) do (bomb) - finally (return (cons lambda-list `(json:encode-json-to-string (list ,@a-list))))))) + finally (return (cons lambda-list `(list ,@a-list)))))) (let* ((lisp-name (intern (json:camel-case-to-lisp name))) (data-sym (gensym)) (ll-and-datum (cond