Skip to content

Commit 19fbbad

Browse files
committed
Rename to
1 parent 0423314 commit 19fbbad

File tree

10 files changed

+95
-95
lines changed

10 files changed

+95
-95
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ import App.DB (Pool, createConnectionPool, withPool, selectItems, createItem)
2929
import Bucketchain (createServer, listen)
3030
import Bucketchain.Middleware (Middleware)
3131
import Bucketchain.SimpleAPI (withSimpleAPI)
32-
import Bucketchain.SimpleAPI.Action (Action, askExtra)
3332
import Bucketchain.SimpleAPI.Body (Body(..))
3433
import Bucketchain.SimpleAPI.JSON (JSON, success_)
34+
import Bucketchain.SimpleAPI.Proc (Proc, askExtra)
3535
import Effect (Effect)
3636
import Node.HTTP (ListenOptions, Server)
3737
@@ -68,13 +68,13 @@ server = createServer $ withSimpleAPI pool routes
6868
6969
-- Define endpoints.
7070
71-
getItems :: Action Pool (JSON (Array Item))
71+
getItems :: Proc Pool (JSON (Array Item))
7272
getItems = do
7373
pool <- askExtra
7474
items <- liftAff $ withPool selectItems pool
7575
pure $ success_ 200 items
7676
77-
createItem :: Body ItemParams -> Action Pool (JSON Item)
77+
createItem :: Body ItemParams -> Proc Pool (JSON Item)
7878
createItem (Body params) = do
7979
pool <- askExtra
8080
item <- liftAff $ withPool (createItem params) pool
@@ -131,8 +131,8 @@ Note: `POST /batch` responds 200 always.
131131
You can use `Authenticatable` for authentication.
132132

