@@ -93,10 +93,10 @@ class ToHttpApiData a where
93
93
toUrlPiece = toQueryParam
94
94
95
95
-- | Convert to a URL path piece, making sure to encode any special chars.
96
- -- The default definition uses 'H.encodePathSegmentsRelative',
96
+ -- The default definition uses @ 'H.urlEncodeBuilder' 'False'@
97
97
-- but this may be overriden with a more efficient version.
98
98
toEncodedUrlPiece :: a -> BS. Builder
99
- toEncodedUrlPiece = H. encodePathSegmentsRelative . ( : [] ) . toUrlPiece
99
+ toEncodedUrlPiece = H. urlEncodeBuilder False . encodeUtf8 . toUrlPiece
100
100
101
101
-- | Convert to HTTP header value.
102
102
toHeader :: a -> ByteString
@@ -106,6 +106,14 @@ class ToHttpApiData a where
106
106
toQueryParam :: a -> Text
107
107
toQueryParam = toUrlPiece
108
108
109
+ -- | Convert to URL query param,
110
+ -- The default definition uses @'H.urlEncodeBuilder' 'True'@
111
+ -- but this may be overriden with a more efficient version.
112
+ --
113
+ -- @since 0.5.1
114
+ toEncodedQueryParam :: a -> BS. Builder
115
+ toEncodedQueryParam = H. urlEncodeBuilder True . encodeUtf8 . toQueryParam
116
+
109
117
-- | Parse value from HTTP API data.
110
118
--
111
119
-- __WARNING__: Do not derive this using @DeriveAnyClass@ as the generated
@@ -422,12 +430,21 @@ parseBounded reader input = do
422
430
unsafeToEncodedUrlPiece :: ToHttpApiData a => a -> BS. Builder
423
431
unsafeToEncodedUrlPiece = BS. byteString . encodeUtf8 . toUrlPiece
424
432
433
+ -- | Convert to a URL-encoded query param using 'toQueryParam'.
434
+ -- /Note/: this function does not check if the result contains unescaped characters!
435
+ --
436
+ -- @since 0.5.1
437
+ unsafeToEncodedQueryParam :: ToHttpApiData a => a -> BS. Builder
438
+ unsafeToEncodedQueryParam = BS. byteString . encodeUtf8 . toQueryParam
439
+
425
440
-- |
426
441
-- >>> toUrlPiece ()
427
442
-- "_"
428
443
instance ToHttpApiData () where
429
- toUrlPiece () = " _"
430
- toEncodedUrlPiece = unsafeToEncodedUrlPiece
444
+ toUrlPiece _ = " _"
445
+ toHeader _ = " _"
446
+ toEncodedUrlPiece _ = " _"
447
+ toEncodedQueryParam _ = " _"
431
448
432
449
instance ToHttpApiData Char where
433
450
toUrlPiece = T. singleton
@@ -438,36 +455,38 @@ instance ToHttpApiData Char where
438
455
instance ToHttpApiData Version where
439
456
toUrlPiece = T. pack . showVersion
440
457
toEncodedUrlPiece = unsafeToEncodedUrlPiece
458
+ toEncodedQueryParam = unsafeToEncodedQueryParam
441
459
442
460
instance ToHttpApiData Void where toUrlPiece = absurd
443
- instance ToHttpApiData Natural where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece
444
-
445
- instance ToHttpApiData Bool where toUrlPiece = showTextData; toEncodedUrlPiece = unsafeToEncodedUrlPiece
446
- instance ToHttpApiData Ordering where toUrlPiece = showTextData; toEncodedUrlPiece = unsafeToEncodedUrlPiece
447
-
448
- instance ToHttpApiData Double where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece
449
- instance ToHttpApiData Float where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece
450
- instance ToHttpApiData Int where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece
451
- instance ToHttpApiData Int8 where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece
452
- instance ToHttpApiData Int16 where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece
453
- instance ToHttpApiData Int32 where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece
454
- instance ToHttpApiData Int64 where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece
455
- instance ToHttpApiData Integer where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece
456
- instance ToHttpApiData Word where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece
457
- instance ToHttpApiData Word8 where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece
458
- instance ToHttpApiData Word16 where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece
459
- instance ToHttpApiData Word32 where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece
460
- instance ToHttpApiData Word64 where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece
461
+ instance ToHttpApiData Natural where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece; toEncodedQueryParam = unsafeToEncodedQueryParam
462
+
463
+ instance ToHttpApiData Bool where toUrlPiece = showTextData; toEncodedUrlPiece = unsafeToEncodedUrlPiece; toEncodedQueryParam = unsafeToEncodedQueryParam
464
+ instance ToHttpApiData Ordering where toUrlPiece = showTextData; toEncodedUrlPiece = unsafeToEncodedUrlPiece; toEncodedQueryParam = unsafeToEncodedQueryParam
465
+
466
+ instance ToHttpApiData Double where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece; toEncodedQueryParam = unsafeToEncodedQueryParam
467
+ instance ToHttpApiData Float where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece; toEncodedQueryParam = unsafeToEncodedQueryParam
468
+ instance ToHttpApiData Int where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece; toEncodedQueryParam = unsafeToEncodedQueryParam
469
+ instance ToHttpApiData Int8 where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece; toEncodedQueryParam = unsafeToEncodedQueryParam
470
+ instance ToHttpApiData Int16 where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece; toEncodedQueryParam = unsafeToEncodedQueryParam
471
+ instance ToHttpApiData Int32 where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece; toEncodedQueryParam = unsafeToEncodedQueryParam
472
+ instance ToHttpApiData Int64 where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece; toEncodedQueryParam = unsafeToEncodedQueryParam
473
+ instance ToHttpApiData Integer where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece; toEncodedQueryParam = unsafeToEncodedQueryParam
474
+ instance ToHttpApiData Word where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece; toEncodedQueryParam = unsafeToEncodedQueryParam
475
+ instance ToHttpApiData Word8 where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece; toEncodedQueryParam = unsafeToEncodedQueryParam
476
+ instance ToHttpApiData Word16 where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece; toEncodedQueryParam = unsafeToEncodedQueryParam
477
+ instance ToHttpApiData Word32 where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece; toEncodedQueryParam = unsafeToEncodedQueryParam
478
+ instance ToHttpApiData Word64 where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece; toEncodedQueryParam = unsafeToEncodedQueryParam
461
479
462
480
-- | Note: this instance is not polykinded
463
- instance F. HasResolution a => ToHttpApiData (F. Fixed (a :: Type )) where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece
481
+ instance F. HasResolution a => ToHttpApiData (F. Fixed (a :: Type )) where toUrlPiece = showt; toEncodedUrlPiece = unsafeToEncodedUrlPiece; toEncodedQueryParam = unsafeToEncodedQueryParam
464
482
465
483
-- |
466
484
-- >>> toUrlPiece (fromGregorian 2015 10 03)
467
485
-- "2015-10-03"
468
486
instance ToHttpApiData Day where
469
487
toUrlPiece = T. pack . show
470
488
toEncodedUrlPiece = unsafeToEncodedUrlPiece
489
+ toEncodedQueryParam = unsafeToEncodedQueryParam
471
490
472
491
timeToUrlPiece :: FormatTime t => String -> t -> Text
473
492
timeToUrlPiece fmt = T. pack . formatTime defaultTimeLocale (iso8601DateFormat (Just fmt))
@@ -478,27 +497,31 @@ timeToUrlPiece fmt = T.pack . formatTime defaultTimeLocale (iso8601DateFormat (J
478
497
instance ToHttpApiData TimeOfDay where
479
498
toUrlPiece = T. pack . formatTime defaultTimeLocale " %H:%M:%S%Q"
480
499
toEncodedUrlPiece = unsafeToEncodedUrlPiece
500
+ -- no toEncodedQueryParam as : is unsafe char.
481
501
482
502
-- |
483
503
-- >>> toUrlPiece $ LocalTime (fromGregorian 2015 10 03) (TimeOfDay 14 55 21.687)
484
504
-- "2015-10-03T14:55:21.687"
485
505
instance ToHttpApiData LocalTime where
486
506
toUrlPiece = timeToUrlPiece " %H:%M:%S%Q"
487
507
toEncodedUrlPiece = unsafeToEncodedUrlPiece
508
+ -- no toEncodedQueryParam as : is unsafe char.
488
509
489
510
-- |
490
511
-- >>> toUrlPiece $ ZonedTime (LocalTime (fromGregorian 2015 10 03) (TimeOfDay 14 55 51.001)) utc
491
512
-- "2015-10-03T14:55:51.001+0000"
492
513
instance ToHttpApiData ZonedTime where
493
514
toUrlPiece = timeToUrlPiece " %H:%M:%S%Q%z"
494
515
toEncodedUrlPiece = unsafeToEncodedUrlPiece
516
+ -- no toEncodedQueryParam as : is unsafe char.
495
517
496
518
-- |
497
519
-- >>> toUrlPiece $ UTCTime (fromGregorian 2015 10 03) 864.5
498
520
-- "2015-10-03T00:14:24.5Z"
499
521
instance ToHttpApiData UTCTime where
500
522
toUrlPiece = timeToUrlPiece " %H:%M:%S%QZ"
501
523
toEncodedUrlPiece = unsafeToEncodedUrlPiece
524
+ -- no toEncodedQueryParam as : is unsafe char.
502
525
503
526
-- |
504
527
-- >>> toUrlPiece Monday
@@ -513,8 +536,9 @@ instance ToHttpApiData DayOfWeek where
513
536
toUrlPiece Sunday = " sunday"
514
537
515
538
toEncodedUrlPiece = unsafeToEncodedUrlPiece
539
+ toEncodedQueryParam = unsafeToEncodedQueryParam
516
540
517
- -- |
541
+ -- |
518
542
-- >>> toUrlPiece Q4
519
543
-- "q4"
520
544
instance ToHttpApiData QuarterOfYear where
@@ -523,6 +547,9 @@ instance ToHttpApiData QuarterOfYear where
523
547
toUrlPiece Q3 = " q3"
524
548
toUrlPiece Q4 = " q4"
525
549
550
+ toEncodedUrlPiece = unsafeToEncodedUrlPiece
551
+ toEncodedQueryParam = unsafeToEncodedQueryParam
552
+
526
553
-- |
527
554
-- >>> import Data.Time.Calendar.Quarter.Compat (Quarter (..))
528
555
-- >>> MkQuarter 8040
@@ -540,6 +567,9 @@ instance ToHttpApiData Quarter where
540
567
f Q3 = " q3"
541
568
f Q4 = " q4"
542
569
570
+ toEncodedUrlPiece = unsafeToEncodedUrlPiece
571
+ toEncodedQueryParam = unsafeToEncodedQueryParam
572
+
543
573
-- |
544
574
-- >>> import Data.Time.Calendar.Month.Compat (Month (..))
545
575
-- >>> MkMonth 24482
@@ -551,8 +581,13 @@ instance ToHttpApiData Quarter where
551
581
instance ToHttpApiData Month where
552
582
toUrlPiece = T. pack . formatTime defaultTimeLocale " %Y-%m"
553
583
584
+ toEncodedUrlPiece = unsafeToEncodedUrlPiece
585
+ toEncodedQueryParam = unsafeToEncodedQueryParam
586
+
554
587
instance ToHttpApiData NominalDiffTime where
555
588
toUrlPiece = toUrlPiece . nominalDiffTimeToSeconds
589
+
590
+ toEncodedQueryParam = unsafeToEncodedQueryParam
556
591
toEncodedUrlPiece = unsafeToEncodedUrlPiece
557
592
558
593
instance ToHttpApiData String where toUrlPiece = T. pack
@@ -562,46 +597,57 @@ instance ToHttpApiData L.Text where toUrlPiece = L.toStrict
562
597
instance ToHttpApiData All where
563
598
toUrlPiece = coerce (toUrlPiece :: Bool -> Text )
564
599
toEncodedUrlPiece = coerce (toEncodedUrlPiece :: Bool -> BS. Builder )
600
+ toEncodedQueryParam = coerce (toEncodedQueryParam :: Bool -> BS. Builder )
565
601
566
602
instance ToHttpApiData Any where
567
603
toUrlPiece = coerce (toUrlPiece :: Bool -> Text )
568
604
toEncodedUrlPiece = coerce (toEncodedUrlPiece :: Bool -> BS. Builder )
605
+ toEncodedQueryParam = coerce (toEncodedQueryParam :: Bool -> BS. Builder )
569
606
570
607
instance ToHttpApiData a => ToHttpApiData (Dual a ) where
571
608
toUrlPiece = coerce (toUrlPiece :: a -> Text )
572
609
toEncodedUrlPiece = coerce (toEncodedUrlPiece :: a -> BS. Builder )
610
+ toEncodedQueryParam = coerce (toEncodedQueryParam :: a -> BS. Builder )
573
611
574
612
instance ToHttpApiData a => ToHttpApiData (Sum a ) where
575
613
toUrlPiece = coerce (toUrlPiece :: a -> Text )
576
614
toEncodedUrlPiece = coerce (toEncodedUrlPiece :: a -> BS. Builder )
615
+ toEncodedQueryParam = coerce (toEncodedQueryParam :: a -> BS. Builder )
577
616
578
617
instance ToHttpApiData a => ToHttpApiData (Product a ) where
579
618
toUrlPiece = coerce (toUrlPiece :: a -> Text )
580
619
toEncodedUrlPiece = coerce (toEncodedUrlPiece :: a -> BS. Builder )
620
+ toEncodedQueryParam = coerce (toEncodedQueryParam :: a -> BS. Builder )
581
621
582
622
instance ToHttpApiData a => ToHttpApiData (First a ) where
583
623
toUrlPiece = coerce (toUrlPiece :: Maybe a -> Text )
584
624
toEncodedUrlPiece = coerce (toEncodedUrlPiece :: Maybe a -> BS. Builder )
625
+ toEncodedQueryParam = coerce (toEncodedQueryParam :: Maybe a -> BS. Builder )
585
626
586
627
instance ToHttpApiData a => ToHttpApiData (Last a ) where
587
628
toUrlPiece = coerce (toUrlPiece :: Maybe a -> Text )
588
629
toEncodedUrlPiece = coerce (toEncodedUrlPiece :: Maybe a -> BS. Builder )
630
+ toEncodedQueryParam = coerce (toEncodedQueryParam :: Maybe a -> BS. Builder )
589
631
590
632
instance ToHttpApiData a => ToHttpApiData (Semi. Min a ) where
591
633
toUrlPiece = coerce (toUrlPiece :: a -> Text )
592
634
toEncodedUrlPiece = coerce (toEncodedUrlPiece :: a -> BS. Builder )
635
+ toEncodedQueryParam = coerce (toEncodedQueryParam :: a -> BS. Builder )
593
636
594
637
instance ToHttpApiData a => ToHttpApiData (Semi. Max a ) where
595
638
toUrlPiece = coerce (toUrlPiece :: a -> Text )
596
639
toEncodedUrlPiece = coerce (toEncodedUrlPiece :: a -> BS. Builder )
640
+ toEncodedQueryParam = coerce (toEncodedQueryParam :: a -> BS. Builder )
597
641
598
642
instance ToHttpApiData a => ToHttpApiData (Semi. First a ) where
599
643
toUrlPiece = coerce (toUrlPiece :: a -> Text )
600
644
toEncodedUrlPiece = coerce (toEncodedUrlPiece :: a -> BS. Builder )
645
+ toEncodedQueryParam = coerce (toEncodedQueryParam :: a -> BS. Builder )
601
646
602
647
instance ToHttpApiData a => ToHttpApiData (Semi. Last a ) where
603
648
toUrlPiece = coerce (toUrlPiece :: a -> Text )
604
649
toEncodedUrlPiece = coerce (toEncodedUrlPiece :: a -> BS. Builder )
650
+ toEncodedQueryParam = coerce (toEncodedQueryParam :: a -> BS. Builder )
605
651
606
652
-- |
607
653
-- >>> toUrlPiece (Just "Hello")
@@ -639,20 +685,23 @@ instance ToHttpApiData a => ToHttpApiData (Tagged (b :: Type) a) where
639
685
toHeader = coerce (toHeader :: a -> ByteString )
640
686
toQueryParam = coerce (toQueryParam :: a -> Text )
641
687
toEncodedUrlPiece = coerce (toEncodedUrlPiece :: a -> BS. Builder )
688
+ toEncodedQueryParam = coerce (toEncodedQueryParam :: a -> BS. Builder )
642
689
643
690
-- | @since 0.4.2
644
691
instance ToHttpApiData a => ToHttpApiData (Const a b ) where
645
692
toUrlPiece = coerce (toUrlPiece :: a -> Text )
646
693
toHeader = coerce (toHeader :: a -> ByteString )
647
694
toQueryParam = coerce (toQueryParam :: a -> Text )
648
695
toEncodedUrlPiece = coerce (toEncodedUrlPiece :: a -> BS. Builder )
696
+ toEncodedQueryParam = coerce (toEncodedQueryParam :: a -> BS. Builder )
649
697
650
698
-- | @since 0.4.2
651
699
instance ToHttpApiData a => ToHttpApiData (Identity a ) where
652
700
toUrlPiece = coerce (toUrlPiece :: a -> Text )
653
701
toHeader = coerce (toHeader :: a -> ByteString )
654
702
toQueryParam = coerce (toQueryParam :: a -> Text )
655
703
toEncodedUrlPiece = coerce (toEncodedUrlPiece :: a -> BS. Builder )
704
+ toEncodedQueryParam = coerce (toEncodedQueryParam :: a -> BS. Builder )
656
705
657
706
-- |
658
707
-- >>> parseUrlPiece "_" :: Either Text ()
0 commit comments