Browse Source

more on types hell

tags/v1.0.0-alpha.0
ojizero 1 year ago
parent
commit
14a4e1d753
No account linked to committer's email address
3 changed files with 46 additions and 13 deletions
  1. 12
    7
      src/client.ts
  2. 2
    2
      src/method.ts
  3. 32
    4
      test/client.spec.ts

+ 12
- 7
src/client.ts View File

@@ -50,16 +50,17 @@ export interface Response {

// Add compatibility with Got type
export interface RequestOptions extends HttpsRequestOptions {
baseUrl: string,
url: string,
json: boolean,
body: string,
baseUrl?: string,
url?: string,
json?: boolean,
body?: string,
retries?: number,
throwHttpErrors: boolean,
throwHttpErrors?: boolean,
headers?: OutgoingHttpHeaders,
}

export interface Config {
baseUrl: string,
baseUrl?: string, // TODO: this shouldn't be optional but i can construct RequestConfig unless i set it as optional
// protocol?: 'http' | 'https',
// port?: number,
headers?: OutgoingHttpHeaders,
@@ -72,16 +73,20 @@ export interface Config {
export type ClientFn = (options: RequestOptions) => Promise<RawResponse>

export interface RequestConfig extends RequestOptions, Config {}
// export type RequestConfig = Config

export interface Client {
request (method: string, path: string, payload: {}, options: any): Promise<Response>
}

export class PortalClient implements Client {
client: ClientFn
config: Config
client: ClientFn

constructor (client: ClientFn, config: Config) {
// TODO: this shouldn't exists but i had to set baseUrl as otpional to construc the RequestConfig type
if (typeof config.baseUrl === 'undefined') throw Error('TODO: givem em a meaningful message')

this.client = client
this.config = config
}

+ 2
- 2
src/method.ts View File

@@ -1,4 +1,4 @@
import { Client, Response } from './client'
import { Client, Response, RequestConfig } from './client'

import defaults from 'lodash.defaultsdeep'
import {
@@ -38,7 +38,7 @@ export function method (client: Client): MethodFactory {

const method = _method.toUpperCase()

const defaultOptions = {
const defaultOptions: RequestConfig = {
headers: {
'Accept': accept,
'Content-Type': contentType,

+ 32
- 4
test/client.spec.ts View File

@@ -49,11 +49,39 @@ describe('Client', () => {
})

it('transforms request parameters with all defaults', () => {
//
const requestOptions = client.constructRequestOptions('GET', '/some-path', {}, {})

expect(requestOptions).to.deep.equal({
baseUrl: "https://dummy.domain",
body: "{}",
headers: {},
json: false,
method: "GET",
retries: 0,
throwHttpErrors: true,
timeout: 30000,
url: "/some-path",
})
})

it('adds additional headers from options', () => {
//
const requestOptions = client.constructRequestOptions('GET', '/some-path', {}, {
headers: {
some: 'mock'
}
})

expect(requestOptions).to.deep.equal({
baseUrl: "https://dummy.domain",
body: "{}",
headers: { some: 'mock' },
json: false,
method: "GET",
retries: 0,
throwHttpErrors: true,
timeout: 30000,
url: "/some-path",
})
})

describe('Authentication setup', () => {
@@ -63,11 +91,11 @@ describe('Client', () => {
})

it('adds basic authentication', () => {
//
// TODO:
})

it('adds bearer authentication', () => {
//
// TODO:
})
})
})

Loading…
Cancel
Save