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

Commit 7fb7f05

Browse files
committed
Resources small example file
1 parent 21e5e20 commit 7fb7f05

1 file changed

Lines changed: 133 additions & 0 deletions

File tree

examples/resources.js

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
const stringify = require('stringify-object')
2+
const chalk = require('chalk')
3+
4+
const { createRest, flattenRoutes, printRoutes } = require('../dist')
5+
6+
/**
7+
* ''
8+
* '/foo'
9+
*
10+
* 'some'
11+
* '/foo/some
12+
*/
13+
14+
const exampleAT = {
15+
demo: {
16+
before: [],
17+
after: [],
18+
methods: {
19+
POST: [() => {}],
20+
PUT: [],
21+
},
22+
scoped: {
23+
foo: {
24+
before: [() => {}],
25+
after: [],
26+
methods: {
27+
POST: [() => {}],
28+
PUT: [],
29+
},
30+
},
31+
},
32+
},
33+
}
34+
35+
// ========================================================================= //
36+
// ========================================================================= //
37+
38+
const TestsController = {
39+
beforeEach() {},
40+
afterEach() {},
41+
index() {},
42+
read() {},
43+
create() {},
44+
update() {},
45+
patch() {},
46+
destroy() {},
47+
}
48+
49+
const before1 = function before1() {
50+
console.log('before1()')
51+
}
52+
const before2 = function before2() {
53+
console.log('before2()')
54+
}
55+
const before3 = function before3() {
56+
console.log('before3()')
57+
}
58+
const after1 = function after1() {
59+
console.log('after1()')
60+
}
61+
const after2 = function after2() {
62+
console.log('after2()')
63+
}
64+
const after3 = function after3() {
65+
console.log('after3()')
66+
}
67+
const post1 = function post1() {
68+
console.log('post1()')
69+
}
70+
const get1 = function get1() {
71+
console.log('get1()')
72+
}
73+
const get2 = function get2() {
74+
console.log('get2()')
75+
}
76+
const get3 = function get3() {
77+
console.log('get3()')
78+
}
79+
const put3 = function put3() {
80+
console.log('put3()')
81+
}
82+
83+
const routes = createRest(root => {
84+
// root.beforeEach(before1)
85+
// root.afterEach(after1)
86+
87+
root.post('/', post1)
88+
89+
root.scope('demo', demo => {
90+
// demo.crud('bar', TestsController, {}, bar => {
91+
// bar.get('baz', get3)
92+
// })
93+
demo.get('bar', get3)
94+
})
95+
root.scope('demo', demo => {
96+
demo.get('baz', get2)
97+
})
98+
// root.resources('tests', TestsController)
99+
})
100+
101+
const strf = (data, indent = ' ') => stringify(data, {
102+
indent,
103+
transform(obj, prop, original) {
104+
if (/^function/.test(original)) {
105+
return chalk.blue(
106+
original
107+
.replace(/\n+/mg, '')
108+
.replace(/\s+/mg, ' ')
109+
.replace(/^\w+\s+(\w+)\(\).*/mg, `$1()`)
110+
)
111+
}
112+
113+
return original
114+
},
115+
})
116+
117+
console.log(strf(routes))
118+
119+
const flat = flattenRoutes(routes)
120+
Object.keys(flat).forEach(path => {
121+
const mt = flat[path]
122+
Object.keys(mt).forEach(method => {
123+
console.log(
124+
chalk.green(`${method} ${path}`),
125+
' >> ',
126+
mt[method].map(fn => `${chalk.yellow(fn.name)}()`).join(', ')
127+
)
128+
})
129+
})
130+
131+
console.log('\n---\n')
132+
133+
printRoutes(routes)

0 commit comments

Comments
 (0)