fix: methods dont carry data

This commit is contained in:
Orion Kindel 2024-12-01 17:21:41 -06:00
parent 15e15aa921
commit 38cd701737
Signed by untrusted user who does not match committer: orion
GPG Key ID: 6D4165AE4C928719
6 changed files with 127 additions and 130 deletions

View File

@ -2,70 +2,70 @@
[ [
"Axon Request Parts Body extracts a JSON body", "Axon Request Parts Body extracts a JSON body",
{ {
"timestamp": "1733094319834.0", "timestamp": "1733095274452.0",
"success": true "success": true
} }
], ],
[ [
"Axon Request Parts Body extracts a string body from a buffer", "Axon Request Parts Body extracts a string body from a buffer",
{ {
"timestamp": "1733094319834.0", "timestamp": "1733095274452.0",
"success": true "success": true
} }
], ],
[ [
"Axon Request Parts Body extracts a string body from a cached string", "Axon Request Parts Body extracts a string body from a cached string",
{ {
"timestamp": "1733094319834.0", "timestamp": "1733095274452.0",
"success": true "success": true
} }
], ],
[ [
"Axon Request Parts Body extracts a string body from a readable stream", "Axon Request Parts Body extracts a string body from a readable stream",
{ {
"timestamp": "1733094319834.0", "timestamp": "1733095274452.0",
"success": true "success": true
} }
], ],
[ [
"Axon Request Parts Path ... but does if ends in IgnoreRest", "Axon Request Parts Path ... but does if ends in IgnoreRest",
{ {
"timestamp": "1733094319834.0", "timestamp": "1733095274452.0",
"success": true "success": true
} }
], ],
[ [
"Axon Request Parts Path does not partially match a route ...", "Axon Request Parts Path does not partially match a route ...",
{ {
"timestamp": "1733094319834.0", "timestamp": "1733095274452.0",
"success": true "success": true
} }
], ],
[ [
"Axon Request Parts Path extracts an int", "Axon Request Parts Path extracts an int",
{ {
"timestamp": "1733094319834.0", "timestamp": "1733095274452.0",
"success": true "success": true
} }
], ],
[ [
"Axon Request Parts Path extracts an int and a string", "Axon Request Parts Path extracts an int and a string",
{ {
"timestamp": "1733094319834.0", "timestamp": "1733095274452.0",
"success": true "success": true
} }
], ],
[ [
"Axon Request Parts Path matches a route matching literal", "Axon Request Parts Path matches a route matching literal",
{ {
"timestamp": "1733094319834.0", "timestamp": "1733095274452.0",
"success": true "success": true
} }
], ],
[ [
"Axon Request Parts Path matches a route matching multiple literals", "Axon Request Parts Path matches a route matching multiple literals",
{ {
"timestamp": "1733094319834.0", "timestamp": "1733095274452.0",
"success": true "success": true
} }
], ],
@ -100,14 +100,14 @@
[ [
"Axon Request Parts extracts method, path, JSON body", "Axon Request Parts extracts method, path, JSON body",
{ {
"timestamp": "1733094319834.0", "timestamp": "1733095274452.0",
"success": true "success": true
} }
], ],
[ [
"Axon Request Parts extracts the whole request", "Axon Request Parts extracts the whole request",
{ {
"timestamp": "1733094319834.0", "timestamp": "1733095274452.0",
"success": true "success": true
} }
], ],

View File

