-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuilder.ts
More file actions
55 lines (44 loc) · 1.24 KB
/
builder.ts
File metadata and controls
55 lines (44 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { onUser, onProject } from './nodes'
import { User, Project } from './queries'
interface INode {
user: (...args: any) => string,
project: (...args: any) => string
}
interface IQuery {
user: (...args: any) => string,
project: (...args: any) => string
}
export default class QueryBuilder {
private _query: string
private _nodeTitleMap: INode = {
user: onUser,
project: onProject
}
private _queryTitleMap: IQuery = {
user: User,
project: Project
}
on (node: keyof INode, params: Record<string, string | number>) {
const args = Object.entries(params).map(([, val]) => val)
const nodeFragment = this._nodeTitleMap[node](...args)
const regex = new RegExp(node, 'g')
this._query = this._query.replace(regex, nodeFragment)
return this
}
get query () {
return this._query
}
request (query: keyof IQuery, params: Record<string, string | number>) {
const args = Object.entries(params).map(([, val]) => val)
this._query = this._queryTitleMap[query](...args)
return this
}
}
const QB = new QueryBuilder()
const query = QB.request('user', {
fields: 'id email age project',
login: 'ioedeveloper'
}).on('project', {
fields: 'name dateCreated'
}).query
console.log('query: ', query)