Browse Source

refactor typing in resourcefactory

tags/v1.0.0-alpha.0
ojizero 1 year ago
parent
commit
9ec98ab906
No account linked to committer's email address
3 changed files with 30 additions and 26 deletions
  1. 2
    1
      package.json
  2. 24
    21
      src/resource.ts
  3. 4
    4
      test/resource.spec.ts

+ 2
- 1
package.json View File

@@ -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",

+ 24
- 21
src/resource.ts View File

@@ -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 } = {
},
}

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 {
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 {
}

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)
}
}

+ 4
- 4
test/resource.spec.ts View File

@@ -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', () => {
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', () => {

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', () => {

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