Browse Source

cleanup

modified-method-api
ojizero 4 years ago
parent
commit
2345d41335
No known key found for this signature in database
GPG Key ID: FEBB7097845B0C7E
  1. 8
      src/client.ts
  2. 32
      src/method.ts
  3. 6
      test/client.spec.ts

8
src/client.ts

@ -1,8 +1,10 @@ @@ -1,8 +1,10 @@
import querystring from 'querystring'
import { IncomingHttpHeaders } from 'http';
import { OutgoingHttpHeaders } from 'http'
import {
IncomingHttpHeaders,
OutgoingHttpHeaders,
} from 'http'
import defaultsDeep from 'lodash.defaultsdeep'
import { RequestOptions as HttpsRequestOptions } from 'https';
import { RequestOptions as HttpsRequestOptions } from 'https'
interface BaseAuth {
type: 'basic' | 'bearer' | 'none',

32
src/method.ts

@ -34,7 +34,6 @@ function isRequestConfig (arg: any): arg is RequestConfig { @@ -34,7 +34,6 @@ function isRequestConfig (arg: any): arg is RequestConfig {
return typeof arg === 'object'
}
export function methodGenerator (client: Client): MethodFactory {
return function methodFactory (spec: MethodSpec): RouteFunction {
const {
@ -61,31 +60,19 @@ export function methodGenerator (client: Client): MethodFactory { @@ -61,31 +60,19 @@ export function methodGenerator (client: Client): MethodFactory {
return async function (...args: any[]): Promise<Response> {
let length = args.length
let options: RequestConfig = {}
let query: any
let payload: any
// let pathParams
let config: RequestConfig = {}
if (isRequestConfig(args[length - 1])) {
// If the last argument it an
// object use it as options
options = args[length - 1]
config = args[length - 1]
args = args.slice(0, length - 1)
length -= 1
}
if ('queryString' in options) {
query = options.queryString
delete options.queryString
}
if ('payload' in options) {
payload = options.payload
delete options.payload
}
// if ('path' in options) {
// pathParams = options.path
// delete options.path
// }
let {
queryString: query,
payload,
...options
} = config
ensureValidData(params, args, 'Parameters')
ensureValidData(body, payload, 'Payload')
@ -93,7 +80,7 @@ export function methodGenerator (client: Client): MethodFactory { @@ -93,7 +80,7 @@ export function methodGenerator (client: Client): MethodFactory {
// Regexp here is global we wanna
// match all avaialbel parameters
let paramsCount: number = (path.match(/:[^\/:]+/g) || []).length
let paramsCount: number = (path.match(/:[^\/:&?]+/g) || []).length
let fullPath: string = args.reduce((acc, arg) => {
paramsCount -= 1
@ -114,8 +101,7 @@ export function methodGenerator (client: Client): MethodFactory { @@ -114,8 +101,7 @@ export function methodGenerator (client: Client): MethodFactory {
...query,
}
query = stringifyQuery(query)
fullPath = `${fullPath}?${query}`
fullPath = `${fullPath}?${stringifyQuery(query)}`
}
options = defaults({}, defaultOptions, options)

6
test/client.spec.ts

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
/// <reference path='typings/globals.d.ts' />
import Client, { Config, RawResponse, Authentication, AuthenticationTypes, ClientFn, Response } from '../src/client'
import Client, { Config, RawResponse, Authentication, ClientFn, Response } from '../src/client'
const mockRawResponse: RawResponse = {
body: 'any',
@ -24,12 +24,12 @@ const mockConfig: Config = { @@ -24,12 +24,12 @@ const mockConfig: Config = {
}
const mockBearerAuthConfig: Authentication = {
type: AuthenticationTypes.BearerAuth,
type: 'bearer',
authToken: 'some-token-value'
}
const mockBasicAuthConfig: Authentication = {
type: AuthenticationTypes.BasicAuth,
type: 'basic',
username: 'some-username',
password: 'some-password',
}

Loading…
Cancel
Save