1+ import IMountableItem from './IMountableItem' ;
2+ import Router from './Router' ;
13import { DocumentNode , parse , print , getOperationAST } from 'graphql' ;
24import { AxiosTransformer , AxiosInstance , AxiosRequestConfig } from 'axios' ;
35import * as express from 'express' ;
46
7+ const PATH_VARIABLES_REGEX = / : ( [ A - Z a - z ] + ) / g
8+
59export interface IConstructorRouteOptions {
610 schema : DocumentNode | string ; // GraphQL Document Type
711 operationName : string ;
8-
12+ axios : AxiosInstance ;
913 path ?: string ;
1014 cacheTimeInMs ?: number ;
1115 method ?: string ;
@@ -23,13 +27,14 @@ export interface IRouteOptions {
2327 defaultVariables ?: { } ;
2428}
2529
26- interface IOperationVariable {
30+ export interface IOperationVariable {
2731 name : string ;
2832 required : boolean ;
33+ type : string ;
2934 defaultValue ?: string | boolean | number ;
3035}
3136
32- interface IResponse {
37+ export interface IResponse {
3338 statusCode : number ;
3439 body : { } ;
3540}
@@ -42,6 +47,23 @@ enum EHTTPMethod {
4247}
4348*/
4449
50+ function translateVariableType ( node : any ) : any {
51+ if ( node . type . kind === 'NonNullType' ) {
52+ return translateVariableType ( node . type ) ;
53+ }
54+
55+ switch ( node . type . name . value ) {
56+ case 'Int' :
57+ return 'number' ;
58+ case 'Boolean' :
59+ return 'boolean' ;
60+ case 'String' :
61+ default :
62+ return 'string' ;
63+
64+ }
65+ }
66+
4567function cleanPath ( path : string ) : string {
4668 if ( path [ 0 ] === '/' ) {
4769 return path ;
@@ -50,7 +72,7 @@ function cleanPath(path: string): string {
5072 return `/${ path } ` ;
5173}
5274
53- export default class Route {
75+ export default class Route implements IMountableItem {
5476 public path ! : string ;
5577 public httpMethod : string = 'get' ;
5678
@@ -62,6 +84,7 @@ export default class Route {
6284 // the change
6385 private configurationIsFrozen : boolean = false ;
6486
87+ private axios ! : AxiosInstance ;
6588 private schema ! : DocumentNode ;
6689
6790 private operationName ! : string ;
@@ -75,7 +98,7 @@ export default class Route {
7598
7699 private cacheTimeInMs : number = 0 ;
77100
78- constructor ( configuration : IConstructorRouteOptions , private axios : AxiosInstance ) {
101+ constructor ( configuration : IConstructorRouteOptions , private router ?: Router ) {
79102 this . configureRoute ( configuration ) ;
80103 }
81104
@@ -90,6 +113,7 @@ export default class Route {
90113 }
91114
92115 this . schema = typeof schema === 'string' ? parse ( schema ) : schema ;
116+ this . axios = configuration . axios ;
93117
94118 this . setOperationName ( operationName ) ;
95119
@@ -100,6 +124,12 @@ export default class Route {
100124 this . withOptions ( options ) ;
101125 }
102126
127+ setRouter ( router : Router ) : this {
128+ this . router = router ;
129+
130+ return this ;
131+ }
132+
103133 at ( path : string ) : this {
104134 this . path = cleanPath ( path ) ;
105135
@@ -118,6 +148,7 @@ export default class Route {
118148 ( node : any ) : IOperationVariable => ( {
119149 name : node . variable . name . value ,
120150 required : node . type . kind === 'NonNullType' ,
151+ type : translateVariableType ( node ) ,
121152 defaultValue : node . defaultValue ,
122153 } )
123154 ) ;
@@ -235,6 +266,10 @@ export default class Route {
235266 throw new Error ( 'Not available! Submit PR' ) ;
236267 }
237268
269+ asMetal ( ) {
270+ throw new Error ( 'Not available! Submit PR' ) ;
271+ }
272+
238273 // areVariablesValid(variables: {}) {}
239274
240275 transformRequest ( fn : AxiosTransformer ) : this {
@@ -255,6 +290,45 @@ export default class Route {
255290 return this ;
256291 }
257292
293+ get queryVariables ( ) : IOperationVariable [ ] {
294+ if ( this . httpMethod === 'post' ) {
295+ return [ ] ;
296+ }
297+
298+ return this . nonPathVariables ;
299+ }
300+
301+ get bodyVariables ( ) : IOperationVariable [ ] {
302+ if ( this . httpMethod === 'get' ) {
303+ return [ ] ;
304+ }
305+
306+ return this . nonPathVariables ;
307+ }
308+
309+ get pathVariables ( ) : IOperationVariable [ ] {
310+ const matches = this . path . match ( PATH_VARIABLES_REGEX ) ;
311+
312+ if ( ! matches ) {
313+ return [ ] ;
314+ }
315+
316+ const pathVariableNames = matches . map ( match => match . substr ( 1 ) ) ;
317+
318+ return this . operationVariables . filter (
319+ ( { name } ) => pathVariableNames . includes ( name )
320+ ) ;
321+ }
322+
323+ get nonPathVariables ( ) : IOperationVariable [ ] {
324+ const pathVariableNames = this . pathVariables . map ( ( { name } ) => name ) ;
325+
326+ return this . operationVariables
327+ . filter (
328+ ( { name } ) => ! pathVariableNames . includes ( name )
329+ ) ;
330+ }
331+
258332 private async makeRequest ( variables : { } ) : Promise < IResponse > {
259333 const { axios, schema, operationName } = this ;
260334
0 commit comments