@@ -425,34 +425,68 @@ instance HasLink sub => HasLink (MultipartForm a :> sub) where
425
425
type MkLink (MultipartForm a :> sub ) = MkLink sub
426
426
toLink _ = toLink (Proxy :: Proxy sub )
427
427
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
+ -- @
428
452
class ToMultipartSample tag a where
429
453
toMultipartSamples :: Proxy a -> [(Text , MultipartData tag )]
430
454
455
+ -- | Format an 'Input' into a markdown list item.
431
456
multipartInputToItem :: Input -> Text
432
457
multipartInputToItem (Input name val) =
433
458
" - *" <> name <> " *: " <> " `" <> val <> " `"
434
459
460
+ -- | Format a 'FileData' into a markdown list item.
435
461
multipartFileToItem :: FileData tag -> Text
436
462
multipartFileToItem (FileData name _ contentType _) =
437
463
" - *" <> name <> " *, content-type: " <> " `" <> contentType <> " `"
438
464
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) =
441
472
" - " <> desc <> " \n " <>
442
473
" - textual inputs (any `<input>` type but file):\n " <>
443
474
foldMap (\ input -> multipartInputToItem input <> " \n " ) inputs <>
444
475
" - file inputs (any HTML input that looks like `<input type=\" file\" name=\" somefile\" />`):\n " <>
445
476
foldMap (\ file -> multipartFileToItem file <> " \n " ) files
446
477
478
+ -- | Format a list of samples generated with 'ToMultipartSample' into sections
479
+ -- of markdown.
447
480
toMultipartDescriptions
448
481
:: forall tag a .
449
482
ToMultipartSample tag a
450
483
=> Proxy tag -> Proxy a -> [Text ]
451
- toMultipartDescriptions _ proxyA = fmap multipartSampleToDesc samples
484
+ toMultipartDescriptions _ proxyA = fmap ( uncurry multipartSampleToDesc) samples
452
485
where
453
486
samples :: [(Text , MultipartData tag )]
454
487
samples = toMultipartSamples proxyA
455
488
489
+ -- | Create a 'DocNote' that represents samples for this multipart input.
456
490
toMultipartNotes
457
491
:: ToMultipartSample tag a
458
492
=> Int -> Proxy tag -> Proxy a -> DocNote
@@ -465,6 +499,8 @@ toMultipartNotes maxSamples' proxyTag proxyA =
465
499
]
466
500
in DocNote " Multipart Request Samples" $ fmap unpack body
467
501
502
+ -- | Declare an instance of 'ToMultipartSample' for your 'MultipartForm' type
503
+ -- to be able to use this 'HasDocs' instance.
468
504
instance (HasDocs api , ToMultipartSample tag a ) => HasDocs (MultipartForm tag a :> api ) where
469
505
docsFor
470
506
:: Proxy (MultipartForm tag a :> api )
0 commit comments