Browse Source

refactor typing in resourcefactory

modified-method-api
ojizero 4 years ago
parent
commit
9ec98ab906
No known key found for this signature in database
GPG Key ID: FEBB7097845B0C7E
  1. 3
      package.json
  2. 45
      src/resource.ts
  3. 8
      test/resource.spec.ts

3
package.json

@ -11,7 +11,8 @@ @@ -11,7 +11,8 @@
"start": "npm test -- --watch --recursive",
"test": "TS_NODE_PROJECT=./tsconfig.spec.json mocha",
"clean": "rm -rf lib",
"build": "tsc"
"build": "tsc",
"repl": "ts-node"
},
"repository": {
"type": "git",

45
src/resource.ts

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
import { Client } from './client'
import method, { MethodSpec } from './method'
import method, { MethodSpec, MethodFactory } from './method'
const defaultBaseSpecs: { [k: string]: MethodSpec } = {
list: {
@ -24,33 +24,33 @@ const defaultBaseSpecs: { [k: string]: MethodSpec } = { @@ -24,33 +24,33 @@ const defaultBaseSpecs: { [k: string]: MethodSpec } = {
},
}
export type ResourceFactory =
(baseRoute: string, enabledRoutes?: string[], extraMethods?: { [k:string]: any }) => Resource
export interface ResourceConfig {
baseRoute: string,
enabledRoutes?: string[],
extraMethods?: { [k:string]: MethodSpec },
}
export type ResourceFactory = (config: ResourceConfig) => Resource
export class Resource {
client: Client
baseRoute: string
enabledRoutes: string[]
methodFactory: any
constructor (
client: Client,
baseRoute: string,
enabledRoutes?: string[],
extraMethods?: { [k: string]: any },
) {
methodFactory: MethodFactory
constructor (client: Client, config: ResourceConfig) {
const {
baseRoute,
extraMethods = {},
enabledRoutes = Object.keys(defaultBaseSpecs),
} = config
this.client = client
this.baseRoute = baseRoute.trim()
if (this.baseRoute.endsWith('/')) this.baseRoute = this.baseRoute.slice(0, -1)
this.enabledRoutes = enabledRoutes || [
'list',
'get',
'edit',
'add',
'del',
]
this.enabledRoutes = enabledRoutes
this.methodFactory = this.generateMethodFactory()
@ -108,7 +108,10 @@ export class Resource { @@ -108,7 +108,10 @@ export class Resource {
if (!extraMethods) return
Object.entries(extraMethods)
.forEach(([method, fn]) => {
.forEach(([method, spec]) => {
// TODO: should it be prefixed by basePath ?
const fn = this.methodFactory(spec)
Object.defineProperty(this, method, {
value: fn,
writable: false,
@ -119,7 +122,7 @@ export class Resource { @@ -119,7 +122,7 @@ export class Resource {
}
export default function resrouceGenerator (client: Client): ResourceFactory {
return function (baseRoute: string, enabledRoutes?: string[], extraMethods?: { [k:string]: any }): Resource {
return new Resource(client, baseRoute, enabledRoutes, extraMethods)
return function (config: ResourceConfig): Resource {
return new Resource(client, config)
}
}

8
test/resource.spec.ts

@ -4,8 +4,8 @@ import resourceGenerator from '../src/resource' @@ -4,8 +4,8 @@ import resourceGenerator from '../src/resource'
describe('Resource', () => {
let client
let resourceFactory
let resource
let resourceFactory
before (() => {
resourceFactory = resourceGenerator(client)
@ -15,7 +15,7 @@ describe('Resource', () => { @@ -15,7 +15,7 @@ describe('Resource', () => {
it('exposes basic CRUD API', () => {
client = { request: sinon.spy() }
resource = resourceFactory('/mock-base')
resource = resourceFactory({ baseRoute: '/mock-base' })
expect(resource)
.to.include.all.keys('list', 'get', 'edit', 'del')
@ -26,7 +26,7 @@ describe('Resource', () => { @@ -26,7 +26,7 @@ describe('Resource', () => {
resourceFactory = resourceGenerator(client)
resource = resourceFactory('/mock-base', undefined, { extraMethod: () => {} })
resource = resourceFactory({ baseRoute: '/mock-base', extraMethods: { extraMethod: { path: '/some-path' } } })
expect(resource)
.to.include.all.keys('list', 'get', 'edit', 'del', 'extraMethod')
@ -37,7 +37,7 @@ describe('Resource', () => { @@ -37,7 +37,7 @@ describe('Resource', () => {
resourceFactory = resourceGenerator(client)
resource = resourceFactory('/mock-base', ['list', 'get'])
resource = resourceFactory({ baseRoute: '/mock-base', enabledMethods: ['list', 'get']})
expect(resource)
.to.include.all.keys('list', 'get')

Loading…
Cancel
Save