133133
```purescript
134-
import Bucketchain.SimpleAPI.Action (askExtra, askRaw)
135134
import Bucketchain.SimpleAPI.Auth.Class (class Authenticatable)
135+
import Bucketchain.SimpleAPI.Proc (askExtra, askRaw)
136136
137137
newtype User = User { id :: Int, name :: String }
138138
@@ -149,7 +149,7 @@ instance authenticatableUser :: Authenticatable Pool User where
149149
Then, define request handlers with `Auth`.
150150

151151
```purescript
152-
getItems :: Auth User -> Action Pool (JSON (Array Item))
152+
getItems :: Auth User -> Proc Pool (JSON (Array Item))
153153
getItems (Auth user) = -- ...
154154
```
155155

example/Main.purs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import Bucketchain (createServer, listen)
66
import Bucketchain.Http (requestHeaders)
77
import Bucketchain.Middleware (Middleware)
88
import Bucketchain.SimpleAPI (withSimpleAPI)
9-
import Bucketchain.SimpleAPI.Action (Action, askExtra, askRaw)
109
import Bucketchain.SimpleAPI.Auth (Auth(..))
1110
import Bucketchain.SimpleAPI.Auth.Class (class Authenticatable)
1211
import Bucketchain.SimpleAPI.Batch (Batch(..))
1312
import Bucketchain.SimpleAPI.Body (Body(..))
1413
import Bucketchain.SimpleAPI.FreeT.Class (class Transformable)
1514
import Bucketchain.SimpleAPI.JSON (JSON, failure, success, success_)
15+
import Bucketchain.SimpleAPI.Proc (Proc, askExtra, askRaw)
1616
import Bucketchain.SimpleAPI.RawData (RawData)
1717
import Control.Monad.Error.Class (throwError)
1818
import Control.Monad.Free.Trans (FreeT, liftFreeT)
@@ -66,49 +66,49 @@ middleware = withSimpleAPI 777 $ Batch
6666
, freeTTest
6767
}
6868

69-
successTest :: Action Int (JSON (Array Item))
69+
successTest :: Proc Int (JSON (Array Item))
7070
successTest = do
7171
num <- askExtra
7272
{ path, rawBody } <- askRaw
7373
pure $ success headers 200 [{ id: 1, name: "Item 1", num, path, rawBody }]
7474
where
7575
headers = singleton "X-Custom" "CustomValue"
7676

77-
failureTest :: Action Int (JSON Item)
77+
failureTest :: Proc Int (JSON Item)
7878
failureTest = pure $ failure headers 503 $ singleton "core" ["This is error test"]
7979
where
8080
headers = singleton "X-Custom" "CustomValue2"
8181

82-
bodyTest :: Body OtherItem -> Action Int (JSON OtherItem)
82+
bodyTest :: Body OtherItem -> Proc Int (JSON OtherItem)
8383
bodyTest (Body x) = pure $ success empty 201 x
8484

85-
authTest :: Auth User -> Action Int (JSON OtherItem)
85+
authTest :: Auth User -> Proc Int (JSON OtherItem)
8686
authTest (Auth (User x)) = pure $ success empty 200 { name: x.name }
8787

88-
errorTest :: Action Int (JSON OtherItem)
88+
errorTest :: Proc Int (JSON OtherItem)
8989
errorTest = throwError $ error "Test error"
9090

91-
freeTTest :: VAction (JSON OtherItem)
91+
freeTTest :: VProc (JSON OtherItem)
9292
freeTTest = do
9393
num <- getExtra
9494
rawData <- getRawData
9595
pure $ success_ 200 { name: show num <> rawData.rawBody }
9696

9797
-- FreeT Example
98-
data VActionF a
98+
data VProcF a
9999
= GetExtra (Int -> a)
100100
| GetRawData (RawData -> a)
101101

102-
type VAction = FreeT VActionF (Action Int)
102+
type VProc = FreeT VProcF (Proc Int)
103103

104-
derive instance functorVAction :: Functor VActionF
104+
derive instance functorVProc :: Functor VProcF
105105

106-
getExtra :: VAction Int
106+
getExtra :: VProc Int
107107
getExtra = liftFreeT $ GetExtra identity
108108

109-
getRawData :: VAction RawData
109+
getRawData :: VProc RawData
110110
getRawData = liftFreeT $ GetRawData identity
111111

112-
instance transformableVActionF :: Transformable Int VActionF where
112+
instance transformableVProcF :: Transformable Int VProcF where
113113
transform (GetExtra k) = k <$> askExtra
114114
transform (GetRawData k) = k <$> askRaw

src/Bucketchain/SimpleAPI.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Simple.JSON (writeJSON)
1717

1818
-- | SimpleAPI middleware.
1919
-- |
20-
-- | `ex` is any extra data. It is typically global context such as db connection and can be used in `Action`.
20+
-- | `ex` is any extra data. It is typically global context such as db connection and can be used in `Proc`.
2121
-- |
2222
-- | `server` is a `Servable` instance.
2323
withSimpleAPI

src/Bucketchain/SimpleAPI/Action.purs

Lines changed: 0 additions & 63 deletions
This file was deleted.

src/Bucketchain/SimpleAPI/Auth.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ module Bucketchain.SimpleAPI.Auth where
66
-- |
77
-- | ```purescript
88
-- | -- This request handler is run with authentication.
9-
-- | getItems :: Auth User -> Action AppContext (JSON Item)
9+
-- | getItems :: Auth User -> Proc AppContext (JSON Item)
1010
-- | ```
1111
newtype Auth a = Auth a
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
module Bucketchain.SimpleAPI.Auth.Class where
22

3-
import Bucketchain.SimpleAPI.Action (Action)
3+
import Bucketchain.SimpleAPI.Proc (Proc)
44

55
-- | A typeclass for authentication.
66
-- |
7-
-- | `ex` is any extra data. It is typically global context such as db connection and can be used in `Action`.
7+
-- | `ex` is any extra data. It is typically global context such as db connection and can be used in `Proc`.
88
-- |
99
-- | `a` is a authenticated result. It is typically user data.
1010
-- |
1111
-- | If you call `throwError` in `authenticate` implementation, a server responds 401.
1212
class Authenticatable ex a where
13-
authenticate :: Action ex a
13+
authenticate :: Proc ex a

src/Bucketchain/SimpleAPI/Batch.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type BatchParams = Array
1010

1111
-- | The type for enable batch operation.
1212
-- |
13-
-- | The batch operation is a mechanism running multiple `Action` in one HTTP request.
13+
-- | The batch operation is a mechanism running multiple `Proc` in one HTTP request.
1414
-- |
1515
-- | How to use:
1616
-- |

src/Bucketchain/SimpleAPI/Class.purs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ module Bucketchain.SimpleAPI.Class where
22

33
import Prelude
44

5-
import Bucketchain.SimpleAPI.Action (Action, context, runAction)
65
import Bucketchain.SimpleAPI.Auth (Auth(..))
76
import Bucketchain.SimpleAPI.Auth.Class (class Authenticatable, authenticate)
87
import Bucketchain.SimpleAPI.Batch (BatchParams, Batch(..))
98
import Bucketchain.SimpleAPI.Body (Body(..), decodeBody)
109
import Bucketchain.SimpleAPI.FreeT.Class (class Transformable, transform)
10+
import Bucketchain.SimpleAPI.Proc (Proc, context, runProc)
1111
import Bucketchain.SimpleAPI.RawData (RawData)
1212
import Bucketchain.SimpleAPI.Response (Response, fromResponses, invalidRequestResponse, unauthorizedResponse, errorResponse)
1313
import Bucketchain.SimpleAPI.Response.Class (class Respondable, toResponse)
@@ -35,14 +35,14 @@ class Servable ex server where
3535
class ServableList ex (l :: RowList) (r :: # Type) | l -> r where
3636
serveList :: RLProxy l -> Record r -> ex -> RawData -> Aff (Maybe Response)
3737

38-
instance servableAction :: (Respondable a) => Servable ex (Action ex a) where
38+
instance servableProc :: (Respondable a) => Servable ex (Proc ex a) where
3939
serve server extraData rawData = do
40-
result <- attempt $ runAction server $ context extraData rawData
40+
result <- attempt $ runProc server $ context extraData rawData
4141
case result of
4242
Left _ -> pure $ Just errorResponse
4343
Right x -> pure $ Just $ toResponse x
4444

45-
instance servableFreeT :: (Transformable ex f, Respondable a) => Servable ex (FreeT f (Action ex) a) where
45+
instance servableFreeT :: (Transformable ex f, Respondable a) => Servable ex (FreeT f (Proc ex) a) where
4646
serve server extraData rawData = serve (runFreeT transform server) extraData rawData
4747

4848
instance servableWithBody :: (ReadForeign a, Servable ex server) => Servable ex (Body a -> server) where
@@ -55,7 +55,7 @@ instance servableWithBody :: (ReadForeign a, Servable ex server) => Servable ex
5555

5656
instance servableWithAuth :: (Authenticatable ex a, Servable ex server) => Servable ex (Auth a -> server) where
5757
serve server extraData rawData = do
58-
result <- attempt $ runAction authenticate $ context extraData rawData
58+
result <- attempt $ runProc authenticate $ context extraData rawData
5959
case result of
6060
Left _ -> pure $ Just unauthorizedResponse
6161
Right x ->

src/Bucketchain/SimpleAPI/FreeT/Class.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ module Bucketchain.SimpleAPI.FreeT.Class where
22

33
import Prelude
44

5-
import Bucketchain.SimpleAPI.Action (Action)
5+
import Bucketchain.SimpleAPI.Proc (Proc)
66
import Control.Monad.Free.Trans (FreeT)
77

88
-- | A typeclass for `FreeT` request handlers.
99
-- |
10-
-- | `ex` is any extra data. It is typically global context such as db connection and can be used in `Action`.
10+
-- | `ex` is any extra data. It is typically global context such as db connection and can be used in `Proc`.
1111
class (Functor f) <= Transformable ex f where
12-
transform :: forall a. f (FreeT f (Action ex) a) -> Action ex (FreeT f (Action ex) a)
12+
transform :: forall a. f (FreeT f (Proc ex) a) -> Proc ex (FreeT f (Proc ex) a)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
module Bucketchain.SimpleAPI.Proc
2+
( Context
3+
, Proc
4+
, askExtra
5+
, askRaw
6+
, runProc
7+
, context
8+
) where
9+
10+
import Prelude
11+
12+
import Bucketchain.SimpleAPI.RawData (RawData)
13+
import Control.Alt (class Alt)
14+
import Control.Monad.Error.Class (class MonadError, class MonadThrow)
15+
import Control.Monad.Reader (class MonadAsk, ReaderT, ask, runReaderT)
16+
import Control.Monad.Rec.Class (class MonadRec)
17+
import Control.Plus (class Plus)
18+
import Effect.Aff (Aff)
19+
import Effect.Aff.Class (class MonadAff)
20+
import Effect.Class (class MonadEffect)
21+
import Effect.Exception (Error)
22+
23+
-- | This is for internal. Do not use it.
24+
newtype Context ex =
25+
Context
26+
{ extraData :: ex
27+
, rawData :: RawData
28+
}
29+
30+
-- | The type of request handler.
31+
newtype Proc ex a = Proc (ReaderT (Context ex) Aff a)
32+
33+
derive newtype instance functorProc :: Functor (Proc ex)
34+
derive newtype instance applyProc :: Apply (Proc ex)
35+
derive newtype instance applicativeProc :: Applicative (Proc ex)
36+
derive newtype instance altProc :: Alt (Proc ex)
37+
derive newtype instance plusProc :: Plus (Proc ex)
38+
derive newtype instance bindProc :: Bind (Proc ex)
39+
derive newtype instance monadProc :: Monad (Proc ex)
40+
derive newtype instance semigroupProc :: Semigroup a => Semigroup (Proc ex a)
41+
derive newtype instance monoidProc :: Monoid a => Monoid (Proc ex a)
42+
derive newtype instance monadEffectProc :: MonadEffect (Proc ex)
43+
derive newtype instance monadAffProc :: MonadAff (Proc ex)
44+
derive newtype instance monadThrowProc :: MonadThrow Error (Proc ex)
45+
derive newtype instance monadErrorProc :: MonadError Error (Proc ex)
46+
derive newtype instance monadAskProc :: MonadAsk (Context ex) (Proc ex)
47+
derive newtype instance monadRecProc :: MonadRec (Proc ex)
48+
49+
-- | Get global context.
50+
askExtra :: forall ex. Proc ex ex
51+
askExtra = flip map ask \(Context ctx) -> ctx.extraData
52+
53+
-- | Get `RawData`.
54+
askRaw :: forall ex. Proc ex RawData
55+
askRaw = flip map ask \(Context ctx) -> ctx.rawData
56+
57+
-- | This is for internal. Do not use it.
58+
runProc :: forall ex a. Proc ex a -> Context ex -> Aff a
59+
runProc (Proc readerT) = runReaderT readerT
60+
61+
-- | This is for internal. Do not use it.
62+
context :: forall ex. ex -> RawData -> Context ex
63+
context extraData rawData = Context { extraData, rawData }

0 commit comments

Comments
 (0)