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

View File

@ -14,28 +14,45 @@
{
"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"
},
"either",
"exceptions",
{
"either": ">=6.1.0 <7.0.0"
},
{
"exceptions": ">=6.1.0 <7.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"
},
"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"
},
@ -45,11 +62,15 @@
{
"nullable": ">=6.0.0 <7.0.0"
},
"ordered-collections",
{
"ordered-collections": ">=3.2.0 <4.0.0"
},
{
"prelude": ">=6.0.1 <7.0.0"
},
"refs",
{
"refs": ">=6.0.0 <7.0.0"
},
{
"strings": ">=6.0.1 <7.0.0"
},
@ -59,7 +80,9 @@
{
"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"
},
@ -76,7 +99,6 @@
"arrays",
"b64",
"bifunctors",
"console",
"const",
"contravariant",
"control",

View File

@ -1,33 +1,33 @@
package:
name: axon
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'
- argonaut-codecs: '>=9.1.0 <10.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'
- control: '>=6.0.0 <7.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'
- foldable-traversable: '>=6.0.0 <7.0.0'
- integers: '>=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-streams: '>=9.0.0 <10.0.0'
- nullable: '>=6.0.0 <7.0.0'
- ordered-collections: '>=3.2.0 <4.0.0'
- prelude: '>=6.0.1 <7.0.0'
- refs: '>=6.0.0 <7.0.0'
- strings: '>=6.0.1 <7.0.0'
- transformers: '>=6.1.0 <7.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'
- web-streams: '>=4.0.0 <5.0.0'
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(..)) as Parts.Body
import Axon.Request.Parts.Method
( Connect
, Delete
, Get
, Options
, Patch
, Post
, Put
, Trace
( Connect(..)
, Delete(..)
, Get(..)
, Options(..)
, Patch(..)
, Post(..)
, Put(..)
, Trace(..)
)
import Axon.Request.Parts.Method
( Get(..)
@ -61,20 +61,14 @@ import Effect.Class (liftEffect)
import Node.Buffer (Buffer)
extractMethod ::
forall @t a.
RequestParts a =>
Newtype t a =>
forall a.
a ->
Method ->
Request ->
Aff (Either Response (Maybe t))
extractMethod method r =
Aff (Either Response (Maybe a))
extractMethod a method r =
if Request.method r == method then
extractRequestParts @a r
# ExceptT
# MaybeT
<#> wrap
# runMaybeT
# runExceptT
pure $ Right $ Just a
else
pure $ Right Nothing
@ -161,29 +155,29 @@ instance RequestParts Stream where
in
streamBody <#> Stream # runMaybeT # runExceptT # liftEffect
instance (RequestParts a) => RequestParts (Get a) where
extractRequestParts = extractMethod @(Get a) Method.GET
instance RequestParts Get where
extractRequestParts = extractMethod Get Method.GET
instance (RequestParts a) => RequestParts (Post a) where
extractRequestParts = extractMethod @(Post a) Method.POST
instance RequestParts Post where
extractRequestParts = extractMethod Post Method.POST
instance (RequestParts a) => RequestParts (Put a) where
extractRequestParts = extractMethod @(Put a) Method.PUT
instance RequestParts Put where
extractRequestParts = extractMethod Put Method.PUT
instance (RequestParts a) => RequestParts (Patch a) where
extractRequestParts = extractMethod @(Patch a) Method.PATCH
instance RequestParts Patch where
extractRequestParts = extractMethod Patch Method.PATCH
instance (RequestParts a) => RequestParts (Delete a) where
extractRequestParts = extractMethod @(Delete a) Method.DELETE
instance RequestParts Delete where
extractRequestParts = extractMethod Delete Method.DELETE
instance (RequestParts a) => RequestParts (Options a) where
extractRequestParts = extractMethod @(Options a) Method.OPTIONS
instance RequestParts Options where
extractRequestParts = extractMethod Options Method.OPTIONS
instance (RequestParts a) => RequestParts (Connect a) where
extractRequestParts = extractMethod @(Connect a) Method.CONNECT
instance RequestParts Connect where
extractRequestParts = extractMethod Connect Method.CONNECT
instance (RequestParts a) => RequestParts (Trace a) where
extractRequestParts = extractMethod @(Trace a) Method.TRACE
instance RequestParts Trace where
extractRequestParts = extractMethod Trace Method.TRACE
instance (RequestParts a, RequestParts b) => RequestParts (a /\ b) where
extractRequestParts r = runExceptT $ runMaybeT do

View File

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

View File

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