Browse Source

working query strings

tags/v1.0.0-alpha.0
ojizero 1 year ago
parent
commit
fa1562cdd2
No account linked to committer's email address
2 changed files with 25 additions and 6 deletions
  1. 5
    1
      src/index.ts
  2. 20
    5
      src/method.ts

+ 5
- 1
src/index.ts View File

@@ -1,6 +1,9 @@
import Client from './client'

// import method from './method'
import Resource from './resource'
// import Resource from './resource'

import { URL } from 'url' // TODO: declare it instead of importing it

export enum AuthenticationTypes {
BasicAuth = 'basic',
@@ -25,6 +28,7 @@ export interface Config {
authentication?: Authentication,
retries?: number,
timeout?: Seconds,
onError?: 'reject' | 'resolve',
}

export interface Portal {

+ 20
- 5
src/method.ts View File

@@ -1,7 +1,12 @@
import { Client } from './client'

import defaults from 'lodash.defaultsdeep'
import { ValdiationSpec, ensureValidData, Validator } from './validation';
import {
parse as parseQuery,
stringify as stringifyQuery,
} from 'querystring'

import { ValdiationSpec, ensureValidData } from './validation';

const applicationJson = 'application/json'

@@ -13,7 +18,7 @@ export interface MethodSpec {
queryString?: ValdiationSpec,
contentType?: string,
accept?: string,
strict?: boolean,
// strict?: boolean,
}

export function method (client: Client) {
@@ -72,7 +77,7 @@ export function method (client: Client) {

let paramsCount = (path.match(/:[^\/]*/) || []).length

const fullPath = args.reduce((acc, arg) => {
let fullPath: string = args.reduce((acc, arg) => {
paramsCount -= 1

return acc.replace(/:[^\/]*/, arg)
@@ -80,10 +85,20 @@ export function method (client: Client) {

if (paramsCount !== 0) throw new Error('TODO: give me a meangingful error')

// TODO: add query
if (query) {
let attachedQuery
[ fullPath, attachedQuery ] = fullPath.split('?', 2)

options = defaults({}, defaultOptions, options)
query = {
...query,
...parseQuery(attachedQuery),
}

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

options = defaults({}, defaultOptions, options)

return client.request(method, fullPath, payload, options)
}

Loading…
Cancel
Save