@ -14,28 +14,45 @@
{ {
"argonaut-core": ">=7.0.0 <8.0.0" "argonaut-core": ">=7.0.0 <8.0.0"
}, },
"arraybuffer-types",
"arrays",
"bifunctors",
{ {
"console": ">=6.1.0 <7.0.0" "arraybuffer-types": ">=3.0.2 <4.0.0"
},
{
"arrays": ">=7.3.0 <8.0.0"
},
{
"bifunctors": ">=6.0.0 <7.0.0"
},
{
"control": ">=6.0.0 <7.0.0"
}, },
"control",
{ {
"effect": ">=4.0.0 <5.0.0" "effect": ">=4.0.0 <5.0.0"
}, },
"either", {
"exceptions", "either": ">=6.1.0 <7.0.0"
},
{
"exceptions": ">=6.1.0 <7.0.0"
},
{ {
"ezfetch": ">=1.1.0 <2.0.0" "ezfetch": ">=1.1.0 <2.0.0"
}, },
"foldable-traversable", {
"integers", "foldable-traversable": ">=6.0.0 <7.0.0"
},
{
"integers": ">=6.0.0 <7.0.0"
},
{ {
"maybe": ">=6.0.0 <7.0.0" "maybe": ">=6.0.0 <7.0.0"
}, },
"newtype", {
"node-buffer", "newtype": ">=5.0.0 <6.0.0"
},
{
"node-buffer": ">=9.0.0 <10.0.0"
},
{ {
"node-net": ">=5.1.0 <6.0.0" "node-net": ">=5.1.0 <6.0.0"
}, },
@ -45,11 +62,15 @@
{ {
"nullable": ">=6.0.0 <7.0.0" "nullable": ">=6.0.0 <7.0.0"
}, },
"ordered-collections", {
"ordered-collections": ">=3.2.0 <4.0.0"
},
{ {
"prelude": ">=6.0.1 <7.0.0" "prelude": ">=6.0.1 <7.0.0"
}, },
"refs", {
"refs": ">=6.0.0 <7.0.0"
},
{ {
"strings": ">=6.0.1 <7.0.0" "strings": ">=6.0.1 <7.0.0"
}, },
@ -59,7 +80,9 @@
{ {
"tuples": ">=7.0.0 <8.0.0" "tuples": ">=7.0.0 <8.0.0"
}, },
"typelevel-prelude", {
"typelevel-prelude": ">=7.0.0 <8.0.0"
},
{ {
"url-immutable": ">=1.0.0 <2.0.0" "url-immutable": ">=1.0.0 <2.0.0"
}, },
@ -76,7 +99,6 @@
"arrays", "arrays",
"b64", "b64",
"bifunctors", "bifunctors",
"console",
"const", "const",
"contravariant", "contravariant",
"control", "control",

View File

@ -1,33 +1,33 @@
package: package:
name: axon name: axon
dependencies: dependencies:
- arraybuffer-types
- arrays
- bifunctors
- control
- either
- exceptions
- foldable-traversable
- integers
- newtype
- node-buffer
- ordered-collections
- refs
- typelevel-prelude
- aff: '>=8.0.0 <9.0.0' - aff: '>=8.0.0 <9.0.0'
- argonaut-codecs: '>=9.1.0 <10.0.0' - argonaut-codecs: '>=9.1.0 <10.0.0'
- argonaut-core: '>=7.0.0 <8.0.0' - argonaut-core: '>=7.0.0 <8.0.0'
- arraybuffer-types: '>=3.0.2 <4.0.0'
- arrays: '>=7.3.0 <8.0.0'
- bifunctors: '>=6.0.0 <7.0.0'
- console: '>=6.1.0 <7.0.0' - console: '>=6.1.0 <7.0.0'
- control: '>=6.0.0 <7.0.0'
- effect: '>=4.0.0 <5.0.0' - effect: '>=4.0.0 <5.0.0'
- either: '>=6.1.0 <7.0.0'
- exceptions: '>=6.1.0 <7.0.0'
- ezfetch: '>=1.1.0 <2.0.0' - ezfetch: '>=1.1.0 <2.0.0'
- foldable-traversable: '>=6.0.0 <7.0.0'
- integers: '>=6.0.0 <7.0.0'
- maybe: '>=6.0.0 <7.0.0' - maybe: '>=6.0.0 <7.0.0'
- newtype: '>=5.0.0 <6.0.0'
- node-buffer: '>=9.0.0 <10.0.0'
- node-net: '>=5.1.0 <6.0.0' - node-net: '>=5.1.0 <6.0.0'
- node-streams: '>=9.0.0 <10.0.0' - node-streams: '>=9.0.0 <10.0.0'
- nullable: '>=6.0.0 <7.0.0' - nullable: '>=6.0.0 <7.0.0'
- ordered-collections: '>=3.2.0 <4.0.0'
- prelude: '>=6.0.1 <7.0.0' - prelude: '>=6.0.1 <7.0.0'
- refs: '>=6.0.0 <7.0.0'
- strings: '>=6.0.1 <7.0.0' - strings: '>=6.0.1 <7.0.0'
- transformers: '>=6.1.0 <7.0.0' - transformers: '>=6.1.0 <7.0.0'
- tuples: '>=7.0.0 <8.0.0' - tuples: '>=7.0.0 <8.0.0'
- typelevel-prelude: '>=7.0.0 <8.0.0'
- url-immutable: '>=1.0.0 <2.0.0' - url-immutable: '>=1.0.0 <2.0.0'
- web-streams: '>=4.0.0 <5.0.0' - web-streams: '>=4.0.0 <5.0.0'
test: test:

