Browse Source

more on types hell

modified-method-api
ojizero 3 years ago
parent
commit
14a4e1d753
No known key found for this signature in database
GPG Key ID: FEBB7097845B0C7E
  1. 19
      src/client.ts
  2. 4
      src/method.ts
  3. 36
      test/client.spec.ts

19
src/client.ts

@ -50,16 +50,17 @@ export interface Response { @@ -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 { @@ -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
}

4
src/method.ts

@ -1,4 +1,4 @@ @@ -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 { @@ -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,

36
test/client.spec.ts

@ -49,11 +49,39 @@ describe('Client', () => { @@ -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', () => { @@ -63,11 +91,11 @@ describe('Client', () => {
})
it('adds basic authentication', () => {
//
// TODO:
})
it('adds bearer authentication', () => {
//
// TODO:
})
})
})

Loading…
Cancel
Save