Browse Source

cleanup

tags/v1.0.0-alpha.2
ojizero 1 year ago
parent
commit
2345d41335
No account linked to committer's email address
3 changed files with 17 additions and 29 deletions
  1. 5
    3
      src/client.ts
  2. 9
    23
      src/method.ts
  3. 3
    3
      test/client.spec.ts

+ 5
- 3
src/client.ts View File

@@ -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',

+ 9
- 23
src/method.ts View File

@@ -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 {
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 {

// 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 {
...query,
}

query = stringifyQuery(query)
fullPath = `${fullPath}?${query}`
fullPath = `${fullPath}?${stringifyQuery(query)}`
}

options = defaults({}, defaultOptions, options)

+ 3
- 3
test/client.spec.ts View File

@@ -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 = {
}

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