View File

@ -15,14 +15,14 @@ import Axon.Request.Method as Method
import Axon.Request.Parts.Body (Json(..), Stream(..)) import Axon.Request.Parts.Body (Json(..), Stream(..))
import Axon.Request.Parts.Body (Json(..), Stream(..)) as Parts.Body import Axon.Request.Parts.Body (Json(..), Stream(..)) as Parts.Body
import Axon.Request.Parts.Method import Axon.Request.Parts.Method
( Connect ( Connect(..)
, Delete , Delete(..)
, Get , Get(..)
, Options , Options(..)
, Patch , Patch(..)
, Post , Post(..)
, Put , Put(..)
, Trace , Trace(..)
) )
import Axon.Request.Parts.Method import Axon.Request.Parts.Method
( Get(..) ( Get(..)
@ -61,20 +61,14 @@ import Effect.Class (liftEffect)
import Node.Buffer (Buffer) import Node.Buffer (Buffer)
extractMethod :: extractMethod ::
forall @t a. forall a.
RequestParts a => a ->
Newtype t a =>
Method -> Method ->
Request -> Request ->
Aff (Either Response (Maybe t)) Aff (Either Response (Maybe a))
extractMethod method r = extractMethod a method r =
if Request.method r == method then if Request.method r == method then
extractRequestParts @a r pure $ Right $ Just a
# ExceptT
# MaybeT
<#> wrap
# runMaybeT
# runExceptT
else else
pure $ Right Nothing pure $ Right Nothing
@ -161,29 +155,29 @@ instance RequestParts Stream where
in in
streamBody <#> Stream # runMaybeT # runExceptT # liftEffect streamBody <#> Stream # runMaybeT # runExceptT # liftEffect
instance (RequestParts a) => RequestParts (Get a) where instance RequestParts Get where
extractRequestParts = extractMethod @(Get a) Method.GET extractRequestParts = extractMethod Get Method.GET
instance (RequestParts a) => RequestParts (Post a) where instance RequestParts Post where
extractRequestParts = extractMethod @(Post a) Method.POST extractRequestParts = extractMethod Post Method.POST
instance (RequestParts a) => RequestParts (Put a) where instance RequestParts Put where
extractRequestParts = extractMethod @(Put a) Method.PUT extractRequestParts = extractMethod Put Method.PUT
instance (RequestParts a) => RequestParts (Patch a) where instance RequestParts Patch where
extractRequestParts = extractMethod @(Patch a) Method.PATCH extractRequestParts = extractMethod Patch Method.PATCH
instance (RequestParts a) => RequestParts (Delete a) where instance RequestParts Delete where
extractRequestParts = extractMethod @(Delete a) Method.DELETE extractRequestParts = extractMethod Delete Method.DELETE
instance (RequestParts a) => RequestParts (Options a) where instance RequestParts Options where
extractRequestParts = extractMethod @(Options a) Method.OPTIONS extractRequestParts = extractMethod Options Method.OPTIONS
instance (RequestParts a) => RequestParts (Connect a) where instance RequestParts Connect where
extractRequestParts = extractMethod @(Connect a) Method.CONNECT extractRequestParts = extractMethod Connect Method.CONNECT
instance (RequestParts a) => RequestParts (Trace a) where instance RequestParts Trace where
extractRequestParts = extractMethod @(Trace a) Method.TRACE extractRequestParts = extractMethod Trace Method.TRACE
instance (RequestParts a, RequestParts b) => RequestParts (a /\ b) where instance (RequestParts a, RequestParts b) => RequestParts (a /\ b) where
extractRequestParts r = runExceptT $ runMaybeT do extractRequestParts r = runExceptT $ runMaybeT do

View File

@ -2,69 +2,50 @@ module Axon.Request.Parts.Method where
import Prelude import Prelude
import Data.Generic.Rep (class Generic) data Get = Get
import Data.Newtype (class Newtype)
newtype Get a = Get a derive instance Eq Get
instance Show Get where
show _ = "Get"
derive instance Generic (Get a) _ data Post = Post
derive instance Newtype (Get a) _
derive newtype instance (Eq a) => Eq (Get a)
derive newtype instance (Ord a) => Ord (Get a)
derive newtype instance (Show a) => Show (Get a)
newtype Post a = Post a derive instance Eq Post
instance Show Post where
show _ = "Post"
derive instance Generic (Post a) _ data Put = Put
derive instance Newtype (Post a) _
derive newtype instance (Eq a) => Eq (Post a)
derive newtype instance (Ord a) => Ord (Post a)
derive newtype instance (Show a) => Show (Post a)
newtype Put a = Put a derive instance Eq Put
instance Show Put where
show _ = "Put"
derive instance Generic (Put a) _ data Patch = Patch
derive instance Newtype (Put a) _
derive newtype instance (Eq a) => Eq (Put a)
derive newtype instance (Ord a) => Ord (Put a)
derive newtype instance (Show a) => Show (Put a)
newtype Patch a = Patch a derive instance Eq Patch
instance Show Patch where
show _ = "Patch"
derive instance Generic (Patch a) _ data Delete = Delete
derive instance Newtype (Patch a) _
derive newtype instance (Eq a) => Eq (Patch a)
derive newtype instance (Ord a) => Ord (Patch a)
derive newtype instance (Show a) => Show (Patch a)
newtype Delete a = Delete a derive instance Eq Delete
instance Show Delete where
show _ = "Delete"
derive instance Generic (Delete a) _ data Options = Options
derive instance Newtype (Delete a) _
derive newtype instance (Eq a) => Eq (Delete a)
derive newtype instance (Ord a) => Ord (Delete a)
derive newtype instance (Show a) => Show (Delete a)
newtype Options a = Options a derive instance Eq Options
instance Show Options where
show _ = "Options"
derive instance Generic (Options a) _ data Trace = Trace
derive instance Newtype (Options a) _
derive newtype instance (Eq a) => Eq (Options a)
derive newtype instance (Ord a) => Ord (Options a)
derive newtype instance (Show a) => Show (Options a)
newtype Trace a = Trace a derive instance Eq Trace
instance Show Trace where
show _ = "Trace"
derive instance Generic (Trace a) _ data Connect = Connect
derive instance Newtype (Trace a) _
derive newtype instance (Eq a) => Eq (Trace a)
derive newtype instance (Ord a) => Ord (Trace a)
derive newtype instance (Show a) => Show (Trace a)
newtype Connect a = Connect a derive instance Eq Connect
instance Show Connect where
derive instance Generic (Connect a) _ show _ = "Connect"
derive instance Newtype (Connect a) _
derive newtype instance (Eq a) => Eq (Connect a)
derive newtype instance (Ord a) => Ord (Connect a)
derive newtype instance (Show a) => Show (Connect a)

View File

@ -61,10 +61,10 @@ spec = describe "Parts" do
} }
a <- a <-
extractRequestParts extractRequestParts
@(Patch ((Path ("users" / Int) _) /\ Json { firstName :: String })) @(Patch /\ (Path ("users" / Int) _) /\ Json { firstName :: String })
req <#> lmap (error <<< show) >>= liftEither >>= liftMaybe req <#> lmap (error <<< show) >>= liftEither >>= liftMaybe
(error "was nothing") (error "was nothing")
a `shouldEqual` Patch (Path 12 /\ Json { firstName: "henry" }) a `shouldEqual` (Patch /\ Path 12 /\ Json { firstName: "henry" })
describe "Path" do describe "Path" do
it "matches a route matching literal" do it "matches a route matching literal" do
@ -231,8 +231,8 @@ spec = describe "Parts" do
{ address: "127.0.0.1", port: 81 } { address: "127.0.0.1", port: 81 }
, method: POST , method: POST
} }
a <- extractRequestParts @(Post (Json { foo :: Int, bar :: String })) req a <- extractRequestParts @(Post /\ Json { foo :: Int, bar :: String }) req
<#> lmap (error <<< show) <#> lmap (error <<< show)
>>= liftEither >>= liftEither
>>= liftMaybe (error "was nothing") >>= liftMaybe (error "was nothing")
a `shouldEqual` Post (Json { foo: 123, bar: "abc" }) a `shouldEqual` (Post /\ Json { foo: 123, bar: "abc" })