Bläddra i källkod

restructure method, split client into interface and implementation

tags/v1.0.0-alpha.0
ojizero 1 år sedan
förälder
incheckning
5aa0d9460c
Inget konto är kopplat till bidragsgivarens mejladress
10 ändrade filer med 152 tillägg och 35 borttagningar
  1. 1
    1
      package.json
  2. 15
    1
      src/client.ts
  3. 11
    9
      src/index.ts
  4. 41
    1
      src/method.ts
  5. 5
    1
      test/client.spec.ts
  6. 57
    1
      test/method.spec.ts
  7. 14
    0
      test/mocks/client.ts
  8. 4
    0
      test/setup.ts
  9. 1
    1
      tsconfig.json
  10. 3
    20
      tsconfig.spec.json

+ 1
- 1
package.json Visa fil

@@ -9,7 +9,7 @@
"scripts": {
"prepare": "npm run clean && npm run build",
"start": "npm test -- --watch --recursive",
"test": "mocha",
"test": "TS_NODE_PROJECT=./tsconfig.spec.json mocha",
"clean": "rm -rf lib",
"build": "tsc"
},

+ 15
- 1
src/client.ts Visa fil

@@ -1 +1,15 @@
export default class Client {}
export interface Client {
request (method: string, path: string, options: any): void // TODO:
}

export class PortalClient implements Client {
constructor () {
// TODO:
}

request (method: string, path: string, options: any) {
// TODO:
}
}

export default PortalClient

+ 11
- 9
src/index.ts Visa fil

@@ -1,5 +1,5 @@
import Client from './client'
import Method from './method'
// import method from './method'
import Resource from './resource'

export enum AuthenticationTypes {
@@ -28,8 +28,8 @@ export interface Config {
}

export interface Portal {
route: Method, // TODO: function taking some spec
resource: Resource, // TODO: function taking some spec
// route: method, // TODO: function taking some spec
// resource: Resource, // TODO: function taking some spec
_client: Client,
}

@@ -43,15 +43,17 @@ export interface Response {
_rawResponse: string, // raw string response
}

export function createBaseClient (config: Config) /*: Portal*/ {
export function createBaseClient (config: Config): Portal {
const client = new Client()

// const portal: Portal = {
// _client: client,
// }
const portal: Portal = {
_client: client,
}

// return portal
return client
return portal
}

export { Client } from './client'
export { MethodSpec } from './method'

export default createBaseClient

+ 41
- 1
src/method.ts Visa fil

@@ -1 +1,41 @@
export default class Method {}
import { Client } from './client'

const applicationJson = 'application/json'

export interface MethodSpec {
path: string,
method?: string,
body?: {},
queryString?: {},
contentType?: string,
accept?: string,
strict?: boolean,
}

export function method (client: Client) {
return function methodGenerator (spec: MethodSpec) {
const {
path,
method = 'GET',
body = {},
queryString = {},
contentType = applicationJson,
accept = applicationJson,
strict = true
} = spec

// It's ok for args here to be of implicit type any[]
// @ts-ignore
return function (...args) {
// TODO: replace path params with args
// TODO: reject if args doesn't match path params if strict

// TODO: last argument is allowed to be object, JSON payload
// TODO: reject if body doesn't match body spec if strict

return client.request(method, path, {})
}
}
}

export default method

+ 5
- 1
test/client.spec.ts Visa fil

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

describe('Client', () => {
// TODO:
let client

before(() => {
client = new Client()
})
})

+ 57
- 1
test/method.spec.ts Visa fil

@@ -1,3 +1,59 @@
/// <reference path='typings/globals.d.ts' />

import method from '../src/method'

import { Client } from '../src/client'
import { MockClient } from './mocks/client'

const mockMethodSpec = {}

describe('Method', () => {
// TODO:
let mockClient: Client
let methodGenerator
let methodFunction

before(() => {
mockClient = new MockClient()
})

it('accepts client and returns generator function', () => {
methodGenerator = method(mockClient)

expect(true).to.deep.equal(true)
// expect(methodFunction).to.be.a.function() // TODO:
})

it('accepts a method specification and returns a method function', () =>{
methodFunction = methodGenerator(mockMethodSpec)

// expect(methodFunction).to.be.a.function() // TODO:
})

describe('Method function', () => {
it('calls underlying request on client', () => {
methodFunction()

// expect(someSpy).to.be.called.once.with.exaclty() // TODO:
})

it('requires path parameters if specified', () => {
// methodFunction = undefined
// methodFunction() // should throw
})

it('passes path parameters if specified', () => {
// methodFunction = undefined
// methodFunction(1) // should not throw
})

it('requires body arguments if specified', () => {
// methodFunction = undefined
// methodFunction() // should throw
})

it('passes body arguments if specified', () => {
// methodFunction = undefined
// methodFunction({ some: 'payload' })
})
})
})

+ 14
- 0
test/mocks/client.ts Visa fil

@@ -0,0 +1,14 @@
import { Client } from '../../src/client'


export class MockClient implements Client {
constructor () {
//
}

request (method: string, options: any) {
//
}
}

// TODO: export spies as well

+ 4
- 0
test/setup.ts Visa fil

@@ -7,3 +7,7 @@ chai.use(chaiAsPromised)

global.chai = chai
global.expect = chai.expect

console.log('**************')
console.log('* SETUP DONE *')
console.log('**************')

+ 1
- 1
tsconfig.json Visa fil

@@ -20,7 +20,7 @@

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "noImplicitAny": false, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */

+ 3
- 20
tsconfig.spec.json Visa fil

@@ -1,23 +1,6 @@
{
"extends": "./tsconfig.json",
// "compilerOptions": {
// "sourceMap": false,
// "allowJs": false,
// "declaration": false,
// "rootDir": ".",
// "target": "ES6",
// "outDir": "./_tests",
// "moduleResolution": "node",
// "lib": [
// "ES6",
// "ES2015",
// "ES2017.object"
// ]
// },
"include": [
"typings/*",
"test/typings/*",
"src/**/*",
"test/**/*"
]
"compilerOptions": {
"noImplicitAny": false
}
}

Laddar…
Avbryt
Spara