@@ -13,6 +13,7 @@ import Control.Lens (makePrisms, makeLenses, Getter, (&), (<>~), (%~),
13
13
import Data.Monoid
14
14
#endif
15
15
import Data.Proxy
16
+ import Data.Typeable (Typeable , TypeRep , typeOf )
16
17
import Data.String
17
18
import Data.Text
18
19
import Data.Text.Encoding (decodeUtf8 )
@@ -125,6 +126,7 @@ data Req f = Req
125
126
, _reqBody :: Maybe f
126
127
, _reqReturnType :: Maybe f
127
128
, _reqFuncName :: FunctionName
129
+ , _reqApiType :: TypeRep
128
130
}
129
131
130
132
deriving instance Eq f => Eq (Req f )
@@ -133,7 +135,7 @@ deriving instance Show f => Show (Req f)
133
135
makeLenses ''Req
134
136
135
137
defReq :: Req ftype
136
- defReq = Req defUrl " GET" [] Nothing Nothing (FunctionName [] )
138
+ defReq = Req defUrl " GET" [] Nothing Nothing (FunctionName [] ) (typeOf () )
137
139
138
140
-- | To be used exclusively as a "negative" return type/constraint
139
141
-- by @'Elem`@ type family.
@@ -196,81 +198,87 @@ instance (HasForeign lang ftype a, HasForeign lang ftype b)
196
198
foreignFor lang ftype (Proxy :: Proxy a ) req
197
199
:<|> foreignFor lang ftype (Proxy :: Proxy b ) req
198
200
199
- instance (KnownSymbol sym , HasForeignType lang ftype t , HasForeign lang ftype sublayout )
201
+ instance (KnownSymbol sym , HasForeignType lang ftype t , HasForeign lang ftype sublayout , Typeable ( Capture sym t :> sublayout ) )
200
202
=> HasForeign lang ftype (Capture sym t :> sublayout ) where
201
203
type Foreign ftype (Capture sym a :> sublayout ) = Foreign ftype sublayout
202
204
203
205
foreignFor lang Proxy Proxy req =
204
206
foreignFor lang Proxy (Proxy :: Proxy sublayout ) $
205
207
req & reqUrl . path <>~ [Segment (Cap arg)]
206
208
& reqFuncName . _FunctionName %~ (++ [" by" , str])
209
+ & reqApiType .~ typeOf (undefined :: Capture sym t :> sublayout )
207
210
where
208
211
str = pack . symbolVal $ (Proxy :: Proxy sym )
209
212
ftype = typeFor lang (Proxy :: Proxy ftype ) (Proxy :: Proxy t )
210
213
arg = Arg
211
214
{ _argName = PathSegment str
212
215
, _argType = ftype }
213
216
214
- instance (Elem JSON list , HasForeignType lang ftype a , ReflectMethod method )
217
+ instance (Elem JSON list , HasForeignType lang ftype a , ReflectMethod method , Typeable ( Verb method status list a ) )
215
218
=> HasForeign lang ftype (Verb method status list a ) where
216
219
type Foreign ftype (Verb method status list a ) = Req ftype
217
220
218
221
foreignFor lang Proxy Proxy req =
219
222
req & reqFuncName . _FunctionName %~ (methodLC : )
220
223
& reqMethod .~ method
221
224
& reqReturnType .~ Just retType
225
+ & reqApiType .~ typeOf (undefined :: Verb method status list a )
222
226
where
223
227
retType = typeFor lang (Proxy :: Proxy ftype ) (Proxy :: Proxy a )
224
228
method = reflectMethod (Proxy :: Proxy method )
225
229
methodLC = toLower $ decodeUtf8 method
226
230
227
- instance (KnownSymbol sym , HasForeignType lang ftype a , HasForeign lang ftype sublayout )
231
+ instance (KnownSymbol sym , HasForeignType lang ftype a , HasForeign lang ftype sublayout , Typeable ( Header sym a :> sublayout ) )
228
232
=> HasForeign lang ftype (Header sym a :> sublayout ) where
229
233
type Foreign ftype (Header sym a :> sublayout ) = Foreign ftype sublayout
230
234
231
235
foreignFor lang Proxy Proxy req =
232
236
foreignFor lang Proxy subP $ req & reqHeaders <>~ [HeaderArg arg]
237
+ & reqApiType .~ typeOf (undefined :: Header sym a :> sublayout )
233
238
where
234
239
hname = pack . symbolVal $ (Proxy :: Proxy sym )
235
240
arg = Arg
236
241
{ _argName = PathSegment hname
237
242
, _argType = typeFor lang (Proxy :: Proxy ftype ) (Proxy :: Proxy a ) }
238
243
subP = Proxy :: Proxy sublayout
239
244
240
- instance (KnownSymbol sym , HasForeignType lang ftype a , HasForeign lang ftype sublayout )
245
+ instance (KnownSymbol sym , HasForeignType lang ftype a , HasForeign lang ftype sublayout , Typeable ( QueryParam sym a :> sublayout ) )
241
246
=> HasForeign lang ftype (QueryParam sym a :> sublayout ) where
242
247
type Foreign ftype (QueryParam sym a :> sublayout ) = Foreign ftype sublayout
243
248
244
249
foreignFor lang Proxy Proxy req =
245
250
foreignFor lang (Proxy :: Proxy ftype ) (Proxy :: Proxy sublayout ) $
246
251
req & reqUrl. queryStr <>~ [QueryArg arg Normal ]
252
+ & reqApiType .~ typeOf (undefined :: QueryParam sym a :> sublayout )
247
253
where
248
254
str = pack . symbolVal $ (Proxy :: Proxy sym )
249
255
arg = Arg
250
256
{ _argName = PathSegment str
251
257
, _argType = typeFor lang (Proxy :: Proxy ftype ) (Proxy :: Proxy a ) }
252
258
253
259
instance
254
- (KnownSymbol sym , HasForeignType lang ftype [a ], HasForeign lang ftype sublayout )
260
+ (KnownSymbol sym , HasForeignType lang ftype [a ], HasForeign lang ftype sublayout , Typeable ( QueryParams sym a :> sublayout ) )
255
261
=> HasForeign lang ftype (QueryParams sym a :> sublayout ) where
256
262
type Foreign ftype (QueryParams sym a :> sublayout ) = Foreign ftype sublayout
257
263
foreignFor lang Proxy Proxy req =
258
264
foreignFor lang (Proxy :: Proxy ftype ) (Proxy :: Proxy sublayout ) $
259
265
req & reqUrl. queryStr <>~ [QueryArg arg List ]
266
+ & reqApiType .~ typeOf (undefined :: QueryParams sym a :> sublayout )
260
267
where
261
268
str = pack . symbolVal $ (Proxy :: Proxy sym )
262
269
arg = Arg
263
270
{ _argName = PathSegment str
264
271
, _argType = typeFor lang (Proxy :: Proxy ftype ) (Proxy :: Proxy [a ]) }
265
272
266
273
instance
267
- (KnownSymbol sym , HasForeignType lang ftype Bool , HasForeign lang ftype sublayout )
274
+ (KnownSymbol sym , HasForeignType lang ftype Bool , HasForeign lang ftype sublayout , Typeable ( QueryFlag sym :> sublayout ) )
268
275
=> HasForeign lang ftype (QueryFlag sym :> sublayout ) where
269
276
type Foreign ftype (QueryFlag sym :> sublayout ) = Foreign ftype sublayout
270
277
271
278
foreignFor lang ftype Proxy req =
272
279
foreignFor lang ftype (Proxy :: Proxy sublayout ) $
273
280
req & reqUrl. queryStr <>~ [QueryArg arg Flag ]
281
+ & reqApiType .~ typeOf (undefined :: QueryFlag sym :> sublayout )
274
282
where
275
283
str = pack . symbolVal $ (Proxy :: Proxy sym )
276
284
arg = Arg
@@ -283,61 +291,70 @@ instance HasForeign lang ftype Raw where
283
291
foreignFor _ Proxy Proxy req method =
284
292
req & reqFuncName . _FunctionName %~ ((toLower $ decodeUtf8 method) : )
285
293
& reqMethod .~ method
294
+ & reqApiType .~ typeOf (undefined :: Raw )
286
295
287
- instance (Elem JSON list , HasForeignType lang ftype a , HasForeign lang ftype sublayout )
296
+ instance (Elem JSON list , HasForeignType lang ftype a , HasForeign lang ftype sublayout , Typeable ( ReqBody list a :> sublayout ) )
288
297
=> HasForeign lang ftype (ReqBody list a :> sublayout ) where
289
298
type Foreign ftype (ReqBody list a :> sublayout ) = Foreign ftype sublayout
290
299
291
300
foreignFor lang ftype Proxy req =
292
301
foreignFor lang ftype (Proxy :: Proxy sublayout ) $
293
302
req & reqBody .~ (Just $ typeFor lang ftype (Proxy :: Proxy a ))
303
+ & reqApiType .~ typeOf (undefined :: ReqBody list a :> sublayout )
294
304
295
- instance (KnownSymbol path , HasForeign lang ftype sublayout )
305
+ instance (KnownSymbol path , HasForeign lang ftype sublayout , Typeable ( path :> sublayout ) )
296
306
=> HasForeign lang ftype (path :> sublayout ) where
297
307
type Foreign ftype (path :> sublayout ) = Foreign ftype sublayout
298
308
299
309
foreignFor lang ftype Proxy req =
300
310
foreignFor lang ftype (Proxy :: Proxy sublayout ) $
301
311
req & reqUrl . path <>~ [Segment (Static (PathSegment str))]
302
312
& reqFuncName . _FunctionName %~ (++ [str])
313
+ & reqApiType .~ typeOf (undefined :: path :> sublayout )
303
314
where
304
315
str =
305
316
Data.Text. map (\ c -> if c == ' .' then ' _' else c)
306
317
. pack . symbolVal $ (Proxy :: Proxy path )
307
318
308
- instance HasForeign lang ftype sublayout
319
+ instance ( HasForeign lang ftype sublayout , Typeable ( RemoteHost :> sublayout ))
309
320
=> HasForeign lang ftype (RemoteHost :> sublayout ) where
310
321
type Foreign ftype (RemoteHost :> sublayout ) = Foreign ftype sublayout
311
322
312
323
foreignFor lang ftype Proxy req =
313
- foreignFor lang ftype (Proxy :: Proxy sublayout ) req
324
+ foreignFor lang ftype (Proxy :: Proxy sublayout ) $
325
+ req & reqApiType .~ typeOf (undefined :: (RemoteHost :> sublayout ))
314
326
315
- instance HasForeign lang ftype sublayout
327
+ instance ( HasForeign lang ftype sublayout , Typeable ( IsSecure :> sublayout ))
316
328
=> HasForeign lang ftype (IsSecure :> sublayout ) where
317
329
type Foreign ftype (IsSecure :> sublayout ) = Foreign ftype sublayout
318
330
319
331
foreignFor lang ftype Proxy req =
320
- foreignFor lang ftype (Proxy :: Proxy sublayout ) req
332
+ foreignFor lang ftype (Proxy :: Proxy sublayout ) $
333
+ req & reqApiType .~ typeOf (undefined :: IsSecure :> sublayout )
321
334
322
- instance HasForeign lang ftype sublayout => HasForeign lang ftype (Vault :> sublayout ) where
335
+ instance (HasForeign lang ftype sublayout , Typeable (Vault :> sublayout ))
336
+ => HasForeign lang ftype (Vault :> sublayout ) where
323
337
type Foreign ftype (Vault :> sublayout ) = Foreign ftype sublayout
324
338
325
339
foreignFor lang ftype Proxy req =
326
- foreignFor lang ftype (Proxy :: Proxy sublayout ) req
340
+ foreignFor lang ftype (Proxy :: Proxy sublayout ) $
341
+ req & reqApiType .~ typeOf (undefined :: Vault :> sublayout )
327
342
328
- instance HasForeign lang ftype sublayout =>
329
- HasForeign lang ftype (WithNamedContext name context sublayout ) where
343
+ instance ( HasForeign lang ftype sublayout , Typeable ( WithNamedContext name context sublayout ))
344
+ => HasForeign lang ftype (WithNamedContext name context sublayout ) where
330
345
331
346
type Foreign ftype (WithNamedContext name context sublayout ) = Foreign ftype sublayout
332
347
333
- foreignFor lang ftype Proxy = foreignFor lang ftype (Proxy :: Proxy sublayout )
348
+ foreignFor lang ftype Proxy req = foreignFor lang ftype (Proxy :: Proxy sublayout ) $
349
+ req & reqApiType .~ typeOf (undefined :: WithNamedContext name context sublayout )
334
350
335
- instance HasForeign lang ftype sublayout
351
+ instance ( HasForeign lang ftype sublayout , Typeable ( HttpVersion :> sublayout ))
336
352
=> HasForeign lang ftype (HttpVersion :> sublayout ) where
337
353
type Foreign ftype (HttpVersion :> sublayout ) = Foreign ftype sublayout
338
354
339
355
foreignFor lang ftype Proxy req =
340
- foreignFor lang ftype (Proxy :: Proxy sublayout ) req
356
+ foreignFor lang ftype (Proxy :: Proxy sublayout ) $
357
+ req & reqApiType .~ typeOf (undefined :: HttpVersion :> sublayout )
341
358
342
359
-- | Utility class used by 'listFromAPI' which computes
343
360
-- the data needed to generate a function for each endpoint
0 commit comments