Skip to content

Commit 37f4bd5

Browse files
committed
Add documentation.
1 parent b415dc9 commit 37f4bd5

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

src/Servant/Multipart.hs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,34 +425,68 @@ instance HasLink sub => HasLink (MultipartForm a :> sub) where
425425
type MkLink (MultipartForm a :> sub) = MkLink sub
426426
toLink _ = toLink (Proxy :: Proxy sub)
427427

428+
-- | The 'ToMultipartSample' class allows you to create sample 'MultipartData'
429+
-- inputs for your type for use with "Servant.Docs". This is used by the
430+
-- 'HasDocs' instance for 'MultipartForm'.
431+
--
432+
-- Given the example 'User' type and 'FromMultipart' instance above, here is a
433+
-- corresponding 'ToMultipartSample' instance:
434+
--
435+
-- @
436+
-- data User = User { username :: Text, pic :: FilePath }
437+
--
438+
-- instance 'ToMultipartSample' 'Tmp' User where
439+
-- 'toMultipartSamples' proxy =
440+
-- [ ( \"sample 1\"
441+
-- , 'MultipartData'
442+
-- [ 'Input' \"username\" \"Elvis Presley\" ]
443+
-- [ 'FileData'
444+
-- \"pic\"
445+
-- \"playing_guitar.jpeg\"
446+
-- \"image/jpeg\"
447+
-- \"/tmp/servant-multipart000.buf\"
448+
-- ]
449+
-- )
450+
-- ]
451+
-- @
428452
class ToMultipartSample tag a where
429453
toMultipartSamples :: Proxy a -> [(Text, MultipartData tag)]
430454

455+
-- | Format an 'Input' into a markdown list item.
431456
multipartInputToItem :: Input -> Text
432457
multipartInputToItem (Input name val) =
433458
" - *" <> name <> "*: " <> "`" <> val <> "`"
434459

460+
-- | Format a 'FileData' into a markdown list item.
435461
multipartFileToItem :: FileData tag -> Text
436462
multipartFileToItem (FileData name _ contentType _) =
437463
" - *" <> name <> "*, content-type: " <> "`" <> contentType <> "`"
438464

439-
multipartSampleToDesc :: (Text, MultipartData tag) -> Text
440-
multipartSampleToDesc (desc, MultipartData inputs files) =
465+
-- | Format a description and a sample 'MultipartData' into a markdown list
466+
-- item.
467+
multipartSampleToDesc
468+
:: Text -- ^ The description for the sample.
469+
-> MultipartData tag -- ^ The sample 'MultipartData'.
470+
-> Text -- ^ A markdown list item.
471+
multipartSampleToDesc desc (MultipartData inputs files) =
441472
"- " <> desc <> "\n" <>
442473
" - textual inputs (any `<input>` type but file):\n" <>
443474
foldMap (\input -> multipartInputToItem input <> "\n") inputs <>
444475
" - file inputs (any HTML input that looks like `<input type=\"file\" name=\"somefile\" />`):\n" <>
445476
foldMap (\file -> multipartFileToItem file <> "\n") files
446477

478+
-- | Format a list of samples generated with 'ToMultipartSample' into sections
479+
-- of markdown.
447480
toMultipartDescriptions
448481
:: forall tag a.
449482
ToMultipartSample tag a
450483
=> Proxy tag -> Proxy a -> [Text]
451-
toMultipartDescriptions _ proxyA = fmap multipartSampleToDesc samples
484+
toMultipartDescriptions _ proxyA = fmap (uncurry multipartSampleToDesc) samples
452485
where
453486
samples :: [(Text, MultipartData tag)]
454487
samples = toMultipartSamples proxyA
455488

489+
-- | Create a 'DocNote' that represents samples for this multipart input.
456490
toMultipartNotes
457491
:: ToMultipartSample tag a
458492
=> Int -> Proxy tag -> Proxy a -> DocNote
@@ -465,6 +499,8 @@ toMultipartNotes maxSamples' proxyTag proxyA =
465499
]
466500
in DocNote "Multipart Request Samples" $ fmap unpack body
467501

502+
-- | Declare an instance of 'ToMultipartSample' for your 'MultipartForm' type
503+
-- to be able to use this 'HasDocs' instance.
468504
instance (HasDocs api, ToMultipartSample tag a) => HasDocs (MultipartForm tag a :> api) where
469505
docsFor
470506
:: Proxy (MultipartForm tag a :> api)

0 commit comments

Comments
 (0)