forked from lowlighter/metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
150 lines (133 loc) · 4.31 KB
/
index.mjs
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//Imports
import faker from "@faker-js/faker"
import fs from "fs/promises"
import axios from "axios"
import paths from "path"
import rss from "rss-parser"
import urls from "url"
//Mocked state
let mocked = false
//Mocking
export default async function({ graphql, rest }) {
//Check if already mocked
if (mocked)
return { graphql, rest }
mocked = true
console.debug("metrics/compute/mocks > mocking")
//Load mocks
const __mocks = paths.join(paths.dirname(urls.fileURLToPath(import.meta.url)))
const mock = async ({ directory, mocks }) => {
for (const entry of await fs.readdir(directory)) {
if ((await fs.lstat(paths.join(directory, entry))).isDirectory()) {
if (!mocks[entry])
mocks[entry] = {}
await mock({ directory: paths.join(directory, entry), mocks: mocks[entry] })
}
else {
mocks[entry.replace(/[.]mjs$/, "")] = (await import(urls.pathToFileURL(paths.join(directory, entry)).href)).default
}
}
return mocks
}
const mocks = await mock({ directory: paths.join(__mocks, "api"), mocks: {} })
//GraphQL API mocking
{
//Unmocked
console.debug("metrics/compute/mocks > mocking graphql api")
const unmocked = graphql
//Mocked
graphql = new Proxy(unmocked, {
apply(target, that, args) {
//Arguments
const [query] = args
const login = query.match(/login: "(?<login>.*?)"/)?.groups?.login ?? faker.internet.userName()
//Search for mocked query
for (const mocked of Object.keys(mocks.github.graphql)) {
if (new RegExp(`^query ${mocked.replace(/([.]\w)/g, (_, g) => g.toLocaleUpperCase().substring(1)).replace(/^(\w)/g, (_, g) => g.toLocaleUpperCase())} `).test(query))
return mocks.github.graphql[mocked]({ faker, query, login })
}
//Unmocked call
return target(...args)
},
})
}
//Rest API mocking
{
//Unmocked
console.debug("metrics/compute/mocks > mocking rest api")
const unmocked = {}
//Mocked
const mocker = ({ path = "rest", mocks, mocked }) => {
for (const [key, value] of Object.entries(mocks)) {
console.debug(`metrics/compute/mocks > mocking rest api > mocking ${path}.${key}`)
if (typeof value === "function") {
unmocked[path] = value
mocked[key] = new Proxy(unmocked[path], { apply: value.bind(null, { faker }) })
}
else {
mocker({ path: `${path}.${key}`, mocks: mocks[key], mocked: mocked[key] })
}
}
}
mocker({ mocks: mocks.github.rest, mocked: rest })
}
//Axios mocking
{
//Unmocked
console.debug("metrics/compute/mocks > mocking axios")
const unmocked = { get: axios.get, post: axios.post }
//Mocked post requests
axios.post = new Proxy(unmocked.post, {
apply(target, that, args) {
//Arguments
const [url, body] = args
//Search for mocked request
for (const service of Object.keys(mocks.axios.post)) {
const mocked = mocks.axios.post[service]({ faker, url, body })
if (mocked)
return mocked
}
//Unmocked call
return target(...args)
},
})
//Mocked get requests
axios.get = new Proxy(unmocked.get, {
apply(target, that, args) {
//Arguments
const [url, options] = args
//Search for mocked request
for (const service of Object.keys(mocks.axios.get)) {
const mocked = mocks.axios.get[service]({ faker, url, options })
if (mocked)
return mocked
}
//Unmocked call
return target(...args)
},
})
}
//RSS mocking
{
//Unmocked
console.debug("metrics/compute/mocks > mocking rss-parser")
//Mock rss feed
rss.prototype.parseURL = function(url) {
console.debug(`metrics/compute/mocks > mocking rss feed result > ${url}`)
return ({
items: new Array(30).fill(null).map(_ => ({
title: faker.lorem.sentence(),
link: faker.internet.url(),
content: faker.lorem.paragraphs(),
contentSnippet: faker.lorem.paragraph(),
isoDate: faker.date.recent(),
})),
title: faker.lorem.words(),
description: faker.lorem.paragraph(),
link: url,
})
}
}
//Return mocked elements
return { graphql, rest }
}