Skip to content
This repository was archived by the owner on Feb 8, 2024. It is now read-only.

Commit 0177d09

Browse files
committed
Refactor check for routes instance
1 parent cebb9bb commit 0177d09

5 files changed

Lines changed: 18 additions & 4 deletions

File tree

lib/flatten.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isCreateRestInstance } from './index'
2+
13
/**
24
* Recursive flatten
35
* @private
@@ -54,5 +56,8 @@ function flattenize(routes, prefix, parent) {
5456
* )
5557
*/
5658
export function flattenRoutes(routes) {
59+
if (!isCreateRestInstance(routes)) {
60+
throw new Error('You can flatten only createRest routes')
61+
}
5762
return flattenize(routes, '', { before: [], after: [] })
5863
}

lib/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createRestInstanceSymbol } from './symbol'
12

23
/**
34
* @desc Simple object for `resource()`
@@ -26,7 +27,7 @@
2627
* }
2728
*/
2829

29-
export const isCreateRestInstance = Symbol('isCreateRestInstance')
30+
export const isCreateRestInstance = routing => routing[createRestInstanceSymbol] === true
3031

3132
/**
3233
* @desc Routes object
@@ -62,7 +63,7 @@ export class Maker {
6263
Object.keys(this.ctx.scoped).forEach(name => {
6364
scoped[name] = this.ctx.scoped[name].build()
6465
})
65-
return Object.assign({}, this.ctx, { scoped, [isCreateRestInstance]: true })
66+
return Object.assign({}, this.ctx, { scoped, [createRestInstanceSymbol]: true })
6667
}
6768

6869
/**

lib/symbol.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const createRestInstanceSymbol = Symbol('createRestInstance')

test/flatten.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,9 @@ avaTest('Creates local, scoped methods and methods in scope', test => {
9696
}
9797
)
9898
})
99+
100+
avaTest('Fail if passed not routes', test => {
101+
test.throws(() => {
102+
flattenRoutes({})
103+
})
104+
})

test/index.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import avaTest from 'ava'
2-
import { createRest, isCreateRestInstance } from '../lib'
2+
import { createRest } from '../lib'
3+
import { createRestInstanceSymbol } from '../lib/symbol'
34

45

56
const get = () => {}
@@ -22,7 +23,7 @@ const make = (before = [], after = [], local = {}, scoped = {}) => ({
2223
after,
2324
local,
2425
scoped,
25-
[isCreateRestInstance]: true,
26+
[createRestInstanceSymbol]: true,
2627
})
2728

2829
avaTest('Creates base structure', test => {

0 commit comments

Comments
 (0)